|
| 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.ir.passes.common import shape_inference |
| 11 | + |
| 12 | + |
| 13 | +class TestShapeInferencePass(unittest.TestCase): |
| 14 | + def test_pass(self): |
| 15 | + # Create a simple ONNX model with shape inference |
| 16 | + # Define the model |
| 17 | + inputs = [ |
| 18 | + ir.Value( |
| 19 | + name="input_a", type=ir.TensorType(ir.DataType.FLOAT), shape=ir.Shape((1, 2)) |
| 20 | + ), |
| 21 | + ir.Value( |
| 22 | + name="input_b", type=ir.TensorType(ir.DataType.FLOAT), shape=ir.Shape((1, 2)) |
| 23 | + ), |
| 24 | + ] |
| 25 | + |
| 26 | + add_node = ir.Node("", "Add", inputs=inputs) |
| 27 | + |
| 28 | + model = ir.Model( |
| 29 | + ir.Graph( |
| 30 | + inputs=inputs, |
| 31 | + outputs=add_node.outputs, |
| 32 | + nodes=[add_node], |
| 33 | + opset_imports={"": 20}, |
| 34 | + ), |
| 35 | + ir_version=10, |
| 36 | + ) |
| 37 | + self.assertIsNone(add_node.outputs[0].shape) |
| 38 | + self.assertIsNone(add_node.outputs[0].dtype) |
| 39 | + |
| 40 | + # Perform shape inference |
| 41 | + result = shape_inference.ShapeInferencePass()(model) |
| 42 | + self.assertTrue(result.modified) |
| 43 | + self.assertEqual(result.model.graph.node(0).outputs[0].shape, ir.Shape((1, 2))) |
| 44 | + self.assertEqual(result.model.graph.node(0).outputs[0].dtype, ir.DataType.FLOAT) |
| 45 | + self.assertEqual(result.model.graph.outputs[0].shape, ir.Shape((1, 2))) |
| 46 | + self.assertEqual(result.model.graph.outputs[0].dtype, ir.DataType.FLOAT) |
| 47 | + |
| 48 | + def test_pass_with_initializers(self): |
| 49 | + # _BIG_TENSOR_SIZE_LIMIT is in bytes, but we create big_dim as size |
| 50 | + # of a tensor. This is fine as we just need to create a big tensor whose size |
| 51 | + # passes _BIG_TENSOR_SIZE_LIMIT |
| 52 | + big_dim = shape_inference._BIG_TENSOR_SIZE_LIMIT * 2 # pylint: disable=protected-access |
| 53 | + inputs = [ |
| 54 | + ir.Value( |
| 55 | + name="input_a", type=ir.TensorType(ir.DataType.FLOAT), shape=ir.Shape((1, 2)) |
| 56 | + ), |
| 57 | + ir.Value( |
| 58 | + name="input_b", |
| 59 | + type=ir.TensorType(ir.DataType.FLOAT), |
| 60 | + shape=ir.Shape((big_dim, 1)), |
| 61 | + const_value=ir.tensor([[42]] * big_dim, dtype=ir.DataType.FLOAT), |
| 62 | + ), |
| 63 | + ] |
| 64 | + |
| 65 | + # Shape and type are not explicitly set for the initializer but it should still work |
| 66 | + initializer = ir.Value( |
| 67 | + name="initializer", const_value=ir.tensor([[2, 3]], dtype=ir.DataType.FLOAT) |
| 68 | + ) |
| 69 | + |
| 70 | + add_node = ir.Node("", "Add", inputs=[*inputs]) |
| 71 | + mul_node = ir.Node("", "Mul", inputs=[add_node.outputs[0], initializer]) |
| 72 | + |
| 73 | + model = ir.Model( |
| 74 | + graph := ir.Graph( |
| 75 | + inputs=inputs, |
| 76 | + outputs=mul_node.outputs, |
| 77 | + nodes=[add_node, mul_node], |
| 78 | + opset_imports={"": 20}, |
| 79 | + ), |
| 80 | + ir_version=10, |
| 81 | + ) |
| 82 | + graph.register_initializer(inputs[1]) |
| 83 | + graph.register_initializer(initializer) |
| 84 | + |
| 85 | + self.assertIsNone(add_node.outputs[0].shape) |
| 86 | + self.assertIsNone(add_node.outputs[0].dtype) |
| 87 | + self.assertIsNone(mul_node.outputs[0].shape) |
| 88 | + self.assertIsNone(mul_node.outputs[0].dtype) |
| 89 | + self.assertIsNone(initializer.shape) |
| 90 | + self.assertIsNone(initializer.dtype) |
| 91 | + |
| 92 | + # Perform shape inference |
| 93 | + result = shape_inference.ShapeInferencePass()(model) |
| 94 | + self.assertTrue(result.modified) |
| 95 | + self.assertEqual(result.model.graph.node(0).outputs[0].shape, ir.Shape((big_dim, 2))) |
| 96 | + self.assertEqual(result.model.graph.node(0).outputs[0].dtype, ir.DataType.FLOAT) |
| 97 | + self.assertEqual(result.model.graph.node(1).outputs[0].shape, ir.Shape((big_dim, 2))) |
| 98 | + self.assertEqual(result.model.graph.node(1).outputs[0].dtype, ir.DataType.FLOAT) |
| 99 | + self.assertEqual( |
| 100 | + result.model.graph.initializers["initializer"].shape, ir.Shape((1, 2)) |
| 101 | + ) |
| 102 | + self.assertEqual( |
| 103 | + result.model.graph.initializers["initializer"].dtype, ir.DataType.FLOAT |
| 104 | + ) |
| 105 | + self.assertEqual(result.model.graph.outputs[0].shape, ir.Shape((big_dim, 2))) |
| 106 | + self.assertEqual(result.model.graph.outputs[0].dtype, ir.DataType.FLOAT) |
| 107 | + |
| 108 | + # Check that the initializer correctly appears in the result |
| 109 | + self.assertEqual(len(result.model.graph.inputs), 2) |
| 110 | + self.assertEqual(len(result.model.graph.initializers), 2) |
| 111 | + np.testing.assert_array_equal( |
| 112 | + result.model.graph.initializers["input_b"].const_value.numpy(), |
| 113 | + np.array([[42]] * big_dim, dtype=np.float32), |
| 114 | + strict=True, |
| 115 | + ) |
| 116 | + self.assertEqual( |
| 117 | + result.model.graph.initializers["input_b"].const_value.dtype, |
| 118 | + ir.DataType.FLOAT, |
| 119 | + ) |
| 120 | + np.testing.assert_array_equal( |
| 121 | + result.model.graph.initializers["initializer"].const_value.numpy(), |
| 122 | + np.array([[2.0, 3.0]], dtype=np.float32), |
| 123 | + strict=True, |
| 124 | + ) |
| 125 | + self.assertEqual( |
| 126 | + result.model.graph.initializers["initializer"].const_value.dtype, |
| 127 | + ir.DataType.FLOAT, |
| 128 | + ) |
| 129 | + |
| 130 | + # Check that the original model is not modified |
| 131 | + self.assertIsNone(add_node.outputs[0].shape) |
| 132 | + self.assertIsNone(add_node.outputs[0].dtype) |
| 133 | + self.assertIsNone(mul_node.outputs[0].shape) |
| 134 | + self.assertIsNone(mul_node.outputs[0].dtype) |
| 135 | + self.assertEqual(len(model.graph.inputs), 2) |
| 136 | + self.assertEqual(len(model.graph.initializers), 2) |
| 137 | + self.assertIs(model.graph.initializers["input_b"].const_value, inputs[1].const_value) |
| 138 | + self.assertEqual(len(model.graph.outputs), 1) |
| 139 | + self.assertEqual(model.graph.outputs[0].shape, None) |
| 140 | + self.assertEqual(model.graph.outputs[0].dtype, None) |
| 141 | + # Check that the initializer is not modified |
| 142 | + self.assertIs( |
| 143 | + model.graph.initializers["initializer"].const_value, initializer.const_value |
| 144 | + ) |
| 145 | + |
| 146 | + |
| 147 | +if __name__ == "__main__": |
| 148 | + unittest.main() |
0 commit comments