mindspore-ai / mindspore-ai/hyper-parallel

fully_shard: dp_shard==1 path skips unshard / reset / grad-plumbing

Open
#306 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

该问题是怎么引起的?

fully_shardshard_world_size==1(退化的单分片 FSDP wrap)路径下存在四个相互耦合的 bug,原先一旦走到就会失效。这条路径被 1c-vs-FSDP_N 自洽对比、以及任何 "无论 world_size 多少都包一层 fully_shard" 的上层调用方使用。

根因TorchHSDPStateV2 中三处用 if hsdp_param.is_sharded 当门,而 shard_size==1is_sharded=False,把 dp_shard==1 路径还需要的逻辑跳过;另外 TorchHSDPParamV2_local_tensor 设成 narrow() 返回的非 leaf view,导致 PyTorch 原生 optim 的 leaf-check 失败。

涉及文件:

  • hyper_parallel/platform/torch/fully_shard/state.py
  • hyper_parallel/platform/torch/fully_shard/param.py
  • tests/ut/platform/torch/fully_shard/test_fully_shard.py
重现步骤
  1. 任何 dp_shard==1 配置下用 hyper_parallel.fully_shard 包模型 —— 比如:
train:
  accelerator:
    dp_shard: 1

启动方式:

torchrun --standalone --nproc_per_node=1 scripts/train_lm.py <yaml>
  1. 用 PyTorch 原生 optimizer(torch.optim.AdamW),mp_policy 不开 apply_grad_on_fp32_main_grad

  2. 模型走标准初始化路径:build_model_on_meta()model.to_empty(device='npu')model.reset_parameters()

任意上述场景都会触发以下症状之一。

报错信息

症状 1 —— DTensor 进 forward

HF 原生模型 forward 报错或静默走 layout 转换,例如:

RuntimeError: aten.<op> not supported on DTensor

症状 2 —— to_empty() 之后 _local_tensor 留 meta

RuntimeError: Tensor still on meta device. Cannot perform operation.

栈在第一次 unshard 的 all-gather(_get_unsharded_param_data)时触发。

症状 3 —— optimizer no-op

无报错,但 loss 不下降 —— param.graddp_shard==1 下没有任何代码路径把 unsharded grad 复制到 sharded_param.grad,optimizer 看到 param.grad is None 静默 no-op 每一步。

症状 4 —— torch.optim.* leaf-check 失败

RuntimeError: a leaf Variable that requires grad is being used in an in-place operation.

栈在 torch.optim.AdamW._init_group / _single_tensor_adamw 内部。原因:narrow() 返回非 leaf view,DTensor nn.Parameter 因此不再是 leaf,optimizer leaf-check 拒绝。

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

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 with the dp_shard==1 guards in hyper_parallel/platform/torch/fully_shard/state.py and the _local_tensor setup in param.py. Then inspect the existing coverage in tests/ut/platform/torch/fully_shard/test_fully_shard.py and reproduce with the provided single-process torchrun configuration. Done means unshard/reset behavior, gradient plumbing, and native optimizer use work for dp_shard==1 without the reported errors or no-op updates.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
distributed-systems, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.