|
| 1 | +import unittest |
| 2 | +import numpy as np |
| 3 | +import torch |
| 4 | +from onnx_diagnostic.ext_test_case import ExtTestCase |
| 5 | +from onnx_diagnostic.reference import ExtendedReferenceEvaluator |
| 6 | +from onnx_diagnostic.helpers.mini_onnx_builder import ( |
| 7 | + create_onnx_model_from_input_tensors, |
| 8 | + create_input_tensors_from_onnx_model, |
| 9 | + MiniOnnxBuilder, |
| 10 | +) |
| 11 | +from onnx_diagnostic.helpers.cache_helper import make_dynamic_cache |
| 12 | +from onnx_diagnostic.helpers import string_type |
| 13 | + |
| 14 | + |
| 15 | +class TestMiniOnnxBuilder(ExtTestCase): |
| 16 | + def test_mini_onnx_builder_sequence_onnx(self): |
| 17 | + builder = MiniOnnxBuilder() |
| 18 | + builder.append_output_sequence("name", [np.array([6, 7])]) |
| 19 | + onx = builder.to_onnx() |
| 20 | + ref = ExtendedReferenceEvaluator(onx) |
| 21 | + got = ref.run(None, {}) |
| 22 | + self.assertEqualAny([np.array([6, 7])], got[0]) |
| 23 | + |
| 24 | + def test_mini_onnx_builder_sequence_ort(self): |
| 25 | + from onnxruntime import InferenceSession |
| 26 | + |
| 27 | + builder = MiniOnnxBuilder() |
| 28 | + builder.append_output_sequence("name", [np.array([6, 7])]) |
| 29 | + onx = builder.to_onnx() |
| 30 | + ref = InferenceSession(onx.SerializeToString(), providers=["CPUExecutionProvider"]) |
| 31 | + got = ref.run(None, {}) |
| 32 | + self.assertEqualAny([np.array([6, 7])], got[0]) |
| 33 | + |
| 34 | + def test_mini_onnx_builder(self): |
| 35 | + data = [ |
| 36 | + ( |
| 37 | + np.array([1, 2], dtype=np.int64), |
| 38 | + torch.tensor([4, 5], dtype=torch.float32), |
| 39 | + { |
| 40 | + "tt1": np.array([-1, -2], dtype=np.int64), |
| 41 | + "tt2": torch.tensor([-4, -5], dtype=torch.float32), |
| 42 | + }, |
| 43 | + {}, |
| 44 | + ), |
| 45 | + { |
| 46 | + "t1": np.array([1, 2], dtype=np.int64), |
| 47 | + "t2": torch.tensor([4, 5], dtype=torch.float32), |
| 48 | + "d1": { |
| 49 | + "tt1": np.array([-1, -2], dtype=np.int64), |
| 50 | + "tt2": torch.tensor([-4, -5], dtype=torch.float32), |
| 51 | + }, |
| 52 | + "d2": {}, |
| 53 | + }, |
| 54 | + ( |
| 55 | + np.array([1, 2], dtype=np.int64), |
| 56 | + torch.tensor([4, 5], dtype=torch.float32), |
| 57 | + ( |
| 58 | + np.array([-1, -2], dtype=np.int64), |
| 59 | + torch.tensor([-4, -5], dtype=torch.float32), |
| 60 | + ), |
| 61 | + tuple(), |
| 62 | + ), |
| 63 | + { |
| 64 | + "t1": np.array([1, 2], dtype=np.int64), |
| 65 | + "t2": torch.tensor([4, 5], dtype=torch.float32), |
| 66 | + "l1": ( |
| 67 | + np.array([-1, -2], dtype=np.int64), |
| 68 | + torch.tensor([-4, -5], dtype=torch.float32), |
| 69 | + ), |
| 70 | + "l2": tuple(), |
| 71 | + }, |
| 72 | + # nested |
| 73 | + ( |
| 74 | + { |
| 75 | + "t1": np.array([1, 2], dtype=np.int64), |
| 76 | + "t2": torch.tensor([4, 5], dtype=torch.float32), |
| 77 | + "l1": ( |
| 78 | + np.array([-1, -2], dtype=np.int64), |
| 79 | + torch.tensor([-4, -5], dtype=torch.float32), |
| 80 | + ), |
| 81 | + "l2": tuple(), |
| 82 | + }, |
| 83 | + ( |
| 84 | + np.array([1, 2], dtype=np.int64), |
| 85 | + torch.tensor([4, 5], dtype=torch.float32), |
| 86 | + ( |
| 87 | + np.array([-1, -2], dtype=np.int64), |
| 88 | + torch.tensor([-4, -5], dtype=torch.float32), |
| 89 | + ), |
| 90 | + tuple(), |
| 91 | + ), |
| 92 | + ), |
| 93 | + # simple |
| 94 | + np.array([1, 2], dtype=np.int64), |
| 95 | + torch.tensor([4, 5], dtype=torch.float32), |
| 96 | + (np.array([1, 2], dtype=np.int64), torch.tensor([4, 5], dtype=torch.float32)), |
| 97 | + [np.array([1, 2], dtype=np.int64), torch.tensor([4, 5], dtype=torch.float32)], |
| 98 | + { |
| 99 | + "t1": np.array([1, 2], dtype=np.int64), |
| 100 | + "t2": torch.tensor([4, 5], dtype=torch.float32), |
| 101 | + }, |
| 102 | + ( |
| 103 | + np.array([1, 2], dtype=np.int64), |
| 104 | + torch.tensor([4, 5], dtype=torch.float32), |
| 105 | + [ |
| 106 | + np.array([-1, -2], dtype=np.int64), |
| 107 | + torch.tensor([-4, -5], dtype=torch.float32), |
| 108 | + ], |
| 109 | + [], |
| 110 | + ), |
| 111 | + { |
| 112 | + "t1": np.array([1, 2], dtype=np.int64), |
| 113 | + "t2": torch.tensor([4, 5], dtype=torch.float32), |
| 114 | + "l1": [ |
| 115 | + np.array([-1, -2], dtype=np.int64), |
| 116 | + torch.tensor([-4, -5], dtype=torch.float32), |
| 117 | + ], |
| 118 | + "l2": [], |
| 119 | + }, |
| 120 | + ] |
| 121 | + |
| 122 | + for inputs in data: |
| 123 | + with self.subTest(types=string_type(inputs)): |
| 124 | + model = create_onnx_model_from_input_tensors(inputs) |
| 125 | + restored = create_input_tensors_from_onnx_model(model) |
| 126 | + self.assertEqualAny(inputs, restored) |
| 127 | + |
| 128 | + def test_mini_onnx_builder_transformers(self): |
| 129 | + cache = make_dynamic_cache([(torch.ones((3, 3)), torch.ones((3, 3)) * 2)]) |
| 130 | + self.assertEqual(len(cache.key_cache), 1) |
| 131 | + self.assertEqual(len(cache.value_cache), 1) |
| 132 | + |
| 133 | + data = [(cache,), cache] |
| 134 | + |
| 135 | + for inputs in data: |
| 136 | + with self.subTest(types=string_type(inputs)): |
| 137 | + model = create_onnx_model_from_input_tensors(inputs) |
| 138 | + restored = create_input_tensors_from_onnx_model(model) |
| 139 | + self.assertEqualAny(inputs, restored) |
| 140 | + |
| 141 | + def test_mini_onnx_builder_transformers_sep(self): |
| 142 | + cache = make_dynamic_cache([(torch.ones((3, 3)), torch.ones((3, 3)) * 2)]) |
| 143 | + self.assertEqual(len(cache.key_cache), 1) |
| 144 | + self.assertEqual(len(cache.value_cache), 1) |
| 145 | + |
| 146 | + data = [(cache,), cache] |
| 147 | + |
| 148 | + for inputs in data: |
| 149 | + with self.subTest(types=string_type(inputs)): |
| 150 | + model = create_onnx_model_from_input_tensors(inputs, sep="#") |
| 151 | + restored = create_input_tensors_from_onnx_model(model, sep="#") |
| 152 | + self.assertEqualAny(inputs, restored) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + unittest.main(verbosity=2) |
0 commit comments