|
| 1 | +import copy |
| 2 | +import unittest |
| 3 | +import torch |
| 4 | +from transformers.cache_utils import DynamicCache |
| 5 | +from onnx_diagnostic.ext_test_case import ExtTestCase, ignore_warnings |
| 6 | +from onnx_diagnostic.torch_models.llms import get_tiny_llm |
| 7 | +from onnx_diagnostic.torch_models.llms import get_phi2 |
| 8 | +from onnx_diagnostic.helpers import string_type |
| 9 | +from onnx_diagnostic.torch_export_patches import bypass_export_some_errors |
| 10 | +from onnx_diagnostic.torch_export_patches.patches.patch_transformers import ( |
| 11 | + patched_DynamicCache, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +class TestTinyLlmBypassed(ExtTestCase): |
| 16 | + @ignore_warnings(UserWarning) |
| 17 | + def test_export_tiny_llm_2_bypassed(self): |
| 18 | + data = get_tiny_llm() |
| 19 | + model, inputs = data["model"], data["inputs"] |
| 20 | + expected = model(**copy.deepcopy(inputs)) |
| 21 | + self.assertEqual( |
| 22 | + {"attention_mask", "past_key_values", "input_ids", "position_ids"}, set(inputs) |
| 23 | + ) |
| 24 | + |
| 25 | + with bypass_export_some_errors( |
| 26 | + patch_torch=False, patch_transformers=True, catch_constraints=False, verbose=10 |
| 27 | + ) as modificator: |
| 28 | + |
| 29 | + for k in patched_DynamicCache._PATCHES_: |
| 30 | + self.assertEqual(getattr(patched_DynamicCache, k), getattr(DynamicCache, k)) |
| 31 | + |
| 32 | + inputs = modificator(copy.deepcopy(inputs)) |
| 33 | + |
| 34 | + def debug(): |
| 35 | + print("***", string_type(inputs, with_shape=True)) |
| 36 | + print("***", data["dynamic_shapes"]) |
| 37 | + import torch.export._draft_export |
| 38 | + |
| 39 | + ep, report = torch.export._draft_export.draft_export( |
| 40 | + model, |
| 41 | + (), |
| 42 | + kwargs=inputs, |
| 43 | + dynamic_shapes=data["dynamic_shapes"], |
| 44 | + strict=False, |
| 45 | + ) |
| 46 | + print(report) |
| 47 | + |
| 48 | + if self._debug(): |
| 49 | + debug() |
| 50 | + |
| 51 | + ep = torch.export.export( |
| 52 | + model, (), kwargs=inputs, dynamic_shapes=data["dynamic_shapes"], strict=False |
| 53 | + ) |
| 54 | + got = ep.module()(**inputs) |
| 55 | + self.assertEqualArrayAny(expected, got) |
| 56 | + |
| 57 | + @ignore_warnings(UserWarning) |
| 58 | + def test_export_phi2_2_bypassed(self): |
| 59 | + data = get_phi2(num_hidden_layers=2) |
| 60 | + model, inputs, ds = data["model"], data["inputs"], data["dynamic_shapes"] |
| 61 | + self.assertEqual( |
| 62 | + {"attention_mask", "past_key_values", "input_ids", "position_ids"}, set(inputs) |
| 63 | + ) |
| 64 | + with bypass_export_some_errors(patch_transformers=True) as modificator: |
| 65 | + inputs = modificator(inputs) |
| 66 | + ep = torch.export.export(model, (), kwargs=inputs, dynamic_shapes=ds, strict=False) |
| 67 | + assert ep |
| 68 | + with bypass_export_some_errors(patch_transformers=True) as modificator: |
| 69 | + inputs = modificator(inputs) |
| 70 | + ep = torch.export.export(model, (), kwargs=inputs, dynamic_shapes=ds, strict=False) |
| 71 | + assert ep |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + unittest.main(verbosity=2) |
0 commit comments