forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Activation.cpp
1296 lines (1249 loc) · 51.3 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/core/TensorBase.h>
#include <ATen/cpu/vec/vec.h>
#include <ATen/native/TensorIterator.h>
#include <ATen/native/cpu/Loops.h>
#include <ATen/Parallel.h>
#include <c10/core/Scalar.h>
namespace at {
namespace native {
namespace {
template <typename scalar_t>
inline void _vec_log_sigmoid(TensorBase &output, TensorBase &buffer, const TensorBase &input) {
if (input.scalar_type() == kBFloat16) {
using Vec = Vectorized<BFloat16>;
BFloat16* output_data = output.data_ptr<BFloat16>();
BFloat16* buffer_data = buffer.data_ptr<BFloat16>();
BFloat16* input_data = input.data_ptr<BFloat16>();
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_bfloat16_float(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_float_bfloat16(buffer_vec0, buffer_vec1).store(buffer_data + begin + d);
convert_float_bfloat16(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_bfloat16_float(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_float_bfloat16(buffer_vec0, buffer_vec1).store(buffer_data + begin + d, size - d);
convert_float_bfloat16(output_vec0, output_vec1).store(output_data + begin + d, size - d);
}
});
} else {
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_cpu_kernel(TensorBase &output, TensorBase &buffer, const TensorBase &input) {
AT_DISPATCH_FLOATING_TYPES_AND(kBFloat16, input.scalar_type(), "log_sigmoid_cpu", [&] {
_vec_log_sigmoid<scalar_t>(output, buffer, input);
});
}
static void log_sigmoid_backward_cpu_kernel(TensorIterator& iter) {
if (iter.dtype() == kBFloat16) {
using Vec = Vectorized<BFloat16>;
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,
[=](BFloat16 a, BFloat16 b, BFloat16 c) -> BFloat16 {
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_bfloat16_float(a);
std::tie(b0, b1) = convert_bfloat16_float(b);
std::tie(c0, c1) = convert_bfloat16_float(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_float_bfloat16(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) {
AT_DISPATCH_ALL_TYPES_AND(kBFloat16, 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 (it.common_dtype() == kBFloat16) {
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](BFloat16 a) -> BFloat16 {
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<BFloat16> a) -> Vectorized<BFloat16> {
Vectorized<float> a0, a1;
std::tie(a0, a1) = convert_bfloat16_float(a);
auto cmp0 = (a0 > zero_vec);
auto cmp1 = (a1 > zero_vec);
if (!cmp0.zero_mask() && !cmp1.zero_mask()) { // only a * poscoef (which is very quick) needs to be computed
return convert_float_bfloat16(a0 * poscoef_vec, a1 * poscoef_vec);
} else {
auto res0 = Vectorized<float>::blendv(((a0 * negiptcoef_vec).exp() - one_vec) * negcoef_vec, a0 * poscoef_vec, cmp0);
auto res1 = Vectorized<float>::blendv(((a1 * negiptcoef_vec).exp() - one_vec) * negcoef_vec, a1 * poscoef_vec, cmp1);
return convert_float_bfloat16(res0, res1);
}
}
);
} else {
AT_DISPATCH_FLOATING_TYPES(it.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 (it.common_dtype() == kBFloat16) {
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](BFloat16 a, BFloat16 b) -> BFloat16 {
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<BFloat16> a, Vectorized<BFloat16> b) -> Vectorized<BFloat16> {
Vectorized<float> a0, a1;
std::tie(a0, a1) = convert_bfloat16_float(a);
Vectorized<float> b0, b1;
std::tie(b0, b1) = convert_bfloat16_float(b);
auto cmp0 = (b0 > zero_vec);
auto cmp1 = (b1 > zero_vec);
if (is_result) {
if (!cmp0.zero_mask() && !cmp1.zero_mask()) { // only a * poscoef (which is very quick) needs to be computed
return convert_float_bfloat16(a0 * poscoef_vec, a1 * poscoef_vec);
} else {
auto res0 = Vectorized<float>::blendv(a0 * negiptcoef_vec * (b0 + negcoef_vec), a0 * poscoef_vec, cmp0);
auto res1 = Vectorized<float>::blendv(a1 * negiptcoef_vec * (b1 + negcoef_vec), a1 * poscoef_vec, cmp1);
return convert_float_bfloat16(res0, res1);
}
} else {
auto res0 = Vectorized<float>::blendv(a0 * negiptcoef_vec * negcoef_vec * (b0 * negiptcoef_vec).exp(), a0 * poscoef_vec, cmp0);
auto res1 = Vectorized<float>::blendv(a1 * negiptcoef_vec * negcoef_vec * (b1 * negiptcoef_vec).exp(), a1 * poscoef_vec, cmp1);
return convert_float_bfloat16(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) {
AT_DISPATCH_FLOATING_TYPES_AND(
ScalarType::BFloat16, 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 {
AT_DISPATCH_FLOATING_TYPES_AND(
ScalarType::BFloat16, 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) {
AT_DISPATCH_FLOATING_TYPES_AND(
ScalarType::BFloat16, 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 {
AT_DISPATCH_FLOATING_TYPES_AND(
ScalarType::BFloat16, 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 (iter.dtype() == kBFloat16) {
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,
[&](BFloat16 self_val) -> BFloat16 {
return std::min(std::max(float(self_val) + three, zero), six) / six;
},
[&](vec::Vectorized<BFloat16> self_val) -> vec::Vectorized<BFloat16> {
Vectorized<float> self_val0, self_val1;
std::tie(self_val0, self_val1) = convert_bfloat16_float(self_val);
self_val0 = minimum(
maximum(self_val0 + kThreeVec, kZeroVec),
kSixVec
) / kSixVec;
self_val1 = minimum(
maximum(self_val1 + kThreeVec, kZeroVec),
kSixVec
) / kSixVec;
return convert_float_bfloat16(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 (iter.dtype() == kBFloat16) {
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,
[=](BFloat16 grad_val, BFloat16 self_val) -> BFloat16 {
return (float(self_val) > neg_three && float(self_val) < three)
? float(grad_val) * one_sixth
: zero;
},
[=](Vectorized<BFloat16> grad_val, Vectorized<BFloat16> self_val) -> Vectorized<BFloat16> {
Vec self_val0, self_val1, grad_val0, grad_val1;
std::tie(self_val0, self_val1) = convert_bfloat16_float(self_val);
std::tie(grad_val0, grad_val1) = convert_bfloat16_float(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_float_bfloat16(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_AND(kBFloat16, 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 (iter.dtype() == kBFloat16) {
auto lambd_val = lambd.to<float>();
auto lambdVec = Vectorized<float>(lambd_val);
cpu_kernel_vec(
iter,
[=](BFloat16 a) -> BFloat16 {
return float(a) > lambd_val ? a - lambd_val : (float(a) < -lambd_val ? a + lambd_val : float(0));
},
[=](Vectorized<BFloat16> self_val) {
Vectorized<float> self_val0, self_val1;
Vectorized<BFloat16> self_val_t0, self_val_t1;
std::tie(self_val0, self_val1) = convert_bfloat16_float(self_val);
self_val_t0 = convert_float_bfloat16((self_val0 > lambdVec) & (self_val0 - lambdVec), (self_val1 > lambdVec) & (self_val1 - lambdVec));
self_val_t1 = convert_float_bfloat16((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> 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_AND(kBFloat16, 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 (iter.dtype() == kBFloat16) {
auto min_val = min.to<float>();
auto max_val = max.to<float>();
cpu_kernel_vec(
iter,
[=](BFloat16 grad_val, BFloat16 self_val) -> BFloat16 {
return (float(self_val) <= min_val || float(self_val) >= max_val) ? BFloat16(0) : grad_val;
},
[=](Vectorized<BFloat16> grad_val, Vectorized<BFloat16> self_val) -> Vectorized<BFloat16> {
Vectorized<float> grad_val0, grad_val1, self_val0, self_val1;
std::tie(grad_val0, grad_val1) = convert_bfloat16_float(grad_val);
std::tie(self_val0, self_val1) = convert_bfloat16_float(self_val);
return convert_float_bfloat16(
((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 (iter.dtype() == kBFloat16) {
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,
[&](BFloat16 x) -> BFloat16 {
return float(x) * std::min(std::max(float(x) + three, zero), six) / six;
},
[&](vec::Vectorized<BFloat16> x_vec) {
Vectorized<float> x_vec0, x_vec1;
std::tie(x_vec0, x_vec1) = convert_bfloat16_float(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_float_bfloat16(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 (iter.dtype() == kBFloat16) {
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,
[&](BFloat16 grad_val, BFloat16 self_val) -> BFloat16 {
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<BFloat16> grad_val, vec::Vectorized<BFloat16> self_val) {
Vectorized<float> self_val0, self_val1, grad_val0, grad_val1;
std::tie(self_val0, self_val1) = convert_bfloat16_float(self_val);
std::tie(grad_val0, grad_val1) = convert_bfloat16_float(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_float_bfloat16(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 (iter.common_dtype() == kBFloat16) {
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,
[&](BFloat16 a) -> BFloat16 {
return float(a) > float(0) ? float(a) : float(a) * negval;
},
[&](Vectorized<BFloat16> a) -> Vectorized<BFloat16> {
Vectorized<float> a0, a1;
std::tie(a0, a1) = convert_bfloat16_float(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_float_bfloat16(res0, res1);
});
} else {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "leaky_relu_cpu", [&] {
using Vec = Vectorized<scalar_t>;
auto zero_vec = Vec((scalar_t)(0));
auto one_vec = Vec((scalar_t)(1));
scalar_t negval = negval_.to<scalar_t>();
Vec negval_v = Vec(negval);
cpu_kernel_vec(
iter,
[&](scalar_t a) -> scalar_t {
return a > scalar_t(0) ? a : a * negval;
},
[&](Vec a) -> Vec {
auto r = Vec::blendv(negval_v, one_vec, a > zero_vec);
return a * r;
});
});
}
}
static void leaky_relu_backward_kernel(TensorIteratorBase& iter, const Scalar& negval_) {
if (iter.common_dtype() == kBFloat16) {
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,
[&](BFloat16 a, BFloat16 b) -> BFloat16 {
return float(a) > float(0) ? float(b) : float(b) * negval;
},
[&](Vectorized<BFloat16> a, Vectorized<BFloat16> b) -> Vectorized<BFloat16> {
Vectorized<float> a0, a1, b0, b1;
std::tie(a0, a1) = convert_bfloat16_float(a);
std::tie(b0, b1) = convert_bfloat16_float(b);
auto res0 = b0 * (Vectorized<float>::blendv(negval_v, one_vec, a0 > zero_vec));
auto res1 = b1 * (Vectorized<float>::blendv(negval_v, one_vec, a1 > zero_vec));
return convert_float_bfloat16(res0, res1);
});
} else {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "leaky_relu_backward_cpu", [&] {
using Vec = Vectorized<scalar_t>;
auto zero_vec = Vec((scalar_t)(0));
auto one_vec = Vec((scalar_t)(1));
scalar_t negval = negval_.to<scalar_t>();
Vec negval_v = Vec(negval);
cpu_kernel_vec(
iter,
[&](scalar_t a, scalar_t b) -> scalar_t {
return a > scalar_t(0) ? b : b * negval;
},
[&](Vec a, Vec b) -> Vec {
auto r = Vec::blendv(negval_v, one_vec, a > zero_vec);
return b * r;
});
});
}
}
void softplus_kernel(TensorIteratorBase& iter, const Scalar& beta_, const Scalar& threshold_) {
if (iter.dtype() == kBFloat16) {
using Vec = Vectorized<float>;
auto beta = beta_.to<float>();
auto threshold = threshold_.to<float>();
const Vec beta_vec(beta);
const Vec threshold_vec(threshold);
cpu_kernel_vec(
iter,
[beta, threshold](BFloat16 a) -> BFloat16 {
return (float(a) * beta) > threshold ? a
: static_cast<BFloat16>((std::log1p(std::exp(float(a) * beta))) / beta);
},
[beta_vec, threshold_vec](Vectorized<BFloat16> a) -> Vectorized<BFloat16> {
Vectorized<float> a0, a1;
std::tie(a0, a1) = convert_bfloat16_float(a);
a0 = Vec::blendv((a0 * beta_vec).exp().log1p() / beta_vec, a0, (a0 * beta_vec) > threshold_vec);
a1 = Vec::blendv((a1 * beta_vec).exp().log1p() / beta_vec, a1, (a1 * beta_vec) > threshold_vec);
return convert_float_bfloat16(a0, a1);
}
);
} else {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "softplus_cpu", [&]() {
using Vec = Vectorized<scalar_t>;
auto beta = beta_.to<scalar_t>();
auto threshold = threshold_.to<scalar_t>();
const Vec beta_vec(beta);
const Vec threshold_vec(threshold);
cpu_kernel_vec(
iter,
[beta, threshold](scalar_t a) -> scalar_t {
return (a * beta) > threshold ? a
: static_cast<scalar_t>(std::log1p(std::exp(a * beta))) / beta;
},
[beta_vec, threshold_vec](Vec a) -> Vec {
return Vec::blendv((a * beta_vec).exp().log1p() / beta_vec, a, (a * beta_vec) > threshold_vec);
}
);
});
}
}
void softplus_backward_kernel(TensorIteratorBase& iter, const Scalar& beta_, const Scalar& threshold_) {
if (iter.dtype() == kBFloat16) {
using Vec = Vectorized<float>;
auto beta = beta_.to<float>();
auto threshold = threshold_.to<float>();
const Vec beta_vec(beta);
const Vec threshold_vec(threshold);
const Vec one_vec(static_cast<float>(1.0));
cpu_kernel_vec(
iter,
[beta, threshold](BFloat16 a, BFloat16 b) -> BFloat16 {
float z = std::exp(float(b) * beta);
return (float(b) * beta) > threshold ? a : static_cast<BFloat16>(float(a) * z / (z + float(1.)));
},
[beta_vec, one_vec, threshold_vec](Vectorized<BFloat16> a, Vectorized<BFloat16> b) -> Vectorized<BFloat16> {
Vectorized<float> a0, a1, b0, b1;
std::tie(a0, a1) = convert_bfloat16_float(a);
std::tie(b0, b1) = convert_bfloat16_float(b);
Vec z = (b0 * beta_vec).exp();
a0 = Vec::blendv(a0 * z / (z + one_vec), a0, (b0 * beta_vec) > threshold_vec);
z = (b1 * beta_vec).exp();
a1 = Vec::blendv(a1 * z / (z + one_vec), a1, (b1 * beta_vec) > threshold_vec);
return convert_float_bfloat16(a0, a1);
}
);
} else {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "softplus_backward_cpu", [&]() {
using Vec = Vectorized<scalar_t>;
auto beta = beta_.to<scalar_t>();
auto threshold = threshold_.to<scalar_t>();
const Vec beta_vec(beta);
const Vec threshold_vec(threshold);
const Vec one_vec(static_cast<scalar_t>(1.0));
cpu_kernel_vec(
iter,
[beta, threshold](scalar_t a, scalar_t b) -> scalar_t {
scalar_t z = std::exp(b * beta);
return (b * beta) > threshold ? a : a * z / (z + scalar_t(1.));
},
[beta_vec, one_vec, threshold_vec](Vec a, Vec b) -> Vec {
const Vec z = (b * beta_vec).exp();
return Vec::blendv(a * z / (z + one_vec), a, (b * beta_vec) > threshold_vec);
}
);
});
}
}
void glu_kernel(TensorIteratorBase& iter) {
if (iter.dtype() == kBFloat16) {
const float float_one_val(1);
const Vectorized<float> float_one_vec(float_one_val);
cpu_kernel_vec(
iter,
[float_one_val](BFloat16 a, BFloat16 b) -> BFloat16 {
return float(a) * (float_one_val / (float_one_val + std::exp(- float(b))));
},
[float_one_vec](Vectorized<BFloat16> a, Vectorized<BFloat16> b) -> Vectorized<BFloat16> {
Vectorized<float> a0, a1, b0, b1;
std::tie(a0, a1) = convert_bfloat16_float(a);
std::tie(b0, b1) = convert_bfloat16_float(b);
a0 = a0 * (float_one_vec / (float_one_vec + b0.neg().exp()));
a1 = a1 * (float_one_vec / (float_one_vec + b1.neg().exp()));
return convert_float_bfloat16(a0, a1);
}
);
} else {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "glu_cpu", [&] {
using Vec = Vectorized<scalar_t>;
const scalar_t one_val(1);
const Vec one_vec(one_val);
cpu_kernel_vec(
iter,
[one_val](scalar_t a, scalar_t b) -> scalar_t {
return a * (one_val / (one_val + std::exp(-b)));
},
[one_vec](Vec a, Vec b) -> Vec {
return a * (one_vec / (one_vec + b.neg().exp()));
}
);
});
}
}
void glu_jvp_kernel(TensorIteratorBase& iter) {