|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import unittest |
| 6 | + |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +from onnxscript import ir |
| 10 | +from onnxscript.rewriter import testing |
| 11 | +from onnxscript.rewriter.rules.common import _materialize_reshape_shape |
| 12 | + |
| 13 | + |
| 14 | +class MaterializeReshapeShapeTest(unittest.TestCase): |
| 15 | + def test_fully_static_output_shape_materializes(self): |
| 16 | + """When output shape is fully static, replace dynamic shape input with constant.""" |
| 17 | + model = ir.from_onnx_text( |
| 18 | + """ |
| 19 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 20 | + agraph (float[6] data) => (float[2, 3] output) |
| 21 | + { |
| 22 | + shape = Shape(data) |
| 23 | + output = Reshape(data, shape) |
| 24 | + } |
| 25 | + """ |
| 26 | + ) |
| 27 | + for node in model.graph: |
| 28 | + if node.op_type == "Reshape": |
| 29 | + node.outputs[0].shape = ir.Shape([2, 3]) |
| 30 | + break |
| 31 | + count = _materialize_reshape_shape.rules.apply_to_model(model) |
| 32 | + self.assertEqual(count, 1) |
| 33 | + reshape_nodes = [n for n in model.graph if n.op_type == "Reshape"] |
| 34 | + self.assertEqual(len(reshape_nodes), 1) |
| 35 | + shape_input = reshape_nodes[0].inputs[1] |
| 36 | + self.assertIsNotNone(shape_input.const_value) |
| 37 | + self.assertEqual(shape_input.const_value.numpy().tolist(), [2, 3]) |
| 38 | + |
| 39 | + def test_one_symbolic_dim_uses_minus_one(self): |
| 40 | + """When output has one symbolic dim, replace it with -1.""" |
| 41 | + model = ir.from_onnx_text( |
| 42 | + """ |
| 43 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 44 | + agraph (float[6] data) => (float[B, 3] output) |
| 45 | + { |
| 46 | + shape = Shape(data) |
| 47 | + output = Reshape(data, shape) |
| 48 | + } |
| 49 | + """ |
| 50 | + ) |
| 51 | + for node in model.graph: |
| 52 | + if node.op_type == "Reshape": |
| 53 | + node.outputs[0].shape = ir.Shape(["B", 3]) |
| 54 | + break |
| 55 | + count = _materialize_reshape_shape.rules.apply_to_model(model) |
| 56 | + self.assertEqual(count, 1) |
| 57 | + reshape_nodes = [n for n in model.graph if n.op_type == "Reshape"] |
| 58 | + self.assertEqual(len(reshape_nodes), 1) |
| 59 | + shape_input = reshape_nodes[0].inputs[1] |
| 60 | + self.assertIsNotNone(shape_input.const_value) |
| 61 | + self.assertEqual(shape_input.const_value.numpy().tolist(), [-1, 3]) |
| 62 | + |
| 63 | + def test_two_symbolic_dims_not_materialized(self): |
| 64 | + """When output has two symbolic dims, the rule should not fire.""" |
| 65 | + model = ir.from_onnx_text( |
| 66 | + """ |
| 67 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 68 | + agraph (float[6] data) => (float[B, C] output) |
| 69 | + { |
| 70 | + shape = Shape(data) |
| 71 | + output = Reshape(data, shape) |
| 72 | + } |
| 73 | + """ |
| 74 | + ) |
| 75 | + for node in model.graph: |
| 76 | + if node.op_type == "Reshape": |
| 77 | + node.outputs[0].shape = ir.Shape(["B", "C"]) |
| 78 | + break |
| 79 | + count = _materialize_reshape_shape.rules.apply_to_model(model) |
| 80 | + self.assertEqual(count, 0) |
| 81 | + |
| 82 | + def test_constant_shape_input_not_replaced(self): |
| 83 | + """When the shape input is already a constant, the rule should not fire.""" |
| 84 | + model = ir.from_onnx_text( |
| 85 | + """ |
| 86 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 87 | + agraph (float[6] data) => (float[2, 3] output) |
| 88 | + { |
| 89 | + shape = Constant<value: tensor = int64[2] {2, 3}>() |
| 90 | + output = Reshape(data, shape) |
| 91 | + } |
| 92 | + """ |
| 93 | + ) |
| 94 | + count = _materialize_reshape_shape.rules.apply_to_model(model) |
| 95 | + self.assertEqual(count, 0) |
| 96 | + |
| 97 | + def test_unknown_output_shape_not_materialized(self): |
| 98 | + """When the output shape is unknown, the rule should not fire.""" |
| 99 | + model = ir.from_onnx_text( |
| 100 | + """ |
| 101 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 102 | + agraph (float[6] data) => (float output) |
| 103 | + { |
| 104 | + shape = Shape(data) |
| 105 | + output = Reshape(data, shape) |
| 106 | + } |
| 107 | + """ |
| 108 | + ) |
| 109 | + for node in model.graph: |
| 110 | + if node.op_type == "Reshape": |
| 111 | + node.outputs[0].shape = None |
| 112 | + break |
| 113 | + count = _materialize_reshape_shape.rules.apply_to_model(model) |
| 114 | + self.assertEqual(count, 0) |
| 115 | + |
| 116 | + def test_allowzero_attribute_preserved(self): |
| 117 | + """The allowzero attribute should be preserved on the new Reshape.""" |
| 118 | + model = ir.from_onnx_text( |
| 119 | + """ |
| 120 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 121 | + agraph (float[6] data) => (float[2, 3] output) |
| 122 | + { |
| 123 | + shape = Shape(data) |
| 124 | + output = Reshape<allowzero=1>(data, shape) |
| 125 | + } |
| 126 | + """ |
| 127 | + ) |
| 128 | + for node in model.graph: |
| 129 | + if node.op_type == "Reshape": |
| 130 | + node.outputs[0].shape = ir.Shape([2, 3]) |
| 131 | + break |
| 132 | + count = _materialize_reshape_shape.rules.apply_to_model(model) |
| 133 | + self.assertEqual(count, 1) |
| 134 | + reshape_nodes = [n for n in model.graph if n.op_type == "Reshape"] |
| 135 | + self.assertEqual(len(reshape_nodes), 1) |
| 136 | + allowzero = reshape_nodes[0].attributes.get_int("allowzero", 0) |
| 137 | + self.assertEqual(allowzero, 1) |
| 138 | + |
| 139 | + def test_numerical_correctness_static(self): |
| 140 | + """Verify numerical equivalence for fully static materialization.""" |
| 141 | + # Build a model where a dynamic Concat produces the shape for Reshape. |
| 142 | + # After materialization, the Reshape uses a constant shape. |
| 143 | + model_text = """ |
| 144 | + <ir_version: 7, opset_import: [ "" : 17]> |
| 145 | + agraph (float[12] data, float[3, 4] ref) => (float[3, 4] output) |
| 146 | + { |
| 147 | + shape = Shape(ref) |
| 148 | + output = Reshape(data, shape) |
| 149 | + } |
| 150 | + """ |
| 151 | + original = ir.from_onnx_text(model_text) |
| 152 | + model = ir.from_onnx_text(model_text) |
| 153 | + for node in model.graph: |
| 154 | + if node.op_type == "Reshape": |
| 155 | + node.outputs[0].shape = ir.Shape([3, 4]) |
| 156 | + break |
| 157 | + _materialize_reshape_shape.rules.apply_to_model(model) |
| 158 | + testing.assert_numerically_equal( |
| 159 | + original, |
| 160 | + model, |
| 161 | + ( |
| 162 | + np.arange(12).astype(np.float32), |
| 163 | + np.zeros((3, 4), dtype=np.float32), |
| 164 | + ), |
| 165 | + ) |
| 166 | + |
| 167 | + |
| 168 | +if __name__ == "__main__": |
| 169 | + unittest.main() |
0 commit comments