forked from onnx/onnx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defs.cc
3417 lines (3179 loc) · 110 KB
/
defs.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-License-Identifier: Apache-2.0
*/
#include <algorithm>
#include <functional>
#include "onnx/defs/function.h"
#include "onnx/defs/schema.h"
#include "onnx/defs/tensor_proto_util.h"
namespace ONNX_NAMESPACE {
inline int MathOpTwoIntegers(std::string op_type, int a, int b) {
if (op_type == "Add") {
return a + b;
} else if (op_type == "Sub") {
return a - b;
} else if (op_type == "Mul") {
return a * b;
}
fail_shape_inference("Wrong op_type name for running propagation: ", op_type);
}
inline void MathOpDataPropagator(DataPropagationContext& ctx, std::string op_type) {
const auto input_0 = ctx.getInputData(0);
const auto input_1 = ctx.getInputData(1);
if (input_0 == nullptr || input_1 == nullptr) {
return;
}
int size_0 = input_0->dim_size();
int size_1 = input_1->dim_size();
// Fails to broadcast if the ranks are different and no any rank is 1
if (size_0 != size_1 && size_0 != 1 && size_1 != 1) {
fail_shape_inference("Invalid rank for ", op_type, " broadcasting: (",
size_0, ") vs (", size_1, ").");
}
TensorShapeProto tsp;
for (int i = 0; i < std::max(size_0, size_1); ++i) {
auto& input_dim_0 = input_0->dim(size_0 == 1 ? 0 : i);
auto& input_dim_1 = input_1->dim(size_1 == 1 ? 0 : i);
if (input_dim_0.has_dim_value() && input_dim_1.has_dim_value()) {
tsp.mutable_dim()->Add()->set_dim_value(
MathOpTwoIntegers(op_type, input_dim_0.dim_value(), input_dim_1.dim_value()));
} else {
// Cannot compute the value; simply add an empty dim without value and param
tsp.mutable_dim()->Add();
}
}
ctx.addOutputData(0, std::move(tsp));
}
std::function<void(OpSchema&)> MathDocGenerator(const char* name) {
return [=](OpSchema& schema) {
std::string doc;
POPULATE_OP_DOC_STR(
doc = R"DOC(
Performs element-wise binary {name} (with Numpy-style broadcasting support).
{broadcast_doc}
(Opset 14 change): Extend supported types to include uint8, int8, uint16, and int16.
)DOC";
ReplaceAll(doc, "{name}", name);
ReplaceAll(
doc, "{broadcast_doc}", GenerateBroadcastingDocMul().c_str()););
schema.SetDoc(doc);
schema.Input(0,
"A",
"First operand.",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable);
schema.Input(1,
"B",
"Second operand.",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable);
schema.Output(0,
"C",
"Result, has same element type as two inputs",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable);
schema.TypeConstraint(
"T",
OpSchema::all_numeric_types_with_bfloat(),
"Constrain input and output types to all numeric tensors.");
schema.TypeAndShapeInferenceFunction([](InferenceContext& ctx) {
propagateElemTypeFromInputToOutput(ctx, 0, 0);
if (hasNInputShapes(ctx, 2))
bidirectionalBroadcastShapeInference(
ctx.getInputType(0)->tensor_type().shape(),
ctx.getInputType(1)->tensor_type().shape(),
*ctx.getOutputType(0)->mutable_tensor_type()->mutable_shape());
});
};
}
std::function<void(OpSchema&)> SoftmaxFamilyDocGenerator(
const char* name,
const char* description,
const char* equation) {
return [=](OpSchema& schema) {
std::string doc;
POPULATE_OP_DOC_STR(doc = R"DOC(
The operator computes the {description} values for the given input:
{equation}
The "axis" attribute indicates the dimension along which {name}
will be performed. The output tensor has the same shape
and contains the {name} values of the corresponding input.
)DOC";
ReplaceAll(doc, "{name}", name);
ReplaceAll(doc, "{description}", description);
ReplaceAll(doc, "{equation}", equation););
std::string axis_attr;
POPULATE_OP_DOC_STR(axis_attr = R"DOC(
Describes the dimension {name} will be performed on.
Negative value means counting dimensions
from the back. Accepted range is [-r, r-1] where r = rank(input).
)DOC";
ReplaceAll(axis_attr, "{name}", name););
schema.SetDoc(doc);
schema.Attr(
"axis", axis_attr, AttributeProto::INT, static_cast<int64_t>(-1));
schema.Input(
0,
"input",
"The input tensor of rank >= axis.",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable);
schema.Output(
0,
"output",
"The output values with the same shape as the input tensor.",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable);
schema.TypeConstraint(
"T",
{"tensor(float16)",
"tensor(float)",
"tensor(double)",
"tensor(bfloat16)"},
"Constrain input and output types to float tensors.");
schema.TypeAndShapeInferenceFunction([](InferenceContext& ctx) {
// Type inference
propagateElemTypeFromInputToOutput(ctx, 0, 0);
// Shape inference starts
if (!hasNInputShapes(ctx, 1)) {
return;
}
// Validate the value of 'axis'
const TensorShapeProto& input_shape =
ctx.getInputType(0)->tensor_type().shape();
int r = input_shape.dim_size();
int axis = static_cast<int>(getAttribute(ctx, "axis", -1));
if (axis < -r || axis >= r) {
fail_shape_inference(
"'axis' must be in [",
-r,
" , ",
(r - 1),
"]. Its actual value is: ",
axis);
}
// Shape inference
propagateShapeFromInputToOutput(ctx, 0, 0);
});
};
}
ONNX_OPERATOR_SET_SCHEMA(
Add,
14,
OpSchema().FillUsing(MathDocGenerator("addition"))
.PartialDataPropagationFunction([](DataPropagationContext& ctx) {
MathOpDataPropagator(ctx, "Add");
}));
ONNX_OPERATOR_SET_SCHEMA(
Sub,
14,
OpSchema().FillUsing(MathDocGenerator("subtraction"))
.PartialDataPropagationFunction([](DataPropagationContext& ctx) {
MathOpDataPropagator(ctx, "Sub");
}));
static const char* Mod_doc = R"DOC(
Performs element-wise binary modulus (with Numpy-style broadcasting support).
The sign of the remainder is the same as that of the Divisor.
Mod operator can also behave like C fmod() or numpy.fmod. In this case, the sign of the remainder however, will be the same as the Dividend
(in contrast to integer mod). To force a behavior like numpy.fmod() an 'fmod' Attribute is provided.
This attribute is set to 0 by default causing the behavior to be like integer mod.
Setting this attribute to 1 causes the remainder to be calculated similar to that of numpy.fmod().
If the input type is floating point, then `fmod` attribute must be set to 1.
In case of dividend being zero, the results will be platform dependent.
This operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](Broadcasting.md).
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
Mod,
13,
OpSchema()
.SetDoc(Mod_doc)
.Attr(
"fmod",
"Whether the operator should behave like fmod (default=0 meaning it will do integer mods); Set this to 1 to force fmod treatment",
AttributeProto::INT,
static_cast<int64_t>(0))
.Input(0,
"A",
"Dividend tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Input(1,
"B",
"Divisor tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::NonDifferentiable)
.Output(0,
"C",
"Remainder tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",
OpSchema::all_numeric_types_with_bfloat(),
"Constrain input and output types to high-precision numeric tensors.")
.TypeAndShapeInferenceFunction([](InferenceContext& ctx) {
propagateElemTypeFromInputToOutput(ctx, 0, 0);
if (hasNInputShapes(ctx, 2))
bidirectionalBroadcastShapeInference(
ctx.getInputType(0)->tensor_type().shape(),
ctx.getInputType(1)->tensor_type().shape(),
*ctx.getOutputType(0)->mutable_tensor_type()->mutable_shape());
}));
ONNX_OPERATOR_SET_SCHEMA(
Mul,
14,
OpSchema().FillUsing(MathDocGenerator("multiplication"))
.PartialDataPropagationFunction([](DataPropagationContext& ctx) {
MathOpDataPropagator(ctx, "Mul");
}));
ONNX_OPERATOR_SET_SCHEMA(
Div,
14,
OpSchema().FillUsing(MathDocGenerator("division")));
static const char* Neg_ver13_doc = R"DOC(
Neg takes one input data (Tensor<T>) and produces one output data
(Tensor<T>) where each element flipped sign, y = -x, is applied to
the tensor elementwise.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
Neg,
13,
OpSchema()
.SetDoc(Neg_ver13_doc)
.Input(0,
"X",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(0,
"Y",
"Output tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",
{"tensor(float)",
"tensor(int32)",
"tensor(int8)",
"tensor(int16)",
"tensor(int64)",
"tensor(float16)",
"tensor(double)",
"tensor(bfloat16)"},
"Constrain input and output types to signed numeric tensors.")
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* Abs_ver13_doc = R"DOC(
Absolute takes one input data (Tensor<T>) and produces one output data
(Tensor<T>) where the absolute is, y = abs(x), is applied to
the tensor elementwise.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
Abs,
13,
OpSchema()
.SetDoc(Abs_ver13_doc)
.Input(0,
"X",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(0,
"Y",
"Output tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",
OpSchema::all_numeric_types_with_bfloat(),
"Constrain input and output types to all numeric tensors.")
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* Reciprocal_ver13_doc = R"DOC(
Reciprocal takes one input data (Tensor<T>) and produces one output data
(Tensor<T>) where the reciprocal is, y = 1/x, is applied to
the tensor elementwise.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
Reciprocal,
13,
OpSchema()
.SetDoc(Reciprocal_ver13_doc)
.Input(0,
"X",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(0,
"Y",
"Output tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",
{"tensor(float16)",
"tensor(float)",
"tensor(double)",
"tensor(bfloat16)"},
"Constrain input and output types to float tensors.")
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* Floor_ver13_doc = R"DOC(
Floor takes one input data (Tensor<T>) and produces one output data
(Tensor<T>) where the floor is, y = floor(x), is applied to
the tensor elementwise.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
Floor,
13,
OpSchema()
.SetDoc(Floor_ver13_doc)
.Input(0,
"X",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::NonDifferentiable)
.Output(0,
"Y",
"Output tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::NonDifferentiable)
.TypeConstraint(
"T",
{"tensor(float16)",
"tensor(float)",
"tensor(double)",
"tensor(bfloat16)"},
"Constrain input and output types to float tensors.")
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* Ceil_ver13_doc = R"DOC(
Ceil takes one input data (Tensor<T>) and produces one output data
(Tensor<T>) where the ceil is, y = ceil(x), is applied to
the tensor elementwise.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
Ceil,
13,
OpSchema()
.SetDoc(Ceil_ver13_doc)
.Input(0,
"X",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::NonDifferentiable)
.Output(0,
"Y",
"Output tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::NonDifferentiable)
.TypeConstraint(
"T",
{"tensor(float16)",
"tensor(float)",
"tensor(double)",
"tensor(bfloat16)"},
"Constrain input and output types to float tensors.")
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* Sqrt_ver13_doc = R"DOC(
Square root takes one input data (Tensor<T>) and produces one output data
(Tensor<T>) where the square root is, y = x^0.5, is applied to
the tensor elementwise. If x is negative, then it will return NaN.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
Sqrt,
13,
OpSchema()
.SetDoc(Sqrt_ver13_doc)
.Input(0,
"X",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(0,
"Y",
"Output tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",
{"tensor(float16)",
"tensor(float)",
"tensor(double)",
"tensor(bfloat16)"},
"Constrain input and output types to float tensors.")
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* Relu_ver14_doc = R"DOC(
Relu takes one input data (Tensor<T>) and produces one output data
(Tensor<T>) where the rectified linear function, y = max(0, x), is applied to
the tensor elementwise.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
Relu,
14,
OpSchema()
.SetDoc(Relu_ver14_doc)
.Input(0,
"X",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(0,
"Y",
"Output tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",
{"tensor(float)",
"tensor(int32)",
"tensor(int8)",
"tensor(int16)",
"tensor(int64)",
"tensor(float16)",
"tensor(double)",
"tensor(bfloat16)"},
"Constrain input and output types to signed numeric tensors.")
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* LeakyRelu_ver16_doc = R"DOC(
LeakyRelu takes input data (Tensor<T>) and an argument alpha, and produces one
output data (Tensor<T>) where the function `f(x) = alpha * x for x < 0`,
`f(x) = x for x >= 0`, is applied to the data tensor elementwise.
**History**
- Version 16 adds bfloat16 to the types allowed.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
LeakyRelu,
16,
OpSchema()
.Attr("alpha", "Coefficient of leakage.", AttributeProto::FLOAT, 0.01f)
.SetDoc(LeakyRelu_ver16_doc)
.Input(0,
"X",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(0,
"Y",
"Output tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",
{"tensor(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)"},
"Constrain input and output types to float tensors.")
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* ThresholdedRelu_ver10_doc = R"DOC(
ThresholdedRelu takes one input data (Tensor<T>) and produces one output data
(Tensor<T>) where the rectified linear function, y = x for x > alpha, y = 0 otherwise,
is applied to the tensor elementwise.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
ThresholdedRelu,
10,
OpSchema()
.SetDoc(ThresholdedRelu_ver10_doc)
.Attr("alpha", "Threshold value", AttributeProto::FLOAT, 1.0f)
.Input(0,
"X",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(0,
"Y",
"Output tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",
{"tensor(float16)", "tensor(float)", "tensor(double)"},
"Constrain input and output types to float tensors.")
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* Selu_ver6_doc = R"DOC(
Selu takes one input data (Tensor<T>) and produces one output data
(Tensor<T>) where the scaled exponential linear unit function,
`y = gamma * (alpha * e^x - alpha) for x <= 0`, `y = gamma * x for x > 0`,
is applied to the tensor elementwise.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
Selu,
6,
OpSchema()
.Attr(
"alpha",
"Coefficient of SELU default to 1.67326319217681884765625 "
"(i.e., float32 approximation of 1.6732632423543772848170429916717).",
AttributeProto::FLOAT,
1.67326319217681884765625f)
.Attr(
"gamma",
"Coefficient of SELU default to 1.05070102214813232421875 "
"(i.e., float32 approximation of 1.0507009873554804934193349852946).",
AttributeProto::FLOAT,
1.05070102214813232421875f)
.SetDoc(Selu_ver6_doc)
.Input(0,
"X",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(0,
"Y",
"Output tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",
{"tensor(float16)", "tensor(float)", "tensor(double)"},
"Constrain input and output types to float tensors.")
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* Elu_ver6_doc = R"DOC(
Elu takes one input data (Tensor<T>) and produces one output data
(Tensor<T>) where the function `f(x) = alpha * (exp(x) - 1.) for x <
0`, `f(x) = x for x >= 0`., is applied to the tensor elementwise.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
Elu,
6,
OpSchema()
.Attr("alpha", "Coefficient of ELU.", AttributeProto::FLOAT, 1.0f)
.SetDoc(Elu_ver6_doc)
.Input(0,
"X",
"1D input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(0,
"Y",
"1D output tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",
{"tensor(float16)", "tensor(float)", "tensor(double)"},
"Constrain input and output types to float tensors.")
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* celu_ver12_doc = R"DOC(
Continuously Differentiable Exponential Linear Units:
Perform the linear unit element-wise on the input tensor X
using formula:
```
max(0,x) + min(0,alpha*(exp(x/alpha)-1))
```
)DOC";
static float celu_default_alpha = 1.0;
TensorProto ToDimensionOneFloatTensor(float value) {
auto t = ToTensor(std::vector<float>({value}));
t.add_dims(1);
return t;
}
TensorProto ToDimensionOneTensor(int32_t value) {
auto t = ToTensor(std::vector<int32_t>({value}));
t.add_dims(1);
return t;
}
TensorProto ToDimensionOneInt64Tensor(int64_t value) {
auto t = ToTensor(std::vector<int64_t>({value}));
t.add_dims(1);
return t;
}
TensorProto ToDimensionOneInt64Tensor(std::vector<int64_t> value) {
auto t = ToTensor(value);
t.add_dims(value.size());
return t;
}
bool BuildContextDependentFunctionBodyCelu(
const FunctionBodyBuildContext& ctx,
const OpSchema& schema,
FunctionProto& functionProto) {
float alpha = ctx.getAttribute("alpha") != nullptr ? ctx.getAttribute("alpha")->f() : celu_default_alpha;
FunctionBuilder builder(functionProto);
builder
.Const("alpha", std::vector<float>{alpha})
.Add(R"(
X_alpha = Div (X, alpha)
Elu_Result = Elu <alpha = 1.0>(X_alpha)
Y = Mul (alpha, Elu_Result)
)");
schema.BuildFunction(functionProto);
return true;
}
ONNX_OPERATOR_SET_SCHEMA(
Celu,
12,
OpSchema()
.SetDoc(celu_ver12_doc)
.Input(0,
"X",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(0,
"Y",
"Output tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Attr(
"alpha",
"The Alpha value in Celu formula which control the shape of "
"the unit. The default value is 1.0.",
AttributeProto::FLOAT,
celu_default_alpha)
.TypeConstraint(
"T",
{"tensor(float)"},
"Constrain input and output types to float32 tensors.")
.SetContextDependentFunctionBodyBuilder(
BuildContextDependentFunctionBodyCelu)
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* Exp_ver13_doc = R"DOC(
Calculates the exponential of the given input tensor, element-wise.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
Exp,
13,
OpSchema()
.SetDoc(Exp_ver13_doc)
.Input(0,
"input",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(
0,
"output",
"The exponential of the input tensor computed "
"element-wise",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",
{"tensor(float16)",
"tensor(float)",
"tensor(double)",
"tensor(bfloat16)"},
"Constrain input and output types to float tensors.")
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* Log_ver13_doc = R"DOC(
Calculates the natural log of the given input tensor, element-wise.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
Log,
13,
OpSchema()
.SetDoc(Log_ver13_doc)
.Input(0,
"input",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(
0,
"output",
"The natural log of the input tensor computed "
"element-wise",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",
{"tensor(float16)",
"tensor(float)",
"tensor(double)",
"tensor(bfloat16)"},
"Constrain input and output types to float tensors.")
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* Tanh_ver13_doc = R"DOC(
Calculates the hyperbolic tangent of the given input tensor element-wise.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
Tanh,
13,
OpSchema()
.SetDoc(Tanh_ver13_doc)
.Input(0,
"input",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(
0,
"output",
"The hyperbolic tangent values of the input tensor "
"computed element-wise",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",
{"tensor(float16)",
"tensor(float)",
"tensor(double)",
"tensor(bfloat16)"},
"Constrain input and output types to float tensors.")
.TypeAndShapeInferenceFunction(propagateShapeAndTypeFromFirstInput));
static const char* Pow_ver15_doc = R"DOC(
Pow takes input data (Tensor<T>) and exponent Tensor, and
produces one output data (Tensor<T>) where the function `f(x) = x^exponent`,
is applied to the data tensor elementwise.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
Pow,
15,
OpSchema()
.SetDoc(GET_OP_DOC_STR(
std::string(Pow_ver15_doc) + GenerateBroadcastingDocMul()))
.Input(0,
"X",
"First operand, base of the exponent.",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Input(1,
"Y",
"Second operand, power of the exponent.",
"T1",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(0,
"Z",
"Output tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",
{"tensor(int32)",
"tensor(int64)",
"tensor(float16)",
"tensor(float)",
"tensor(double)",
"tensor(bfloat16)"},
"Constrain input X and output types to float/int tensors.")
.TypeConstraint(
"T1",
{"tensor(uint8)",
"tensor(uint16)",
"tensor(uint32)",
"tensor(uint64)",
"tensor(int8)",
"tensor(int16)",
"tensor(int32)",
"tensor(int64)",
"tensor(float16)",
"tensor(float)",
"tensor(double)",
"tensor(bfloat16)"},
"Constrain input Y types to float/int tensors.")
.TypeAndShapeInferenceFunction([](InferenceContext& ctx) {
propagateElemTypeFromInputToOutput(ctx, 0, 0);
if (hasNInputShapes(ctx, 2))
bidirectionalBroadcastShapeInference(
ctx.getInputType(0)->tensor_type().shape(),
ctx.getInputType(1)->tensor_type().shape(),
*ctx.getOutputType(0)->mutable_tensor_type()->mutable_shape());
}));
static const char* PRelu_ver16_doc = R"DOC(
PRelu takes input data (Tensor<T>) and slope tensor as input, and produces one
output data (Tensor<T>) where the function `f(x) = slope * x for x < 0`,
`f(x) = x for x >= 0`., is applied to the data tensor elementwise.
**History**
- Version 16 adds bfloat16 to the types allowed.
)DOC";
ONNX_OPERATOR_SET_SCHEMA(
PRelu,
16,
OpSchema()
.SetDoc(GET_OP_DOC_STR(
std::string(PRelu_ver16_doc) +
GenerateBroadcastingDocUni("tensor slope", "input tensor X")))
.Input(0,
"X",
"Input tensor",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Input(
1,
"slope",
"Slope tensor. The shape of slope can be smaller then first input X; "
"if so, its shape must be unidirectional broadcastable to X",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.Output(0,
"Y",
"Output tensor (same size as X)",
"T",
OpSchema::Single,
true,
1,
OpSchema::Differentiable)
.TypeConstraint(
"T",