|
8 | 8 | requires_transformers, |
9 | 9 | has_torch, |
10 | 10 | ) |
| 11 | +from onnx_diagnostic.helpers.cache_helper import CacheKeyValue, make_dynamic_cache |
11 | 12 | from onnx_diagnostic.helpers.torch_helper import torch_deepcopy |
12 | 13 | from onnx_diagnostic.torch_models.hghub import get_untrained_model_with_inputs |
13 | 14 | from onnx_diagnostic.torch_export_patches import torch_export_patches |
@@ -345,16 +346,92 @@ def forward(self, x, ind1, ind2): |
345 | 346 |
|
346 | 347 | @requires_torch("2.7.9999") |
347 | 348 | @requires_transformers("4.49.9999") |
348 | | - def test_export_tiny_llm_dim_meta(self): |
| 349 | + def test_export_with_patch_tiny_llm_dim_meta(self): |
349 | 350 | data = get_untrained_model_with_inputs("arnir0/Tiny-LLM", verbose=0) |
350 | 351 | model, inputs, ds = data["model"], data["inputs"], data["dynamic_shapes"] |
| 352 | + order = ["input_ids", "attention_mask", "position_ids", "past_key_values"] |
| 353 | + self.assertEqual(list(inputs), order) |
351 | 354 | expected = model(**torch_deepcopy(inputs)) |
352 | | - with torch_export_patches(patch_transformers=True): |
353 | | - ep = torch.export.export( |
354 | | - model, (), kwargs=inputs, dynamic_shapes=use_dyn_not_str(ds) |
355 | | - ) |
356 | | - got = ep.module()(**inputs) |
357 | | - self.assertEqualArrayAny(expected, got) |
| 355 | + with self.subTest(input="no01", backed_size_oblivious=False): |
| 356 | + with torch_export_patches(patch_transformers=True): |
| 357 | + ep = torch.export.export( |
| 358 | + model, (), kwargs=inputs, dynamic_shapes=use_dyn_not_str(ds) |
| 359 | + ) |
| 360 | + got = ep.module()(**torch_deepcopy(inputs)) |
| 361 | + self.assertEqualArrayAny(expected, got) |
| 362 | + |
| 363 | + with self.subTest(input="no01", backed_size_oblivious=True): |
| 364 | + with ( |
| 365 | + torch.fx.experimental._config.patch(backed_size_oblivious=True), |
| 366 | + torch_export_patches(patch_transformers=True), |
| 367 | + ): |
| 368 | + ep = torch.export.export( |
| 369 | + model, (), kwargs=inputs, dynamic_shapes=use_dyn_not_str(ds) |
| 370 | + ) |
| 371 | + got = ep.module()(**torch_deepcopy(inputs)) |
| 372 | + self.assertEqualArrayAny(expected, got) |
| 373 | + |
| 374 | + def _batch1(t): |
| 375 | + if t.__class__.__name__ == "DynamicCache": |
| 376 | + kv = CacheKeyValue(t) |
| 377 | + keys = [t[:1] for t in kv.key_cache] |
| 378 | + values = [t[:1] for t in kv.value_cache] |
| 379 | + return make_dynamic_cache(tuple(zip(keys, values))) |
| 380 | + if t.ndim > 1: |
| 381 | + return t[:1] |
| 382 | + return t |
| 383 | + |
| 384 | + export_inputs = {k: _batch1(v) for k, v in inputs.items()} |
| 385 | + |
| 386 | + # with self.subTest(input="batch1", backed_size_oblivious=False): |
| 387 | + # with torch_export_patches(patch_transformers=True): |
| 388 | + # ep = torch.export.export( |
| 389 | + # model, (), kwargs=export_inputs, dynamic_shapes=use_dyn_not_str(ds) |
| 390 | + # ) |
| 391 | + # got = ep.module()(**torch_deepcopy(inputs)) |
| 392 | + # self.assertEqualArrayAny(expected, got) |
| 393 | + |
| 394 | + with self.subTest(input="batch1", backed_size_oblivious=True): |
| 395 | + with ( |
| 396 | + torch.fx.experimental._config.patch(backed_size_oblivious=True), |
| 397 | + torch_export_patches(patch_transformers=True), |
| 398 | + ): |
| 399 | + ep = torch.export.export( |
| 400 | + model, (), kwargs=export_inputs, dynamic_shapes=use_dyn_not_str(ds) |
| 401 | + ) |
| 402 | + try: |
| 403 | + got = ep.module()(**torch_deepcopy(inputs)) |
| 404 | + except AssertionError as e: |
| 405 | + got = None |
| 406 | + if "Guard failed: position_ids.size()[0] == 1" not in str(e): |
| 407 | + raise |
| 408 | + |
| 409 | + if got is not None: |
| 410 | + self.assertEqualArrayAny(expected, got) |
| 411 | + |
| 412 | + if "inputs_empty_cache" not in data: |
| 413 | + return |
| 414 | + |
| 415 | + export_inputs = data["inputs_empty_cache"] |
| 416 | + |
| 417 | + # with self.subTest(input="cache0", backed_size_oblivious=False): |
| 418 | + # with torch_export_patches(patch_transformers=True): |
| 419 | + # ep = torch.export.export( |
| 420 | + # model, (), kwargs=export_inputs, dynamic_shapes=use_dyn_not_str(ds) |
| 421 | + # ) |
| 422 | + # got = ep.module()(**torch_deepcopy(inputs)) |
| 423 | + # self.assertEqualArrayAny(expected, got) |
| 424 | + |
| 425 | + with self.subTest(input="cache0", backed_size_oblivious=True): |
| 426 | + with ( |
| 427 | + torch.fx.experimental._config.patch(backed_size_oblivious=True), |
| 428 | + torch_export_patches(patch_transformers=True), |
| 429 | + ): |
| 430 | + ep = torch.export.export( |
| 431 | + model, (), kwargs=export_inputs, dynamic_shapes=use_dyn_not_str(ds) |
| 432 | + ) |
| 433 | + got = ep.module()(**torch_deepcopy(inputs)) |
| 434 | + self.assertEqualArrayAny(expected, got) |
358 | 435 |
|
359 | 436 |
|
360 | 437 | if __name__ == "__main__": |
|
0 commit comments