[NPU] reduce_sum_across_dp_group builds the all_reduce tensor on CPU when CUDA is unavailable

未关闭
#2,126 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 4 天内回复

@li-lizhe 已经在做这个了。

开始于 2026年9月25日。

  • #2127 来自 @li-lizhe —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
30/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python

调研方向

Start in xtuner/v1/datasets/dataloader.py at reduce_sum_across_dp_group(), then inspect its callers through Dataloader.get_state_dict(). Reproduce the two-rank collective on an accelerator-backed process group and verify completion when the tensor uses the mesh device; done means both ranks report the expected reduced value without a CPU-backend error.

由索引模型根据 Issue 内容生成。

描述

What happens

On a host without CUDA (for example an Ascend NPU host, where torch.cuda.is_available() is False), reduce_sum_across_dp_group() in xtuner/v1/datasets/dataloader.py builds the tensor for the DP all_reduce on CPU:

if torch.cuda.is_available():
    device = torch.device(f"cuda:{torch.cuda.current_device()}")
else:
    device = torch.device("cpu")
tensor = torch.tensor([local_value], dtype=torch.int64, device=device)
dist.all_reduce(tensor, op=dist.ReduceOp.SUM, group=dp_mesh.get_group())

The process group comes from dp_mesh, i.e. it is an NCCL/HCCL group on the accelerator, so handing it a CPU tensor fails.

Real-machine evidence

Ascend 910B4, torch 2.15.0.dev + torch_npu, 2 processes via torchrun, real HCCL process group, reduce_sum_across_dp_group(DeviceMesh("npu", [0, 1]), 3) with the unmodified file:

[rank0] current npu device=0 mesh.device_type=npu
[rank0] RAISED RuntimeError: No backend type associated with device type cpu
[rank1] current npu device=1 mesh.device_type=npu
[rank1] RAISED RuntimeError: No backend type associated with device type cpu

This is not an exotic path: it is reached from Dataloader.get_state_dict(), which is used when saving/restoring dataloader state, so on Ascend the dataloader state cannot be written at all.

Proposed fix

dp_mesh is guaranteed non-None at that point (the early return above handles None), and it already carries the device type of the collective, so use it instead of assuming CUDA:

device = torch.device(dp_mesh.device_type)

That is device-agnostic (cuda / npu / xpu / cpu) and keeps a CPU mesh working. I have the fix ready and verified on the same 2-rank HCCL setup (RESULT=6 expected=6 on both ranks) - opening a PR right after this.

主要语言
Python
星标
5.2k
派生
466
平均合并
5 天 8 小时
30 天内合并 PR
44

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

InternLM/xtuner 的其他 Issue

查看 InternLM/xtuner 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。