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,1078 @@
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, Tuple, TypeVar, Union
17
+
18
+ from onnx import SparseTensorProto, TensorProto
19
+ from onnx.defs import get_schema
20
+ from typing_extensions import TypeAlias
21
+
22
+ from onnxscript.onnx_opset._impl.opset11 import Opset11
23
+ from onnxscript.onnx_types import (
24
+ BOOL,
25
+ COMPLEX64,
26
+ COMPLEX128,
27
+ DOUBLE,
28
+ FLOAT,
29
+ FLOAT16,
30
+ INT8,
31
+ INT16,
32
+ INT32,
33
+ INT64,
34
+ STRING,
35
+ UINT8,
36
+ UINT16,
37
+ UINT32,
38
+ UINT64,
39
+ )
40
+ from onnxscript.values import Op, Opset
41
+
42
+
43
+ class Opset12(Opset11):
44
+ def __new__(cls):
45
+ return Opset.__new__(cls, "", 12)
46
+
47
+ T_ArgMax = TypeVar(
48
+ "T_ArgMax",
49
+ DOUBLE,
50
+ FLOAT,
51
+ FLOAT16,
52
+ INT16,
53
+ INT32,
54
+ INT64,
55
+ INT8,
56
+ UINT16,
57
+ UINT32,
58
+ UINT64,
59
+ UINT8,
60
+ )
61
+
62
+ def ArgMax(
63
+ self, data: T_ArgMax, *, axis: int = 0, keepdims: int = 1, select_last_index: int = 0
64
+ ) -> INT64:
65
+ r"""[🌐 ArgMax(12)](https://onnx.ai/onnx/operators/onnx__ArgMax.html#argmax-12 "Online Documentation")
66
+
67
+
68
+ Computes the indices of the max elements of the input tensor's element along the
69
+ provided axis. The resulting tensor has the same rank as the input if keepdims equals 1.
70
+ If keepdims equal 0, then the resulting tensor has the reduced dimension pruned.
71
+ If select_last_index is True (default False), the index of the last occurrence of the max
72
+ is selected if the max appears more than once in the input. Otherwise the index of the
73
+ first occurrence is selected.
74
+ The type of the output tensor is integer.
75
+
76
+ Args:
77
+ data: An input tensor.
78
+
79
+ axis: The axis in which to compute the arg indices. Accepted range is [-r,
80
+ r-1] where r = rank(data).
81
+
82
+ keepdims: Keep the reduced dimension or not, default 1 means keep reduced
83
+ dimension.
84
+
85
+ select_last_index: Whether to select the last index or the first index if
86
+ the {name} appears in multiple indices, default is False (first index).
87
+ """
88
+
89
+ schema = get_schema("ArgMax", 12, "")
90
+ op = Op(self, "ArgMax", schema)
91
+ return op(
92
+ *self._prepare_inputs(schema, data),
93
+ axis=axis,
94
+ keepdims=keepdims,
95
+ select_last_index=select_last_index,
96
+ )
97
+
98
+ T_ArgMin = TypeVar(
99
+ "T_ArgMin",
100
+ DOUBLE,
101
+ FLOAT,
102
+ FLOAT16,
103
+ INT16,
104
+ INT32,
105
+ INT64,
106
+ INT8,
107
+ UINT16,
108
+ UINT32,
109
+ UINT64,
110
+ UINT8,
111
+ )
112
+
113
+ def ArgMin(
114
+ self, data: T_ArgMin, *, axis: int = 0, keepdims: int = 1, select_last_index: int = 0
115
+ ) -> INT64:
116
+ r"""[🌐 ArgMin(12)](https://onnx.ai/onnx/operators/onnx__ArgMin.html#argmin-12 "Online Documentation")
117
+
118
+
119
+ Computes the indices of the min elements of the input tensor's element along the
120
+ provided axis. The resulting tensor has the same rank as the input if keepdims equals 1.
121
+ If keepdims equal 0, then the resulting tensor has the reduced dimension pruned.
122
+ If select_last_index is True (default False), the index of the last occurrence of the min
123
+ is selected if the min appears more than once in the input. Otherwise the index of the
124
+ first occurrence is selected.
125
+ The type of the output tensor is integer.
126
+
127
+ Args:
128
+ data: An input tensor.
129
+
130
+ axis: The axis in which to compute the arg indices. Accepted range is [-r,
131
+ r-1] where r = rank(data).
132
+
133
+ keepdims: Keep the reduced dimension or not, default 1 means keep reduced
134
+ dimension.
135
+
136
+ select_last_index: Whether to select the last index or the first index if
137
+ the {name} appears in multiple indices, default is False (first index).
138
+ """
139
+
140
+ schema = get_schema("ArgMin", 12, "")
141
+ op = Op(self, "ArgMin", schema)
142
+ return op(
143
+ *self._prepare_inputs(schema, data),
144
+ axis=axis,
145
+ keepdims=keepdims,
146
+ select_last_index=select_last_index,
147
+ )
148
+
149
+ T_Celu: TypeAlias = FLOAT
150
+
151
+ def Celu(self, X: T_Celu, *, alpha: float = 1.0) -> T_Celu:
152
+ r"""[🌐 Celu(12)](https://onnx.ai/onnx/operators/onnx__Celu.html#celu-12 "Online Documentation")
153
+
154
+
155
+ Continuously Differentiable Exponential Linear Units:
156
+ Perform the linear unit element-wise on the input tensor X
157
+ using formula:
158
+
159
+ ::
160
+
161
+ max(0,x) + min(0,alpha*(exp(x/alpha)-1))
162
+
163
+
164
+
165
+
166
+ Args:
167
+ X: (differentiable) Input tensor
168
+
169
+ alpha: The Alpha value in Celu formula which control the shape of the unit.
170
+ The default value is 1.0.
171
+ """
172
+
173
+ schema = get_schema("Celu", 12, "")
174
+ op = Op(self, "Celu", schema)
175
+ return op(*self._prepare_inputs(schema, X), alpha=alpha)
176
+
177
+ T_Clip = TypeVar(
178
+ "T_Clip",
179
+ DOUBLE,
180
+ FLOAT,
181
+ FLOAT16,
182
+ INT16,
183
+ INT32,
184
+ INT64,
185
+ INT8,
186
+ UINT16,
187
+ UINT32,
188
+ UINT64,
189
+ UINT8,
190
+ )
191
+
192
+ def Clip(
193
+ self, input: T_Clip, min: Optional[T_Clip] = None, max: Optional[T_Clip] = None
194
+ ) -> T_Clip:
195
+ r"""[🌐 Clip(12)](https://onnx.ai/onnx/operators/onnx__Clip.html#clip-12 "Online Documentation")
196
+
197
+
198
+ Clip operator limits the given input within an interval. The interval is
199
+ specified by the inputs 'min' and 'max'. They default to
200
+ numeric_limits::lowest() and numeric_limits::max(), respectively.
201
+
202
+
203
+ Args:
204
+ input: Input tensor whose elements to be clipped
205
+
206
+ min: (optional) Minimum value, under which element is replaced by min. It
207
+ must be a scalar(tensor of empty shape).
208
+
209
+ max: (optional) Maximum value, above which element is replaced by max. It
210
+ must be a scalar(tensor of empty shape).
211
+ """
212
+
213
+ schema = get_schema("Clip", 12, "")
214
+ op = Op(self, "Clip", schema)
215
+ return op(*self._prepare_inputs(schema, input, min, max))
216
+
217
+ T_Constant: TypeAlias = Union[
218
+ BOOL,
219
+ COMPLEX128,
220
+ COMPLEX64,
221
+ DOUBLE,
222
+ FLOAT,
223
+ FLOAT16,
224
+ INT16,
225
+ INT32,
226
+ INT64,
227
+ INT8,
228
+ STRING,
229
+ UINT16,
230
+ UINT32,
231
+ UINT64,
232
+ UINT8,
233
+ ]
234
+
235
+ def Constant(
236
+ self,
237
+ *,
238
+ sparse_value: Optional[SparseTensorProto] = None,
239
+ value: Optional[TensorProto] = None,
240
+ value_float: Optional[float] = None,
241
+ value_floats: Optional[Sequence[float]] = None,
242
+ value_int: Optional[int] = None,
243
+ value_ints: Optional[Sequence[int]] = None,
244
+ value_string: Optional[str] = None,
245
+ value_strings: Optional[Sequence[str]] = None,
246
+ ) -> T_Constant:
247
+ r"""[🌐 Constant(12)](https://onnx.ai/onnx/operators/onnx__Constant.html#constant-12 "Online Documentation")
248
+
249
+
250
+ This operator produces a constant tensor. Exactly one of the provided attributes, either value, sparse_value,
251
+ or value_* must be specified.
252
+
253
+
254
+ Args:
255
+ sparse_value: The value for the elements of the output tensor in sparse
256
+ format.
257
+
258
+ value: The value for the elements of the output tensor.
259
+
260
+ value_float: The value for the sole element for the scalar, float32, output
261
+ tensor.
262
+
263
+ value_floats: The values for the elements for the 1D, float32, output
264
+ tensor.
265
+
266
+ value_int: The value for the sole element for the scalar, int64, output
267
+ tensor.
268
+
269
+ value_ints: The values for the elements for the 1D, int64, output tensor.
270
+
271
+ value_string: The value for the sole element for the scalar, UTF-8 string,
272
+ output tensor.
273
+
274
+ value_strings: The values for the elements for the 1D, UTF-8 string, output
275
+ tensor.
276
+ """
277
+
278
+ schema = get_schema("Constant", 12, "")
279
+ op = Op(self, "Constant", schema)
280
+ return op(
281
+ sparse_value=sparse_value,
282
+ value=value,
283
+ value_float=value_float,
284
+ value_floats=value_floats,
285
+ value_int=value_int,
286
+ value_ints=value_ints,
287
+ value_string=value_string,
288
+ value_strings=value_strings,
289
+ )
290
+
291
+ T_Dropout = TypeVar("T_Dropout", DOUBLE, FLOAT, FLOAT16)
292
+
293
+ T1_Dropout = TypeVar("T1_Dropout", DOUBLE, FLOAT, FLOAT16)
294
+
295
+ T2_Dropout: TypeAlias = BOOL
296
+
297
+ def Dropout(
298
+ self,
299
+ data: T_Dropout,
300
+ ratio: Optional[T1_Dropout] = None,
301
+ training_mode: Optional[T2_Dropout] = None,
302
+ *,
303
+ seed: Optional[int] = None,
304
+ ) -> Tuple[T_Dropout, T2_Dropout]:
305
+ r"""[🌐 Dropout(12)](https://onnx.ai/onnx/operators/onnx__Dropout.html#dropout-12 "Online Documentation")
306
+
307
+
308
+ Dropout takes an input floating-point tensor, an optional input ratio (floating-point scalar) and an optional input training_mode (boolean scalar). It produces two tensor outputs,
309
+ output (floating-point tensor) and mask (optional `Tensor<bool>`). If `training_mode` is true then the output Y will be a random dropout;
310
+ Note that this Dropout scales the masked input data by the following equation, so to convert the trained model into inference mode,
311
+ the user can simply not pass `training_mode` input or set it to false.
312
+ ::
313
+
314
+ output = scale * data * mask,
315
+
316
+
317
+ where
318
+ ::
319
+
320
+ scale = 1. / (1. - ratio).
321
+
322
+
323
+ This operator has **optional** inputs/outputs. See `ONNX <https://github.com/onnx/onnx/blob/master/docs/IR.md>`_ for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.
324
+
325
+
326
+ Args:
327
+ data: The input data as Tensor.
328
+
329
+ ratio: (optional) The ratio of random dropout, with value in [0, 1). If this
330
+ input was not set, or if it was set to 0, the output would be a simple
331
+ copy of the input. If it's non-zero, output will be a random dropout of
332
+ the scaled input, which is typically the case during training. It is an
333
+ optional value, if not specified it will default to 0.5.
334
+
335
+ training_mode: (optional) If set to true then it indicates dropout is being
336
+ used for training. It is an optional value hence unless specified
337
+ explicitly, it is false. If it is false, ratio is ignored and the
338
+ operation mimics inference mode where nothing will be dropped from the
339
+ input data and if mask is requested as output it will contain all ones.
340
+
341
+ seed: (Optional) Seed to the random generator, if not specified we will auto
342
+ generate one.
343
+ """
344
+
345
+ schema = get_schema("Dropout", 12, "")
346
+ op = Op(self, "Dropout", schema)
347
+ return op(*self._prepare_inputs(schema, data, ratio, training_mode), seed=seed)
348
+
349
+ T_Einsum = TypeVar(
350
+ "T_Einsum",
351
+ DOUBLE,
352
+ FLOAT,
353
+ FLOAT16,
354
+ INT16,
355
+ INT32,
356
+ INT64,
357
+ INT8,
358
+ UINT16,
359
+ UINT32,
360
+ UINT64,
361
+ UINT8,
362
+ )
363
+
364
+ def Einsum(self, *Inputs: T_Einsum, equation: str) -> T_Einsum:
365
+ r"""[🌐 Einsum(12)](https://onnx.ai/onnx/operators/onnx__Einsum.html#einsum-12 "Online Documentation")
366
+
367
+
368
+ An einsum of the form `term1, term2 -> output-term` produces an output tensor using the following equation
369
+
370
+ ::
371
+
372
+ output[output-term] = reduce-sum( input1[term1] * input2[term2] )
373
+
374
+
375
+
376
+ where the reduce-sum performs a summation over all the indices occurring in the input terms (term1, term2)
377
+ that do not occur in the output-term.
378
+
379
+ The Einsum operator evaluates algebraic tensor operations on a sequence of tensors, using the Einstein summation
380
+ convention. The equation string contains a comma-separated sequence of lower case letters. Each term corresponds to
381
+ an operand tensor, and the characters within the terms correspond to operands dimensions.
382
+
383
+ This sequence may be followed by "->" to separate the left and right hand side of the equation.
384
+ If the equation contains "->" followed by the right-hand side, the explicit (not classical) form of the Einstein
385
+ summation is performed, and the right-hand side indices indicate output tensor dimensions. In other cases,
386
+ output indices are (implicitly) set to the alphabetically sorted sequence of indices appearing exactly once in the
387
+ equation.
388
+
389
+ When a dimension character is repeated in the left-hand side, it represents summation along the dimension.
390
+
391
+ The equation may contain ellipsis ("...") to enable broadcasting. Ellipsis must indicate a fixed number of dimensions.
392
+ Specifically, every occurrence of ellipsis in the equation must represent the same number of dimensions.
393
+ The right-hand side may contain exactly one ellipsis. In implicit mode, the ellipsis dimensions are set to the
394
+ beginning of the output. The equation string may contain space (U+0020) character.
395
+
396
+
397
+ Args:
398
+ Inputs: (variadic, differentiable) Operands
399
+
400
+ equation: Einsum expression string.
401
+ """
402
+
403
+ schema = get_schema("Einsum", 12, "")
404
+ op = Op(self, "Einsum", schema)
405
+ return op(*self._prepare_inputs(schema, *Inputs), equation=equation)
406
+
407
+ T_GatherND = TypeVar(
408
+ "T_GatherND",
409
+ BOOL,
410
+ COMPLEX128,
411
+ COMPLEX64,
412
+ DOUBLE,
413
+ FLOAT,
414
+ FLOAT16,
415
+ INT16,
416
+ INT32,
417
+ INT64,
418
+ INT8,
419
+ STRING,
420
+ UINT16,
421
+ UINT32,
422
+ UINT64,
423
+ UINT8,
424
+ )
425
+
426
+ def GatherND(self, data: T_GatherND, indices: INT64, *, batch_dims: int = 0) -> T_GatherND:
427
+ r"""[🌐 GatherND(12)](https://onnx.ai/onnx/operators/onnx__GatherND.html#gathernd-12 "Online Documentation")
428
+
429
+
430
+ Given `data` tensor of rank `r` >= 1, `indices` tensor of rank `q` >= 1, and `batch_dims` integer `b`, this operator gathers
431
+ slices of `data` into an output tensor of rank `q + r - indices_shape[-1] - 1 - b`.
432
+
433
+ `indices` is an q-dimensional integer tensor, best thought of as a `(q-1)`-dimensional tensor of index-tuples into `data`,
434
+ where each element defines a slice of `data`
435
+
436
+ `batch_dims` (denoted as `b`) is an integer indicating the number of batch dimensions, i.e the leading `b` number of dimensions of
437
+ `data` tensor and `indices` are representing the batches, and the gather starts from the `b+1` dimension.
438
+
439
+ Some salient points about the inputs' rank and shape:
440
+
441
+ 1) r >= 1 and q >= 1 are to be honored. There is no dependency condition to be met between ranks `r` and `q`
442
+
443
+ 2) The first `b` dimensions of the shape of `indices` tensor and `data` tensor must be equal.
444
+
445
+ 3) b < min(q, r) is to be honored.
446
+
447
+ 4) The `indices_shape[-1]` should have a value between 1 (inclusive) and rank `r-b` (inclusive)
448
+
449
+ 5) All values in `indices` are expected to be within bounds [-s, s-1] along axis of size `s` (i.e.) `-data_shape[i] <= indices[...,i] <= data_shape[i] - 1`.
450
+ It is an error if any of the index values are out of bounds.
451
+
452
+ The output is computed as follows:
453
+
454
+ The output tensor is obtained by mapping each index-tuple in the `indices` tensor to the corresponding slice of the input `data`.
455
+
456
+ 1) If `indices_shape[-1] > r-b` => error condition
457
+
458
+ 2) If `indices_shape[-1] == r-b`, since the rank of `indices` is `q`, `indices` can be thought of as `N` `(q-b-1)`-dimensional tensors
459
+ containing 1-D tensors of dimension `r-b`, where `N` is an integer equals to the product of 1 and all the elements in the batch dimensions
460
+ of the indices_shape. Let us think of each such `r-b` ranked tensor as `indices_slice`. Each *scalar value* corresponding to `data[0:b-1,indices_slice]`
461
+ is filled into the corresponding location of the `(q-b-1)`-dimensional tensor to form the `output` tensor (Example 1 below)
462
+
463
+ 3) If `indices_shape[-1] < r-b`, since the rank of `indices` is `q`, `indices` can be thought of as `N` `(q-b-1)`-dimensional tensor
464
+ containing 1-D tensors of dimension `< r-b`. Let us think of each such tensors as `indices_slice`. Each *tensor slice* corresponding
465
+ to `data[0:b-1, indices_slice , :]` is filled into the corresponding location of the `(q-b-1)`-dimensional tensor
466
+ to form the `output` tensor (Examples 2, 3, 4 and 5 below)
467
+
468
+ This operator is the inverse of `ScatterND`.
469
+
470
+ `Example 1`
471
+
472
+ batch_dims = 0
473
+
474
+ data = [[0,1],[2,3]] # data_shape = [2, 2]
475
+
476
+ indices = [[0,0],[1,1]] # indices_shape = [2, 2]
477
+
478
+ output = [0,3] # output_shape = [2]
479
+
480
+ `Example 2`
481
+
482
+ batch_dims = 0
483
+
484
+ data = [[0,1],[2,3]] # data_shape = [2, 2]
485
+
486
+ indices = [[1],[0]] # indices_shape = [2, 1]
487
+
488
+ output = [[2,3],[0,1]] # output_shape = [2, 2]
489
+
490
+ `Example 3`
491
+
492
+ batch_dims = 0
493
+
494
+ data = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]
495
+
496
+ indices = [[0,1],[1,0]] # indices_shape = [2, 2]
497
+
498
+ output = [[2,3],[4,5]] # output_shape = [2, 2]
499
+
500
+ `Example 4`
501
+
502
+ batch_dims = 0
503
+
504
+ data = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]
505
+
506
+ indices = [[[0,1]],[[1,0]]] # indices_shape = [2, 1, 2]
507
+
508
+ output = [[[2,3]],[[4,5]]] # output_shape = [2, 1, 2]
509
+
510
+ `Example 5`
511
+
512
+ batch_dims = 1
513
+
514
+ data = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]
515
+
516
+ indices = [[1],[0]] # indices_shape = [2, 1]
517
+
518
+ output = [[2,3],[4,5]] # output_shape = [2, 2]
519
+
520
+
521
+
522
+
523
+ Args:
524
+ data: Tensor of rank r >= 1.
525
+
526
+ indices: Tensor of rank q >= 1. All index values are expected to be within
527
+ bounds [-s, s-1] along axis of size s. It is an error if any of the
528
+ index values are out of bounds.
529
+
530
+ batch_dims: The number of batch dimensions. The gather of indexing starts
531
+ from dimension of data[batch_dims:]
532
+ """
533
+
534
+ schema = get_schema("GatherND", 12, "")
535
+ op = Op(self, "GatherND", schema)
536
+ return op(*self._prepare_inputs(schema, data, indices), batch_dims=batch_dims)
537
+
538
+ T_GreaterOrEqual = TypeVar(
539
+ "T_GreaterOrEqual",
540
+ DOUBLE,
541
+ FLOAT,
542
+ FLOAT16,
543
+ INT16,
544
+ INT32,
545
+ INT64,
546
+ INT8,
547
+ UINT16,
548
+ UINT32,
549
+ UINT64,
550
+ UINT8,
551
+ )
552
+
553
+ T1_GreaterOrEqual: TypeAlias = BOOL
554
+
555
+ def GreaterOrEqual(self, A: T_GreaterOrEqual, B: T_GreaterOrEqual) -> T1_GreaterOrEqual:
556
+ r"""[🌐 GreaterOrEqual(12)](https://onnx.ai/onnx/operators/onnx__GreaterOrEqual.html#greaterorequal-12 "Online Documentation")
557
+
558
+
559
+ Returns the tensor resulted from performing the `greater_equal` logical operation
560
+ elementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).
561
+
562
+ 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>`_.
563
+
564
+
565
+ Args:
566
+ A: (non-differentiable) First input operand for the logical operator.
567
+
568
+ B: (non-differentiable) Second input operand for the logical operator.
569
+ """
570
+
571
+ schema = get_schema("GreaterOrEqual", 12, "")
572
+ op = Op(self, "GreaterOrEqual", schema)
573
+ return op(*self._prepare_inputs(schema, A, B))
574
+
575
+ T_LessOrEqual = TypeVar(
576
+ "T_LessOrEqual",
577
+ DOUBLE,
578
+ FLOAT,
579
+ FLOAT16,
580
+ INT16,
581
+ INT32,
582
+ INT64,
583
+ INT8,
584
+ UINT16,
585
+ UINT32,
586
+ UINT64,
587
+ UINT8,
588
+ )
589
+
590
+ T1_LessOrEqual: TypeAlias = BOOL
591
+
592
+ def LessOrEqual(self, A: T_LessOrEqual, B: T_LessOrEqual) -> T1_LessOrEqual:
593
+ r"""[🌐 LessOrEqual(12)](https://onnx.ai/onnx/operators/onnx__LessOrEqual.html#lessorequal-12 "Online Documentation")
594
+
595
+
596
+ Returns the tensor resulted from performing the `less_equal` logical operation
597
+ elementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).
598
+
599
+ 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>`_.
600
+
601
+
602
+ Args:
603
+ A: (non-differentiable) First input operand for the logical operator.
604
+
605
+ B: (non-differentiable) Second input operand for the logical operator.
606
+ """
607
+
608
+ schema = get_schema("LessOrEqual", 12, "")
609
+ op = Op(self, "LessOrEqual", schema)
610
+ return op(*self._prepare_inputs(schema, A, B))
611
+
612
+ T_Max = TypeVar(
613
+ "T_Max",
614
+ DOUBLE,
615
+ FLOAT,
616
+ FLOAT16,
617
+ INT16,
618
+ INT32,
619
+ INT64,
620
+ INT8,
621
+ UINT16,
622
+ UINT32,
623
+ UINT64,
624
+ UINT8,
625
+ )
626
+
627
+ def Max(self, *data_0: T_Max) -> T_Max:
628
+ r"""[🌐 Max(12)](https://onnx.ai/onnx/operators/onnx__Max.html#max-12 "Online Documentation")
629
+
630
+
631
+ Element-wise max of each of the input tensors (with Numpy-style broadcasting support).
632
+ All inputs and outputs must have the same data type.
633
+ 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>`_.
634
+
635
+
636
+ Args:
637
+ data_0: (variadic) List of tensors for max.
638
+ """
639
+
640
+ schema = get_schema("Max", 12, "")
641
+ op = Op(self, "Max", schema)
642
+ return op(*self._prepare_inputs(schema, *data_0))
643
+
644
+ T_MaxPool = TypeVar("T_MaxPool", DOUBLE, FLOAT, FLOAT16, INT8, UINT8)
645
+
646
+ I_MaxPool: TypeAlias = INT64
647
+
648
+ def MaxPool(
649
+ self,
650
+ X: T_MaxPool,
651
+ *,
652
+ auto_pad: str = "NOTSET",
653
+ ceil_mode: int = 0,
654
+ dilations: Optional[Sequence[int]] = None,
655
+ kernel_shape: Sequence[int],
656
+ pads: Optional[Sequence[int]] = None,
657
+ storage_order: int = 0,
658
+ strides: Optional[Sequence[int]] = None,
659
+ ) -> Tuple[T_MaxPool, I_MaxPool]:
660
+ r"""[🌐 MaxPool(12)](https://onnx.ai/onnx/operators/onnx__MaxPool.html#maxpool-12 "Online Documentation")
661
+
662
+
663
+ MaxPool consumes an input tensor X and applies max pooling across
664
+ the tensor according to kernel sizes, stride sizes, and pad lengths.
665
+ max pooling consisting of computing the max on all values of a
666
+ subset of the input tensor according to the kernel size and downsampling the
667
+ data into the output tensor Y for further processing. The output spatial shape is calculated differently
668
+ depending on whether explicit padding is used, where pads is employed, or auto padding is used, where auto_pad is utilized.
669
+ With explicit padding (https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html?highlight=maxpool#torch.nn.MaxPool2d):
670
+ ```
671
+ output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - dilation[i] * (kernel_shape[i] - 1) - 1) / strides_spatial_shape[i] + 1)
672
+ ```
673
+ or
674
+ ```
675
+ output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - dilation[i] * (kernel_shape[i] - 1) - 1) / strides_spatial_shape[i] + 1)
676
+ ```
677
+ if ceil_mode is enabled. `pad_shape[i]` is the sum of pads along axis `i`.
678
+
679
+ `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following when ceil_mode is enabled:
680
+ ```
681
+ VALID: output_spatial_shape[i] = ceil((input_spatial_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1) + 1) / strides_spatial_shape[i])
682
+ SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides_spatial_shape[i])
683
+ ```
684
+ or when ceil_mode is disabled (https://www.tensorflow.org/api_docs/python/tf/keras/layers/AveragePooling2D):
685
+ ```
686
+ VALID: output_spatial_shape[i] = floor((input_spatial_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1)) / strides_spatial_shape[i]) + 1
687
+ SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = floor((input_spatial_shape[i] - 1) / strides_spatial_shape[i]) + 1
688
+ ```
689
+ And pad shape will be following if `SAME_UPPER` or `SAME_LOWER`:
690
+ ```
691
+ pad_shape[i] = (output_spatial_shape[i] - 1) * strides_spatial_shape[i] + ((kernel_spatial_shape[i] - 1) * dilations[i] + 1) - input_spatial_shape[i]
692
+ ```
693
+ The output of each pooling window is maximum number of elements exclude pad.
694
+
695
+
696
+ Args:
697
+ X: (differentiable) Input data tensor from the previous operator; dimensions
698
+ for image case are (N x C x H x W), where N is the batch size, C is the
699
+ number of channels, and H and W are the height and the width of the
700
+ data. For non image case, the dimensions are in the form of (N x C x D1
701
+ x D2 ... Dn), where N is the batch size. Optionally, if dimension
702
+ denotation is in effect, the operation expects the input data tensor to
703
+ arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL,
704
+ DATA_FEATURE, DATA_FEATURE ...].
705
+
706
+ auto_pad: auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID.
707
+ Where default value is NOTSET, which means explicit padding is used.
708
+ SAME_UPPER or SAME_LOWER mean pad the input so that `output_shape[i] =
709
+ ceil(input_shape[i] / strides[i])` for each axis `i`. The padding is
710
+ split between the two sides equally or almost equally (depending on
711
+ whether it is even or odd). In case the padding is an odd number, the
712
+ extra padding is added at the end for SAME_UPPER and at the beginning
713
+ for SAME_LOWER.
714
+
715
+ ceil_mode: Whether to use ceil or floor (default) to compute the output
716
+ shape.
717
+
718
+ dilations: Dilation value along each spatial axis of filter. If not present,
719
+ the dilation defaults to 1 along each spatial axis.
720
+
721
+ kernel_shape: The size of the kernel along each axis.
722
+
723
+ pads: Padding for the beginning and ending along each spatial axis, it can
724
+ take any value greater than or equal to 0. The value represent the
725
+ number of pixels added to the beginning and end part of the
726
+ corresponding axis. `pads` format should be as follow [x1_begin,
727
+ x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels
728
+ added at the beginning of axis `i` and xi_end, the number of pixels
729
+ added at the end of axis `i`. This attribute cannot be used
730
+ simultaneously with auto_pad attribute. If not present, the padding
731
+ defaults to 0 along start and end of each spatial axis.
732
+
733
+ storage_order: The storage order of the tensor. 0 is row major, and 1 is
734
+ column major. This attribute is used only to convert an n-tuple index
735
+ value into a single integer value for producing the second output.
736
+
737
+ strides: Stride along each spatial axis. If not present, the stride defaults
738
+ to 1 along each spatial axis.
739
+ """
740
+
741
+ schema = get_schema("MaxPool", 12, "")
742
+ op = Op(self, "MaxPool", schema)
743
+ return op(
744
+ *self._prepare_inputs(schema, X),
745
+ auto_pad=auto_pad,
746
+ ceil_mode=ceil_mode,
747
+ dilations=dilations,
748
+ kernel_shape=kernel_shape,
749
+ pads=pads,
750
+ storage_order=storage_order,
751
+ strides=strides,
752
+ )
753
+
754
+ T_Min = TypeVar(
755
+ "T_Min",
756
+ DOUBLE,
757
+ FLOAT,
758
+ FLOAT16,
759
+ INT16,
760
+ INT32,
761
+ INT64,
762
+ INT8,
763
+ UINT16,
764
+ UINT32,
765
+ UINT64,
766
+ UINT8,
767
+ )
768
+
769
+ def Min(self, *data_0: T_Min) -> T_Min:
770
+ r"""[🌐 Min(12)](https://onnx.ai/onnx/operators/onnx__Min.html#min-12 "Online Documentation")
771
+
772
+
773
+ Element-wise min of each of the input tensors (with Numpy-style broadcasting support).
774
+ All inputs and outputs must have the same data type.
775
+ 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>`_.
776
+
777
+
778
+ Args:
779
+ data_0: (variadic) List of tensors for min.
780
+ """
781
+
782
+ schema = get_schema("Min", 12, "")
783
+ op = Op(self, "Min", schema)
784
+ return op(*self._prepare_inputs(schema, *data_0))
785
+
786
+ T_NegativeLogLikelihoodLoss = TypeVar(
787
+ "T_NegativeLogLikelihoodLoss", DOUBLE, FLOAT, FLOAT16
788
+ )
789
+
790
+ Tind_NegativeLogLikelihoodLoss = TypeVar("Tind_NegativeLogLikelihoodLoss", INT32, INT64)
791
+
792
+ def NegativeLogLikelihoodLoss(
793
+ self,
794
+ input: T_NegativeLogLikelihoodLoss,
795
+ target: Tind_NegativeLogLikelihoodLoss,
796
+ weight: Optional[T_NegativeLogLikelihoodLoss] = None,
797
+ *,
798
+ ignore_index: Optional[int] = None,
799
+ reduction: str = "mean",
800
+ ) -> T_NegativeLogLikelihoodLoss:
801
+ r"""[🌐 NegativeLogLikelihoodLoss(12)](https://onnx.ai/onnx/operators/onnx__NegativeLogLikelihoodLoss.html#negativeloglikelihoodloss-12 "Online Documentation")
802
+
803
+
804
+ A NegativeLogLikelihoodLoss operator computes (weighted) negative log likelihood loss.
805
+ Its "input" tensor has the shape of (N, C, d1, d2, ..., dk) where k >= 0.
806
+ The "input" tensor contains log-probabilities for input[n, :, d_1, d_2,..., d_k] being in a class of [0, C).
807
+ The operator's "target" input tensor has the shape of (N, d1, d2, ..., dk). It encodes class labels (one of C classes)
808
+ or it may contain a special value (indicated by an attribute ignore_index) for N x d1 x d2 x ... x dk samples.
809
+ The loss value for input[n, :, d_1, d_2,...d_k] being classified as class c = target[n][d_1][d_2]...[d_k] is computed as:
810
+ loss[n][d_1][d_2]...[d_k] = -input[n][c][d_1][d_2]...[d_k].
811
+ When an optional "weight" is provided, the sample loss is calculated as:
812
+ loss[n][d_1][d_2]...[d_k] = -input[n][c][d_1][d_2]...[d_k] * weight[c].
813
+ loss is zero for the case when target-value equals ignore_index.
814
+
815
+ loss[n][d_1][d_2]...[d_k] = 0, when target[n][d_1][d_2]...[d_k] = ignore_index
816
+ If "reduction" attribute is set to "none", the operator's output will be the above loss with shape (N, d1, d2, ..., dk).
817
+ If "reduction" attribute is set to "mean" (the default attribute value), the output loss is (weight) averaged:
818
+ mean(loss), if "weight" is not provided,
819
+ or if weight is provided,
820
+ sum(loss) / sum(weight[target[n][d_1][d_2]...[d_k]]]), for all samples.
821
+ If "reduction" attribute is set to "sum", the output is a scalar:
822
+ sum(loss).
823
+ See also https://pytorch.org/docs/stable/nn.html#torch.nn.NLLLoss.
824
+ Example 1:
825
+ // negative log likelihood loss, "none" reduction
826
+ N, C, d1 = 2, 3, 2
827
+ input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],
828
+ [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]
829
+ target = [[2, 1], [0, 2]]
830
+ loss = np.zeros((N, d1))
831
+ for n in range(N):
832
+ for d_1 in range(d1):
833
+ c = target[n][d_1]
834
+ loss[n][d_1] = -input[n][c][d_1]
835
+ // print(loss)
836
+ // [[-3. -2.]
837
+ // [-0. -2.]]
838
+ Example 2:
839
+ // weighted negative log likelihood loss, sum reduction
840
+ N, C, d1 = 2, 3, 2
841
+ input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],
842
+ [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]
843
+ target = [[2, 1], [0, 2]]
844
+ weight = [0.2, 0.3, 0.1]
845
+ loss = np.zeros((N, d1))
846
+ for n in range(N):
847
+ for d_1 in range(d1):
848
+ c = target[n][d_1]
849
+ loss[n][d_1] = -input[n][c][d_1] * weight[c]
850
+ loss = np.sum(loss)
851
+ // print(loss)
852
+ // -1.1
853
+ Example 3:
854
+ // weighted negative log likelihood loss, mean reduction
855
+ N, C, d1 = 2, 3, 2
856
+ input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],
857
+ [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]
858
+ target = [[2, 1], [0, 2]]
859
+ weight = [0.2, 0.3, 0.1]
860
+ loss = np.zeros((N, d1))
861
+ weight_total = 0
862
+ for n in range(N):
863
+ for d_1 in range(d1):
864
+ c = target[n][d_1]
865
+ loss[n][d_1] = -input[n][c][d_1] * weight[c]
866
+ weight_total = weight_total + weight[c]
867
+ loss = np.sum(loss) / weight_total
868
+ // print(loss)
869
+ // -1.57
870
+
871
+
872
+ Args:
873
+ input: Input tensor of shape (N, C) or (N, C, d1, d2, ..., dk).
874
+
875
+ target: Target tensor of shape (N) or (N, d1, d2, ..., dk). Target element
876
+ value shall be in range of [0, C). If ignore_index is specified, it may
877
+ have a value outside [0, C) and the target values should either be in
878
+ the range [0, C) or have the value ignore_index.
879
+
880
+ weight: (optional) Optional rescaling weight tensor. If given, it has to be
881
+ a tensor of size C. Otherwise, it is treated as if having all ones.
882
+
883
+ ignore_index: Specifies a target value that is ignored and does not
884
+ contribute to the input gradient. It's an optional value.
885
+
886
+ reduction: Type of reduction to apply to loss: none, sum, mean (default).
887
+ 'none': the output is the loss for each sample. 'sum': the output will
888
+ be summed. 'mean': the sum of the output will be divided by the sum of
889
+ applied weights.
890
+ """
891
+
892
+ schema = get_schema("NegativeLogLikelihoodLoss", 12, "")
893
+ op = Op(self, "NegativeLogLikelihoodLoss", schema)
894
+ return op(
895
+ *self._prepare_inputs(schema, input, target, weight),
896
+ ignore_index=ignore_index,
897
+ reduction=reduction,
898
+ )
899
+
900
+ T_Pow = TypeVar("T_Pow", DOUBLE, FLOAT, FLOAT16, INT32, INT64)
901
+
902
+ T1_Pow = TypeVar(
903
+ "T1_Pow",
904
+ DOUBLE,
905
+ FLOAT,
906
+ FLOAT16,
907
+ INT16,
908
+ INT32,
909
+ INT64,
910
+ INT8,
911
+ UINT16,
912
+ UINT32,
913
+ UINT64,
914
+ UINT8,
915
+ )
916
+
917
+ def Pow(self, X: T_Pow, Y: T1_Pow) -> T_Pow:
918
+ r"""[🌐 Pow(12)](https://onnx.ai/onnx/operators/onnx__Pow.html#pow-12 "Online Documentation")
919
+
920
+
921
+ Pow takes input data (Tensor<T>) and exponent Tensor, and
922
+ produces one output data (Tensor<T>) where the function `f(x) = x^exponent`,
923
+ is applied to the data tensor elementwise.
924
+ 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>`_.
925
+
926
+ Args:
927
+ X: First operand, base of the exponent.
928
+
929
+ Y: Second operand, power of the exponent.
930
+ """
931
+
932
+ schema = get_schema("Pow", 12, "")
933
+ op = Op(self, "Pow", schema)
934
+ return op(*self._prepare_inputs(schema, X, Y))
935
+
936
+ T_ReduceMax = TypeVar(
937
+ "T_ReduceMax", DOUBLE, FLOAT, FLOAT16, INT32, INT64, INT8, UINT32, UINT64, UINT8
938
+ )
939
+
940
+ def ReduceMax(
941
+ self, data: T_ReduceMax, *, axes: Optional[Sequence[int]] = None, keepdims: int = 1
942
+ ) -> T_ReduceMax:
943
+ r"""[🌐 ReduceMax(12)](https://onnx.ai/onnx/operators/onnx__ReduceMax.html#reducemax-12 "Online Documentation")
944
+
945
+
946
+ Computes the max of the input tensor's element along the provided axes. The resulting
947
+ tensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then
948
+ the resulted tensor have the reduced dimension pruned.
949
+
950
+ The above behavior is similar to numpy, with the exception that numpy defaults keepdims to
951
+ False instead of True.
952
+
953
+ Args:
954
+ data: An input tensor.
955
+
956
+ axes: A list of integers, along which to reduce. The default is to reduce
957
+ over all the dimensions of the input tensor. Accepted range is [-r, r-1]
958
+ where r = rank(data).
959
+
960
+ keepdims: Keep the reduced dimension or not, default 1 means keep reduced
961
+ dimension.
962
+ """
963
+
964
+ schema = get_schema("ReduceMax", 12, "")
965
+ op = Op(self, "ReduceMax", schema)
966
+ return op(*self._prepare_inputs(schema, data), axes=axes, keepdims=keepdims)
967
+
968
+ T_ReduceMin = TypeVar(
969
+ "T_ReduceMin", DOUBLE, FLOAT, FLOAT16, INT32, INT64, INT8, UINT32, UINT64, UINT8
970
+ )
971
+
972
+ def ReduceMin(
973
+ self, data: T_ReduceMin, *, axes: Optional[Sequence[int]] = None, keepdims: int = 1
974
+ ) -> T_ReduceMin:
975
+ r"""[🌐 ReduceMin(12)](https://onnx.ai/onnx/operators/onnx__ReduceMin.html#reducemin-12 "Online Documentation")
976
+
977
+
978
+ Computes the min of the input tensor's element along the provided axes. The resulting
979
+ tensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then
980
+ the resulted tensor have the reduced dimension pruned.
981
+
982
+ The above behavior is similar to numpy, with the exception that numpy defaults keepdims to
983
+ False instead of True.
984
+
985
+ Args:
986
+ data: An input tensor.
987
+
988
+ axes: A list of integers, along which to reduce. The default is to reduce
989
+ over all the dimensions of the input tensor. Accepted range is [-r, r-1]
990
+ where r = rank(data).
991
+
992
+ keepdims: Keep the reduced dimension or not, default 1 means keep reduced
993
+ dimension.
994
+ """
995
+
996
+ schema = get_schema("ReduceMin", 12, "")
997
+ op = Op(self, "ReduceMin", schema)
998
+ return op(*self._prepare_inputs(schema, data), axes=axes, keepdims=keepdims)
999
+
1000
+ T_SoftmaxCrossEntropyLoss = TypeVar("T_SoftmaxCrossEntropyLoss", DOUBLE, FLOAT, FLOAT16)
1001
+
1002
+ Tind_SoftmaxCrossEntropyLoss = TypeVar("Tind_SoftmaxCrossEntropyLoss", INT32, INT64)
1003
+
1004
+ def SoftmaxCrossEntropyLoss(
1005
+ self,
1006
+ scores: T_SoftmaxCrossEntropyLoss,
1007
+ labels: Tind_SoftmaxCrossEntropyLoss,
1008
+ weights: Optional[T_SoftmaxCrossEntropyLoss] = None,
1009
+ *,
1010
+ ignore_index: Optional[int] = None,
1011
+ reduction: str = "mean",
1012
+ ) -> Tuple[T_SoftmaxCrossEntropyLoss, T_SoftmaxCrossEntropyLoss]:
1013
+ r"""[🌐 SoftmaxCrossEntropyLoss(12)](https://onnx.ai/onnx/operators/onnx__SoftmaxCrossEntropyLoss.html#softmaxcrossentropyloss-12 "Online Documentation")
1014
+
1015
+ Loss function that measures the softmax cross entropy
1016
+ between 'scores' and 'labels'.
1017
+ This operator first computes a loss tensor whose shape is identical to the labels input.
1018
+ If the input is 2-D with shape (N, C), the loss tensor may be a N-element vector L = (l_1, l_2, ..., l_N).
1019
+ If the input is N-D tensor with shape (N, C, D1, D2, ..., Dk),
1020
+ the loss tensor L may have (N, D1, D2, ..., Dk) as its shape and L[i,][j_1][j_2]...[j_k] denotes a scalar element in L.
1021
+ After L is available, this operator can optionally do a reduction operator.
1022
+
1023
+ shape(scores): (N, C) where C is the number of classes, or (N, C, D1, D2,..., Dk),
1024
+ with K >= 1 in case of K-dimensional loss.
1025
+ shape(labels): (N) where each value is 0 <= labels[i] <= C-1, or (N, D1, D2,..., Dk),
1026
+ with K >= 1 in case of K-dimensional loss.
1027
+
1028
+ The loss for one sample, l_i, can calculated as follows:
1029
+ l[i][d1][d2]...[dk] = -y[i][c][d1][d2]..[dk], where i is the index of classes.
1030
+ or
1031
+ l[i][d1][d2]...[dk] = -y[i][c][d1][d2]..[dk] * weights[c], if 'weights' is provided.
1032
+
1033
+ loss is zero for the case when label-value equals ignore_index.
1034
+ l[i][d1][d2]...[dk] = 0, when labels[n][d1][d2]...[dk] = ignore_index
1035
+
1036
+ where:
1037
+ p = Softmax(scores)
1038
+ y = Log(p)
1039
+ c = labels[i][d1][d2]...[dk]
1040
+
1041
+ Finally, L is optionally reduced:
1042
+ If reduction = 'none', the output is L with shape (N, D1, D2, ..., Dk).
1043
+ If reduction = 'sum', the output is scalar: Sum(L).
1044
+ If reduction = 'mean', the output is scalar: ReduceMean(L), or if weight is provided: ReduceSum(L) / ReduceSum(W),
1045
+ where tensor W is of shape (N, D1, D2, ..., Dk) and W[n][d1][d2]...[dk] = weights[labels[i][d1][d2]...[dk]].
1046
+
1047
+
1048
+ Args:
1049
+ scores: The predicted outputs with shape [batch_size, class_size], or
1050
+ [batch_size, class_size, D1, D2 , ..., Dk], where K is the number of
1051
+ dimensions.
1052
+
1053
+ labels: The ground truth output tensor, with shape [batch_size], or
1054
+ [batch_size, D1, D2, ..., Dk], where K is the number of dimensions.
1055
+ Labels element value shall be in range of [0, C). If ignore_index is
1056
+ specified, it may have a value outside [0, C) and the label values
1057
+ should either be in the range [0, C) or have the value ignore_index.
1058
+
1059
+ weights: (optional) A manual rescaling weight given to each class. If given,
1060
+ it has to be a 1D Tensor assigning weight to each of the classes.
1061
+ Otherwise, it is treated as if having all ones.
1062
+
1063
+ ignore_index: Specifies a target value that is ignored and does not
1064
+ contribute to the input gradient. It's an optional value.
1065
+
1066
+ reduction: Type of reduction to apply to loss: none, sum, mean(default).
1067
+ 'none': no reduction will be applied, 'sum': the output will be summed.
1068
+ 'mean': the sum of the output will be divided by the number of elements
1069
+ in the output.
1070
+ """
1071
+
1072
+ schema = get_schema("SoftmaxCrossEntropyLoss", 12, "")
1073
+ op = Op(self, "SoftmaxCrossEntropyLoss", schema)
1074
+ return op(
1075
+ *self._prepare_inputs(schema, scores, labels, weights),
1076
+ ignore_index=ignore_index,
1077
+ reduction=reduction,
1078
+ )