Skip to content

Commit 5e0eb0d

Browse files
authored
add DeepSpeed support for NPU (huggingface#2054)
1 parent 183c9dd commit 5e0eb0d

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

src/accelerate/commands/config/cluster.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def get_cluster_input():
179179

180180
use_mps = not use_cpu and is_mps_available()
181181
deepspeed_config = {}
182-
if distributed_type in [DistributedType.MULTI_GPU, DistributedType.NO] and not use_mps:
182+
if distributed_type in [DistributedType.MULTI_GPU, DistributedType.MULTI_NPU, DistributedType.NO] and not use_mps:
183183
use_deepspeed = _ask_field(
184184
"Do you want to use DeepSpeed? [yes/NO]: ",
185185
_convert_yes_no_to_bool,

src/accelerate/state.py

+6
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ def __init__(self, cpu: bool = False, **kwargs):
175175
if is_xpu_available and is_ccl_available():
176176
# Set DeepSpeed backend to ccl for xpu
177177
self.backend = "ccl"
178+
elif is_npu_available():
179+
self.backend = "hccl"
178180
else:
179181
self.backend = "nccl"
180182
dist.init_distributed(dist_backend=self.backend, auto_mpi_discovery=False, **kwargs)
@@ -187,6 +189,10 @@ def __init__(self, cpu: bool = False, **kwargs):
187189
self.device = torch.device("xpu", self.local_process_index)
188190
if self.device is not None:
189191
torch.xpu.set_device(self.device)
192+
elif is_npu_available():
193+
self.device = torch.device("npu", self.local_process_index)
194+
if self.device is not None:
195+
torch.npu.set_device(self.device)
190196
else:
191197
self.device = torch.device("cuda", self.local_process_index)
192198
if self.device is not None:

src/accelerate/utils/launch.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,12 @@ def prepare_deepspeed_cmd_env(args: argparse.Namespace) -> Tuple[List[str], Dict
291291
current_env["ACCELERATE_DEBUG_MODE"] = "true"
292292
gpu_ids = getattr(args, "gpu_ids", "all")
293293
if gpu_ids != "all" and args.gpu_ids is not None:
294-
if not is_xpu_available():
295-
current_env["CUDA_VISIBLE_DEVICES"] = gpu_ids
296-
else:
294+
if is_xpu_available():
297295
current_env["ZE_AFFINITY_MASK"] = gpu_ids
296+
elif is_npu_available():
297+
current_env["ASCEND_RT_VISIBLE_DEVICES"] = gpu_ids
298+
else:
299+
current_env["CUDA_VISIBLE_DEVICES"] = gpu_ids
298300
try:
299301
mixed_precision = PrecisionType(args.mixed_precision.lower())
300302
except ValueError:

0 commit comments

Comments
 (0)