MogensR commited on
Commit
62d73e2
·
1 Parent(s): 59aded7

Update utils/hardware/device_manager.py

Browse files
Files changed (1) hide show
  1. utils/hardware/device_manager.py +21 -12
utils/hardware/device_manager.py CHANGED
@@ -3,7 +3,12 @@
3
  Handles device detection, optimization, and hardware compatibility
4
  """
5
 
 
6
  import os
 
 
 
 
7
  import sys
8
  import platform
9
  import subprocess
@@ -171,6 +176,14 @@ def fix_cuda_compatibility(self):
171
  def setup_optimal_threading(self):
172
  """Configure optimal threading for the system"""
173
  try:
 
 
 
 
 
 
 
 
174
  # Get physical CPU count
175
  physical_cores = psutil.cpu_count(logical=False)
176
  if physical_cores is None:
@@ -201,8 +214,10 @@ def setup_optimal_threading(self):
201
  except Exception as e:
202
  logger.warning(f"Error setting up threading: {e}")
203
  # Set safe defaults
204
- os.environ['OMP_NUM_THREADS'] = '4'
205
- os.environ['MKL_NUM_THREADS'] = '4'
 
 
206
 
207
  def get_system_diagnostics(self) -> Dict[str, Any]:
208
  """Get comprehensive system diagnostics"""
@@ -333,17 +348,11 @@ def get_system_diagnostics() -> Dict[str, Any]:
333
 
334
  # Initialize and configure on module import
335
  if __name__ != "__main__":
336
- # When imported, automatically set up threading to avoid the libgomp error
337
  try:
338
- # Ensure OMP_NUM_THREADS is set before any OpenMP operations
339
- if 'OMP_NUM_THREADS' not in os.environ:
340
- # Set a safe default immediately
341
- os.environ['OMP_NUM_THREADS'] = '4'
342
-
343
- # Get the manager instance and configure threading properly
344
  manager = get_device_manager()
 
345
  manager.setup_optimal_threading()
346
  except Exception as e:
347
- logger.warning(f"Error during device manager initialization: {e}")
348
- # Ensure we have safe defaults even if initialization fails
349
- os.environ['OMP_NUM_THREADS'] = '4'
 
3
  Handles device detection, optimization, and hardware compatibility
4
  """
5
 
6
+ # CRITICAL: Set OMP_NUM_THREADS before ANY other imports to prevent libgomp error
7
  import os
8
+ if 'OMP_NUM_THREADS' not in os.environ:
9
+ os.environ['OMP_NUM_THREADS'] = '4'
10
+ os.environ['MKL_NUM_THREADS'] = '4'
11
+
12
  import sys
13
  import platform
14
  import subprocess
 
176
  def setup_optimal_threading(self):
177
  """Configure optimal threading for the system"""
178
  try:
179
+ # Skip if already configured (to avoid overwriting the early setting)
180
+ current_omp = os.environ.get('OMP_NUM_THREADS')
181
+ if current_omp and current_omp.isdigit() and int(current_omp) > 0:
182
+ logger.info(f"Threading already configured: OMP_NUM_THREADS={current_omp}")
183
+ # Just ensure PyTorch uses the same settings
184
+ torch.set_num_threads(int(current_omp))
185
+ return
186
+
187
  # Get physical CPU count
188
  physical_cores = psutil.cpu_count(logical=False)
189
  if physical_cores is None:
 
214
  except Exception as e:
215
  logger.warning(f"Error setting up threading: {e}")
216
  # Set safe defaults
217
+ if 'OMP_NUM_THREADS' not in os.environ:
218
+ os.environ['OMP_NUM_THREADS'] = '4'
219
+ if 'MKL_NUM_THREADS' not in os.environ:
220
+ os.environ['MKL_NUM_THREADS'] = '4'
221
 
222
  def get_system_diagnostics(self) -> Dict[str, Any]:
223
  """Get comprehensive system diagnostics"""
 
348
 
349
  # Initialize and configure on module import
350
  if __name__ != "__main__":
351
+ # When imported, automatically set up the device manager
352
  try:
353
+ # Get the manager instance (threading is already configured at top of file)
 
 
 
 
 
354
  manager = get_device_manager()
355
+ # Only run setup_optimal_threading if needed (it will check internally)
356
  manager.setup_optimal_threading()
357
  except Exception as e:
358
+ logger.warning(f"Error during device manager initialization: {e}")