@@ -98,6 +98,49 @@ def are_libraries_initialized(*library_names: str) -> list[str]:
9898 return [lib_name for lib_name in library_names if lib_name in sys .modules .keys ()]
9999
100100
101+ def get_current_device_type () -> tuple [str , str ]:
102+ """
103+ Determines the current device type and distributed type without initializing any device.
104+
105+ This is particularly important when using fork-based multiprocessing, as device initialization
106+ before forking can cause errors.
107+
108+ The device detection order follows the same priority as state.py:_prepare_backend():
109+ MLU -> SDAA -> MUSA -> NPU -> HPU -> CUDA -> XPU
110+
111+ Returns:
112+ tuple[str, str]: A tuple of (device_type, distributed_type)
113+ - device_type: The device string (e.g., "cuda", "npu", "xpu")
114+ - distributed_type: The distributed type string (e.g., "MULTI_GPU", "MULTI_NPU")
115+
116+ Example:
117+ ```python
118+ >>> device_type, distributed_type = get_current_device_type()
119+ >>> print(device_type) # "cuda"
120+ >>> print(distributed_type) # "MULTI_GPU"
121+ ```
122+ """
123+ from .imports import is_hpu_available , is_mlu_available , is_musa_available , is_npu_available , is_sdaa_available , is_xpu_available
124+
125+ if is_mlu_available ():
126+ return "mlu" , "MULTI_MLU"
127+ elif is_sdaa_available ():
128+ return "sdaa" , "MULTI_SDAA"
129+ elif is_musa_available ():
130+ return "musa" , "MULTI_MUSA"
131+ elif is_npu_available ():
132+ return "npu" , "MULTI_NPU"
133+ elif is_hpu_available ():
134+ return "hpu" , "MULTI_HPU"
135+ elif torch .cuda .is_available ():
136+ return "cuda" , "MULTI_GPU"
137+ elif is_xpu_available ():
138+ return "xpu" , "MULTI_XPU"
139+ else :
140+ # Default to CUDA even if not available (for CPU-only scenarios where CUDA code paths are still used)
141+ return "cuda" , "MULTI_GPU"
142+
143+
101144def _nvidia_smi ():
102145 """
103146 Returns the right nvidia-smi command based on the system.
@@ -248,7 +291,7 @@ def override_numa_affinity(local_process_index: int, verbose: Optional[bool] = N
248291
249292 if not is_pynvml_available ():
250293 raise ImportError (
251- "To set CPU affinity on CUDA GPUs the `pynvml ` package must be available. (`pip install pynvml `)"
294+ "To set CPU affinity on CUDA GPUs the `nvidia-ml-py ` package must be available. (`pip install nvidia-ml-py `)"
252295 )
253296 import pynvml as nvml
254297
0 commit comments