|
| 1 | +import copy |
| 2 | +import os |
| 3 | +import unittest |
| 4 | +from typing import Any, Dict, List, Tuple |
| 5 | +import torch |
| 6 | +from onnx_diagnostic.ext_test_case import ( |
| 7 | + ExtTestCase, |
| 8 | + ignore_warnings, |
| 9 | + hide_stdout, |
| 10 | + requires_torch, |
| 11 | + has_transformers, |
| 12 | +) |
| 13 | +from onnx_diagnostic.helpers import string_type |
| 14 | +from onnx_diagnostic.cache_helpers import make_dynamic_cache |
| 15 | +from onnx_diagnostic.torch_export_patches.onnx_export_errors import ( |
| 16 | + bypass_export_some_errors, |
| 17 | +) |
| 18 | +from onnx_diagnostic.torch_models.hghub.model_inputs import get_untrained_model_with_inputs |
| 19 | + |
| 20 | + |
| 21 | +class TestOnnxExportErrors(ExtTestCase): |
| 22 | + @ignore_warnings(UserWarning) |
| 23 | + @hide_stdout() |
| 24 | + def test_export_dynamic_cache_update(self): |
| 25 | + values = [True, False] if has_transformers("4.50") else [False] |
| 26 | + for strict in self.subloop(values, verbose=1): |
| 27 | + |
| 28 | + class SubModelCache(torch.nn.Module): |
| 29 | + def forward(self, cache): |
| 30 | + d = cache.__class__() |
| 31 | + d.update(cache.key_cache[0] + 1, cache.value_cache[0] + 2, 0) |
| 32 | + d.update(cache.key_cache[0] + 3, cache.value_cache[0] + 5, 1) |
| 33 | + return d |
| 34 | + |
| 35 | + class SubModel(torch.nn.Module): |
| 36 | + def forward(self, x, cache): |
| 37 | + return x + cache.key_cache[0] + cache.value_cache[0] |
| 38 | + |
| 39 | + class Model(torch.nn.Module): |
| 40 | + def __init__(self): |
| 41 | + super().__init__() |
| 42 | + self.sub = SubModel() |
| 43 | + self.subcache = SubModelCache() |
| 44 | + |
| 45 | + def forward(self, x, cache): |
| 46 | + return self.sub(x, self.subcache(cache)) |
| 47 | + |
| 48 | + # no patch |
| 49 | + cache = make_dynamic_cache( |
| 50 | + [(torch.ones((5, 6, 5, 6)), torch.ones((5, 6, 5, 6)) + 2)] |
| 51 | + ) |
| 52 | + model = Model() |
| 53 | + inputs = (torch.randn((5, 6, 5, 6)), cache) |
| 54 | + expected = model(*inputs) |
| 55 | + |
| 56 | + DYN = torch.export.Dim.DYNAMIC |
| 57 | + |
| 58 | + # patching |
| 59 | + with bypass_export_some_errors(patch_transformers=True): |
| 60 | + got = model(*inputs) |
| 61 | + self.assertEqualArray(expected, got) |
| 62 | + ep = torch.export.export( |
| 63 | + model, |
| 64 | + inputs, |
| 65 | + dynamic_shapes=( |
| 66 | + {0: DYN, 2: DYN}, |
| 67 | + [[{0: DYN, 2: DYN}], [{0: DYN, 2: DYN}]], |
| 68 | + ), |
| 69 | + strict=strict, |
| 70 | + ) |
| 71 | + mod = ep.module() |
| 72 | + got = mod(*inputs) |
| 73 | + self.assertEqualArray(expected, got) |
| 74 | + |
| 75 | + class MyInterpreter(torch.fx.Interpreter): |
| 76 | + def call_function(self, target, args, kwargs): |
| 77 | + res = super().call_function(target, args, kwargs) |
| 78 | + return res |
| 79 | + |
| 80 | + args, _spec = torch.utils._pytree.tree_flatten(inputs) |
| 81 | + got = MyInterpreter(ep.module()).run(*args) |
| 82 | + self.assertEqualAny(expected, got) |
| 83 | + |
| 84 | + @ignore_warnings(UserWarning) |
| 85 | + @requires_torch( |
| 86 | + "2.7", |
| 87 | + "MyCache78'> does not have a flatten_fn_spec, " |
| 88 | + "use torch.fx._pytree.register_pytree_flatten_spec", |
| 89 | + ) |
| 90 | + def test_export_mycache_list_cat(self): |
| 91 | + TreeContext = torch.utils._pytree.Context |
| 92 | + MappingKey = torch.utils._pytree.MappingKey |
| 93 | + KeyEntry = torch.utils._pytree.KeyEntry |
| 94 | + |
| 95 | + class MyCache77: |
| 96 | + def __init__(self, key=None, value=None): |
| 97 | + self.key_cache = [key] if key is not None else [] |
| 98 | + self.value_cache = [value] if value is not None else [] |
| 99 | + |
| 100 | + class ModelMyCache(torch.nn.Module): |
| 101 | + def forward(self, x, dc): |
| 102 | + y = ( |
| 103 | + ( |
| 104 | + torch.cat(dc.key_cache, axis=1) + torch.cat(dc.value_cache, axis=1) |
| 105 | + ).reshape((-1, x.shape[1])) |
| 106 | + ).transpose(1, 0) |
| 107 | + return x @ y |
| 108 | + |
| 109 | + inputs = { |
| 110 | + "x": torch.randn(3, 8), |
| 111 | + "dc": MyCache77(torch.ones((3, 8, 3, 8)), torch.ones((3, 8, 3, 8))), |
| 112 | + } |
| 113 | + model = ModelMyCache() |
| 114 | + expected = model(**inputs) |
| 115 | + |
| 116 | + def flatten_my_cache77(cache: MyCache77) -> Tuple[List[Any], TreeContext]: |
| 117 | + flat = [ |
| 118 | + (k, getattr(cache, k)) |
| 119 | + for k in ["key_cache", "value_cache"] |
| 120 | + if hasattr(cache, k) |
| 121 | + ] |
| 122 | + return [f[1] for f in flat], [f[0] for f in flat] |
| 123 | + |
| 124 | + def flatten_with_keys_my_cache77( |
| 125 | + d: Dict[Any, Any], |
| 126 | + ) -> Tuple[List[Tuple[KeyEntry, Any]], TreeContext]: |
| 127 | + values, context = flatten_my_cache77(d) |
| 128 | + return [(MappingKey(k), v) for k, v in zip(context, values)], context |
| 129 | + |
| 130 | + def unflatten_my_cache_77( |
| 131 | + values: List[Any], context: TreeContext, output_type=None |
| 132 | + ) -> MyCache77: |
| 133 | + cache = MyCache77() |
| 134 | + values = dict(zip(context, values)) |
| 135 | + for k, v in values.items(): |
| 136 | + setattr(cache, k, v) |
| 137 | + return cache |
| 138 | + |
| 139 | + torch.utils._pytree.register_pytree_node( |
| 140 | + MyCache77, |
| 141 | + flatten_my_cache77, |
| 142 | + unflatten_my_cache_77, |
| 143 | + serialized_type_name="MyCache77", |
| 144 | + flatten_with_keys_fn=flatten_with_keys_my_cache77, |
| 145 | + ) |
| 146 | + |
| 147 | + # DYN = torch.export.Dim.DYNAMIC |
| 148 | + ep = torch.export.export(model, (), kwargs=inputs) |
| 149 | + |
| 150 | + args, _spec = torch.utils._pytree.tree_flatten(inputs) |
| 151 | + got = torch.fx.Interpreter(ep.module()).run(*args) |
| 152 | + self.assertEqualAny(expected, got) |
| 153 | + |
| 154 | + mod = ep.module() |
| 155 | + got = mod(**inputs) |
| 156 | + self.assertEqualArray(expected, got) |
| 157 | + |
| 158 | + @ignore_warnings(UserWarning) |
| 159 | + @requires_torch( |
| 160 | + "2.7", |
| 161 | + "MyCache78'> does not have a flatten_fn_spec, " |
| 162 | + "use torch.fx._pytree.register_pytree_flatten_spec", |
| 163 | + ) |
| 164 | + def test_export_mycache_dict_cat(self): |
| 165 | + TreeContext = torch.utils._pytree.Context |
| 166 | + |
| 167 | + class MyCache78: |
| 168 | + def __init__(self, key=None, value=None): |
| 169 | + self.key_cache = [key] if key is not None else [] |
| 170 | + self.value_cache = [value] if value is not None else [] |
| 171 | + |
| 172 | + class ModelMyCache(torch.nn.Module): |
| 173 | + def forward(self, x, dc): |
| 174 | + y = ( |
| 175 | + ( |
| 176 | + torch.cat(dc.key_cache, axis=1) + torch.cat(dc.value_cache, axis=1) |
| 177 | + ).reshape((-1, x.shape[1])) |
| 178 | + ).transpose(1, 0) |
| 179 | + return x @ y |
| 180 | + |
| 181 | + inputs = { |
| 182 | + "x": torch.randn(3, 8), |
| 183 | + "dc": MyCache78(torch.ones((3, 8, 3, 8)), torch.ones((3, 8, 3, 8))), |
| 184 | + } |
| 185 | + model = ModelMyCache() |
| 186 | + expected = model(**inputs) |
| 187 | + |
| 188 | + def flatten_my_cache78(cache: MyCache78): |
| 189 | + dictionary = { |
| 190 | + "key_cache": cache.key_cache, |
| 191 | + "value_cache": cache.value_cache, |
| 192 | + } |
| 193 | + return torch.utils._pytree._dict_flatten(dictionary) |
| 194 | + |
| 195 | + def flatten_with_keys_my_cache78(cache: MyCache78): |
| 196 | + dictionary = { |
| 197 | + "key_cache": cache.key_cache, |
| 198 | + "value_cache": cache.value_cache, |
| 199 | + } |
| 200 | + return torch.utils._pytree._dict_flatten_with_keys(dictionary) |
| 201 | + |
| 202 | + def unflatten_my_cache_78(values, context: TreeContext, output_type=None) -> MyCache78: |
| 203 | + dictionary = torch.utils._pytree._dict_unflatten(values, context) |
| 204 | + cache = MyCache78() |
| 205 | + for k, v in dictionary.items(): |
| 206 | + setattr(cache, k, v) |
| 207 | + return cache |
| 208 | + |
| 209 | + torch.utils._pytree.register_pytree_node( |
| 210 | + MyCache78, |
| 211 | + flatten_my_cache78, |
| 212 | + unflatten_my_cache_78, |
| 213 | + serialized_type_name="MyCache78", |
| 214 | + flatten_with_keys_fn=flatten_with_keys_my_cache78, |
| 215 | + ) |
| 216 | + |
| 217 | + # DYN = torch.export.Dim.DYNAMIC |
| 218 | + ep = torch.export.export(model, (), kwargs=inputs) |
| 219 | + |
| 220 | + args, _spec = torch.utils._pytree.tree_flatten(inputs) |
| 221 | + got = torch.fx.Interpreter(ep.module()).run(*args) |
| 222 | + self.assertEqualAny(expected, got) |
| 223 | + |
| 224 | + mod = ep.module() |
| 225 | + got = mod(**inputs) |
| 226 | + self.assertEqualArray(expected, got) |
| 227 | + |
| 228 | + @ignore_warnings(UserWarning) |
| 229 | + def test_export_dynamic_cache_cat(self): |
| 230 | + |
| 231 | + class ModelDynamicCache(torch.nn.Module): |
| 232 | + def forward(self, x, dc): |
| 233 | + y = ( |
| 234 | + ( |
| 235 | + torch.cat(dc.key_cache, axis=1) + torch.cat(dc.value_cache, axis=1) |
| 236 | + ).reshape((-1, x.shape[1])) |
| 237 | + ).transpose(1, 0) |
| 238 | + return x @ y |
| 239 | + |
| 240 | + inputs = { |
| 241 | + "x": torch.randn(3, 8), |
| 242 | + "dc": make_dynamic_cache( |
| 243 | + [(torch.ones((3, 8, 3, 8)), (torch.ones((3, 8, 3, 8)) * 2))] |
| 244 | + ), |
| 245 | + } |
| 246 | + model = ModelDynamicCache() |
| 247 | + expected = model(**inputs) |
| 248 | + |
| 249 | + # DYN = torch.export.Dim.DYNAMIC |
| 250 | + NOBYPASS = int(os.environ.get("NOBYBASS", "0")) |
| 251 | + if NOBYPASS: |
| 252 | + ep = torch.export.export(model, (), kwargs=inputs) |
| 253 | + |
| 254 | + args, _spec = torch.utils._pytree.tree_flatten(inputs) |
| 255 | + got = torch.fx.Interpreter(ep.module()).run(*args) |
| 256 | + self.assertEqualAny(expected, got) |
| 257 | + |
| 258 | + mod = ep.module() |
| 259 | + got = mod(**inputs) |
| 260 | + self.assertEqualArray(expected, got) |
| 261 | + return |
| 262 | + |
| 263 | + with bypass_export_some_errors(patch_transformers=True): |
| 264 | + ep = torch.export.export(model, (), kwargs=inputs) |
| 265 | + |
| 266 | + args, _spec = torch.utils._pytree.tree_flatten(inputs) |
| 267 | + got = torch.fx.Interpreter(ep.module()).run(*args) |
| 268 | + self.assertEqualAny(expected, got) |
| 269 | + |
| 270 | + mod = ep.module() |
| 271 | + got = mod(**inputs) |
| 272 | + self.assertEqualArray(expected, got) |
| 273 | + |
| 274 | + @ignore_warnings(UserWarning) |
| 275 | + @requires_torch("2.9") |
| 276 | + def test_phi2_export_module(self): |
| 277 | + data = get_untrained_model_with_inputs("microsoft/phi-2") |
| 278 | + model, inputs, dyn_shapes = data["model"], data["inputs"], data["dynamic_shapes"] |
| 279 | + str_inputs = string_type(inputs, with_shape=True, with_min_max=True) |
| 280 | + inputs_copied = copy.deepcopy(inputs) |
| 281 | + expected = model(**inputs_copied) |
| 282 | + self.maxDiff = None |
| 283 | + self.assertEqual(str_inputs, string_type(inputs, with_shape=True, with_min_max=True)) |
| 284 | + |
| 285 | + # The cache is modified inplace, that's why, we copied it. |
| 286 | + self.assertNotEqual( |
| 287 | + string_type(inputs, with_shape=True, with_min_max=True), |
| 288 | + string_type(inputs_copied, with_shape=True, with_min_max=True), |
| 289 | + ) |
| 290 | + inputs_copied = copy.deepcopy(inputs) |
| 291 | + self.assertEqual( |
| 292 | + str_inputs, string_type(inputs_copied, with_shape=True, with_min_max=True) |
| 293 | + ) |
| 294 | + |
| 295 | + with bypass_export_some_errors(patch_transformers=True): |
| 296 | + ep = torch.export.export( |
| 297 | + model, |
| 298 | + (), |
| 299 | + kwargs=inputs, |
| 300 | + dynamic_shapes=dyn_shapes, |
| 301 | + strict=False, # True works but then the it fails during the execution |
| 302 | + ) |
| 303 | + # ep = ep.run_decompositions() |
| 304 | + mod = ep.module() |
| 305 | + inputs_copied = copy.deepcopy(inputs) |
| 306 | + self.assertEqual( |
| 307 | + str_inputs, string_type(inputs_copied, with_shape=True, with_min_max=True) |
| 308 | + ) |
| 309 | + got = mod(**inputs_copied) |
| 310 | + self.assertEqualAny(expected, got) |
| 311 | + |
| 312 | + @ignore_warnings(UserWarning) |
| 313 | + @requires_torch("2.9") |
| 314 | + def test_phi2_export_interpreter(self): |
| 315 | + data = get_untrained_model_with_inputs("microsoft/phi-2") |
| 316 | + model, inputs, dyn_shapes = data["model"], data["inputs"], data["dynamic_shapes"] |
| 317 | + str_inputs = string_type(inputs, with_shape=True, with_min_max=True) |
| 318 | + inputs_copied = copy.deepcopy(inputs) |
| 319 | + expected = model(**inputs_copied) |
| 320 | + self.maxDiff = None |
| 321 | + self.assertEqual(str_inputs, string_type(inputs, with_shape=True, with_min_max=True)) |
| 322 | + |
| 323 | + # The cache is modified inplace, that's why, we copied it. |
| 324 | + self.assertNotEqual( |
| 325 | + string_type(inputs, with_shape=True, with_min_max=True), |
| 326 | + string_type(inputs_copied, with_shape=True, with_min_max=True), |
| 327 | + ) |
| 328 | + inputs_copied = copy.deepcopy(inputs) |
| 329 | + self.assertEqual( |
| 330 | + str_inputs, string_type(inputs_copied, with_shape=True, with_min_max=True) |
| 331 | + ) |
| 332 | + |
| 333 | + with bypass_export_some_errors(patch_transformers=True): |
| 334 | + ep = torch.export.export( |
| 335 | + model, |
| 336 | + (), |
| 337 | + kwargs=inputs, |
| 338 | + dynamic_shapes=dyn_shapes, |
| 339 | + strict=False, # True works but then the it fails during the execution |
| 340 | + ) |
| 341 | + # ep = ep.run_decompositions() |
| 342 | + |
| 343 | + # from experimental_experiment.torch_interpreter.tracing import CustomTracer |
| 344 | + # CustomTracer.remove_unnecessary_slices(ep.graph) |
| 345 | + memorize = [] |
| 346 | + |
| 347 | + class MyInterpreter(torch.fx.Interpreter): |
| 348 | + def call_function(self, target, args, kwargs): |
| 349 | + res = super().call_function(target, args, kwargs) |
| 350 | + memorize.append((target, args, kwargs, res)) |
| 351 | + return res |
| 352 | + |
| 353 | + inputs_copied = copy.deepcopy(inputs) |
| 354 | + self.assertEqual( |
| 355 | + str_inputs, string_type(inputs_copied, with_shape=True, with_min_max=True) |
| 356 | + ) |
| 357 | + args, _spec = torch.utils._pytree.tree_flatten(inputs_copied) |
| 358 | + got = MyInterpreter(ep.module()).run(*args) |
| 359 | + self.assertEqualAny(expected, got) |
| 360 | + |
| 361 | + |
| 362 | +if __name__ == "__main__": |
| 363 | + unittest.main(verbosity=2) |
0 commit comments