|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | + |
| 4 | +"""Unit Tests for einsum decomposition.""" |
| 5 | + |
| 6 | +import unittest |
| 7 | +from itertools import permutations |
| 8 | +import numpy as np |
| 9 | +from numpy.testing import assert_almost_equal |
| 10 | +from onnx import helper, TensorProto, numpy_helper |
| 11 | +from tf2onnx.optimizer.einsum_optimizer import ( |
| 12 | + OnnxMicroRuntime, |
| 13 | + predict_transposition_cost, |
| 14 | + compute_transposition_features) |
| 15 | +from tf2onnx import constants |
| 16 | +from backend_test_base import Tf2OnnxBackendTestBase |
| 17 | + |
| 18 | + |
| 19 | +class TestEinsumMl(Tf2OnnxBackendTestBase): |
| 20 | + "unit tests for einsum optimizer" |
| 21 | + |
| 22 | + def test_onnx_micro_runtime(self): |
| 23 | + "test OnnxMicroRuntime" |
| 24 | + opset = self.config.opset |
| 25 | + x = np.array([1, 2, 4, 5, 5, 4]).astype( |
| 26 | + np.float32).reshape((3, 2)) |
| 27 | + |
| 28 | + model_def = helper.make_model( |
| 29 | + opset_imports=[helper.make_operatorsetid('', opset)], |
| 30 | + ir_version=constants.OPSET_TO_IR_VERSION[opset], |
| 31 | + producer_name='tf2onnx', |
| 32 | + producer_version='0.0.1', |
| 33 | + graph=helper.make_graph( |
| 34 | + name='einsum', |
| 35 | + inputs=[helper.make_tensor_value_info('X', TensorProto.FLOAT, None)], |
| 36 | + outputs=[helper.make_tensor_value_info("Y", TensorProto.FLOAT, None)], |
| 37 | + nodes=[ |
| 38 | + helper.make_node('Add', ["X", "X"], ["temp"]), |
| 39 | + helper.make_node('Add', ["X", "temp"], ["Y"]), |
| 40 | + ])) |
| 41 | + |
| 42 | + rt = OnnxMicroRuntime(model_def) |
| 43 | + out = rt.run({'X': x}) |
| 44 | + self.assertIn('X', out) |
| 45 | + self.assertIn('Y', out) |
| 46 | + self.assertIn('temp', out) |
| 47 | + self.assertEqual(len(out), 3) |
| 48 | + |
| 49 | + def test_onnx_micro_runtime_exc1(self): |
| 50 | + "test OnnxMicroRuntime" |
| 51 | + with self.assertRaises(TypeError): |
| 52 | + OnnxMicroRuntime(None) |
| 53 | + |
| 54 | + def test_onnx_micro_runtime_exc2(self): |
| 55 | + "test OnnxMicroRuntime" |
| 56 | + opset = self.config.opset |
| 57 | + x = np.array([1, 2, 4, 5, 5, 4]).astype( |
| 58 | + np.float32).reshape((3, 2)) |
| 59 | + |
| 60 | + model_def = helper.make_model( |
| 61 | + opset_imports=[helper.make_operatorsetid('', opset)], |
| 62 | + ir_version=constants.OPSET_TO_IR_VERSION[opset], |
| 63 | + producer_name='tf2onnx', |
| 64 | + producer_version='0.0.1', |
| 65 | + graph=helper.make_graph( |
| 66 | + name='einsum', |
| 67 | + inputs=[helper.make_tensor_value_info('X', TensorProto.FLOAT, None)], |
| 68 | + outputs=[helper.make_tensor_value_info("Y", TensorProto.FLOAT, None)], |
| 69 | + initializer=[ |
| 70 | + numpy_helper.from_array(np.array([1], dtype=np.float32), name="C1"), |
| 71 | + numpy_helper.from_array(np.array([2], dtype=np.float32), name="C2"), |
| 72 | + ], |
| 73 | + nodes=[ |
| 74 | + helper.make_node('Add', ["X", "C1"], ["temp"]), |
| 75 | + helper.make_node('Pow', ["temp", "C2"], ["Y"]), |
| 76 | + ])) |
| 77 | + |
| 78 | + rt = OnnxMicroRuntime(model_def) |
| 79 | + with self.assertRaises(NotImplementedError): |
| 80 | + rt.run({'X': x}) |
| 81 | + with self.assertRaises(TypeError): |
| 82 | + rt.run(x) |
| 83 | + |
| 84 | + def test_onnx_micro_runtime_shape(self): |
| 85 | + "test OnnxMicroRuntime" |
| 86 | + opset = self.config.opset |
| 87 | + x = np.array([1, 2, 4, 5, 5, 4]).astype( |
| 88 | + np.float32).reshape((3, 2)) |
| 89 | + |
| 90 | + model_def = helper.make_model( |
| 91 | + opset_imports=[helper.make_operatorsetid('', opset)], |
| 92 | + ir_version=constants.OPSET_TO_IR_VERSION[opset], |
| 93 | + producer_name='tf2onnx', |
| 94 | + producer_version='0.0.1', |
| 95 | + graph=helper.make_graph( |
| 96 | + name='einsum', |
| 97 | + inputs=[helper.make_tensor_value_info('X', TensorProto.FLOAT, None)], |
| 98 | + outputs=[helper.make_tensor_value_info("Y", TensorProto.INT64, None)], |
| 99 | + nodes=[ |
| 100 | + helper.make_node('Shape', ["X"], ["Y"]), |
| 101 | + ])) |
| 102 | + |
| 103 | + rt = OnnxMicroRuntime(model_def) |
| 104 | + out = rt.run({'X': x}) |
| 105 | + assert_almost_equal(np.array(x.shape, dtype=np.int64), out['Y']) |
| 106 | + |
| 107 | + def test_onnx_micro_runtime_unsqueeze(self): |
| 108 | + "test OnnxMicroRuntime" |
| 109 | + opset = self.config.opset |
| 110 | + x = np.array([1, 2, 4, 5, 5, 4]).astype( |
| 111 | + np.float32).reshape((3, 2)) |
| 112 | + i = np.array([1]).astype(np.int64) |
| 113 | + |
| 114 | + model_def = helper.make_model( |
| 115 | + opset_imports=[helper.make_operatorsetid('', opset)], |
| 116 | + ir_version=constants.OPSET_TO_IR_VERSION[opset], |
| 117 | + producer_name='tf2onnx', |
| 118 | + producer_version='0.0.1', |
| 119 | + graph=helper.make_graph( |
| 120 | + name='einsum', |
| 121 | + inputs=[helper.make_tensor_value_info('X', TensorProto.FLOAT, None), |
| 122 | + helper.make_tensor_value_info('I', TensorProto.INT64, None)], |
| 123 | + outputs=[helper.make_tensor_value_info("Y", TensorProto.INT64, None)], |
| 124 | + nodes=[ |
| 125 | + helper.make_node('Unsqueeze', ["X", "I"], ["Y"]), |
| 126 | + ])) |
| 127 | + |
| 128 | + rt = OnnxMicroRuntime(model_def) |
| 129 | + out = rt.run({'X': x, 'I': i}) |
| 130 | + assert_almost_equal(np.array(x.reshape((3, 1, 2))), out['Y']) |
| 131 | + |
| 132 | + def test_onnx_micro_runtime_transpose(self): |
| 133 | + "test OnnxMicroRuntime" |
| 134 | + opset = self.config.opset |
| 135 | + x = np.array([1, 2, 4, 5, 5, 4]).astype( |
| 136 | + np.float32).reshape((3, 2)) |
| 137 | + |
| 138 | + model_def = helper.make_model( |
| 139 | + opset_imports=[helper.make_operatorsetid('', opset)], |
| 140 | + ir_version=constants.OPSET_TO_IR_VERSION[opset], |
| 141 | + producer_name='tf2onnx', |
| 142 | + producer_version='0.0.1', |
| 143 | + graph=helper.make_graph( |
| 144 | + name='einsum', |
| 145 | + inputs=[helper.make_tensor_value_info('X', TensorProto.FLOAT, None)], |
| 146 | + outputs=[helper.make_tensor_value_info("Y", TensorProto.FLOAT, None)], |
| 147 | + nodes=[ |
| 148 | + helper.make_node('Transpose', ["X"], ["Y"], perm=[1, 0]), |
| 149 | + ])) |
| 150 | + |
| 151 | + rt = OnnxMicroRuntime(model_def) |
| 152 | + out = rt.run({'X': x}) |
| 153 | + assert_almost_equal(x.T, out['Y']) |
| 154 | + |
| 155 | + def test_onnx_micro_runtime_matmul(self): |
| 156 | + "test OnnxMicroRuntime" |
| 157 | + opset = self.config.opset |
| 158 | + x = np.array([1, 2, 4, 5]).astype( |
| 159 | + np.float32).reshape((2, 2)) |
| 160 | + |
| 161 | + model_def = helper.make_model( |
| 162 | + opset_imports=[helper.make_operatorsetid('', opset)], |
| 163 | + ir_version=constants.OPSET_TO_IR_VERSION[opset], |
| 164 | + producer_name='tf2onnx', |
| 165 | + producer_version='0.0.1', |
| 166 | + graph=helper.make_graph( |
| 167 | + name='einsum', |
| 168 | + inputs=[helper.make_tensor_value_info('X', TensorProto.FLOAT, None)], |
| 169 | + outputs=[helper.make_tensor_value_info("Y", TensorProto.INT64, None)], |
| 170 | + nodes=[ |
| 171 | + helper.make_node('MatMul', ["X", "X"], ["Y"]), |
| 172 | + ])) |
| 173 | + |
| 174 | + rt = OnnxMicroRuntime(model_def) |
| 175 | + out = rt.run({'X': x}) |
| 176 | + assert_almost_equal(np.matmul(x, x), out['Y']) |
| 177 | + |
| 178 | + def test_features(self): |
| 179 | + res = compute_transposition_features((3, 5, 7), (0, 1, 2)) |
| 180 | + self.assertIsInstance(res, dict) |
| 181 | + self.assertEqual(res["edit"], 0) |
| 182 | + self.assertEqual(res["rot"], -1) |
| 183 | + res = compute_transposition_features((3, 5, 7), (2, 1, 0)) |
| 184 | + self.assertEqual(res["edit"], 2) |
| 185 | + self.assertEqual(res["rot"], 0) |
| 186 | + self.assertEqual(res["rev"], 1) |
| 187 | + |
| 188 | + def test_cost(self): |
| 189 | + res = predict_transposition_cost((300, 500, 700), (0, 1, 2)) |
| 190 | + self.assertIsInstance(res, float) |
| 191 | + self.assertGreater(res, 0) |
| 192 | + for shape in [(3, 5, 7), (30, 50, 70)]: |
| 193 | + for perm in permutations([0, 1, 2]): |
| 194 | + p = tuple(perm) |
| 195 | + cost = predict_transposition_cost(shape, p) |
| 196 | + if p[-1] == 2: |
| 197 | + self.assertEqual(cost, 0) |
| 198 | + |
| 199 | + |
| 200 | +if __name__ == "__main__": |
| 201 | + unittest.main() |
0 commit comments