-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathReduce.h
1551 lines (1398 loc) · 50.2 KB
/
Reduce.h
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
#pragma once
#include <ATen/ATen.h>
#include <ATen/OpMathType.h>
#include <ATen/core/Array.h>
#include <ATen/detail/FunctionTraits.h>
#include <ATen/native/TensorIterator.h>
#include <ATen/native/xpu/sycl/Loops.h>
#include <ATen/native/xpu/sycl/MemoryAccess.h>
#include <ATen/native/xpu/sycl/MemoryAccessUtils.h>
#include <ATen/native/xpu/sycl/OffsetCalculator.h>
#include <c10/core/Allocator.h>
#include <c10/macros/Macros.h>
#include <comm/DeviceProperties.h>
#include <comm/SYCLContext.h>
#include <functional>
#include <iosfwd>
#include <type_traits>
#include <utility>
namespace at {
namespace native {
namespace xpu {
using namespace at::xpu;
using at::detail::Array;
namespace detail {
template <class arg_t, class item_t, class CombineFunc, int out_vec_sz = 1>
inline at::detail::Array<arg_t, out_vec_sz> group_reduce(
item_t item,
int wg_size,
sycl_local_ptr<void> shared,
at::detail::Array<arg_t, out_vec_sz> value,
CombineFunc combine) {
using vec_t = at::detail::Array<arg_t, out_vec_sz>;
sycl_local_ptr<vec_t> shared_(shared);
int l_x = item.get_local_linear_id();
// int dim_x = wg_size;
auto sg = item.get_sub_group();
int sg_size = sg.get_local_range()[0];
int sg_lid = sg.get_local_linear_id();
int sg_gid = sg.get_group_linear_id();
int sg_range = sg.get_group_range()[0];
// group reduce requests workgroup size is multiple of subgroup size
SYCL_KERNEL_ASSERT(
wg_size % sg_size == 0 && "unsupported workgroup size for group reduce");
for (int offset = 1; offset < sg_size; offset <<= 1) {
#pragma unroll(out_vec_sz)
for (int i = 0; i < out_vec_sz; ++i) {
arg_t other = sg.shuffle_down(value[i], offset);
value[i] = combine(value[i], other);
}
}
if (sg_lid == 0) {
shared_[sg_gid] = value;
}
item.barrier(sycl_local_fence);
if (sg_range <= sg_size) {
// sub-group reduce
if (l_x < sg_size) {
value = shared_[l_x];
}
if (sg_gid == 0 && sg_lid < sg_range) {
value = shared_[sg_lid];
for (int offset = 1; offset < sg_range; offset <<= 1) {
#pragma unroll(out_vec_sz)
for (int i = 0; i < out_vec_sz; ++i) {
arg_t other = sg.shuffle_down(value[i], offset);
value[i] = combine(value[i], other);
}
}
}
} else {
// work item tree reduce
if (l_x < sg_range) {
value = shared_[l_x];
}
for (int offset = sg_range / 2; offset > 0; offset >>= 1) {
if ((int)l_x < offset) {
vec_t other = shared_[l_x + offset];
#pragma unroll(out_vec_sz)
for (int i = 0; i < out_vec_sz; ++i) {
value[i] = combine(value[i], other[i]);
}
shared_[l_x] = value;
}
item.barrier(sycl_local_fence);
}
}
return value;
}
template <class arg_t, class item_t, class CombineFunc, int out_vec_sz = 1>
inline at::detail::Array<arg_t, out_vec_sz> group_x_reduce(
item_t item,
sycl_local_ptr<void> shared,
at::detail::Array<arg_t, out_vec_sz> value,
CombineFunc combine) {
using vec_t = at::detail::Array<arg_t, out_vec_sz>;
sycl_local_ptr<vec_t> shared_(shared);
int l_x = item.get_local_id(1), l_y = item.get_local_id(0);
int g_x = item.get_local_range(1);
int dim_x = g_x;
auto sg = item.get_sub_group();
int sg_size = sg.get_local_range()[0];
if (dim_x > sg_size) {
int base = l_x + l_y * g_x;
shared_[base] = value;
for (int offset = dim_x / 2; offset >= sg_size; offset >>= 1) {
item.barrier(sycl_local_fence);
if (l_x < offset && l_x + offset < g_x) {
vec_t other = shared_[base + offset];
#pragma unroll(out_vec_sz)
for (int i = 0; i < out_vec_sz; ++i) {
value[i] = combine(value[i], other[i]);
}
shared_[base] = value;
}
}
dim_x = sg_size;
}
// sub-group reduction
for (int offset = 1; offset < dim_x; offset <<= 1) {
#pragma unroll(out_vec_sz)
for (int i = 0; i < out_vec_sz; ++i) {
arg_t other = sg.shuffle_down(value[i], offset);
value[i] = combine(value[i], other);
}
}
return value;
}
template <class arg_t, class item_t, class CombineFunc, int out_vec_sz = 1>
inline at::detail::Array<arg_t, out_vec_sz> group_y_reduce(
item_t item,
sycl_local_ptr<void> shared,
at::detail::Array<arg_t, out_vec_sz> value,
CombineFunc combine) {
using vec_t = at::detail::Array<arg_t, out_vec_sz>;
sycl_local_ptr<vec_t> shared_(shared);
int l_x = item.get_local_id(1), l_y = item.get_local_id(0);
int g_x = item.get_local_range(1);
int dim_y = item.get_local_range(0);
auto slm_off = [l_x, l_y, g_x](int off) -> int {
return l_x + (l_y + off) * g_x;
};
shared_[slm_off(0)] = value;
for (int offset = dim_y / 2; offset > 0; offset >>= 1) {
item.barrier(sycl_local_fence);
if (l_y < offset && l_y + offset < dim_y) {
vec_t other = shared_[slm_off(offset)];
#pragma unroll(out_vec_sz)
for (int i = 0; i < out_vec_sz; ++i) {
value[i] = combine(value[i], other[i]);
}
shared_[slm_off(0)] = value;
}
}
return value;
}
} // namespace detail
using namespace detail;
using at::detail::Array;
inline int64_t div_up(int64_t a, int64_t b) {
return (a + b - 1) / b;
}
// returns floor(log2(n))
inline int last_pow2(int n) {
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
return std::max(1, n - (n >> 1));
}
// Warning: 64-bit integer loop inside device
static void reduce_fraction(size_t& numerator, size_t& denominator) {
// get GCD of num and denom using Euclid's algorithm.
// Can replace this with std::gcd if we ever support c++17.
size_t a = denominator;
size_t b = numerator;
while (b != 0) {
a %= b;
// swap(a, b)
size_t tmp = a;
a = b;
b = tmp;
}
// a is now the GCD
numerator /= a;
denominator /= a;
}
struct ReduceConfig {
static constexpr int GROUP_X = 0;
static constexpr int GROUP_Y = 1;
static constexpr int GROUP = 2;
ReduceConfig(int element_size_bytes, int num_outputs, int num_inputs)
: element_size_bytes(element_size_bytes),
num_inputs(num_inputs),
num_outputs(num_outputs) {}
int element_size_bytes;
int num_inputs;
int num_outputs;
int step_input = 1;
int step_output = 1;
int groups_per_output = 1;
int input_mult[3] = {0, 0, 0};
int output_mult[2] = {0, 0};
// Change terms in accordance with SYCL
// Common pattern: width is equal to sub-group size
int group_width;
int group_height;
int num_items; /* workgroup size */
bool vectorize_input = false;
int input_vec_size = 1;
int output_vec_size = 1;
template <typename T, class KernelClass>
void set_group_dimension(int64_t dim0, int64_t dim1) {
auto max_wg_sz = syclMaxWorkGroupSize<KernelClass>();
auto max_sg_sz = syclMaxSubGroupSize();
const int max_num_items = max_wg_sz / output_vec_size;
int dim0_pow2 = dim0 < max_num_items ? static_cast<int>(last_pow2(dim0))
: max_num_items;
int dim1_pow2 = dim1 < max_num_items ? static_cast<int>(last_pow2(dim1))
: max_num_items;
group_width = std::min(dim0_pow2, int(max_sg_sz));
group_height = std::min(dim1_pow2, int(max_num_items / group_width));
group_width = std::min(dim0_pow2, int(max_num_items / group_height));
num_items = group_width * group_height;
if (num_items < max_sg_sz)
group_width = max_sg_sz;
}
int split_input(int parallelism) {
int step = step_input;
step_input *= parallelism;
return step;
}
int split_output(int parallelism) {
int step = step_output;
step_output *= parallelism;
return step;
}
// Becareful of the geometry order
sycl::range<2> group_sz() const {
return {(size_t)group_height, (size_t)group_width};
}
sycl::range<2> global_sz() const {
return {
(size_t)(groups_per_output * group_height),
(size_t)(group_width * div_up(num_outputs / output_vec_size, step_output))};
}
sycl::range<2> n_groups() const {
return {
(size_t)(groups_per_output),
(size_t)(div_up(num_outputs / output_vec_size, step_output))};
}
bool should_group_x_reduce() const {
return input_mult[GROUP_X] != 0;
}
bool should_group_y_reduce() const {
return input_mult[GROUP_Y] != 0;
}
bool should_global_reduce() const {
return input_mult[GROUP] != 0;
}
bool should_store(sycl::nd_item<2> pos, int output_idx) const {
return output_idx < num_outputs &&
(!should_group_x_reduce() || pos.get_local_id(1) == 0) &&
(!should_group_y_reduce() || pos.get_local_id(0) == 0);
}
bool should_reduce_tail(sycl::nd_item<2> pos) const {
return (!should_group_y_reduce() || pos.get_local_id(0) == 0) &&
(!should_global_reduce() || pos.get_group(0) == 0);
}
int input_idx(sycl::nd_item<2> pos) const {
int lane = pos.get_local_id(1);
int thread = pos.get_local_id(0);
int group_y = pos.get_group(0);
return (
lane * input_mult[GROUP_X] + thread * input_mult[GROUP_Y] +
group_y * input_mult[GROUP]);
}
template <int output_vec_size>
int output_idx(sycl::nd_item<2> pos) const {
int lane = pos.get_local_id(1);
int thread = pos.get_local_id(0);
int group_x = pos.get_group(1);
return (lane * output_mult[GROUP_X] + thread * output_mult[GROUP_Y] +
group_x * step_output) *
output_vec_size;
}
int slm_offset(sycl::nd_item<2> pos, int offset) const {
return pos.get_local_id(1) +
(pos.get_local_id(0) + offset) * pos.get_local_range(1);
}
int staging_memory_offset(sycl::nd_item<2> pos, int wg_y) const {
int offset = wg_y + pos.get_group(1) * pos.get_group_range(0);
if (!should_group_x_reduce()) {
offset = pos.get_local_id(1) + offset * pos.get_local_range(1);
}
return offset;
}
int slm_sz() const {
// if (!should_group_y_reduce() && (!should_group_x_reduce() || group_width
// <= 32)) { return 0; }
return element_size_bytes * num_items * output_vec_size;
}
int64_t global_memory_size() const {
if (!should_global_reduce()) {
return 0;
}
auto size = (int64_t)element_size_bytes * num_outputs * groups_per_output;
if (!should_group_x_reduce()) {
size *= group_sz()[1] * output_vec_size;
}
return size;
}
int semaphore_size() const {
if (!should_global_reduce()) {
return 0;
}
return sizeof(int) * n_groups()[1];
}
int values_per_item() const {
return div_up(num_inputs, step_input);
}
};
std::ostream& operator<<(std::ostream& out, const ReduceConfig& config);
template <int output_vec_size, typename R>
class ReduceKernel : public __SYCL_KER_CONFIG_CONVENTION__ {
public:
ReduceKernel(R reduction, sycl::range<1> slm_sz)
: reduction_(reduction), slm_sz_(slm_sz), shared_(), finished_() {}
void operator()(sycl::nd_item<2> pos) const {
reduction_.template run<output_vec_size>(pos, shared_, finished_);
}
void sycl_ker_config_convention(sycl::handler& cgh) {
shared_ = sycl_local_acc_t<char>(slm_sz_, cgh);
finished_ = sycl_local_acc_t<bool>({1}, cgh);
}
private:
R reduction_;
sycl::range<1> slm_sz_;
sycl_local_acc_t<char> shared_; /* group tree reduce */
sycl_local_acc_t<bool> finished_; /* last WG flag to broadcast inner WG */
};
template <typename index_t>
static OffsetCalculator<2, index_t> make_output_calculator(
const at::TensorIterator& iter) {
int num_reduce_dims = iter.num_reduce_dims();
int num_output_dims = iter.ndim() - num_reduce_dims;
int input_index = iter.ntensors() - 1;
int output_index = 0;
std::array<const int64_t*, 2> strides = {
iter.strides(output_index).data() + num_reduce_dims,
iter.strides(input_index).data() + num_reduce_dims,
};
auto shape = iter.shape().data() + num_reduce_dims;
return OffsetCalculator<2, index_t>(num_output_dims, shape, strides.data());
}
template <typename index_t>
static OffsetCalculator<1, index_t> make_input_calculator(
const at::TensorIterator& iter) {
int num_reduce_dims = iter.num_reduce_dims();
int input_index = iter.ntensors() - 1;
std::array<const int64_t*, 1> strides = {
iter.strides(input_index).data(),
};
return OffsetCalculator<1, index_t>(
num_reduce_dims, iter.shape().data(), strides.data());
}
template <typename out_scalar_t, typename func_t>
struct func_wrapper_t {
using arg_t = typename binary_function_traits<func_t>::arg1_t;
using scalar_t = typename binary_function_traits<func_t>::arg2_t;
func_t combine;
static inline out_scalar_t project(arg_t arg) {
return (out_scalar_t)arg;
}
static inline arg_t translate_idx(arg_t acc, int64_t /*idx*/) {
return acc;
}
func_wrapper_t(const func_t& op) : combine(op) {}
arg_t reduce(arg_t acc, scalar_t val, int64_t idx) const {
return combine(acc, val);
}
};
template <typename out_scalar_t, typename func_t>
func_wrapper_t<out_scalar_t, func_t> func_wrapper(const func_t& op) {
return func_wrapper_t<out_scalar_t, func_t>{op};
}
template <
typename scalar_t,
typename ops_t,
typename index_t,
typename out_scalar_t = scalar_t,
int vt0 = 4>
struct ReduceOp {
using traits = function_traits<decltype(&ops_t::reduce)>;
using arg_t =
typename std::decay<typename traits::template arg<0>::type>::type;
using InputCalculator = OffsetCalculator<1, index_t>;
using OutputCalculator = OffsetCalculator<2, index_t>;
using arg1_t =
typename binary_function_traits<decltype(&ops_t::combine)>::arg1_t;
using arg2_t =
typename binary_function_traits<decltype(&ops_t::combine)>::arg2_t;
static constexpr bool can_accumulate_in_output =
std::is_convertible<arg_t, out_scalar_t>::value &&
std::is_convertible<out_scalar_t, arg_t>::value;
static constexpr float acc_buffer_multiplier =
(float)sizeof(arg_t) / sizeof(out_scalar_t);
ops_t ops;
arg_t ident;
ReduceConfig config;
InputCalculator input_calc;
OutputCalculator output_calc;
const void* src;
const char* dst[2]; // it accepts at most two destinations
// acc_buf used for accumulation among sub Tensor Iterator when accumulation
// on output is not permissible
void* acc_buf;
// workgroup buf used for accumulation between workgrouops during global
// reduction
void* group_buf;
int* semaphores;
int64_t base_idx;
bool accumulate;
bool final_output;
int noutputs;
ReduceOp(
ops_t ops,
ReduceConfig config,
InputCalculator input_calc,
OutputCalculator output_calc,
const void* src,
char* dst0,
c10::optional<char*> dst1,
void* acc_buf,
void* group_buf,
int* semaphores,
arg_t ident,
int noutputs,
int64_t base_idx)
: ops(ops),
ident(ident),
config(config),
input_calc(input_calc),
output_calc(output_calc),
src(src),
acc_buf(acc_buf),
group_buf(group_buf),
semaphores(semaphores),
base_idx(base_idx),
noutputs(noutputs) {
dst[0] = dst0;
if (dst1.has_value()) {
dst[1] = dst1.value();
}
}
template <int output_vec_size>
void run(
sycl::nd_item<2> pos,
sycl_local_ptr<char> shared,
sycl_local_ptr<bool> finished) const {
index_t output_idx = config.output_idx<output_vec_size>(pos);
index_t input_idx = config.input_idx(pos);
auto base_offsets1 = output_calc.get(output_idx)[1];
using arg_vec_t = at::detail::Array<arg_t, output_vec_size>;
arg_vec_t value;
#pragma unroll(output_vec_size)
for (int i = 0; i < output_vec_size; i++) {
value[i] = ident;
}
if (output_idx < (index_t)config.num_outputs &&
input_idx < (index_t)config.num_inputs) {
const scalar_t* input_slice =
(const scalar_t*)((const char*)src + base_offsets1);
value = item_reduce<output_vec_size>(pos, input_slice);
}
// TODO: Currently, there are bugs with shuffle_down when the arg_t is a
// pair for half dtype, We temporarily workaround to do
// "reduce_for_compound_dtype" function.
constexpr bool is_pair =
std::is_same<std::pair<scalar_t, int64_t>, arg_t>::value;
auto combine = [=](arg1_t value, arg2_t other) -> arg1_t {
return ops.combine(value, other);
};
if (config.should_group_x_reduce() && config.should_group_y_reduce()) {
if constexpr (is_pair) {
value = group_reduce_for_compound_dtype<output_vec_size>(
pos, value, shared);
} else {
value = group_reduce<
arg_t,
decltype(pos),
decltype(combine),
output_vec_size>(pos, config.num_items, shared, value, combine);
}
} else {
if (config.should_group_y_reduce()) {
value = group_y_reduce<
arg_t,
decltype(pos),
decltype(combine),
output_vec_size>(pos, shared, value, combine);
}
if (config.should_group_x_reduce()) {
if constexpr (is_pair) {
value = group_x_reduce_for_compound_dtype<output_vec_size>(
pos, value, shared);
} else {
value = group_x_reduce<
arg_t,
decltype(pos),
decltype(combine),
output_vec_size>(pos, shared, value, combine);
}
}
}
using out_ptr_vec_t = at::detail::Array<out_scalar_t*, output_vec_size>;
using offset_vec_t = at::detail::Array<index_t, output_vec_size>;
offset_vec_t base_offsets;
out_ptr_vec_t out;
#pragma unroll
for (int i = 0; i < output_vec_size; i++) {
base_offsets[i] = output_calc.get(output_idx + i)[0];
out[i] = (out_scalar_t*)((char*)dst[0] + base_offsets[i]);
}
arg_vec_t* acc = nullptr;
if (acc_buf != nullptr) {
size_t numerator = sizeof(arg_t);
size_t denominator = sizeof(out_scalar_t);
reduce_fraction(numerator, denominator);
acc =
(arg_vec_t*)((char*)acc_buf + (base_offsets[0] * numerator / denominator));
}
if (config.should_global_reduce()) {
value = global_reduce<output_vec_size>(pos, value, acc, shared, finished);
} else if (config.should_store(pos, output_idx)) {
if (accumulate) {
#pragma unroll
for (int i = 0; i < output_vec_size; i++) {
value[i] = ops.translate_idx(value[i], base_idx);
}
}
if (acc == nullptr) {
if (accumulate) {
value =
accumulate_in_output<output_vec_size, can_accumulate_in_output>(
out, value);
}
if (final_output) {
set_results_to_output<output_vec_size>(value, base_offsets);
} else {
#pragma unroll
for (int i = 0; i < output_vec_size; i++) {
*(out[i]) = get_accumulated_output<can_accumulate_in_output>(
out[i], value[i]);
}
}
} else {
if (accumulate) {
#pragma unroll
for (int i = 0; i < output_vec_size; i++) {
value[i] = ops.combine((*acc)[i], value[i]);
}
}
if (final_output) {
set_results_to_output<output_vec_size>(value, base_offsets);
} else {
*acc = value;
}
}
}
}
template <int output_vec_size>
at::detail::Array<arg_t, output_vec_size> item_reduce(
sycl::nd_item<2> pos,
const scalar_t* data) const {
if (config.vectorize_input) {
// assert(output_vec_size == 1);
// reduce at the header of input_slice where memory is not aligned,
// so that group_reduce will have an aligned memory to work on.
switch (config.input_vec_size) {
case 16:
return {input_vectorized_item_reduce_impl<16>(pos, data)};
case 8:
return {input_vectorized_item_reduce_impl<8>(pos, data)};
case 4:
return {input_vectorized_item_reduce_impl<4>(pos, data)};
case 2:
return {input_vectorized_item_reduce_impl<2>(pos, data)};
default:
return {input_vectorized_item_reduce_impl<1>(pos, data)};
};
} else {
index_t element_stride = input_calc.strides_[0][0] / sizeof(scalar_t);
bool is_contiguous = (input_calc.dims == 1 && element_stride == 1);
if (is_contiguous) {
return item_reduce_impl<output_vec_size>(
pos, data, [](index_t idx) { return idx; });
} else if (input_calc.dims == 1) {
return item_reduce_impl<output_vec_size>(
pos, data, [&](index_t idx) { return idx * element_stride; });
} else {
return item_reduce_impl<output_vec_size>(pos, data, [&](index_t idx) {
return input_calc.get(idx)[0] / sizeof(scalar_t);
});
}
}
}
template <int input_vec_size>
arg_t input_vectorized_item_reduce_impl(
sycl::nd_item<2> pos,
const scalar_t* data) const {
index_t end = config.num_inputs;
// Handle the head of input slice where data is not aligned
arg_t value = ident;
// sycl provided the composition so we don't need to rewrite it.
constexpr int align_bytes =
alignof(at::native::memory::aligned_vector<scalar_t, input_vec_size>);
constexpr int align_elements = align_bytes / sizeof(scalar_t);
int shift = ((uint64_t)data) % align_bytes / sizeof(scalar_t);
if (shift > 0) {
int idx = pos.get_local_id(1);
if (idx >= shift && idx < align_elements &&
config.should_reduce_tail(pos)) {
value = ops.reduce(
value,
data[pos.get_local_id(1) - shift],
pos.get_local_id(1) - shift);
}
// align data to vector start
data += align_elements - shift;
if (end > (index_t)align_elements - shift)
end -= align_elements - shift; /* warning: end flip */
else
end = 0;
shift = align_elements - shift;
}
// Do the vectorized reduction
using load_t = at::native::memory::aligned_vector<scalar_t, input_vec_size>;
index_t idx = config.input_idx(pos);
const index_t stride = config.step_input;
// multiple registers
arg_t value_list[input_vec_size];
value_list[0] = value;
#pragma unroll
for (int i = 1; i < input_vec_size; ++i) {
value_list[i] = ident;
}
load_t values;
while (idx * input_vec_size + input_vec_size - 1 < end) {
values = reinterpret_cast<const load_t*>(data)[idx];
#pragma unroll
for (index_t i = 0; i < input_vec_size; ++i) {
value_list[i] = ops.reduce(
value_list[i], values[i], shift + idx * input_vec_size + i);
}
idx += stride;
}
// tail
index_t tail_start = end - end % input_vec_size;
if (config.should_reduce_tail(pos)) {
int idx = tail_start + pos.get_local_id(1);
if ((index_t)idx < end) {
value_list[0] = ops.reduce(value_list[0], data[idx], idx + shift);
}
}
// registers accumulation
#pragma unroll
for (int i = 1; i < input_vec_size; ++i) {
value_list[0] = ops.combine(value_list[0], value_list[i]);
}
return value_list[0];
}
template <int output_vec_size, typename offset_calc_t>
at::detail::Array<arg_t, output_vec_size> item_reduce_impl(
sycl::nd_item<2> pos,
const scalar_t* data_,
offset_calc_t calc) const {
index_t idx = config.input_idx(pos);
const index_t end = config.num_inputs;
const index_t stride = config.step_input;
using arg_vec_t = at::detail::Array<arg_t, output_vec_size>;
using load_t =
at::native::memory::aligned_vector<scalar_t, output_vec_size>;
const load_t* data = reinterpret_cast<const load_t*>(data_);
arg_vec_t value_list[vt0];
#pragma unroll(vt0)
for (int i = 0; i < vt0; i++) {
#pragma unroll(output_vec_size)
for (int j = 0; j < output_vec_size; ++j) {
value_list[i][j] = ident;
}
}
load_t values[vt0];
while (idx + (vt0 - 1) * stride < end) {
#pragma unroll(vt0)
for (index_t i = 0; i < vt0; ++i) {
values[i] = data[calc(idx + i * stride) / output_vec_size];
}
#pragma unroll(vt0)
for (index_t i = 0; i < vt0; ++i) {
#pragma unroll(output_vec_size)
for (index_t j = 0; j < output_vec_size; ++j) {
value_list[i][j] =
ops.reduce(value_list[i][j], values[i][j], idx + i * stride);
}
}
idx += stride * vt0;
}
// tail
int idx_ = idx;
#pragma unroll(vt0)
for (index_t i = 0; i < vt0; ++i) {
if (idx >= end) {
break;
}
values[i] = data[calc(idx) / output_vec_size];
idx += stride;
}
idx = idx_;
#pragma unroll(vt0)
for (index_t i = 0; i < vt0; ++i) {
if (idx >= end) {
break;
}
#pragma unroll(output_vec_size)
for (index_t j = 0; j < output_vec_size; ++j) {
value_list[i][j] = ops.reduce(value_list[i][j], values[i][j], idx);
}
idx += stride;
}
// combine accumulators
#pragma unroll(vt0)
for (int i = 1; i < vt0; ++i) {
#pragma unroll(output_vec_size)
for (index_t j = 0; j < output_vec_size; ++j) {
value_list[0][j] = ops.combine(value_list[0][j], value_list[i][j]);
}
}
return value_list[0];
}
// TODO: Currently, there are bugs with shuffle_down when the arg_t is a
// pair with half dtype, We temporarily workaround to do
// "reduce_for_compound_dtype" function.
template <int output_vec_size>
at::detail::Array<arg_t, output_vec_size> group_reduce_for_compound_dtype(
sycl::nd_item<2> pos,
at::detail::Array<arg_t, output_vec_size> value,
sycl_local_ptr<void> shared_memory) const {
auto sg = pos.get_sub_group();
uint32_t sbgrpSize = sg.get_local_range()[0];
int l_x = pos.get_local_linear_id();
int sg_lid = sg.get_local_linear_id();
int sg_gid = sg.get_group_linear_id();
int sg_range = sg.get_group_range()[0];
for (int offset = 1; offset < (int)sbgrpSize; offset <<= 1) {
#pragma unroll(output_vec_size)
for (int i = 0; i < output_vec_size; ++i) {
arg_t other = sg.shuffle_down(value[i], offset);
value[i] = ops.combine(value[i], other);
}
}
using args_vec_t = at::detail::Array<arg_t, output_vec_size>;
sycl_local_ptr<args_vec_t> shared{shared_memory};
if (sg_lid == 0) {
shared[sg_gid] = value;
}
pos.barrier(sycl_local_fence);
if (sg_range <= (int)sbgrpSize) {
// sub-group reduce
#pragma unroll(output_vec_size)
for (int i = 0; i < output_vec_size; i++) {
value[i] = ident;
}
if (sg_gid == 0 && sg_lid < sg_range) {
value = shared[sg_lid];
for (int offset = 1; offset < sg_range; offset <<= 1) {
#pragma unroll(output_vec_size)
for (int i = 0; i < output_vec_size; ++i) {
// Shuffle down separately for first and second pair.
std::pair<typename arg_t::first_type, typename arg_t::second_type>
other = std::pair<
typename arg_t::first_type,
typename arg_t::second_type>(
sg.shuffle_down(value[i].first, offset),
sg.shuffle_down(value[i].second, offset));
value[i] = ops.combine(value[i], other);
}
}
}
} else {
// work item tree reduce
if (l_x < sg_range) {
value = shared[l_x];
}
for (int offset = sg_range / 2; offset > 0; offset >>= 1) {
if (l_x < offset) {
args_vec_t other = shared[l_x + offset];
#pragma unroll(output_vec_size)
for (int i = 0; i < output_vec_size; ++i) {
value[i] = ops.combine(value[i], other[i]);
}
shared[l_x] = value;
}
pos.barrier(sycl_local_fence);
}
}
return value;
}
// TODO: Currently, there are bugs with shuffle_down when the arg_t is a
// pair for half dtype, We temporarily workaround to do
// "reduce_for_compound_dtype" function.
template <int output_vec_size>
at::detail::Array<arg_t, output_vec_size> group_x_reduce_for_compound_dtype(
sycl::nd_item<2> pos,
at::detail::Array<arg_t, output_vec_size> value,
sycl_local_ptr<void> shared_memory) const {
using args_vec_t = at::detail::Array<arg_t, output_vec_size>;
auto l_x = pos.get_local_id(1), l_y = pos.get_local_id(0);
auto gp_x = pos.get_local_range(1);
int dim_x = gp_x;
sycl_local_ptr<args_vec_t> shared(shared_memory);
auto sg = pos.get_sub_group();
uint32_t sbgrpSize = sg.get_local_range()[0];
if (dim_x > (int)sbgrpSize) {
int address_base = l_x + l_y * gp_x;
shared[address_base] = value;
for (int offset = dim_x / 2; offset >= (int)sbgrpSize; offset >>= 1) {
pos.barrier(sycl_local_fence);
if ((int)l_x < offset &&
(int)l_x + offset < (int)gp_x /* redundant??? */) {
args_vec_t other = shared[address_base + offset];
#pragma unroll(output_vec_size)
for (int i = 0; i < output_vec_size; ++i) {
value[i] = ops.combine(value[i], other[i]);
}
shared[address_base] = value;
}
}
dim_x = sbgrpSize;
}
pos.barrier(sycl_local_fence);
// sub-group reduction
for (int offset = 1; offset < dim_x; offset <<= 1) {
#pragma unroll(output_vec_size)
for (int i = 0; i < output_vec_size; ++i) {
std::pair<typename arg_t::first_type, typename arg_t::second_type>
other = std::
pair<typename arg_t::first_type, typename arg_t::second_type>(
sg.shuffle_down(value[i].first, offset),
sg.shuffle_down(value[i].second, offset));
value[i] = ops.combine(value[i], other);
}
}
return value;
}
// In/out from slm pointers
void mark_group_finished(sycl::nd_item<2> pos, sycl_local_ptr<bool> finished)
const {
pos.barrier(sycl_local_fence);
if (pos.get_local_linear_id() == 0) {
sycl_atomic_ref_rlx_dev_global_t<int> count(semaphores[pos.get_group(1)]);
int prev_groups_finished = count.fetch_add(
1, sycl_mem_odr_acq_rel
/* , default memory scope is device */);
finished[0] = (prev_groups_finished == (int)(pos.get_group_range(0) - 1));
}
pos.barrier(sycl_local_fence);
}
template <int output_vec_size, bool can_acc>
at::detail::Array<arg_t, output_vec_size> accumulate_in_output(
at::detail::Array<out_scalar_t*, output_vec_size> out,
at::detail::Array<arg_t, output_vec_size> value,
typename std::enable_if<can_acc>::type* = nullptr) const {
at::detail::Array<arg_t, output_vec_size> ret;
#pragma unroll(output_vec_size)
for (int i = 0; i < output_vec_size; ++i) {
ret[i] = ops.combine(*(out[i]), value[i]);
}
return ret;
}
template <bool can_acc>
out_scalar_t get_accumulated_output(
out_scalar_t* out,
arg_t value,
typename std::enable_if<can_acc>::type* = nullptr) const {
assert(!final_output);
return (out_scalar_t)value;
}
// This function should never be called --
// It's the version of `accumulate_in_output`
// when accumulation in the output is not possible.