|
1 | | -from typing import Any, Set |
| 1 | +from typing import Any, Dict, List, Set, Tuple, Union |
2 | 2 | from ..helpers.cache_helper import flatten_unflatten_for_dynamic_shapes |
| 3 | +from .dynamic_shapes import ModelInputs |
3 | 4 |
|
4 | 5 |
|
5 | 6 | def all_dynamic_shape_from_inputs(inputs: Any, dim_prefix: Any = "d") -> Any: |
@@ -47,3 +48,79 @@ def tensor_to_shape(tensor): |
47 | 48 | return flatten_unflatten_for_dynamic_shapes( |
48 | 49 | inputs, change_function=tensor_to_shape, use_dict=True |
49 | 50 | ) |
| 51 | + |
| 52 | + |
| 53 | +def guess_dynamic_shapes_from_inputs( |
| 54 | + inputs: List[Any], auto: Union[bool, str] = False |
| 55 | +) -> Tuple[Tuple[Any, ...], Dict[str, Any]]: |
| 56 | + """ |
| 57 | + Guesses which dimension is dimension from a set of inputs. |
| 58 | + Every dimension having different values over multiple sets |
| 59 | + of inputs. Every dimension not changing remains static. |
| 60 | +
|
| 61 | + :param inputs: a list of input sets |
| 62 | + :param auto: True for ``torch.export.Dim.AUTO``, |
| 63 | + False for ``torch.export.Dim.DYNAMIC``, |
| 64 | + a string to get a unique string for every dynamic dimension |
| 65 | + :return: args and kwargs |
| 66 | +
|
| 67 | + .. runpython:: |
| 68 | + :showcode: |
| 69 | +
|
| 70 | + import pprint |
| 71 | + import torch |
| 72 | + from onnx_diagnostic.helpers.cache_helper import make_dynamic_cache |
| 73 | + from onnx_diagnostic.export.shape_helper import guess_dynamic_shapes_from_inputs |
| 74 | +
|
| 75 | + bsize, nheads, slen, dim = 2, 1, 30, 96 |
| 76 | + inputs1 = dict( |
| 77 | + input_ids=torch.randint(15, size=(2, 3), dtype=torch.int64), |
| 78 | + attention_mask=torch.randint(1, size=(2, 33), dtype=torch.int64), |
| 79 | + position_ids=torch.arange(3, dtype=torch.int64), |
| 80 | + past_key_values=make_dynamic_cache( |
| 81 | + [ |
| 82 | + ( |
| 83 | + torch.randn(bsize, nheads, slen, dim), |
| 84 | + torch.randn(bsize, nheads, slen, dim), |
| 85 | + ), |
| 86 | + ] |
| 87 | + ), |
| 88 | + ) |
| 89 | + bsize, nheads, slen, dim = 3, 1, 33, 96 |
| 90 | + inputs2 = dict( |
| 91 | + input_ids=torch.randint(15, size=(3, 4), dtype=torch.int64), |
| 92 | + attention_mask=torch.randint(1, size=(3, 34), dtype=torch.int64), |
| 93 | + position_ids=torch.arange(4, dtype=torch.int64), |
| 94 | + past_key_values=make_dynamic_cache( |
| 95 | + [ |
| 96 | + ( |
| 97 | + torch.randn(bsize, nheads, slen, dim), |
| 98 | + torch.randn(bsize, nheads, slen, dim), |
| 99 | + ), |
| 100 | + ] |
| 101 | + ), |
| 102 | + ) |
| 103 | + ds = guess_dynamic_shapes_from_inputs([inputs1, inputs2], auto="d") |
| 104 | + pprint.pprint(ds) |
| 105 | +
|
| 106 | + This function returns something equivalent to function |
| 107 | + :class:`torch.export.dynamic_shapes.AdditionalInputs` but this |
| 108 | + one needs a model. |
| 109 | +
|
| 110 | + .. runpython:: |
| 111 | + :showcode: |
| 112 | +
|
| 113 | + import pprint |
| 114 | + import torch |
| 115 | + from onnx_diagnostic.helpers.cache_helper import make_dynamic_cache |
| 116 | + from onnx_diagnostic.export.shape_helper import guess_dynamic_shapes_from_inputs |
| 117 | + from onnx_diagnostic.torch_models.hghub import get_untrained_model_with_inputs |
| 118 | +
|
| 119 | + data = get_untrained_model_with_inputs("arnir0/Tiny-LLM", add_second_input=True) |
| 120 | + ds = torch.export.dynamic_shapes.AdditionalInputs() |
| 121 | + ds.add((), data["inputs"]) |
| 122 | + ds.add((), data["inputs2"]) |
| 123 | + pprint.pprint(ds.dynamic_shapes(data["model"], (), data["inputs"])) |
| 124 | + """ |
| 125 | + mi = ModelInputs(None, inputs) |
| 126 | + return mi.guess_dynamic_shapes(auto=auto) |
0 commit comments