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.
Files changed (176) hide show
  1. onnxscript/__init__.py +131 -0
  2. onnxscript/_framework_apis/__init__.py +3 -0
  3. onnxscript/_framework_apis/torch_2_5.py +117 -0
  4. onnxscript/_framework_apis/torch_2_6.py +45 -0
  5. onnxscript/_internal/__init__.py +0 -0
  6. onnxscript/_internal/analysis.py +229 -0
  7. onnxscript/_internal/ast_utils.py +64 -0
  8. onnxscript/_internal/autocast.py +250 -0
  9. onnxscript/_internal/deprecation.py +78 -0
  10. onnxscript/_internal/param_manipulation.py +148 -0
  11. onnxscript/_internal/runtime_typing.py +43 -0
  12. onnxscript/_internal/utils.py +99 -0
  13. onnxscript/_internal/version_utils.py +118 -0
  14. onnxscript/_legacy_ir/__init__.py +341 -0
  15. onnxscript/_legacy_ir/visitor.py +937 -0
  16. onnxscript/_thirdparty/asciichartpy.py +313 -0
  17. onnxscript/backend/__init__.py +2 -0
  18. onnxscript/backend/onnx_backend.py +303 -0
  19. onnxscript/backend/onnx_export.py +885 -0
  20. onnxscript/converter.py +1470 -0
  21. onnxscript/evaluator.py +619 -0
  22. onnxscript/function_libs/tools/torch_lib/deduce_type_constraints.py +403 -0
  23. onnxscript/function_libs/tools/torch_lib/generate_aten_signatures.py +333 -0
  24. onnxscript/function_libs/tools/torch_lib/generate_prims_signatures.py +331 -0
  25. onnxscript/function_libs/torch_lib/__init__.py +12 -0
  26. onnxscript/function_libs/torch_lib/_constants.py +5 -0
  27. onnxscript/function_libs/torch_lib/_flags.py +58 -0
  28. onnxscript/function_libs/torch_lib/graph_building/__init__.py +56 -0
  29. onnxscript/function_libs/torch_lib/graph_building/_graph_building_ir.py +723 -0
  30. onnxscript/function_libs/torch_lib/graph_building/_graph_building_torch.py +1125 -0
  31. onnxscript/function_libs/torch_lib/ops/__init__.py +27 -0
  32. onnxscript/function_libs/torch_lib/ops/common.py +80 -0
  33. onnxscript/function_libs/torch_lib/ops/core.py +8935 -0
  34. onnxscript/function_libs/torch_lib/ops/fft.py +385 -0
  35. onnxscript/function_libs/torch_lib/ops/linalg.py +399 -0
  36. onnxscript/function_libs/torch_lib/ops/nested.py +25 -0
  37. onnxscript/function_libs/torch_lib/ops/nn.py +2713 -0
  38. onnxscript/function_libs/torch_lib/ops/prims.py +850 -0
  39. onnxscript/function_libs/torch_lib/ops/quantized_decomposed.py +63 -0
  40. onnxscript/function_libs/torch_lib/ops/sparse.py +23 -0
  41. onnxscript/function_libs/torch_lib/ops/special.py +387 -0
  42. onnxscript/function_libs/torch_lib/ops/vision.py +25 -0
  43. onnxscript/function_libs/torch_lib/registration.py +151 -0
  44. onnxscript/function_libs/torch_lib/tensor_typing.py +74 -0
  45. onnxscript/ir/__init__.py +153 -0
  46. onnxscript/ir/_convenience.py +447 -0
  47. onnxscript/ir/_core.py +3119 -0
  48. onnxscript/ir/_display.py +49 -0
  49. onnxscript/ir/_enums.py +163 -0
  50. onnxscript/ir/_graph_comparison.py +23 -0
  51. onnxscript/ir/_io.py +97 -0
  52. onnxscript/ir/_linked_list.py +276 -0
  53. onnxscript/ir/_metadata.py +44 -0
  54. onnxscript/ir/_name_authority.py +72 -0
  55. onnxscript/ir/_polyfill.py +25 -0
  56. onnxscript/ir/_protocols.py +598 -0
  57. onnxscript/ir/_schemas.py +548 -0
  58. onnxscript/ir/_tape.py +120 -0
  59. onnxscript/ir/_type_casting.py +106 -0
  60. onnxscript/ir/convenience.py +32 -0
  61. onnxscript/ir/external_data.py +396 -0
  62. onnxscript/ir/passes/__init__.py +33 -0
  63. onnxscript/ir/passes/_pass_infra.py +172 -0
  64. onnxscript/ir/serde.py +1620 -0
  65. onnxscript/ir/tensor_adapters.py +122 -0
  66. onnxscript/ir/traversal.py +82 -0
  67. onnxscript/irbuilder.py +542 -0
  68. onnxscript/main.py +167 -0
  69. onnxscript/onnx_opset/__init__.py +232 -0
  70. onnxscript/onnx_opset/_impl/opset1.py +4100 -0
  71. onnxscript/onnx_opset/_impl/opset10.py +1227 -0
  72. onnxscript/onnx_opset/_impl/opset11.py +4013 -0
  73. onnxscript/onnx_opset/_impl/opset12.py +1078 -0
  74. onnxscript/onnx_opset/_impl/opset13.py +3924 -0
  75. onnxscript/onnx_opset/_impl/opset14.py +999 -0
  76. onnxscript/onnx_opset/_impl/opset15.py +604 -0
  77. onnxscript/onnx_opset/_impl/opset16.py +1255 -0
  78. onnxscript/onnx_opset/_impl/opset17.py +561 -0
  79. onnxscript/onnx_opset/_impl/opset18.py +1803 -0
  80. onnxscript/onnx_opset/_impl/opset19.py +1942 -0
  81. onnxscript/onnx_opset/_impl/opset2.py +218 -0
  82. onnxscript/onnx_opset/_impl/opset20.py +675 -0
  83. onnxscript/onnx_opset/_impl/opset21.py +1976 -0
  84. onnxscript/onnx_opset/_impl/opset22.py +2588 -0
  85. onnxscript/onnx_opset/_impl/opset3.py +199 -0
  86. onnxscript/onnx_opset/_impl/opset4.py +77 -0
  87. onnxscript/onnx_opset/_impl/opset5.py +84 -0
  88. onnxscript/onnx_opset/_impl/opset6.py +944 -0
  89. onnxscript/onnx_opset/_impl/opset7.py +1243 -0
  90. onnxscript/onnx_opset/_impl/opset8.py +444 -0
  91. onnxscript/onnx_opset/_impl/opset9.py +1485 -0
  92. onnxscript/onnx_opset/_impl/opset_ai_onnx_ml1.py +974 -0
  93. onnxscript/onnx_opset/_impl/opset_ai_onnx_ml2.py +112 -0
  94. onnxscript/onnx_opset/_impl/opset_ai_onnx_ml3.py +308 -0
  95. onnxscript/onnx_opset/_impl/opset_ai_onnx_ml4.py +129 -0
  96. onnxscript/onnx_opset/_impl/opset_ai_onnx_ml5.py +158 -0
  97. onnxscript/onnx_opset/_impl/opset_ai_onnx_preview_training1.py +577 -0
  98. onnxscript/onnx_types.py +229 -0
  99. onnxscript/optimizer/__init__.py +39 -0
  100. onnxscript/optimizer/_constant_folding.py +1083 -0
  101. onnxscript/optimizer/_inliner.py +312 -0
  102. onnxscript/optimizer/_legacy/_optimizer.py +98 -0
  103. onnxscript/optimizer/_legacy/_remove_unused_proto.py +144 -0
  104. onnxscript/optimizer/_legacy/_simple_function_folding.py +243 -0
  105. onnxscript/optimizer/_legacy/constant_folding.py +293 -0
  106. onnxscript/optimizer/_legacy/evaluator.py +439 -0
  107. onnxscript/optimizer/_optimizer.py +61 -0
  108. onnxscript/optimizer/_remove_unused.py +106 -0
  109. onnxscript/optimizer/_remove_unused_function.py +72 -0
  110. onnxscript/py.typed +1 -0
  111. onnxscript/rewriter/__init__.py +56 -0
  112. onnxscript/rewriter/_ir_utils.py +111 -0
  113. onnxscript/rewriter/broadcast_to_matmul.py +180 -0
  114. onnxscript/rewriter/cast_constant_of_shape.py +50 -0
  115. onnxscript/rewriter/collapse_slices.py +141 -0
  116. onnxscript/rewriter/erfgelu.py +27 -0
  117. onnxscript/rewriter/function_rule.py +232 -0
  118. onnxscript/rewriter/gemm_to_matmul_add.py +21 -0
  119. onnxscript/rewriter/generic_pattern.py +700 -0
  120. onnxscript/rewriter/llama_rule_sets.py +287 -0
  121. onnxscript/rewriter/no_op.py +56 -0
  122. onnxscript/rewriter/onnxruntime/__init__.py +50 -0
  123. onnxscript/rewriter/onnxruntime/bfloat16_utils/bfloat16_converter.py +99 -0
  124. onnxscript/rewriter/onnxruntime/fused_matmul_rule_sets.py +177 -0
  125. onnxscript/rewriter/onnxruntime/group_normalization_merge_silu.py +64 -0
  126. onnxscript/rewriter/onnxruntime/instance_to_group_normalization.py +155 -0
  127. onnxscript/rewriter/onnxruntime/softmax.py +63 -0
  128. onnxscript/rewriter/onnxruntime/transformers/__init__.py +21 -0
  129. onnxscript/rewriter/onnxruntime/transformers/biassplitgelu.py +31 -0
  130. onnxscript/rewriter/onnxruntime/transformers/fastgelu.py +29 -0
  131. onnxscript/rewriter/onnxruntime/transformers/layernorm.py +47 -0
  132. onnxscript/rewriter/onnxruntime/transformers/multihead_attention.py +715 -0
  133. onnxscript/rewriter/ort_fusions/__init__.py +9 -0
  134. onnxscript/rewriter/ort_fusions/_core.py +28 -0
  135. onnxscript/rewriter/ort_fusions/_smollm_1.py +253 -0
  136. onnxscript/rewriter/ort_fusions/_smollm_2.py +467 -0
  137. onnxscript/rewriter/ort_fusions/_test_models.py +122 -0
  138. onnxscript/rewriter/ort_fusions/_test_utils.py +42 -0
  139. onnxscript/rewriter/ort_fusions/cos_sin_cache.py +154 -0
  140. onnxscript/rewriter/ort_fusions/gqa.py +156 -0
  141. onnxscript/rewriter/ort_fusions/mha.py +198 -0
  142. onnxscript/rewriter/ort_fusions/rms_normalization.py +95 -0
  143. onnxscript/rewriter/ort_fusions/rotary_embedding.py +64 -0
  144. onnxscript/rewriter/ort_fusions/sdpa.py +75 -0
  145. onnxscript/rewriter/ort_fusions/skip_normalization.py +46 -0
  146. onnxscript/rewriter/pattern.py +1714 -0
  147. onnxscript/rewriter/testing.py +77 -0
  148. onnxscript/sourceinfo.py +59 -0
  149. onnxscript/tensor.py +227 -0
  150. onnxscript/testing/__init__.py +482 -0
  151. onnxscript/tools/__init__.py +4 -0
  152. onnxscript/tools/benchmark/__init__.py +23 -0
  153. onnxscript/tools/benchmark/benchmark_helpers.py +783 -0
  154. onnxscript/tools/benchmark/benchmark_run.py +140 -0
  155. onnxscript/tools/benchmark/export_model.py +207 -0
  156. onnxscript/tools/benchmark/export_model_batch.py +146 -0
  157. onnxscript/tools/memory_peak.py +244 -0
  158. onnxscript/tools/training_helper.py +50 -0
  159. onnxscript/tools/transformers_models/__init__.py +190 -0
  160. onnxscript/tools/transformers_models/llama.py +168 -0
  161. onnxscript/tools/transformers_models/mistral.py +238 -0
  162. onnxscript/tools/transformers_models/phi.py +248 -0
  163. onnxscript/tools/transformers_models/phi3.py +259 -0
  164. onnxscript/type_annotation.py +281 -0
  165. onnxscript/utils/__init__.py +0 -0
  166. onnxscript/utils/evaluation_utils.py +56 -0
  167. onnxscript/utils/timing_utils.py +33 -0
  168. onnxscript/utils/utils.py +84 -0
  169. onnxscript/values.py +790 -0
  170. onnxscript/version_converter/__init__.py +21 -0
  171. onnxscript/version_converter/_version_converter.py +314 -0
  172. onnxscript-0.1.0.dist-info/LICENSE +21 -0
  173. onnxscript-0.1.0.dist-info/METADATA +370 -0
  174. onnxscript-0.1.0.dist-info/RECORD +176 -0
  175. onnxscript-0.1.0.dist-info/WHEEL +5 -0
  176. onnxscript-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1976 @@
1
+ # --------------------------------------------------------------------------
2
+ # ⚠️ WARNING - AUTO-GENERATED CODE - DO NOT EDIT ⚠️
3
+ # ⚙️ Generated by 'python -m opgen'
4
+ # --------------------------------------------------------------------------
5
+ # Copyright (c) Microsoft Corporation. All rights reserved.
6
+ # Licensed under the MIT License.
7
+ # --------------------------------------------------------------------------
8
+ # pylint: disable=W0221,W0222,R0901,W0237
9
+ # mypy: disable-error-code=override
10
+ # ruff: noqa: N801,E741
11
+ # ruff: noqa: D214,D402,D405,D411,D412,D416,D417
12
+ # --------------------------------------------------------------------------
13
+
14
+ from __future__ import annotations
15
+
16
+ from typing import Optional, Sequence, TypeVar, Union
17
+
18
+ from onnx import GraphProto, SparseTensorProto, TensorProto
19
+ from onnx.defs import get_schema
20
+ from typing_extensions import TypeAlias
21
+
22
+ from onnxscript.onnx_opset._impl.opset20 import Opset20
23
+ from onnxscript.onnx_types import (
24
+ BFLOAT16,
25
+ BOOL,
26
+ COMPLEX64,
27
+ COMPLEX128,
28
+ DOUBLE,
29
+ FLOAT,
30
+ FLOAT8E4M3FN,
31
+ FLOAT8E4M3FNUZ,
32
+ FLOAT8E5M2,
33
+ FLOAT8E5M2FNUZ,
34
+ FLOAT16,
35
+ INT4,
36
+ INT8,
37
+ INT16,
38
+ INT32,
39
+ INT64,
40
+ STRING,
41
+ UINT4,
42
+ UINT8,
43
+ UINT16,
44
+ UINT32,
45
+ UINT64,
46
+ )
47
+ from onnxscript.values import Op, Opset
48
+
49
+
50
+ class Opset21(Opset20):
51
+ def __new__(cls):
52
+ return Opset.__new__(cls, "", 21)
53
+
54
+ T1_Cast = TypeVar(
55
+ "T1_Cast",
56
+ BFLOAT16,
57
+ BOOL,
58
+ DOUBLE,
59
+ FLOAT,
60
+ FLOAT16,
61
+ FLOAT8E4M3FN,
62
+ FLOAT8E4M3FNUZ,
63
+ FLOAT8E5M2,
64
+ FLOAT8E5M2FNUZ,
65
+ INT16,
66
+ INT32,
67
+ INT4,
68
+ INT64,
69
+ INT8,
70
+ STRING,
71
+ UINT16,
72
+ UINT32,
73
+ UINT4,
74
+ UINT64,
75
+ UINT8,
76
+ )
77
+
78
+ T2_Cast: TypeAlias = Union[
79
+ BFLOAT16,
80
+ BOOL,
81
+ DOUBLE,
82
+ FLOAT,
83
+ FLOAT16,
84
+ FLOAT8E4M3FN,
85
+ FLOAT8E4M3FNUZ,
86
+ FLOAT8E5M2,
87
+ FLOAT8E5M2FNUZ,
88
+ INT16,
89
+ INT32,
90
+ INT4,
91
+ INT64,
92
+ INT8,
93
+ STRING,
94
+ UINT16,
95
+ UINT32,
96
+ UINT4,
97
+ UINT64,
98
+ UINT8,
99
+ ]
100
+
101
+ def Cast(self, input: T1_Cast, *, saturate: int = 1, to: int) -> T2_Cast:
102
+ r"""[🌐 Cast(21)](https://onnx.ai/onnx/operators/onnx__Cast.html#cast-21 "Online Documentation")
103
+
104
+
105
+ The operator casts the elements of a given input tensor to a data type
106
+ specified by the 'to' argument and returns an output tensor of the same size in
107
+ the converted type. The 'to' argument must be one of the data types specified
108
+ in the 'DataType' enum field in the TensorProto message.
109
+
110
+ Casting from string tensor in plain (e.g., "3.14" and "1000") and scientific numeric representations
111
+ (e.g., "1e-5" and "1E8") to float types is supported. For example, converting string "100.5" to an integer may
112
+ yield result 100. There are some string literals reserved for special floating-point values;
113
+ "+INF" (and "INF"), "-INF", and "NaN" are positive infinity, negative infinity, and not-a-number, respectively.
114
+ Any string which can exactly match "+INF" in a case-insensitive way would be mapped to positive infinite. Similarly,
115
+ this case-insensitive rule is applied to "INF" and "NaN". When casting from numeric tensors
116
+ to string tensors, plain floating-point representation (such as "314.15926") would be used.
117
+ Converting non-numerical-literal string such as "Hello World!" is an undefined behavior. Cases
118
+ of converting string representing floating-point arithmetic value, such as "2.718", to INT is an undefined behavior.
119
+
120
+ Conversion from a numerical type to any numerical type is always allowed.
121
+ User must be aware of precision loss and value change caused by range difference between two types.
122
+ For example, a 64-bit float 3.1415926459 may be round to a 32-bit float 3.141592. Similarly, converting
123
+ an integer 36 to Boolean may produce 1 because we truncate bits which can't be stored in the targeted type.
124
+
125
+ In more detail, the conversion among numerical types should follow these rules
126
+ if the destination type is not a float 8 type.
127
+
128
+ * Casting from floating point to:
129
+ * floating point: +/- infinity if OOR (out of range).
130
+ * fixed point: undefined if OOR.
131
+ * bool: +/- 0.0 to False; all else to True.
132
+ * Casting from fixed point to:
133
+ * floating point: +/- infinity if OOR. (+ infinity in the case of uint)
134
+ * fixed point: when OOR, discard higher bits and reinterpret (with respect to two's complement representation for
135
+ signed types). For example, 200 (int16) -> -56 (int8).
136
+ * bool: zero to False; nonzero to True.
137
+ * Casting from bool to:
138
+ * floating point: `{1.0, 0.0}`.
139
+ * fixed point: `{1, 0}`.
140
+ * bool: no change.
141
+
142
+ Float 8 type were introduced to speed up the training of
143
+ deep models. By default the conversion of a float *x* obeys
144
+ to the following rules. `[x]` means the value rounded to
145
+ the target mantissa width.
146
+
147
+ | x | E4M3FN | E4M3FNUZ | E5M2 | E5M2FNUZ |
148
+ |------|----|----|----|----|
149
+ | 0 | 0 | 0 | 0 | 0 |
150
+ |-0 | -0 | 0 | -0 | 0 |
151
+ | NaN | NaN | NaN | NaN | NaN |
152
+ | +/- Inf | +/- FLT_MAX | NaN | FLT_MAX | NaN |
153
+ | [x] > FLT_MAX | FLT_MAX | FLT_MAX | FLT_MAX | FLT_MAX |
154
+ | [x] < -FLT_MAX | -FLT_MAX | -FLT_MAX | -FLT_MAX | -FLT_MAX |
155
+ | else | RNE | RNE | RNE | RNE |
156
+
157
+ The behavior changes if the parameter 'saturate' is set to False.
158
+ The rules then become:
159
+
160
+ | x | E4M3FN | E4M3FNUZ | E5M2 | E5M2FNUZ |
161
+ |------|----|----|----|----|
162
+ | 0 | 0 | 0 | 0 | 0 |
163
+ |-0 | -0 | 0 | -0 | 0 |
164
+ | NaN | NaN | NaN | NaN | NaN |
165
+ | +/- Inf | NaN | NaN | +/- Inf | NaN |
166
+ | [x] > FLT_MAX | NaN | NaN | Inf | NaN |
167
+ | [x] < -FLT_MAX | NaN | NaN | -Inf | NaN |
168
+ | else | RNE | RNE | RNE | RNE |
169
+
170
+
171
+ Args:
172
+ input: (differentiable) Input tensor to be cast.
173
+
174
+ saturate: The parameter defines how the conversion behaves if an input value
175
+ is out of range of the destination type. It only applies for float 8
176
+ conversion (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz).
177
+ It is true by default. All cases are fully described in two tables
178
+ inserted in the operator description.
179
+
180
+ to: The data type to which the elements of the input tensor are cast.
181
+ Strictly must be one of the types from DataType enum in TensorProto
182
+ """
183
+
184
+ schema = get_schema("Cast", 21, "")
185
+ op = Op(self, "Cast", schema)
186
+ return op(*self._prepare_inputs(schema, input), saturate=saturate, to=to)
187
+
188
+ T1_CastLike = TypeVar(
189
+ "T1_CastLike",
190
+ BFLOAT16,
191
+ BOOL,
192
+ DOUBLE,
193
+ FLOAT,
194
+ FLOAT16,
195
+ FLOAT8E4M3FN,
196
+ FLOAT8E4M3FNUZ,
197
+ FLOAT8E5M2,
198
+ FLOAT8E5M2FNUZ,
199
+ INT16,
200
+ INT32,
201
+ INT4,
202
+ INT64,
203
+ INT8,
204
+ STRING,
205
+ UINT16,
206
+ UINT32,
207
+ UINT4,
208
+ UINT64,
209
+ UINT8,
210
+ )
211
+
212
+ T2_CastLike = TypeVar(
213
+ "T2_CastLike",
214
+ BFLOAT16,
215
+ BOOL,
216
+ DOUBLE,
217
+ FLOAT,
218
+ FLOAT16,
219
+ FLOAT8E4M3FN,
220
+ FLOAT8E4M3FNUZ,
221
+ FLOAT8E5M2,
222
+ FLOAT8E5M2FNUZ,
223
+ INT16,
224
+ INT32,
225
+ INT4,
226
+ INT64,
227
+ INT8,
228
+ STRING,
229
+ UINT16,
230
+ UINT32,
231
+ UINT4,
232
+ UINT64,
233
+ UINT8,
234
+ )
235
+
236
+ def CastLike(
237
+ self, input: T1_CastLike, target_type: T2_CastLike, *, saturate: int = 1
238
+ ) -> T2_CastLike:
239
+ r"""[🌐 CastLike(21)](https://onnx.ai/onnx/operators/onnx__CastLike.html#castlike-21 "Online Documentation")
240
+
241
+
242
+ The operator casts the elements of a given input tensor (the first input) to
243
+ the same data type as the elements of the second input tensor.
244
+ See documentation of the Cast operator for further details.
245
+
246
+
247
+ Args:
248
+ input: (differentiable) Input tensor to be cast.
249
+
250
+ target_type: (non-differentiable) The (first) input tensor will be cast to
251
+ produce a tensor of the same type as this (second input) tensor.
252
+
253
+ saturate: The parameter defines how the conversion behaves if an input value
254
+ is out of range of the destination type. It only applies for float 8
255
+ conversion (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz).
256
+ It is true by default. Please refer to operator Cast description for
257
+ further details.
258
+ """
259
+
260
+ schema = get_schema("CastLike", 21, "")
261
+ op = Op(self, "CastLike", schema)
262
+ return op(*self._prepare_inputs(schema, input, target_type), saturate=saturate)
263
+
264
+ T_Constant: TypeAlias = Union[
265
+ BFLOAT16,
266
+ BOOL,
267
+ COMPLEX128,
268
+ COMPLEX64,
269
+ DOUBLE,
270
+ FLOAT,
271
+ FLOAT16,
272
+ FLOAT8E4M3FN,
273
+ FLOAT8E4M3FNUZ,
274
+ FLOAT8E5M2,
275
+ FLOAT8E5M2FNUZ,
276
+ INT16,
277
+ INT32,
278
+ INT4,
279
+ INT64,
280
+ INT8,
281
+ STRING,
282
+ UINT16,
283
+ UINT32,
284
+ UINT4,
285
+ UINT64,
286
+ UINT8,
287
+ ]
288
+
289
+ def Constant(
290
+ self,
291
+ *,
292
+ sparse_value: Optional[SparseTensorProto] = None,
293
+ value: Optional[TensorProto] = None,
294
+ value_float: Optional[float] = None,
295
+ value_floats: Optional[Sequence[float]] = None,
296
+ value_int: Optional[int] = None,
297
+ value_ints: Optional[Sequence[int]] = None,
298
+ value_string: Optional[str] = None,
299
+ value_strings: Optional[Sequence[str]] = None,
300
+ ) -> T_Constant:
301
+ r"""[🌐 Constant(21)](https://onnx.ai/onnx/operators/onnx__Constant.html#constant-21 "Online Documentation")
302
+
303
+
304
+ This operator produces a constant tensor. Exactly one of the provided attributes, either value, sparse_value,
305
+ or value_* must be specified.
306
+
307
+
308
+ Args:
309
+ sparse_value: The value for the elements of the output tensor in sparse
310
+ format.
311
+
312
+ value: The value for the elements of the output tensor.
313
+
314
+ value_float: The value for the sole element for the scalar, float32, output
315
+ tensor.
316
+
317
+ value_floats: The values for the elements for the 1D, float32, output
318
+ tensor.
319
+
320
+ value_int: The value for the sole element for the scalar, int64, output
321
+ tensor.
322
+
323
+ value_ints: The values for the elements for the 1D, int64, output tensor.
324
+
325
+ value_string: The value for the sole element for the scalar, UTF-8 string,
326
+ output tensor.
327
+
328
+ value_strings: The values for the elements for the 1D, UTF-8 string, output
329
+ tensor.
330
+ """
331
+
332
+ schema = get_schema("Constant", 21, "")
333
+ op = Op(self, "Constant", schema)
334
+ return op(
335
+ sparse_value=sparse_value,
336
+ value=value,
337
+ value_float=value_float,
338
+ value_floats=value_floats,
339
+ value_int=value_int,
340
+ value_ints=value_ints,
341
+ value_string=value_string,
342
+ value_strings=value_strings,
343
+ )
344
+
345
+ T1_ConstantOfShape: TypeAlias = INT64
346
+
347
+ T2_ConstantOfShape: TypeAlias = Union[
348
+ BFLOAT16,
349
+ BOOL,
350
+ DOUBLE,
351
+ FLOAT,
352
+ FLOAT16,
353
+ FLOAT8E4M3FN,
354
+ FLOAT8E4M3FNUZ,
355
+ FLOAT8E5M2,
356
+ FLOAT8E5M2FNUZ,
357
+ INT16,
358
+ INT32,
359
+ INT4,
360
+ INT64,
361
+ INT8,
362
+ UINT16,
363
+ UINT32,
364
+ UINT4,
365
+ UINT64,
366
+ UINT8,
367
+ ]
368
+
369
+ def ConstantOfShape(
370
+ self, input: T1_ConstantOfShape, *, value: Optional[TensorProto] = None
371
+ ) -> T2_ConstantOfShape:
372
+ r"""[🌐 ConstantOfShape(21)](https://onnx.ai/onnx/operators/onnx__ConstantOfShape.html#constantofshape-21 "Online Documentation")
373
+
374
+
375
+ Generate a tensor with given value and shape.
376
+
377
+
378
+ Args:
379
+ input: 1D tensor. The shape of the expected output tensor. If empty tensor
380
+ is given, the output would be a scalar. All values must be >= 0.
381
+
382
+ value: (Optional) The value of the output elements.Should be a one-element
383
+ tensor. If not specified, it defaults to a tensor of value 0 and
384
+ datatype float32
385
+ """
386
+
387
+ schema = get_schema("ConstantOfShape", 21, "")
388
+ op = Op(self, "ConstantOfShape", schema)
389
+ return op(*self._prepare_inputs(schema, input), value=value)
390
+
391
+ T1_DequantizeLinear = TypeVar(
392
+ "T1_DequantizeLinear",
393
+ FLOAT8E4M3FN,
394
+ FLOAT8E4M3FNUZ,
395
+ FLOAT8E5M2,
396
+ FLOAT8E5M2FNUZ,
397
+ INT16,
398
+ INT32,
399
+ INT4,
400
+ INT8,
401
+ UINT16,
402
+ UINT4,
403
+ UINT8,
404
+ )
405
+
406
+ T2_DequantizeLinear = TypeVar("T2_DequantizeLinear", BFLOAT16, FLOAT, FLOAT16)
407
+
408
+ def DequantizeLinear(
409
+ self,
410
+ x: T1_DequantizeLinear,
411
+ x_scale: T2_DequantizeLinear,
412
+ x_zero_point: Optional[T1_DequantizeLinear] = None,
413
+ *,
414
+ axis: int = 1,
415
+ block_size: int = 0,
416
+ ) -> T2_DequantizeLinear:
417
+ r"""[🌐 DequantizeLinear(21)](https://onnx.ai/onnx/operators/onnx__DequantizeLinear.html#dequantizelinear-21 "Online Documentation")
418
+
419
+
420
+ The linear dequantization operator. It consumes a quantized tensor, a scale, and a zero point to compute the
421
+ full-precision tensor. The dequantization formula is `y = (x - x_zero_point) * x_scale`. `x_scale` and `x_zero_point`
422
+ must have the same shape, determining the quantization's granularity: a scalar for per-tensor/per-layer quantization,
423
+ a 1-D tensor for per-axis quantization, or have a rank identical to the input for blocked quantization.
424
+ See QuantizeLinear for details on quantization granularity.
425
+
426
+ `x_zero_point` and `x` must have the same type. `x` and `y` must have the same shape. In the case of dequantizing
427
+ `int32`, there's no zero point (zero point is supposed to be 0).
428
+ `zero-point` is usually not used in the case of float8 types quantization, but the dequantization formula remains the same
429
+ for consistency, and `x_scale` still determines the output type.
430
+
431
+
432
+ Args:
433
+ x: N-D quantized input tensor to be de-quantized.
434
+
435
+ x_scale: Scale for input `x`. For per-tensor/layer dequantization the scale
436
+ is a scalar, for per per-axis dequantization it is a 1-D Tensor and for
437
+ blocked dequantization it has the same shape as the input, except for
438
+ one dimension in which blocking is performed.
439
+
440
+ x_zero_point: (optional) Zero point for input `x`. Shape must match x_scale.
441
+ It's optional. Zero point is 0 when it's not specified.
442
+
443
+ axis: (Optional) The axis of the dequantizing dimension of the input tensor.
444
+ Used for per-axis and blocked quantization. Negative value means
445
+ counting dimensions from the back. Accepted range is `[-r, r-1]` where
446
+ `r = rank(input)`.
447
+
448
+ block_size: (Optional) The size of the quantization block (number of times
449
+ every scale is replicated). Used only for blocked quantization. The
450
+ block size is a positive integer. Given `x` shape `(D0, ..., Di, ...,
451
+ Dn)`, `y_scale` shape `(S0, ... Si, ...Sn)` and `axis=i`, the accepted
452
+ range is `[ceil(Di/Si), ceil(Di/(Si-1))-1]`
453
+ """
454
+
455
+ schema = get_schema("DequantizeLinear", 21, "")
456
+ op = Op(self, "DequantizeLinear", schema)
457
+ return op(
458
+ *self._prepare_inputs(schema, x, x_scale, x_zero_point),
459
+ axis=axis,
460
+ block_size=block_size,
461
+ )
462
+
463
+ T_Flatten = TypeVar(
464
+ "T_Flatten",
465
+ BFLOAT16,
466
+ BOOL,
467
+ COMPLEX128,
468
+ COMPLEX64,
469
+ DOUBLE,
470
+ FLOAT,
471
+ FLOAT16,
472
+ FLOAT8E4M3FN,
473
+ FLOAT8E4M3FNUZ,
474
+ FLOAT8E5M2,
475
+ FLOAT8E5M2FNUZ,
476
+ INT16,
477
+ INT32,
478
+ INT4,
479
+ INT64,
480
+ INT8,
481
+ STRING,
482
+ UINT16,
483
+ UINT32,
484
+ UINT4,
485
+ UINT64,
486
+ UINT8,
487
+ )
488
+
489
+ def Flatten(self, input: T_Flatten, *, axis: int = 1) -> T_Flatten:
490
+ r"""[🌐 Flatten(21)](https://onnx.ai/onnx/operators/onnx__Flatten.html#flatten-21 "Online Documentation")
491
+
492
+
493
+ Flattens the input tensor into a 2D matrix. If input tensor has shape
494
+ (d_0, d_1, ... d_n) then the output will have shape
495
+ (d_0 X d_1 ... d_(axis-1), d_axis X d_(axis+1) ... X dn).
496
+
497
+
498
+ Args:
499
+ input: (differentiable) A tensor of rank >= axis.
500
+
501
+ axis: Indicate up to which input dimensions (exclusive) should be flattened
502
+ to the outer dimension of the output. The value for axis must be in the
503
+ range [-r, r], where r is the rank of the input tensor. Negative value
504
+ means counting dimensions from the back. When axis = 0, the shape of the
505
+ output tensor is (1, (d_0 X d_1 ... d_n), where the shape of the input
506
+ tensor is (d_0, d_1, ... d_n).
507
+ """
508
+
509
+ schema = get_schema("Flatten", 21, "")
510
+ op = Op(self, "Flatten", schema)
511
+ return op(*self._prepare_inputs(schema, input), axis=axis)
512
+
513
+ T_GroupNormalization = TypeVar("T_GroupNormalization", BFLOAT16, DOUBLE, FLOAT, FLOAT16)
514
+
515
+ def GroupNormalization(
516
+ self,
517
+ X: T_GroupNormalization,
518
+ scale: T_GroupNormalization,
519
+ bias: T_GroupNormalization,
520
+ *,
521
+ epsilon: float = 9.999999747378752e-06,
522
+ num_groups: int,
523
+ stash_type: int = 1,
524
+ ) -> T_GroupNormalization:
525
+ r"""[🌐 GroupNormalization(21)](https://onnx.ai/onnx/operators/onnx__GroupNormalization.html#groupnormalization-21 "Online Documentation")
526
+
527
+
528
+ A GroupNormalization function. Carries out group normalization as described in
529
+ the paper https://arxiv.org/abs/1803.08494
530
+
531
+ This operator transforms input according to
532
+ ::
533
+
534
+ y = scale * (x - mean) / sqrt(variance + epsilon) + bias,
535
+
536
+
537
+ where the mean and variance are computed per instance per group of channels, and
538
+ `scale` and `bias` should be specified for each group of channels. The number of
539
+ groups `num_groups` should be divisible by the number of channels so that there are
540
+ an equal number of channels per group.
541
+
542
+ The overall computation has two stages: the first stage normalizes the elements to
543
+ have zero mean and unit variance for each instance in each group, and the second
544
+ stage scales and shifts the results of the first stage. The floating-point precision
545
+ used in the first stage is determined by the `stash_type` attribute. For example,
546
+ if `stash_type` is 1, the operator casts all input variables to 32-bit float,
547
+ performs the computation, and finally casts the normalized results back to the
548
+ original type of `X`. The second stage does not depend on `stash_type`.
549
+
550
+ When the number of groups is the same as the number of channels, this operator is
551
+ equivalent to InstanceNormalization. When there is only one group, this operator
552
+ is equivalent to LayerNormalization.
553
+
554
+
555
+ Args:
556
+ X: (differentiable) Input data tensor. Dimensions for image cases are `(N x
557
+ C x H x W)`, where `N` is the batch size, `C` is the number of channels,
558
+ and `H` and `W` are the height and width of the data. Statistics are
559
+ computed for every group of channels over `C`, `H`, and `W`. For
560
+ non-image cases, the dimensions are in the form of `(N x C x D1 x D2 ...
561
+ Dn)`.
562
+
563
+ scale: (differentiable) Scale tensor of shape `(C)`.
564
+
565
+ bias: (differentiable) Bias tensor of shape `(C)`.
566
+
567
+ epsilon: The epsilon value to use to avoid division by zero.
568
+
569
+ num_groups: The number of groups of channels. It should be a divisor of the
570
+ number of channels `C`.
571
+
572
+ stash_type: The floating-point precision used in stage one of the
573
+ computation.
574
+ """
575
+
576
+ schema = get_schema("GroupNormalization", 21, "")
577
+ op = Op(self, "GroupNormalization", schema)
578
+ return op(
579
+ *self._prepare_inputs(schema, X, scale, bias),
580
+ epsilon=epsilon,
581
+ num_groups=num_groups,
582
+ stash_type=stash_type,
583
+ )
584
+
585
+ V_Identity = TypeVar(
586
+ "V_Identity",
587
+ Optional[Sequence[BOOL]],
588
+ Optional[Sequence[COMPLEX128]],
589
+ Optional[Sequence[COMPLEX64]],
590
+ Optional[Sequence[DOUBLE]],
591
+ Optional[Sequence[FLOAT]],
592
+ Optional[Sequence[FLOAT16]],
593
+ Optional[Sequence[INT16]],
594
+ Optional[Sequence[INT32]],
595
+ Optional[Sequence[INT64]],
596
+ Optional[Sequence[INT8]],
597
+ Optional[Sequence[STRING]],
598
+ Optional[Sequence[UINT16]],
599
+ Optional[Sequence[UINT32]],
600
+ Optional[Sequence[UINT64]],
601
+ Optional[Sequence[UINT8]],
602
+ Optional[BOOL],
603
+ Optional[COMPLEX128],
604
+ Optional[COMPLEX64],
605
+ Optional[DOUBLE],
606
+ Optional[FLOAT],
607
+ Optional[FLOAT16],
608
+ Optional[INT16],
609
+ Optional[INT32],
610
+ Optional[INT64],
611
+ Optional[INT8],
612
+ Optional[STRING],
613
+ Optional[UINT16],
614
+ Optional[UINT32],
615
+ Optional[UINT64],
616
+ Optional[UINT8],
617
+ Sequence[BOOL],
618
+ Sequence[COMPLEX128],
619
+ Sequence[COMPLEX64],
620
+ Sequence[DOUBLE],
621
+ Sequence[FLOAT],
622
+ Sequence[FLOAT16],
623
+ Sequence[INT16],
624
+ Sequence[INT32],
625
+ Sequence[INT64],
626
+ Sequence[INT8],
627
+ Sequence[STRING],
628
+ Sequence[UINT16],
629
+ Sequence[UINT32],
630
+ Sequence[UINT64],
631
+ Sequence[UINT8],
632
+ BFLOAT16,
633
+ BOOL,
634
+ COMPLEX128,
635
+ COMPLEX64,
636
+ DOUBLE,
637
+ FLOAT,
638
+ FLOAT16,
639
+ FLOAT8E4M3FN,
640
+ FLOAT8E4M3FNUZ,
641
+ FLOAT8E5M2,
642
+ FLOAT8E5M2FNUZ,
643
+ INT16,
644
+ INT32,
645
+ INT4,
646
+ INT64,
647
+ INT8,
648
+ STRING,
649
+ UINT16,
650
+ UINT32,
651
+ UINT4,
652
+ UINT64,
653
+ UINT8,
654
+ )
655
+
656
+ def Identity(self, input: V_Identity) -> V_Identity:
657
+ r"""[🌐 Identity(21)](https://onnx.ai/onnx/operators/onnx__Identity.html#identity-21 "Online Documentation")
658
+
659
+ Identity operator
660
+
661
+ Args:
662
+ input: (differentiable) Input tensor
663
+ """
664
+
665
+ schema = get_schema("Identity", 21, "")
666
+ op = Op(self, "Identity", schema)
667
+ return op(*self._prepare_inputs(schema, input))
668
+
669
+ B_If: TypeAlias = BOOL
670
+
671
+ V_If: TypeAlias = Union[
672
+ Optional[Sequence[BFLOAT16]],
673
+ Optional[Sequence[BOOL]],
674
+ Optional[Sequence[COMPLEX128]],
675
+ Optional[Sequence[COMPLEX64]],
676
+ Optional[Sequence[DOUBLE]],
677
+ Optional[Sequence[FLOAT]],
678
+ Optional[Sequence[FLOAT16]],
679
+ Optional[Sequence[INT16]],
680
+ Optional[Sequence[INT32]],
681
+ Optional[Sequence[INT64]],
682
+ Optional[Sequence[INT8]],
683
+ Optional[Sequence[STRING]],
684
+ Optional[Sequence[UINT16]],
685
+ Optional[Sequence[UINT32]],
686
+ Optional[Sequence[UINT64]],
687
+ Optional[Sequence[UINT8]],
688
+ Optional[BFLOAT16],
689
+ Optional[BOOL],
690
+ Optional[COMPLEX128],
691
+ Optional[COMPLEX64],
692
+ Optional[DOUBLE],
693
+ Optional[FLOAT],
694
+ Optional[FLOAT16],
695
+ Optional[FLOAT8E4M3FN],
696
+ Optional[FLOAT8E4M3FNUZ],
697
+ Optional[FLOAT8E5M2],
698
+ Optional[FLOAT8E5M2FNUZ],
699
+ Optional[INT16],
700
+ Optional[INT32],
701
+ Optional[INT4],
702
+ Optional[INT64],
703
+ Optional[INT8],
704
+ Optional[STRING],
705
+ Optional[UINT16],
706
+ Optional[UINT32],
707
+ Optional[UINT4],
708
+ Optional[UINT64],
709
+ Optional[UINT8],
710
+ Sequence[BFLOAT16],
711
+ Sequence[BOOL],
712
+ Sequence[COMPLEX128],
713
+ Sequence[COMPLEX64],
714
+ Sequence[DOUBLE],
715
+ Sequence[FLOAT],
716
+ Sequence[FLOAT16],
717
+ Sequence[FLOAT8E4M3FN],
718
+ Sequence[FLOAT8E4M3FNUZ],
719
+ Sequence[FLOAT8E5M2],
720
+ Sequence[FLOAT8E5M2FNUZ],
721
+ Sequence[INT16],
722
+ Sequence[INT32],
723
+ Sequence[INT4],
724
+ Sequence[INT64],
725
+ Sequence[INT8],
726
+ Sequence[STRING],
727
+ Sequence[UINT16],
728
+ Sequence[UINT32],
729
+ Sequence[UINT4],
730
+ Sequence[UINT64],
731
+ Sequence[UINT8],
732
+ BFLOAT16,
733
+ BOOL,
734
+ COMPLEX128,
735
+ COMPLEX64,
736
+ DOUBLE,
737
+ FLOAT,
738
+ FLOAT16,
739
+ FLOAT8E4M3FN,
740
+ FLOAT8E4M3FNUZ,
741
+ FLOAT8E5M2,
742
+ FLOAT8E5M2FNUZ,
743
+ INT16,
744
+ INT32,
745
+ INT4,
746
+ INT64,
747
+ INT8,
748
+ STRING,
749
+ UINT16,
750
+ UINT32,
751
+ UINT4,
752
+ UINT64,
753
+ UINT8,
754
+ ]
755
+
756
+ def If(self, cond: B_If, *, else_branch: GraphProto, then_branch: GraphProto) -> V_If:
757
+ r"""[🌐 If(21)](https://onnx.ai/onnx/operators/onnx__If.html#if-21 "Online Documentation")
758
+
759
+ If conditional
760
+
761
+ Args:
762
+ cond: Condition for the if. The tensor must contain a single element.
763
+
764
+ else_branch: Graph to run if condition is false. Has N outputs: values you
765
+ wish to be live-out to the enclosing scope. The number of outputs must
766
+ match the number of outputs in the then_branch.
767
+
768
+ then_branch: Graph to run if condition is true. Has N outputs: values you
769
+ wish to be live-out to the enclosing scope. The number of outputs must
770
+ match the number of outputs in the else_branch.
771
+ """
772
+
773
+ schema = get_schema("If", 21, "")
774
+ op = Op(self, "If", schema)
775
+ return op(
776
+ *self._prepare_inputs(schema, cond),
777
+ else_branch=else_branch,
778
+ then_branch=then_branch,
779
+ )
780
+
781
+ I_Loop: TypeAlias = INT64
782
+
783
+ B_Loop: TypeAlias = BOOL
784
+
785
+ V_Loop = TypeVar(
786
+ "V_Loop",
787
+ Optional[Sequence[BFLOAT16]],
788
+ Optional[Sequence[BOOL]],
789
+ Optional[Sequence[COMPLEX128]],
790
+ Optional[Sequence[COMPLEX64]],
791
+ Optional[Sequence[DOUBLE]],
792
+ Optional[Sequence[FLOAT]],
793
+ Optional[Sequence[FLOAT16]],
794
+ Optional[Sequence[INT16]],
795
+ Optional[Sequence[INT32]],
796
+ Optional[Sequence[INT64]],
797
+ Optional[Sequence[INT8]],
798
+ Optional[Sequence[STRING]],
799
+ Optional[Sequence[UINT16]],
800
+ Optional[Sequence[UINT32]],
801
+ Optional[Sequence[UINT64]],
802
+ Optional[Sequence[UINT8]],
803
+ Optional[BFLOAT16],
804
+ Optional[BOOL],
805
+ Optional[COMPLEX128],
806
+ Optional[COMPLEX64],
807
+ Optional[DOUBLE],
808
+ Optional[FLOAT],
809
+ Optional[FLOAT16],
810
+ Optional[FLOAT8E4M3FN],
811
+ Optional[FLOAT8E4M3FNUZ],
812
+ Optional[FLOAT8E5M2],
813
+ Optional[FLOAT8E5M2FNUZ],
814
+ Optional[INT16],
815
+ Optional[INT32],
816
+ Optional[INT4],
817
+ Optional[INT64],
818
+ Optional[INT8],
819
+ Optional[STRING],
820
+ Optional[UINT16],
821
+ Optional[UINT32],
822
+ Optional[UINT4],
823
+ Optional[UINT64],
824
+ Optional[UINT8],
825
+ Sequence[BFLOAT16],
826
+ Sequence[BOOL],
827
+ Sequence[COMPLEX128],
828
+ Sequence[COMPLEX64],
829
+ Sequence[DOUBLE],
830
+ Sequence[FLOAT],
831
+ Sequence[FLOAT16],
832
+ Sequence[FLOAT8E4M3FN],
833
+ Sequence[FLOAT8E4M3FNUZ],
834
+ Sequence[FLOAT8E5M2],
835
+ Sequence[FLOAT8E5M2FNUZ],
836
+ Sequence[INT16],
837
+ Sequence[INT32],
838
+ Sequence[INT4],
839
+ Sequence[INT64],
840
+ Sequence[INT8],
841
+ Sequence[STRING],
842
+ Sequence[UINT16],
843
+ Sequence[UINT32],
844
+ Sequence[UINT4],
845
+ Sequence[UINT64],
846
+ Sequence[UINT8],
847
+ BFLOAT16,
848
+ BOOL,
849
+ COMPLEX128,
850
+ COMPLEX64,
851
+ DOUBLE,
852
+ FLOAT,
853
+ FLOAT16,
854
+ FLOAT8E4M3FN,
855
+ FLOAT8E4M3FNUZ,
856
+ FLOAT8E5M2,
857
+ FLOAT8E5M2FNUZ,
858
+ INT16,
859
+ INT32,
860
+ INT4,
861
+ INT64,
862
+ INT8,
863
+ STRING,
864
+ UINT16,
865
+ UINT32,
866
+ UINT4,
867
+ UINT64,
868
+ UINT8,
869
+ )
870
+
871
+ def Loop(
872
+ self, M: Optional[I_Loop], cond: Optional[B_Loop], *v_initial: V_Loop, body: GraphProto
873
+ ) -> V_Loop:
874
+ r"""[🌐 Loop(21)](https://onnx.ai/onnx/operators/onnx__Loop.html#loop-21 "Online Documentation")
875
+
876
+
877
+ Generic Looping construct. This loop has multiple termination conditions:
878
+
879
+ 1) Trip count. Iteration count specified at runtime. Set by
880
+ specifying the input M. Optional. Set to empty string to omit.
881
+ Note that a static trip count (specified at graph construction time) can be
882
+ specified by passing in a constant node for input M.
883
+ 2) Loop termination condition. This is an input to the op that determines
884
+ whether to run the first iteration and also a loop-carried dependency for
885
+ the body graph. The body graph must yield a value for the condition variable,
886
+ whether this input is provided or not.
887
+
888
+ This table summarizes the operating modes of this operator with equivalent
889
+ C-style code:
890
+
891
+ Operator inputs defined as (max_trip_count, condition_var).
892
+
893
+ * input ("", ""):
894
+ for (int i=0; ; ++i) {
895
+ cond = ... // Note this value is ignored, but is required in the body
896
+ }
897
+
898
+ * input ("", cond) // Note this is analogous to a while loop
899
+ bool cond = ...;
900
+ for (int i=0; cond; ++i) {
901
+ cond = ...;
902
+ }
903
+
904
+ * input ("", 1) // Note this is analogous to a do-while loop
905
+ bool cond = true
906
+ for (int i=0; cond; ++i) {
907
+ cond = ...;
908
+ }
909
+
910
+ * input (trip_count, "") // Note this is analogous to a for loop
911
+ int trip_count = ...
912
+ for (int i=0; i < trip_count; ++i) {
913
+ cond = ...; // ignored
914
+ }
915
+
916
+ * input (trip_count, cond)
917
+ int trip_count = ...;
918
+ bool cond = ...;
919
+ for (int i=0; i < trip_count && cond; ++i) {
920
+ cond = ...;
921
+ }
922
+
923
+
924
+ *Sample usage - cond as well as trip count*
925
+
926
+ graph predict-net {
927
+ %a = Constant[value = <Scalar Tensor [3]>]()
928
+ %b = Constant[value = <Scalar Tensor [6]>]()
929
+ %keepgoing = Constant[value = <Scalar Tensor [1]>]()
930
+ %max_trip_count = Constant[value = <Scalar Tensor [10]>]()
931
+ %keepgoing_out, %b_out, %user_defined_vals = Loop[body = <graph body-net>](%max_trip_count, %keepgoing, %b)
932
+ return
933
+ }
934
+
935
+ graph body-net (
936
+ %i[INT32, scalar] // iteration number
937
+ %keepgoing_in[BOOL, scalar] // incoming loop-termination-condition; not used
938
+ %b_in[INT32, scalar] // incoming value of loop-carried-dependency b
939
+ ) {
940
+ %my_local = Add(%a, %b_in)
941
+ %b_out = Sub(%a, %b_in) // outgoing value of loop-carried-dependency b
942
+ %keepgoing_out = Greater(%my_local, %b_out) // outgoing loop-termination-condition
943
+ %user_defined_val = Add(%b_in, %b_in) // scan-output value to be accumulated
944
+ return %keepgoing_out, %b_out, %user_defined_val
945
+ }
946
+
947
+ *Sample equivalent C code*
948
+
949
+ {
950
+ /* User-defined code (enclosing scope) */
951
+ int a = 3, b = 6;
952
+ bool keepgoing = true; // Analogous to input cond
953
+ /* End user-defined code */
954
+
955
+ /* Implicitly-defined code */
956
+ const int max_trip_count = 10; // Analogous to input M
957
+ int user_defined_vals[]; // Imagine this is resizable
958
+ /* End implicitly-defined code */
959
+ /* initialize loop-carried variables and scan-output variables */
960
+ bool keepgoing_out = keepgoing
961
+ int b_out = b
962
+
963
+ for (int i=0; i < max_trip_count && keepgoing_out; ++i) {
964
+ /* Implicitly-defined code: bind actual parameter values
965
+ to formal parameter variables of loop-body */
966
+ bool keepgoing_in = keepgoing_out;
967
+ bool b_in = b_out;
968
+
969
+ /* User-defined code (loop body) */
970
+ int my_local = a + b_in; // Reading value "a" from the enclosing scope is fine
971
+ b_out = a - b_in;
972
+ keepgoing_out = my_local > b_out;
973
+ user_defined_val = b_in + b_in; // b_in and b_out are different variables
974
+ /* End user-defined code */
975
+
976
+ /* Implicitly defined-code */
977
+ user_defined_vals[i] = user_defined_val // accumulate scan-output values
978
+ }
979
+ // int t = my_local; // Can't do this. my_local is not accessible here.
980
+
981
+ // The values below are bound to the output variables of the loop and therefore accessible
982
+ // b_out; user_defined_vals; keepgoing_out;
983
+ }
984
+
985
+ There are several things of note in this code snippet:
986
+
987
+ 1) Values from the enclosing scope (i.e. variable "a" here) are in scope and can
988
+ be referenced in the inputs of the loop.
989
+ 2) Any values computed in the loop body that needs to be used in a subsequent
990
+ iteration or after the loop are modelled using a pair of variables in the loop-body,
991
+ consisting of an input variable (eg., b_in) and an output variable (eg., b_out).
992
+ These are referred to as loop-carried dependences. The loop operation node
993
+ supplies the input value of the input variable for the first iteration, and
994
+ returns the output value of the output variable produced by the final
995
+ iteration.
996
+ 3) Scan_output variables are used to implicitly concatenate values computed across
997
+ all the iterations. In the above example, the value of user_defined_val computed
998
+ over all iterations are concatenated and returned as the value of user_defined_vals
999
+ after the loop.
1000
+ 4) Values created in the body cannot be accessed in the enclosing scope,
1001
+ except using the mechanism described above.
1002
+
1003
+ Note that the semantics of this op support "diagonal" or "wavefront" execution.
1004
+ (See Step 3 here for an example:
1005
+ https://devblogs.nvidia.com/optimizing-recurrent-neural-networks-cudnn-5/).
1006
+ Frontends should emit multi-layer RNNs as a series of While operators (with
1007
+ time being the inner looping dimension), with each successive layer consuming
1008
+ the scan_outputs from the previous layer, possibly going through several
1009
+ point-wise operators (e.g. dropout, residual connections, linear layer).
1010
+
1011
+ The input/output of subgraph (produced by loop node) matching is based on order instead of name. The implementation will figure out the names based on this order.
1012
+
1013
+
1014
+ Args:
1015
+ M: (optional) A maximum trip-count for the loop specified at runtime.
1016
+ Optional. Pass empty string to skip.
1017
+
1018
+ cond: (optional) A boolean termination condition. Optional. Pass empty
1019
+ string to skip.
1020
+
1021
+ v_initial: (variadic, heterogeneous) The initial values of any loop-carried
1022
+ dependencies (values that change across loop iterations)
1023
+
1024
+ body: The graph run each iteration. It has 2+N inputs: (iteration_num,
1025
+ condition, loop carried dependencies...). It has 1+N+K outputs:
1026
+ (condition, loop carried dependencies..., scan_outputs...). Each
1027
+ scan_output is created by concatenating the value of the specified
1028
+ output value at the end of each iteration of the loop. It is an error if
1029
+ the dimensions or data type of these scan_outputs change across loop
1030
+ iterations.
1031
+ """
1032
+
1033
+ schema = get_schema("Loop", 21, "")
1034
+ op = Op(self, "Loop", schema)
1035
+ return op(*self._prepare_inputs(schema, M, cond, *v_initial), body=body)
1036
+
1037
+ T_Pad = TypeVar(
1038
+ "T_Pad",
1039
+ BFLOAT16,
1040
+ BOOL,
1041
+ COMPLEX128,
1042
+ COMPLEX64,
1043
+ DOUBLE,
1044
+ FLOAT,
1045
+ FLOAT16,
1046
+ FLOAT8E4M3FN,
1047
+ FLOAT8E4M3FNUZ,
1048
+ FLOAT8E5M2,
1049
+ FLOAT8E5M2FNUZ,
1050
+ INT16,
1051
+ INT32,
1052
+ INT4,
1053
+ INT64,
1054
+ INT8,
1055
+ STRING,
1056
+ UINT16,
1057
+ UINT32,
1058
+ UINT4,
1059
+ UINT64,
1060
+ UINT8,
1061
+ )
1062
+
1063
+ Tind_Pad = TypeVar("Tind_Pad", INT32, INT64)
1064
+
1065
+ def Pad(
1066
+ self,
1067
+ data: T_Pad,
1068
+ pads: INT64,
1069
+ constant_value: Optional[T_Pad] = None,
1070
+ axes: Optional[Tind_Pad] = None,
1071
+ *,
1072
+ mode: str = "constant",
1073
+ ) -> T_Pad:
1074
+ r"""[🌐 Pad(21)](https://onnx.ai/onnx/operators/onnx__Pad.html#pad-21 "Online Documentation")
1075
+
1076
+
1077
+ Given a tensor containing the data to be padded (`data`), a tensor containing the number of start and end pad values for axis (`pads`), (optionally) a `mode`, and (optionally) `constant_value`,
1078
+ a padded tensor (`output`) is generated.
1079
+
1080
+ The three supported `modes` are (similar to corresponding modes supported by `numpy.pad`):
1081
+
1082
+ 1) `constant`(default) - pads with a given constant value as specified by `constant_value` (which defaults to 0, empty string, or False)
1083
+
1084
+ 2) `reflect` - pads with the reflection of the vector mirrored on the first and last values of the vector along each axis
1085
+
1086
+ 3) `edge` - pads with the edge values of array
1087
+
1088
+ 4) `wrap` - wrap-around padding as if the data tensor forms a torus
1089
+
1090
+
1091
+ Example 1 (`constant` mode):
1092
+
1093
+ Insert 0 pads to the beginning of the second dimension.
1094
+
1095
+ ::
1096
+
1097
+ data = [
1098
+ [1.0, 1.2],
1099
+ [2.3, 3.4],
1100
+ [4.5, 5.7],
1101
+ ]
1102
+
1103
+ pads = [0, 2, 0, 0]
1104
+
1105
+ mode = 'constant'
1106
+
1107
+ constant_value = 0.0
1108
+
1109
+ output = [
1110
+ [0.0, 0.0, 1.0, 1.2],
1111
+ [0.0, 0.0, 2.3, 3.4],
1112
+ [0.0, 0.0, 4.5, 5.7],
1113
+ ]
1114
+
1115
+
1116
+
1117
+ Example 2 (`reflect` mode):
1118
+
1119
+ ::
1120
+
1121
+ data = [
1122
+ [1.0, 1.2],
1123
+ [2.3, 3.4],
1124
+ [4.5, 5.7],
1125
+ ]
1126
+
1127
+ pads = [0, 2, 0, 0]
1128
+
1129
+ mode = 'reflect'
1130
+
1131
+ output = [
1132
+ [1.0, 1.2, 1.0, 1.2],
1133
+ [2.3, 3.4, 2.3, 3.4],
1134
+ [4.5, 5.7, 4.5, 5.7],
1135
+ ]
1136
+
1137
+
1138
+
1139
+ Example 3 (`edge` mode):
1140
+
1141
+ ::
1142
+
1143
+ data = [
1144
+ [1.0, 1.2],
1145
+ [2.3, 3.4],
1146
+ [4.5, 5.7],
1147
+ ]
1148
+
1149
+ pads = [0, 2, 0, 0]
1150
+
1151
+ mode = 'edge'
1152
+
1153
+ output = [
1154
+ [1.0, 1.0, 1.0, 1.2],
1155
+ [2.3, 2.3, 2.3, 3.4],
1156
+ [4.5, 4.5, 4.5, 5.7],
1157
+ ]
1158
+
1159
+
1160
+
1161
+ Example 4 (`wrap` mode):
1162
+
1163
+ ::
1164
+
1165
+ data = [
1166
+ [1.0, 1.2],
1167
+ [2.3, 3.4],
1168
+ [4.5, 5.7],
1169
+ ]
1170
+
1171
+ pads = [2, 1, 1, 1]
1172
+
1173
+ mode = 'wrap'
1174
+
1175
+ output = [
1176
+ [3.4, 2.3, 3.4, 2.3],
1177
+ [5.7, 4.5, 5.7, 4.5],
1178
+ [1.2, 1.0, 1.2, 1.0],
1179
+ [3.4, 2.3, 3.4, 2.3],
1180
+ [5.7, 4.5, 5.7, 4.5],
1181
+ [1.2, 1.0, 1.2, 1.0],
1182
+ ]
1183
+
1184
+
1185
+
1186
+
1187
+ Args:
1188
+ data: (differentiable) Input tensor.
1189
+
1190
+ pads: (non-differentiable) Tensor of integers indicating the number of
1191
+ padding elements to add or remove (if negative) at the beginning and end
1192
+ of each axis. For 2D input tensor, it is the number of pixels. `pads`
1193
+ should be a 1D tensor of shape [2 * num_axes] where `num_axes` refers to
1194
+ the number of elements in the `axes` input or the input rank if `axes`
1195
+ are not provided explicitly. `pads` format should be: [x1_begin,
1196
+ x2_begin, ..., x1_end, x2_end,...], where xi_begin is the number of pad
1197
+ values added at the beginning of axis `axes[i]` and xi_end, the number
1198
+ of pad values added at the end of axis `axes[i]`.
1199
+
1200
+ constant_value: (optional, non-differentiable) (Optional) A scalar value to
1201
+ be used if the mode chosen is `constant` (by default it is 0, empty
1202
+ string or False).
1203
+
1204
+ axes: (optional, non-differentiable) 1-D tensor of axes that `pads` apply
1205
+ to. Negative value means counting dimensions from the back. Accepted
1206
+ range is [-r, r-1] where r = rank(data). Behavior is undefined if an
1207
+ axis is repeated. If not provided, all axes are assumed (`[0, 1, ...,
1208
+ input_rank-1]`).
1209
+
1210
+ mode: Supported modes: `constant`(default), `reflect`, `edge`, `wrap`
1211
+ """
1212
+
1213
+ schema = get_schema("Pad", 21, "")
1214
+ op = Op(self, "Pad", schema)
1215
+ return op(*self._prepare_inputs(schema, data, pads, constant_value, axes), mode=mode)
1216
+
1217
+ T1_QLinearMatMul = TypeVar(
1218
+ "T1_QLinearMatMul",
1219
+ FLOAT8E4M3FN,
1220
+ FLOAT8E4M3FNUZ,
1221
+ FLOAT8E5M2,
1222
+ FLOAT8E5M2FNUZ,
1223
+ INT8,
1224
+ UINT8,
1225
+ )
1226
+
1227
+ TS_QLinearMatMul = TypeVar("TS_QLinearMatMul", BFLOAT16, FLOAT, FLOAT16)
1228
+
1229
+ T2_QLinearMatMul = TypeVar(
1230
+ "T2_QLinearMatMul",
1231
+ FLOAT8E4M3FN,
1232
+ FLOAT8E4M3FNUZ,
1233
+ FLOAT8E5M2,
1234
+ FLOAT8E5M2FNUZ,
1235
+ INT8,
1236
+ UINT8,
1237
+ )
1238
+
1239
+ T3_QLinearMatMul = TypeVar(
1240
+ "T3_QLinearMatMul",
1241
+ FLOAT8E4M3FN,
1242
+ FLOAT8E4M3FNUZ,
1243
+ FLOAT8E5M2,
1244
+ FLOAT8E5M2FNUZ,
1245
+ INT8,
1246
+ UINT8,
1247
+ )
1248
+
1249
+ def QLinearMatMul(
1250
+ self,
1251
+ a: T1_QLinearMatMul,
1252
+ a_scale: TS_QLinearMatMul,
1253
+ a_zero_point: T1_QLinearMatMul,
1254
+ b: T2_QLinearMatMul,
1255
+ b_scale: TS_QLinearMatMul,
1256
+ b_zero_point: T2_QLinearMatMul,
1257
+ y_scale: TS_QLinearMatMul,
1258
+ y_zero_point: T3_QLinearMatMul,
1259
+ ) -> T3_QLinearMatMul:
1260
+ r"""[🌐 QLinearMatMul(21)](https://onnx.ai/onnx/operators/onnx__QLinearMatMul.html#qlinearmatmul-21 "Online Documentation")
1261
+
1262
+
1263
+ Matrix product that behaves like [numpy.matmul](https://numpy.org/doc/stable/reference/generated/numpy.matmul.html).
1264
+ It consumes two quantized input tensors, their scales and zero points, scale and zero point of output,
1265
+ and computes the quantized output. The quantization formula is y = saturate((x / y_scale) + y_zero_point).
1266
+ For (x / y_scale), it is rounding to nearest ties to even. Refer to https://en.wikipedia.org/wiki/Rounding for details.
1267
+ Scale and zero point must have same shape. They must be either scalar (per tensor) or N-D tensor
1268
+ (per row for 'a' and per column for 'b'). Scalar refers to per tensor quantization whereas N-D refers to per row
1269
+ or per column quantization. If the input is 2D of shape [M, K] then zero point and scale tensor may be
1270
+ an M element vector [v_1, v_2, ..., v_M] for per row quantization and K element vector of shape [v_1, v_2, ..., v_K]
1271
+ for per column quantization. If the input is N-D tensor with shape [D1, D2, M, K] then zero point and scale tensor may
1272
+ have shape [D1, D2, M, 1] for per row quantization and shape [D1, D2, 1, K] for per column quantization.
1273
+ Production must never overflow, and accumulation may overflow if and only if in 32 bits.
1274
+
1275
+
1276
+ Args:
1277
+ a: (non-differentiable) N-dimensional quantized matrix a
1278
+
1279
+ a_scale: (non-differentiable) scale of quantized input a
1280
+
1281
+ a_zero_point: (non-differentiable) zero point of quantized input a
1282
+
1283
+ b: (non-differentiable) N-dimensional quantized matrix b
1284
+
1285
+ b_scale: (non-differentiable) scale of quantized input b
1286
+
1287
+ b_zero_point: (non-differentiable) zero point of quantized input b
1288
+
1289
+ y_scale: (non-differentiable) scale of quantized output y
1290
+
1291
+ y_zero_point: (non-differentiable) zero point of quantized output y
1292
+ """
1293
+
1294
+ schema = get_schema("QLinearMatMul", 21, "")
1295
+ op = Op(self, "QLinearMatMul", schema)
1296
+ return op(
1297
+ *self._prepare_inputs(
1298
+ schema,
1299
+ a,
1300
+ a_scale,
1301
+ a_zero_point,
1302
+ b,
1303
+ b_scale,
1304
+ b_zero_point,
1305
+ y_scale,
1306
+ y_zero_point,
1307
+ )
1308
+ )
1309
+
1310
+ T1_QuantizeLinear = TypeVar("T1_QuantizeLinear", BFLOAT16, FLOAT, FLOAT16, INT32)
1311
+
1312
+ T2_QuantizeLinear = TypeVar(
1313
+ "T2_QuantizeLinear",
1314
+ FLOAT8E4M3FN,
1315
+ FLOAT8E4M3FNUZ,
1316
+ FLOAT8E5M2,
1317
+ FLOAT8E5M2FNUZ,
1318
+ INT16,
1319
+ INT4,
1320
+ INT8,
1321
+ UINT16,
1322
+ UINT4,
1323
+ UINT8,
1324
+ )
1325
+
1326
+ def QuantizeLinear(
1327
+ self,
1328
+ x: T1_QuantizeLinear,
1329
+ y_scale: T1_QuantizeLinear,
1330
+ y_zero_point: Optional[T2_QuantizeLinear] = None,
1331
+ *,
1332
+ axis: int = 1,
1333
+ block_size: int = 0,
1334
+ output_dtype: int = 0,
1335
+ saturate: int = 1,
1336
+ ) -> T2_QuantizeLinear:
1337
+ r"""[🌐 QuantizeLinear(21)](https://onnx.ai/onnx/operators/onnx__QuantizeLinear.html#quantizelinear-21 "Online Documentation")
1338
+
1339
+
1340
+ The linear quantization operator consumes a high-precision tensor, a scale, and a zero point to compute the
1341
+ low-precision/quantized tensor. The scale factor and zero point must have the same shape, determining the quantization
1342
+ granularity. The quantization formula is `y = saturate((x / y_scale) + y_zero_point)`.
1343
+
1344
+ Saturation is done according to:
1345
+ - uint16: [0, 65535]
1346
+ - int16: [-32768, 32767]
1347
+ - uint8: [0, 255]
1348
+ - int8: [-128, 127]
1349
+ - uint4: [0, 15]
1350
+ - int4: [-8, 7]
1351
+
1352
+ For `(x / y_scale)`, it rounds to the nearest even. Refer to https://en.wikipedia.org/wiki/Rounding for details.
1353
+
1354
+ `y_zero_point` and `y` must have the same type. `y_zero_point` is usually not used for quantization to float8 types, but the quantization
1355
+ formula remains the same for consistency, and the type of the attribute `y_zero_point` still determines the quantization type.
1356
+
1357
+ There are three supported quantization granularities, determined by the shape of `y_scale`.
1358
+ In all cases, `y_zero_point` must have the same shape as `y_scale`.
1359
+ - Per-tensor (per-layer) quantization: `y_scale` is a scalar.
1360
+ - Per-axis quantization: The scale must be a 1-D tensor, with the length of the quantization axis. For an input shape
1361
+ `(D0, ..., Di, ..., Dn)` and `axis=i`, `y_scale` is a 1-D tensor of length `Di`.
1362
+ - Blocked quantization: The scale's shape is identical to the input's shape, except for one dimension, in which
1363
+ blocking is performed. Given `x` shape `(D0, ..., Di, ..., Dn)`, `axis=i`, and block size `B`: `y_scale` shape is
1364
+ `(D0, ..., ceil(Di/B), ..., Dn)`.
1365
+
1366
+
1367
+ Args:
1368
+ x: N-D full precision Input tensor to be quantized.
1369
+
1370
+ y_scale: Scale for doing quantization to get `y`. For per-tensor/layer
1371
+ quantization the scale is a scalar, for per-axis quantization it is a
1372
+ 1-D Tensor and for blocked quantization it has the same shape as the
1373
+ input, except for one dimension in which blocking is performed.
1374
+
1375
+ y_zero_point: (optional) Zero point for doing quantization to get `y`. Shape
1376
+ must match `y_scale`.Default is uint8 with zero point of 0 if it's not
1377
+ specified.
1378
+
1379
+ axis: (Optional) The axis of the dequantizing dimension of the input tensor.
1380
+ Used only for per-axis and blocked quantization. Negative value means
1381
+ counting dimensions from the back. Accepted range is `[-r, r-1]` where
1382
+ `r = rank(input)`. When the rank of the input is 1, per-tensor
1383
+ quantization is applied, rendering the axis unnecessary in this
1384
+ scenario.
1385
+
1386
+ block_size: (Optional) The size of the quantization block (number of times
1387
+ every scale is replicated). Used only for blocked quantization. The
1388
+ block size is a positive integer. Given `x` shape `(D0, ..., Di, ...,
1389
+ Dn)`, `y_scale` shape `(S0, ... Si, ...Sn)` and `axis=i`, the accepted
1390
+ range is `[ceil(Di/Si), ceil(Di/(Si-1))-1]`
1391
+
1392
+ output_dtype: (Optional) The output data type. If not supplied, the output
1393
+ data type is inferred from `y_zero_point` data type (`T2`). If neither
1394
+ `output_dtype` nor `y_zero_point` are supplied, output data type is
1395
+ uint8. If both `output_dtype` and `y_zero_point` are specified,
1396
+ `output_dtype` must be `T2`.
1397
+
1398
+ saturate: The parameter defines how the conversion behaves if an input value
1399
+ is out of range of the destination type. It only applies for float 8
1400
+ quantization (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz).
1401
+ It is true by default. All cases are fully described in two tables
1402
+ inserted in the operator description.
1403
+ """
1404
+
1405
+ schema = get_schema("QuantizeLinear", 21, "")
1406
+ op = Op(self, "QuantizeLinear", schema)
1407
+ return op(
1408
+ *self._prepare_inputs(schema, x, y_scale, y_zero_point),
1409
+ axis=axis,
1410
+ block_size=block_size,
1411
+ output_dtype=output_dtype,
1412
+ saturate=saturate,
1413
+ )
1414
+
1415
+ T_Reshape = TypeVar(
1416
+ "T_Reshape",
1417
+ BFLOAT16,
1418
+ BOOL,
1419
+ COMPLEX128,
1420
+ COMPLEX64,
1421
+ DOUBLE,
1422
+ FLOAT,
1423
+ FLOAT16,
1424
+ FLOAT8E4M3FN,
1425
+ FLOAT8E4M3FNUZ,
1426
+ FLOAT8E5M2,
1427
+ FLOAT8E5M2FNUZ,
1428
+ INT16,
1429
+ INT32,
1430
+ INT4,
1431
+ INT64,
1432
+ INT8,
1433
+ STRING,
1434
+ UINT16,
1435
+ UINT32,
1436
+ UINT4,
1437
+ UINT64,
1438
+ UINT8,
1439
+ )
1440
+
1441
+ def Reshape(self, data: T_Reshape, shape: INT64, *, allowzero: int = 0) -> T_Reshape:
1442
+ r"""[🌐 Reshape(21)](https://onnx.ai/onnx/operators/onnx__Reshape.html#reshape-21 "Online Documentation")
1443
+
1444
+
1445
+ Reshape the input tensor similar to numpy.reshape.
1446
+ First input is the data tensor, second input is a shape tensor which specifies the output shape. It outputs the reshaped tensor.
1447
+ At most one dimension of the new shape can be -1. In this case, the value is
1448
+ inferred from the size of the tensor and the remaining dimensions. A dimension
1449
+ could also be 0, in which case the actual dimension value is unchanged (i.e. taken
1450
+ from the input tensor). If 'allowzero' is set, and the new shape includes 0, the
1451
+ dimension will be set explicitly to zero (i.e. not taken from input tensor).
1452
+ Shape (second input) could be an empty shape, which means converting to a scalar.
1453
+ The input tensor's shape and the output tensor's shape are required to have the same number of elements.
1454
+
1455
+ If the attribute 'allowzero' is set, it is invalid for the specified shape to
1456
+ contain both a zero value and -1, as the value of the dimension corresponding
1457
+ to -1 cannot be determined uniquely.
1458
+
1459
+
1460
+ Args:
1461
+ data: (differentiable) An input tensor.
1462
+
1463
+ shape: (non-differentiable) Specified shape for output.
1464
+
1465
+ allowzero: (Optional) By default, when any value in the 'shape' input is
1466
+ equal to zero the corresponding dimension value is copied from the input
1467
+ tensor dynamically. allowzero=1 indicates that if any value in the
1468
+ 'shape' input is set to zero, the zero value is honored, similar to
1469
+ NumPy.
1470
+ """
1471
+
1472
+ schema = get_schema("Reshape", 21, "")
1473
+ op = Op(self, "Reshape", schema)
1474
+ return op(*self._prepare_inputs(schema, data, shape), allowzero=allowzero)
1475
+
1476
+ V_Scan = TypeVar(
1477
+ "V_Scan",
1478
+ BFLOAT16,
1479
+ BOOL,
1480
+ COMPLEX128,
1481
+ COMPLEX64,
1482
+ DOUBLE,
1483
+ FLOAT,
1484
+ FLOAT16,
1485
+ FLOAT8E4M3FN,
1486
+ FLOAT8E4M3FNUZ,
1487
+ FLOAT8E5M2,
1488
+ FLOAT8E5M2FNUZ,
1489
+ INT16,
1490
+ INT32,
1491
+ INT4,
1492
+ INT64,
1493
+ INT8,
1494
+ STRING,
1495
+ UINT16,
1496
+ UINT32,
1497
+ UINT4,
1498
+ UINT64,
1499
+ UINT8,
1500
+ )
1501
+
1502
+ def Scan(
1503
+ self,
1504
+ *initial_state_and_scan_inputs: V_Scan,
1505
+ body: GraphProto,
1506
+ num_scan_inputs: int,
1507
+ scan_input_axes: Optional[Sequence[int]] = None,
1508
+ scan_input_directions: Optional[Sequence[int]] = None,
1509
+ scan_output_axes: Optional[Sequence[int]] = None,
1510
+ scan_output_directions: Optional[Sequence[int]] = None,
1511
+ ) -> V_Scan:
1512
+ r"""[🌐 Scan(21)](https://onnx.ai/onnx/operators/onnx__Scan.html#scan-21 "Online Documentation")
1513
+
1514
+
1515
+ Scan can be used to iterate over one or more scan_input tensors,
1516
+ constructing zero or more scan_output tensors. It combines ideas from general recurrences,
1517
+ functional programming constructs such as scan, fold, map, and zip, and is intended to enable
1518
+ generalizations of RNN-like constructs for sequence-to-sequence processing.
1519
+ Other tensors (referred to as state_variables here) can be used to carry a state
1520
+ when iterating from one element to another (similar to hidden-state in RNNs, also referred
1521
+ to as loop-carried dependences in the context of loops).
1522
+ Many common usages involve a single scan_input tensor (where functionality
1523
+ similar to scan, fold and map can be obtained). When more than one scan_input is used,
1524
+ a behavior similar to zip is obtained.
1525
+
1526
+ The attribute body must be a graph, specifying the computation to be performed in
1527
+ every iteration. It takes as input the current values of the state_variables and
1528
+ the current iterated element of the scan_inputs. It must return the (updated) values
1529
+ of the state_variables and zero or more scan_output_element tensors. The values of the
1530
+ scan_output_element tensors are concatenated over all the iterations to produce the
1531
+ scan_output values of the scan construct (similar to the concatenated intermediate
1532
+ hidden-state values of RNN-like constructs). All the output tensors (state_variables as
1533
+ well as scan_output_element tensors) are required to have the same shape in each iteration
1534
+ of the loop (a restriction imposed to enable efficient memory allocation).
1535
+
1536
+ Note that the iterated element passed to the body subgraph does not have a sequence
1537
+ axis. It will have a rank one less than the rank of the corresponding scan_input.
1538
+
1539
+ The scan operation returns the final values of the state_variables as well as the
1540
+ scan_outputs.
1541
+
1542
+ The optional attribute scan_input_directions specifies the direction (forward or backward)
1543
+ for each scan input. If this attribute is omitted, all sequences are scanned in the forward
1544
+ direction. A bidirectional scan may be performed by specifying the same tensor input twice
1545
+ in the scan_inputs, once with a forward direction, and once with a backward direction.
1546
+
1547
+ The scan_output of the operation is produced by concatenating the scan_output_element
1548
+ values produced by the body in each iteration. The optional attribute scan_output_directions
1549
+ specifies the direction in which scan_output is constructed (by appending or prepending the
1550
+ scan_output_element to scan_output in each iteration) for each scan_output. If this attribute
1551
+ is omitted, the scan_output_element is appended to the scan_output in each iteration.
1552
+
1553
+ The optional attribute scan_input_axes specifies the axis to be scanned for each scan_input.
1554
+ If omitted, every scan_input will be scanned in axis 0. For example, if axis 0 is the
1555
+ batch axis and axis 1 is the time axis (to be scanned), specify an axis value of 1.
1556
+ Note that scanning a non-zero axis may be less efficient than scanning axis zero.
1557
+
1558
+ The optional attribute scan_output_axes specifies the axis along which the scan_outputs
1559
+ are accumulated for each scan_output. For example, if axis 1 is the time axis (to be
1560
+ scanned) for both inputs and outputs, specify a scan_input axis and scan_output axis
1561
+ value of 1.
1562
+
1563
+ Note that because of the ONNX restriction that only the last parameter of an operator can
1564
+ be variadic, the initial-states and scan-inputs are listed together as one input parameter.
1565
+ Similarly, the final-states and scan-outputs are listed together as one output parameter.
1566
+ The attribute num_scan_inputs indicates the number M of scan-inputs.
1567
+
1568
+ The behavior of
1569
+
1570
+ Scan <
1571
+ num_scan_inputs = m,
1572
+ body = loop-body,
1573
+ scan_input_axes = [axis_1, ..., axis_m]
1574
+ > (init_1, ..., init_n, scan_1, ..., scan_m)
1575
+
1576
+ is equivalent to the following pseudo-code:
1577
+
1578
+ // scan_i.shape[axis_i] denotes the (max) sequence-length of scan_i
1579
+ // scan_i.shape[axis_i] is required to be equal to scan_j.shape[axis_j] for all i,j.
1580
+ sequence_length = scan_1.shape[axis_1];
1581
+
1582
+ // initialize state-variables
1583
+ st_1 = init_1; ... st_n = init_n;
1584
+ // initialize scan-output variables: [] denotes an empty tensor
1585
+ scan_out_1 = []; ...; scan_out_k = [];
1586
+ // identify number of iterations:
1587
+
1588
+ // execute loop
1589
+ for (int t = 0; t < sequence_length; ++t) {
1590
+ // generate the scan-input elements: the notation T<axis=k>[t] indicates the sub-tensor
1591
+ // of rank one less than T obtained by indexing T at position t along axis k.
1592
+ si_1 = scan_1<axis=axis_1>[t];
1593
+ ... ;
1594
+ si_m = scan_m<axis=axis_m>[t];
1595
+ // execute loop-body
1596
+ st_1, ..., st_n, so_1, ..., so_k = loop-body(st_1, ..., st_n, si_1, ..., si_m)
1597
+ // accumulate the scan-output elements
1598
+ scan_out_1 = Concat<axis=0>(scan_out_1, so_1); ... ; scan_out_k = Concat<axis=0>(scan_out_k, so_k);
1599
+ }
1600
+
1601
+ return st_1, ..., st_n, scan_out_1, ..., scan_out_k;
1602
+
1603
+ *Sample usage: Encoding RNN using a Scan*
1604
+
1605
+ The following example shows how a simple RNN over an input tensor %X, with weight tensor %Wi,
1606
+ recurrence weight tensor %Ri, bias tensors %Wbi and %Rbi, and initial hidden-state %H_0 can
1607
+ be encoded as a ScanLoop. Note that the loop-body is a nested graph, and it directly computes
1608
+ %Wi, %Ri, %Wbi, and %Rbi (typically constants or initializers in the body graph). If these
1609
+ values are computed in the outer graph, they need to be passed in as extra state_variables.
1610
+
1611
+ graph rnn-encoding {
1612
+ %H_0 = ...
1613
+ %X = ...
1614
+ %Y_h, %Y = Scan[body = <graph rnn-cell-1>, num_scan_inputs=1](%H_0, %X)
1615
+ return %Y, %Y_h
1616
+ }
1617
+
1618
+ graph rnn-cell-1 (
1619
+ %H_tminus1[FLOAT, tensor]
1620
+ %X_t[FLOAT, tensor]
1621
+ ) {
1622
+ %Wi = ...
1623
+ %Ri = ...
1624
+ %Wbi = ...
1625
+ %Rbi = ...
1626
+ %t1 = X_t * (Wi^T)
1627
+ %t2 = H_tminus1*(Ri^T)
1628
+ %t3 = Add(%t1, %t2)
1629
+ %t4 = Add(%t3, %Wbi)
1630
+ %t5 = Add(%t4, %Rbi)
1631
+ %Ht = Tanh(%t5)
1632
+ %Accumulate = Identity(%Ht)
1633
+ return %Ht, %Accumulate
1634
+ }
1635
+
1636
+
1637
+
1638
+ Args:
1639
+ initial_state_and_scan_inputs: (variadic, heterogeneous) Initial values of
1640
+ the loop's N state variables followed by M scan_inputs
1641
+
1642
+ body: The graph run each iteration. It has N+M inputs: (loop state
1643
+ variables..., scan_input_elts...). It has N+K outputs: (loop state
1644
+ variables..., scan_output_elts...). Each scan_output is created by
1645
+ concatenating the value of the specified scan_output_elt value at the
1646
+ end of each iteration of the loop. It is an error if the dimensions of
1647
+ these values change across loop iterations.
1648
+
1649
+ num_scan_inputs: An attribute specifying the number of scan_inputs M.
1650
+
1651
+ scan_input_axes: An optional list of M flags. The i-th element of the list
1652
+ specifies the axis to be scanned (the sequence axis) for the i-th
1653
+ scan_input. If omitted, 0 will be used as the scan axis for every
1654
+ scan_input. Negative value for an axis means counting dimensions from
1655
+ the back. Accepted range is [-r, r-1] where r = rank(input).
1656
+
1657
+ scan_input_directions: An optional list of M flags. The i-th element of the
1658
+ list specifies the direction to be scanned for the i-th scan_input
1659
+ tensor: 0 indicates forward direction and 1 indicates reverse direction.
1660
+ If omitted, all scan_input tensors will be scanned in the forward
1661
+ direction.
1662
+
1663
+ scan_output_axes: An optional list of K flags. The i-th element of the list
1664
+ specifies the axis for the i-th scan_output. The scan outputs are
1665
+ accumulated along the specified axis. If omitted, 0 will be used as the
1666
+ scan axis for every scan_output. Negative value for an axis means
1667
+ counting dimensions from the back. Accepted range is [-r, r-1].
1668
+
1669
+ scan_output_directions: An optional list of K flags, one for each
1670
+ scan_output. The i-th element of the list specifies whether the i-th
1671
+ scan_output should be constructed by appending or prepending a new value
1672
+ in each iteration: 0 indicates appending and 1 indicates prepending. If
1673
+ omitted, all scan_output tensors will be produced by appending a value
1674
+ in each iteration.
1675
+ """
1676
+
1677
+ schema = get_schema("Scan", 21, "")
1678
+ op = Op(self, "Scan", schema)
1679
+ return op(
1680
+ *self._prepare_inputs(schema, *initial_state_and_scan_inputs),
1681
+ body=body,
1682
+ num_scan_inputs=num_scan_inputs,
1683
+ scan_input_axes=scan_input_axes,
1684
+ scan_input_directions=scan_input_directions,
1685
+ scan_output_axes=scan_output_axes,
1686
+ scan_output_directions=scan_output_directions,
1687
+ )
1688
+
1689
+ T_Shape = TypeVar(
1690
+ "T_Shape",
1691
+ BFLOAT16,
1692
+ BOOL,
1693
+ COMPLEX128,
1694
+ COMPLEX64,
1695
+ DOUBLE,
1696
+ FLOAT,
1697
+ FLOAT16,
1698
+ FLOAT8E4M3FN,
1699
+ FLOAT8E4M3FNUZ,
1700
+ FLOAT8E5M2,
1701
+ FLOAT8E5M2FNUZ,
1702
+ INT16,
1703
+ INT32,
1704
+ INT4,
1705
+ INT64,
1706
+ INT8,
1707
+ STRING,
1708
+ UINT16,
1709
+ UINT32,
1710
+ UINT4,
1711
+ UINT64,
1712
+ UINT8,
1713
+ )
1714
+
1715
+ T1_Shape: TypeAlias = INT64
1716
+
1717
+ def Shape(self, data: T_Shape, *, end: Optional[int] = None, start: int = 0) -> T1_Shape:
1718
+ r"""[🌐 Shape(21)](https://onnx.ai/onnx/operators/onnx__Shape.html#shape-21 "Online Documentation")
1719
+
1720
+
1721
+ Takes a tensor as input and outputs an 1D int64 tensor containing the shape of the input tensor.
1722
+ Optional attributes start and end can be used to compute a slice of the input tensor's shape.
1723
+ If start axis is omitted, the slice starts from axis 0.
1724
+ The end axis, if specified, is exclusive (and the returned value will not include the size of that axis).
1725
+ If the end axis is omitted, the axes upto the last one will be included.
1726
+ Negative axes indicate counting back from the last axis.
1727
+ Note that axes will be clamped to the range [0, r-1], where r is the
1728
+ rank of the input tensor if they are out-of-range (after adding r in the case of
1729
+ negative axis). Thus, specifying any end value > r is equivalent to specifying an end
1730
+ value of r, and specifying any start value < -r is equivalent to specifying a start
1731
+ value of 0.
1732
+
1733
+ Examples:
1734
+
1735
+ ::
1736
+
1737
+ Input tensor with shape: [2, 3, 4]
1738
+ No attributes specified.
1739
+ Output: [2, 3, 4]
1740
+
1741
+
1742
+
1743
+ ::
1744
+
1745
+ Input tensor with shape: [2, 3, 4]
1746
+ start: -1
1747
+ Output: [4]
1748
+
1749
+
1750
+
1751
+ ::
1752
+
1753
+ Input tensor with shape: [2, 3, 4]
1754
+ end: -1
1755
+ Output: [2, 3]
1756
+
1757
+
1758
+
1759
+ ::
1760
+
1761
+ Input tensor with shape: [2, 3, 4]
1762
+ start: 1
1763
+ end: 2
1764
+ Output: [3]
1765
+
1766
+
1767
+
1768
+
1769
+ Args:
1770
+ data: (non-differentiable) An input tensor.
1771
+
1772
+ end: (Optional) Ending axis for slicing the shape. Negative value means
1773
+ counting dimensions from the back. If omitted, sizes of all axes upto
1774
+ (including) the last one will be included.
1775
+
1776
+ start: (Optional) Starting axis for slicing the shape. Default value is
1777
+ 0.Negative value means counting dimensions from the back.
1778
+ """
1779
+
1780
+ schema = get_schema("Shape", 21, "")
1781
+ op = Op(self, "Shape", schema)
1782
+ return op(*self._prepare_inputs(schema, data), end=end, start=start)
1783
+
1784
+ T_Size = TypeVar(
1785
+ "T_Size",
1786
+ BFLOAT16,
1787
+ BOOL,
1788
+ COMPLEX128,
1789
+ COMPLEX64,
1790
+ DOUBLE,
1791
+ FLOAT,
1792
+ FLOAT16,
1793
+ FLOAT8E4M3FN,
1794
+ FLOAT8E4M3FNUZ,
1795
+ FLOAT8E5M2,
1796
+ FLOAT8E5M2FNUZ,
1797
+ INT16,
1798
+ INT32,
1799
+ INT4,
1800
+ INT64,
1801
+ INT8,
1802
+ STRING,
1803
+ UINT16,
1804
+ UINT32,
1805
+ UINT4,
1806
+ UINT64,
1807
+ UINT8,
1808
+ )
1809
+
1810
+ T1_Size: TypeAlias = INT64
1811
+
1812
+ def Size(self, data: T_Size) -> T1_Size:
1813
+ r"""[🌐 Size(21)](https://onnx.ai/onnx/operators/onnx__Size.html#size-21 "Online Documentation")
1814
+
1815
+
1816
+ Takes a tensor as input and outputs a int64 scalar that equals to the total number of elements of the input tensor.
1817
+
1818
+
1819
+ Args:
1820
+ data: (non-differentiable) An input tensor.
1821
+ """
1822
+
1823
+ schema = get_schema("Size", 21, "")
1824
+ op = Op(self, "Size", schema)
1825
+ return op(*self._prepare_inputs(schema, data))
1826
+
1827
+ T_Squeeze = TypeVar(
1828
+ "T_Squeeze",
1829
+ BFLOAT16,
1830
+ BOOL,
1831
+ COMPLEX128,
1832
+ COMPLEX64,
1833
+ DOUBLE,
1834
+ FLOAT,
1835
+ FLOAT16,
1836
+ FLOAT8E4M3FN,
1837
+ FLOAT8E4M3FNUZ,
1838
+ FLOAT8E5M2,
1839
+ FLOAT8E5M2FNUZ,
1840
+ INT16,
1841
+ INT32,
1842
+ INT4,
1843
+ INT64,
1844
+ INT8,
1845
+ STRING,
1846
+ UINT16,
1847
+ UINT32,
1848
+ UINT4,
1849
+ UINT64,
1850
+ UINT8,
1851
+ )
1852
+
1853
+ def Squeeze(self, data: T_Squeeze, axes: Optional[INT64] = None) -> T_Squeeze:
1854
+ r"""[🌐 Squeeze(21)](https://onnx.ai/onnx/operators/onnx__Squeeze.html#squeeze-21 "Online Documentation")
1855
+
1856
+
1857
+ Remove single-dimensional entries from the shape of a tensor.
1858
+ Takes an input `axes` with a list of axes to squeeze.
1859
+ If `axes` is not provided, all the single dimensions will be removed from
1860
+ the shape. If an axis is selected with shape entry not equal to one, an error is raised.
1861
+
1862
+
1863
+ Args:
1864
+ data: (differentiable) Tensors with at least max(dims) dimensions.
1865
+
1866
+ axes: (optional, non-differentiable) List of integers indicating the
1867
+ dimensions to squeeze. Negative value means counting dimensions from the
1868
+ back. Accepted range is [-r, r-1] where r = rank(data).
1869
+ """
1870
+
1871
+ schema = get_schema("Squeeze", 21, "")
1872
+ op = Op(self, "Squeeze", schema)
1873
+ return op(*self._prepare_inputs(schema, data, axes))
1874
+
1875
+ T_Transpose = TypeVar(
1876
+ "T_Transpose",
1877
+ BFLOAT16,
1878
+ BOOL,
1879
+ COMPLEX128,
1880
+ COMPLEX64,
1881
+ DOUBLE,
1882
+ FLOAT,
1883
+ FLOAT16,
1884
+ FLOAT8E4M3FN,
1885
+ FLOAT8E4M3FNUZ,
1886
+ FLOAT8E5M2,
1887
+ FLOAT8E5M2FNUZ,
1888
+ INT16,
1889
+ INT32,
1890
+ INT4,
1891
+ INT64,
1892
+ INT8,
1893
+ STRING,
1894
+ UINT16,
1895
+ UINT32,
1896
+ UINT4,
1897
+ UINT64,
1898
+ UINT8,
1899
+ )
1900
+
1901
+ def Transpose(
1902
+ self, data: T_Transpose, *, perm: Optional[Sequence[int]] = None
1903
+ ) -> T_Transpose:
1904
+ r"""[🌐 Transpose(21)](https://onnx.ai/onnx/operators/onnx__Transpose.html#transpose-21 "Online Documentation")
1905
+
1906
+
1907
+ Transpose the input tensor similar to numpy.transpose. For example, when
1908
+ perm=(1, 0, 2), given an input tensor of shape (1, 2, 3), the output shape
1909
+ will be (2, 1, 3).
1910
+
1911
+
1912
+ Args:
1913
+ data: (differentiable) An input tensor.
1914
+
1915
+ perm: A list of integers. By default, reverse the dimensions, otherwise
1916
+ permute the axes according to the values given. Its length must be equal
1917
+ to the rank of the input.
1918
+ """
1919
+
1920
+ schema = get_schema("Transpose", 21, "")
1921
+ op = Op(self, "Transpose", schema)
1922
+ return op(*self._prepare_inputs(schema, data), perm=perm)
1923
+
1924
+ T_Unsqueeze = TypeVar(
1925
+ "T_Unsqueeze",
1926
+ BFLOAT16,
1927
+ BOOL,
1928
+ COMPLEX128,
1929
+ COMPLEX64,
1930
+ DOUBLE,
1931
+ FLOAT,
1932
+ FLOAT16,
1933
+ FLOAT8E4M3FN,
1934
+ FLOAT8E4M3FNUZ,
1935
+ FLOAT8E5M2,
1936
+ FLOAT8E5M2FNUZ,
1937
+ INT16,
1938
+ INT32,
1939
+ INT4,
1940
+ INT64,
1941
+ INT8,
1942
+ STRING,
1943
+ UINT16,
1944
+ UINT32,
1945
+ UINT4,
1946
+ UINT64,
1947
+ UINT8,
1948
+ )
1949
+
1950
+ def Unsqueeze(self, data: T_Unsqueeze, axes: INT64) -> T_Unsqueeze:
1951
+ r"""[🌐 Unsqueeze(21)](https://onnx.ai/onnx/operators/onnx__Unsqueeze.html#unsqueeze-21 "Online Documentation")
1952
+
1953
+
1954
+ Insert single-dimensional entries to the shape of an input tensor (`data`).
1955
+ Takes one required input `axes` - which contains a list of dimension indices and this operator will insert a dimension of value `1` into the corresponding index of the output tensor (`expanded`).
1956
+
1957
+ For example, given an input tensor (`data`) of shape [3, 4, 5], then
1958
+ Unsqueeze(data, axes=[0, 4]) outputs a tensor (`expanded`) containing same data as `data` but with shape [1, 3, 4, 5, 1].
1959
+
1960
+ The input `axes` should not contain any duplicate entries. It is an error if it contains duplicates.
1961
+ The rank of the output tensor (`output_rank`) is the rank of the input tensor (`data`) plus the number of values in `axes`.
1962
+ Each value in `axes` should be within the (inclusive) range [-output_rank , output_rank - 1].
1963
+ The order of values in `axes` does not matter and can come in any order.
1964
+
1965
+
1966
+ Args:
1967
+ data: (differentiable) Original tensor
1968
+
1969
+ axes: (non-differentiable) List of integers indicating the dimensions to be
1970
+ inserted. Negative value means counting dimensions from the back.
1971
+ Accepted range is [-r, r-1] where r = rank(expanded).
1972
+ """
1973
+
1974
+ schema = get_schema("Unsqueeze", 21, "")
1975
+ op = Op(self, "Unsqueeze", schema)
1976
+ return op(*self._prepare_inputs(schema, data, axes))