|
| 1 | +import unittest |
| 2 | +import onnx |
| 3 | +import torch |
| 4 | +import onnxruntime |
| 5 | +from onnx_diagnostic.ext_test_case import ExtTestCase |
| 6 | +from onnx_diagnostic.reference import OnnxruntimeEvaluator, ExtendedReferenceEvaluator |
| 7 | + |
| 8 | +try: |
| 9 | + from experimental_experiment.torch_interpreter import to_onnx, ExportOptions |
| 10 | +except ImportError: |
| 11 | + to_onnx = None |
| 12 | + |
| 13 | + |
| 14 | +class TestOnnxruntimeEvaluator(ExtTestCase): |
| 15 | + def test_ort_eval_scan_cdist_add(self): |
| 16 | + |
| 17 | + def dist(unused: torch.Tensor, x: torch.Tensor, samex: torch.Tensor): |
| 18 | + sub = samex - x.reshape((1, -1)) |
| 19 | + sq = sub * sub |
| 20 | + rd = torch.sqrt(sq.sum(axis=1)) |
| 21 | + # clone --> UnsupportedAliasMutationException: |
| 22 | + # Combine_fn might be aliasing the input! |
| 23 | + return [unused.clone(), rd] |
| 24 | + |
| 25 | + class ScanModel(torch.nn.Module): |
| 26 | + def forward(self, x): |
| 27 | + z = torch.tensor([0], dtype=torch.float32) |
| 28 | + y = x.clone() |
| 29 | + out = torch.ops.higher_order.scan(dist, [z], [x], additional_inputs=[y]) |
| 30 | + return out[1] |
| 31 | + |
| 32 | + x = torch.tensor([[1, 2, 3, -1], [4, 5, 6, -1], [7, 8, 9, -1]], dtype=torch.float32) |
| 33 | + model = ScanModel() |
| 34 | + expected = model(x) |
| 35 | + onx = to_onnx( |
| 36 | + model, |
| 37 | + (x,), |
| 38 | + optimize=True, |
| 39 | + export_options=ExportOptions(decomposition_table="default", strict=False), |
| 40 | + inline=False, |
| 41 | + ) |
| 42 | + filename = self.get_dump_file("test_ort_eval_scan_cdist_add.onnx") |
| 43 | + onnx.save(onx, filename) |
| 44 | + inits = [i.name for i in onx.graph.initializer] |
| 45 | + self.assertEqual(inits, ["c_lifted_tensor_0"]) |
| 46 | + name = onx.graph.input[0].name |
| 47 | + |
| 48 | + sess = onnxruntime.InferenceSession( |
| 49 | + onx.SerializeToString(), providers=["CPUExecutionProvider"] |
| 50 | + ) |
| 51 | + got = sess.run(None, {name: x.numpy()})[0] |
| 52 | + self.assertEqualArray(expected, got) |
| 53 | + |
| 54 | + ref = ExtendedReferenceEvaluator(onx) |
| 55 | + got = ref.run(None, {name: x.numpy()})[0] |
| 56 | + self.assertEqualArray(expected, got) |
| 57 | + |
| 58 | + orte = OnnxruntimeEvaluator(onx) |
| 59 | + got = orte.run(None, {name: x.numpy()})[0] |
| 60 | + self.assertEqualArray(expected, got) |
| 61 | + |
| 62 | + def test_ort_eval_cond(self): |
| 63 | + import torch |
| 64 | + |
| 65 | + class TwoInputs(torch.nn.Module): |
| 66 | + def forward(self, x, y): |
| 67 | + def true_fn(x, y): |
| 68 | + return torch.sin(x), torch.cos(x) + y |
| 69 | + |
| 70 | + def false_fn(x, y): |
| 71 | + return torch.cos(x), torch.sin(x) + y |
| 72 | + |
| 73 | + return torch.cond(x.sum() > 0, true_fn, false_fn, [x, y]) |
| 74 | + |
| 75 | + x, y = torch.rand(5, 3), torch.rand(5, 3) |
| 76 | + model = TwoInputs() |
| 77 | + onx = to_onnx(model, (x, y), inline=False) |
| 78 | + self.assertEqual(len(onx.functions), 2) |
| 79 | + |
| 80 | + # ExtendedReferenceEvaluator |
| 81 | + ref = ExtendedReferenceEvaluator(onx) |
| 82 | + for _x in (x, -x): |
| 83 | + expected = model(_x, y) |
| 84 | + got = ref.run(None, {"x": _x.detach().numpy(), "y": y.detach().numpy()}) |
| 85 | + self.assertEqual(len(expected), len(got)) |
| 86 | + for e, g in zip(expected, got): |
| 87 | + self.assertEqualArray(e, g, atol=1e-5) |
| 88 | + |
| 89 | + # OnnxruntimeEvaluator |
| 90 | + ref = OnnxruntimeEvaluator(onx) |
| 91 | + |
| 92 | + for _x in (x, -x): |
| 93 | + expected = model(_x, y) |
| 94 | + got = ref.run(None, {"x": _x.detach().numpy(), "y": y.detach().numpy()}) |
| 95 | + self.assertEqual(len(expected), len(got)) |
| 96 | + for e, g in zip(expected, got): |
| 97 | + self.assertEqualArray(e, g, atol=1e-5) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + unittest.main(verbosity=2) |
0 commit comments