已合并
[test] add tests for torch.fx Graph APIs #34402
lihaokun-2026创建于 4月25日
[test] add tests for torch.fx Graph APIs #34402
已合并
共 1 个文件变更+122-0
| @@ -0,0 +1,122 @@ | |||
| 1 | +import contextlib | ||
| 2 | +import io | ||
| 3 | +import unittest | ||
| 4 | + | ||
| 5 | +import torch | ||
| 6 | +import torch_npu | ||
| 7 | +from torch.fx import Graph, GraphModule | ||
| 8 | +from torch.testing._internal.common_utils import TestCase, run_tests | ||
| 9 | + | ||
| 10 | + | ||
| 11 | +class TestFxGraphApi(TestCase): | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + def test_graph_placeholder_with_npu_tensor(self): | ||
| 15 | + graph = Graph() | ||
| 16 | + | ||
| 17 | + x = graph.placeholder("x") | ||
| 18 | + y = graph.placeholder("y") | ||
| 19 | + | ||
| 20 | + self.assertEqual(x.op, "placeholder") | ||
| 21 | + self.assertEqual(x.target, "x") | ||
| 22 | + self.assertEqual(y.op, "placeholder") | ||
| 23 | + self.assertEqual(y.target, "y") | ||
| 24 | + | ||
| 25 | + add_node = graph.call_function(torch.ops.aten.add.Tensor, args=(x, y)) | ||
| 26 | + graph.output(add_node) | ||
| 27 | + | ||
| 28 | + gm = GraphModule({}, graph) | ||
| 29 | + | ||
| 30 | + cpu_x = torch.randn(2, 3) | ||
| 31 | + cpu_y = torch.randn(2, 3) | ||
| 32 | + | ||
| 33 | + npu_x = cpu_x.npu() | ||
| 34 | + npu_y = cpu_y.npu() | ||
| 35 | + | ||
| 36 | + cpu_out = cpu_x + cpu_y | ||
| 37 | + npu_out = gm(npu_x, npu_y).cpu() | ||
| 38 | + | ||
| 39 | + self.assertTrue(torch.allclose(cpu_out, npu_out, rtol=1e-3, atol=1e-3)) | ||
| 40 | + | ||
| 41 | + | ||
| 42 | + def test_graph_output_node_with_npu_tensor(self): | ||
| 43 | + graph = Graph() | ||
| 44 | + | ||
| 45 | + x = graph.placeholder("x") | ||
| 46 | + neg_node = graph.call_function(torch.ops.aten.neg.default, args=(x,)) | ||
| 47 | + graph.output(neg_node) | ||
| 48 | + | ||
| 49 | + output_node = graph.output_node() | ||
| 50 | + | ||
| 51 | + self.assertIsNotNone(output_node) | ||
| 52 | + self.assertEqual(output_node.op, "output") | ||
| 53 | + self.assertEqual(output_node.args[0], neg_node) | ||
| 54 | + | ||
| 55 | + gm = GraphModule({}, graph) | ||
| 56 | + | ||
| 57 | + cpu_x = torch.randn(4, 4) | ||
| 58 | + npu_x = cpu_x.npu() | ||
| 59 | + | ||
| 60 | + cpu_out = torch.neg(cpu_x) | ||
| 61 | + npu_out = gm(npu_x).cpu() | ||
| 62 | + | ||
| 63 | + self.assertTrue(torch.allclose(cpu_out, npu_out, rtol=1e-3, atol=1e-3)) | ||
| 64 | + | ||
| 65 | + | ||
| 66 | + def test_graph_print_tabular_with_npu_meta(self): | ||
| 67 | + try: | ||
| 68 | + import tabulate # noqa: F401 | ||
| 69 | + except ImportError: | ||
| 70 | + self.skipTest("tabulate is not installed") | ||
| 71 | + | ||
| 72 | + graph = Graph() | ||
| 73 | + | ||
| 74 | + x = graph.placeholder("x") | ||
| 75 | + x.meta["example_value"] = torch.randn(2, 3).npu() | ||
| 76 | + | ||
| 77 | + relu_node = graph.call_function(torch.ops.aten.relu.default, args=(x,)) | ||
| 78 | + graph.output(relu_node) | ||
| 79 | + | ||
| 80 | + buffer = io.StringIO() | ||
| 81 | + with contextlib.redirect_stdout(buffer): | ||
| 82 | + graph.print_tabular() | ||
| 83 | + | ||
| 84 | + output = buffer.getvalue() | ||
| 85 | + | ||
| 86 | + self.assertIn("placeholder", output) | ||
| 87 | + self.assertIn("call_function", output) | ||
| 88 | + self.assertIn("output", output) | ||
| 89 | + | ||
| 90 | + | ||
| 91 | + def test_graph_process_inputs_with_npu_tensor(self): | ||
| 92 | + graph = Graph() | ||
| 93 | + | ||
| 94 | + cpu_x = torch.randn(2, 3) | ||
| 95 | + cpu_y = torch.randn(2, 3) | ||
| 96 | + | ||
| 97 | + npu_x = cpu_x.npu() | ||
| 98 | + npu_y = cpu_y.npu() | ||
| 99 | + | ||
| 100 | + processed_inputs = graph.process_inputs(npu_x, npu_y) | ||
| 101 | + | ||
| 102 | + self.assertEqual(len(processed_inputs), 2) | ||
| 103 | + self.assertTrue(processed_inputs[0].is_npu) | ||
| 104 | + self.assertTrue(processed_inputs[1].is_npu) | ||
| 105 | + self.assertTrue(torch.allclose(processed_inputs[0].cpu(), cpu_x, rtol=1e-3, atol=1e-3)) | ||
| 106 | + self.assertTrue(torch.allclose(processed_inputs[1].cpu(), cpu_y, rtol=1e-3, atol=1e-3)) | ||
| 107 | + | ||
| 108 | + | ||
| 109 | + def test_graph_process_outputs_with_npu_tensor(self): | ||
| 110 | + graph = Graph() | ||
| 111 | + | ||
| 112 | + cpu_out = torch.randn(2, 3) | ||
| 113 | + npu_out = cpu_out.npu() | ||
| 114 | + | ||
| 115 | + processed_output = graph.process_outputs(npu_out) | ||
| 116 | + | ||
| 117 | + self.assertTrue(processed_output.is_npu) | ||
| 118 | + self.assertTrue(torch.allclose(processed_output.cpu(), cpu_out, rtol=1e-3, atol=1e-3)) | ||
| 119 | + | ||
| 120 | + | ||
| 121 | +if __name__ == "__main__": | ||
| 122 | + run_tests() | ||