forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BinaryOpsKernel.cpp
1446 lines (1376 loc) · 53.3 KB
/
BinaryOpsKernel.cpp
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
#define TORCH_ASSERT_NO_OPERATORS
#include <ATen/native/BinaryOps.h>
#include <cmath>
#include <ATen/Dispatch.h>
#include <ATen/Dispatch_v2.h>
#include <ATen/OpMathType.h>
#include <ATen/Parallel.h>
#include <ATen/cpu/vec/functional.h>
#include <ATen/cpu/vec/vec.h>
#include <ATen/native/Math.h>
#include <ATen/native/TensorIterator.h>
#include <ATen/native/cpu/LogAddExp.h>
#include <ATen/native/cpu/Loops.h>
#include <c10/macros/Macros.h>
#include <c10/util/TypeSafeSignMath.h>
#include <c10/util/generic_math.h>
namespace at::native {
namespace {
using namespace vec;
template <
typename scalar_t,
typename Op,
typename opmath_t = at::opmath_type<scalar_t>,
typename std::enable_if_t<is_reduced_floating_point_v<scalar_t>, int> = 0>
inline Vectorized<scalar_t> binary_op_scalar(
const Vectorized<scalar_t>& a,
opmath_t b,
const Op& op) {
Vectorized<opmath_t> a0, a1, vec_b(b);
std::tie(a0, a1) = convert_to_float<scalar_t>(a);
return convert_from_float<scalar_t>(op(a0, vec_b), op(a1, vec_b));
}
void add_clamp_kernel(
TensorIterator& iter,
const Scalar& alpha_scalar,
const Scalar& min_val,
const Scalar& max_val) {
AT_DISPATCH_ALL_TYPES(iter.dtype(), "add_clamp_cpu", [&]() {
auto alpha = alpha_scalar.to<scalar_t>();
auto alpha_vec = Vectorized<scalar_t>(alpha);
auto min_scalar = min_val.to<scalar_t>();
auto min_vec = Vectorized<scalar_t>(min_scalar);
auto max_scalar = max_val.to<scalar_t>();
auto max_vec = Vectorized<scalar_t>(max_scalar);
cpu_kernel_vec(
iter,
[=](scalar_t a, scalar_t b) __ubsan_ignore_undefined__ -> scalar_t {
return std::min(
max_scalar,
std::max(min_scalar, static_cast<scalar_t>(a + alpha * b)));
},
[=](Vectorized<scalar_t> a, Vectorized<scalar_t> b)
__ubsan_ignore_undefined__ {
auto add_clamp_res = vec::fmadd(b, alpha_vec, a);
add_clamp_res = vec::clamp_min(add_clamp_res, min_vec);
add_clamp_res = vec::clamp_max(add_clamp_res, max_vec);
return add_clamp_res;
});
});
}
void atan2_kernel(TensorIteratorBase& iter) {
AT_DISPATCH_FLOATING_TYPES_AND2(
kBFloat16, kHalf, iter.dtype(), "atan2_cpu", [&]() {
cpu_kernel_vec(
iter,
[=](scalar_t a, scalar_t b) -> scalar_t {
return std::atan2(a, b);
},
[=](Vectorized<scalar_t> a, Vectorized<scalar_t> b) {
return a.atan2(b);
});
});
}
#if !defined(C10_MOBILE)
#define _AT_DISPATCH_ALL_TYPES_AND_BOOL(TYPE, NAME, ...) \
AT_DISPATCH_V2( \
TYPE, \
NAME, \
AT_WRAP(__VA_ARGS__), \
kComplexHalf, \
kHalf, \
kBool, \
kBFloat16, \
kFloat8_e5m2, \
kFloat8_e5m2fnuz, \
kFloat8_e4m3fn, \
kFloat8_e4m3fnuz, AT_EXPAND(AT_ALL_TYPES_AND_COMPLEX), AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES))
#define _AT_DISPATCH_ALL_TYPES_NO_BOOL(TYPE, NAME, ...) \
AT_DISPATCH_V2( \
TYPE, \
NAME, \
AT_WRAP(__VA_ARGS__), \
kComplexHalf, \
kHalf, \
kBFloat16, \
kFloat8_e5m2, \
kFloat8_e4m3fn, \
AT_EXPAND(AT_ALL_TYPES_AND_COMPLEX), AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES))
#define _AT_DISPATCH_MUL_TYPES(TYPE, NAME, ...) \
AT_DISPATCH_V2(TYPE, NAME, AT_WRAP(__VA_ARGS__), \
kHalf, kBFloat16, kFloat8_e5m2, kFloat8_e4m3fn, AT_EXPAND(AT_ALL_TYPES_AND_COMPLEX), AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES))
#else
#define _AT_DISPATCH_ALL_TYPES_AND_BOOL(TYPE, NAME, ...) \
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND4( \
kComplexHalf, kHalf, kBool, kBFloat16, TYPE, NAME, __VA_ARGS__)
#define _AT_DISPATCH_ALL_TYPES_NO_BOOL(TYPE, NAME, ...) \
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND3( \
kComplexHalf, kHalf, kBFloat16, TYPE, NAME, __VA_ARGS__)
#define _AT_DISPATCH_MUL_TYPES(TYPE, NAME, ...) \
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND2( \
kHalf, kBFloat16, TYPE, NAME, __VA_ARGS__)
#endif
void mul_kernel(TensorIteratorBase& iter) {
auto dtype = iter.common_dtype();
if (dtype == ScalarType::Bool) {
cpu_kernel(iter, [=](bool a, bool b) -> bool { return a && b; });
} else if (dtype == kComplexHalf) {
cpu_kernel(
iter,
[=](c10::complex<at::Half> a,
c10::complex<at::Half> b) -> c10::complex<at::Half> {
using comp_t = c10::complex<float>;
return comp_t{a} * comp_t{b};
});
} else if (iter.is_scalar(2) && iter.data_ptr(2) != nullptr && at::isReducedFloatingType(dtype)) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(dtype, "mul_cpu_reduced_float", [&]() {
using opmath_t = at::opmath_type<scalar_t>;
opmath_t b = iter.original_scalar_value<opmath_t>(2);
iter.remove_operand(2);
cpu_kernel_vec(
iter,
[=](scalar_t a) __ubsan_ignore_undefined__ -> scalar_t {
return static_cast<opmath_t>(a) * b;
},
[=](Vectorized<scalar_t> a) __ubsan_ignore_undefined__ {
return binary_op_scalar(
a,
b,
[](const Vectorized<opmath_t>& x,
const Vectorized<opmath_t>& y) { return x * y; });
});
});
} else {
_AT_DISPATCH_MUL_TYPES(dtype, "mul_cpu", [&]() {
cpu_kernel_vec(
iter,
[=](scalar_t a, scalar_t b)
__ubsan_ignore_undefined__ -> scalar_t { return a * b; },
[=](Vectorized<scalar_t> a, Vectorized<scalar_t> b)
__ubsan_ignore_undefined__ { return a * b; });
});
}
}
void div_true_kernel(TensorIteratorBase& iter) {
const auto dtype = iter.common_dtype();
if (iter.is_scalar(2) && iter.data_ptr(2) != nullptr && at::isReducedFloatingType(dtype)) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(dtype, "div_cpu_reduced_float", [&]() {
using opmath_t = at::opmath_type<scalar_t>;
opmath_t b = iter.original_scalar_value<opmath_t>(2);
iter.remove_operand(2);
cpu_kernel_vec(
iter,
[=](scalar_t a) __ubsan_ignore_float_divide_by_zero__ -> scalar_t {
return static_cast<opmath_t>(a) / b;
},
[=](Vectorized<scalar_t> a) {
return binary_op_scalar(
a,
b,
[](const Vectorized<opmath_t>& x,
const Vectorized<opmath_t>& y) { return x / y; });
});
});
} else {
AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES_AND2(
kBFloat16, kHalf, dtype, "div_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b)
__ubsan_ignore_float_divide_by_zero__ -> scalar_t {
return a / b;
},
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b) {
return a / b;
});
});
}
}
void div_trunc_kernel(TensorIteratorBase& iter) {
const auto dtype = iter.common_dtype();
if (isIntegralType(dtype, /*includeBool*/ false)) {
// There's no SIMD integer division, so don't try to vectorize it.
// TODO: if the divisor is a scalar, rewrite as multiplication by a
// constant.
AT_DISPATCH_INTEGRAL_TYPES(dtype, "div_trunc_cpu", [&]() {
cpu_kernel(iter, [](scalar_t a, scalar_t b) -> scalar_t {
TORCH_CHECK(b != 0, "ZeroDivisionError");
return a / b;
});
});
} else if (iter.is_scalar(2) && iter.data_ptr(2) != nullptr && at::isReducedFloatingType(dtype)) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(
dtype, "div_trunc_cpu_reduced_float", [&]() {
using opmath_t = at::opmath_type<scalar_t>;
opmath_t b = iter.original_scalar_value<opmath_t>(2);
iter.remove_operand(2);
cpu_kernel_vec(
iter,
[=](scalar_t a)
__ubsan_ignore_float_divide_by_zero__ -> scalar_t {
return std::trunc(static_cast<opmath_t>(a) / b);
},
[=](Vectorized<scalar_t> a) {
return binary_op_scalar(
a,
b,
[](const Vectorized<opmath_t>& x,
const Vectorized<opmath_t>& y) {
return (x / y).trunc();
});
});
});
} else {
AT_DISPATCH_FLOATING_TYPES_AND2(
kBFloat16, kHalf, dtype, "div_trunc_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b)
__ubsan_ignore_float_divide_by_zero__ -> scalar_t {
return std::trunc(a / b);
},
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b) {
return (a / b).trunc();
});
});
}
}
template <typename scalar_t>
inline Vectorized<scalar_t> div_floor_floating_vec(
const Vectorized<scalar_t>& a,
const Vectorized<scalar_t>& b) {
using vec_t = Vectorized<scalar_t>;
auto mod = a.fmod(b);
auto div = (a - mod) / b;
const auto zero = vec_t(0);
auto mask = (mod != zero) & ((b < zero) ^ (mod < zero));
const auto one = vec_t(1);
div = vec_t::blendv(div, div - one, mask);
auto floordiv = div.floor();
mask = (div - floordiv) > vec_t(0.5);
floordiv = vec_t::blendv(floordiv, floordiv + one, mask);
const auto basic_div = a / b;
floordiv = vec_t::blendv(floordiv, zero.copysign(basic_div), div == zero);
floordiv = vec_t::blendv(floordiv, basic_div, b == zero);
return floordiv;
};
void div_floor_kernel(TensorIteratorBase& iter) {
const auto dtype = iter.common_dtype();
if (dtype == kByte) {
// In the special case of unsigned integer division, floor division is
// equivalent to truncation division (since the signs of the divisor and
// dividend are always the same)
return div_trunc_kernel(iter);
} else if (isIntegralType(dtype, /*includeBool*/ false)) {
// There's no SIMD integer division, so don't try to vectorize it.
AT_DISPATCH_INTEGRAL_TYPES(dtype, "div_floor_cpu", [&]() {
cpu_kernel(iter, [](scalar_t a, scalar_t b) -> scalar_t {
TORCH_CHECK(b != 0, "ZeroDivisionError");
return c10::div_floor_integer(a, b);
});
});
} else {
// See NOTE: [Floor Division in Python]
if (iter.is_scalar(2) && iter.data_ptr(2) != nullptr && at::isReducedFloatingType(dtype)) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(
dtype, "div_floor_cpu_reduced_float", [&]() {
using opmath_t = at::opmath_type<scalar_t>;
opmath_t b = iter.original_scalar_value<opmath_t>(2);
iter.remove_operand(2);
using vec_t = Vectorized<opmath_t>;
cpu_kernel_vec(
iter,
[=](scalar_t a) -> scalar_t {
return c10::div_floor_floating(static_cast<opmath_t>(a), b);
},
[=](Vectorized<scalar_t> a) {
return binary_op_scalar(
a, b, [](const vec_t& x, const vec_t& y) {
return div_floor_floating_vec(x, y);
});
});
});
} else {
AT_DISPATCH_FLOATING_TYPES_AND2(
kBFloat16, kHalf, dtype, "div_floor_cpu", [&]() {
using vec_t = Vectorized<scalar_t>;
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t {
return c10::div_floor_floating(a, b);
},
[](vec_t a, vec_t b) -> vec_t {
return div_floor_floating_vec(a, b);
});
});
}
}
}
void remainder_kernel(TensorIteratorBase& iter) {
if (isIntegralType(iter.common_dtype(), /*includeBool*/ false)) {
AT_DISPATCH_INTEGRAL_TYPES(iter.common_dtype(), "remainder_cpu", [&]() {
cpu_kernel(iter, [](scalar_t a, scalar_t b) -> scalar_t {
TORCH_CHECK(b != 0, "ZeroDivisionError");
scalar_t r = a % b;
if ((r != 0) && (c10::is_negative(r) != c10::is_negative(b))) {
r += b;
}
return r;
});
});
} else if (iter.common_dtype() == kBFloat16) {
cpu_kernel_vec(
iter,
[=](BFloat16 a, BFloat16 b)
__ubsan_ignore_float_divide_by_zero__ -> BFloat16 {
float a0 = static_cast<float>(a);
float b0 = static_cast<float>(b);
float mod0 = std::fmod(a0, b0);
if ((mod0 != 0) && ((b0 < 0) != (mod0 < 0))) {
mod0 += b0;
}
return mod0;
},
[=](Vectorized<BFloat16> a, Vectorized<BFloat16> b) {
Vectorized<float> a0, a1, b0, b1;
std::tie(a0, a1) = convert_bfloat16_float(a);
std::tie(b0, b1) = convert_bfloat16_float(b);
auto mod0 = a0.fmod(b0);
auto mod1 = a1.fmod(b1);
const auto zero = Vectorized<float>(0);
auto mask0 = (mod0 != zero) & ((b0 < zero) ^ (mod0 < zero));
auto mask1 = (mod1 != zero) & ((b1 < zero) ^ (mod1 < zero));
a0 = Vectorized<float>::blendv(mod0, mod0 + b0, mask0);
a1 = Vectorized<float>::blendv(mod1, mod1 + b1, mask1);
return convert_float_bfloat16(a0, a1);
});
} else {
AT_DISPATCH_FLOATING_TYPES_AND_HALF(
iter.common_dtype(), "remainder_cpu", [&]() {
cpu_kernel_vec(
iter,
[=](scalar_t a, scalar_t b)
__ubsan_ignore_float_divide_by_zero__ -> scalar_t {
scalar_t mod = std::fmod(a, b);
if ((mod != 0) && ((b < 0) != (mod < 0)))
mod += b;
return mod;
},
[=](Vectorized<scalar_t> a, Vectorized<scalar_t> b) {
auto mod = a.fmod(b);
const auto zero = Vectorized<scalar_t>(0);
auto mask = (mod != zero) & ((b < zero) ^ (mod < zero));
return Vectorized<scalar_t>::blendv(mod, mod + b, mask);
});
});
}
}
void bitwise_and_kernel(TensorIteratorBase& iter) {
if (iter.dtype() == ScalarType::Bool) {
cpu_kernel(iter, [](bool a, bool b) { return a && b; });
} else {
AT_DISPATCH_INTEGRAL_TYPES(iter.dtype(), "bitwise_and_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t { return a & b; },
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b) { return a & b; });
});
}
}
void bitwise_or_kernel(TensorIteratorBase& iter) {
if (iter.dtype() == ScalarType::Bool) {
cpu_kernel(iter, [](bool a, bool b) { return a || b; });
} else {
AT_DISPATCH_INTEGRAL_TYPES(iter.dtype(), "bitwise_or_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t { return a | b; },
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b) { return a | b; });
});
}
}
void bitwise_xor_kernel(TensorIteratorBase& iter) {
if (iter.dtype() == ScalarType::Bool) {
// Boolean type does not work with ^ (bitwise XOR) in C++. bitwise_xor wraps
// this operation for both Boolean and integral types.
cpu_kernel(iter, [](bool a, bool b) { return a != b; });
} else {
AT_DISPATCH_INTEGRAL_TYPES(iter.dtype(), "bitwise_xor_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t { return a ^ b; },
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b) { return a ^ b; });
});
}
}
void lshift_kernel(TensorIteratorBase& iter) {
AT_DISPATCH_INTEGRAL_TYPES(iter.dtype(), "lshift_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t {
constexpr scalar_t max_shift = sizeof(scalar_t) * CHAR_BIT;
if ((static_cast<std::make_signed_t<scalar_t>>(b) < 0) ||
(b >= max_shift)) {
return 0;
}
return static_cast<std::make_unsigned_t<scalar_t>>(a) << b;
},
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b) { return a << b; });
});
}
void logical_and_kernel(TensorIterator& iter) {
// See Note [special-case bool outputs]
if (iter.dtype() == ScalarType::Bool) {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND3(
kBool, kBFloat16, kHalf, iter.common_dtype(), "logical_and_cpu", [&]() {
cpu_kernel(
iter, [](scalar_t a, scalar_t b) -> bool { return a && b; });
});
} else {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND2(
kBFloat16, kHalf, iter.common_dtype(), "logical_and_cpu", [&]() {
cpu_kernel(iter, [](scalar_t a, scalar_t b) -> scalar_t {
return static_cast<scalar_t>(a && b);
});
});
}
}
void logical_or_kernel(TensorIterator& iter) {
// See Note [special-case bool outputs]
if (iter.dtype() == ScalarType::Bool) {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND3(
kBool, kBFloat16, kHalf, iter.common_dtype(), "logical_or_cpu", [&]() {
cpu_kernel(
iter, [](scalar_t a, scalar_t b) -> bool { return a || b; });
});
} else {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND3(
kBool, kBFloat16, kHalf, iter.common_dtype(), "logical_or_cpu", [&]() {
cpu_kernel(iter, [](scalar_t a, scalar_t b) -> scalar_t {
return static_cast<scalar_t>(a || b);
});
});
}
}
void logical_xor_kernel(TensorIterator& iter) {
// See Note [special-case bool outputs]
if (iter.dtype() == ScalarType::Bool) {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND3(
kBool, kBFloat16, kHalf, iter.common_dtype(), "logical_xor_cpu", [&]() {
cpu_kernel(iter, [](scalar_t a, scalar_t b) -> bool {
return bool(a) != bool(b);
});
});
} else {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND2(
kBFloat16, kHalf, iter.common_dtype(), "logical_xor_cpu", [&]() {
cpu_kernel(iter, [](scalar_t a, scalar_t b) -> scalar_t {
return static_cast<scalar_t>(bool(a) != bool(b));
});
});
}
}
void rshift_kernel(TensorIteratorBase& iter) {
AT_DISPATCH_INTEGRAL_TYPES(iter.dtype(), "rshift_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t {
// right shift value to retain sign bit for signed and no bits for
// unsigned
constexpr scalar_t max_shift =
sizeof(scalar_t) * CHAR_BIT - std::is_signed_v<scalar_t>;
if ((static_cast<std::make_signed_t<scalar_t>>(b) < 0) ||
(b >= max_shift)) {
return a >> max_shift;
}
return a >> b;
},
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b) { return a >> b; });
});
}
void lt_kernel(TensorIteratorBase& iter) {
// See Note [special-case bool outputs]
if (iter.dtype() == ScalarType::Bool) {
AT_DISPATCH_ALL_TYPES_AND3(
kBool, kBFloat16, kHalf, iter.common_dtype(), "lt_cpu", [&]() {
cpu_kernel(
iter, [](scalar_t a, scalar_t b) -> bool { return a < b; });
});
} else {
AT_DISPATCH_ALL_TYPES_AND2(
kBFloat16, kHalf, iter.common_dtype(), "lt_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t { return a < b; },
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b)
-> Vectorized<scalar_t> { return a.lt(b); });
});
}
}
void le_kernel(TensorIteratorBase& iter) {
// See Note [special-case bool outputs]
if (iter.dtype() == ScalarType::Bool) {
AT_DISPATCH_ALL_TYPES_AND3(
kBool, kBFloat16, kHalf, iter.common_dtype(), "le_cpu", [&]() {
cpu_kernel(
iter, [](scalar_t a, scalar_t b) -> bool { return a <= b; });
});
} else {
AT_DISPATCH_ALL_TYPES_AND2(
kBFloat16, kHalf, iter.common_dtype(), "le_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t { return a <= b; },
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b)
-> Vectorized<scalar_t> { return a.le(b); });
});
}
}
void gt_kernel(TensorIteratorBase& iter) {
// See Note [special-case bool outputs]
if (iter.dtype() == ScalarType::Bool) {
AT_DISPATCH_ALL_TYPES_AND3(
kBool, kBFloat16, kHalf, iter.common_dtype(), "gt_cpu", [&]() {
cpu_kernel(
iter, [](scalar_t a, scalar_t b) -> bool { return a > b; });
});
} else {
AT_DISPATCH_ALL_TYPES_AND2(
kBFloat16, kHalf, iter.common_dtype(), "gt_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t { return a > b; },
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b)
-> Vectorized<scalar_t> { return a.gt(b); });
});
}
}
void ge_kernel(TensorIteratorBase& iter) {
// See Note [special-case bool outputs]
if (iter.dtype() == ScalarType::Bool) {
AT_DISPATCH_ALL_TYPES_AND3(
kBool, kBFloat16, kHalf, iter.common_dtype(), "ge_cpu", [&]() {
cpu_kernel(
iter, [](scalar_t a, scalar_t b) -> bool { return a >= b; });
});
} else {
AT_DISPATCH_ALL_TYPES_AND2(
kBFloat16, kHalf, iter.common_dtype(), "ge_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t { return a >= b; },
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b)
-> Vectorized<scalar_t> { return a.ge(b); });
});
}
}
void eq_kernel(TensorIteratorBase& iter) {
// See Note [special-case bool outputs]
if (iter.dtype() == ScalarType::Bool) {
_AT_DISPATCH_ALL_TYPES_AND_BOOL(iter.common_dtype(), "eq_cpu", [&]() {
cpu_kernel(iter, [](scalar_t a, scalar_t b) -> bool { return a == b; });
});
} else {
_AT_DISPATCH_ALL_TYPES_NO_BOOL(iter.common_dtype(), "eq_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t {
return static_cast<scalar_t>(a == b);
},
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b)
-> Vectorized<scalar_t> { return a.eq(b); });
});
}
}
void ne_kernel(TensorIteratorBase& iter) {
// See Note [special-case bool outputs]
if (iter.dtype() == ScalarType::Bool) {
_AT_DISPATCH_ALL_TYPES_AND_BOOL(iter.common_dtype(), "ne_cpu", [&]() {
cpu_kernel(iter, [](scalar_t a, scalar_t b) -> bool { return a != b; });
});
} else {
_AT_DISPATCH_ALL_TYPES_NO_BOOL(iter.common_dtype(), "ne_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t {
return static_cast<scalar_t>(a != b);
},
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b)
-> Vectorized<scalar_t> { return a.ne(b); });
});
}
}
void maximum_kernel(TensorIteratorBase& iter) {
if (iter.dtype() == ScalarType::Bool) {
cpu_kernel(iter, [](bool a, bool b) -> bool { return a || b; });
} else if (isIntegralType(iter.dtype(), /*includeBool=*/false)) {
AT_DISPATCH_INTEGRAL_TYPES(iter.dtype(), "maximum_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t { return std::max(a, b); },
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b) {
return at::vec::maximum(a, b);
});
});
} else {
AT_DISPATCH_FLOATING_TYPES_AND2(
at::ScalarType::Half,
at::ScalarType::BFloat16,
iter.dtype(),
"maximum_cpu",
[&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t {
if (a != a || b != b) {
return std::numeric_limits<scalar_t>::quiet_NaN();
} else {
return std::max(a, b);
}
},
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b) {
return at::vec::maximum(a, b);
});
});
}
}
void minimum_kernel(TensorIteratorBase& iter) {
if (iter.dtype() == ScalarType::Bool) {
cpu_kernel(iter, [](bool a, bool b) -> bool { return a && b; });
} else if (isIntegralType(iter.dtype(), /*includeBool=*/false)) {
AT_DISPATCH_INTEGRAL_TYPES(iter.dtype(), "minimum_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t { return std::min(a, b); },
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b) {
return at::vec::minimum(a, b);
});
});
} else {
AT_DISPATCH_FLOATING_TYPES_AND2(
at::ScalarType::Half,
at::ScalarType::BFloat16,
iter.dtype(),
"minimum_cpu",
[&]() {
cpu_kernel_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t {
if (a != a || b != b) {
return std::numeric_limits<scalar_t>::quiet_NaN();
} else {
return std::min(a, b);
}
},
[](Vectorized<scalar_t> a, Vectorized<scalar_t> b) {
return at::vec::minimum(a, b);
});
});
}
}
void fmax_kernel(TensorIteratorBase& iter) {
if (isFloatingType(iter.common_dtype())) {
AT_DISPATCH_FLOATING_TYPES_AND2(
at::ScalarType::Half,
at::ScalarType::BFloat16,
iter.common_dtype(),
"fmax_cpu",
[&]() {
cpu_kernel(iter, [](scalar_t a, scalar_t b) -> scalar_t {
return std::fmax(a, b);
});
});
} else {
maximum_kernel(iter);
}
}
void fmin_kernel(TensorIteratorBase& iter) {
if (isFloatingType(iter.common_dtype())) {
AT_DISPATCH_FLOATING_TYPES_AND2(
at::ScalarType::Half,
at::ScalarType::BFloat16,
iter.common_dtype(),
"fmin_cpu",
[&]() {
cpu_kernel(iter, [](scalar_t a, scalar_t b) -> scalar_t {
return std::fmin(a, b);
});
});
} else {
minimum_kernel(iter);
}
}
void smooth_l1_kernel(TensorIteratorBase& iter, double beta) {
if (iter.dtype() == kBFloat16) {
const float beta_val(beta);
const Vectorized<float> beta_val_vec(beta_val);
const Vectorized<float> point_five_vec(static_cast<float>(0.5));
cpu_kernel_vec(
iter,
[&beta_val](BFloat16 a, BFloat16 b) -> BFloat16 {
auto z = std::abs(float(a) - float(b));
return z < beta_val ? static_cast<float>(0.5) * z * z / beta_val
: z - static_cast<float>(0.5) * beta_val;
},
[&beta_val_vec, &point_five_vec](
Vectorized<BFloat16> a, Vectorized<BFloat16> b) {
Vectorized<float> a0, a1, b0, b1;
std::tie(a0, a1) = convert_bfloat16_float(a);
std::tie(b0, b1) = convert_bfloat16_float(b);
auto z = (a0 - b0).abs();
a0 = Vectorized<float>::blendv(
point_five_vec * z * z / beta_val_vec,
z - point_five_vec * beta_val_vec,
z >= beta_val_vec);
z = (a1 - b1).abs();
a1 = Vectorized<float>::blendv(
point_five_vec * z * z / beta_val_vec,
z - point_five_vec * beta_val_vec,
z >= beta_val_vec);
return convert_float_bfloat16(a0, a1);
});
} else {
AT_DISPATCH_FLOATING_TYPES_AND(kHalf, iter.dtype(), "smooth_l1_cpu", [&]() {
using Vec = Vectorized<scalar_t>;
const scalar_t beta_val(beta);
const Vec beta_val_vec(beta_val);
const Vec point_five_vec(static_cast<scalar_t>(0.5));
cpu_kernel_vec(
iter,
[&beta_val](scalar_t a, scalar_t b) -> scalar_t {
auto z = std::abs(a - b);
return z < beta_val ? static_cast<scalar_t>(0.5) * z * z / beta_val
: z - static_cast<scalar_t>(0.5) * beta_val;
},
[&beta_val_vec, &point_five_vec](Vec a, Vec b) {
auto z = (a - b).abs();
return Vec::blendv(
point_five_vec * z * z / beta_val_vec,
z - point_five_vec * beta_val_vec,
z >= beta_val_vec);
});
});
}
}
void huber_kernel(TensorIterator& iter, double delta) {
AT_DISPATCH_FLOATING_TYPES_AND2(
kBFloat16, kHalf, iter.dtype(), "huber_cpu", [&]() {
using Vec = Vectorized<scalar_t>;
const scalar_t delta_val(delta);
const Vec delta_val_vec(delta_val);
const Vec point_five_vec(static_cast<scalar_t>(0.5));
cpu_kernel_vec(
iter,
[&delta_val](scalar_t a, scalar_t b) -> scalar_t {
auto z = std::abs(a - b);
return z < delta_val
? static_cast<scalar_t>(0.5) * z * z
: delta_val * (z - static_cast<scalar_t>(0.5) * delta_val);
},
[&delta_val_vec, &point_five_vec](Vec a, Vec b) {
auto z = (a - b).abs();
return Vec::blendv(
point_five_vec * z * z,
delta_val_vec * (z - point_five_vec * delta_val_vec),
z >= delta_val_vec);
});
});
}
void sigmoid_backward_kernel(TensorIteratorBase& iter) {
if (isComplexType(iter.dtype())) {
AT_DISPATCH_COMPLEX_TYPES(iter.dtype(), "sigmoid_backward_cpu", [&]() {
auto one_vec = Vectorized<scalar_t>(scalar_t{1});
cpu_kernel_vec(
iter,
[=](scalar_t a, scalar_t b) -> scalar_t {
return a * std::conj((scalar_t(1) - b) * b);
},
[=](Vectorized<scalar_t> a, Vectorized<scalar_t> b) {
return a * ((one_vec - b) * b).conj();
});
});
} else if (iter.dtype() == kBFloat16) {
auto one_vec = Vectorized<float>((float)(1));
cpu_kernel_vec(
iter,
[=](BFloat16 a, BFloat16 b) -> BFloat16 {
float a0 = static_cast<float>(a);
float b0 = static_cast<float>(b);
return a0 * (float(1) - b0) * b0;
},
[=](Vectorized<BFloat16> a, Vectorized<BFloat16> b) {
Vectorized<float> a0, a1, b0, b1;
std::tie(a0, a1) = convert_bfloat16_float(a);
std::tie(b0, b1) = convert_bfloat16_float(b);
a0 = a0 * (one_vec - b0) * b0;
a1 = a1 * (one_vec - b1) * b1;
return convert_float_bfloat16(a0, a1);
});
} else {
AT_DISPATCH_FLOATING_TYPES_AND(
kHalf, iter.dtype(), "sigmoid_backward_cpu", [&]() {
auto one_vec = Vectorized<scalar_t>((scalar_t)(1));
cpu_kernel_vec(
iter,
[=](scalar_t a, scalar_t b) -> scalar_t {
return a * (scalar_t(1) - b) * b;
},
[=](Vectorized<scalar_t> a, Vectorized<scalar_t> b) {
return a * (one_vec - b) * b;
});
});
}
}
void logit_backward_kernel(TensorIteratorBase& iter, const Scalar& eps_scalar) {
AT_DISPATCH_FLOATING_TYPES_AND2(
kBFloat16, kHalf, iter.dtype(), "logit_backward_cpu", [&]() {
const scalar_t eps = eps_scalar.to<scalar_t>();
const Vectorized<scalar_t> kZeroVec(scalar_t(0));
const Vectorized<scalar_t> kOneVec(scalar_t(1));
if (eps < scalar_t(0)) {
const Vectorized<scalar_t> kNanVec(
std::numeric_limits<scalar_t>::quiet_NaN());
cpu_kernel_vec(
iter,
[](scalar_t dy, scalar_t x) {
return (x < scalar_t(0) || x > scalar_t(1))
? std::numeric_limits<scalar_t>::quiet_NaN()
: ((x == scalar_t(0) || x == scalar_t(1))
? (dy * std::numeric_limits<scalar_t>::infinity())
: (dy / (x * (scalar_t(1) - x))));
},
[kZeroVec, kOneVec, kNanVec](
Vectorized<scalar_t> dy_vec, Vectorized<scalar_t> x_vec) {
return Vectorized<scalar_t>::blendv(
kNanVec,
dy_vec / (x_vec * (kOneVec - x_vec)),
(x_vec >= kZeroVec) & (x_vec <= kOneVec));
});
} else {
const scalar_t lo = eps;
const scalar_t hi = scalar_t(1) - eps;
const Vectorized<scalar_t> lo_vec(lo);
const Vectorized<scalar_t> hi_vec(hi);
cpu_kernel_vec(
iter,
[lo, hi](scalar_t dy, scalar_t x) {
return (x < lo || x > hi)
? scalar_t(0)
: ((x == scalar_t(0) || x == scalar_t(1))
? dy * std::numeric_limits<scalar_t>::infinity()
: dy / (x * (scalar_t(1) - x)));
},
[kZeroVec, kOneVec, lo_vec, hi_vec](
Vectorized<scalar_t> dy_vec, Vectorized<scalar_t> x_vec) {
return Vectorized<scalar_t>::blendv(
kZeroVec,
dy_vec / (x_vec * (kOneVec - x_vec)),
(x_vec >= lo_vec) & (x_vec <= hi_vec));
});
}
});
}
void tanh_backward_kernel(TensorIteratorBase& iter) {
if (isComplexType(iter.dtype())) {
AT_DISPATCH_COMPLEX_TYPES(iter.dtype(), "tanh_backward_cpu", [&]() {
auto one_vec = Vectorized<scalar_t>(scalar_t{1});
cpu_kernel_vec(
iter,
[=](scalar_t a, scalar_t b) -> scalar_t {
return a * std::conj(scalar_t{1} - b * b);
},
[=](Vectorized<scalar_t> a, Vectorized<scalar_t> b) {
return a * (one_vec - b * b).conj();
});
});
} else if (at::isReducedFloatingType(iter.dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(
iter.dtype(), "tanh_backward_cpu", [&]() {
auto one_vec = Vectorized<float>(float{1});
cpu_kernel_vec(
iter,
[=](scalar_t a, scalar_t b) -> scalar_t {
float a0 = float(a);
float b0 = float(b);
return a0 * (float{1} - b0 * b0);
},
[=](Vectorized<scalar_t> a, Vectorized<scalar_t> b) {
Vectorized<float> a0, a1, b0, b1;
std::tie(a0, a1) = convert_to_float<scalar_t>(a);
std::tie(b0, b1) = convert_to_float<scalar_t>(b);
a0 = a0 * (one_vec - b0 * b0);
a1 = a1 * (one_vec - b1 * b1);
return convert_from_float<scalar_t>(a0, a1);
});
});
} else {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "tanh_backward_cpu", [&]() {
auto one_vec = Vectorized<scalar_t>(scalar_t{1});
cpu_kernel_vec(
iter,
[=](scalar_t a, scalar_t b) -> scalar_t {
return a * (scalar_t{1} - b * b);
},
[=](Vectorized<scalar_t> a, Vectorized<scalar_t> b) {
return a * (one_vec - b * b);
});
});
}
}
void mse_kernel(TensorIteratorBase& iter) {
if (iter.dtype() == ScalarType::Half) {
TORCH_WARN_ONCE(
"Applying the CPU mse kernel on half-type tensors. "
"This may be slower than using float or double-type tensors.");
}
AT_DISPATCH_FLOATING_TYPES_AND_HALF(iter.dtype(), "mse_cpu", [&]() {
cpu_kernel_vec(
iter,
[=](scalar_t a, scalar_t b) -> scalar_t {
auto diff = a - b;
return diff * diff;
},
[=](Vectorized<scalar_t> a, Vectorized<scalar_t> b) {
auto diff = a - b;
return diff * diff;
});
});
}
void fmod_kernel(TensorIteratorBase& iter) {
if (isIntegralType(iter.common_dtype(), /*includeBool=*/false)) {
AT_DISPATCH_INTEGRAL_TYPES(iter.common_dtype(), "fmod_cpu", [&]() {
cpu_kernel(iter, [=](scalar_t x, scalar_t d) -> scalar_t {
TORCH_CHECK(d != 0, "ZeroDivisionError");
return x % d;
});
});
} else {
AT_DISPATCH_FLOATING_TYPES_AND2(
kBFloat16, kHalf, iter.common_dtype(), "fmod_cpu", [&]() {
cpu_kernel_vec(
iter,
[](scalar_t x, scalar_t d) -> scalar_t {
return std::fmod(x, d);
},
[](Vectorized<scalar_t> x, Vectorized<scalar_t> d) {
return x.fmod(d);
});
});
}