Skip to content

Commit e54b937

Browse files
authored
Detect the number of sockets when needed (#1781)
Signed-off-by: yiliu30 <[email protected]>
1 parent a6f8ff7 commit e54b937

File tree

2 files changed

+71
-21
lines changed

2 files changed

+71
-21
lines changed

neural_compressor/utils/utility.py

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,28 @@ def __init__(self):
249249
b"\xB8\x07\x00\x00\x00" b"\x0f\xa2" b"\xC3", # mov eax, 7 # cpuid # ret
250250
)
251251
self._bf16 = bool(eax & (1 << 5))
252-
if "arch" in info and "ARM" in info["arch"]: # pragma: no cover
253-
self._sockets = 1
254-
else:
255-
self._sockets = self.get_number_of_sockets()
256-
self._cores = psutil.cpu_count(logical=False)
257-
self._cores_per_socket = int(self._cores / self._sockets)
252+
self._info = info
253+
# detect the below info when needed
254+
self._cores = None
255+
self._sockets = None
256+
self._cores_per_socket = None
257+
258+
@staticmethod
259+
def _detect_cores():
260+
physical_cores = psutil.cpu_count(logical=False)
261+
return physical_cores
262+
263+
@property
264+
def cores(self):
265+
"""Get the number of cores in platform."""
266+
if self._cores is None:
267+
self._cores = self._detect_cores()
268+
return self._cores
269+
270+
@cores.setter
271+
def cores(self, num_of_cores):
272+
"""Set the number of cores in platform."""
273+
self._cores = num_of_cores
258274

259275
@property
260276
def bf16(self):
@@ -267,30 +283,55 @@ def vnni(self):
267283
return self._vnni
268284

269285
@property
270-
def cores_per_socket(self):
286+
def cores_per_socket(self) -> int:
271287
"""Get the cores per socket."""
288+
if self._cores_per_socket is None:
289+
self._cores_per_socket = self.cores // self.sockets
272290
return self._cores_per_socket
273291

274-
def get_number_of_sockets(self) -> int:
275-
"""Get number of sockets in platform."""
292+
@property
293+
def sockets(self):
294+
"""Get the number of sockets in platform."""
295+
if self._sockets is None:
296+
self._sockets = self._get_number_of_sockets()
297+
return self._sockets
298+
299+
@sockets.setter
300+
def sockets(self, num_of_sockets):
301+
"""Set the number of sockets in platform."""
302+
self._sockets = num_of_sockets
303+
304+
def _get_number_of_sockets(self) -> int:
305+
if "arch" in self._info and "ARM" in self._info["arch"]: # pragma: no cover
306+
return 1
307+
308+
num_sockets = None
276309
cmd = "cat /proc/cpuinfo | grep 'physical id' | sort -u | wc -l"
277310
if psutil.WINDOWS:
278311
cmd = r'wmic cpu get DeviceID | C:\Windows\System32\find.exe /C "CPU"'
279312
elif psutil.MACOS: # pragma: no cover
280313
cmd = "sysctl -n machdep.cpu.core_count"
281314

282-
with subprocess.Popen(
283-
args=cmd,
284-
shell=True,
285-
stdout=subprocess.PIPE,
286-
stderr=subprocess.STDOUT,
287-
universal_newlines=False,
288-
) as proc:
289-
proc.wait()
290-
if proc.stdout:
291-
for line in proc.stdout:
292-
return int(line.decode("utf-8", errors="ignore").strip())
293-
return 0
315+
num_sockets = None
316+
try:
317+
with subprocess.Popen(
318+
args=cmd,
319+
shell=True,
320+
stdout=subprocess.PIPE,
321+
stderr=subprocess.STDOUT,
322+
universal_newlines=False,
323+
) as proc:
324+
proc.wait()
325+
if proc.stdout:
326+
for line in proc.stdout:
327+
num_sockets = int(line.decode("utf-8", errors="ignore").strip())
328+
except Exception as e:
329+
logger.error("Failed to get number of sockets: %s" % e)
330+
if isinstance(num_sockets, int) and num_sockets >= 1:
331+
return num_sockets
332+
else:
333+
logger.warning("Failed to get number of sockets, return 1 as default.")
334+
return 1
294335

295336

296337
def dump_elapsed_time(customized_msg=""):

test/utils/test_cpu_info.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from neural_compressor.utils.utility import CpuInfo
2+
3+
4+
class TestCPUInfo:
5+
def test_get_cpu_info(self):
6+
cpu_info = CpuInfo()
7+
assert cpu_info.cores >= 1
8+
assert cpu_info.sockets >= 1
9+
assert cpu_info.cores_per_socket >= 1

0 commit comments

Comments
 (0)