/
math_ops.cc
2867 lines (2367 loc) · 90.8 KB
/
math_ops.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
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/core/framework/common_shape_fns.h"
#include "tensorflow/core/framework/numeric_op.h"
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/shape_inference.h"
namespace tensorflow {
using shape_inference::DimensionHandle;
using shape_inference::InferenceContext;
using shape_inference::ShapeHandle;
REGISTER_OP("AddN")
.Input("inputs: N * T")
.Output("sum: T")
.Attr("N: int >= 1")
.Attr("T: {numbertype, variant}")
.SetIsCommutative()
.SetIsAggregate()
.SetShapeFn([](InferenceContext* c) {
ShapeHandle cur = c->input(c->num_inputs() - 1);
for (int i = c->num_inputs() - 2; i >= 0; --i) {
TF_RETURN_WITH_CONTEXT_IF_ERROR(c->Merge(c->input(i), cur, &cur),
"From merging shape ", i,
" with other shapes.");
}
c->set_output(0, cur);
return Status::OK();
})
.Doc(R"doc(
Add all input tensors element wise.
inputs: Must all be the same size and shape.
)doc");
// --------------------------------------------------------------------------
// Note that the following operator is just a placeholder and has no
// associated kernel. The code in accumulate_n_optimizer.cc replaces
// this placeholder with a graph of operators that do have kernels.
// The Python code that generates instances of this op is currently in
// contrib/framework/python/ops/accumulate_n_v2.py
REGISTER_OP("AccumulateNV2")
.Input("inputs: N * T")
.Output("sum: T")
.Attr("N: int >= 1")
.Attr("T: numbertype")
.Attr("shape: shape")
.SetIsCommutative()
.SetIsAggregate()
.SetShapeFn(shape_inference::ExplicitShape)
.Doc(R"doc(
Returns the element-wise sum of a list of tensors.
`tf.accumulate_n_v2` performs the same operation as `tf.add_n`, but does not
wait for all of its inputs to be ready before beginning to sum. This can
save memory if inputs are ready at different times, since minimum temporary
storage is proportional to the output size rather than the inputs size.
Unlike the original `accumulate_n`, `accumulate_n_v2` is differentiable.
Returns a `Tensor` of same shape and type as the elements of `inputs`.
inputs: A list of `Tensor` objects, each with same shape and type.
shape: Shape of elements of `inputs`.
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("BatchMatMul")
.Input("x: T")
.Input("y: T")
.Output("output: T")
.Attr("T: {half, float, double, int32, complex64, complex128}")
.Attr("adj_x: bool = false")
.Attr("adj_y: bool = false")
.SetShapeFn([](InferenceContext* c) {
ShapeHandle a_shape;
ShapeHandle b_shape;
TF_RETURN_IF_ERROR(c->WithRankAtLeast(c->input(0), 2, &a_shape));
TF_RETURN_IF_ERROR(c->WithRankAtLeast(c->input(1), 2, &b_shape));
// Determine output rows and cols.
bool adj_x;
bool adj_y;
TF_RETURN_IF_ERROR(c->GetAttr("adj_x", &adj_x));
TF_RETURN_IF_ERROR(c->GetAttr("adj_y", &adj_y));
DimensionHandle output_rows = c->Dim(a_shape, adj_x ? -1 : -2);
DimensionHandle output_cols = c->Dim(b_shape, adj_y ? -2 : -1);
// Batch dims match between inputs.
ShapeHandle a_batch_dims;
ShapeHandle b_batch_dims;
ShapeHandle batch_dims;
TF_RETURN_IF_ERROR(c->Subshape(a_shape, 0, -2, &a_batch_dims));
TF_RETURN_IF_ERROR(c->Subshape(b_shape, 0, -2, &b_batch_dims));
TF_RETURN_IF_ERROR(c->Merge(a_batch_dims, b_batch_dims, &batch_dims));
// Assert inner dims match.
DimensionHandle unused;
TF_RETURN_IF_ERROR(c->Merge(c->Dim(a_shape, adj_x ? -2 : -1),
c->Dim(b_shape, adj_y ? -1 : -2), &unused));
ShapeHandle out;
TF_RETURN_IF_ERROR(c->Concatenate(
batch_dims, c->Matrix(output_rows, output_cols), &out));
c->set_output(0, out);
return Status::OK();
})
.Doc(R"doc(
Multiplies slices of two tensors in batches.
Multiplies all slices of `Tensor` `x` and `y` (each slice can be
viewed as an element of a batch), and arranges the individual results
in a single output tensor of the same batch size. Each of the
individual slices can optionally be adjointed (to adjoint a matrix
means to transpose and conjugate it) before multiplication by setting
the `adj_x` or `adj_y` flag to `True`, which are by default `False`.
The input tensors `x` and `y` are 2-D or higher with shape `[..., r_x, c_x]`
and `[..., r_y, c_y]`.
The output tensor is 2-D or higher with shape `[..., r_o, c_o]`, where:
r_o = c_x if adj_x else r_x
c_o = r_y if adj_y else c_y
It is computed as:
output[..., :, :] = matrix(x[..., :, :]) * matrix(y[..., :, :])
x: 2-D or higher with shape `[..., r_x, c_x]`.
y: 2-D or higher with shape `[..., r_y, c_y]`.
output: 3-D or higher with shape `[..., r_o, c_o]`
adj_x: If `True`, adjoint the slices of `x`. Defaults to `False`.
adj_y: If `True`, adjoint the slices of `y`. Defaults to `False`.
)doc");
// --------------------------------------------------------------------------
// Casting Ops
//
// NOTE: Only a smaller number of types are supported by
// Cast. The exact casting rule is TBD. The current
// implementation uses C++ static cast rules for numeric
// types, which may be changed in the future.
REGISTER_OP("Cast")
.Input("x: SrcT")
.Output("y: DstT")
.Attr("SrcT: type")
.Attr("DstT: type")
.SetShapeFn(shape_inference::UnchangedShape)
.Doc(R"doc(
Cast x of type SrcT to y of DstT.
)doc");
REGISTER_OP("_HostCast")
.Input("x: SrcT")
.Output("y: DstT")
.Attr("SrcT: type")
.Attr("DstT: type")
.SetShapeFn(shape_inference::UnchangedShape)
.Doc(R"doc(
Cast x of type SrcT to y of DstT.
_HostCast requires its input and produces its output in host memory.
)doc");
// --------------------------------------------------------------------------
REGISTER_OP("Abs")
.Input("x: T")
.Output("y: T")
.Attr("T: {half, float, double, int32, int64}")
.SetShapeFn(shape_inference::UnchangedShape)
.Doc(R"doc(
Computes the absolute value of a tensor.
Given a tensor `x`, this operation returns a tensor containing the absolute
value of each element in `x`. For example, if x is an input element and y is
an output element, this operation computes \\(y = |x|\\).
)doc");
REGISTER_OP("ComplexAbs")
.Input("x: T")
.Output("y: Tout")
.Attr("T: {complex64, complex128} = DT_COMPLEX64")
.Attr("Tout: {float, double} = DT_FLOAT")
.SetShapeFn(shape_inference::UnchangedShape)
.Doc(R"doc(
Computes the complex absolute value of a tensor.
Given a tensor `x` of complex numbers, this operation returns a tensor of type
`float` or `double` that is the absolute value of each element in `x`. All
elements in `x` must be complex numbers of the form \\(a + bj\\). The absolute
value is computed as \\( \sqrt{a^2 + b^2}\\).
)doc");
// Declares cwise unary operations signature: 't -> 't
#define UNARY() \
Input("x: T") \
.Output("y: T") \
.Attr("T: {half, float, double, int32, int64, complex64, complex128}") \
.SetShapeFn(shape_inference::UnchangedShape)
#define UNARY_REAL() \
Input("x: T") \
.Output("y: T") \
.Attr("T: {half, float, double}") \
.SetShapeFn(shape_inference::UnchangedShape)
#define UNARY_COMPLEX() \
Input("x: T") \
.Output("y: T") \
.Attr("T: {half, float, double, complex64, complex128}") \
.SetShapeFn(shape_inference::UnchangedShape)
#define UNARY_GRADIENT_COMPLEX() \
Input("y: T") \
.Input("dy: T") \
.Output("z: T") \
.Attr("T: {half, float, double, complex64, complex128}") \
.SetShapeFn(shape_inference::UnchangedShape)
REGISTER_OP("Neg")
.UNARY()
.Doc(R"doc(
Computes numerical negative value element-wise.
I.e., \\(y = -x\\).
)doc");
REGISTER_OP("Inv")
.UNARY()
.Doc(R"doc(
Computes the reciprocal of x element-wise.
I.e., \\(y = 1 / x\\).
)doc")
.Deprecated(17, "Use Reciprocal");
REGISTER_OP("InvGrad")
.UNARY_GRADIENT_COMPLEX()
.Doc(R"doc(
Computes the gradient for the inverse of `x` wrt its input.
Specifically, `grad = -dy * y*y`, where `y = 1/x`, and `dy`
is the corresponding input gradient.
)doc")
.Deprecated(17, "Use ReciprocalGrad");
REGISTER_OP("Reciprocal")
.UNARY()
.Doc(R"doc(
Computes the reciprocal of x element-wise.
I.e., \\(y = 1 / x\\).
)doc");
REGISTER_OP("ReciprocalGrad")
.UNARY_GRADIENT_COMPLEX()
.Doc(R"doc(
Computes the gradient for the inverse of `x` wrt its input.
Specifically, `grad = -dy * y*y`, where `y = 1/x`, and `dy`
is the corresponding input gradient.
)doc");
REGISTER_OP("Square")
.UNARY()
.Doc(R"doc(
Computes square of x element-wise.
I.e., \\(y = x * x = x^2\\).
)doc");
REGISTER_OP("Sqrt")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes square root of x element-wise.
I.e., \\(y = \sqrt{x} = x^{1/2}\\).
)doc");
REGISTER_OP("SqrtGrad")
.UNARY_GRADIENT_COMPLEX()
.Doc(R"doc(
Computes the gradient for the sqrt of `x` wrt its input.
Specifically, `grad = dy * 0.5 / y`, where `y = sqrt(x)`, and `dy`
is the corresponding input gradient.
)doc");
REGISTER_OP("Rsqrt")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes reciprocal of square root of x element-wise.
I.e., \\(y = 1 / \sqrt{x}\\).
)doc");
REGISTER_OP("Round")
.UNARY()
.Doc(R"doc(
Rounds the values of a tensor to the nearest integer, element-wise.
Rounds half to even. Also known as bankers rounding. If you want to round
according to the current system rounding mode use std::cint.
)doc");
REGISTER_OP("RsqrtGrad")
.UNARY_GRADIENT_COMPLEX()
.Doc(R"doc(
Computes the gradient for the rsqrt of `x` wrt its input.
Specifically, `grad = dy * -0.5 * y^3`, where `y = rsqrt(x)`, and `dy`
is the corresponding input gradient.
)doc");
REGISTER_OP("Exp")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes exponential of x element-wise. \\(y = e^x\\).
)doc");
REGISTER_OP("Expm1")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes exponential of x - 1 element-wise.
I.e., \\(y = (\exp x) - 1\\).
)doc");
REGISTER_OP("Log")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes natural logarithm of x element-wise.
I.e., \\(y = \log_e x\\).
)doc");
REGISTER_OP("Log1p")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes natural logarithm of (1 + x) element-wise.
I.e., \\(y = \log_e (1 + x)\\).
)doc");
REGISTER_OP("Sinh")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes hyperbolic sine of x element-wise.
)doc");
REGISTER_OP("Cosh")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes hyperbolic cosine of x element-wise.
)doc");
REGISTER_OP("Tanh")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes hyperbolic tangent of `x` element-wise.
)doc");
REGISTER_OP("Asinh")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes inverse hyperbolic sine of x element-wise.
)doc");
REGISTER_OP("Acosh")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes inverse hyperbolic cosine of x element-wise.
)doc");
REGISTER_OP("Atanh")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes inverse hyperbolic tangent of x element-wise.
)doc");
REGISTER_OP("TanhGrad")
.UNARY_GRADIENT_COMPLEX()
.Doc(R"doc(
Computes the gradient for the tanh of `x` wrt its input.
Specifically, `grad = dy * (1 - y*y)`, where `y = tanh(x)`, and `dy`
is the corresponding input gradient.
)doc");
REGISTER_OP("Lgamma")
.UNARY_REAL()
.Doc(R"doc(
Computes the log of the absolute value of `Gamma(x)` element-wise.
)doc");
REGISTER_OP("Digamma")
.UNARY_REAL()
.Doc(R"doc(
Computes Psi, the derivative of Lgamma (the log of the absolute value of
`Gamma(x)`), element-wise.
)doc");
REGISTER_OP("Erf")
.UNARY_REAL()
.Doc(R"doc(
Computes the Gauss error function of `x` element-wise.
)doc");
REGISTER_OP("Erfc")
.UNARY_REAL()
.Doc(R"doc(
Computes the complementary error function of `x` element-wise.
)doc");
REGISTER_OP("Sigmoid")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes sigmoid of `x` element-wise.
Specifically, `y = 1 / (1 + exp(-x))`.
)doc");
REGISTER_OP("SigmoidGrad")
.UNARY_GRADIENT_COMPLEX()
.Doc(R"doc(
Computes the gradient of the sigmoid of `x` wrt its input.
Specifically, `grad = dy * y * (1 - y)`, where `y = sigmoid(x)`, and
`dy` is the corresponding input gradient.
)doc");
REGISTER_OP("Sin")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes sin of x element-wise.
)doc");
REGISTER_OP("Cos")
.UNARY_COMPLEX()
.Doc(R"doc(
Computes cos of x element-wise.
)doc");
REGISTER_OP("Tan")
.UNARY()
.Doc(R"doc(
Computes tan of x element-wise.
)doc");
REGISTER_OP("Asin")
.UNARY()
.Doc(R"doc(
Computes asin of x element-wise.
)doc");
REGISTER_OP("Acos")
.UNARY()
.Doc(R"doc(
Computes acos of x element-wise.
)doc");
REGISTER_OP("Atan")
.UNARY()
.Doc(R"doc(
Computes atan of x element-wise.
)doc");
#undef UNARY
#undef UNARY_REAL
#undef UNARY_COMPLEX
REGISTER_OP("IsNan")
.Input("x: T")
.Output("y: bool")
.Attr("T: {half, float, double}")
.SetShapeFn(shape_inference::UnchangedShape)
.Doc(R"doc(
Returns which elements of x are NaN.
@compatibility(numpy)
Equivalent to np.isnan
@end_compatibility
)doc");
REGISTER_OP("IsInf")
.Input("x: T")
.Output("y: bool")
.Attr("T: {half, float, double}")
.SetShapeFn(shape_inference::UnchangedShape)
.Doc(R"doc(
Returns which elements of x are Inf.
@compatibility(numpy)
Equivalent to np.isinf
@end_compatibility
)doc");
REGISTER_OP("IsFinite")
.Input("x: T")
.Output("y: bool")
.Attr("T: {half, float, double}")
.SetShapeFn(shape_inference::UnchangedShape)
.Doc(R"doc(
Returns which elements of x are finite.
@compatibility(numpy)
Equivalent to np.isfinite
@end_compatibility
)doc");
REGISTER_OP("Sign")
.Input("x: T")
.Output("y: T")
.Attr("T: {half, float, double, int32, int64, complex64, complex128}")
.SetShapeFn(shape_inference::UnchangedShape)
.Doc(R"doc(
Returns an element-wise indication of the sign of a number.
`y = sign(x) = -1` if `x < 0`; 0 if `x == 0`; 1 if `x > 0`.
For complex numbers, `y = sign(x) = x / |x|` if `x != 0`, otherwise `y = 0`.
)doc");
REGISTER_OP("Floor")
.Input("x: T")
.Output("y: T")
.Attr("T: {half, float, double}")
.SetShapeFn(shape_inference::UnchangedShape)
.Doc(R"doc(
Returns element-wise largest integer not greater than x.
)doc");
REGISTER_OP("Ceil")
.Input("x: T")
.Output("y: T")
.Attr("T: {half, float, double}")
.SetShapeFn(shape_inference::UnchangedShape)
.Doc(R"doc(
Returns element-wise smallest integer in not less than x.
)doc");
REGISTER_OP("Rint")
.Input("x: T")
.Output("y: T")
.Attr("T: {float, double}")
.SetShapeFn(shape_inference::UnchangedShape)
.Doc(R"doc(
Returns element-wise integer closest to x.
If the result is midway between two representable values,
the even representable is chosen.
For example:
```
rint(-1.5) ==> -2.0
rint(0.5000001) ==> 1.0
rint([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) ==> [-2., -2., -0., 0., 2., 2., 2.]
```
)doc");
// Declares cwise binary operations signature: 't, 't -> 't.
#define BINARY_MORE() \
Input("x: T").Input("y: T").Output("z: T").Attr( \
"T: {half, float, double, uint8, int8, uint16, int16, int32, int64, " \
"complex64, complex128}")
#define BINARY_FEWER() \
Input("x: T").Input("y: T").Output("z: T").Attr( \
"T: {half, float, double, int32, int64, complex64, complex128}")
// TODO(mrry): Restore `SetIsCommutative()` for non-string types.
REGISTER_OP("Add")
.Input("x: T")
.Input("y: T")
.Output("z: T")
.Attr(
"T: {half, float, double, uint8, int8, int16, int32, int64, complex64, "
"complex128, string}")
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns x + y element-wise.
*NOTE*: `Add` supports broadcasting. `AddN` does not. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("_MklAdd")
.Input("x: T")
.Input("y: T")
.Input("mkl_x: uint8")
.Input("mkl_y: uint8")
.Output("z: T")
.Output("mkl_z: uint8")
.Attr(
"T: {half, float, double, uint8, int8, int16, int32, int64, complex64, "
"complex128, string}")
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns x + y element-wise.
*NOTE*: `Add` supports broadcasting. `AddN` does not. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("Sub")
.BINARY_MORE()
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns x - y element-wise.
*NOTE*: `Sub` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("_MklSub")
.BINARY_FEWER()
.Input("mkl_x: uint8")
.Input("mkl_y: uint8")
.Output("mkl_z: uint8")
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns x - y element-wise.
*NOTE*: `Sub` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("Mul")
.BINARY_MORE()
.SetIsCommutative()
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns x * y element-wise.
*NOTE*: `Mul` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("_MklMul")
.BINARY_MORE()
.Input("mkl_x: uint8")
.Input("mkl_y: uint8")
.Output("mkl_z: uint8")
.SetIsCommutative()
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns x * y element-wise.
*NOTE*: `Mul` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("Div")
.BINARY_MORE()
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns x / y element-wise.
*NOTE*: `Div` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("FloorDiv")
.BINARY_MORE()
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns x // y element-wise.
*NOTE*: `FloorDiv` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("TruncateDiv")
.BINARY_MORE()
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns x / y element-wise for integer types.
Truncation designates that negative numbers will round fractional quantities
toward zero. I.e. -7 / 5 = -1. This matches C semantics but it is different
than Python semantics. See `FloorDiv` for a division function that matches
Python Semantics.
*NOTE*: `TruncateDiv` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("RealDiv")
.BINARY_MORE()
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns x / y element-wise for real types.
If `x` and `y` are reals, this will return the floating-point division.
*NOTE*: `Div` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("SquaredDifference")
.BINARY_FEWER()
.SetIsCommutative()
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns (x - y)(x - y) element-wise.
*NOTE*: `SquaredDifference` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("_MklSquaredDifference")
.BINARY_FEWER()
.Input("mkl_x: uint8")
.Input("mkl_y: uint8")
.Output("mkl_z: uint8")
.SetIsCommutative()
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns (x - y)(x - y) element-wise.
*NOTE*: `SquaredDifference` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
#undef BINARY_FEWER
#undef BINARY_MORE
REGISTER_OP("Maximum")
.Input("x: T")
.Input("y: T")
.Output("z: T")
.Attr("T: {half, float, double, int32, int64}")
.SetIsCommutative()
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns the max of x and y (i.e. x > y ? x : y) element-wise.
*NOTE*: `Maximum` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("_MklMaximum")
.Input("x: T")
.Input("y: T")
.Input("mkl_x: uint8")
.Input("mkl_y: uint8")
.Output("z: T")
.Output("mkl_z: uint8")
.Attr("T: {half, float, double, int32, int64}")
.SetIsCommutative()
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns the max of x and y (i.e. x > y ? x : y) element-wise.
*NOTE*: `Maximum` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("Minimum")
.Input("x: T")
.Input("y: T")
.Output("z: T")
.Attr("T: {half, float, double, int32, int64}")
.SetIsCommutative()
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns the min of x and y (i.e. x < y ? x : y) element-wise.
*NOTE*: `Minimum` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("Mod")
.Input("x: T")
.Input("y: T")
.Output("z: T")
.Attr("T: {int32, int64, float, double}")
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns element-wise remainder of division. This emulates C semantics in that
the result here is consistent with a truncating divide. E.g.
`tf.truncatediv(x, y) * y + truncate_mod(x, y) = x`.
*NOTE*: `Mod` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("FloorMod")
.Input("x: T")
.Input("y: T")
.Output("z: T")
.Attr("T: {int32, int64, float, double}")
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns element-wise remainder of division. When `x < 0` xor `y < 0` is
true, this follows Python semantics in that the result here is consistent
with a flooring divide. E.g. `floor(x / y) * y + mod(x, y) = x`.
*NOTE*: `FloorMod` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("TruncateMod")
.Input("x: T")
.Input("y: T")
.Output("z: T")
.Attr("T: {int32, int64, float, double}")
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Returns element-wise remainder of division. This emulates C semantics in that
the result here is consistent with a truncating divide. E.g. `truncate(x / y) *
y + truncate_mod(x, y) = x`.
*NOTE*: `TruncateMod` supports broadcasting. More about broadcasting
[here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
)doc");
REGISTER_OP("Pow")
.Input("x: T")
.Input("y: T")
.Output("z: T")
.Attr("T: {half, float, double, int32, int64, complex64, complex128}")
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Computes the power of one value to another.
Given a tensor `x` and a tensor `y`, this operation computes \\(x^y\\) for
corresponding elements in `x` and `y`. For example:
```
# tensor 'x' is [[2, 2]], [3, 3]]
# tensor 'y' is [[8, 16], [2, 3]]
tf.pow(x, y) ==> [[256, 65536], [9, 27]]
```
)doc");
REGISTER_OP("Igammac")
.Input("a: T")
.Input("x: T")
.Output("z: T")
.Attr("T: {float, double}")
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Compute the upper regularized incomplete Gamma function `Q(a, x)`.
The upper regularized incomplete Gamma function is defined as:
\\(Q(a, x) = Gamma(a, x) / Gamma(a) = 1 - P(a, x)\\)
where
\\(Gamma(a, x) = int_{x}^{\infty} t^{a-1} exp(-t) dt\\)
is the upper incomplete Gama function.
Note, above `P(a, x)` (`Igamma`) is the lower regularized complete
Gamma function.
)doc");
REGISTER_OP("Igamma")
.Input("a: T")
.Input("x: T")
.Output("z: T")
.Attr("T: {float, double}")
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Compute the lower regularized incomplete Gamma function `Q(a, x)`.
The lower regularized incomplete Gamma function is defined as:
\\(P(a, x) = gamma(a, x) / Gamma(a) = 1 - Q(a, x)\\)
where
\\(gamma(a, x) = int_{0}^{x} t^{a-1} exp(-t) dt\\)
is the lower incomplete Gamma function.
Note, above `Q(a, x)` (`Igammac`) is the upper regularized complete
Gamma function.
)doc");
REGISTER_OP("Zeta")
.Input("x: T")
.Input("q: T")
.Output("z: T")
.Attr("T: {float, double}")
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Compute the Hurwitz zeta function \\(\zeta(x, q)\\).
The Hurwitz zeta function is defined as:
\\(\zeta(x, q) = \sum_{n=0}^{\infty} (q + n)^{-x}\\)
)doc");
REGISTER_OP("Polygamma")
.Input("a: T")
.Input("x: T")
.Output("z: T")
.Attr("T: {float, double}")
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Compute the polygamma function \\(\psi^{(n)}(x)\\).
The polygamma function is defined as:
\\(\psi^{(n)}(x) = \frac{d^n}{dx^n} \psi(x)\\)
where \\(\psi(x)\\) is the digamma function.
)doc");
REGISTER_OP("Atan2")
.Input("y: T")
.Input("x: T")
.Output("z: T")
.Attr("T: {float, double}")
.SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn)
.Doc(R"doc(
Computes arctangent of `y/x` element-wise, respecting signs of the arguments.
This is the angle \( \theta \in [-\pi, \pi] \) such that
\[ x = r \cos(\theta) \]
and
\[ y = r \sin(\theta) \]
where \(r = \sqrt(x^2 + y^2) \).
)doc");
REGISTER_OP("Betainc")
.Input("a: T")
.Input("b: T")
.Input("x: T")
.Output("z: T")
.Attr("T: {float, double}")
.SetShapeFn([](InferenceContext* c) {
const int num_inputs = 3;
ShapeHandle output = c->UnknownShape();
int num_scalars = 0;
ShapeHandle some_non_scalar;
for (int i = 0; i < num_inputs; ++i) {
ShapeHandle in = c->input(i);
if (!c->RankKnown(in)) {
some_non_scalar = in;
// An input with unknown rank could be either a scalar (to be
// broadcast) or some other shape.
} else if (c->Rank(in) == 0) {
// Input is a scalar, it will be broadcast to the output shape.
++num_scalars;
} else {
TF_RETURN_IF_ERROR(c->Merge(output, in, &output));
some_non_scalar = output;
}
}
if (num_scalars == num_inputs - 1) {
// If all but one input is known to be a scalar, then output is the
// remaining input.
output = some_non_scalar;
} else if (num_scalars == num_inputs) {
// If all are scalars, output is scalar; pick the first one arbitrarily.
output = c->input(0);
}
c->set_output(0, output);
return Status::OK();
})
.Doc(R"doc(
Compute the regularized incomplete beta integral \\(I_x(a, b)\\).
The regularized incomplete beta integral is defined as:
\\(I_x(a, b) = \frac{B(x; a, b)}{B(a, b)}\\)
where
\\(B(x; a, b) = \int_0^x t^{a-1} (1 - t)^{b-1} dt\\)
is the incomplete beta function and \\(B(a, b)\\) is the *complete*
beta function.
)doc");
// --------------------------------------------------------------------------
// Declares cwise binary comparison operations signature: 't, 't -> bool,