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,1255 @@
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
19
+ from onnx.defs import get_schema
20
+ from typing_extensions import TypeAlias
21
+
22
+ from onnxscript.onnx_opset._impl.opset15 import Opset15
23
+ from onnxscript.onnx_types import (
24
+ BFLOAT16,
25
+ BOOL,
26
+ COMPLEX64,
27
+ COMPLEX128,
28
+ DOUBLE,
29
+ FLOAT,
30
+ FLOAT16,
31
+ INT8,
32
+ INT16,
33
+ INT32,
34
+ INT64,
35
+ STRING,
36
+ UINT8,
37
+ UINT16,
38
+ UINT32,
39
+ UINT64,
40
+ )
41
+ from onnxscript.values import Op, Opset
42
+
43
+
44
+ class Opset16(Opset15):
45
+ def __new__(cls):
46
+ return Opset.__new__(cls, "", 16)
47
+
48
+ T_GreaterOrEqual = TypeVar(
49
+ "T_GreaterOrEqual",
50
+ BFLOAT16,
51
+ DOUBLE,
52
+ FLOAT,
53
+ FLOAT16,
54
+ INT16,
55
+ INT32,
56
+ INT64,
57
+ INT8,
58
+ UINT16,
59
+ UINT32,
60
+ UINT64,
61
+ UINT8,
62
+ )
63
+
64
+ T1_GreaterOrEqual: TypeAlias = BOOL
65
+
66
+ def GreaterOrEqual(self, A: T_GreaterOrEqual, B: T_GreaterOrEqual) -> T1_GreaterOrEqual:
67
+ r"""[🌐 GreaterOrEqual(16)](https://onnx.ai/onnx/operators/onnx__GreaterOrEqual.html#greaterorequal-16 "Online Documentation")
68
+
69
+
70
+ Returns the tensor resulted from performing the `greater_equal` logical operation
71
+ elementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).
72
+
73
+ This operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check `Broadcasting in ONNX <https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md>`_.
74
+
75
+
76
+ Args:
77
+ A: (non-differentiable) First input operand for the logical operator.
78
+
79
+ B: (non-differentiable) Second input operand for the logical operator.
80
+ """
81
+
82
+ schema = get_schema("GreaterOrEqual", 16, "")
83
+ op = Op(self, "GreaterOrEqual", schema)
84
+ return op(*self._prepare_inputs(schema, A, B))
85
+
86
+ T1_GridSample = TypeVar(
87
+ "T1_GridSample",
88
+ BOOL,
89
+ COMPLEX128,
90
+ COMPLEX64,
91
+ DOUBLE,
92
+ FLOAT,
93
+ FLOAT16,
94
+ INT16,
95
+ INT32,
96
+ INT64,
97
+ INT8,
98
+ STRING,
99
+ UINT16,
100
+ UINT32,
101
+ UINT64,
102
+ UINT8,
103
+ )
104
+
105
+ T2_GridSample = TypeVar("T2_GridSample", DOUBLE, FLOAT, FLOAT16)
106
+
107
+ def GridSample(
108
+ self,
109
+ X: T1_GridSample,
110
+ grid: T2_GridSample,
111
+ *,
112
+ align_corners: int = 0,
113
+ mode: str = "bilinear",
114
+ padding_mode: str = "zeros",
115
+ ) -> T1_GridSample:
116
+ r"""[🌐 GridSample(16)](https://onnx.ai/onnx/operators/onnx__GridSample.html#gridsample-16 "Online Documentation")
117
+
118
+
119
+ Given an input `X` and a flow-field `grid`, computes the output `Y` using `X` values and pixel locations from `grid`.
120
+ Currently, only spatial (4-D) inputs are supported. For input `X` with shape (N, C, H, W) and `grid` with shape (N, H_out, W_out, 2),
121
+ the output `Y` will have shape (N, C, H_out, W_out).
122
+
123
+ The tensor `X` contains values at centers of square pixels in a H by W 2-dimensional image.
124
+ The tensor `grid` describes normalized positions where the output `Y` is to be computed
125
+ using a specified interpolation method (the mode) and a padding mode (for grid positions falling outside the 2-dimensional image).
126
+
127
+ Elements in `grid[N, H_out, W_out]` are size-2 vectors specifying positions in the 2-dimensional space of `X`.
128
+ They are used to interpolate output values of `Y[N, C, H_out, W_out]`.
129
+
130
+ The GridSample operator is often used in doing grid generator and sampler in the [Spatial Transformer Networks](https://arxiv.org/abs/1506.02025).
131
+ See also in [torch.nn.functional.grid_sample](https://pytorch.org/docs/master/generated/torch.nn.functional.grid_sample.html#torch-nn-functional-grid-sample).
132
+
133
+
134
+ Args:
135
+ X: (differentiable) 4-D tensor of shape (N, C, H, W), where N is the batch
136
+ size, C is the numbers of channels, H and W are the height and width of
137
+ the input data.
138
+
139
+ grid: (non-differentiable) Input offset, 4-D tensor of shape (N, H_out,
140
+ W_out, 2), where H_out and W_out are the height and width of grid and
141
+ output, Grid specifies the sampling pixel locations normalized by the
142
+ input spatial dimensions. Therefore, it should have most values in the
143
+ range of [-1, 1]. If grid has values outside the range of [-1, 1], the
144
+ corresponding outputs will be handled as defined by padding_mode.
145
+
146
+ align_corners: If align_corners=1, the extrema (-1 and 1) are considered as
147
+ referring to the center points of the input's corner pixels. If
148
+ align_corners=0, they are instead considered as referring to the corner
149
+ points of the input's corner pixels, making the sampling more resolution
150
+ agnostic.
151
+
152
+ mode: Three interpolation modes: bilinear (default), nearest and bicubic.
153
+
154
+ padding_mode: Support padding modes for outside grid values:
155
+ `zeros`(default), `border`, `reflection`. zeros: use 0 for out-of-bound
156
+ grid locations, border: use border values for out-of-bound grid
157
+ locations, reflection: use values at locations reflected by the border
158
+ for out-of-bound grid locations. If index 0 represents the margin pixel,
159
+ the reflected value at index -1 will be the same as the value at index
160
+ 1. For location far away from the border, it will keep being reflected
161
+ until becoming in bound. If pixel location x = -3.5 reflects by border
162
+ -1 and becomes x' = 1.5, then reflects by border 1 and becomes x'' =
163
+ 0.5.
164
+ """
165
+
166
+ schema = get_schema("GridSample", 16, "")
167
+ op = Op(self, "GridSample", schema)
168
+ return op(
169
+ *self._prepare_inputs(schema, X, grid),
170
+ align_corners=align_corners,
171
+ mode=mode,
172
+ padding_mode=padding_mode,
173
+ )
174
+
175
+ V_Identity = TypeVar(
176
+ "V_Identity",
177
+ Optional[Sequence[BOOL]],
178
+ Optional[Sequence[COMPLEX128]],
179
+ Optional[Sequence[COMPLEX64]],
180
+ Optional[Sequence[DOUBLE]],
181
+ Optional[Sequence[FLOAT]],
182
+ Optional[Sequence[FLOAT16]],
183
+ Optional[Sequence[INT16]],
184
+ Optional[Sequence[INT32]],
185
+ Optional[Sequence[INT64]],
186
+ Optional[Sequence[INT8]],
187
+ Optional[Sequence[STRING]],
188
+ Optional[Sequence[UINT16]],
189
+ Optional[Sequence[UINT32]],
190
+ Optional[Sequence[UINT64]],
191
+ Optional[Sequence[UINT8]],
192
+ Optional[BOOL],
193
+ Optional[COMPLEX128],
194
+ Optional[COMPLEX64],
195
+ Optional[DOUBLE],
196
+ Optional[FLOAT],
197
+ Optional[FLOAT16],
198
+ Optional[INT16],
199
+ Optional[INT32],
200
+ Optional[INT64],
201
+ Optional[INT8],
202
+ Optional[STRING],
203
+ Optional[UINT16],
204
+ Optional[UINT32],
205
+ Optional[UINT64],
206
+ Optional[UINT8],
207
+ Sequence[BOOL],
208
+ Sequence[COMPLEX128],
209
+ Sequence[COMPLEX64],
210
+ Sequence[DOUBLE],
211
+ Sequence[FLOAT],
212
+ Sequence[FLOAT16],
213
+ Sequence[INT16],
214
+ Sequence[INT32],
215
+ Sequence[INT64],
216
+ Sequence[INT8],
217
+ Sequence[STRING],
218
+ Sequence[UINT16],
219
+ Sequence[UINT32],
220
+ Sequence[UINT64],
221
+ Sequence[UINT8],
222
+ BFLOAT16,
223
+ BOOL,
224
+ COMPLEX128,
225
+ COMPLEX64,
226
+ DOUBLE,
227
+ FLOAT,
228
+ FLOAT16,
229
+ INT16,
230
+ INT32,
231
+ INT64,
232
+ INT8,
233
+ STRING,
234
+ UINT16,
235
+ UINT32,
236
+ UINT64,
237
+ UINT8,
238
+ )
239
+
240
+ def Identity(self, input: V_Identity) -> V_Identity:
241
+ r"""[🌐 Identity(16)](https://onnx.ai/onnx/operators/onnx__Identity.html#identity-16 "Online Documentation")
242
+
243
+ Identity operator
244
+
245
+ Args:
246
+ input: (differentiable) Input tensor
247
+ """
248
+
249
+ schema = get_schema("Identity", 16, "")
250
+ op = Op(self, "Identity", schema)
251
+ return op(*self._prepare_inputs(schema, input))
252
+
253
+ B_If: TypeAlias = BOOL
254
+
255
+ V_If: TypeAlias = Union[
256
+ Optional[Sequence[BFLOAT16]],
257
+ Optional[Sequence[BOOL]],
258
+ Optional[Sequence[COMPLEX128]],
259
+ Optional[Sequence[COMPLEX64]],
260
+ Optional[Sequence[DOUBLE]],
261
+ Optional[Sequence[FLOAT]],
262
+ Optional[Sequence[FLOAT16]],
263
+ Optional[Sequence[INT16]],
264
+ Optional[Sequence[INT32]],
265
+ Optional[Sequence[INT64]],
266
+ Optional[Sequence[INT8]],
267
+ Optional[Sequence[STRING]],
268
+ Optional[Sequence[UINT16]],
269
+ Optional[Sequence[UINT32]],
270
+ Optional[Sequence[UINT64]],
271
+ Optional[Sequence[UINT8]],
272
+ Optional[BFLOAT16],
273
+ Optional[BOOL],
274
+ Optional[COMPLEX128],
275
+ Optional[COMPLEX64],
276
+ Optional[DOUBLE],
277
+ Optional[FLOAT],
278
+ Optional[FLOAT16],
279
+ Optional[INT16],
280
+ Optional[INT32],
281
+ Optional[INT64],
282
+ Optional[INT8],
283
+ Optional[STRING],
284
+ Optional[UINT16],
285
+ Optional[UINT32],
286
+ Optional[UINT64],
287
+ Optional[UINT8],
288
+ Sequence[BFLOAT16],
289
+ Sequence[BOOL],
290
+ Sequence[COMPLEX128],
291
+ Sequence[COMPLEX64],
292
+ Sequence[DOUBLE],
293
+ Sequence[FLOAT],
294
+ Sequence[FLOAT16],
295
+ Sequence[INT16],
296
+ Sequence[INT32],
297
+ Sequence[INT64],
298
+ Sequence[INT8],
299
+ Sequence[STRING],
300
+ Sequence[UINT16],
301
+ Sequence[UINT32],
302
+ Sequence[UINT64],
303
+ Sequence[UINT8],
304
+ BFLOAT16,
305
+ BOOL,
306
+ COMPLEX128,
307
+ COMPLEX64,
308
+ DOUBLE,
309
+ FLOAT,
310
+ FLOAT16,
311
+ INT16,
312
+ INT32,
313
+ INT64,
314
+ INT8,
315
+ STRING,
316
+ UINT16,
317
+ UINT32,
318
+ UINT64,
319
+ UINT8,
320
+ ]
321
+
322
+ def If(self, cond: B_If, *, else_branch: GraphProto, then_branch: GraphProto) -> V_If:
323
+ r"""[🌐 If(16)](https://onnx.ai/onnx/operators/onnx__If.html#if-16 "Online Documentation")
324
+
325
+ If conditional
326
+
327
+ Args:
328
+ cond: Condition for the if. The tensor must contain a single element.
329
+
330
+ else_branch: Graph to run if condition is false. Has N outputs: values you
331
+ wish to be live-out to the enclosing scope. The number of outputs must
332
+ match the number of outputs in the then_branch.
333
+
334
+ then_branch: Graph to run if condition is true. Has N outputs: values you
335
+ wish to be live-out to the enclosing scope. The number of outputs must
336
+ match the number of outputs in the else_branch.
337
+ """
338
+
339
+ schema = get_schema("If", 16, "")
340
+ op = Op(self, "If", schema)
341
+ return op(
342
+ *self._prepare_inputs(schema, cond),
343
+ else_branch=else_branch,
344
+ then_branch=then_branch,
345
+ )
346
+
347
+ T_LeakyRelu = TypeVar("T_LeakyRelu", BFLOAT16, DOUBLE, FLOAT, FLOAT16)
348
+
349
+ def LeakyRelu(self, X: T_LeakyRelu, *, alpha: float = 0.009999999776482582) -> T_LeakyRelu:
350
+ r"""[🌐 LeakyRelu(16)](https://onnx.ai/onnx/operators/onnx__LeakyRelu.html#leakyrelu-16 "Online Documentation")
351
+
352
+
353
+ LeakyRelu takes input data (Tensor<T>) and an argument alpha, and produces one
354
+ output data (Tensor<T>) where the function `f(x) = alpha * x for x < 0`,
355
+ `f(x) = x for x >= 0`, is applied to the data tensor elementwise.
356
+
357
+
358
+ Args:
359
+ X: (differentiable) Input tensor
360
+
361
+ alpha: Coefficient of leakage.
362
+ """
363
+
364
+ schema = get_schema("LeakyRelu", 16, "")
365
+ op = Op(self, "LeakyRelu", schema)
366
+ return op(*self._prepare_inputs(schema, X), alpha=alpha)
367
+
368
+ T_LessOrEqual = TypeVar(
369
+ "T_LessOrEqual",
370
+ BFLOAT16,
371
+ DOUBLE,
372
+ FLOAT,
373
+ FLOAT16,
374
+ INT16,
375
+ INT32,
376
+ INT64,
377
+ INT8,
378
+ UINT16,
379
+ UINT32,
380
+ UINT64,
381
+ UINT8,
382
+ )
383
+
384
+ T1_LessOrEqual: TypeAlias = BOOL
385
+
386
+ def LessOrEqual(self, A: T_LessOrEqual, B: T_LessOrEqual) -> T1_LessOrEqual:
387
+ r"""[🌐 LessOrEqual(16)](https://onnx.ai/onnx/operators/onnx__LessOrEqual.html#lessorequal-16 "Online Documentation")
388
+
389
+
390
+ Returns the tensor resulted from performing the `less_equal` logical operation
391
+ elementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).
392
+
393
+ This operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check `Broadcasting in ONNX <https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md>`_.
394
+
395
+
396
+ Args:
397
+ A: (non-differentiable) First input operand for the logical operator.
398
+
399
+ B: (non-differentiable) Second input operand for the logical operator.
400
+ """
401
+
402
+ schema = get_schema("LessOrEqual", 16, "")
403
+ op = Op(self, "LessOrEqual", schema)
404
+ return op(*self._prepare_inputs(schema, A, B))
405
+
406
+ I_Loop: TypeAlias = INT64
407
+
408
+ B_Loop: TypeAlias = BOOL
409
+
410
+ V_Loop = TypeVar(
411
+ "V_Loop",
412
+ Optional[Sequence[BFLOAT16]],
413
+ Optional[Sequence[BOOL]],
414
+ Optional[Sequence[COMPLEX128]],
415
+ Optional[Sequence[COMPLEX64]],
416
+ Optional[Sequence[DOUBLE]],
417
+ Optional[Sequence[FLOAT]],
418
+ Optional[Sequence[FLOAT16]],
419
+ Optional[Sequence[INT16]],
420
+ Optional[Sequence[INT32]],
421
+ Optional[Sequence[INT64]],
422
+ Optional[Sequence[INT8]],
423
+ Optional[Sequence[STRING]],
424
+ Optional[Sequence[UINT16]],
425
+ Optional[Sequence[UINT32]],
426
+ Optional[Sequence[UINT64]],
427
+ Optional[Sequence[UINT8]],
428
+ Optional[BFLOAT16],
429
+ Optional[BOOL],
430
+ Optional[COMPLEX128],
431
+ Optional[COMPLEX64],
432
+ Optional[DOUBLE],
433
+ Optional[FLOAT],
434
+ Optional[FLOAT16],
435
+ Optional[INT16],
436
+ Optional[INT32],
437
+ Optional[INT64],
438
+ Optional[INT8],
439
+ Optional[STRING],
440
+ Optional[UINT16],
441
+ Optional[UINT32],
442
+ Optional[UINT64],
443
+ Optional[UINT8],
444
+ Sequence[BFLOAT16],
445
+ Sequence[BOOL],
446
+ Sequence[COMPLEX128],
447
+ Sequence[COMPLEX64],
448
+ Sequence[DOUBLE],
449
+ Sequence[FLOAT],
450
+ Sequence[FLOAT16],
451
+ Sequence[INT16],
452
+ Sequence[INT32],
453
+ Sequence[INT64],
454
+ Sequence[INT8],
455
+ Sequence[STRING],
456
+ Sequence[UINT16],
457
+ Sequence[UINT32],
458
+ Sequence[UINT64],
459
+ Sequence[UINT8],
460
+ BFLOAT16,
461
+ BOOL,
462
+ COMPLEX128,
463
+ COMPLEX64,
464
+ DOUBLE,
465
+ FLOAT,
466
+ FLOAT16,
467
+ INT16,
468
+ INT32,
469
+ INT64,
470
+ INT8,
471
+ STRING,
472
+ UINT16,
473
+ UINT32,
474
+ UINT64,
475
+ UINT8,
476
+ )
477
+
478
+ def Loop(
479
+ self, M: Optional[I_Loop], cond: Optional[B_Loop], *v_initial: V_Loop, body: GraphProto
480
+ ) -> V_Loop:
481
+ r"""[🌐 Loop(16)](https://onnx.ai/onnx/operators/onnx__Loop.html#loop-16 "Online Documentation")
482
+
483
+
484
+ Generic Looping construct. This loop has multiple termination conditions:
485
+
486
+ 1) Trip count. Iteration count specified at runtime. Set by
487
+ specifying the input M. Optional. Set to empty string to omit.
488
+ Note that a static trip count (specified at graph construction time) can be
489
+ specified by passing in a constant node for input M.
490
+ 2) Loop termination condition. This is an input to the op that determines
491
+ whether to run the first iteration and also a loop-carried dependency for
492
+ the body graph. The body graph must yield a value for the condition variable,
493
+ whether this input is provided or not.
494
+
495
+ This table summarizes the operating modes of this operator with equivalent
496
+ C-style code:
497
+
498
+ Operator inputs defined as (max_trip_count, condition_var).
499
+
500
+ * input ("", ""):
501
+ for (int i=0; ; ++i) {
502
+ cond = ... // Note this value is ignored, but is required in the body
503
+ }
504
+
505
+ * input ("", cond) // Note this is analogous to a while loop
506
+ bool cond = ...;
507
+ for (int i=0; cond; ++i) {
508
+ cond = ...;
509
+ }
510
+
511
+ * input ("", 1) // Note this is analogous to a do-while loop
512
+ bool cond = true
513
+ for (int i=0; cond; ++i) {
514
+ cond = ...;
515
+ }
516
+
517
+ * input (trip_count, "") // Note this is analogous to a for loop
518
+ int trip_count = ...
519
+ for (int i=0; i < trip_count; ++i) {
520
+ cond = ...; // ignored
521
+ }
522
+
523
+ * input (trip_count, cond)
524
+ int trip_count = ...;
525
+ bool cond = ...;
526
+ for (int i=0; i < trip_count && cond; ++i) {
527
+ cond = ...;
528
+ }
529
+
530
+
531
+ *Sample usage - cond as well as trip count*
532
+
533
+ graph predict-net {
534
+ %a = Constant[value = <Scalar Tensor [3]>]()
535
+ %b = Constant[value = <Scalar Tensor [6]>]()
536
+ %keepgoing = Constant[value = <Scalar Tensor [1]>]()
537
+ %max_trip_count = Constant[value = <Scalar Tensor [10]>]()
538
+ %keepgoing_out, %b_out, %user_defined_vals = Loop[body = <graph body-net>](%max_trip_count, %keepgoing, %b)
539
+ return
540
+ }
541
+
542
+ graph body-net (
543
+ %i[INT32, scalar] // iteration number
544
+ %keepgoing_in[BOOL, scalar] // incoming loop-termination-condition; not used
545
+ %b_in[INT32, scalar] // incoming value of loop-carried-dependency b
546
+ ) {
547
+ %my_local = Add(%a, %b_in)
548
+ %b_out = Sub(%a, %b_in) // outgoing value of loop-carried-dependency b
549
+ %keepgoing_out = Greater(%my_local, %b_out) // outgoing loop-termination-condition
550
+ %user_defined_val = Add(%b_in, %b_in) // scan-output value to be accumulated
551
+ return %keepgoing_out, %b_out, %user_defined_val
552
+ }
553
+
554
+ *Sample equivalent C code*
555
+
556
+ {
557
+ /* User-defined code (enclosing scope) */
558
+ int a = 3, b = 6;
559
+ bool keepgoing = true; // Analogous to input cond
560
+ /* End user-defined code */
561
+
562
+ /* Implicitly-defined code */
563
+ const int max_trip_count = 10; // Analogous to input M
564
+ int user_defined_vals[]; // Imagine this is resizable
565
+ /* End implicitly-defined code */
566
+ /* initialize loop-carried variables and scan-output variables */
567
+ bool keepgoing_out = keepgoing
568
+ int b_out = b
569
+
570
+ for (int i=0; i < max_trip_count && keepgoing_out; ++i) {
571
+ /* Implicitly-defined code: bind actual parameter values
572
+ to formal parameter variables of loop-body */
573
+ bool keepgoing_in = keepgoing_out;
574
+ bool b_in = b_out;
575
+
576
+ /* User-defined code (loop body) */
577
+ int my_local = a + b_in; // Reading value "a" from the enclosing scope is fine
578
+ b_out = a - b_in;
579
+ keepgoing_out = my_local > b_out;
580
+ user_defined_val = b_in + b_in; // b_in and b_out are different variables
581
+ /* End user-defined code */
582
+
583
+ /* Implicitly defined-code */
584
+ user_defined_vals[i] = user_defined_val // accumulate scan-output values
585
+ }
586
+ // int t = my_local; // Can't do this. my_local is not accessible here.
587
+
588
+ // The values below are bound to the output variables of the loop and therefore accessible
589
+ // b_out; user_defined_vals; keepgoing_out;
590
+ }
591
+
592
+ There are several things of note in this code snippet:
593
+
594
+ 1) Values from the enclosing scope (i.e. variable "a" here) are in scope and can
595
+ be referenced in the inputs of the loop.
596
+ 2) Any values computed in the loop body that needs to be used in a subsequent
597
+ iteration or after the loop are modelled using a pair of variables in the loop-body,
598
+ consisting of an input variable (eg., b_in) and an output variable (eg., b_out).
599
+ These are referred to as loop-carried dependences. The loop operation node
600
+ supplies the input value of the input variable for the first iteration, and
601
+ returns the output value of the output variable produced by the final
602
+ iteration.
603
+ 3) Scan_output variables are used to implicitly concatenate values computed across
604
+ all the iterations. In the above example, the value of user_defined_val computed
605
+ over all iterations are concatenated and returned as the value of user_defined_vals
606
+ after the loop.
607
+ 4) Values created in the body cannot be accessed in the enclosing scope,
608
+ except using the mechanism described above.
609
+
610
+ Note that the semantics of this op support "diagonal" or "wavefront" execution.
611
+ (See Step 3 here for an example:
612
+ https://devblogs.nvidia.com/optimizing-recurrent-neural-networks-cudnn-5/).
613
+ Frontends should emit multi-layer RNNs as a series of While operators (with
614
+ time being the inner looping dimension), with each successive layer consuming
615
+ the scan_outputs from the previous layer, possibly going through several
616
+ point-wise operators (e.g. dropout, residual connections, linear layer).
617
+
618
+ 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.
619
+
620
+
621
+ Args:
622
+ M: (optional) A maximum trip-count for the loop specified at runtime.
623
+ Optional. Pass empty string to skip.
624
+
625
+ cond: (optional) A boolean termination condition. Optional. Pass empty
626
+ string to skip.
627
+
628
+ v_initial: (variadic, heterogeneous) The initial values of any loop-carried
629
+ dependencies (values that change across loop iterations)
630
+
631
+ body: The graph run each iteration. It has 2+N inputs: (iteration_num,
632
+ condition, loop carried dependencies...). It has 1+N+K outputs:
633
+ (condition, loop carried dependencies..., scan_outputs...). Each
634
+ scan_output is created by concatenating the value of the specified
635
+ output value at the end of each iteration of the loop. It is an error if
636
+ the dimensions or data type of these scan_outputs change across loop
637
+ iterations.
638
+ """
639
+
640
+ schema = get_schema("Loop", 16, "")
641
+ op = Op(self, "Loop", schema)
642
+ return op(*self._prepare_inputs(schema, M, cond, *v_initial), body=body)
643
+
644
+ T_PRelu = TypeVar(
645
+ "T_PRelu", BFLOAT16, DOUBLE, FLOAT, FLOAT16, INT32, INT64, UINT32, UINT64
646
+ )
647
+
648
+ def PRelu(self, X: T_PRelu, slope: T_PRelu) -> T_PRelu:
649
+ r"""[🌐 PRelu(16)](https://onnx.ai/onnx/operators/onnx__PRelu.html#prelu-16 "Online Documentation")
650
+
651
+
652
+ PRelu takes input data (Tensor<T>) and slope tensor as input, and produces one
653
+ output data (Tensor<T>) where the function `f(x) = slope * x for x < 0`,
654
+ `f(x) = x for x >= 0`., is applied to the data tensor elementwise.
655
+ This operator supports **unidirectional broadcasting** (tensor slope should be unidirectional broadcastable to input tensor X); for more details please check `Broadcasting in ONNX <https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md>`_.
656
+
657
+ Args:
658
+ X: (differentiable) Input tensor
659
+
660
+ slope: (differentiable) Slope tensor. The shape of slope can be smaller than
661
+ first input X; if so, its shape must be unidirectional broadcastable to
662
+ X
663
+ """
664
+
665
+ schema = get_schema("PRelu", 16, "")
666
+ op = Op(self, "PRelu", schema)
667
+ return op(*self._prepare_inputs(schema, X, slope))
668
+
669
+ T1_RoiAlign = TypeVar("T1_RoiAlign", DOUBLE, FLOAT, FLOAT16)
670
+
671
+ T2_RoiAlign: TypeAlias = INT64
672
+
673
+ def RoiAlign(
674
+ self,
675
+ X: T1_RoiAlign,
676
+ rois: T1_RoiAlign,
677
+ batch_indices: T2_RoiAlign,
678
+ *,
679
+ coordinate_transformation_mode: str = "half_pixel",
680
+ mode: str = "avg",
681
+ output_height: int = 1,
682
+ output_width: int = 1,
683
+ sampling_ratio: int = 0,
684
+ spatial_scale: float = 1.0,
685
+ ) -> T1_RoiAlign:
686
+ r"""[🌐 RoiAlign(16)](https://onnx.ai/onnx/operators/onnx__RoiAlign.html#roialign-16 "Online Documentation")
687
+
688
+
689
+ Region of Interest (RoI) align operation described in the
690
+ [Mask R-CNN paper](https://arxiv.org/abs/1703.06870).
691
+ RoiAlign consumes an input tensor X and region of interests (rois)
692
+ to apply pooling across each RoI; it produces a 4-D tensor of shape
693
+ (num_rois, C, output_height, output_width).
694
+
695
+ RoiAlign is proposed to avoid the misalignment by removing
696
+ quantizations while converting from original image into feature
697
+ map and from feature map into RoI feature; in each ROI bin,
698
+ the value of the sampled locations are computed directly
699
+ through bilinear interpolation.
700
+
701
+
702
+ Args:
703
+ X: Input data tensor from the previous operator; 4-D feature map of shape
704
+ (N, C, H, W), where N is the batch size, C is the number of channels,
705
+ and H and W are the height and the width of the data.
706
+
707
+ rois: RoIs (Regions of Interest) to pool over; rois is 2-D input of shape
708
+ (num_rois, 4) given as [[x1, y1, x2, y2], ...]. The RoIs' coordinates
709
+ are in the coordinate system of the input image. Each coordinate set has
710
+ a 1:1 correspondence with the 'batch_indices' input.
711
+
712
+ batch_indices: 1-D tensor of shape (num_rois,) with each element denoting
713
+ the index of the corresponding image in the batch.
714
+
715
+ coordinate_transformation_mode: Allowed values are 'half_pixel' and
716
+ 'output_half_pixel'. Use the value 'half_pixel' to pixel shift the input
717
+ coordinates by -0.5 (the recommended behavior). Use the value
718
+ 'output_half_pixel' to omit the pixel shift for the input (use this for
719
+ a backward-compatible behavior).
720
+
721
+ mode: The pooling method. Two modes are supported: 'avg' and 'max'. Default
722
+ is 'avg'.
723
+
724
+ output_height: default 1; Pooled output Y's height.
725
+
726
+ output_width: default 1; Pooled output Y's width.
727
+
728
+ sampling_ratio: Number of sampling points in the interpolation grid used to
729
+ compute the output value of each pooled output bin. If > 0, then exactly
730
+ sampling_ratio x sampling_ratio grid points are used. If == 0, then an
731
+ adaptive number of grid points are used (computed as ceil(roi_width /
732
+ output_width), and likewise for height). Default is 0.
733
+
734
+ spatial_scale: Multiplicative spatial scale factor to translate ROI
735
+ coordinates from their input spatial scale to the scale used when
736
+ pooling, i.e., spatial scale of the input feature map X relative to the
737
+ input image. E.g.; default is 1.0f.
738
+ """
739
+
740
+ schema = get_schema("RoiAlign", 16, "")
741
+ op = Op(self, "RoiAlign", schema)
742
+ return op(
743
+ *self._prepare_inputs(schema, X, rois, batch_indices),
744
+ coordinate_transformation_mode=coordinate_transformation_mode,
745
+ mode=mode,
746
+ output_height=output_height,
747
+ output_width=output_width,
748
+ sampling_ratio=sampling_ratio,
749
+ spatial_scale=spatial_scale,
750
+ )
751
+
752
+ V_Scan = TypeVar(
753
+ "V_Scan",
754
+ BFLOAT16,
755
+ BOOL,
756
+ COMPLEX128,
757
+ COMPLEX64,
758
+ DOUBLE,
759
+ FLOAT,
760
+ FLOAT16,
761
+ INT16,
762
+ INT32,
763
+ INT64,
764
+ INT8,
765
+ STRING,
766
+ UINT16,
767
+ UINT32,
768
+ UINT64,
769
+ UINT8,
770
+ )
771
+
772
+ def Scan(
773
+ self,
774
+ *initial_state_and_scan_inputs: V_Scan,
775
+ body: GraphProto,
776
+ num_scan_inputs: int,
777
+ scan_input_axes: Optional[Sequence[int]] = None,
778
+ scan_input_directions: Optional[Sequence[int]] = None,
779
+ scan_output_axes: Optional[Sequence[int]] = None,
780
+ scan_output_directions: Optional[Sequence[int]] = None,
781
+ ) -> V_Scan:
782
+ r"""[🌐 Scan(16)](https://onnx.ai/onnx/operators/onnx__Scan.html#scan-16 "Online Documentation")
783
+
784
+
785
+ Scan can be used to iterate over one or more scan_input tensors,
786
+ constructing zero or more scan_output tensors. It combines ideas from general recurrences,
787
+ functional programming constructs such as scan, fold, map, and zip, and is intended to enable
788
+ generalizations of RNN-like constructs for sequence-to-sequence processing.
789
+ Other tensors (referred to as state_variables here) can be used to carry a state
790
+ when iterating from one element to another (similar to hidden-state in RNNs, also referred
791
+ to as loop-carried dependences in the context of loops).
792
+ Many common usages involve a single scan_input tensor (where functionality
793
+ similar to scan, fold and map can be obtained). When more than one scan_input is used,
794
+ a behavior similar to zip is obtained.
795
+
796
+ The attribute body must be a graph, specifying the computation to be performed in
797
+ every iteration. It takes as input the current values of the state_variables and
798
+ the current iterated element of the scan_inputs. It must return the (updated) values
799
+ of the state_variables and zero or more scan_output_element tensors. The values of the
800
+ scan_output_element tensors are concatenated over all the iterations to produce the
801
+ scan_output values of the scan construct (similar to the concatenated intermediate
802
+ hidden-state values of RNN-like constructs). All the output tensors (state_variables as
803
+ well as scan_output_element tensors) are required to have the same shape in each iteration
804
+ of the loop (a restriction imposed to enable efficient memory allocation).
805
+
806
+ Note that the iterated element passed to the body subgraph does not have a sequence
807
+ axis. It will have a rank one less than the rank of the corresponding scan_input.
808
+
809
+ The scan operation returns the final values of the state_variables as well as the
810
+ scan_outputs.
811
+
812
+ The optional attribute scan_input_directions specifies the direction (forward or backward)
813
+ for each scan input. If this attribute is omitted, all sequences are scanned in the forward
814
+ direction. A bidirectional scan may be performed by specifying the same tensor input twice
815
+ in the scan_inputs, once with a forward direction, and once with a backward direction.
816
+
817
+ The scan_output of the operation is produced by concatenating the scan_output_element
818
+ values produced by the body in each iteration. The optional attribute scan_output_directions
819
+ specifies the direction in which scan_output is constructed (by appending or prepending the
820
+ scan_output_element to scan_output in each iteration) for each scan_output. If this attribute
821
+ is omitted, the scan_output_element is appended to the scan_output in each iteration.
822
+
823
+ The optional attribute scan_input_axes specifies the axis to be scanned for each scan_input.
824
+ If omitted, every scan_input will be scanned in axis 0. For example, if axis 0 is the
825
+ batch axis and axis 1 is the time axis (to be scanned), specify an axis value of 1.
826
+ Note that scanning a non-zero axis may be less efficient than scanning axis zero.
827
+
828
+ The optional attribute scan_output_axes specifies the axis along which the scan_outputs
829
+ are accumulated for each scan_output. For example, if axis 1 is the time axis (to be
830
+ scanned) for both inputs and outputs, specify a scan_input axis and scan_output axis
831
+ value of 1.
832
+
833
+ Note that because of the ONNX restriction that only the last parameter of an operator can
834
+ be variadic, the initial-states and scan-inputs are listed together as one input parameter.
835
+ Similarly, the final-states and scan-outputs are listed together as one output parameter.
836
+ The attribute num_scan_inputs indicates the number M of scan-inputs.
837
+
838
+ The behavior of
839
+
840
+ Scan <
841
+ num_scan_inputs = m,
842
+ body = loop-body,
843
+ scan_input_axes = [axis_1, ..., axis_m]
844
+ > (init_1, ..., init_n, scan_1, ..., scan_m)
845
+
846
+ is equivalent to the following pseudo-code:
847
+
848
+ // scan_i.shape[axis_i] denotes the (max) sequence-length of scan_i
849
+ // scan_i.shape[axis_i] is required to be equal to scan_j.shape[axis_j] for all i,j.
850
+ sequence_length = scan_1.shape[axis_1];
851
+
852
+ // initialize state-variables
853
+ st_1 = init_1; ... st_n = init_n;
854
+ // initialize scan-output variables: [] denotes an empty tensor
855
+ scan_out_1 = []; ...; scan_out_k = [];
856
+ // identify number of iterations:
857
+
858
+ // execute loop
859
+ for (int t = 0; t < sequence_length; ++t) {
860
+ // generate the scan-input elements: the notation T<axis=k>[t] indicates the sub-tensor
861
+ // of rank one less than T obtained by indexing T at position t along axis k.
862
+ si_1 = scan_1<axis=axis_1>[t];
863
+ ... ;
864
+ si_m = scan_m<axis=axis_m>[t];
865
+ // execute loop-body
866
+ st_1, ..., st_n, so_1, ..., so_k = loop-body(st_1, ..., st_n, si_1, ..., si_m)
867
+ // accumulate the scan-output elements
868
+ scan_out_1 = Concat<axis=0>(scan_out_1, so_1); ... ; scan_out_k = Concat<axis=0>(scan_out_k, so_k);
869
+ }
870
+
871
+ return st_1, ..., st_n, scan_out_1, ..., scan_out_k;
872
+
873
+ *Sample usage: Encoding RNN using a Scan*
874
+
875
+ The following example shows how a simple RNN over an input tensor %X, with weight tensor %Wi,
876
+ recurrence weight tensor %Ri, bias tensors %Wbi and %Rbi, and initial hidden-state %H_0 can
877
+ be encoded as a ScanLoop. Note that the loop-body is a nested graph, and it directly computes
878
+ %Wi, %Ri, %Wbi, and %Rbi (typically constants or initializers in the body graph). If these
879
+ values are computed in the outer graph, they need to be passed in as extra state_variables.
880
+
881
+ graph rnn-encoding {
882
+ %H_0 = ...
883
+ %X = ...
884
+ %Y_h, %Y = Scan[body = <graph rnn-cell-1>, num_scan_inputs=1](%H_0, %X)
885
+ return %Y, %Y_h
886
+ }
887
+
888
+ graph rnn-cell-1 (
889
+ %H_tminus1[FLOAT, tensor]
890
+ %X_t[FLOAT, tensor]
891
+ ) {
892
+ %Wi = ...
893
+ %Ri = ...
894
+ %Wbi = ...
895
+ %Rbi = ...
896
+ %t1 = X_t * (Wi^T)
897
+ %t2 = H_tminus1*(Ri^T)
898
+ %t3 = Add(%t1, %t2)
899
+ %t4 = Add(%t3, %Wbi)
900
+ %t5 = Add(%t4, %Rbi)
901
+ %Ht = Tanh(%t5)
902
+ %Accumulate = Identity(%Ht)
903
+ return %Ht, %Accumulate
904
+ }
905
+
906
+
907
+
908
+ Args:
909
+ initial_state_and_scan_inputs: (variadic, heterogeneous) Initial values of
910
+ the loop's N state variables followed by M scan_inputs
911
+
912
+ body: The graph run each iteration. It has N+M inputs: (loop state
913
+ variables..., scan_input_elts...). It has N+K outputs: (loop state
914
+ variables..., scan_output_elts...). Each scan_output is created by
915
+ concatenating the value of the specified scan_output_elt value at the
916
+ end of each iteration of the loop. It is an error if the dimensions of
917
+ these values change across loop iterations.
918
+
919
+ num_scan_inputs: An attribute specifying the number of scan_inputs M.
920
+
921
+ scan_input_axes: An optional list of M flags. The i-th element of the list
922
+ specifies the axis to be scanned (the sequence axis) for the i-th
923
+ scan_input. If omitted, 0 will be used as the scan axis for every
924
+ scan_input. Negative value for an axis means counting dimensions from
925
+ the back. Accepted range is [-r, r-1] where r = rank(input).
926
+
927
+ scan_input_directions: An optional list of M flags. The i-th element of the
928
+ list specifies the direction to be scanned for the i-th scan_input
929
+ tensor: 0 indicates forward direction and 1 indicates reverse direction.
930
+ If omitted, all scan_input tensors will be scanned in the forward
931
+ direction.
932
+
933
+ scan_output_axes: An optional list of K flags. The i-th element of the list
934
+ specifies the axis for the i-th scan_output. The scan outputs are
935
+ accumulated along the specified axis. If omitted, 0 will be used as the
936
+ scan axis for every scan_output. Negative value for an axis means
937
+ counting dimensions from the back. Accepted range is [-r, r-1].
938
+
939
+ scan_output_directions: An optional list of K flags, one for each
940
+ scan_output. The i-th element of the list specifies whether the i-th
941
+ scan_output should be constructed by appending or prepending a new value
942
+ in each iteration: 0 indicates appending and 1 indicates prepending. If
943
+ omitted, all scan_output tensors will be produced by appending a value
944
+ in each iteration.
945
+ """
946
+
947
+ schema = get_schema("Scan", 16, "")
948
+ op = Op(self, "Scan", schema)
949
+ return op(
950
+ *self._prepare_inputs(schema, *initial_state_and_scan_inputs),
951
+ body=body,
952
+ num_scan_inputs=num_scan_inputs,
953
+ scan_input_axes=scan_input_axes,
954
+ scan_input_directions=scan_input_directions,
955
+ scan_output_axes=scan_output_axes,
956
+ scan_output_directions=scan_output_directions,
957
+ )
958
+
959
+ T_ScatterElements = TypeVar(
960
+ "T_ScatterElements",
961
+ BFLOAT16,
962
+ BOOL,
963
+ COMPLEX128,
964
+ COMPLEX64,
965
+ DOUBLE,
966
+ FLOAT,
967
+ FLOAT16,
968
+ INT16,
969
+ INT32,
970
+ INT64,
971
+ INT8,
972
+ STRING,
973
+ UINT16,
974
+ UINT32,
975
+ UINT64,
976
+ UINT8,
977
+ )
978
+
979
+ Tind_ScatterElements = TypeVar("Tind_ScatterElements", INT32, INT64)
980
+
981
+ def ScatterElements(
982
+ self,
983
+ data: T_ScatterElements,
984
+ indices: Tind_ScatterElements,
985
+ updates: T_ScatterElements,
986
+ *,
987
+ axis: int = 0,
988
+ reduction: str = "none",
989
+ ) -> T_ScatterElements:
990
+ r"""[🌐 ScatterElements(16)](https://onnx.ai/onnx/operators/onnx__ScatterElements.html#scatterelements-16 "Online Documentation")
991
+
992
+
993
+ ScatterElements takes three inputs `data`, `updates`, and `indices` of the same
994
+ rank r >= 1 and an optional attribute axis that identifies an axis of `data`
995
+ (by default, the outer-most axis, that is axis 0). The output of the operation
996
+ is produced by creating a copy of the input `data`, and then updating its value
997
+ to values specified by `updates` at specific index positions specified by
998
+ `indices`. Its output shape is the same as the shape of `data`.
999
+ For each entry in `updates`, the target index in `data` is obtained by combining
1000
+ the corresponding entry in `indices` with the index of the entry itself: the
1001
+ index-value for dimension = axis is obtained from the value of the corresponding
1002
+ entry in `indices` and the index-value for dimension != axis is obtained from the
1003
+ index of the entry itself.
1004
+ `reduction` allows specification of an optional reduction operation, which is applied to all values in `updates`
1005
+ tensor into `output` at the specified `indices`.
1006
+ In cases where `reduction` is set to "none", indices should not have duplicate entries: that is, if idx1 != idx2,
1007
+ then indices[idx1] != indices[idx2]. For instance, in a 2-D tensor case, the update
1008
+ corresponding to the [i][j] entry is performed as below:
1009
+ ::
1010
+
1011
+ output[indices[i][j]][j] = updates[i][j] if axis = 0,
1012
+ output[i][indices[i][j]] = updates[i][j] if axis = 1,
1013
+
1014
+
1015
+ When `reduction` is set to "add", the update corresponding to the [i][j] entry is performed as below:
1016
+ ::
1017
+
1018
+ output[indices[i][j]][j] += updates[i][j] if axis = 0,
1019
+ output[i][indices[i][j]] += updates[i][j] if axis = 1,
1020
+
1021
+
1022
+ When `reduction` is set to "mul", the update corresponding to the [i][j] entry is performed as below:
1023
+ ::
1024
+
1025
+ output[indices[i][j]][j] *= updates[i][j] if axis = 0,
1026
+ output[i][indices[i][j]] *= updates[i][j] if axis = 1,
1027
+
1028
+
1029
+ This operator is the inverse of GatherElements. It is similar to Torch's Scatter operation.
1030
+ Example 1:
1031
+ ::
1032
+
1033
+ data = [
1034
+ [0.0, 0.0, 0.0],
1035
+ [0.0, 0.0, 0.0],
1036
+ [0.0, 0.0, 0.0],
1037
+ ]
1038
+ indices = [
1039
+ [1, 0, 2],
1040
+ [0, 2, 1],
1041
+ ]
1042
+ updates = [
1043
+ [1.0, 1.1, 1.2],
1044
+ [2.0, 2.1, 2.2],
1045
+ ]
1046
+ output = [
1047
+ [2.0, 1.1, 0.0]
1048
+ [1.0, 0.0, 2.2]
1049
+ [0.0, 2.1, 1.2]
1050
+ ]
1051
+
1052
+
1053
+ Example 2:
1054
+ ::
1055
+
1056
+ data = [[1.0, 2.0, 3.0, 4.0, 5.0]]
1057
+ indices = [[1, 3]]
1058
+ updates = [[1.1, 2.1]]
1059
+ axis = 1
1060
+ output = [[1.0, 1.1, 3.0, 2.1, 5.0]]
1061
+
1062
+
1063
+
1064
+
1065
+ Args:
1066
+ data: (differentiable) Tensor of rank r >= 1.
1067
+
1068
+ indices: (non-differentiable) Tensor of int32/int64 indices, of r >= 1 (same
1069
+ rank as input). All index values are expected to be within bounds [-s,
1070
+ s-1] along axis of size s. It is an error if any of the index values are
1071
+ out of bounds.
1072
+
1073
+ updates: (differentiable) Tensor of rank r >=1 (same rank and shape as
1074
+ indices)
1075
+
1076
+ axis: Which axis to scatter on. Negative value means counting dimensions
1077
+ from the back. Accepted range is [-r, r-1] where r = rank(data).
1078
+
1079
+ reduction: Type of reduction to apply: none (default), add, mul. 'none': no
1080
+ reduction applied. 'add': reduction using the addition operation.
1081
+ 'mul': reduction using the multiplication operation.
1082
+ """
1083
+
1084
+ schema = get_schema("ScatterElements", 16, "")
1085
+ op = Op(self, "ScatterElements", schema)
1086
+ return op(
1087
+ *self._prepare_inputs(schema, data, indices, updates),
1088
+ axis=axis,
1089
+ reduction=reduction,
1090
+ )
1091
+
1092
+ T_ScatterND = TypeVar(
1093
+ "T_ScatterND",
1094
+ BFLOAT16,
1095
+ BOOL,
1096
+ COMPLEX128,
1097
+ COMPLEX64,
1098
+ DOUBLE,
1099
+ FLOAT,
1100
+ FLOAT16,
1101
+ INT16,
1102
+ INT32,
1103
+ INT64,
1104
+ INT8,
1105
+ STRING,
1106
+ UINT16,
1107
+ UINT32,
1108
+ UINT64,
1109
+ UINT8,
1110
+ )
1111
+
1112
+ def ScatterND(
1113
+ self,
1114
+ data: T_ScatterND,
1115
+ indices: INT64,
1116
+ updates: T_ScatterND,
1117
+ *,
1118
+ reduction: str = "none",
1119
+ ) -> T_ScatterND:
1120
+ r"""[🌐 ScatterND(16)](https://onnx.ai/onnx/operators/onnx__ScatterND.html#scatternd-16 "Online Documentation")
1121
+
1122
+
1123
+ ScatterND takes three inputs `data` tensor of rank r >= 1, `indices` tensor of rank q >= 1,
1124
+ and `updates` tensor of rank q + r - indices.shape[-1] - 1. The output of the operation
1125
+ is produced by creating a copy of the input `data`, and then updating its value to values
1126
+ specified by `updates` at specific index positions specified by `indices`. Its output shape
1127
+ is the same as the shape of `data`.
1128
+
1129
+ `indices` is an integer tensor. Let k denote indices.shape[-1], the last dimension in the shape of `indices`.
1130
+ `indices` is treated as a (q-1)-dimensional tensor of k-tuples, where each k-tuple is a partial-index into `data`.
1131
+ Hence, k can be a value at most the rank of `data`. When k equals rank(data), each update entry specifies an
1132
+ update to a single element of the tensor. When k is less than rank(data) each update entry specifies an
1133
+ update to a slice of the tensor. Index values are allowed to be negative, as per the usual
1134
+ convention for counting backwards from the end, but are expected in the valid range.
1135
+
1136
+ `updates` is treated as a (q-1)-dimensional tensor of replacement-slice-values. Thus, the
1137
+ first (q-1) dimensions of updates.shape must match the first (q-1) dimensions of indices.shape.
1138
+ The remaining dimensions of `updates` correspond to the dimensions of the
1139
+ replacement-slice-values. Each replacement-slice-value is a (r-k) dimensional tensor,
1140
+ corresponding to the trailing (r-k) dimensions of `data`. Thus, the shape of `updates`
1141
+ must equal indices.shape[0:q-1] ++ data.shape[k:r-1], where ++ denotes the concatenation
1142
+ of shapes.
1143
+
1144
+ The `output` is calculated via the following equation:
1145
+ output = np.copy(data)
1146
+ update_indices = indices.shape[:-1]
1147
+ for idx in np.ndindex(update_indices):
1148
+ output[indices[idx]] = updates[idx]
1149
+ The order of iteration in the above loop is not specified.
1150
+ In particular, indices should not have duplicate entries: that is, if idx1 != idx2, then indices[idx1] != indices[idx2].
1151
+ This ensures that the output value does not depend on the iteration order.
1152
+
1153
+ `reduction` allows specification of an optional reduction operation, which is applied to all values in `updates`
1154
+ tensor into `output` at the specified `indices`.
1155
+ In cases where `reduction` is set to "none", indices should not have duplicate entries: that is, if idx1 != idx2,
1156
+ then indices[idx1] != indices[idx2]. This ensures that the output value does not depend on the iteration order.
1157
+ When `reduction` is set to "add", `output` is calculated as follows:
1158
+ output = np.copy(data)
1159
+ update_indices = indices.shape[:-1]
1160
+ for idx in np.ndindex(update_indices):
1161
+ output[indices[idx]] += updates[idx]
1162
+ When `reduction` is set to "mul", `output` is calculated as follows:
1163
+ output = np.copy(data)
1164
+ update_indices = indices.shape[:-1]
1165
+ for idx in np.ndindex(update_indices):
1166
+ output[indices[idx]] *= updates[idx]
1167
+ This operator is the inverse of GatherND.
1168
+ Example 1:
1169
+ ::
1170
+
1171
+ data = [1, 2, 3, 4, 5, 6, 7, 8]
1172
+ indices = [[4], [3], [1], [7]]
1173
+ updates = [9, 10, 11, 12]
1174
+ output = [1, 11, 3, 10, 9, 6, 7, 12]
1175
+
1176
+
1177
+ Example 2:
1178
+ ::
1179
+
1180
+ data = [[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],
1181
+ [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],
1182
+ [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],
1183
+ [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]]
1184
+ indices = [[0], [2]]
1185
+ updates = [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],
1186
+ [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]]
1187
+ output = [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],
1188
+ [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],
1189
+ [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],
1190
+ [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]]
1191
+
1192
+
1193
+
1194
+
1195
+ Args:
1196
+ data: (differentiable) Tensor of rank r >= 1.
1197
+
1198
+ indices: (non-differentiable) Tensor of rank q >= 1.
1199
+
1200
+ updates: (differentiable) Tensor of rank q + r - indices_shape[-1] - 1.
1201
+
1202
+ reduction: Type of reduction to apply: none (default), add, mul. 'none': no
1203
+ reduction applied. 'add': reduction using the addition operation.
1204
+ 'mul': reduction using the multiplication operation.
1205
+ """
1206
+
1207
+ schema = get_schema("ScatterND", 16, "")
1208
+ op = Op(self, "ScatterND", schema)
1209
+ return op(*self._prepare_inputs(schema, data, indices, updates), reduction=reduction)
1210
+
1211
+ B_Where: TypeAlias = BOOL
1212
+
1213
+ T_Where = TypeVar(
1214
+ "T_Where",
1215
+ BFLOAT16,
1216
+ BOOL,
1217
+ COMPLEX128,
1218
+ COMPLEX64,
1219
+ DOUBLE,
1220
+ FLOAT,
1221
+ FLOAT16,
1222
+ INT16,
1223
+ INT32,
1224
+ INT64,
1225
+ INT8,
1226
+ STRING,
1227
+ UINT16,
1228
+ UINT32,
1229
+ UINT64,
1230
+ UINT8,
1231
+ )
1232
+
1233
+ def Where(self, condition: B_Where, X: T_Where, Y: T_Where) -> T_Where:
1234
+ r"""[🌐 Where(16)](https://onnx.ai/onnx/operators/onnx__Where.html#where-16 "Online Documentation")
1235
+
1236
+
1237
+ Return elements, either from X or Y, depending on condition.
1238
+ Where behaves like
1239
+ [numpy.where](https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html)
1240
+ with three parameters.
1241
+
1242
+ This operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check `Broadcasting in ONNX <https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md>`_.
1243
+
1244
+ Args:
1245
+ condition: (non-differentiable) When True (nonzero), yield X, otherwise
1246
+ yield Y
1247
+
1248
+ X: (differentiable) values selected at indices where condition is True
1249
+
1250
+ Y: (differentiable) values selected at indices where condition is False
1251
+ """
1252
+
1253
+ schema = get_schema("Where", 16, "")
1254
+ op = Op(self, "Where", schema)
1255
+ return op(*self._prepare_inputs(schema, condition, X, Y))