已合并
[feat] deterministic_level: add graph-mode guard and EX scope API #41859
Lyric创建于 7月16日
[feat] deterministic_level: add graph-mode guard and EX scope API #41859
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 Lyric 的贡献)atomgit-bot
7月16日 评论:
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值,运行期检查值是否变化并触发重编译。 - 新增
deterministicscope 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)。


ascend-robot
7月16日 评论:
7月16日 评论:
atomgit-bot
7月16日 评论:
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 |
💬 仅评论


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


【合入来源】
【修改方案】
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。
【资料变更】
不涉及
【接口变更】
不涉及
【功能验证】
自验证ok
【CheckList】