|
| 1 | +""" |
| 2 | +Builds dynamic shapes from any input |
| 3 | +==================================== |
| 4 | +
|
| 5 | +Getting dynamic shapes right for :func:`torch.export.export` when the inputs |
| 6 | +includes a custom class such as a :class:`transformers.cache_utils.DynamicCache`. |
| 7 | +:func:`torch.export.export` cannot use a DynamicCache filled with dynamic shapes |
| 8 | +but instead it uses a kind of unserialized serialized form of it. |
| 9 | +
|
| 10 | +Standard inputs for a LLM with a dynamic cache |
| 11 | +++++++++++++++++++++++++++++++++++++++++++++++ |
| 12 | +""" |
| 13 | + |
| 14 | +import pprint |
| 15 | +import torch |
| 16 | +from onnx_diagnostic import doc |
| 17 | +from onnx_diagnostic.helpers import string_type |
| 18 | +from onnx_diagnostic.helpers.cache_helper import make_dynamic_cache |
| 19 | +from onnx_diagnostic.export.shape_helper import all_dynamic_shape_from_inputs |
| 20 | +from onnx_diagnostic.torch_models.hghub import get_untrained_model_with_inputs |
| 21 | +from onnx_diagnostic.torch_export_patches import torch_export_patches |
| 22 | + |
| 23 | +bsize, nheads, slen, dim = 2, 1, 30, 96 |
| 24 | + |
| 25 | +inputs = dict( |
| 26 | + input_ids=torch.randint(15, size=(2, 3), dtype=torch.int64), |
| 27 | + attention_mask=torch.randint(1, size=(2, 33), dtype=torch.int64), |
| 28 | + position_ids=torch.arange(3, dtype=torch.int64), |
| 29 | + past_key_values=make_dynamic_cache( |
| 30 | + [(torch.randn(bsize, nheads, slen, dim), torch.randn(bsize, nheads, slen, dim))] |
| 31 | + ), |
| 32 | +) |
| 33 | + |
| 34 | +print(string_type(inputs, with_shape=True)) |
| 35 | + |
| 36 | +# %% |
| 37 | +# Function :func:`onnx_diagnostic.export.shape_helper.all_dynamic_shape_from_inputs` |
| 38 | +# produces the corresponding dynamic shapes assuming they are all dynamic. |
| 39 | +ds = all_dynamic_shape_from_inputs(inputs) |
| 40 | +pprint.pprint(ds) |
| 41 | + |
| 42 | +# %% |
| 43 | +# What about a StaticCache? |
| 44 | +# +++++++++++++++++++++++++ |
| 45 | +# |
| 46 | +# We use :func:`onnx_diagnostic.torch_models.hghub.get_untrained_model_with_inputs` to get |
| 47 | +# a consistent configuration with a static cache. |
| 48 | + |
| 49 | +data = get_untrained_model_with_inputs( |
| 50 | + "arnir0/Tiny-LLM", |
| 51 | + model_kwargs=dict(cache_implementation="static"), |
| 52 | + inputs_kwargs=dict(cls_cache="StaticCache"), |
| 53 | +) |
| 54 | +inputs = data["inputs"] |
| 55 | +print(string_type(inputs, with_shape=True)) |
| 56 | + |
| 57 | +# %% |
| 58 | +# And the input shapes. |
| 59 | +ds = all_dynamic_shape_from_inputs(inputs) |
| 60 | +if ds["past_key_values"]: |
| 61 | + print("transformers implemented serialization function for StaticCache.") |
| 62 | +else: |
| 63 | + print("We need to use serialization function implemented in this package.") |
| 64 | + with torch_export_patches(patch_transformers=True): |
| 65 | + ds = all_dynamic_shape_from_inputs(inputs) |
| 66 | + |
| 67 | +# %% |
| 68 | +# That gives. |
| 69 | +pprint.pprint(ds) |
| 70 | + |
| 71 | +# %% |
| 72 | +# We can compare with the ones returned by the function. |
| 73 | +pprint.pprint(data["dynamic_shapes"]) |
| 74 | + |
| 75 | + |
| 76 | +# %% |
| 77 | + |
| 78 | +doc.plot_legend("dynamic shapes\nfrom inputs", "dynamic shapes", "green") |
0 commit comments