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,28 @@
|
|
|
1
|
+
# Copyright (c) Microsoft Corporation.
|
|
2
|
+
# Licensed under the MIT License.
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import onnxscript.ir as ir
|
|
6
|
+
from onnxscript.optimizer import optimize, remove_unused_nodes
|
|
7
|
+
from onnxscript.rewriter.ort_fusions.cos_sin_cache import fuse_cos_sin_cache
|
|
8
|
+
from onnxscript.rewriter.ort_fusions.mha import fuse_mha
|
|
9
|
+
from onnxscript.rewriter.ort_fusions.rms_normalization import fuse_rms_normalization
|
|
10
|
+
from onnxscript.rewriter.ort_fusions.rotary_embedding import fuse_rotary_embedding
|
|
11
|
+
from onnxscript.rewriter.ort_fusions.sdpa import fuse_sdpa
|
|
12
|
+
from onnxscript.rewriter.ort_fusions.skip_normalization import fuse_normalization
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def fuse_xformers(model: ir.Model) -> None:
|
|
16
|
+
optimize(model)
|
|
17
|
+
fuse_rms_normalization(model)
|
|
18
|
+
fuse_normalization(model)
|
|
19
|
+
fuse_rotary_embedding(model)
|
|
20
|
+
fuse_cos_sin_cache(model)
|
|
21
|
+
fuse_sdpa(model)
|
|
22
|
+
fuse_mha(model)
|
|
23
|
+
remove_unused_nodes(model)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def optimize_for_ort(model: ir.Model) -> None:
|
|
27
|
+
# TODO(rama): Include the other optimizations
|
|
28
|
+
fuse_xformers(model)
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# Copyright (c) Microsoft Corporation.
|
|
2
|
+
# Licensed under the MIT License.
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
A one-layer SmolLM model test case, with inputs: input_ids, attention_mask, and position_ids.
|
|
6
|
+
This is an onnxscript version of the model.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import numpy
|
|
10
|
+
from onnx.helper import make_tensor
|
|
11
|
+
|
|
12
|
+
import onnxscript.ir as ir
|
|
13
|
+
from onnxscript import script
|
|
14
|
+
from onnxscript.onnx_opset import opset18
|
|
15
|
+
from onnxscript.onnx_types import FLOAT, INT64
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def make_model(
|
|
19
|
+
input_layernorm_weight_0,
|
|
20
|
+
post_attention_layernorm_weight0,
|
|
21
|
+
norm_weight,
|
|
22
|
+
head_weight,
|
|
23
|
+
self_attn_q_proj_weight0,
|
|
24
|
+
self_attn_k_proj_weight0,
|
|
25
|
+
self_attn_v_proj_weight0,
|
|
26
|
+
self_attn_o_proj_weight0,
|
|
27
|
+
mlp_gate_proj_weight0,
|
|
28
|
+
mlp_up_proj_weight0,
|
|
29
|
+
mlp_down_proj_weight0,
|
|
30
|
+
):
|
|
31
|
+
@script()
|
|
32
|
+
def main_graph(
|
|
33
|
+
input0: INT64[1, 10], input1: FLOAT[1, 10], input2: INT64[1, 10]
|
|
34
|
+
) -> (FLOAT[1, 10, 49152], FLOAT[1, 32, 10, 64], FLOAT[1, 32, 10, 64]):
|
|
35
|
+
model_layers_0_input_layernorm_weight = opset18.Constant(
|
|
36
|
+
value=input_layernorm_weight_0
|
|
37
|
+
)
|
|
38
|
+
model_layers_0_post_attention_layernorm_weight = opset18.Constant(
|
|
39
|
+
value=post_attention_layernorm_weight0
|
|
40
|
+
)
|
|
41
|
+
model_norm_weight = opset18.Constant(value=norm_weight)
|
|
42
|
+
lm_head_weight = opset18.Constant(value=head_weight)
|
|
43
|
+
model_layers_0_self_attn_q_proj_weight = opset18.Constant(
|
|
44
|
+
value=self_attn_q_proj_weight0
|
|
45
|
+
)
|
|
46
|
+
model_layers_0_self_attn_k_proj_weight = opset18.Constant(
|
|
47
|
+
value=self_attn_k_proj_weight0
|
|
48
|
+
)
|
|
49
|
+
model_layers_0_self_attn_v_proj_weight = opset18.Constant(
|
|
50
|
+
value=self_attn_v_proj_weight0
|
|
51
|
+
)
|
|
52
|
+
model_layers_0_self_attn_o_proj_weight = opset18.Constant(
|
|
53
|
+
value=self_attn_o_proj_weight0
|
|
54
|
+
)
|
|
55
|
+
model_layers_0_mlp_gate_proj_weight = opset18.Constant(value=mlp_gate_proj_weight0)
|
|
56
|
+
model_layers_0_mlp_up_proj_weight = opset18.Constant(value=mlp_up_proj_weight0)
|
|
57
|
+
model_layers_0_mlp_down_proj_weight = opset18.Constant(value=mlp_down_proj_weight0)
|
|
58
|
+
|
|
59
|
+
embedding = opset18.Gather(lm_head_weight, input0, axis=0)
|
|
60
|
+
minus_inf_10x10 = opset18.ConstantOfShape([10, 10], [-3.4028234663852886e38])
|
|
61
|
+
mask_10x10 = opset18.Trilu(minus_inf_10x10, 1)
|
|
62
|
+
slice_5 = opset18.Reshape(mask_10x10, [1, 1, 10, 10])
|
|
63
|
+
unsqueeze_2 = opset18.Unsqueeze(input1, 1)
|
|
64
|
+
unsqueeze_3 = opset18.Unsqueeze(unsqueeze_2, 2)
|
|
65
|
+
add = slice_5 + unsqueeze_3
|
|
66
|
+
eq = add == 0.0
|
|
67
|
+
slice_10 = slice_5
|
|
68
|
+
masked_fill = opset18.Where(eq, -3.4028235e38, slice_10)
|
|
69
|
+
val_179 = opset18.Transpose(masked_fill, perm=[2, 1, 0, 3])
|
|
70
|
+
slice_scatter = opset18.Transpose(val_179, perm=[2, 1, 0, 3])
|
|
71
|
+
val_191 = opset18.Transpose(slice_scatter, perm=[1, 0, 2, 3])
|
|
72
|
+
slice_scatter_1 = opset18.Transpose(val_191, perm=[1, 0, 2, 3])
|
|
73
|
+
unsqueeze_6 = opset18.Unsqueeze(input2, 1)
|
|
74
|
+
to_copy_1 = opset18.Cast(unsqueeze_6, to=1)
|
|
75
|
+
view_1 = opset18.Constant(
|
|
76
|
+
value=make_tensor(
|
|
77
|
+
"value",
|
|
78
|
+
1,
|
|
79
|
+
dims=[1, 32, 1],
|
|
80
|
+
vals=[
|
|
81
|
+
1.0,
|
|
82
|
+
0.7498942017555237,
|
|
83
|
+
0.5623413324356079,
|
|
84
|
+
0.4216965138912201,
|
|
85
|
+
0.3162277638912201,
|
|
86
|
+
0.23713736236095428,
|
|
87
|
+
0.17782793939113617,
|
|
88
|
+
0.1333521455526352,
|
|
89
|
+
0.10000000149011612,
|
|
90
|
+
0.07498941570520401,
|
|
91
|
+
0.05623412877321243,
|
|
92
|
+
0.04216964915394783,
|
|
93
|
+
0.03162277862429619,
|
|
94
|
+
0.0237137358635664,
|
|
95
|
+
0.017782794311642647,
|
|
96
|
+
0.01333521492779255,
|
|
97
|
+
0.009999999776482582,
|
|
98
|
+
0.007498942315578461,
|
|
99
|
+
0.005623413249850273,
|
|
100
|
+
0.0042169648222625256,
|
|
101
|
+
0.003162277862429619,
|
|
102
|
+
0.0023713738191872835,
|
|
103
|
+
0.0017782794311642647,
|
|
104
|
+
0.0013335214462131262,
|
|
105
|
+
0.0010000000474974513,
|
|
106
|
+
0.0007498941849917173,
|
|
107
|
+
0.000562341301701963,
|
|
108
|
+
0.00042169648804701865,
|
|
109
|
+
0.0003162277862429619,
|
|
110
|
+
0.0002371373848291114,
|
|
111
|
+
0.00017782794020604342,
|
|
112
|
+
0.0001333521504420787,
|
|
113
|
+
],
|
|
114
|
+
)
|
|
115
|
+
)
|
|
116
|
+
view_2 = opset18.Reshape(to_copy_1, [1, 1, 10], allowzero=0)
|
|
117
|
+
bmm = view_1 @ view_2
|
|
118
|
+
view_3 = opset18.Reshape(bmm, [1, 32, 10], allowzero=0)
|
|
119
|
+
transpose = opset18.Transpose(view_3, perm=[0, 2, 1])
|
|
120
|
+
cat = opset18.Concat(transpose, transpose, axis=-1)
|
|
121
|
+
cos = opset18.Cos(cat)
|
|
122
|
+
sin = opset18.Sin(cat)
|
|
123
|
+
pow_1 = embedding**2.0
|
|
124
|
+
mean = opset18.ReduceMean(pow_1, [-1], keepdims=1, noop_with_empty_axes=0)
|
|
125
|
+
add_1 = mean + 1e-05
|
|
126
|
+
val_244 = opset18.Sqrt(add_1)
|
|
127
|
+
rsqrt = opset18.Reciprocal(val_244)
|
|
128
|
+
mul_3 = embedding * rsqrt
|
|
129
|
+
mul_4 = model_layers_0_input_layernorm_weight * mul_3
|
|
130
|
+
t = opset18.Transpose(model_layers_0_self_attn_q_proj_weight, perm=[1, 0])
|
|
131
|
+
view_5 = mul_4 @ t
|
|
132
|
+
t_1 = opset18.Transpose(model_layers_0_self_attn_k_proj_weight, perm=[1, 0])
|
|
133
|
+
view_7 = mul_4 @ t_1
|
|
134
|
+
t_2 = opset18.Transpose(model_layers_0_self_attn_v_proj_weight, perm=[1, 0])
|
|
135
|
+
view_9 = mul_4 @ t_2
|
|
136
|
+
view_10 = opset18.Reshape(view_5, [1, 10, 32, 64], allowzero=0)
|
|
137
|
+
transpose_1 = opset18.Transpose(view_10, perm=[0, 2, 1, 3])
|
|
138
|
+
view_11 = opset18.Reshape(view_7, [1, 10, 32, 64], allowzero=0)
|
|
139
|
+
transpose_2 = opset18.Transpose(view_11, perm=[0, 2, 1, 3])
|
|
140
|
+
view_12 = opset18.Reshape(view_9, [1, 10, 32, 64], allowzero=0)
|
|
141
|
+
transpose_3 = opset18.Transpose(view_12, perm=[0, 2, 1, 3])
|
|
142
|
+
unsqueeze_7 = opset18.Unsqueeze(cos, 1)
|
|
143
|
+
unsqueeze_8 = opset18.Unsqueeze(sin, 1)
|
|
144
|
+
mul_5 = transpose_1 * unsqueeze_7
|
|
145
|
+
val_267 = opset18.Constant(value_ints=[1])
|
|
146
|
+
slice_19 = opset18.Slice(transpose_1, [0], [32], [3], val_267)
|
|
147
|
+
val_277 = opset18.Constant(value_ints=[1])
|
|
148
|
+
slice_20 = opset18.Slice(transpose_1, [32], [9223372036854775807], [3], val_277)
|
|
149
|
+
neg = opset18.Neg(slice_20)
|
|
150
|
+
cat_1 = opset18.Concat(neg, slice_19, axis=-1)
|
|
151
|
+
mul_6 = cat_1 * unsqueeze_8
|
|
152
|
+
add_2 = mul_5 + mul_6
|
|
153
|
+
mul_7 = transpose_2 * unsqueeze_7
|
|
154
|
+
val_287 = opset18.Constant(value_ints=[1])
|
|
155
|
+
slice_21 = opset18.Slice(transpose_2, [0], [32], [3], val_287)
|
|
156
|
+
val_297 = opset18.Constant(value_ints=[1])
|
|
157
|
+
slice_22 = opset18.Slice(transpose_2, [32], [9223372036854775807], [3], val_297)
|
|
158
|
+
neg_1 = opset18.Neg(slice_22)
|
|
159
|
+
cat_2 = opset18.Concat(neg_1, slice_21, axis=-1)
|
|
160
|
+
mul_8 = cat_2 * unsqueeze_8
|
|
161
|
+
add_3 = mul_7 + mul_8
|
|
162
|
+
val_346 = opset18.Reshape(add_3, [-1, 10, 64], allowzero=0)
|
|
163
|
+
val_347 = opset18.Transpose(val_346, perm=[0, 2, 1])
|
|
164
|
+
val_349 = opset18.Reshape(val_347, [1, 32, 64, 10], allowzero=0)
|
|
165
|
+
val_351 = add_2 * [0.35355338]
|
|
166
|
+
val_353 = val_349 * [0.35355338]
|
|
167
|
+
val_354 = val_351 @ val_353
|
|
168
|
+
val_355 = val_354 + slice_scatter_1
|
|
169
|
+
val_356 = opset18.Softmax(val_355, axis=-1)
|
|
170
|
+
getitem = val_356 @ transpose_3
|
|
171
|
+
transpose_4 = opset18.Transpose(getitem, perm=[0, 2, 1, 3])
|
|
172
|
+
view_13 = opset18.Reshape(transpose_4, [1, 10, -1], allowzero=0)
|
|
173
|
+
t_3 = opset18.Transpose(model_layers_0_self_attn_o_proj_weight, perm=[1, 0])
|
|
174
|
+
view_15 = view_13 @ t_3
|
|
175
|
+
add_4 = embedding + view_15
|
|
176
|
+
pow_2 = add_4**2.0
|
|
177
|
+
mean_1 = opset18.ReduceMean(pow_2, [-1], keepdims=1, noop_with_empty_axes=0)
|
|
178
|
+
add_5 = mean_1 + 1e-05
|
|
179
|
+
val_379 = opset18.Sqrt(add_5)
|
|
180
|
+
rsqrt_1 = opset18.Reciprocal(val_379)
|
|
181
|
+
mul_9 = add_4 * rsqrt_1
|
|
182
|
+
mul_10 = model_layers_0_post_attention_layernorm_weight * mul_9
|
|
183
|
+
t_4 = opset18.Transpose(model_layers_0_mlp_gate_proj_weight, perm=[1, 0])
|
|
184
|
+
view_17 = mul_10 @ t_4
|
|
185
|
+
val_383 = opset18.Sigmoid(view_17)
|
|
186
|
+
silu = view_17 * val_383
|
|
187
|
+
t_5 = opset18.Transpose(model_layers_0_mlp_up_proj_weight, perm=[1, 0])
|
|
188
|
+
view_19 = mul_10 @ t_5
|
|
189
|
+
mul_11 = silu * view_19
|
|
190
|
+
t_6 = opset18.Transpose(model_layers_0_mlp_down_proj_weight, perm=[1, 0])
|
|
191
|
+
view_21 = mul_11 @ t_6
|
|
192
|
+
add_6 = add_4 + view_21
|
|
193
|
+
pow_3 = add_6**2.0
|
|
194
|
+
mean_2 = opset18.ReduceMean(pow_3, [-1], keepdims=1, noop_with_empty_axes=0)
|
|
195
|
+
add_7 = mean_2 + 1e-05
|
|
196
|
+
val_391 = opset18.Sqrt(add_7)
|
|
197
|
+
rsqrt_2 = opset18.Reciprocal(val_391)
|
|
198
|
+
mul_12 = add_6 * rsqrt_2
|
|
199
|
+
mul_13 = model_norm_weight * mul_12
|
|
200
|
+
t_7 = opset18.Transpose(lm_head_weight, perm=[1, 0])
|
|
201
|
+
view_23 = mul_13 @ t_7
|
|
202
|
+
to_copy_12 = opset18.Identity(view_23)
|
|
203
|
+
return to_copy_12, add_3, transpose_3
|
|
204
|
+
|
|
205
|
+
model = main_graph.to_model_proto()
|
|
206
|
+
return model
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def make_model_with_random_weights():
|
|
210
|
+
input_layernorm_weight_0 = numpy.random.rand(2048).astype(numpy.float32)
|
|
211
|
+
post_attention_layernorm_weight0 = numpy.random.rand(2048).astype(numpy.float32)
|
|
212
|
+
norm_weight = numpy.random.rand(2048).astype(numpy.float32)
|
|
213
|
+
head_weight = numpy.random.rand(49152, 2048).astype(numpy.float32)
|
|
214
|
+
self_attn_q_proj_weight0 = numpy.random.rand(2048, 2048).astype(numpy.float32)
|
|
215
|
+
self_attn_k_proj_weight0 = numpy.random.rand(2048, 2048).astype(numpy.float32)
|
|
216
|
+
self_attn_v_proj_weight0 = numpy.random.rand(2048, 2048).astype(numpy.float32)
|
|
217
|
+
self_attn_o_proj_weight0 = numpy.random.rand(2048, 2048).astype(numpy.float32)
|
|
218
|
+
mlp_gate_proj_weight0 = numpy.random.rand(8192, 2048).astype(numpy.float32)
|
|
219
|
+
mlp_up_proj_weight0 = numpy.random.rand(8192, 2048).astype(numpy.float32)
|
|
220
|
+
mlp_down_proj_weight0 = numpy.random.rand(2048, 8192).astype(numpy.float32)
|
|
221
|
+
model = make_model(
|
|
222
|
+
input_layernorm_weight_0,
|
|
223
|
+
post_attention_layernorm_weight0,
|
|
224
|
+
norm_weight,
|
|
225
|
+
head_weight,
|
|
226
|
+
self_attn_q_proj_weight0,
|
|
227
|
+
self_attn_k_proj_weight0,
|
|
228
|
+
self_attn_v_proj_weight0,
|
|
229
|
+
self_attn_o_proj_weight0,
|
|
230
|
+
mlp_gate_proj_weight0,
|
|
231
|
+
mlp_up_proj_weight0,
|
|
232
|
+
mlp_down_proj_weight0,
|
|
233
|
+
)
|
|
234
|
+
return model
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
class TestData:
|
|
238
|
+
def get_onnx_model(self):
|
|
239
|
+
if not hasattr(self, "_onnx_model"):
|
|
240
|
+
model_proto = make_model_with_random_weights()
|
|
241
|
+
model = ir.serde.deserialize_model(model_proto)
|
|
242
|
+
self._onnx_model = model
|
|
243
|
+
return self._onnx_model
|
|
244
|
+
|
|
245
|
+
def get_ort_inputs(self):
|
|
246
|
+
if not hasattr(self, "_ort_inputs"):
|
|
247
|
+
inputs = {
|
|
248
|
+
"input0": numpy.random.randint(0, 49152, (1, 10)).astype(numpy.int64),
|
|
249
|
+
"input1": numpy.ones((1, 10), dtype=numpy.float32),
|
|
250
|
+
"input2": numpy.arange(10, dtype=numpy.int64).reshape(1, 10),
|
|
251
|
+
}
|
|
252
|
+
self._ort_inputs = inputs
|
|
253
|
+
return self._ort_inputs
|