Skip to content

fix(checkpoint): 初始化被忽略的缺失模型状态 - #1468

Closed
xingleixu123 wants to merge 1 commit into
mindspore-ai:masterfrom
xingleixu123:codex/hf-ignored-missing-init-20261105
Closed

xingleixu123 wants to merge 1 commit into
mindspore-ai:masterfrom
xingleixu123:codex/hf-ignored-missing-init-20261105

Conversation

@xingleixu123

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

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

模型声明 _keys_to_ignore_on_load_missing 时,当前 finalizer 先过滤缺键,再据此选择初始化目标,导致被忽略的持久参数/buffer既没加载、也没初始化,strict=True却返回成功。真实meta/to_empty模型的缺失存储预置为71后,legacy、DCP都复现保留71。

本补丁只修改checkpoint_loader.py(+4/−2):在tied aliases消解后保留初始化用missing集合;ignore规则继续作用于strict检查及报告,真实缺失的持久状态仍进入现有guarded initializer。已加载值、tied别名、nonpersistent rebuild及其他strict缺键行为保持;缺失DTensor明确要求DTensor-aware initialization,从原静默跳过改为拒绝。

沿用HF guarded nn.init契约,不新增初始化框架、全量权重副本或模型专用条件。自定义_init_weights直接调用Tensor.zero_/copy_绕过guard不在自动保护范围;Xing4此类写入由#1439的e4013cfb单独修复。

Which issue(s) this PR fixes:

关联 #1467;模型初始化问题 #1466 / #1439 是独立修复。保持草稿问题开放,不因创建PR关闭。

Test Plan and Test result:

  • 基于master c108cf9,独立单提交5fcd3a8a。
  • 真实HF PreTrainedModel+safetensors,经legacy/DCP读取:ignored parameter/buffer由旧71/71变为期望3/5,已加载兄弟保持7/8;完整权重、不忽略缺键strict、meta/to_empty及nonpersistent rebuild均验证。
  • CPU/Gloo单进程真实HP DTensor仍明确拒绝缺失初始化;tied alias保持共享,init_calls=0,两加载后端均覆盖。
  • 既有加载测试16 passed、18 subtests;独立测试stage缺pytest标记注册产生6条UnknownMarkWarning,未修改产品掩盖。
  • 129物理NPU卡5的before/after finalizer:旧count=[71,71],新count=[0,0],已加载NPU weight/bias均不变,1 passed。不是多rank或大模型精度/性能验收。
  • CPU Python3.12.3/Torch2.12.0+cpu/Transformers5.22.1;NPU Python3.12.14/PTA2.12.0.post3/Transformers5.21.0/CANN9.2 B237。
  • 与模型增量合计5文件的同配置pylint前后24项、新增0、退出22;未称lint全绿。语法编译与diff检查通过。

复现脚本、原始输出、源码绑定及限制

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另查,不将旧门禁结果归给本提交。

将缺失状态初始化与告警过滤分离,保留 strict 和 DTensor 初始化边界。
@xingleixu123

Copy link
Copy Markdown
Contributor Author

/check-pr

@xiaoyuxing-010818

Copy link
Copy Markdown
Contributor

lgtm

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants