|
5 | 5 | Find and fix an export issue due to dynamic shapes |
6 | 6 | ================================================== |
7 | 7 |
|
| 8 | +LLMs must be exported with dynamic shapes and it is common that |
| 9 | +a static dimension turns into a static ones. The error message from |
| 10 | +:epkg:`pytorch` tells the user to define ``TORCH_LOGS="+dynamic"`` |
| 11 | +but it shows a very long list of messages where we need |
| 12 | +to find the string ``range_refined_to_singleton`` and that |
| 13 | +does not really indicates where it comes from. The example |
| 14 | +shows how to tweak pytorch to get that information until |
| 15 | +it gets better. |
8 | 16 |
|
9 | 17 | A model with an export issue |
10 | 18 | ============================ |
|
15 | 23 | it is difficult to find deep inside a big model. |
16 | 24 | """ |
17 | 25 |
|
| 26 | +import traceback |
18 | 27 | import torch |
19 | 28 | from onnx_diagnostic.torch_export_patches import bypass_export_some_errors |
20 | 29 |
|
@@ -69,10 +78,17 @@ def forward(self, x: torch.Tensor, ys: list[torch.Tensor]): |
69 | 78 | # Stop when a dynamic dimension turns static |
70 | 79 | # ========================================== |
71 | 80 | # |
72 | | -# |
| 81 | +# We use :func:`bypass_export_some_errors |
| 82 | +# <onnx_diagnostic.torch_export_patches.bypass_export_some_errors>` |
| 83 | +# to replace torch implementation by a new one raising the exception |
| 84 | +# mentioned in previous section. |
73 | 85 |
|
74 | 86 | with bypass_export_some_errors(stop_if_static=True, verbose=1): |
75 | | - torch.export.export(model, inputs, dynamic_shapes=dyn_shapes) |
| 87 | + try: |
| 88 | + torch.export.export(model, inputs, dynamic_shapes=dyn_shapes) |
| 89 | + except AssertionError: |
| 90 | + print("-- It failed as excepted. Let's print the stack trace.") |
| 91 | + print(traceback.format_exc()) |
76 | 92 |
|
77 | 93 | # The stack trace is quite long but the first line referring to this example |
78 | 94 | # is the following one. It points out the line turing a dynamic dimension into |
|
0 commit comments