已合并
【bugfix】npugraph_ex.scope.limit_core_num增加默认参数stream #39058
【bugfix】npugraph_ex.scope.limit_core_num增加默认参数stream #39058
已合并
mihudan创建于 6月23日
mihudan成员
6月23日

【合入来源】

如有社区issue,请关联issue链接
请勿携带内部流程信息(需求链接、问题单、内部issue等)
https://gitcode.com/Ascend/pytorch/issues/2445

【修改方案】

请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列
如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

【资料变更】

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

【接口变更】

请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”
npugraph_ex接口增加默认参数,ex资料修改已合入,组内已检视

【功能验证】

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

【CheckList】

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

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 mihudan 的贡献)
Mmihudan成员
6月23日 创建了 pull request,commit 10f0a39b
Mmihudan成员
6月23日 关联了issue:[Bug]: npugraph_ex,控核图模式预期不支持嵌套逻辑,接口增加一个stream参数
ascend-robotascend-robot成员
6月23日 添加了label:stat/needs-squash
ascend-robotascend-robot成员
6月23日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
6月23日 评论:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here
You can get sig-info at here


PR Approval Progress

Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
test medivh-x, luochao60 (2/2) medivh-x (1/1)
torch_npu/npu medivh-x, luochao60 (2/2) medivh-x (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

mihudan, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robot
ascend-robot成员
6月23日 评论:

当前仓库存在以下 保护分支

Protected Branch Version Release
master
v2.7.1
v2.12.0
v2.11.0
v2.7.1-26.1.0
v2.9.0
v2.9.0-26.1.0
v2.10.0
v2.12.0-26.1.0
v2.11.0-26.1.0
v2.10.0-26.1.0
ci-test
sync-pr28113--to-v2.9.0

评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作

注意:

  1. /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
  2. 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭
likedislike
mihudan成员
6月23日 评论:

compile

likedislike
ascend-robotascend-robot成员
6月23日 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
6月23日 评论:

Ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
6月23日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
6月23日 评论:

Ascend docs pipeline is running...

likedislike
ascend-robot
ascend-robot成员
6月23日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
6月23日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
6月23日 添加了label:docs-ci-pipeline-success
ascend-robot
ascend-robot成员
6月23日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
atomgit-bot
atomgit-bot
6月23日 评论:

审查结果

文件 1: torch_npu/npu/npugraph_ex/scope/__init__.py

变更内容:为 limit_core_num 函数增加了 stream=None 可选参数,并透传给底层 scope.limit_core_num

审查结论:无问题。参数使用 None 作为默认值(不可变对象),向后兼容旧调用方式,透传逻辑正确。

文件 2: test/dynamo/test_npugraph_ex.py

变更内容:新增测试方法 test_limit_core_num_with_stream,验证 limit_core_num 配合 stream 参数的使用场景。

审查结论:无问题。测试模式与已有的 test_limit_core_num 一致,通过 torch.compile 编译后执行模型来验证功能正确性;torch.npu.Stream() 创建和 torch.npu.stream() 上下文管理器的使用均符合 NPU API 惯例。

文件 3: test/torch_npu_schema.json

变更内容:更新 torch_npu.npu.npugraph_ex.scope.limit_core_num 的签名,从 (op_aicore_num: int, op_vectorcore_num: int) 改为 (op_aicore_num: int, op_vectorcore_num: int, stream=None)

审查结论:无问题。JSON schema 签名与新接口定义一致,stream=None 正确反映了可选参数的语义。

likedislike
atomgit-bot
atomgit-bot
6月23日 评论:

代码审查

Now I have all the context I need. Let me compile my findings.


审查总结

本次变更包含 3 个文件,逐文件审查结果如下:

文件 结论
torch_npu/npu/npugraph_ex/scope/__init__.py 发现问题 2 项(P2 ×1,P3 ×1)
test/dynamo/test_npugraph_ex.py 无新增问题(测试为已有模式的合理扩展)
test/torch_npu_schema.json 无问题(签名文档正确更新)

发现统计

  • P2:1 项 — wrapper 无条件传递 stream=stream 关键字参数,对未同步升级的底层 torchair 实现构成兼容性风险
  • P3:1 项 — stream 参数缺失类型注解,与同签名的其他参数不一致

整体风险评估中等。核心功能变更(添加 stream 可选参数)本身设计合理,向后兼容(stream=None 默认值)。主要风险在于 wrapper 层始终以关键字形式传递 stream 给底层实现,在跨仓协同升级出现版本偏差时会导致所有调用(包括不传 stream 的旧用法)直接 runtime 崩溃。建议对 streamNone 时使用原有位置参数调用路径,以消除此兼容性风险。

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

💬 仅评论

likedislike
atomgit-bot
atomgit-bot6月23日进行代码检视1
test/torch_npu_schema.json
@@ -2309,3 +2309,3 @@
23092309 "torch_npu.npu.npugraph_ex.scope.limit_core_num": {
2310- "signature": "(op_aicore_num: int, op_vectorcore_num: int)"
2310+ "signature": "(op_aicore_num: int, op_vectorcore_num: int, stream=None)"
23112311 },
atomgit-bot
atomgit-bot6月23日评论:

🔵 Low Priority

第2310行:签名字符串正确反映了新增的 stream=None 参数,JSON key "torch_npu.npu.npugraph_ex.scope.limit_core_num" 与实际模块路径一致,未发现拼写错误或格式问题。

该文件是本次变更的配套文档更新,与 Python 代码变更保持一致。

建议:无需修改,签名正确。

likedislike
atomgit-bot
atomgit-bot6月23日进行代码检视1
torch_npu/npu/npugraph_ex/scope/__init__.py
@@ -2,3 +2,3 @@
22 
33 
4-def limit_core_num(op_aicore_num: int, op_vectorcore_num: int):
4+def limit_core_num(op_aicore_num: int, op_vectorcore_num: int, stream=None):
atomgit-bot
atomgit-bot6月23日评论:

🔵 Low Priority

第4行:op_aicore_numop_vectorcore_num 均有 : int 类型注解,但新增的 stream 参数无任何类型提示,与同一函数签名的风格不一致。

虽然这不是功能缺陷,但缺失类型注解会降低代码可读性,且 IDE/类型检查器无法对 stream 提供类型推导与校验。合理的注解应为 stream: Optional[torch.npu.Stream] = None(需从 typing 导入 Optional)。

建议:为 stream 参数添加类型注解,例如 stream: Optional[torch.npu.Stream] = None,与其他参数保持风格一致。

likedislike
atomgit-bot
atomgit-bot6月23日进行代码检视1
torch_npu/npu/npugraph_ex/scope/__init__.py
@@ -5,2 +4,3 @@
4+def limit_core_num(op_aicore_num: int, op_vectorcore_num: int, stream=None):
55 from torch_npu.dynamo.npugraph_ex import scope
6- return scope.limit_core_num(op_aicore_num, op_vectorcore_num)
6+ return scope.limit_core_num(op_aicore_num, op_vectorcore_num, stream=stream)
atomgit-bot
atomgit-bot6月23日评论:

🟡 Medium Priority

变更后的 wrapper limit_core_num 始终将 stream 以关键字参数形式传递给底层 scope.limit_core_num(第6行:stream=stream),即使 stream 取默认值 None 时也是如此。

受影响的合约/行为:旧版 scope.limit_core_num(来自 torch_npu.dynamo.npugraph_ex,实际由 torchair 提供)的签名为 (op_aicore_num, op_vectorcore_num),不接受 stream 关键字参数。

失效模式:若 torchair 包未同步升级,所有limit_core_num 的调用(包括不传 stream 的现有用法,如已有测试 test_limit_core_num 第175行)都会抛出 TypeError: limit_core_num() got an unexpected keyword argument 'stream'

触发条件:torchair 版本未包含对应的 stream 参数支持(PR #3181),而本变更已合入。虽然此次发布是协同进行的,但在部分环境升级或版本回退场景下会直接 runtime 崩溃。

建议:在 wrapper 中判断 stream 是否为 None,仅在非 None 时才以关键字参数传递,保持对旧版 torchair 的向后兼容。

改动建议
6
+ def limit_core_num(op_aicore_num: int, op_vectorcore_num: int, stream=None):
7
+ from torch_npu.dynamo.npugraph_ex import scope
8
+ if stream is not None:
6
- return scope.limit_core_num(op_aicore_num, op_vectorcore_num, stream=stream)
9
+ return scope.limit_core_num(op_aicore_num, op_vectorcore_num, stream=stream)
10
+ return scope.limit_core_num(op_aicore_num, op_vectorcore_num)
应用建议
likedislike
Mmihudan成员
6月23日 解决了最后一个问题
ascend-robotascend-robot成员
6月23日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
6月23日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
6月23日 评论:
流水线 PR-pipeline_pytorch#38484 已完成
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
CodeCheck_lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 🛑 >>>
UT_X86_Part_02 🛑 >>>
UT_ARM_A3_Part_01 🛑 >>>
UT_ARM_A3_Part_02 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
UT_ARM_A2_Select_Part_01 >>>
UT_ARM_A2_Select_Part_02 >>>
流水线 PR-pipeline_pytorch >>>
likedislike
薛鹏
薛鹏成员
6月24日 评论:

/approve

likedislike
ascend-robotascend-robot成员
6月24日 添加了label:approved
luochao60成员
6月24日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
6月24日 添加了label:lgtm
ascend-robotascend-robot成员
6月24日 关闭了关联的issue
ascend-robotascend-robot成员
6月24日 合入了pull request
ascend-robot
ascend-robot成员
6月24日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
6月24日 评论:
流水线 pytorch_gitcode_PR_multiVersion#11214 [ commitID:10a2bb50 ] 已完成
likedislike