| 196 | |
| 197 | |
| 198 | class DeviceManager: |
| 199 | def __init__(self): |
| 200 | self.device_type = self._detect_device_type() |
| 201 | self._setup_device_module() |
| 202 | |
| 203 | def _is_torch_npu_available(self) -> bool: |
| 204 | try: |
| 205 | if hasattr(torch, "npu") and callable(getattr(torch.npu, "is_available", None)): |
| 206 | return torch.npu.is_available() |
| 207 | else: |
| 208 | return False |
| 209 | except ImportError: |
| 210 | return False |
| 211 | |
| 212 | def _detect_device_type(self) -> str: |
| 213 | if self._is_torch_npu_available(): |
| 214 | return "npu" |
| 215 | elif torch.cuda.is_available(): |
| 216 | return "cuda" |
| 217 | else: |
| 218 | raise TypeError("The current device type is not supported") |
| 219 | |
| 220 | def _setup_device_module(self): |
| 221 | if self.device_type == "npu": |
| 222 | import torch_npu |
| 223 | |
| 224 | self.device_module = torch_npu.npu |
| 225 | elif self.device_type == "cuda": |
| 226 | self.device_module = torch.cuda |
| 227 | else: |
| 228 | raise TypeError("The current device type is not supported") |
| 229 | |
| 230 | @property |
| 231 | def backend(self) -> str: |
| 232 | if self.device_type == "npu": |
| 233 | return "hccl" |
| 234 | elif self.device_type == "cuda": |
| 235 | return "nccl" |
| 236 | else: |
| 237 | raise TypeError("The current device type is not supported") |
| 238 | |
| 239 | @property |
| 240 | def transfer_engine_protocol(self) -> str: |
| 241 | if self.device_type == "npu": |
| 242 | return "ascend_direct" |
| 243 | elif self.device_type == "cuda": |
| 244 | if has_efa_pci(): |
| 245 | return "efa" |
| 246 | else: |
| 247 | return "rdma" |
| 248 | else: |
| 249 | raise TypeError("The current device type is not supported") |
| 250 | |
| 251 | def rdma_device(self, rank: int) -> str: |
| 252 | if self.transfer_engine_protocol == "ascend_direct": |
| 253 | return "" |
| 254 | elif self.transfer_engine_protocol in ["rdma", "efa"]: |
| 255 | return _get_my_rdma_device(rank, self.device_module.device_count(), _get_rdma_devices()) |
no outgoing calls
no test coverage detected