{{ message }}
Repository navigation
Conversation
…ve accelerator The distributed helpers assumed CUDA everywhere: - init_dist() defaulted to backend='nccl' and _init_dist_pytorch() called torch.cuda.set_device() unconditionally (crashes on Ascend NPU/hccl and on CPU/gloo with AttributeError: module 'torch._C' has no attribute '_cuda_setDevice') - broadcast() built every tensor with device='cuda' - _serialize_to_tensor() mapped every non-gloo backend to 'cuda' and asserted backend in ['gloo', 'nccl'] - set_random_seed() seeded only the CUDA RNG Resolve the device type and the default collective backend from the actuator that is actually available (torch_npu -> 'npu', xpu -> 'xpu', mps -> 'mps', else 'cuda'), keeping the CUDA behaviour identical. Signed-off-by: li-lizhe <147392333@qq.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

What & why
modelscope/utils/torch_utils.pyresolves devices and the collective backend from CUDA instead offrom the accelerator that is actually available. On an Ascend NPU (
torch_npu, backendhccl) — andeven on a plain CPU build —
init_dist()/broadcast()/all_gather()raise before doing any work,so the distributed entry points of the framework are unusable there.
Fixes #1824
Everything below is in that one file:
init_dist()backend: str = 'nccl'backend=None→ resolved from the accelerator (nccl/hccl/xccl/gloo)_init_dist_pytorch()torch.cuda.set_device(local_rank)unconditionally, beforebackendis even looked atlocal_rankon the active accelerator_init_dist_mpi(),_init_dist_slurm()torch.cuda.set_device()/torch.cuda.device_count()broadcast()device='cuda'npuon Ascend)set_random_seed()torch.npu.manual_seed_allwhen available)_serialize_to_tensor()gloobackend mapped tocuda;assert backend in ['gloo', 'nccl']hcclaccepted; the tensor is placed on the backend's deviceNew small module-level helpers:
get_accelerator_device_type(),get_accelerator_device(),_get_accelerator_module(),_set_accelerator_device(),_accelerator_device_count(),_default_dist_backend(),_collective_device(). The device type is resolved throughtorch.accelerator.current_accelerator()when the PyTorch build provides it (it was added in 2.5),falling back to probing
torch.cuda/torch.npu/torch.xpu/torch.mps;cpuwhen there isnone. Collectives stay on CPU for
gloo, as before.Deliberately not used:
device = 'cuda' if torch.cuda.is_available() else 'cpu'. That shape wouldmake Ascend users silently fall back to CPU instead of using the NPU; here the accelerator is always
resolved to the real device type. No NPU-specific branch is added to any call site — this is a plain
device-agnostic change, so the CUDA behaviour is untouched.
Verification — Ascend 910B4, real hardware
torchrun --nproc_per_node=2, 2 × Ascend 910B4, torch2.15.0.dev20260917+cpu+torch_npu,CANN toolchain. The module under test is transferred byte-identically (md5-checked) and driven by the
same script in both runs, only the module differs (
MODE=default=init_dist(launcher='pytorch'),MODE=hccl= explicitbackend='hccl').before:
after:
(identical output on rank 1, with
npu current_device 1; the explicitbackend='hccl'case gives thesame before/after split)
Note that before this change even
init_dist(launcher='pytorch', backend='hccl')failed: the processgroup was created but
_init_dist_pytorch()calledtorch.cuda.set_device()first, so passing thecorrect backend was not a workaround.
Regression check on a CPU-only build (torch 2.14.0a0,
torch.cuda.is_available() == False,torchrun --nproc_per_node=2,gloo) — this path was broken as well, not only the NPU one:Not tested
(
get_accelerator_device_type()returns'cuda'and the default backend resolves tonccl), but Icould not execute it.
launcher='pytorch'was exercised end to end; the two other helperschanged in the same one-line way.
installed on the test machine.
Also noticed, not changed in this PR
_serialize_to_tensor()callslogger.warning(...)on the >1 GB branch, but there is no module-levelloggerintorch_utils.py(nologgingimport, noget_logger()call) — that branch would raiseNameError. I left it alone to keep this PR to a single concern; happy to send a separate one-liner.