|
| 1 | +import copy |
1 | 2 | import unittest |
2 | 3 | import torch |
3 | 4 | from onnx_diagnostic.ext_test_case import ExtTestCase, ignore_warnings, hide_stdout |
| 5 | +from onnx_diagnostic.helpers import string_type |
4 | 6 | from onnx_diagnostic.cache_helpers import make_dynamic_cache |
5 | 7 | from onnx_diagnostic.torch_export_patches.onnx_export_errors import ( |
6 | 8 | bypass_export_some_errors, |
7 | 9 | ) |
| 10 | +from onnx_diagnostic.torch_models.hghub.model_inputs import get_untrained_model_with_inputs |
8 | 11 |
|
9 | 12 |
|
10 | 13 | class TestOnnxExportErrors(ExtTestCase): |
@@ -69,6 +72,97 @@ def forward(self, x, cache): |
69 | 72 | got = mod(*inputs) |
70 | 73 | self.assertEqualArray(expected, got) |
71 | 74 |
|
| 75 | + @ignore_warnings(UserWarning) |
| 76 | + def test_phi2_export_module(self): |
| 77 | + data = get_untrained_model_with_inputs("microsoft/phi-2") |
| 78 | + model, inputs, dyn_shapes = data["model"], data["inputs"], data["dynamic_shapes"] |
| 79 | + str_inputs = string_type(inputs, with_shape=True, with_min_max=True) |
| 80 | + inputs_copied = copy.deepcopy(inputs) |
| 81 | + expected = model(**inputs_copied) |
| 82 | + self.maxDiff = None |
| 83 | + self.assertEqual(str_inputs, string_type(inputs, with_shape=True, with_min_max=True)) |
| 84 | + |
| 85 | + # The cache is modified inplace, that's why, we copied it. |
| 86 | + self.assertNotEqual( |
| 87 | + string_type(inputs, with_shape=True, with_min_max=True), |
| 88 | + string_type(inputs_copied, with_shape=True, with_min_max=True), |
| 89 | + ) |
| 90 | + inputs_copied = copy.deepcopy(inputs) |
| 91 | + self.assertEqual( |
| 92 | + str_inputs, string_type(inputs_copied, with_shape=True, with_min_max=True) |
| 93 | + ) |
| 94 | + |
| 95 | + with bypass_export_some_errors(patch_transformers=True): |
| 96 | + ep = torch.export.export( |
| 97 | + model, |
| 98 | + (), |
| 99 | + kwargs=inputs, |
| 100 | + dynamic_shapes=dyn_shapes, |
| 101 | + strict=False, # True works but then the it fails during the execution |
| 102 | + ) |
| 103 | + mod = ep.module() |
| 104 | + inputs_copied = copy.deepcopy(inputs) |
| 105 | + self.assertEqual( |
| 106 | + str_inputs, string_type(inputs_copied, with_shape=True, with_min_max=True) |
| 107 | + ) |
| 108 | + got = mod(**inputs_copied) |
| 109 | + self.assertEqualAny(expected, got) |
| 110 | + |
| 111 | + inputs_copied = copy.deepcopy(inputs) |
| 112 | + self.assertEqual( |
| 113 | + str_inputs, string_type(inputs_copied, with_shape=True, with_min_max=True) |
| 114 | + ) |
| 115 | + mod = ep.module() |
| 116 | + got = mod(**inputs_copied) |
| 117 | + self.assertEqualAny(expected, got) |
| 118 | + |
| 119 | + @ignore_warnings(UserWarning) |
| 120 | + def test_phi2_export_interpreter(self): |
| 121 | + data = get_untrained_model_with_inputs("microsoft/phi-2") |
| 122 | + model, inputs, dyn_shapes = data["model"], data["inputs"], data["dynamic_shapes"] |
| 123 | + str_inputs = string_type(inputs, with_shape=True, with_min_max=True) |
| 124 | + inputs_copied = copy.deepcopy(inputs) |
| 125 | + expected = model(**inputs_copied) |
| 126 | + self.maxDiff = None |
| 127 | + self.assertEqual(str_inputs, string_type(inputs, with_shape=True, with_min_max=True)) |
| 128 | + |
| 129 | + # The cache is modified inplace, that's why, we copied it. |
| 130 | + self.assertNotEqual( |
| 131 | + string_type(inputs, with_shape=True, with_min_max=True), |
| 132 | + string_type(inputs_copied, with_shape=True, with_min_max=True), |
| 133 | + ) |
| 134 | + inputs_copied = copy.deepcopy(inputs) |
| 135 | + self.assertEqual( |
| 136 | + str_inputs, string_type(inputs_copied, with_shape=True, with_min_max=True) |
| 137 | + ) |
| 138 | + |
| 139 | + with bypass_export_some_errors(patch_transformers=True): |
| 140 | + ep = torch.export.export( |
| 141 | + model, |
| 142 | + (), |
| 143 | + kwargs=inputs, |
| 144 | + dynamic_shapes=dyn_shapes, |
| 145 | + strict=False, # True works but then the it fails during the execution |
| 146 | + ) |
| 147 | + |
| 148 | + # from experimental_experiment.torch_interpreter.tracing import CustomTracer |
| 149 | + # CustomTracer.remove_unnecessary_slices(ep.graph) |
| 150 | + memorize = [] |
| 151 | + |
| 152 | + class MyInterpreter(torch.fx.Interpreter): |
| 153 | + def call_function(self, target, args, kwargs): |
| 154 | + res = super().call_function(target, args, kwargs) |
| 155 | + memorize.append((target, args, kwargs, res)) |
| 156 | + return res |
| 157 | + |
| 158 | + inputs_copied = copy.deepcopy(inputs) |
| 159 | + self.assertEqual( |
| 160 | + str_inputs, string_type(inputs_copied, with_shape=True, with_min_max=True) |
| 161 | + ) |
| 162 | + args, _spec = torch.utils._pytree.tree_flatten(inputs_copied) |
| 163 | + got = MyInterpreter(ep.module()).run(*args) |
| 164 | + self.assertEqualAny(expected, got) |
| 165 | + |
72 | 166 |
|
73 | 167 | if __name__ == "__main__": |
74 | 168 | unittest.main(verbosity=2) |
0 commit comments