已合并
test(nn): add NPU tests for global full backward hooks #37875
test(nn): add NPU tests for global full backward hooks #37875
已合并
coconut创建于 6月8日
1 个文件变更+63-0
Atest/nn/test_global_module_full_backward_hooks.py+63-0
@@ -0,0 +1,63 @@
1+"""
2+Add validation cases for torch.nn global module hook APIs on NPU:
3+ 
4+1. PyTorch community lacks direct validations for some global backward hook APIs.
5+2. This file validates torch.nn.modules.module.register_module_full_backward_hook and
6+ torch.nn.modules.module.register_module_full_backward_pre_hook.
7+ 
8+"""
9+ 
10+import torch
11+from torch.testing._internal.common_utils import TestCase, run_tests
12+ 
13+ 
14+device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu"
15+ 
16+ 
17+class TestGlobalModuleFullBackwardHooks(TestCase):
18+ 
19+ def test_register_module_full_backward_hook(self):
20+ module = torch.nn.Sigmoid().to(device_type)
21+ inp = torch.randn(5, 5, device=device_type, requires_grad=True)
22+ sig_x = torch.sigmoid(inp)
23+ calls = []
24+ 
25+ def hook(mod, grad_input, grad_output):
26+ if isinstance(mod, torch.nn.Sigmoid):
27+ calls.append(mod)
28+ return (grad_input[0] * 2,)
29+ return None
30+ 
31+ handle = torch.nn.modules.module.register_module_full_backward_hook(hook)
32+ try:
33+ module(inp).backward(torch.ones(5, 5, device=device_type))
34+ finally:
35+ handle.remove()
36+ 
37+ self.assertEqual(len(calls), 1)
38+ self.assertEqual(inp.grad, sig_x * (1 - sig_x) * 2)
39+ 
40+ def test_register_module_full_backward_pre_hook(self):
41+ module = torch.nn.Sigmoid().to(device_type)
42+ inp = torch.randn(5, 5, device=device_type, requires_grad=True)
43+ sig_x = torch.sigmoid(inp)
44+ calls = []
45+ 
46+ def hook(mod, grad_output):
47+ if isinstance(mod, torch.nn.Sigmoid):
48+ calls.append(mod)
49+ return (grad_output[0] * 0.5,)
50+ return None
51+ 
52+ handle = torch.nn.modules.module.register_module_full_backward_pre_hook(hook)
53+ try:
54+ module(inp).backward(torch.ones(5, 5, device=device_type))
55+ finally:
56+ handle.remove()
57+ 
58+ self.assertEqual(len(calls), 1)
59+ self.assertEqual(inp.grad, sig_x * (1 - sig_x) * 0.5)
60+ 
61+ 
62+if __name__ == "__main__":
63+ run_tests()