Skip to content

fix(dtensor): 支持常数初始化并保持原位张量身份 - #1470

Closed
xingleixu123 wants to merge 1 commit into
mindspore-ai:masterfrom
xingleixu123:codex/dtensor-constant-init-20261105
Closed

xingleixu123 wants to merge 1 commit into
mindspore-ai:masterfrom
xingleixu123:codex/dtensor-constant-init-20261105

Conversation

@xingleixu123

@xingleixu123 xingleixu123 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What does this PR do / why do we need it:

FSDP包装后的HF模型调用torch.nn.init.constant_时,HP DTensor因缺少布局规则而在训练前报错。PyTorch以tensor关键字进入分发,仅补算子注册还会返回新wrapper,不满足原位初始化返回原对象的契约。

本补丁仅改两文件(+11/−4):通过YAML复用ElementWiseDistributedOp,并在原位结果恢复中支持constant_及tensor关键字。没有复制初始化算法或增加模型专用分支;HF已加载tensor的guard、现有Partial/Ragged拒绝边界保留,不消耗随机状态,不改变训练数学。

Which issue(s) this PR fixes:

关联 #1469。#1439/#1466的guarded初始化需要这项通用能力;#1468是独立HF部分加载问题。

Test Plan and Test result:

  • 基于master435ccc0287056af621c3f5fda0182e27284967e5,单提交60a2dc219489f39f7aefa199bd6413ef5fcc775a。
  • 真实HP DTensor CPU/Gloo复现旧constant_缺规则;候选覆盖Shard/Replicate、非均匀空分片、positional/keyword、Parameter对象/storage、HF已加载保护及RNG不变;Partial/Ragged在修改前拒绝。
  • 既有相关dispatch/inplace测试89 passed、10 subtests passed;在最新master435ccc02 + 本提交精确两文件的独立目录复测,同样89 passed、10 subtests passed。
  • 129物理0–3真实四卡NPU,每rank9项有效布局/调用组合和2项拒绝边界通过;物理设备准入、脚本/source哈希、退出和资源释放均留证。
  • 原始单卡/四卡大模型构建失败证据保留;修复后的完整MF/HP与并行自洽运行中,未宣称长程验收完成。
  • Python3.12.14/PTA2.12.0.post3/Transformers5.21.0/CANN9.2 B237;CPU Python3.12.3/Torch2.12.0+cpu/Transformers5.22.1。
  • 显式SkipDTensorDispatch上下文不属于本次原位身份扩展;它仍按既有显式跳过语义返回local结果。

Code review checklist [illustration]:

  • whether to verify the function's return value (It is forbidden to use void to mask the return values of security functions and self-developed functions. C++ STL functions can be masked if there is no problem)
  • Whether to comply with SOLID principle / Demeter's law
  • Whether there is UT test case && the test case is a valid (if there is no test case, please explain the reason)
  • Whether the API change is involved
  • Whether official document modification is involved

本轮保持逻辑代码提交范围,复用测试及必要外置回归脚本/原始结果随总跟踪证据归档。外部CI按本提交另查,不以旧通过标签代替新head的门禁结果。

复用逐元素布局推导注册 constant_,并支持关键字 tensor 的原位返回。
保留 Partial 与 Ragged 的现有拒绝规则,不引入随机初始化路径。
@xingleixu123

Copy link
Copy Markdown
Contributor Author

/check-pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants