onnxscript 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- onnxscript/__init__.py +131 -0
- onnxscript/_framework_apis/__init__.py +3 -0
- onnxscript/_framework_apis/torch_2_5.py +117 -0
- onnxscript/_framework_apis/torch_2_6.py +45 -0
- onnxscript/_internal/__init__.py +0 -0
- onnxscript/_internal/analysis.py +229 -0
- onnxscript/_internal/ast_utils.py +64 -0
- onnxscript/_internal/autocast.py +250 -0
- onnxscript/_internal/deprecation.py +78 -0
- onnxscript/_internal/param_manipulation.py +148 -0
- onnxscript/_internal/runtime_typing.py +43 -0
- onnxscript/_internal/utils.py +99 -0
- onnxscript/_internal/version_utils.py +118 -0
- onnxscript/_legacy_ir/__init__.py +341 -0
- onnxscript/_legacy_ir/visitor.py +937 -0
- onnxscript/_thirdparty/asciichartpy.py +313 -0
- onnxscript/backend/__init__.py +2 -0
- onnxscript/backend/onnx_backend.py +303 -0
- onnxscript/backend/onnx_export.py +885 -0
- onnxscript/converter.py +1470 -0
- onnxscript/evaluator.py +619 -0
- onnxscript/function_libs/tools/torch_lib/deduce_type_constraints.py +403 -0
- onnxscript/function_libs/tools/torch_lib/generate_aten_signatures.py +333 -0
- onnxscript/function_libs/tools/torch_lib/generate_prims_signatures.py +331 -0
- onnxscript/function_libs/torch_lib/__init__.py +12 -0
- onnxscript/function_libs/torch_lib/_constants.py +5 -0
- onnxscript/function_libs/torch_lib/_flags.py +58 -0
- onnxscript/function_libs/torch_lib/graph_building/__init__.py +56 -0
- onnxscript/function_libs/torch_lib/graph_building/_graph_building_ir.py +723 -0
- onnxscript/function_libs/torch_lib/graph_building/_graph_building_torch.py +1125 -0
- onnxscript/function_libs/torch_lib/ops/__init__.py +27 -0
- onnxscript/function_libs/torch_lib/ops/common.py +80 -0
- onnxscript/function_libs/torch_lib/ops/core.py +8935 -0
- onnxscript/function_libs/torch_lib/ops/fft.py +385 -0
- onnxscript/function_libs/torch_lib/ops/linalg.py +399 -0
- onnxscript/function_libs/torch_lib/ops/nested.py +25 -0
- onnxscript/function_libs/torch_lib/ops/nn.py +2713 -0
- onnxscript/function_libs/torch_lib/ops/prims.py +850 -0
- onnxscript/function_libs/torch_lib/ops/quantized_decomposed.py +63 -0
- onnxscript/function_libs/torch_lib/ops/sparse.py +23 -0
- onnxscript/function_libs/torch_lib/ops/special.py +387 -0
- onnxscript/function_libs/torch_lib/ops/vision.py +25 -0
- onnxscript/function_libs/torch_lib/registration.py +151 -0
- onnxscript/function_libs/torch_lib/tensor_typing.py +74 -0
- onnxscript/ir/__init__.py +153 -0
- onnxscript/ir/_convenience.py +447 -0
- onnxscript/ir/_core.py +3119 -0
- onnxscript/ir/_display.py +49 -0
- onnxscript/ir/_enums.py +163 -0
- onnxscript/ir/_graph_comparison.py +23 -0
- onnxscript/ir/_io.py +97 -0
- onnxscript/ir/_linked_list.py +276 -0
- onnxscript/ir/_metadata.py +44 -0
- onnxscript/ir/_name_authority.py +72 -0
- onnxscript/ir/_polyfill.py +25 -0
- onnxscript/ir/_protocols.py +598 -0
- onnxscript/ir/_schemas.py +548 -0
- onnxscript/ir/_tape.py +120 -0
- onnxscript/ir/_type_casting.py +106 -0
- onnxscript/ir/convenience.py +32 -0
- onnxscript/ir/external_data.py +396 -0
- onnxscript/ir/passes/__init__.py +33 -0
- onnxscript/ir/passes/_pass_infra.py +172 -0
- onnxscript/ir/serde.py +1620 -0
- onnxscript/ir/tensor_adapters.py +122 -0
- onnxscript/ir/traversal.py +82 -0
- onnxscript/irbuilder.py +542 -0
- onnxscript/main.py +167 -0
- onnxscript/onnx_opset/__init__.py +232 -0
- onnxscript/onnx_opset/_impl/opset1.py +4100 -0
- onnxscript/onnx_opset/_impl/opset10.py +1227 -0
- onnxscript/onnx_opset/_impl/opset11.py +4013 -0
- onnxscript/onnx_opset/_impl/opset12.py +1078 -0
- onnxscript/onnx_opset/_impl/opset13.py +3924 -0
- onnxscript/onnx_opset/_impl/opset14.py +999 -0
- onnxscript/onnx_opset/_impl/opset15.py +604 -0
- onnxscript/onnx_opset/_impl/opset16.py +1255 -0
- onnxscript/onnx_opset/_impl/opset17.py +561 -0
- onnxscript/onnx_opset/_impl/opset18.py +1803 -0
- onnxscript/onnx_opset/_impl/opset19.py +1942 -0
- onnxscript/onnx_opset/_impl/opset2.py +218 -0
- onnxscript/onnx_opset/_impl/opset20.py +675 -0
- onnxscript/onnx_opset/_impl/opset21.py +1976 -0
- onnxscript/onnx_opset/_impl/opset22.py +2588 -0
- onnxscript/onnx_opset/_impl/opset3.py +199 -0
- onnxscript/onnx_opset/_impl/opset4.py +77 -0
- onnxscript/onnx_opset/_impl/opset5.py +84 -0
- onnxscript/onnx_opset/_impl/opset6.py +944 -0
- onnxscript/onnx_opset/_impl/opset7.py +1243 -0
- onnxscript/onnx_opset/_impl/opset8.py +444 -0
- onnxscript/onnx_opset/_impl/opset9.py +1485 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml1.py +974 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml2.py +112 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml3.py +308 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml4.py +129 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml5.py +158 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_preview_training1.py +577 -0
- onnxscript/onnx_types.py +229 -0
- onnxscript/optimizer/__init__.py +39 -0
- onnxscript/optimizer/_constant_folding.py +1083 -0
- onnxscript/optimizer/_inliner.py +312 -0
- onnxscript/optimizer/_legacy/_optimizer.py +98 -0
- onnxscript/optimizer/_legacy/_remove_unused_proto.py +144 -0
- onnxscript/optimizer/_legacy/_simple_function_folding.py +243 -0
- onnxscript/optimizer/_legacy/constant_folding.py +293 -0
- onnxscript/optimizer/_legacy/evaluator.py +439 -0
- onnxscript/optimizer/_optimizer.py +61 -0
- onnxscript/optimizer/_remove_unused.py +106 -0
- onnxscript/optimizer/_remove_unused_function.py +72 -0
- onnxscript/py.typed +1 -0
- onnxscript/rewriter/__init__.py +56 -0
- onnxscript/rewriter/_ir_utils.py +111 -0
- onnxscript/rewriter/broadcast_to_matmul.py +180 -0
- onnxscript/rewriter/cast_constant_of_shape.py +50 -0
- onnxscript/rewriter/collapse_slices.py +141 -0
- onnxscript/rewriter/erfgelu.py +27 -0
- onnxscript/rewriter/function_rule.py +232 -0
- onnxscript/rewriter/gemm_to_matmul_add.py +21 -0
- onnxscript/rewriter/generic_pattern.py +700 -0
- onnxscript/rewriter/llama_rule_sets.py +287 -0
- onnxscript/rewriter/no_op.py +56 -0
- onnxscript/rewriter/onnxruntime/__init__.py +50 -0
- onnxscript/rewriter/onnxruntime/bfloat16_utils/bfloat16_converter.py +99 -0
- onnxscript/rewriter/onnxruntime/fused_matmul_rule_sets.py +177 -0
- onnxscript/rewriter/onnxruntime/group_normalization_merge_silu.py +64 -0
- onnxscript/rewriter/onnxruntime/instance_to_group_normalization.py +155 -0
- onnxscript/rewriter/onnxruntime/softmax.py +63 -0
- onnxscript/rewriter/onnxruntime/transformers/__init__.py +21 -0
- onnxscript/rewriter/onnxruntime/transformers/biassplitgelu.py +31 -0
- onnxscript/rewriter/onnxruntime/transformers/fastgelu.py +29 -0
- onnxscript/rewriter/onnxruntime/transformers/layernorm.py +47 -0
- onnxscript/rewriter/onnxruntime/transformers/multihead_attention.py +715 -0
- onnxscript/rewriter/ort_fusions/__init__.py +9 -0
- onnxscript/rewriter/ort_fusions/_core.py +28 -0
- onnxscript/rewriter/ort_fusions/_smollm_1.py +253 -0
- onnxscript/rewriter/ort_fusions/_smollm_2.py +467 -0
- onnxscript/rewriter/ort_fusions/_test_models.py +122 -0
- onnxscript/rewriter/ort_fusions/_test_utils.py +42 -0
- onnxscript/rewriter/ort_fusions/cos_sin_cache.py +154 -0
- onnxscript/rewriter/ort_fusions/gqa.py +156 -0
- onnxscript/rewriter/ort_fusions/mha.py +198 -0
- onnxscript/rewriter/ort_fusions/rms_normalization.py +95 -0
- onnxscript/rewriter/ort_fusions/rotary_embedding.py +64 -0
- onnxscript/rewriter/ort_fusions/sdpa.py +75 -0
- onnxscript/rewriter/ort_fusions/skip_normalization.py +46 -0
- onnxscript/rewriter/pattern.py +1714 -0
- onnxscript/rewriter/testing.py +77 -0
- onnxscript/sourceinfo.py +59 -0
- onnxscript/tensor.py +227 -0
- onnxscript/testing/__init__.py +482 -0
- onnxscript/tools/__init__.py +4 -0
- onnxscript/tools/benchmark/__init__.py +23 -0
- onnxscript/tools/benchmark/benchmark_helpers.py +783 -0
- onnxscript/tools/benchmark/benchmark_run.py +140 -0
- onnxscript/tools/benchmark/export_model.py +207 -0
- onnxscript/tools/benchmark/export_model_batch.py +146 -0
- onnxscript/tools/memory_peak.py +244 -0
- onnxscript/tools/training_helper.py +50 -0
- onnxscript/tools/transformers_models/__init__.py +190 -0
- onnxscript/tools/transformers_models/llama.py +168 -0
- onnxscript/tools/transformers_models/mistral.py +238 -0
- onnxscript/tools/transformers_models/phi.py +248 -0
- onnxscript/tools/transformers_models/phi3.py +259 -0
- onnxscript/type_annotation.py +281 -0
- onnxscript/utils/__init__.py +0 -0
- onnxscript/utils/evaluation_utils.py +56 -0
- onnxscript/utils/timing_utils.py +33 -0
- onnxscript/utils/utils.py +84 -0
- onnxscript/values.py +790 -0
- onnxscript/version_converter/__init__.py +21 -0
- onnxscript/version_converter/_version_converter.py +314 -0
- onnxscript-0.1.0.dist-info/LICENSE +21 -0
- onnxscript-0.1.0.dist-info/METADATA +370 -0
- onnxscript-0.1.0.dist-info/RECORD +176 -0
- onnxscript-0.1.0.dist-info/WHEEL +5 -0
- onnxscript-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
# -------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# Licensed under the MIT License.
|
|
4
|
+
# --------------------------------------------------------------------------
|
|
5
|
+
# pylint: disable=import-outside-toplevel
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from typing import Any, Sequence
|
|
9
|
+
|
|
10
|
+
import torch
|
|
11
|
+
|
|
12
|
+
import onnxscript.tools.transformers_models
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _prepare_config_and_inputs(
|
|
16
|
+
batch_size: int,
|
|
17
|
+
seq_length: int,
|
|
18
|
+
vocab_size: int,
|
|
19
|
+
type_sequence_label_size: int = 2,
|
|
20
|
+
type_vocab_size: int = 16,
|
|
21
|
+
num_labels: int = 3,
|
|
22
|
+
num_choices: int = 4,
|
|
23
|
+
use_input_mask: bool = False,
|
|
24
|
+
use_token_type_ids: bool = False,
|
|
25
|
+
use_labels: bool = False,
|
|
26
|
+
) -> tuple[Any, ...]:
|
|
27
|
+
input_ids = onnxscript.tools.transformers_models.ids_tensor(
|
|
28
|
+
[batch_size, seq_length], vocab_size
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
input_mask = None
|
|
32
|
+
if use_input_mask:
|
|
33
|
+
input_mask = torch.tril(torch.ones(batch_size, seq_length))
|
|
34
|
+
|
|
35
|
+
token_type_ids = None
|
|
36
|
+
if use_token_type_ids:
|
|
37
|
+
assert type_vocab_size > 0, "type_vocab_size is null"
|
|
38
|
+
token_type_ids = onnxscript.tools.transformers_models.ids_tensor(
|
|
39
|
+
[batch_size, seq_length], type_vocab_size
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
sequence_labels = None
|
|
43
|
+
token_labels = None
|
|
44
|
+
choice_labels = None
|
|
45
|
+
if use_labels:
|
|
46
|
+
assert type_sequence_label_size > 0, "type_sequence_label_size is null"
|
|
47
|
+
assert num_labels > 0, "num_labels is null"
|
|
48
|
+
assert num_choices > 0, "num_choices is null"
|
|
49
|
+
sequence_labels = onnxscript.tools.transformers_models.ids_tensor(
|
|
50
|
+
[batch_size], type_sequence_label_size
|
|
51
|
+
)
|
|
52
|
+
token_labels = onnxscript.tools.transformers_models.ids_tensor(
|
|
53
|
+
[batch_size, seq_length], num_labels
|
|
54
|
+
)
|
|
55
|
+
choice_labels = onnxscript.tools.transformers_models.ids_tensor(
|
|
56
|
+
[batch_size], num_choices
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
input_ids,
|
|
61
|
+
token_type_ids,
|
|
62
|
+
input_mask,
|
|
63
|
+
sequence_labels,
|
|
64
|
+
token_labels,
|
|
65
|
+
choice_labels,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def get_mistral_model(
|
|
70
|
+
input_dims: Sequence[tuple[int, int]] = ((13, 7), (14, 7), (15, 8)),
|
|
71
|
+
hidden_size=32,
|
|
72
|
+
num_hidden_layers=2,
|
|
73
|
+
vocab_size=99,
|
|
74
|
+
intermediate_size=16,
|
|
75
|
+
max_position_embeddings=512,
|
|
76
|
+
num_attention_heads=2,
|
|
77
|
+
num_key_value_heads=2,
|
|
78
|
+
sliding_window=4096,
|
|
79
|
+
_attn_implementation="eager", # needed value to remove graph breaks
|
|
80
|
+
with_mask: bool = True,
|
|
81
|
+
) -> tuple[Any, list[tuple[torch.Tensor, ...]], dict]:
|
|
82
|
+
"""
|
|
83
|
+
Returns a model.
|
|
84
|
+
See `MistralConfig
|
|
85
|
+
<https://huggingface.co/docs/transformers/main/en/model_doc/mistral#transformers.MistralConfig>`_.
|
|
86
|
+
The parameters are chosen for a unit test configuration.
|
|
87
|
+
"""
|
|
88
|
+
from transformers import MistralConfig
|
|
89
|
+
from transformers.models.mistral.modeling_mistral import MistralModel
|
|
90
|
+
|
|
91
|
+
config = MistralConfig(
|
|
92
|
+
num_hidden_layers=num_hidden_layers,
|
|
93
|
+
vocab_size=vocab_size,
|
|
94
|
+
hidden_size=hidden_size,
|
|
95
|
+
intermediate_size=intermediate_size,
|
|
96
|
+
max_position_embeddings=max_position_embeddings,
|
|
97
|
+
num_attention_heads=num_attention_heads,
|
|
98
|
+
num_key_value_heads=num_key_value_heads,
|
|
99
|
+
sliding_window=sliding_window,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
dynamic_shapes = {0: {0: "batch", 1: "length"}}
|
|
103
|
+
if with_mask:
|
|
104
|
+
dynamic_shapes.update({1: {0: "batch", 1: "length"}})
|
|
105
|
+
|
|
106
|
+
if _attn_implementation:
|
|
107
|
+
config._attn_implementation = _attn_implementation # pylint: disable=protected-access
|
|
108
|
+
|
|
109
|
+
def generate_example_inputs(batch: int, seq: int, vocab_size: int, with_mask: bool):
|
|
110
|
+
(
|
|
111
|
+
input_ids,
|
|
112
|
+
_, # token_type_ids,
|
|
113
|
+
input_mask,
|
|
114
|
+
_, # sequence_labels,
|
|
115
|
+
_, # token_labels,
|
|
116
|
+
_, # choice_labels,
|
|
117
|
+
) = _prepare_config_and_inputs(
|
|
118
|
+
batch_size=batch,
|
|
119
|
+
seq_length=seq,
|
|
120
|
+
vocab_size=vocab_size,
|
|
121
|
+
use_input_mask=with_mask,
|
|
122
|
+
)
|
|
123
|
+
if with_mask:
|
|
124
|
+
return input_ids, input_mask
|
|
125
|
+
return (input_ids,)
|
|
126
|
+
|
|
127
|
+
if with_mask:
|
|
128
|
+
|
|
129
|
+
class MistralModelWrapperWithMask(torch.nn.Module):
|
|
130
|
+
def __init__(self, config):
|
|
131
|
+
super().__init__()
|
|
132
|
+
self.model = MistralModel(config)
|
|
133
|
+
|
|
134
|
+
def forward(self, input_ids, attention_mask):
|
|
135
|
+
model_output = self.model(
|
|
136
|
+
input_ids, attention_mask=attention_mask, use_cache=False
|
|
137
|
+
)
|
|
138
|
+
return model_output.to_tuple()
|
|
139
|
+
|
|
140
|
+
example_args_collection = []
|
|
141
|
+
for b, s in input_dims:
|
|
142
|
+
example_args_collection.append(
|
|
143
|
+
generate_example_inputs(b, s, vocab_size, with_mask)
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
return MistralModelWrapperWithMask(config), example_args_collection, dynamic_shapes
|
|
147
|
+
|
|
148
|
+
class MistralModelWrapper(torch.nn.Module):
|
|
149
|
+
def __init__(self, config):
|
|
150
|
+
super().__init__()
|
|
151
|
+
self.model = MistralModel(config)
|
|
152
|
+
|
|
153
|
+
def forward(self, input_ids):
|
|
154
|
+
model_output = self.model(input_ids, use_cache=False)
|
|
155
|
+
return model_output.to_tuple()
|
|
156
|
+
|
|
157
|
+
example_args_collection = []
|
|
158
|
+
for b, s in input_dims:
|
|
159
|
+
example_args_collection.append(generate_example_inputs(b, s, vocab_size, with_mask))
|
|
160
|
+
|
|
161
|
+
return MistralModelWrapper(config), example_args_collection, dynamic_shapes
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def get_mistral_model_from_config(
|
|
165
|
+
warmup: int = 5,
|
|
166
|
+
repeat: int = 10,
|
|
167
|
+
config: str = "small",
|
|
168
|
+
num_hidden_layers: int = 1,
|
|
169
|
+
implementation: str = "eager",
|
|
170
|
+
dynamic_shapes: bool = False,
|
|
171
|
+
with_mask: bool = True,
|
|
172
|
+
) -> tuple[Any, list[tuple[torch.Tensor, ...]], dict]:
|
|
173
|
+
"""
|
|
174
|
+
Returns a model Phi to test or benchmark.
|
|
175
|
+
|
|
176
|
+
Args:
|
|
177
|
+
warmup: Number of inputs to generate.
|
|
178
|
+
repeat: Number of inputs to generate for repeat.
|
|
179
|
+
config: small, medium or large
|
|
180
|
+
num_hidden_layers: number of hidden layers
|
|
181
|
+
implementation: eager or sdpa
|
|
182
|
+
with_mask: One or two inputs.
|
|
183
|
+
dynamic_shapes: dynamic shapes or not
|
|
184
|
+
|
|
185
|
+
Returns:
|
|
186
|
+
Model and list of inputs.
|
|
187
|
+
"""
|
|
188
|
+
if config == "small":
|
|
189
|
+
conf_dict = dict(
|
|
190
|
+
input_dims=onnxscript.tools.transformers_models.get_input_dims_for_llm(
|
|
191
|
+
dynamic_shapes, warmup, repeat
|
|
192
|
+
),
|
|
193
|
+
hidden_size=32,
|
|
194
|
+
num_hidden_layers=num_hidden_layers,
|
|
195
|
+
vocab_size=99,
|
|
196
|
+
intermediate_size=16,
|
|
197
|
+
max_position_embeddings=512,
|
|
198
|
+
num_attention_heads=4,
|
|
199
|
+
num_key_value_heads=2,
|
|
200
|
+
_attn_implementation=implementation,
|
|
201
|
+
with_mask=with_mask,
|
|
202
|
+
)
|
|
203
|
+
elif config == "medium":
|
|
204
|
+
conf_dict = dict(
|
|
205
|
+
input_dims=onnxscript.tools.transformers_models.get_input_dims_for_llm(
|
|
206
|
+
dynamic_shapes, warmup, repeat
|
|
207
|
+
),
|
|
208
|
+
hidden_size=1024,
|
|
209
|
+
num_hidden_layers=num_hidden_layers,
|
|
210
|
+
vocab_size=1024,
|
|
211
|
+
intermediate_size=1024,
|
|
212
|
+
num_attention_heads=4,
|
|
213
|
+
num_key_value_heads=4,
|
|
214
|
+
max_position_embeddings=1024,
|
|
215
|
+
sliding_window=4096,
|
|
216
|
+
_attn_implementation=implementation,
|
|
217
|
+
with_mask=with_mask,
|
|
218
|
+
)
|
|
219
|
+
elif config in ("large", "default"):
|
|
220
|
+
conf_dict = dict(
|
|
221
|
+
input_dims=onnxscript.tools.transformers_models.get_input_dims_for_llm(
|
|
222
|
+
dynamic_shapes, warmup, repeat
|
|
223
|
+
),
|
|
224
|
+
hidden_size=4096,
|
|
225
|
+
num_hidden_layers=num_hidden_layers,
|
|
226
|
+
vocab_size=32000,
|
|
227
|
+
intermediate_size=14336,
|
|
228
|
+
num_attention_heads=32,
|
|
229
|
+
num_key_value_heads=8,
|
|
230
|
+
max_position_embeddings=131072,
|
|
231
|
+
sliding_window=4096,
|
|
232
|
+
_attn_implementation=implementation,
|
|
233
|
+
with_mask=with_mask,
|
|
234
|
+
)
|
|
235
|
+
else:
|
|
236
|
+
raise ValueError(f"Unexpected configuration {config!r}.")
|
|
237
|
+
|
|
238
|
+
return get_mistral_model(**conf_dict) # type: ignore[arg-type]
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
# -------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# Licensed under the MIT License.
|
|
4
|
+
# --------------------------------------------------------------------------
|
|
5
|
+
# pylint: disable=import-outside-toplevel
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from typing import Any, Sequence
|
|
9
|
+
|
|
10
|
+
import torch
|
|
11
|
+
|
|
12
|
+
import onnxscript.tools.transformers_models
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _prepare_config_and_inputs(
|
|
16
|
+
batch_size: int,
|
|
17
|
+
seq_length: int,
|
|
18
|
+
vocab_size: int,
|
|
19
|
+
type_sequence_label_size: int = 2,
|
|
20
|
+
type_vocab_size: int = 16,
|
|
21
|
+
num_labels: int = 3,
|
|
22
|
+
num_choices: int = 4,
|
|
23
|
+
use_input_mask: bool = False,
|
|
24
|
+
use_token_type_ids: bool = False,
|
|
25
|
+
use_labels: bool = False,
|
|
26
|
+
) -> tuple[Any, ...]:
|
|
27
|
+
input_ids = onnxscript.tools.transformers_models.ids_tensor(
|
|
28
|
+
[batch_size, seq_length], vocab_size
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
input_mask = None
|
|
32
|
+
if use_input_mask:
|
|
33
|
+
input_mask = torch.tril(torch.ones(batch_size, seq_length))
|
|
34
|
+
|
|
35
|
+
token_type_ids = None
|
|
36
|
+
if use_token_type_ids:
|
|
37
|
+
assert type_vocab_size > 0, "type_vocab_size is null"
|
|
38
|
+
token_type_ids = onnxscript.tools.transformers_models.ids_tensor(
|
|
39
|
+
[batch_size, seq_length], type_vocab_size
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
sequence_labels = None
|
|
43
|
+
token_labels = None
|
|
44
|
+
choice_labels = None
|
|
45
|
+
if use_labels:
|
|
46
|
+
assert type_sequence_label_size > 0, "type_sequence_label_size is null"
|
|
47
|
+
assert num_labels > 0, "num_labels is null"
|
|
48
|
+
assert num_choices > 0, "num_choices is null"
|
|
49
|
+
sequence_labels = onnxscript.tools.transformers_models.ids_tensor(
|
|
50
|
+
[batch_size], type_sequence_label_size
|
|
51
|
+
)
|
|
52
|
+
token_labels = onnxscript.tools.transformers_models.ids_tensor(
|
|
53
|
+
[batch_size, seq_length], num_labels
|
|
54
|
+
)
|
|
55
|
+
choice_labels = onnxscript.tools.transformers_models.ids_tensor(
|
|
56
|
+
[batch_size], num_choices
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
input_ids,
|
|
61
|
+
token_type_ids,
|
|
62
|
+
input_mask,
|
|
63
|
+
sequence_labels,
|
|
64
|
+
token_labels,
|
|
65
|
+
choice_labels,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def get_phi_model(
|
|
70
|
+
input_dims: Sequence[tuple[int, int]] = ((13, 7), (14, 7), (15, 8)),
|
|
71
|
+
hidden_size: int = 32,
|
|
72
|
+
num_hidden_layers: int = 2,
|
|
73
|
+
vocab_size: int = 99,
|
|
74
|
+
intermediate_size: int = 16,
|
|
75
|
+
max_position_embeddings: int = 512,
|
|
76
|
+
num_attention_heads: int = 4,
|
|
77
|
+
num_key_value_heads: int = 2,
|
|
78
|
+
_attn_implementation: str = "eager", # needed value to remove graph breaks
|
|
79
|
+
with_mask: bool = True,
|
|
80
|
+
) -> tuple[Any, list[tuple[torch.Tensor, ...]], dict]:
|
|
81
|
+
"""
|
|
82
|
+
Returns a model.
|
|
83
|
+
See `PhiConfig
|
|
84
|
+
<https://huggingface.co/docs/transformers/main/en/model_doc/phi#transformers.PhiConfig>`_.
|
|
85
|
+
The parameters are chosen for a unit test configuration from `test_modeling_phi.py
|
|
86
|
+
<https://github.com/huggingface/transformers/blob/main/tests/models/phi/test_modeling_phi.py>`_.
|
|
87
|
+
"""
|
|
88
|
+
from transformers import PhiConfig
|
|
89
|
+
from transformers.models.phi.modeling_phi import PhiModel
|
|
90
|
+
|
|
91
|
+
dynamic_shapes = {0: {0: "batch", 1: "length"}}
|
|
92
|
+
if with_mask:
|
|
93
|
+
dynamic_shapes.update({1: {0: "batch", 1: "length"}})
|
|
94
|
+
|
|
95
|
+
config = PhiConfig(
|
|
96
|
+
hidden_size=hidden_size,
|
|
97
|
+
num_hidden_layers=num_hidden_layers,
|
|
98
|
+
vocab_size=vocab_size,
|
|
99
|
+
intermediate_size=intermediate_size,
|
|
100
|
+
max_position_embeddings=max_position_embeddings,
|
|
101
|
+
num_attention_heads=num_attention_heads,
|
|
102
|
+
num_key_value_heads=num_key_value_heads,
|
|
103
|
+
)
|
|
104
|
+
if _attn_implementation:
|
|
105
|
+
config._attn_implementation = _attn_implementation # pylint: disable=protected-access
|
|
106
|
+
|
|
107
|
+
if with_mask:
|
|
108
|
+
|
|
109
|
+
class PhiModelWrapper(torch.nn.Module):
|
|
110
|
+
def __init__(self, config):
|
|
111
|
+
super().__init__()
|
|
112
|
+
self.model = PhiModel(config)
|
|
113
|
+
|
|
114
|
+
def forward(self, input_ids, attention_mask):
|
|
115
|
+
model_output = self.model(
|
|
116
|
+
input_ids, attention_mask=attention_mask, use_cache=False
|
|
117
|
+
)
|
|
118
|
+
return model_output.to_tuple()
|
|
119
|
+
|
|
120
|
+
def generate_example_inputs(batch: int, seq: int, vocab_size: int):
|
|
121
|
+
(
|
|
122
|
+
input_ids,
|
|
123
|
+
_, # token_type_ids,
|
|
124
|
+
input_mask,
|
|
125
|
+
_, # sequence_labels,
|
|
126
|
+
_, # token_labels,
|
|
127
|
+
_, # choice_labels,
|
|
128
|
+
) = _prepare_config_and_inputs(
|
|
129
|
+
batch_size=batch,
|
|
130
|
+
seq_length=seq,
|
|
131
|
+
vocab_size=vocab_size,
|
|
132
|
+
use_input_mask=True,
|
|
133
|
+
)
|
|
134
|
+
return input_ids, input_mask
|
|
135
|
+
|
|
136
|
+
example_args_collection = []
|
|
137
|
+
for b, s in input_dims:
|
|
138
|
+
example_args_collection.append(generate_example_inputs(b, s, vocab_size))
|
|
139
|
+
|
|
140
|
+
return PhiModelWrapper(config), example_args_collection, dynamic_shapes
|
|
141
|
+
|
|
142
|
+
# no mask
|
|
143
|
+
|
|
144
|
+
class PhiModelWrapperNoMask(torch.nn.Module):
|
|
145
|
+
def __init__(self, config):
|
|
146
|
+
super().__init__()
|
|
147
|
+
self.model = PhiModel(config)
|
|
148
|
+
|
|
149
|
+
def forward(self, input_ids):
|
|
150
|
+
model_output = self.model(input_ids, use_cache=False)
|
|
151
|
+
return model_output.to_tuple()
|
|
152
|
+
|
|
153
|
+
def generate_example_inputs_no_mask(batch: int, seq: int, vocab_size: int):
|
|
154
|
+
(
|
|
155
|
+
input_ids,
|
|
156
|
+
_, # token_type_ids,
|
|
157
|
+
_, # input_mask,
|
|
158
|
+
_, # sequence_labels,
|
|
159
|
+
_, # token_labels,
|
|
160
|
+
_, # choice_labels,
|
|
161
|
+
) = _prepare_config_and_inputs(
|
|
162
|
+
batch_size=batch,
|
|
163
|
+
seq_length=seq,
|
|
164
|
+
vocab_size=vocab_size,
|
|
165
|
+
use_input_mask=True,
|
|
166
|
+
)
|
|
167
|
+
return (input_ids,)
|
|
168
|
+
|
|
169
|
+
example_args_collection = []
|
|
170
|
+
for b, s in input_dims:
|
|
171
|
+
example_args_collection.append(generate_example_inputs_no_mask(b, s, vocab_size))
|
|
172
|
+
|
|
173
|
+
return PhiModelWrapperNoMask(config), example_args_collection, dynamic_shapes
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def get_phi_model_from_config(
|
|
177
|
+
warmup: int = 5,
|
|
178
|
+
repeat: int = 10,
|
|
179
|
+
config: str = "small",
|
|
180
|
+
num_hidden_layers: int = 1,
|
|
181
|
+
implementation: str = "eager",
|
|
182
|
+
dynamic_shapes: bool = False,
|
|
183
|
+
with_mask: bool = True,
|
|
184
|
+
) -> tuple[Any, list[tuple[torch.Tensor, ...]], dict]:
|
|
185
|
+
"""
|
|
186
|
+
Returns a model Phi to test or benchmark.
|
|
187
|
+
|
|
188
|
+
Args:
|
|
189
|
+
warmup: Number of inputs to generate.
|
|
190
|
+
repeat: Number of inputs to generate for repeat.
|
|
191
|
+
config: small, medium or large
|
|
192
|
+
num_hidden_layers: number of hidden layers
|
|
193
|
+
implementation: eager or sdpa
|
|
194
|
+
with_mask: One or two inputs.
|
|
195
|
+
dynamic_shapes: dynamic shapes or not
|
|
196
|
+
|
|
197
|
+
Returns:
|
|
198
|
+
Model and list of inputs.
|
|
199
|
+
"""
|
|
200
|
+
if config == "small":
|
|
201
|
+
conf_dict = dict(
|
|
202
|
+
input_dims=onnxscript.tools.transformers_models.get_input_dims_for_llm(
|
|
203
|
+
dynamic_shapes, warmup, repeat
|
|
204
|
+
),
|
|
205
|
+
hidden_size=32,
|
|
206
|
+
num_hidden_layers=num_hidden_layers,
|
|
207
|
+
vocab_size=99,
|
|
208
|
+
intermediate_size=16,
|
|
209
|
+
max_position_embeddings=512,
|
|
210
|
+
num_attention_heads=4,
|
|
211
|
+
num_key_value_heads=2,
|
|
212
|
+
_attn_implementation=implementation,
|
|
213
|
+
with_mask=with_mask,
|
|
214
|
+
)
|
|
215
|
+
elif config == "medium":
|
|
216
|
+
conf_dict = dict(
|
|
217
|
+
input_dims=onnxscript.tools.transformers_models.get_input_dims_for_llm(
|
|
218
|
+
dynamic_shapes, warmup, repeat
|
|
219
|
+
),
|
|
220
|
+
hidden_size=1024,
|
|
221
|
+
num_hidden_layers=num_hidden_layers,
|
|
222
|
+
vocab_size=1024,
|
|
223
|
+
intermediate_size=1024,
|
|
224
|
+
num_attention_heads=4,
|
|
225
|
+
num_key_value_heads=4,
|
|
226
|
+
max_position_embeddings=1024,
|
|
227
|
+
_attn_implementation=implementation,
|
|
228
|
+
with_mask=with_mask,
|
|
229
|
+
)
|
|
230
|
+
elif config in ("large", "default"):
|
|
231
|
+
conf_dict = dict(
|
|
232
|
+
input_dims=onnxscript.tools.transformers_models.get_input_dims_for_llm(
|
|
233
|
+
dynamic_shapes, warmup, repeat
|
|
234
|
+
),
|
|
235
|
+
hidden_size=2048,
|
|
236
|
+
num_hidden_layers=num_hidden_layers,
|
|
237
|
+
vocab_size=51200,
|
|
238
|
+
intermediate_size=8192,
|
|
239
|
+
num_attention_heads=32,
|
|
240
|
+
num_key_value_heads=None,
|
|
241
|
+
max_position_embeddings=2048,
|
|
242
|
+
_attn_implementation=implementation,
|
|
243
|
+
with_mask=with_mask,
|
|
244
|
+
)
|
|
245
|
+
else:
|
|
246
|
+
raise ValueError(f"Unexpected configuration {config!r}.")
|
|
247
|
+
|
|
248
|
+
return get_phi_model(**conf_dict) # type: ignore[arg-type]
|