已合并
test(fx): add _get_qualified_name alignment test case [v2.9.0] #39601
test(fx): add _get_qualified_name alignment test case [v2.9.0] #39601
已合并
zkx创建于 6月30日
1 个文件变更+60-0
Atest/test_fx_node_npu.py+60-0
@@ -0,0 +1,60 @@
1+# Owner(s): ["module: fx"]
2+"""
3+Add validation cases for torch.fx.node APIs on NPU:
4+1. PyTorch community lacks dedicated test cases for
5+ torch.fx.node._get_qualified_name, so this file is added.
6+2. This file validates torch.fx.node._get_qualified_name.
7+ torch.fx.Node already has comprehensive tests in
8+ PyTorch upstream test/test_fx.py and requires no NPU adaptation
9+ (pure graph IR, no device dependency).
10+"""
11+import operator
12+ 
13+import torch
14+ 
15+from torch.testing._internal.common_utils import TestCase, run_tests
16+from torch.fx.node import _get_qualified_name
17+ 
18+ 
19+class TestFXNodeGetQualifiedName(TestCase):
20+ """Test torch.fx.node._get_qualified_name function."""
21+ 
22+ def test_builtin_function(self):
23+ """Test _get_qualified_name with builtin functions."""
24+ result = _get_qualified_name(getattr)
25+ self.assertEqual(result, "getattr")
26+ 
27+ def test_torch_function(self):
28+ """Test _get_qualified_name with torch module functions."""
29+ result = _get_qualified_name(torch.relu)
30+ self.assertEqual(result, "torch.relu")
31+ 
32+ def test_torch_binary_function(self):
33+ """Test _get_qualified_name with torch namespace function."""
34+ result = _get_qualified_name(torch.add)
35+ self.assertEqual(result, "torch.add")
36+ 
37+ def test_operator_function(self):
38+ """Test _get_qualified_name with operator module functions."""
39+ result = _get_qualified_name(operator.add)
40+ self.assertEqual(result, "_operator.add")
41+ 
42+ def test_tensor_method(self):
43+ """Test _get_qualified_name with torch.Tensor methods."""
44+ result = _get_qualified_name(torch.Tensor.add)
45+ self.assertEqual(result, "torch.Tensor.add")
46+ 
47+ def test_submodule_function(self):
48+ """Test _get_qualified_name with sub-module function."""
49+ result = _get_qualified_name(torch.nn.functional.relu)
50+ self.assertEqual(result, "torch.nn.functional.relu")
51+ 
52+ def test_consistency_on_repeated_calls(self):
53+ """Test _get_qualified_name returns consistent results."""
54+ results = [_get_qualified_name(torch.abs) for _ in range(5)]
55+ for r in results:
56+ self.assertEqual(r, "torch.abs")
57+ 
58+ 
59+if __name__ == "__main__":
60+ run_tests()