LongSana hardcodes CUDA/NCCL for distributed init and FSDP device (crashes on non-CUDA accelerators such as Ascend NPU)
LongSana training/inference cannot start on a non-CUDA accelerator (tested on Ascend NPU 910B4 + torch_npu): device and collective backend are hardcoded to CUDA/NCCL.
Root cause (files + lines, current main)
| Location | Statement | Failure on NPU |
|---|---|---|
diffusion/longsana/utils/distributed.py:83 |
def launch_distributed_job(backend: str = "nccl") |
RuntimeError: Distributed package doesn't have NCCL built in |
diffusion/longsana/utils/distributed.py:97 |
torch.cuda.set_device(local_rank) |
AttributeError: module 'torch._C' has no attribute '_cuda_setDevice' |
diffusion/longsana/utils/distributed.py:69 |
device_id=torch.cuda.current_device() (FSDP) |
AssertionError: Torch not compiled with CUDA enabled |
diffusion/longsana/trainer/ode.py:41, trainer/self_forcing_trainer.py:47 |
self.device = torch.cuda.current_device() |
same AssertionError |
pipeline/sana_training_pipeline.py:80-81, pipeline/sana_inference_pipeline.py:50-51, pipeline/sana_inference_interactive_pipeline.py:33-34, ..._long_chunk.py:30-31 |
torch.device("cuda" if torch.cuda.is_available() else "cpu") / torch.bfloat16 if torch.cuda.is_available() else torch.float32 |
silently falls back to CPU/fp32 on NPU, i.e. no acceleration |
Reproduction (2 ranks, real hardware, torchrun --nproc_per_node=2)
unpatched longsana/utils/distributed.py, Ascend 910B4 x2:
RESULT ode.py:41 torch.cuda.current_device() -> ERR AssertionError: Torch not compiled with CUDA enabled
RESULT launch_distributed_job() -> ERR RuntimeError: Distributed package doesn't have NCCL built in
RESULT collective -> SKIPPED (process group not initialised)So torchrun ... diffusion/longsana/trainer/longsana_trainer.py cannot initialise the process group at all on NPU.
Proposed fix (PR follows)
Add device/backend resolution to longsana/utils/distributed.py (get_device_type, get_current_device, get_default_dtype, set_device, get_distributed_backend) and replace every CUDA-only device/backend choice above with it. CUDA behaviour is unchanged (cuda/nccl), NPU resolves to npu/hccl, CPU-only stays cpu/gloo.
Verified after the change on the same 2x910B4 box:
RESULT get_current_device() (patched ode.py:41) -> OK npu:0
RESULT launch_distributed_job() -> OK None
RESULT hccl all_reduce + rank->card marker -> OK all_reduce sum=3.0 local_rank=0 npu.current_device()=0 marker=512MiB
RESULT fsdp_wrap() on the accelerated device -> OK fsdp_wrapped param device=npu:0 (rank 0) / npu:1 (rank 1)(npu-smi cross-checks: one process per card, each rank's 512 MiB marker on its own card.)
Environment: Ascend 910B4 (32 GB HBM) x2, torch 2.15.0.dev20260917+cpu, torch_npu same build, CANN 9.2.0-beta.2, Python 3.12.
Source: NVlabs/Sana