|
| 1 | +import unittest |
| 2 | +import onnx.helper as oh |
| 3 | +import torch |
| 4 | +from onnx_diagnostic.ext_test_case import ExtTestCase |
| 5 | +from onnx_diagnostic.export.onnx_plug import EagerDirectReplacementWithOnnx |
| 6 | +from onnx_diagnostic.export.api import to_onnx |
| 7 | + |
| 8 | + |
| 9 | +class TestOnnxPlus(ExtTestCase): |
| 10 | + def test_onnx_plug_verify(self): |
| 11 | + def _test_customadd(x, y): |
| 12 | + return x + y |
| 13 | + |
| 14 | + def _test_customadd_shape(x, y): |
| 15 | + return torch.empty(torch.broadcast_shapes(x.shape, y.shape), dtype=x.dtype) |
| 16 | + |
| 17 | + def make_function_proto(): |
| 18 | + return oh.make_function( |
| 19 | + "onnx_plug", |
| 20 | + "_test_customadd", |
| 21 | + ["x", "y"], |
| 22 | + ["z"], |
| 23 | + [oh.make_node("Add", ["x", "y"], ["z"])], |
| 24 | + opset_imports=[oh.make_opsetid("", 22)], |
| 25 | + ) |
| 26 | + |
| 27 | + rep = EagerDirectReplacementWithOnnx( |
| 28 | + _test_customadd, _test_customadd_shape, make_function_proto(), 2, 1 |
| 29 | + ) |
| 30 | + |
| 31 | + x = torch.randn((3, 4), dtype=torch.float32) |
| 32 | + y = torch.randn((3, 1), dtype=torch.float32) |
| 33 | + self.assertEqualArray(_test_customadd(x, y), x + y) |
| 34 | + res = rep.verify(x, y) |
| 35 | + self.assertEqualAny(res.eager_outputs, (x + y,)) |
| 36 | + self.assertEqual(len(res.diffs), 1) |
| 37 | + self.assertEqual(res.diffs[0]["abs"], 0) |
| 38 | + |
| 39 | + def test_onnx_plug_export(self): |
| 40 | + def _test_customsub(x, y): |
| 41 | + return x - y |
| 42 | + |
| 43 | + def _test_customsub_shape(x, y): |
| 44 | + return torch.empty(torch.broadcast_shapes(x.shape, y.shape), dtype=x.dtype) |
| 45 | + |
| 46 | + def make_function_proto(): |
| 47 | + return oh.make_function( |
| 48 | + "onnx_plug", |
| 49 | + "_test_customsub", |
| 50 | + ["x", "y"], |
| 51 | + ["z"], |
| 52 | + [oh.make_node("Sub", ["x", "y"], ["z"])], |
| 53 | + opset_imports=[oh.make_opsetid("", 22)], |
| 54 | + ) |
| 55 | + |
| 56 | + class Model(torch.nn.Module): |
| 57 | + def forward(self, x): |
| 58 | + y = x.sum(axis=1, keepdim=True) |
| 59 | + d = torch.ops.onnx_plug._test_customsub(x, y) |
| 60 | + return torch.abs(d) |
| 61 | + |
| 62 | + replacements = [ |
| 63 | + EagerDirectReplacementWithOnnx( |
| 64 | + _test_customsub, _test_customsub_shape, make_function_proto(), 2, 1 |
| 65 | + ) |
| 66 | + ] |
| 67 | + |
| 68 | + x = torch.randn((3, 4), dtype=torch.float32) |
| 69 | + model = Model() |
| 70 | + expected = model(x) |
| 71 | + ds = ({0: "d1", 1: "d2"},) |
| 72 | + ep = torch.export.export(model, (x,), dynamic_shapes=self.use_dyn_not_str(ds)) |
| 73 | + self.assertIn("torch.ops.onnx_plug._test_customsub.default", str(ep)) |
| 74 | + got = ep.module()(x) |
| 75 | + self.assertEqualArray(expected, got) |
| 76 | + |
| 77 | + with self.subTest(exporter="custom"): |
| 78 | + onx = to_onnx( |
| 79 | + model, |
| 80 | + (x,), |
| 81 | + dynamic_shapes=ds, |
| 82 | + exporter="custom", |
| 83 | + onnx_plugs=replacements, |
| 84 | + target_opset=22, |
| 85 | + ) |
| 86 | + self.assert_onnx_disc("test_onnx_plug_export_custom", onx.model_proto, model, (x,)) |
| 87 | + |
| 88 | + with self.subTest(exporter="onnx-dynamo"): |
| 89 | + onx = to_onnx( |
| 90 | + model, |
| 91 | + (x,), |
| 92 | + dynamic_shapes=ds, |
| 93 | + exporter="onnx-dynamo", |
| 94 | + onnx_plugs=replacements, |
| 95 | + target_opset=22, |
| 96 | + ) |
| 97 | + self.assert_onnx_disc( |
| 98 | + "test_onnx_plug_export_onnx_dynamo", onx.model_proto, model, (x,) |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + unittest.main(verbosity=2) |
0 commit comments