已合并
【bugfix】npugraph_ex.scope.limit_core_num增加默认参数stream #39058
【bugfix】npugraph_ex.scope.limit_core_num增加默认参数stream #39058
已合并
mihudan创建于 6月23日
3 个文件变更+24-3
@@ -186,6 +186,27 @@ class TestNpuGraphEx(TestCase):
186 in4 = torch.randn(1000, 1000, dtype=torch.float16, device="npu")186 in4 = torch.randn(1000, 1000, dtype=torch.float16, device="npu")
187 res = compiled_model(in1, in2, in3, in4)187 res = compiled_model(in1, in2, in3, in4)
188 188 
189+ def test_limit_core_num_with_stream(self):
190+ class Model(torch.nn.Module):
191+ def __init__(self):
192+ super().__init__()
193+ 
194+ def forward(self, in1, in2, in3, in4):
195+ stream1 = torch.npu.Stream()
196+ with torch.npu.npugraph_ex.scope.limit_core_num(4, 5, stream=stream1):
197+ mm_result = torch.mm(in3, in4)
198+ with torch.npu.stream(stream1):
199+ add_result = torch.add(in1, in2)
200+ mm1_result = torch.mm(in3, in4)
201+ return add_result, mm_result, mm1_result
202+ 
203+ model = Model().npu()
204+ compiled_model = torch.compile(model, backend="npugraph_ex", fullgraph=True, dynamic=False)
205+ in1 = torch.randn(1000, 1000, dtype=torch.float16, device="npu")
206+ in2 = torch.randn(1000, 1000, dtype=torch.float16, device="npu")
207+ in3 = torch.randn(1000, 1000, dtype=torch.float16, device="npu")
208+ in4 = torch.randn(1000, 1000, dtype=torch.float16, device="npu")
209+ res = compiled_model(in1, in2, in3, in4)
189 210 
190if __name__ == "__main__":211if __name__ == "__main__":
191 from torch._dynamo.test_case import run_tests212 from torch._dynamo.test_case import run_tests
@@ -2307,7 +2307,7 @@
2307 "signature": "(cache_bin, print_output=True, file=None)"2307 "signature": "(cache_bin, print_output=True, file=None)"
2308 },2308 },
2309 "torch_npu.npu.npugraph_ex.scope.limit_core_num": {2309 "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)"
2311 },2311 },
atomgit-bot
atomgit-botatomgit-bot6月23日

🔵 Low Priority

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

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

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

likedislike
2312 "torch_npu.npu.npugraph_ex.compile_fx": {2312 "torch_npu.npu.npugraph_ex.compile_fx": {
2313 "signature": "(gm, example_inputs=None, options=None)"2313 "signature": "(gm, example_inputs=None, options=None)"
@@ -1,6 +1,6 @@
1__all__ = ["limit_core_num"]1__all__ = ["limit_core_num"]
2 2 
3 3 
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-botatomgit-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
5 from torch_npu.dynamo.npugraph_ex import scope5 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-botatomgit-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