@@ -249,12 +249,28 @@ def __init__(self):
249
249
b"\xB8 \x07 \x00 \x00 \x00 " b"\x0f \xa2 " b"\xC3 " , # mov eax, 7 # cpuid # ret
250
250
)
251
251
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
258
274
259
275
@property
260
276
def bf16 (self ):
@@ -267,30 +283,55 @@ def vnni(self):
267
283
return self ._vnni
268
284
269
285
@property
270
- def cores_per_socket (self ):
286
+ def cores_per_socket (self ) -> int :
271
287
"""Get the cores per socket."""
288
+ if self ._cores_per_socket is None :
289
+ self ._cores_per_socket = self .cores // self .sockets
272
290
return self ._cores_per_socket
273
291
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
276
309
cmd = "cat /proc/cpuinfo | grep 'physical id' | sort -u | wc -l"
277
310
if psutil .WINDOWS :
278
311
cmd = r'wmic cpu get DeviceID | C:\Windows\System32\find.exe /C "CPU"'
279
312
elif psutil .MACOS : # pragma: no cover
280
313
cmd = "sysctl -n machdep.cpu.core_count"
281
314
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
294
335
295
336
296
337
def dump_elapsed_time (customized_msg = "" ):
0 commit comments