已合并
[feat] deterministic_level: add graph-mode guard and EX scope API #41859
[feat] deterministic_level: add graph-mode guard and EX scope API #41859
已合并
Lyric创建于 7月16日
Lyric
Lyric成员
7月16日

【合入来源】

如有社区issue,请关联issue链接
请勿携带内部流程信息(需求链接、问题单、内部issue等)

【修改方案】

1、新增 torch_npu.dynamo._deterministic_guard.install_npu_deterministic_level_guard()。在 Dynamo 的 TracingContext.guards_context.dynamo_guards 中注册 Lambda Guard。
2、新增 torch_npu._inductor.deterministic_cache,在 AscendC 编译 scope 内 patch。

【资料变更】

请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”

不涉及

【接口变更】

请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”

不涉及

【功能验证】

说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤
新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图

自验证ok

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 Lyric 的贡献)
LyricLyric成员
7月16日 创建了 pull request,commit 943b953e
atomgit-bot
atomgit-bot
7月16日 评论:

变更摘要

该 PR 为 deterministic_level 功能新增了 TorchDynamo 图模式下的守卫(guard)机制和 npugraph_ex.scope 公开 API。核心变更是新增 install_npu_deterministic_level_guard() 函数,在图编译阶段向 TorchDynamo 的 guard 系统注册一个 lambda guard,用于检测 torch_npu.npu._get_deterministic_level() 的返回值变化;当用户调用 torch_npu.npu.set_deterministic_level() 修改 level 时,guard 不匹配会触发重新编译,确保图模式下的计算正确性。同时在 scope 模块中暴露了 deterministic(level: int) 接口。

主要改动

  • 新增 _deterministic_guard.py 模块:实现 install_npu_deterministic_level_guard() 函数,通过 GlobalStateSource().make_guard() 向 Dynamo 注册一个 lambda guard,在编译时捕获当前的 deterministic_level 值,运行期检查值是否变化并触发重编译。
  • 新增 deterministic scope API:在 torch_npu/npu/npugraph_ex/scope/__init__.py 中新增 deterministic(level: int) 函数,委托调用 torch_npu.dynamo.npugraph_ex.scope.deterministic(level),并加入 __all__ 导出列表。
  • 新增单元测试test/dynamo/test_deterministic_level_graph_mode.py 验证在同一编译函数中切换 deterministic_level(1→2→1→2)时,guard 正确触发两次重新编译(compile_count == 2)。
  • 更新 Schema 定义test/torch_npu_schema.json 新增 torch_npu.npu.npugraph_ex.scope.deterministic 条目,其签名为 (level: int)
likedislike
atomgit-bot
atomgit-bot
7月16日 评论:

代码审查

Closing Summary

已完成对全部 4 个变更文件的审查,结果如下:

文件 审查结果
test/dynamo/test_deterministic_level_graph_mode.py 无问题
test/torch_npu_schema.json 无问题
torch_npu/dynamo/_deterministic_guard.py 1 个 P3 发现(guard check 副作用)
torch_npu/npu/npugraph_ex/scope/__init__.py 1 个 P2 发现(委托目标可能未实现)
  • P2: 1 个 — scope.deterministic 在外部 npugraph_ex 包中的实现可能缺失,运行时可能抛出 AttributeError
  • P3: 1 个 — Guard 的 check_fn 通过 _get_deterministic_level() 引入副作用,在全局状态不一致时可能意外修改确定性等级

整体风险判断:中等偏低。核心逻辑(guard 机制)正确,测试覆盖合理。主要风险在于 scope.deterministic 对外部 npugraph_ex 包的依赖未在本 PR 中体现,合入前需确认外部包已提供对应实现。

类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论

likedislike
ascend-robotascend-robot成员
7月16日 添加了label:ascend-cla/yes
此处折叠了82条消息 查看更多
ascend-robotascend-robot成员
29 天前 添加了label:lgtm
ascend-robotascend-robot成员
29 天前 删除了label:ci-pipeline-passed
ascend-robotascend-robot成员
29 天前 解决了最后一个问题
ascend-robotascend-robot成员
29 天前 合入了pull request
ascend-robot
ascend-robot成员
29 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#12971 [ commitID:784862c8 ] 已完成
likedislike