mindspore-ai / mindspore-ai/hyper-parallel

[Bug] FSDP CPU offload 开启后,梯度裁剪报 No backend type associated with device type cpu

Open
#612 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
53
Forks
63
Avg merge
23h 45m
Merged PRs (30d)
63

Description

Checklist
  • 1. I have searched the existing issues (https://gitcode.com/mindspore/hyper-parallel/issues)
  • 2. I have read the relevant documentation.
  • 3. I have created a minimal reproduction case that clearly demonstrates the issue, including a complete code example and the error message with full traceback and error logs.
🐛 Describe the bug

开启 FSDP CPU offload(fsdp_config.enable_offload: true)后,训练在第一个 optimizer step 的梯度裁剪处崩溃:

RuntimeError: No backend type associated with device type cpu
  File ".../hyper_parallel/platform/torch/clip_grad.py", line 536, in _total_norm_fsdp2_aligned
    dist.all_reduce(local_p, op=dist.ReduceOp.SUM, group=group)

根因:offload 后参数与其分片梯度常驻 CPU,clip_grad_norm_ 因此把每个 rank 的 norm 标量也建在 CPU 上,再对该标量做跨设备进程组(hccl/nccl)all-reduce;这两种后端都没有注册 CPU 后端,于是报错。未开 offload 时标量本就在加速器上,因此不受影响。

复现步骤

  1. 任意训练配置加上 fsdp_config.enable_offload: true(FSDP / EP 组合均可复现);
  2. 启动训练(torch 文本入口或 VLM 入口均可);
  3. 第 1 步 forward/backward 正常通过,在 optimizer.step() 之前的梯度裁剪处崩溃。

最小复现规模:VLM 小模型 FSDP-8 + enable_offload: true 即可复现。实际触发场景为全量文本塔(61 层 × 384 路由专家,约 1T 参数)+ FSDP2-128 + EP128(16 节点 128 卡)。

已排查的相似 issue(非本问题):#313 [RFC]: swap optimizer(optimizer state swap 机制设计,走 swap_optimizer() 路径)、#178(clip_grad 范数重复计数的数值问题)。

Expected behavior

enable_offload: true 时训练应正常进行——offload 只改变参数/梯度/优化器状态的驻留位置,不应导致梯度裁剪失败。

Additional context
  • 已完成定位与修复,修复 PR:#1385 fix(platform): reduce CPU norm scalars on accelerator
  • 相关背景:
    • #1217 feat: add fsdp mix_precision and cpu offload config(当初引入 enable_offload 的 PR)
    • #313 [RFC]: swap optimizer(另一个"训练状态放 CPU"的机制,走 swap_optimizer() 接口,与本问题不是同一路径)
  • 修复方式:把 norm 标量临时搬到本卡加速器完成归约、结果拷回原设备;非 offload 路径逐字不变
  • 验证结果:UT tests/ut/platform/torch/test_clip_grad.py 5/5;VLM FSDP-8 与 TP2×EP2 开 offload 后 loss 轨迹与非 offload 逐位一致;全量 1T + offload 在单超节点 128 卡上 3/3 步跑通(该配置不开 offload 会 OOM)
Environment info
  • 硬件:Ascend 910(64GB/卡),单超节点 16 节点 × 8 卡
  • torch 2.9.1、torch_npu 2.9.1、transformers 5.13.0、Python 3.11
  • CANN 9.0.0

Thanks for contributing 🎉!

schema_version: 1
source: gitcode
gitcode_repo: mindspore/hyper-parallel
gitcode_issue: 379
source_url: https://gitcode.com/mindspore/hyper-parallel/issues/379

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in hyper_parallel/platform/torch/clip_grad.py at _total_norm_fsdp2_aligned, then read the related tests in tests/ut/platform/torch/test_clip_grad.py. Run the five existing clip-gradient tests and compare CPU-offload and non-offload behavior; the issue reports that repair PR #1385 already addresses the failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
distributed-systems, performance
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.