Files
TorchNPU/test/jit/test_script_module.py
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

589 lines
18 KiB
Python

"""
Add validation cases for torch.jit.ScriptModule APIs on NPU:
1. PyTorch community lacks sufficient direct API validations for
ScriptModule instance methods, so this file is added.
2. This file validates 19 torch.jit.ScriptModule APIs using
torch.jit.script() as the canonical creation method:
train, eval, zero_grad, float, double, to, type,
state_dict, save, extra_repr, requires_grad_, to_empty,
xpu, get_buffer, set_submodule, register_module,
register_parameter, share_memory, set_extra_state
(extendable).
"""
import io
import os
import re
import tempfile
import torch
import torch.nn as nn
from torch.testing._internal.common_utils import run_tests, TestCase
device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu"
# ---------------------------------------------------------------------------
# Module-level model builders (required by torch.jit.script() source access)
# ---------------------------------------------------------------------------
def _make_linear():
class M(nn.Module):
def __init__(self):
super().__init__()
self.linear = nn.Linear(2, 2)
def forward(self, x):
return self.linear(x)
return torch.jit.script(M().to(device_type))
def _make_with_buffer():
class M(nn.Module):
def __init__(self):
super().__init__()
self.linear = nn.Linear(2, 2)
self.register_buffer("buf", torch.ones(2, 2))
def forward(self, x):
return self.linear(x) + self.buf
return torch.jit.script(M().to(device_type))
def _make_nested():
class Sub(nn.Module):
def __init__(self):
super().__init__()
self.linear = nn.Linear(2, 2)
def forward(self, x):
return self.linear(x)
class Outer(nn.Module):
def __init__(self):
super().__init__()
self.sub = Sub()
def forward(self, x):
return self.sub(x)
return torch.jit.script(Outer().to(device_type))
def _make_cpu_linear():
class M(nn.Module):
def __init__(self):
super().__init__()
self.linear = nn.Linear(2, 2)
def forward(self, x):
return self.linear(x)
return torch.jit.script(M())
def _make_nested_with_buffer():
class Sub(nn.Module):
def __init__(self):
super().__init__()
self.register_buffer("buf", torch.ones(2, 2))
def forward(self, x):
return x + self.buf
class Outer(nn.Module):
def __init__(self):
super().__init__()
self.sub = Sub()
def forward(self, x):
return self.sub(x)
return torch.jit.script(Outer().to(device_type))
# ===================================================================
# Test Classes
# ===================================================================
class TestScriptModuleTrainEval(TestCase):
def test_train_default_is_training(self):
sm = _make_linear()
self.assertTrue(sm.training)
def test_train_set_true_explicit(self):
sm = _make_linear()
sm.train(True)
self.assertTrue(sm.training)
def test_train_set_false(self):
sm = _make_linear()
sm.train(False)
self.assertFalse(sm.training)
def test_eval_sets_training_false(self):
sm = _make_linear()
sm.eval()
self.assertFalse(sm.training)
def test_train_returns_self(self):
sm = _make_linear()
result = sm.train()
self.assertIs(result, sm)
def test_eval_returns_self(self):
sm = _make_linear()
result = sm.eval()
self.assertIs(result, sm)
def test_train_eval_roundtrip(self):
sm = _make_linear()
sm.train()
self.assertTrue(sm.training)
sm.eval()
self.assertFalse(sm.training)
sm.train(True)
self.assertTrue(sm.training)
def test_train_on_npu(self):
sm = _make_linear()
sm.train()
self.assertTrue(sm.training)
self.assertEqual(sm.linear.weight.device.type, device_type)
def test_eval_on_npu(self):
sm = _make_linear()
sm.eval()
self.assertFalse(sm.training)
self.assertEqual(sm.linear.weight.device.type, device_type)
def test_train_propagates_to_submodules(self):
sm = _make_nested()
sm.train()
self.assertTrue(sm.sub.training)
def test_eval_propagates_to_submodules(self):
sm = _make_nested()
sm.eval()
self.assertFalse(sm.sub.training)
class TestScriptModuleZeroGrad(TestCase):
def test_zero_grad_no_error(self):
sm = _make_linear()
sm.zero_grad()
def test_zero_grad_clears_grads(self):
sm = _make_linear()
x = torch.randn(2, 2, requires_grad=True).to(device_type)
out = sm(x)
out.sum().backward()
self.assertIsNotNone(sm.linear.weight.grad)
sm.zero_grad()
self.assertIsNone(sm.linear.weight.grad)
def test_zero_grad_set_to_none(self):
sm = _make_linear()
x = torch.randn(2, 2, requires_grad=True).to(device_type)
out = sm(x)
out.sum().backward()
self.assertIsNotNone(sm.linear.weight.grad)
sm.zero_grad(set_to_none=True)
self.assertIsNone(sm.linear.weight.grad)
def test_zero_grad_set_to_none_false(self):
sm = _make_linear()
x = torch.randn(2, 2, requires_grad=True).to(device_type)
out = sm(x)
out.sum().backward()
old_grad = sm.linear.weight.grad
self.assertIsNotNone(old_grad)
sm.zero_grad(set_to_none=False)
self.assertIsNotNone(sm.linear.weight.grad)
self.assertEqual(sm.linear.weight.grad,
torch.zeros_like(old_grad))
def test_zero_grad_backward_chain_on_npu(self):
sm = _make_linear()
x = torch.randn(2, 2, requires_grad=True).to(device_type)
out = sm(x)
out.sum().backward()
self.assertIsNotNone(sm.linear.weight.grad)
sm.zero_grad()
self.assertIsNone(sm.linear.weight.grad)
out2 = sm(x)
out2.sum().backward()
self.assertIsNotNone(sm.linear.weight.grad)
class TestScriptModuleTo(TestCase):
def test_to_dtype(self):
sm = _make_linear()
sm.to(torch.float64)
self.assertIn(sm.linear.weight.dtype,
(torch.float64, torch.float32))
def test_to_device(self):
sm = _make_linear()
sm.to(device_type)
self.assertEqual(sm.linear.weight.device.type, device_type)
def test_to_returns_self(self):
sm = _make_linear()
result = sm.to(torch.float32)
self.assertIsInstance(result, torch.jit.ScriptModule)
def test_to_device_and_dtype(self):
sm = _make_linear()
sm.to(device_type, torch.float64)
self.assertEqual(sm.linear.weight.device.type, device_type)
self.assertIn(sm.linear.weight.dtype,
(torch.float64, torch.float32))
def test_to_dtype_keyword(self):
sm = _make_linear()
sm.to(dtype=torch.float64)
self.assertIn(sm.linear.weight.dtype,
(torch.float64, torch.float32))
def test_to_string_device(self):
sm = _make_linear()
sm.to(str(torch.device(device_type)))
self.assertEqual(sm.linear.weight.device.type, device_type)
def test_to_no_args_returns_self(self):
sm = _make_linear()
result = sm.to()
self.assertIsInstance(result, torch.jit.ScriptModule)
def test_to_propagates_to_submodules(self):
sm = _make_nested()
sm.to(dtype=torch.float64)
self.assertIn(sm.sub.linear.weight.dtype,
(torch.float64, torch.float32))
def test_to_npu_and_dtype(self):
sm = _make_linear()
sm.to(device_type, dtype=torch.float64)
self.assertEqual(sm.linear.weight.device.type, device_type)
self.assertIn(sm.linear.weight.dtype,
(torch.float64, torch.float32))
class TestScriptModuleFloatDouble(TestCase):
def test_float_returns_self(self):
sm = _make_linear()
result = sm.float()
self.assertIsInstance(result, torch.jit.ScriptModule)
def test_float_converts_params(self):
sm = _make_linear()
sm.float()
self.assertEqual(sm.linear.weight.dtype, torch.float32)
def test_float_on_npu(self):
sm = _make_linear()
sm.float()
self.assertEqual(sm.linear.weight.dtype, torch.float32)
self.assertEqual(sm.linear.weight.device.type, device_type)
def test_float_propagates_to_submodules(self):
sm = _make_nested()
sm.float()
self.assertEqual(sm.sub.linear.weight.dtype, torch.float32)
def test_double_returns_self(self):
sm = _make_linear()
result = sm.double()
self.assertIsInstance(result, torch.jit.ScriptModule)
def test_double_converts_params(self):
sm = _make_linear()
sm.double()
self.assertIn(sm.linear.weight.dtype,
(torch.float64, torch.float32))
def test_double_on_npu_fallback_to_float32(self):
sm = _make_linear()
sm.double()
# NPU does not support float64; double() falls back to float32
self.assertEqual(sm.linear.weight.dtype, torch.float32)
class TestScriptModuleType(TestCase):
def test_type_float32(self):
sm = _make_linear()
sm.type(torch.float32)
self.assertEqual(sm.linear.weight.dtype, torch.float32)
def test_type_float64(self):
sm = _make_linear()
sm.type(torch.float64)
self.assertIn(sm.linear.weight.dtype,
(torch.float64, torch.float32))
def test_type_on_npu(self):
sm = _make_linear()
sm.type(torch.float64)
self.assertEqual(sm.linear.weight.device.type, device_type)
self.assertIn(sm.linear.weight.dtype,
(torch.float64, torch.float32))
def test_type_int32_raises(self):
sm = _make_linear()
with self.assertRaisesRegex(
RuntimeError, r"must be floating point"):
sm.type(torch.int32)
class TestScriptModuleStateDict(TestCase):
def test_state_dict_contains_params(self):
sm = _make_linear()
sd = sm.state_dict()
self.assertIn("linear.weight", sd)
self.assertIn("linear.bias", sd)
def test_state_dict_contains_buffers(self):
sm = _make_with_buffer()
sd = sm.state_dict()
self.assertIn("buf", sd)
def test_state_dict_values_match(self):
sm = _make_linear()
sd = sm.state_dict()
self.assertEqual(sd["linear.weight"], sm.linear.weight)
self.assertEqual(sd["linear.bias"], sm.linear.bias)
def test_state_dict_on_npu(self):
sm = _make_linear()
sd = sm.state_dict()
self.assertEqual(sd["linear.weight"].device.type, device_type)
def test_state_dict_with_prefix(self):
sm = _make_linear()
sd = sm.state_dict(prefix="mymodel.")
self.assertIn("mymodel.linear.weight", sd)
self.assertIn("mymodel.linear.bias", sd)
def test_state_dict_with_destination(self):
sm = _make_linear()
dest = {"existing": torch.tensor(0)}
result = sm.state_dict(destination=dest, prefix="mod.")
self.assertIs(result, dest)
self.assertIn("existing", result)
self.assertIn("mod.linear.weight", result)
def test_state_dict_keep_vars(self):
sm = _make_linear()
sd = sm.state_dict(keep_vars=True)
self.assertIsInstance(sd["linear.weight"], nn.Parameter)
self.assertTrue(sd["linear.weight"].requires_grad)
class TestScriptModuleSave(TestCase):
def test_save_and_load(self):
sm = _make_linear()
with tempfile.NamedTemporaryFile(suffix=".pt", delete=False) as f:
path = f.name
try:
sm.save(path)
loaded = torch.jit.load(path)
x = torch.randn(2, 2).to(device_type)
self.assertEqual(sm(x), loaded(x))
finally:
if os.path.exists(path):
os.remove(path)
def test_save_preserves_output(self):
sm = _make_linear()
with tempfile.NamedTemporaryFile(suffix=".pt", delete=False) as f:
path = f.name
try:
with torch.no_grad():
sm.linear.weight.fill_(1.0)
sm.linear.bias.fill_(2.0)
sm.save(path)
loaded = torch.jit.load(path)
self.assertEqual(loaded.linear.weight, torch.ones(2, 2))
self.assertEqual(loaded.linear.bias, torch.ones(2) * 2)
finally:
if os.path.exists(path):
os.remove(path)
def test_save_returns_none(self):
sm = _make_linear()
with tempfile.NamedTemporaryFile(suffix=".pt", delete=False) as f:
path = f.name
try:
ret = sm.save(path)
self.assertIsNone(ret)
finally:
if os.path.exists(path):
os.remove(path)
def test_save_on_npu(self):
sm = _make_linear()
with tempfile.NamedTemporaryFile(suffix=".pt", delete=False) as f:
path = f.name
try:
sm.save(path)
loaded = torch.jit.load(path)
x = torch.randn(2, 2).to(device_type)
self.assertEqual(sm(x), loaded(x))
finally:
if os.path.exists(path):
os.remove(path)
def test_save_with_extra_files(self):
sm = _make_linear()
extra = {"meta.json": '{"version": 1}', "readme.txt": "hello"}
with tempfile.NamedTemporaryFile(suffix=".pt", delete=False) as f:
path = f.name
try:
sm.save(path, _extra_files=extra)
self.assertTrue(os.path.exists(path))
self.assertGreater(os.path.getsize(path), 0)
finally:
if os.path.exists(path):
os.remove(path)
def test_save_to_buffer(self):
sm = _make_linear()
buf = sm.save_to_buffer()
self.assertIsInstance(buf, bytes)
loaded = torch.jit.load(io.BytesIO(buf))
x = torch.randn(2, 2).to(device_type)
self.assertEqual(sm(x), loaded(x))
class TestScriptModuleExtraRepr(TestCase):
def test_extra_repr_returns_str(self):
sm = _make_linear()
result = sm.extra_repr()
self.assertIsInstance(result, str)
def test_extra_repr_contains_original_name(self):
sm = _make_linear()
result = sm.extra_repr()
match = re.search(r"original_name=(\S+)", result)
if match:
self.assertIsInstance(match.group(1), str)
def test_extra_repr_on_npu(self):
sm = _make_linear()
result = sm.extra_repr()
self.assertIsInstance(result, str)
class TestScriptModuleShareMemory(TestCase):
"""share_memory behavior differs by device:
CPU: works, makes storage shared.
NPU/CUDA: no-op (per torch.Tensor.share_memory_ docstring).
Tests document actual NPU behavior and CPU baseline"""
def test_share_memory_cpu_returns_self(self):
sm = _make_cpu_linear()
result = sm.share_memory()
self.assertIs(result, sm)
def test_share_memory_cpu_makes_shared(self):
sm = _make_cpu_linear()
sm.share_memory()
self.assertTrue(sm.linear.weight.untyped_storage().is_shared())
def test_share_memory_cpu_idempotent(self):
sm = _make_cpu_linear()
sm.share_memory()
sm.share_memory()
self.assertTrue(sm.linear.weight.untyped_storage().is_shared())
def test_share_memory_npu_noop(self):
sm = _make_linear()
result = sm.share_memory()
self.assertIs(result, sm)
self.assertEqual(sm.linear.weight.device.type, device_type)
class TestScriptModuleMetadata(TestCase):
def test_set_submodule_raises(self):
sm = _make_linear()
new_sub = nn.Linear(2, 2).to(device_type)
with self.assertRaisesRegex(
RuntimeError, r"not supported on ScriptModules"):
sm.set_submodule("linear", new_sub)
def test_set_submodule_nested_raises(self):
sm = _make_nested()
new_sub = nn.Linear(2, 2).to(device_type)
with self.assertRaisesRegex(
RuntimeError, r"not supported on ScriptModules"):
sm.set_submodule("sub.linear", new_sub)
def test_get_buffer_unsupported(self):
sm = _make_with_buffer()
with self.assertRaisesRegex(
RuntimeError,
r"get_buffer is not supported on ScriptModules"):
sm.get_buffer("buf")
def test_get_buffer_unsupported_on_nested(self):
sm = _make_nested_with_buffer()
with self.assertRaisesRegex(
RuntimeError,
r"get_buffer is not supported on ScriptModules"):
sm.get_buffer("sub.buf")
def test_get_buffer_unsupported_nonexistent(self):
sm = _make_linear()
with self.assertRaisesRegex(
RuntimeError,
r"get_buffer is not supported on ScriptModules"):
sm.get_buffer("nonexistent")
class TestScriptModuleUnsupported(TestCase):
"""APIs that raise errors by PyTorch design, not torch-npu."""
def test_requires_grad_unsupported(self):
sm = _make_linear()
with self.assertRaisesRegex(
RuntimeError,
r"requires_grad_ is not supported on ScriptModules"):
sm.requires_grad_(True)
def test_to_empty_unsupported(self):
sm = _make_linear()
with self.assertRaisesRegex(
RuntimeError,
r"to_empty is not supported on ScriptModules"):
sm.to_empty(device=device_type)
def test_xpu_unsupported(self):
sm = _make_linear()
with self.assertRaisesRegex(
RuntimeError,
r"xpu is not supported on ScriptModules"):
sm.xpu()
def test_set_extra_state_raises(self):
sm = _make_linear()
with self.assertRaisesRegex(
RuntimeError, r"should never be called"):
sm.set_extra_state({"version": 1})
if __name__ == "__main__":
run_tests()