forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Activation.cpp
1464 lines (1408 loc) · 60.6 KB
/
Activation.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
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <ATen/native/Activation.h>
#include <cmath>
#include <functional>
#include <ATen/Dispatch.h>
#include <ATen/OpMathType.h>
#include <ATen/core/TensorBase.h>
#include <ATen/cpu/vec/functional.h>
#include <ATen/cpu/vec/vec.h>
#include <ATen/cpu/vec/functional.h>
#include <ATen/native/TensorIterator.h>
#include <ATen/native/cpu/Loops.h>
#include <ATen/Parallel.h>
#include <c10/core/Scalar.h>
namespace at::native {
namespace {
static void log_sigmoid_cpu_kernel(TensorBase &output, TensorBase &buffer, const TensorBase &input) {
if (at::isReducedFloatingType(input.scalar_type())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(input.scalar_type(), "log_sigmoid_cpu", [&]() {
using Vec = Vectorized<scalar_t>;
scalar_t* output_data = output.data_ptr<scalar_t>();
scalar_t* buffer_data = buffer.data_ptr<scalar_t>();
scalar_t* input_data = input.data_ptr<scalar_t>();
parallel_for(0, input.numel(), 1, [&] (int64_t begin, int64_t end) {
int64_t size = end - begin;
int64_t d = 0;
for (; d < size - (size % Vec::size()); d += Vec::size()) {
Vec data_vec = Vec::loadu(input_data + begin+ d);
Vectorized<float> data_vec0, data_vec1;
std::tie(data_vec0, data_vec1) = convert_to_float<scalar_t>(data_vec);
Vectorized<float> min_vec = minimum(data_vec0, Vectorized<float>(float(0)));
Vectorized<float> buffer_vec0 = data_vec0.abs().neg().exp();
Vectorized<float> output_vec0 = min_vec - buffer_vec0.log1p();
min_vec = minimum(data_vec1, Vectorized<float>(float(0)));
Vectorized<float> buffer_vec1 = data_vec1.abs().neg().exp();
Vectorized<float> output_vec1 = min_vec - buffer_vec1.log1p();
convert_from_float<scalar_t>(buffer_vec0, buffer_vec1).store(buffer_data + begin + d);
convert_from_float<scalar_t>(output_vec0, output_vec1).store(output_data + begin + d);
}
if (size - d > 0) {
Vec data_vec = Vec::loadu(input_data + begin + d, size - d);
Vectorized<float> data_vec0, data_vec1;
std::tie(data_vec0, data_vec1) = convert_to_float<scalar_t>(data_vec);
Vectorized<float> min_vec = minimum(data_vec0, Vectorized<float>(float(0)));
Vectorized<float> buffer_vec0 = data_vec0.abs().neg().exp();
Vectorized<float> output_vec0 = min_vec - buffer_vec0.log1p();
min_vec = minimum(data_vec1, Vectorized<float>(float(0)));
Vectorized<float> buffer_vec1 = data_vec1.abs().neg().exp();
Vectorized<float> output_vec1 = min_vec - buffer_vec1.log1p();
convert_from_float<scalar_t>(buffer_vec0, buffer_vec1).store(buffer_data + begin + d, size - d);
convert_from_float<scalar_t>(output_vec0, output_vec1).store(output_data + begin + d, size - d);
}
});
});
} else {
AT_DISPATCH_FLOATING_TYPES(input.scalar_type(), "log_sigmoid_cpu", [&] {
using Vec = Vectorized<scalar_t>;
scalar_t* output_data = output.data_ptr<scalar_t>();
scalar_t* buffer_data = buffer.data_ptr<scalar_t>();
scalar_t* input_data = input.data_ptr<scalar_t>();
parallel_for(0, input.numel(), 1, [&] (int64_t begin, int64_t end) {
int64_t size = end - begin;
int64_t d = 0;
for (; d < size - (size % Vec::size()); d += Vec::size()) {
Vec data_vec = Vec::loadu(input_data + begin+ d);
Vec min_vec = vec::minimum(data_vec, Vec(scalar_t(0)));
Vec buffer_vec = data_vec.abs().neg().exp();
Vec output_vec = min_vec - buffer_vec.log1p();
buffer_vec.store(buffer_data + begin + d);
output_vec.store(output_data + begin + d);
}
if (size - d > 0) {
Vec data_vec = Vec::loadu(input_data + begin + d, size - d);
Vec min_vec = vec::minimum(data_vec, Vec(scalar_t(0)));
Vec buffer_vec = data_vec.abs().neg().exp();
Vec output_vec = min_vec - buffer_vec.log1p();
buffer_vec.store(buffer_data + begin + d, size - d);
output_vec.store(output_data + begin + d, size - d);
}
});
});
}
}
static void log_sigmoid_backward_cpu_kernel(TensorIterator& iter) {
if (at::isReducedFloatingType(iter.dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(iter.dtype(), "log_sigmoid_backward_cpu", [&]() {
using Vec = Vectorized<scalar_t>;
auto zero_val = float(0);
auto zero_vec = Vectorized<float>(zero_val);
auto one_val = float(1);
auto one_vec = Vectorized<float>(one_val);
cpu_kernel_vec(iter,
[=](scalar_t a, scalar_t b, scalar_t c) -> scalar_t {
auto in_negative = float(a) < float(0);
auto max_deriv = in_negative ? float(1) : float(0);
auto sign = in_negative ? float(1) : -float(1);
return (max_deriv - sign * (float(b) / (float(1) + b))) * float(c);
},
[=](Vec a, Vec b, Vec c) -> Vec {
Vectorized<float> a0, a1, b0, b1, c0, c1;
std::tie(a0, a1) = convert_to_float<scalar_t>(a);
std::tie(b0, b1) = convert_to_float<scalar_t>(b);
std::tie(c0, c1) = convert_to_float<scalar_t>(c);
auto mask = a0 < zero_vec;
auto max_deriv_vec = Vectorized<float>::blendv(zero_vec, one_vec, mask);
auto sign_vec = Vectorized<float>::blendv(one_vec.neg(), one_vec, mask);
a0 = (max_deriv_vec - sign_vec * (b0 / (one_vec + b0))) * c0;
mask = a1 < zero_vec;
max_deriv_vec = Vectorized<float>::blendv(zero_vec, one_vec, mask);
sign_vec = Vectorized<float>::blendv(one_vec.neg(), one_vec, mask);
a1 = (max_deriv_vec - sign_vec * (b1 / (one_vec + b1))) * c1;
return convert_from_float<scalar_t>(a0, a1);
});
});
} else {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "log_sigmoid_backward_cpu", [&]() {
using Vec = Vectorized<scalar_t>;
auto zero_val = scalar_t(0);
auto zero_vec = Vec(zero_val);
auto one_val = scalar_t(1);
auto one_vec = Vec(one_val);
cpu_kernel_vec(iter,
[=](scalar_t a, scalar_t b, scalar_t c) -> scalar_t {
auto in_negative = a < scalar_t(0);
auto max_deriv = in_negative ? scalar_t(1) : scalar_t(0);
auto sign = in_negative ? scalar_t(1) : -scalar_t(1);
return (max_deriv - sign * (b / (scalar_t(1) + b))) * c;
},
[=](Vec a, Vec b, Vec c) -> Vec {
auto mask = a < zero_vec;
auto max_deriv_vec = Vec::blendv(zero_vec, one_vec, mask);
auto sign_vec = Vec::blendv(one_vec.neg(), one_vec, mask);
return (max_deriv_vec - sign_vec * (b / (one_vec + b))) * c;
});
});
}
}
static void threshold_kernel(
TensorIteratorBase& iter,
const Scalar& threshold_scalar,
const Scalar& value_scalar) {
if (at::isReducedFloatingType(iter.dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(iter.dtype(), "threshold_cpu", [&]() {
using Vec = Vectorized<float>;
float threshold = threshold_scalar.to<float>();
Vec threshold_v = Vec(threshold);
scalar_t value = value_scalar.to<scalar_t>();
Vec value_v = Vec(float(value));
cpu_kernel_vec(
iter,
[&](scalar_t x, scalar_t other) -> scalar_t {
return float(x) <= threshold ? value : other;
},
[&](Vectorized<scalar_t> x, Vectorized<scalar_t> other) -> Vectorized<scalar_t> {
Vec x0, x1, other0, other1;
std::tie(x0, x1) = convert_to_float<scalar_t>(x);
std::tie(other0, other1) = convert_to_float<scalar_t>(other);
return convert_from_float<scalar_t>(Vec::blendv(other0, value_v, x0 <= threshold_v),
Vec::blendv(other1, value_v, x1 <= threshold_v));
});
});
} else {
AT_DISPATCH_ALL_TYPES(iter.dtype(), "threshold_cpu", [&] {
using Vec = Vectorized<scalar_t>;
scalar_t threshold = threshold_scalar.to<scalar_t>();
Vec threshold_v = Vec(threshold);
scalar_t value = value_scalar.to<scalar_t>();
Vec value_v = Vec(value);
cpu_kernel_vec(
iter,
[&](scalar_t x, scalar_t other) -> scalar_t {
return x <= threshold ? value : other;
},
[&](Vec x, Vec other) -> Vec {
return Vec::blendv(other, value_v, x <= threshold_v);
});
});
}
}
void elu_kernel(TensorIteratorBase& it, const Scalar& alpha, const Scalar& scale, const Scalar& input_scale) {
if (at::isReducedFloatingType(it.common_dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(it.common_dtype(), "elu_cpu", [&]() {
auto negcoef = alpha.to<float>() * scale.to<float>();
auto poscoef = scale.to<float>();
auto negiptcoef = input_scale.to<float>();
const Vectorized<float> negcoef_vec(negcoef);
const Vectorized<float> negiptcoef_vec(negiptcoef);
const Vectorized<float> poscoef_vec(poscoef);
const Vectorized<float> one_vec(static_cast<float>(1));
const Vectorized<float> zero_vec(static_cast<float>(0));
cpu_kernel_vec(
it,
[negcoef, negiptcoef, poscoef](scalar_t a) -> scalar_t {
return float(a) <= float(0) ? (std::exp(float(a) * negiptcoef) - float(1)) * negcoef : float(a) * poscoef;
},
[&negcoef_vec, &negiptcoef_vec, &poscoef_vec, &one_vec, &zero_vec](Vectorized<scalar_t> a) -> Vectorized<scalar_t> {
Vectorized<float> a0, a1, res0, res1;
std::tie(a0, a1) = convert_to_float<scalar_t>(a);
auto cmp0 = (a0 > zero_vec);
auto cmp1 = (a1 > zero_vec);
auto get_res_masked = [&](Vectorized<float>& cmp, Vectorized<float>& a) {
return !cmp.zero_mask() ? a * poscoef_vec :
Vectorized<float>::blendv(((a * negiptcoef_vec).exp() - one_vec) * negcoef_vec, a * poscoef_vec, cmp);
};
res0 = get_res_masked(cmp0, a0);
res1 = get_res_masked(cmp1, a1);
return convert_from_float<scalar_t>(res0, res1);
});
});
} else {
AT_DISPATCH_FLOATING_TYPES(it.common_dtype(), "elu_cpu", [&]() {
using Vec = Vectorized<scalar_t>;
auto negcoef = alpha.to<scalar_t>() * scale.to<scalar_t>();
auto poscoef = scale.to<scalar_t>();
auto negiptcoef = input_scale.to<scalar_t>();
const Vec negcoef_vec(negcoef);
const Vec negiptcoef_vec(negiptcoef);
const Vec poscoef_vec(poscoef);
const Vec one_vec(static_cast<scalar_t>(1));
const Vec zero_vec(static_cast<scalar_t>(0));
cpu_kernel_vec(
it,
[negcoef, negiptcoef, poscoef](scalar_t a) -> scalar_t {
return a <= scalar_t(0) ? (std::exp(a * negiptcoef) - scalar_t(1)) * negcoef : a * poscoef;
},
[&negcoef_vec, &negiptcoef_vec, &poscoef_vec, &one_vec, &zero_vec](Vec a) -> Vec {
auto cmp = (a > zero_vec);
if (!cmp.zero_mask()) { // only a * poscoef (which is very quick) needs to be computed
return a * poscoef_vec;
} else {
return Vec::blendv(((a * negiptcoef_vec).exp() - one_vec) * negcoef_vec, a * poscoef_vec, cmp);
}
});
});
}
}
void elu_backward_kernel(TensorIteratorBase& it, const Scalar& alpha, const Scalar& scale, const Scalar& input_scale, bool is_result) {
if (at::isReducedFloatingType(it.common_dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(it.common_dtype(), "elu_backward_cpu", [&]() {
auto negcoef = alpha.to<float>() * scale.to<float>();
auto poscoef = scale.to<float>();
auto negiptcoef = input_scale.to<float>();
const Vectorized<float> negcoef_vec(negcoef);
const Vectorized<float> negiptcoef_vec(negiptcoef);
const Vectorized<float> poscoef_vec(poscoef);
const Vectorized<float> zero_vec(static_cast<float>(0));
cpu_kernel_vec(
it,
[negcoef, negiptcoef, poscoef, is_result](scalar_t a, scalar_t b) -> scalar_t {
if (is_result) {
return float(b) <= float(0) ? float(a) * negiptcoef * (float(b) + negcoef) : float(a) * poscoef;
} else {
return float(b) <= float(0) ? float(a) * negiptcoef * negcoef * std::exp(float(b) * negiptcoef): float(a) * poscoef;
}
},
[&negcoef_vec, &negiptcoef_vec, &poscoef_vec, &zero_vec, is_result](Vectorized<scalar_t> a, Vectorized<scalar_t> b) -> Vectorized<scalar_t> {
Vectorized<float> a0, a1, res0, res1;
std::tie(a0, a1) = convert_to_float<scalar_t>(a);
Vectorized<float> b0, b1;
std::tie(b0, b1) = convert_to_float<scalar_t>(b);
auto cmp0 = (b0 > zero_vec);
auto cmp1 = (b1 > zero_vec);
auto get_res_masked = [&](Vectorized<float>& cmp, Vectorized<float>& a, Vectorized<float>& b) {
if (is_result) {
return !cmp.zero_mask() ? a * poscoef_vec :
Vectorized<float>::blendv(a * negiptcoef_vec * (b + negcoef_vec), a * poscoef_vec, cmp);
} else {
return Vectorized<float>::blendv(a * negiptcoef_vec * negcoef_vec * (b * negiptcoef_vec).exp(), a * poscoef_vec, cmp);
}
};
res0 = get_res_masked(cmp0, a0, b0);
res1 = get_res_masked(cmp1, a1, b1);
return convert_from_float<scalar_t>(res0, res1);
});
});
} else {
AT_DISPATCH_FLOATING_TYPES(it.dtype(), "elu_backward_cpu", [&]() {
using Vec = Vectorized<scalar_t>;
auto negcoef = alpha.to<scalar_t>() * scale.to<scalar_t>();
auto poscoef = scale.to<scalar_t>();
auto negiptcoef = input_scale.to<scalar_t>();
const Vec negcoef_vec(negcoef);
const Vec negiptcoef_vec(negiptcoef);
const Vec poscoef_vec(poscoef);
const Vec zero_vec(static_cast<scalar_t>(0));
cpu_kernel_vec(
it,
[negcoef, negiptcoef, poscoef, is_result](scalar_t a, scalar_t b) -> scalar_t {
if (is_result) {
return b <= scalar_t(0) ? a * negiptcoef * (b + negcoef) : a * poscoef;
} else {
return b <= scalar_t(0) ? a * negiptcoef * negcoef * std::exp(b * negiptcoef): a * poscoef;
}
},
[&negcoef_vec, &negiptcoef_vec, &poscoef_vec, &zero_vec, is_result](Vec a, Vec b) -> Vec {
auto cmp = (b > zero_vec);
if (is_result) {
if (!cmp.zero_mask()) { // only a * poscoef (which is very quick) needs to be computed
return a * poscoef_vec;
} else {
return Vec::blendv(a * negiptcoef_vec * (b + negcoef_vec), a * poscoef_vec, cmp);
}
} else {
return Vec::blendv(a * negiptcoef_vec * negcoef_vec * (b * negiptcoef_vec).exp(), a * poscoef_vec, cmp);
}
}
);
});
}
}
// TODO(yangxm): Add another fast kernel using formula
// y = 0.5x * (1 + tanh(sqrt(2/Pi) * (x + 0.044715x^3)))
// and the fast tanh impl from Eigen.
void GeluKernelImpl(TensorIteratorBase& it, GeluType approximate) {
auto grain_size = at::internal::GRAIN_SIZE;
// Numbers based on benchmarking.
// Benchmark: benchmarks/operator_benchmarks/pt/gelu_test.py
#ifdef C10_MOBILE
// Benchmarked on S8 US phone.
// Internal benchmarking that converts operator benchmark into
// a torchscript module and run that on mobile.
// Same benchmark as server side.
constexpr int64_t GELU_MIN_ELEMENTS_FOR_MULTI_THREADING{6144};
#else
// Benchmarked on i9 8 core 16 thread machine.
// 1 thread: cd benchmark/operator_benchmarks;
// python -m pt.gelu_test --tag_filter long --omp_num_threads 1
// 2 threads: cd benchmark/operator_benchmarks;
// python -m pt.gelu_test --tag_filter long --omp_num_threads 1
constexpr int64_t GELU_MIN_ELEMENTS_FOR_MULTI_THREADING{16384};
#endif
if (it.numel() > GELU_MIN_ELEMENTS_FOR_MULTI_THREADING) {
grain_size = it.numel() / at::get_num_threads();
}
if (approximate == GeluType::Tanh) {
if (at::isReducedFloatingType(it.common_dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(it.common_dtype(), "GeluKernelImpl", [&]() {
auto kBetaVec = Vectorized<float>((float)(M_SQRT2 * M_2_SQRTPI * 0.5));
auto kKappaVec = Vectorized<float>((float)(0.044715));
auto kOneVec = Vectorized<float>((float)(1));
auto kPointFiveVec = Vectorized<float>((float)(0.5));
cpu_kernel_vec(
it,
[](scalar_t x) -> scalar_t {
const float kBeta = float(M_SQRT2 * M_2_SQRTPI * 0.5);
const float kKappa = float(0.044715);
float x_cube = float(x) * float(x) * float(x);
float inner = kBeta * (float(x) + kKappa * x_cube);
return float(0.5) * float(x) * (float(1) + std::tanh(inner));
},
[&](Vectorized<scalar_t> x) -> Vectorized<scalar_t> {
Vectorized<float> x0, x1;
std::tie(x0, x1) = convert_to_float<scalar_t>(x);
auto x0_cube = x0 * x0 * x0;
auto x1_cube = x1 * x1 * x1;
auto inner_vec0 = kBetaVec * (x0 + kKappaVec * x0_cube);
auto inner_vec1 = kBetaVec * (x1 + kKappaVec * x1_cube);
auto res0 = kPointFiveVec * x0 * (kOneVec + inner_vec0.tanh());
auto res1 = kPointFiveVec * x1 * (kOneVec + inner_vec1.tanh());
return convert_from_float<scalar_t>(res0, res1);
},
grain_size);
});
} else {
AT_DISPATCH_FLOATING_TYPES(
it.dtype(), "GeluKernelImpl", [&]() {
using Vec = vec::Vectorized<scalar_t>;
const Vec kBetaVec(scalar_t(M_SQRT2 * M_2_SQRTPI * 0.5));
const Vec kKappaVec(scalar_t(0.044715));
const Vec kOneVec(scalar_t(1));
const Vec kPointFiveVec(scalar_t(0.5));
cpu_kernel_vec(
it,
[](scalar_t x) {
const scalar_t kBeta = M_SQRT2 * M_2_SQRTPI * 0.5;
const scalar_t kKappa = 0.044715;
auto x_cube = x * x * x;
auto inner = kBeta * (x + kKappa * x_cube);
return scalar_t(0.5) * x * (scalar_t(1) + std::tanh(inner));
},
[&](Vec x_vec) {
auto x_cube = x_vec * x_vec * x_vec;
auto inner_vec = kBetaVec * (x_vec + kKappaVec * x_cube);
return kPointFiveVec * x_vec * (kOneVec + inner_vec.tanh());
},
grain_size);
});
}
} else {
if (at::isReducedFloatingType(it.common_dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(it.dtype(), "GeluKernelImpl", [&]() {
auto kAlphaVec = Vectorized<float>((float)(M_SQRT1_2));
auto kOneVec = Vectorized<float>((float)(1));
auto kPointFiveVec = Vectorized<float>((float)(0.5));
cpu_kernel_vec(
it,
[](scalar_t x) -> scalar_t {
const float kAlpha = float(M_SQRT1_2);
return float(x) * float(0.5) * (float(1) + std::erf(float(x) * kAlpha));
},
[&](Vectorized<scalar_t> x) -> Vectorized<scalar_t> {
Vectorized<float> x0, x1;
std::tie(x0, x1) = convert_to_float<scalar_t>(x);
auto res0 = x0 * kPointFiveVec * (kOneVec + (x0 * kAlphaVec).erf());
auto res1 = x1 * kPointFiveVec * (kOneVec + (x1 * kAlphaVec).erf());
return convert_from_float<scalar_t>(res0, res1);
},
grain_size);
});
} else {
AT_DISPATCH_FLOATING_TYPES(
it.dtype(), "GeluKernelImpl", [&]() {
using Vec = vec::Vectorized<scalar_t>;
const Vec kAlphaVec(scalar_t(M_SQRT1_2));
const Vec kOneVec(scalar_t(1));
const Vec kPointFiveVec(scalar_t(0.5));
cpu_kernel_vec(
it,
[](scalar_t x) {
const scalar_t kAlpha = scalar_t(M_SQRT1_2);
return x * scalar_t(0.5) * (scalar_t(1) + std::erf(x * kAlpha));
},
[&](Vec x_vec) {
return x_vec * kPointFiveVec *
(kOneVec + (x_vec * kAlphaVec).erf());
},
grain_size);
});
}
}
}
void GeluBackwardKernelImpl(TensorIteratorBase& it, GeluType approximate) {
if (approximate == GeluType::Tanh) {
if (at::isReducedFloatingType(it.common_dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(it.common_dtype(), "GeluBackwardKernelImpl", [&]() {
auto kBetaVec = Vectorized<float>((float)(M_SQRT2 * M_2_SQRTPI * 0.5));
auto kKappaVec = Vectorized<float>((float)(0.044715));
auto kOneVec = Vectorized<float>((float)(1));
auto kThreeVec = Vectorized<float>((float)(3));
auto kPointFiveVec = Vectorized<float>((float)(0.5));
cpu_kernel_vec(
it,
[](scalar_t dy, scalar_t x) -> scalar_t {
const float kBeta = float(M_SQRT2 * M_2_SQRTPI * 0.5);
const float kKappa = float(0.044715);
float x_sq = float(x) * float(x);
float x_cube = x_sq * float(x);
float inner = kBeta * (float(x) + kKappa * x_cube);
float tanh_inner = float(std::tanh(inner));
float left = float(0.5) * float(x);
float right = float(1) + tanh_inner;
float left_derivative = float(0.5) * right;
float tanh_derivative = float(1) - tanh_inner * tanh_inner;
float inner_derivative =
kBeta * (float(1) + float(3) * kKappa * x_sq);
float right_derivative = left * tanh_derivative * inner_derivative;
return float(dy) * (left_derivative + right_derivative);
},
[&](Vectorized<scalar_t> dy_vec, Vectorized<scalar_t> x_vec) -> Vectorized<scalar_t> {
Vectorized<float> x0_vec, x1_vec;
std::tie(x0_vec, x1_vec) = convert_to_float<scalar_t>(x_vec);
Vectorized<float> dy0_vec, dy1_vec;
std::tie(dy0_vec, dy1_vec) = convert_to_float<scalar_t>(dy_vec);
auto x0_sq = x0_vec * x0_vec;
auto x1_sq = x1_vec * x1_vec;
auto x0_cube = x0_vec * x0_vec * x0_vec;
auto x1_cube = x1_vec * x1_vec * x1_vec;
auto inner_vec0 = kBetaVec * (x0_vec + kKappaVec * x0_cube);
auto inner_vec1 = kBetaVec * (x1_vec + kKappaVec * x1_cube);
auto tanh_inner_vec0 = inner_vec0.tanh();
auto tanh_inner_vec1 = inner_vec1.tanh();
auto left_vec0 = kPointFiveVec * x0_vec;
auto left_vec1 = kPointFiveVec * x1_vec;
auto right_vec0 = kOneVec + tanh_inner_vec0;
auto right_vec1 = kOneVec + tanh_inner_vec1;
auto left_derivative_vec0 = kPointFiveVec * right_vec0;
auto left_derivative_vec1 = kPointFiveVec * right_vec1;
auto tanh_derivative_vec0 = kOneVec - tanh_inner_vec0 * tanh_inner_vec0;
auto tanh_derivative_vec1 = kOneVec - tanh_inner_vec1 * tanh_inner_vec1;
auto inner_derivative_vec0 = kBetaVec * (kOneVec + kThreeVec * kKappaVec * x0_sq);
auto inner_derivative_vec1 = kBetaVec * (kOneVec + kThreeVec * kKappaVec * x1_sq);
auto right_derivative_vec0 = left_vec0 * tanh_derivative_vec0 * inner_derivative_vec0;
auto right_derivative_vec1 = left_vec1 * tanh_derivative_vec1 * inner_derivative_vec1;
auto res0 = dy0_vec * (left_derivative_vec0 + right_derivative_vec0);
auto res1 = dy1_vec * (left_derivative_vec1 + right_derivative_vec1);
return convert_from_float<scalar_t>(res0, res1);
});
});
} else {
AT_DISPATCH_FLOATING_TYPES(
it.dtype(), "GeluBackwardKernelImpl", [&]() {
using Vec = vec::Vectorized<scalar_t>;
const Vec kBetaVec(scalar_t(M_SQRT2 * M_2_SQRTPI * 0.5));
const Vec kKappaVec(scalar_t(0.044715));
const Vec kOneVec(scalar_t(1));
const Vec kThreeVec(scalar_t(3));
const Vec kPointFiveVec(scalar_t(0.5));
cpu_kernel_vec(
it,
[](scalar_t dy, scalar_t x) {
const scalar_t kBeta = M_SQRT2 * M_2_SQRTPI * 0.5;
const scalar_t kKappa = 0.044715;
auto x_sq = x * x;
auto x_cube = x_sq * x;
auto inner = kBeta * (x + kKappa * x_cube);
auto tanh_inner = std::tanh(inner);
auto left = scalar_t(0.5) * x;
auto right = scalar_t(1) + tanh_inner;
auto left_derivative = scalar_t(0.5) * right;
auto tanh_derivative = scalar_t(1) - tanh_inner * tanh_inner;
auto inner_derivative =
kBeta * (scalar_t(1) + scalar_t(3) * kKappa * x_sq);
auto right_derivative = left * tanh_derivative * inner_derivative;
return dy * (left_derivative + right_derivative);
},
[&](Vec dy_vec, Vec x_vec) {
auto x_sq = x_vec * x_vec;
auto x_cube = x_vec * x_vec * x_vec;
auto inner_vec =
kBetaVec * (x_vec + kKappaVec * x_cube);
auto tanh_inner_vec = inner_vec.tanh();
auto left_vec = kPointFiveVec * x_vec;
auto right_vec = kOneVec + tanh_inner_vec;
auto left_derivative_vec = kPointFiveVec * right_vec;
auto tanh_derivative_vec =
kOneVec - tanh_inner_vec * tanh_inner_vec;
auto inner_derivative_vec =
kBetaVec * (kOneVec + kThreeVec * kKappaVec * x_sq);
auto right_derivative_vec =
left_vec * tanh_derivative_vec * inner_derivative_vec;
return dy_vec * (left_derivative_vec + right_derivative_vec);
});
});
}
} else {
if (at::isReducedFloatingType(it.common_dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(it.common_dtype(), "GeluBackwardKernelImpl", [&]() {
auto kAlphaVec = Vectorized<float>((float)(M_SQRT1_2));
auto kBetaVec = Vectorized<float>((float)(M_2_SQRTPI * M_SQRT1_2 * 0.5));
auto kOneVec = Vectorized<float>((float)(1));
auto kPointFiveVec = Vectorized<float>((float)(0.5));
auto kMinusPointFiveVec = Vectorized<float>((float)(-0.5));
cpu_kernel_vec(
it,
[](scalar_t dy, scalar_t x) -> scalar_t {
const float kAlpha = float(M_SQRT1_2);
const float kBeta = float(M_2_SQRTPI) * float(M_SQRT1_2) * float(0.5);
const float cdf =
float(0.5) * (float(1) + std::erf(float(x) * kAlpha));
const float pdf = kBeta * std::exp(float(x) * float(x) * float(-0.5));
return float(dy) * (cdf + float(x) * pdf);
},
[&](Vectorized<scalar_t> dy, Vectorized<scalar_t> x) -> Vectorized<scalar_t> {
Vectorized<float> x0, x1;
std::tie(x0, x1) = convert_to_float<scalar_t>(x);
Vectorized<float> dy0, dy1;
std::tie(dy0, dy1) = convert_to_float<scalar_t>(dy);
auto cdf_vec0 = kPointFiveVec * (kOneVec + (x0 * kAlphaVec).erf());
auto cdf_vec1 = kPointFiveVec * (kOneVec + (x1 * kAlphaVec).erf());
auto pdf_vec0 = kBetaVec * (x0 * x0 * kMinusPointFiveVec).exp();
auto pdf_vec1 = kBetaVec * (x1 * x1 * kMinusPointFiveVec).exp();
auto res0 = dy0 * (cdf_vec0 + x0 * pdf_vec0);
auto res1 = dy1 * (cdf_vec1 + x1 * pdf_vec1);
return convert_from_float<scalar_t>(res0, res1);
});
});
} else {
AT_DISPATCH_FLOATING_TYPES(
it.dtype(), "GeluBackwardKernelImpl", [&]() {
using Vec = vec::Vectorized<scalar_t>;
const Vec kAlphaVec(scalar_t(M_SQRT1_2));
const Vec kBetaVec(scalar_t(M_2_SQRTPI * M_SQRT1_2 * 0.5));
const Vec kOneVec(scalar_t(1));
const Vec kPointFiveVec(scalar_t(0.5));
const Vec kMinusPointFiveVec(scalar_t(-0.5));
cpu_kernel_vec(
it,
[](scalar_t dy, scalar_t x) {
const scalar_t kAlpha = scalar_t(M_SQRT1_2);
const scalar_t kBeta = M_2_SQRTPI * M_SQRT1_2 * scalar_t(0.5);
const scalar_t cdf =
scalar_t(0.5) * (scalar_t(1) + std::erf(x * kAlpha));
const scalar_t pdf = kBeta * std::exp(x * x * scalar_t(-0.5));
return dy * (cdf + x * pdf);
},
[&](Vec dy_vec, Vec x_vec) {
const Vec cdf_vec =
kPointFiveVec * (kOneVec + (x_vec * kAlphaVec).erf());
const Vec pdf_vec =
kBetaVec * (x_vec * x_vec * kMinusPointFiveVec).exp();
return dy_vec * (cdf_vec + x_vec * pdf_vec);
});
});
}
}
}
void hardsigmoid_kernel(TensorIteratorBase& iter) {
if (at::isReducedFloatingType(iter.dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(iter.dtype(), "hardsigmoid_cpu", [&]() {
const float zero(0.0f);
const float three(3.0f);
const float six(6.0f);
using Vec = vec::Vectorized<float>;
const Vec kZeroVec(zero);
const Vec kThreeVec(three);
const Vec kSixVec(six);
cpu_kernel_vec(
iter,
[&](scalar_t self_val) -> scalar_t {
return std::min(std::max(float(self_val) + three, zero), six) / six;
},
[&](vec::Vectorized<scalar_t> self_val) -> vec::Vectorized<scalar_t> {
Vectorized<float> self_val0, self_val1;
std::tie(self_val0, self_val1) = convert_to_float<scalar_t>(self_val);
self_val0 = minimum(
maximum(self_val0 + kThreeVec, kZeroVec),
kSixVec
) / kSixVec;
self_val1 = minimum(
maximum(self_val1 + kThreeVec, kZeroVec),
kSixVec
) / kSixVec;
return convert_from_float<scalar_t>(self_val0, self_val1);
});
});
} else {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "hardsigmoid_cpu", [&] {
const scalar_t zero(0.0f);
const scalar_t three(3.0f);
const scalar_t six(6.0f);
using Vec = vec::Vectorized<scalar_t>;
const Vec kZeroVec(zero);
const Vec kThreeVec(three);
const Vec kSixVec(six);
cpu_kernel_vec(
iter,
[&](scalar_t self_val) {
return std::min(std::max(self_val + three, zero), six) / six;
},
[&](Vec self_val) {
return vec::minimum(
vec::maximum(self_val + kThreeVec, kZeroVec),
kSixVec
) / kSixVec;
});
});
}
}
void hardsigmoid_backward_kernel(TensorIteratorBase& iter) {
if (at::isReducedFloatingType(iter.dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(iter.common_dtype(), "hardsigmoid_backward", [&]() {
const float zero(0.0f);
const float three(3.0f);
const float neg_three(-3.0f);
const float one_sixth(1.0f / 6.0f);
using Vec = Vectorized<float>;
Vec kZeroVec(0.0f);
Vec kOneSixthVec(1.0f / 6.0f);
cpu_kernel_vec(
iter,
[=](scalar_t grad_val, scalar_t self_val) -> scalar_t {
return (float(self_val) > neg_three && float(self_val) < three)
? float(grad_val) * one_sixth
: zero;
},
[=](Vectorized<scalar_t> grad_val, Vectorized<scalar_t> self_val) -> Vectorized<scalar_t> {
Vec self_val0, self_val1, grad_val0, grad_val1;
std::tie(self_val0, self_val1) = convert_to_float<scalar_t>(self_val);
std::tie(grad_val0, grad_val1) = convert_to_float<scalar_t>(grad_val);
Vec gradNonZeroMask = (self_val0 > neg_three) & (self_val0 < three);
self_val0 = Vec::blendv(kZeroVec, grad_val0 * kOneSixthVec, gradNonZeroMask);
gradNonZeroMask = (self_val1 > neg_three) & (self_val1 < three);
self_val1 = Vec::blendv(kZeroVec, grad_val1 * kOneSixthVec, gradNonZeroMask);
return convert_from_float<scalar_t>(self_val0, self_val1);
});
});
} else {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "hardsigmoid_backward", [&] {
const scalar_t zero(0.0f);
const scalar_t three(3.0f);
const scalar_t neg_three(-3.0f);
const scalar_t one_sixth(1.0f / 6.0f);
using Vec = Vectorized<scalar_t>;
Vec kZeroVec(0.0f);
Vec kOneSixthVec(1.0f / 6.0f);
cpu_kernel_vec(
iter,
[=](scalar_t grad_val, scalar_t self_val) {
return (self_val > neg_three && self_val < three)
? grad_val * one_sixth
: zero;
},
[=](Vec grad_val, Vec self_val) {
Vec gradNonZeroMask = (self_val > neg_three) & (self_val < three);
return Vec::blendv(kZeroVec, grad_val * kOneSixthVec, gradNonZeroMask);
});
});
}
}
void hardshrink_kernel(TensorIteratorBase& iter, const Scalar& lambd) {
AT_DISPATCH_FLOATING_TYPES_AND2(kBFloat16, kHalf, iter.dtype(), "hardshrink_cpu", [&] {
auto lambd_val = lambd.to<scalar_t>();
using Vec = Vectorized<scalar_t>;
cpu_kernel_vec(
iter,
[=](scalar_t self_val) {
return (self_val >= -lambd_val && self_val <= lambd_val) ? scalar_t(0)
: self_val;
},
[=](Vec self_val) {
return Vec::blendv(self_val, Vec(0), (self_val >= -lambd_val) & (self_val <= lambd_val));
});
});
}
void softshrink_kernel(TensorIteratorBase& iter, const Scalar& lambd) {
if (at::isReducedFloatingType(iter.dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(iter.common_dtype(), "softshrink_cpu", [&]() {
auto lambd_val = lambd.to<float>();
auto lambdVec = Vectorized<float>(lambd_val);
cpu_kernel_vec(
iter,
[=](scalar_t a) -> scalar_t {
return float(a) > lambd_val ? a - lambd_val : (float(a) < -lambd_val ? a + lambd_val : float(0));
},
[=](Vectorized<scalar_t> self_val) -> Vectorized<scalar_t> {
Vectorized<float> self_val0, self_val1;
Vectorized<scalar_t> self_val_t0, self_val_t1;
std::tie(self_val0, self_val1) = convert_to_float<scalar_t>(self_val);
self_val_t0 = convert_from_float<scalar_t>((self_val0 > lambdVec) & (self_val0 - lambdVec), (self_val1 > lambdVec) & (self_val1 - lambdVec));
self_val_t1 = convert_from_float<scalar_t>((self_val0 < -lambd_val) & (self_val0 + lambdVec), (self_val1 < -lambd_val) & (self_val1 + lambdVec));
return (self_val_t0 | self_val_t1);
});
});
} else {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "softshrink_cpu", [&]() {
auto lambd_val = lambd.to<scalar_t>();
auto lambdVec = Vectorized<scalar_t>(lambd_val);
cpu_kernel_vec(
iter,
[=](scalar_t a) -> scalar_t {
return a > lambd_val ? a - lambd_val : (a < -lambd_val ? a + lambd_val : scalar_t(0));
},
[=](Vectorized<scalar_t> self_val) -> Vectorized<scalar_t> {
Vectorized<scalar_t> self_val_t0, self_val_t1;
self_val_t0 = (self_val > lambdVec) & (self_val - lambdVec);
self_val_t1 = (self_val < -lambd_val) & (self_val + lambdVec);
return (self_val_t0 | self_val_t1);
});
});
}
}
void shrink_backward_kernel(TensorIteratorBase& iter, const Scalar& lambd) {
AT_DISPATCH_FLOATING_TYPES_AND2(kBFloat16, kHalf, iter.dtype(), "shrink_backward_cpu", [&] {
auto lambd_val = lambd.to<scalar_t>();
cpu_kernel_vec(
iter,
[=](scalar_t grad_val, scalar_t self_val) {
return (self_val >= -lambd_val && self_val <= lambd_val) ? scalar_t(0)
: grad_val;
},
[=](Vectorized<scalar_t> grad_val, Vectorized<scalar_t> self_val) {
return ((self_val < -lambd_val) | (self_val > lambd_val)) & grad_val;
});
});
}
void hardtanh_backward_kernel(TensorIterator& iter, const Scalar& min, const Scalar& max) {
if (at::isReducedFloatingType(iter.dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(iter.dtype(), "hardshrink_backward_cpu", [&]() {
auto min_val = min.to<float>();
auto max_val = max.to<float>();
cpu_kernel_vec(
iter,
[=](scalar_t grad_val, scalar_t self_val) -> scalar_t {
return (float(self_val) <= min_val || float(self_val) >= max_val) ? scalar_t(0) : grad_val;
},
[=](Vectorized<scalar_t> grad_val, Vectorized<scalar_t> self_val) -> Vectorized<scalar_t> {
Vectorized<float> grad_val0, grad_val1, self_val0, self_val1;
std::tie(grad_val0, grad_val1) = convert_to_float<scalar_t>(grad_val);
std::tie(self_val0, self_val1) = convert_to_float<scalar_t>(self_val);
return convert_from_float<scalar_t>(
((self_val0 > min_val) & (self_val0 < max_val)) & grad_val0,
((self_val1 > min_val) & (self_val1 < max_val)) & grad_val1
);
});
});
} else {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "hardshrink_backward_cpu", [&] {
auto min_val = min.to<scalar_t>();
auto max_val = max.to<scalar_t>();
cpu_kernel_vec(
iter,
[=](scalar_t grad_val, scalar_t self_val) {
return (self_val <= min_val || self_val >= max_val) ? scalar_t(0) : grad_val;
},
[=](Vectorized<scalar_t> grad_val, Vectorized<scalar_t> self_val) {
return ((self_val > min_val) & (self_val < max_val)) & grad_val;
});
});
}
}
void hardswish_kernel(TensorIterator& iter) {
if (at::isReducedFloatingType(iter.dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(iter.dtype(), "hardswish_cpu", [&]() {
const float zero(0.0f);
const float three(3.0f);
const float six(6.0f);
using Vec = vec::Vectorized<float>;
const Vec kZeroVec(zero);
const Vec kThreeVec(three);
const Vec kSixVec(six);
cpu_kernel_vec(
iter,
[&](scalar_t x) -> scalar_t {
return float(x) * std::min(std::max(float(x) + three, zero), six) / six;
},
[&](vec::Vectorized<scalar_t> x_vec) {
Vectorized<float> x_vec0, x_vec1;
std::tie(x_vec0, x_vec1) = convert_to_float<scalar_t>(x_vec);
x_vec0 = x_vec0 * minimum(
maximum(x_vec0 + kThreeVec, kZeroVec),
kSixVec
) / kSixVec;
x_vec1 = x_vec1 * minimum(
maximum(x_vec1 + kThreeVec, kZeroVec),
kSixVec
) / kSixVec;
return convert_from_float<scalar_t>(x_vec0, x_vec1);
});
});
} else {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "hardswish_cpu", [&]() {
const scalar_t zero(0.0f);
const scalar_t three(3.0f);
const scalar_t six(6.0f);
using Vec = vec::Vectorized<scalar_t>;
const Vec kZeroVec(zero);
const Vec kThreeVec(three);
const Vec kSixVec(six);
cpu_kernel_vec(
iter,
[&](scalar_t x) {
return x * std::min(std::max(x + three, zero), six) / six;
},
[&](Vec x_vec) {
return x_vec * vec::minimum(
vec::maximum(x_vec + kThreeVec, kZeroVec),
kSixVec
) / kSixVec;
}
);
});
}
}
void hardswish_backward_kernel(TensorIterator& iter) {
if (at::isReducedFloatingType(iter.dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(iter.dtype(), "hardswish_backward_cpu", [&]() {
const float zero(0.0f);
const float three(3.0f);
const float neg_three(-3.0f);
const float one_half(0.5f);
using Vec = vec::Vectorized<float>;
const Vec kZeroVec(zero);
const Vec kThreeVec(three);
const Vec kNegThreeVec(neg_three);
const Vec kOneHalfVec(one_half);
cpu_kernel_vec(
iter,
[&](scalar_t grad_val, scalar_t self_val) -> scalar_t {
if (float(self_val) < neg_three) {
return zero;
} else if (float(self_val) <= three) {
return float(grad_val) * ((float(self_val) / three) + one_half);
} else {
return grad_val;
}
},
[&](vec::Vectorized<scalar_t> grad_val, vec::Vectorized<scalar_t> self_val) {
Vectorized<float> self_val0, self_val1, grad_val0, grad_val1;
std::tie(self_val0, self_val1) = convert_to_float<scalar_t>(self_val);
std::tie(grad_val0, grad_val1) = convert_to_float<scalar_t>(grad_val);
self_val0 = Vec::blendv(
Vec::blendv(
grad_val0 * ((self_val0 / kThreeVec) + kOneHalfVec),
grad_val0,
self_val0 >= kThreeVec
),
kZeroVec,
self_val0 < kNegThreeVec
);
self_val1 = Vec::blendv(
Vec::blendv(
grad_val1 * ((self_val1 / kThreeVec) + kOneHalfVec),
grad_val1,
self_val1 >= kThreeVec
),
kZeroVec,
self_val1 < kNegThreeVec
);
return convert_from_float<scalar_t>(self_val0, self_val1);
});
});
} else {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "hardswish_backward_cpu", [&]() {
const scalar_t zero(0.0f);
const scalar_t three(3.0f);
const scalar_t neg_three(-3.0f);
const scalar_t one_half(0.5f);
using Vec = vec::Vectorized<scalar_t>;
const Vec kZeroVec(zero);
const Vec kThreeVec(three);
const Vec kNegThreeVec(neg_three);
const Vec kOneHalfVec(one_half);
cpu_kernel_vec(
iter,
[&](scalar_t grad_val, scalar_t self_val) {
if (self_val < neg_three) {
return zero;
} else if (self_val <= three) {
return grad_val * ((self_val / three) + one_half);
} else {
return grad_val;
}
},
[&](Vec grad_val, Vec self_val) {
return Vec::blendv(
Vec::blendv(
grad_val * ((self_val / kThreeVec) + kOneHalfVec),
grad_val,
self_val >= kThreeVec
),
kZeroVec,
self_val < kNegThreeVec
);
}
);
});
}
}
static void leaky_relu_kernel(TensorIteratorBase& iter, const Scalar& negval_) {
if (at::isReducedFloatingType(iter.dtype())) {
AT_DISPATCH_REDUCED_FLOATING_TYPES(iter.dtype(), "leaky_relu_cpu", [&]() {
auto zero_vec = Vectorized<float>((float)(0));
auto one_vec = Vectorized<float>((float)(1));
float negval = negval_.to<float>();
Vectorized<float> negval_v = Vectorized<float>(negval);
cpu_kernel_vec(
iter,
[&](scalar_t a) -> scalar_t {
return float(a) > float(0) ? float(a) : float(a) * negval;
},
[&](Vectorized<scalar_t> a) -> Vectorized<scalar_t> {
Vectorized<float> a0, a1;
std::tie(a0, a1) = convert_to_float<scalar_t>(a);
auto res0 = a0 * (Vectorized<float>::blendv(negval_v, one_vec, a0 > zero_vec));
auto res1 = a1 * (Vectorized<float>::blendv(negval_v, one_vec, a1 > zero_vec));
return convert_from_float<scalar_t>(res0, res1);
});
});