|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | +"""Tests for the remove-Expand-before-binary-op fusion rule.""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import unittest |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +import onnx.reference |
| 11 | +import parameterized |
| 12 | + |
| 13 | +import onnxscript.ir as ir |
| 14 | +from onnxscript.rewriter.rules.common import _remove_expand_before_binary_op as mod |
| 15 | + |
| 16 | + |
| 17 | +def _run_model(model: ir.Model, feeds: dict) -> list: |
| 18 | + """Run a model using the ONNX reference evaluator.""" |
| 19 | + proto = ir.to_proto(model) |
| 20 | + ref = onnx.reference.ReferenceEvaluator(proto) |
| 21 | + return ref.run(None, feeds) |
| 22 | + |
| 23 | + |
| 24 | +class RemoveExpandBeforeBinaryOpTest(unittest.TestCase): |
| 25 | + """Tests for _remove_expand_before_binary_op rules.""" |
| 26 | + |
| 27 | + def _apply_and_check( |
| 28 | + self, |
| 29 | + model_text: str, |
| 30 | + expected_count: int, |
| 31 | + expected_op_types: list[str], |
| 32 | + ) -> ir.Model: |
| 33 | + """Helper: apply the rules and verify the result.""" |
| 34 | + model = ir.from_onnx_text(model_text) |
| 35 | + count = mod.expand_before_binary_op_rules.apply_to_model(model) |
| 36 | + self.assertEqual(count, expected_count) |
| 37 | + actual_op_types = [node.op_type for node in model.graph] |
| 38 | + self.assertEqual(actual_op_types, expected_op_types) |
| 39 | + return model |
| 40 | + |
| 41 | + # ------------------------------------------------------------------ |
| 42 | + # Cases where the Expand should be removed |
| 43 | + # ------------------------------------------------------------------ |
| 44 | + |
| 45 | + @parameterized.parameterized.expand( |
| 46 | + [ |
| 47 | + ("Add",), |
| 48 | + ("Sub",), |
| 49 | + ("Mul",), |
| 50 | + ("Div",), |
| 51 | + ] |
| 52 | + ) |
| 53 | + def test_expand_first_input_same_shape_is_removed(self, op_type: str): |
| 54 | + """Expand producing same shape as input should be removed from BinaryOp.""" |
| 55 | + model_text = f""" |
| 56 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 57 | + agraph (float[3, 4] x, float[3, 4] y) => (float[3, 4] output) |
| 58 | + <int64[2] shape = {{3, 4}}> |
| 59 | + {{ |
| 60 | + expanded = Expand(x, shape) |
| 61 | + output = {op_type}(expanded, y) |
| 62 | + }} |
| 63 | + """ |
| 64 | + model = self._apply_and_check(model_text, 1, [op_type]) |
| 65 | + |
| 66 | + # Verify numerical correctness |
| 67 | + x = np.random.randn(3, 4).astype(np.float32) |
| 68 | + y = np.random.randn(3, 4).astype(np.float32) |
| 69 | + original = ir.from_onnx_text(model_text) |
| 70 | + expected = _run_model(original, {"x": x, "y": y}) |
| 71 | + got = _run_model(model, {"x": x, "y": y}) |
| 72 | + np.testing.assert_allclose(got[0], expected[0], rtol=1e-5) |
| 73 | + |
| 74 | + def test_expand_first_input_broadcast_covered_by_other_input(self): |
| 75 | + """Expand from [3, 4] to [4, 3, 4] can be removed when y has shape [4, 3, 4].""" |
| 76 | + model_text = """ |
| 77 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 78 | + agraph (float[3, 4] x, float[4, 3, 4] y) => (float[4, 3, 4] output) |
| 79 | + <int64[3] shape = {4, 3, 4}> |
| 80 | + { |
| 81 | + expanded = Expand(x, shape) |
| 82 | + output = Add(expanded, y) |
| 83 | + } |
| 84 | + """ |
| 85 | + model = self._apply_and_check(model_text, 1, ["Add"]) |
| 86 | + |
| 87 | + x = np.random.randn(3, 4).astype(np.float32) |
| 88 | + y = np.random.randn(4, 3, 4).astype(np.float32) |
| 89 | + original = ir.from_onnx_text(model_text) |
| 90 | + expected = _run_model(original, {"x": x, "y": y}) |
| 91 | + got = _run_model(model, {"x": x, "y": y}) |
| 92 | + np.testing.assert_allclose(got[0], expected[0], rtol=1e-5) |
| 93 | + |
| 94 | + def test_expand_second_input_is_removed(self): |
| 95 | + """Expand on the second input of a binary op should be removed.""" |
| 96 | + model_text = """ |
| 97 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 98 | + agraph (float[4, 3, 4] x, float[3, 4] y) => (float[4, 3, 4] output) |
| 99 | + <int64[3] shape = {4, 3, 4}> |
| 100 | + { |
| 101 | + expanded = Expand(y, shape) |
| 102 | + output = Mul(x, expanded) |
| 103 | + } |
| 104 | + """ |
| 105 | + model = self._apply_and_check(model_text, 1, ["Mul"]) |
| 106 | + |
| 107 | + x = np.random.randn(4, 3, 4).astype(np.float32) |
| 108 | + y = np.random.randn(3, 4).astype(np.float32) |
| 109 | + original = ir.from_onnx_text(model_text) |
| 110 | + expected = _run_model(original, {"x": x, "y": y}) |
| 111 | + got = _run_model(model, {"x": x, "y": y}) |
| 112 | + np.testing.assert_allclose(got[0], expected[0], rtol=1e-5) |
| 113 | + |
| 114 | + def test_expand_with_broadcast_compatible_other_input(self): |
| 115 | + """Expand from [3] to [4, 3] can be removed when y has shape [4, 1].""" |
| 116 | + model_text = """ |
| 117 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 118 | + agraph (float[3] x, float[4, 1] y) => (float[4, 3] output) |
| 119 | + <int64[2] shape = {4, 3}> |
| 120 | + { |
| 121 | + expanded = Expand(x, shape) |
| 122 | + output = Add(expanded, y) |
| 123 | + } |
| 124 | + """ |
| 125 | + model = self._apply_and_check(model_text, 1, ["Add"]) |
| 126 | + |
| 127 | + x = np.random.randn(3).astype(np.float32) |
| 128 | + y = np.random.randn(4, 1).astype(np.float32) |
| 129 | + original = ir.from_onnx_text(model_text) |
| 130 | + expected = _run_model(original, {"x": x, "y": y}) |
| 131 | + got = _run_model(model, {"x": x, "y": y}) |
| 132 | + np.testing.assert_allclose(got[0], expected[0], rtol=1e-5) |
| 133 | + |
| 134 | + def test_expand_sub_first_input_is_removed(self): |
| 135 | + """Expand on the first input of Sub should be removed.""" |
| 136 | + model_text = """ |
| 137 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 138 | + agraph (float[3, 4] x, float[3, 4] y) => (float[3, 4] output) |
| 139 | + <int64[2] shape = {3, 4}> |
| 140 | + { |
| 141 | + expanded = Expand(x, shape) |
| 142 | + output = Sub(expanded, y) |
| 143 | + } |
| 144 | + """ |
| 145 | + model = self._apply_and_check(model_text, 1, ["Sub"]) |
| 146 | + |
| 147 | + x = np.random.randn(3, 4).astype(np.float32) |
| 148 | + y = np.random.randn(3, 4).astype(np.float32) |
| 149 | + original = ir.from_onnx_text(model_text) |
| 150 | + expected = _run_model(original, {"x": x, "y": y}) |
| 151 | + got = _run_model(model, {"x": x, "y": y}) |
| 152 | + np.testing.assert_allclose(got[0], expected[0], rtol=1e-5) |
| 153 | + |
| 154 | + def test_expand_div_second_input_is_removed(self): |
| 155 | + """Expand on the second input of Div should be removed.""" |
| 156 | + model_text = """ |
| 157 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 158 | + agraph (float[4, 3, 4] x, float[3, 4] y) => (float[4, 3, 4] output) |
| 159 | + <int64[3] shape = {4, 3, 4}> |
| 160 | + { |
| 161 | + expanded = Expand(y, shape) |
| 162 | + output = Div(x, expanded) |
| 163 | + } |
| 164 | + """ |
| 165 | + model = self._apply_and_check(model_text, 1, ["Div"]) |
| 166 | + |
| 167 | + x = np.random.randn(4, 3, 4).astype(np.float32) |
| 168 | + y = (np.random.randn(3, 4).astype(np.float32) + 2.0) # avoid division by zero |
| 169 | + original = ir.from_onnx_text(model_text) |
| 170 | + expected = _run_model(original, {"x": x, "y": y}) |
| 171 | + got = _run_model(model, {"x": x, "y": y}) |
| 172 | + np.testing.assert_allclose(got[0], expected[0], rtol=1e-5) |
| 173 | + |
| 174 | + # ------------------------------------------------------------------ |
| 175 | + # Cases where the Expand should NOT be removed |
| 176 | + # ------------------------------------------------------------------ |
| 177 | + |
| 178 | + def test_expand_changes_output_shape_not_removed(self): |
| 179 | + """Expand that changes the output shape compared to direct broadcast must be kept.""" |
| 180 | + # x has shape [3], expand to [4, 3], other is a scalar. |
| 181 | + # With expand: broadcast([4, 3], []) = [4, 3] |
| 182 | + # Without expand: broadcast([3], []) = [3] <- different! |
| 183 | + model_text = """ |
| 184 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 185 | + agraph (float[3] x) => (float[4, 3] output) |
| 186 | + <int64[2] shape = {4, 3}, float[1] one = {1.0}> |
| 187 | + { |
| 188 | + expanded = Expand(x, shape) |
| 189 | + output = Add(expanded, one) |
| 190 | + } |
| 191 | + """ |
| 192 | + model = ir.from_onnx_text(model_text) |
| 193 | + count = mod.expand_before_binary_op_rules.apply_to_model(model) |
| 194 | + self.assertEqual(count, 0) |
| 195 | + |
| 196 | + def test_expand_target_shape_not_constant_not_removed(self): |
| 197 | + """Expand with a dynamic (non-constant) shape cannot be removed.""" |
| 198 | + model_text = """ |
| 199 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 200 | + agraph (float[3, 4] x, float[3, 4] y, int64[2] shape) => (float[3, 4] output) |
| 201 | + { |
| 202 | + expanded = Expand(x, shape) |
| 203 | + output = Add(expanded, y) |
| 204 | + } |
| 205 | + """ |
| 206 | + model = ir.from_onnx_text(model_text) |
| 207 | + count = mod.expand_before_binary_op_rules.apply_to_model(model) |
| 208 | + self.assertEqual(count, 0) |
| 209 | + |
| 210 | + def test_expand_unknown_input_shape_not_removed(self): |
| 211 | + """Expand cannot be removed when the input shape is not statically known.""" |
| 212 | + # No shape annotation on 'x' |
| 213 | + model_text = """ |
| 214 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 215 | + agraph (float[N] x, float[3, 4] y) => (float[3, 4] output) |
| 216 | + <int64[2] shape = {3, 4}> |
| 217 | + { |
| 218 | + expanded = Expand(x, shape) |
| 219 | + output = Add(expanded, y) |
| 220 | + } |
| 221 | + """ |
| 222 | + model = ir.from_onnx_text(model_text) |
| 223 | + count = mod.expand_before_binary_op_rules.apply_to_model(model) |
| 224 | + self.assertEqual(count, 0) |
| 225 | + |
| 226 | + |
| 227 | +if __name__ == "__main__": |
| 228 | + unittest.main() |
0 commit comments