已合并
test(fx): add tests for proxy tensor api #34871
test(fx): add tests for proxy tensor api #34871
已合并
nwww创建于 5月2日
1 个文件变更+83-0
Atest/fx/test_proxy_tensor_api.py+83-0
@@ -0,0 +1,83 @@
1+# Owner(s): ["module: fx"]
2+ 
3+"""
4+Add validation cases for torch.fx.experimental.proxy_tensor APIs on NPU:
5+1. PyTorch community lacks sufficient and direct API validations for
6+ some proxy_tensor APIs, so this file is added.
7+2. This file validates get_proxy_mode, handle_sym_dispatch, make_fx,
8+ maybe_enable_thunkify, and maybe_disable_thunkify (extendable).
9+"""
10+ 
11+import torch
12+from torch.fx import GraphModule
13+from torch.fx.experimental.proxy_tensor import (
14+ get_proxy_mode,
15+ handle_sym_dispatch,
16+ make_fx,
17+ maybe_disable_thunkify,
18+ maybe_enable_thunkify,
19+)
20+from torch.testing._internal.common_utils import run_tests, TestCase
21+ 
22+ 
23+device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu"
24+ 
25+ 
26+class TestProxyTensorAPI(TestCase):
27+ def test_make_fx_returns_graph_module(self):
28+ def fn(x, y):
29+ return torch.relu(x + y)
30+ 
31+ x = torch.randn(2, 3).to(device_type)
32+ y = torch.randn(2, 3).to(device_type)
33+ 
34+ gm = make_fx(fn)(x, y)
35+ 
36+ self.assertIsInstance(gm, GraphModule)
37+ self.assertEqual(gm(x, y), fn(x, y))
38+ self.assertIn("aten", str(gm.graph))
39+ 
40+ def test_get_proxy_mode_during_make_fx(self):
41+ modes = []
42+ 
43+ def fn(x):
44+ modes.append(get_proxy_mode())
45+ return torch.sin(x) + 1
46+ 
47+ self.assertIsNone(get_proxy_mode())
48+ 
49+ x = torch.randn(2, 3).to(device_type)
50+ gm = make_fx(fn)(x)
51+ 
52+ self.assertIsInstance(gm, GraphModule)
53+ self.assertEqual(gm(x), torch.sin(x) + 1)
54+ self.assertGreaterEqual(len(modes), 1)
55+ self.assertIsNotNone(modes[0])
56+ 
57+ def test_handle_sym_dispatch_requires_proxy_mode(self):
58+ def fn(x):
59+ return x
60+ 
61+ self.assertTrue(callable(handle_sym_dispatch))
62+ self.assertIsNone(get_proxy_mode())
63+ 
64+ with self.assertRaises(AssertionError):
65+ handle_sym_dispatch(fn, (3,), {})
66+ 
67+ def test_thunkify_context_managers(self):
68+ def fn(x):
69+ with maybe_enable_thunkify():
70+ y = x + 1
71+ with maybe_disable_thunkify():
72+ z = y * 2
73+ return z
74+ 
75+ x = torch.randn(2, 3).to(device_type)
76+ gm = make_fx(fn)(x)
77+ 
78+ self.assertIsInstance(gm, GraphModule)
79+ self.assertEqual(gm(x), fn(x))
80+ 
81+ 
82+if __name__ == "__main__":
83+ run_tests()