|
| 1 | +import unittest |
| 2 | +import torch |
| 3 | +import transformers |
| 4 | +from onnx_diagnostic.ext_test_case import ( |
| 5 | + ExtTestCase, |
| 6 | + requires_transformers, |
| 7 | + hide_stdout, |
| 8 | + has_transformers, |
| 9 | +) |
| 10 | +from onnx_diagnostic.torch_export_patches import torch_export_patches |
| 11 | +from onnx_diagnostic.torch_export_patches.patch_inputs import use_dyn_not_str |
| 12 | +from onnx_diagnostic.torch_export_patches.patch_details import PatchDetails, PatchInfo |
| 13 | +from onnx_diagnostic.torch_models.hghub import get_untrained_model_with_inputs |
| 14 | + |
| 15 | + |
| 16 | +class TestPatchDetails(ExtTestCase): |
| 17 | + @hide_stdout() |
| 18 | + def test_patch_details(self): |
| 19 | + details = PatchDetails() |
| 20 | + with torch_export_patches( |
| 21 | + patch_transformers=True, |
| 22 | + verbose=10, |
| 23 | + patch_torch=True, |
| 24 | + patch_diffusers=True, |
| 25 | + patch_details=details, |
| 26 | + ): |
| 27 | + pass |
| 28 | + self.assertGreater(details.n_patches, 1) |
| 29 | + data = details.data() |
| 30 | + self.assertEqual(len(data), details.n_patches) |
| 31 | + for patch in details.patched: |
| 32 | + _kind, f1, f2 = patch.family, patch.function_to_patch, patch.patch |
| 33 | + raw = patch.format_diff(format="raw") |
| 34 | + if callable(f1): |
| 35 | + self.assertIn(f1.__name__, raw) |
| 36 | + self.assertIn(f2.__name__, raw) |
| 37 | + rst = patch.format_diff(format="rst") |
| 38 | + self.assertIn("====", rst) |
| 39 | + |
| 40 | + # second time to make every patch was removed |
| 41 | + with torch_export_patches( |
| 42 | + patch_transformers=True, |
| 43 | + verbose=10, |
| 44 | + patch_torch=True, |
| 45 | + patch_diffusers=True, |
| 46 | + patch_details=details, |
| 47 | + ): |
| 48 | + pass |
| 49 | + |
| 50 | + @requires_transformers("4.55") |
| 51 | + def test_patch_diff(self): |
| 52 | + from onnx_diagnostic.torch_export_patches.patches.patch_transformers import ( |
| 53 | + patched_eager_mask, |
| 54 | + ) |
| 55 | + |
| 56 | + eager_mask = transformers.masking_utils.eager_mask |
| 57 | + self.assertEqual(eager_mask.__name__, "eager_mask") |
| 58 | + self.assertEqual(patched_eager_mask.__name__, "patched_eager_mask") |
| 59 | + diff = PatchInfo(eager_mask, patched_eager_mask).format_diff(format="rst") |
| 60 | + self.assertIn("+ # PATCHED:", diff) |
| 61 | + |
| 62 | + def test_involved_patches(self): |
| 63 | + data = get_untrained_model_with_inputs("arnir0/Tiny-LLM", verbose=0) |
| 64 | + model, inputs, ds = data["model"], data["inputs"], data["dynamic_shapes"] |
| 65 | + details = PatchDetails() |
| 66 | + with torch_export_patches( |
| 67 | + patch_transformers=True, patch_details=details, patch_torch=False |
| 68 | + ): |
| 69 | + ep = torch.export.export( |
| 70 | + model, (), kwargs=inputs, dynamic_shapes=use_dyn_not_str(ds) |
| 71 | + ) |
| 72 | + patches = details.patches_involded_in_graph(ep.graph) |
| 73 | + self.assertNotEmpty(patches) |
| 74 | + report = details.make_report(patches, format="rst") |
| 75 | + if has_transformers("4.51"): |
| 76 | + self.assertIn("====", report) |
| 77 | + self.assertIn("def longrope_frequency_update", report) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + unittest.main(verbosity=2) |
0 commit comments