Files
TorchNPU/test/jit
zhounan0730 0a0f799ed9 refactor: remove unsupported api interception patches
Co-authored-by: zhounan0730<zhounan46@huawei.com>



# message auto-generated for no-merge-commit merge:
!43782 merge master into master

refactor: remove unsupported api interception patches

Created-by: zhounan0730
Commit-by: zhounan0730
Merged-by: ascend-robot
Description: <!--
PR描述模板更新日期:20260203
-->

# 【合入来源】
https://gitcode.com/Ascend/pytorch/issues/3682

- [ ] 需求
- [ ] 问题单
- [ ] issue/工单
- [x] 重构优化
- [ ] 资料更新

# 【修改方案】
 移除 torch_npu 对 6 个 PyTorch 模块方法(`nn.Module.share_memory` 与 `torch.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.py`:`TestScriptModuleShareMemory.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]

- [x] 代码注释完备,正********确记录错误日志
- [x] 代码实现进行了返回值、空指针等校验
- [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
- [x] PR持续集成流水线(CI)执行通过,代码检查无异常


See merge request: Ascend/pytorch!43782
2026-08-13 17:36:57 +08:00
..