|
| 1 | +import unittest |
| 2 | +from typing import Tuple |
| 3 | +import torch |
| 4 | +from onnxscript import script, FLOAT, INT64 |
| 5 | +from onnxscript import opset18 as op |
| 6 | +from onnx_diagnostic.ext_test_case import ExtTestCase, requires_torch |
| 7 | +from onnx_diagnostic.export.control_flow import enable_code_export_control_flow, loop_for |
| 8 | +from onnx_diagnostic.export.control_flow_research import simple_loop_for as loop_for_r |
| 9 | +from onnx_diagnostic.export.api import to_onnx |
| 10 | + |
| 11 | + |
| 12 | +class TestControlFlow(ExtTestCase): |
| 13 | + @unittest.skip("not working") |
| 14 | + def test_loop_one_research(self): |
| 15 | + class Model(torch.nn.Module): |
| 16 | + def forward(self, n_iter, x): |
| 17 | + def body(i: torch.Tensor, x: torch.Tensor) -> Tuple[torch.Tensor]: |
| 18 | + return (x[: i.item() + 1].unsqueeze(1),) |
| 19 | + |
| 20 | + return loop_for_r(n_iter, body, (x,))[0] |
| 21 | + |
| 22 | + model = Model() |
| 23 | + n_iter = torch.tensor(4, dtype=torch.int64) |
| 24 | + x = torch.arange(10, dtype=torch.float32) |
| 25 | + expected = torch.tensor([0, 0, 1, 0, 1, 2, 0, 1, 2, 3], dtype=x.dtype).unsqueeze(1) |
| 26 | + got = model(n_iter, x) |
| 27 | + self.assertEqualArray(expected, got) |
| 28 | + |
| 29 | + with enable_code_export_control_flow(): |
| 30 | + got = model(n_iter, x) |
| 31 | + self.assertEqualArray(expected, got) |
| 32 | + |
| 33 | + ep = torch.export.export( |
| 34 | + model, (n_iter, x), dynamic_shapes=({}, ({0: torch.export.Dim.DYNAMIC})) |
| 35 | + ) |
| 36 | + print(ep) |
| 37 | + |
| 38 | + def test_onnxscript_loop(self): |
| 39 | + @script() |
| 40 | + def concatenation(N: INT64[1], x: FLOAT[None]) -> FLOAT[None, 1]: |
| 41 | + copy = op.Identity(x) |
| 42 | + res = op.SequenceEmpty() |
| 43 | + for i in range(N): |
| 44 | + res = op.SequenceInsert(res, op.Unsqueeze(copy[:i], [1])) |
| 45 | + return op.ConcatFromSequence(res, axis=1) |
| 46 | + |
| 47 | + onx = concatenation.to_model_proto() |
| 48 | + self.dump_onnx("test_onnxscript_loop.onnx", onx) |
| 49 | + |
| 50 | + @requires_torch("2.9.99") |
| 51 | + def test_loop_one_custom(self): |
| 52 | + class Model(torch.nn.Module): |
| 53 | + def forward(self, n_iter, x): |
| 54 | + def body(i, x): |
| 55 | + return x[: i.item() + 1].unsqueeze(1) |
| 56 | + |
| 57 | + return loop_for(n_iter, body, (x,)) |
| 58 | + |
| 59 | + model = Model() |
| 60 | + n_iter = torch.tensor(4, dtype=torch.int64) |
| 61 | + x = torch.arange(10, dtype=torch.float32) |
| 62 | + expected = torch.tensor([0, 0, 1, 0, 1, 2, 0, 1, 2, 3], dtype=x.dtype).unsqueeze(1) |
| 63 | + got = model(n_iter, x) |
| 64 | + self.assertEqualArray(expected, got) |
| 65 | + |
| 66 | + ep = torch.export.export( |
| 67 | + model, (n_iter, x), dynamic_shapes=({}, ({0: torch.export.Dim.DYNAMIC})) |
| 68 | + ) |
| 69 | + self.assertIn("torch.ops.onnx_higher_ops.loop_for_body_", str(ep)) |
| 70 | + |
| 71 | + onx = to_onnx( |
| 72 | + model, |
| 73 | + (n_iter, x), |
| 74 | + dynamic_shapes=({}, ({0: torch.export.Dim.DYNAMIC})), |
| 75 | + exporter="custom", |
| 76 | + use_control_flow_dispatcher=True, |
| 77 | + ).model_proto |
| 78 | + self.dump_onnx("test_loop_one_custom.onnx", onx) |
| 79 | + self.assert_onnx_disc("test_loop_one_custom", onx, model, (n_iter, x)) |
| 80 | + |
| 81 | + @requires_torch("2.9.99") |
| 82 | + def test_loop_one_custom_different_opset(self): |
| 83 | + class Model(torch.nn.Module): |
| 84 | + def forward(self, n_iter, x): |
| 85 | + def body(i, x): |
| 86 | + return x[: i.item() + 1].unsqueeze(1) |
| 87 | + |
| 88 | + return loop_for(n_iter, body, (x,)) |
| 89 | + |
| 90 | + model = Model() |
| 91 | + n_iter = torch.tensor(4, dtype=torch.int64) |
| 92 | + x = torch.arange(10, dtype=torch.float32) |
| 93 | + expected = torch.tensor([0, 0, 1, 0, 1, 2, 0, 1, 2, 3], dtype=x.dtype).unsqueeze(1) |
| 94 | + got = model(n_iter, x) |
| 95 | + self.assertEqualArray(expected, got) |
| 96 | + |
| 97 | + ep = torch.export.export( |
| 98 | + model, (n_iter, x), dynamic_shapes=({}, ({0: torch.export.Dim.DYNAMIC})) |
| 99 | + ) |
| 100 | + self.assertIn("torch.ops.onnx_higher_ops.loop_for_body_", str(ep)) |
| 101 | + |
| 102 | + onx = to_onnx( |
| 103 | + model, |
| 104 | + (n_iter, x), |
| 105 | + dynamic_shapes=({}, ({0: torch.export.Dim.DYNAMIC})), |
| 106 | + exporter="custom", |
| 107 | + use_control_flow_dispatcher=True, |
| 108 | + target_opset=22, |
| 109 | + ).model_proto |
| 110 | + opsets = {d.domain: d.version for d in onx.opset_import} |
| 111 | + self.assertEqual(opsets[""], 22) |
| 112 | + self.dump_onnx("test_loop_one_custom.onnx", onx) |
| 113 | + self.assert_onnx_disc("test_loop_one_custom", onx, model, (n_iter, x)) |
| 114 | + |
| 115 | + @requires_torch("2.9.99") |
| 116 | + def test_loop_two_custom(self): |
| 117 | + class Model(torch.nn.Module): |
| 118 | + def forward(self, n_iter, x): |
| 119 | + def body(i, x): |
| 120 | + return x[: i.item() + 1].unsqueeze(1), x[: i.item() + 1].unsqueeze(1) + 1 |
| 121 | + |
| 122 | + res = loop_for(n_iter, body, (x,)) |
| 123 | + return res[0] + res[1] |
| 124 | + |
| 125 | + model = Model() |
| 126 | + n_iter = torch.tensor(4, dtype=torch.int64) |
| 127 | + x = torch.arange(10, dtype=torch.float32) |
| 128 | + expected = torch.tensor([1, 1, 3, 1, 3, 5, 1, 3, 5, 7], dtype=x.dtype).unsqueeze(1) |
| 129 | + got = model(n_iter, x) |
| 130 | + self.assertEqualArray(expected, got) |
| 131 | + |
| 132 | + ep = torch.export.export( |
| 133 | + model, (n_iter, x), dynamic_shapes=({}, ({0: torch.export.Dim.DYNAMIC})) |
| 134 | + ) |
| 135 | + self.assertIn("torch.ops.onnx_higher_ops.loop_for_body_", str(ep)) |
| 136 | + |
| 137 | + onx = to_onnx( |
| 138 | + model, |
| 139 | + (n_iter, x), |
| 140 | + dynamic_shapes=({}, ({0: torch.export.Dim.DYNAMIC})), |
| 141 | + exporter="custom", |
| 142 | + use_control_flow_dispatcher=True, |
| 143 | + ).model_proto |
| 144 | + self.dump_onnx("test_loop_one_custom.onnx", onx) |
| 145 | + self.assert_onnx_disc("test_loop_one_custom", onx, model, (n_iter, x)) |
| 146 | + |
| 147 | + @requires_torch("2.9.99") |
| 148 | + def test_loop_two_custom_reduction_dim(self): |
| 149 | + class Model(torch.nn.Module): |
| 150 | + def forward(self, n_iter, x): |
| 151 | + def body(i, x): |
| 152 | + return x[: i.item() + 1].unsqueeze(1), x[: i.item() + 1].unsqueeze(0) + 1 |
| 153 | + |
| 154 | + res = loop_for(n_iter, body, (x,), reduction_dim=[0, 1]) |
| 155 | + return res[0] + res[1].T |
| 156 | + |
| 157 | + model = Model() |
| 158 | + n_iter = torch.tensor(4, dtype=torch.int64) |
| 159 | + x = torch.arange(10, dtype=torch.float32) |
| 160 | + expected = torch.tensor([1, 1, 3, 1, 3, 5, 1, 3, 5, 7], dtype=x.dtype).unsqueeze(1) |
| 161 | + got = model(n_iter, x) |
| 162 | + self.assertEqualArray(expected, got) |
| 163 | + |
| 164 | + ep = torch.export.export( |
| 165 | + model, (n_iter, x), dynamic_shapes=({}, ({0: torch.export.Dim.DYNAMIC})) |
| 166 | + ) |
| 167 | + self.assertIn("torch.ops.onnx_higher_ops.loop_for_body_", str(ep)) |
| 168 | + |
| 169 | + onx = to_onnx( |
| 170 | + model, |
| 171 | + (n_iter, x), |
| 172 | + dynamic_shapes=({}, ({0: torch.export.Dim.DYNAMIC})), |
| 173 | + exporter="custom", |
| 174 | + use_control_flow_dispatcher=True, |
| 175 | + ).model_proto |
| 176 | + self.dump_onnx("test_loop_one_custom.onnx", onx) |
| 177 | + self.assert_onnx_disc("test_loop_one_custom", onx, model, (n_iter, x)) |
| 178 | + |
| 179 | + |
| 180 | +if __name__ == "__main__": |
| 181 | + unittest.main(verbosity=2) |
0 commit comments