已合并
refactor: remove unsupported api interception patches #43782
zhounan创建于 8月5日
refactor: remove unsupported api interception patches #43782
已合并
zhounan创建于 8月5日
zhounan成员
8月5日

【合入来源】

https://gitcode.com/Ascend/pytorch/issues/3682

【修改方案】

移除 torch_npu 对 6 个 PyTorch 模块方法(nn.Module.share_memorytorch.jit.ScriptModule.add_module / bfloat16 / register_buffer / register_parameter / register_module)的 monkey-patch 拦截机制,并整体删除torch_npu/utils/npu_intercept.py。其中仍被使用的 CANN 包检测逻辑(_cann_package_check()cann_pytorch_version_map迁移torch_npu/_init/registry/registry_manager.py(其唯一调用点所在)。- test/npu/test_unsupport_api.py:删除 6 项 *_runtimeerror 拦截测试;test_Module_share_memory_runtimeerror 改为断言 NPU 上正常执行;test/jit/test_script_module.pyTestScriptModuleShareMemory.test_share_memory_on_npu_raises改为断言 NPU 上正常执行;TestScriptModuleMetadata 的 2 项 raise 测试删除。

【资料变更】

不涉及

【接口变更】

不涉及

【功能验证】

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

测试 削减前 削减后
test/npu/test_unsupport_api.py 13 passed 7 passed, 6 failed
test/jit/test_script_module.py(ShareMemory/Metadata 类) 11 passed 8 passed, 3 failed

candidate 的 9 项失败全部为原拦截 raise 测试:

  • share_memory / bfloat16:不再抛错(功能可用)——预期行为变更
  • add_module / register_module:原生 RuntimeError(消息不同);
  • register_parameter / register_buffer:原生 AttributeError(类型不同)。

官方用例对照

官方用例 位置 实测结果
test_module_share_memory@onlyPRIVATEUSE1,NPU ) test/test_torch.py 削减后 PASS / 削减前 FAIL(被拦截抛错)——保留拦截补丁会使官方用例失败,删除后通过
test_share_memory(CPU storage shared 语义) test/test_nn.py 削减后 PASS
test_jit.py 中与本修改相关的官方用例全部通过

对进行削减的6个api在npu和gpu环境下进行调用,获取其输出结果,观察削减后两个环境中代码运行结果是否一致

npu运行结果 (Ascend 910B4,PyTorch: 2.13.0)

结果说明:这里主要考虑行为的一致,pass/fail仅表示是否会报错,若fail,则看报错信息是否一致。

削减后

============================================================
1. 构造时调用 (ScriptModule __init__ 中)
============================================================
  PASS | add_module/register_buffer/register_parameter/register_module in __init__

============================================================
2. traced ScriptModule 构造后调用
============================================================
  FAIL | add_module on traced
       | RuntimeError: Cannot re-assign modules in a ScriptModule with non-scripted module, tried to replace existing modul
  PASS | bfloat16 on traced
  FAIL | register_buffer on traced
       | AttributeError: cannot assign buffer before Module.__init__() call
  FAIL | register_parameter on traced
       | AttributeError: cannot assign parameter before Module.__init__() call
  FAIL | register_module on traced
       | RuntimeError: Cannot re-assign modules in a ScriptModule with non-scripted module, tried to replace existing modul

============================================================
3. scripted ScriptModule 构造后调用
============================================================
  FAIL | add_module on scripted
       | RuntimeError: Cannot re-assign modules in a ScriptModule with non-scripted module, tried to replace existing modul
  FAIL | bfloat16 on scripted
       | RuntimeError: bfloat16 is not supported on ScriptModules
  FAIL | register_buffer on scripted
       | RuntimeError: Can't add a new parameter after ScriptModule construction. Tried to add 'b
  FAIL | register_parameter on scripted
       | RuntimeError: Can't add a new parameter after ScriptModule construction. Tried to add 'p
  FAIL | register_module on scripted
       | RuntimeError: Cannot re-assign modules in a ScriptModule with non-scripted module, tried to replace existing modul

============================================================
4. share_memory on nn.Module
============================================================
  PASS | share_memory (no-op, return self, device stays npu)

cuda运行结果(torch==2.13.0+cu126)

============================================================
1. 构造时调用 (ScriptModule __init__ 中)
============================================================
  PASS | add_module/register_buffer/register_parameter/register_module in __init__

============================================================
2. traced ScriptModule 构造后调用
============================================================
  FAIL | add_module on traced
       | RuntimeError: Cannot re-assign modules in a ScriptModule with non-scripted module, tried to replace existing modul
  PASS | bfloat16 on traced
  FAIL | register_buffer on traced
       | AttributeError: cannot assign buffer before Module.__init__() call
  FAIL | register_parameter on traced
       | AttributeError: cannot assign parameter before Module.__init__() call
  FAIL | register_module on traced
       | RuntimeError: Cannot re-assign modules in a ScriptModule with non-scripted module, tried to replace existing modul

============================================================
3. scripted ScriptModule 构造后调用
============================================================
  FAIL | add_module on scripted
       | RuntimeError: Cannot re-assign modules in a ScriptModule with non-scripted module, tried to replace existing modul
  FAIL | bfloat16 on scripted
       | RuntimeError: bfloat16 is not supported on ScriptModules
  FAIL | register_buffer on scripted
       | RuntimeError: Can't add a new parameter after ScriptModule construction. Tried to add 'b
  FAIL | register_parameter on scripted
       | RuntimeError: Can't add a new parameter after ScriptModule construction. Tried to add 'p
  FAIL | register_module on scripted
       | RuntimeError: Cannot re-assign modules in a ScriptModule with non-scripted module, tried to replace existing modul

============================================================
4. share_memory on nn.Module
============================================================
  PASS | share_memory (no-op, return self, device stays cuda)

削减后api调用的测试结果与cuda上一致

【CheckList】

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

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 zhounan 的贡献)
Zzhounan成员
8月5日 创建了 pull request,commit acbff756
Zzhounan成员
8月5日 关联了看板:FrameworkPTAdapter 版本issue看板
ascend-robotascend-robot成员
8月5日 添加了label:stat/needs-squash
ascend-robotascend-robot成员
8月5日 添加了label:ascend-cla/no
此处折叠了280条消息 查看更多
Wwanglijun55成员
27 天前 解决了最后一个问题
ascend-robotascend-robot成员
27 天前 关闭了关联的issue
ascend-robotascend-robot成员
27 天前 合入了pull request
ascend-robot
ascend-robot成员
27 天前 评论:

Pull Request 已合并或已关闭。

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

likedislike
ascend-robot
ascend-robot成员
27 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#13973 [ commitID:fc17a48f ] 已完成
likedislike