-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathseg_ops.cu
1719 lines (1606 loc) · 78.2 KB
/
seg_ops.cu
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
#include <iostream>
#include <cstring>
#include <vector>
#include <random>
#include <limits>
#include <algorithm>
#include <string>
#include <cmath>
#include <ctime>
#include <utility>
#include <stdio.h>
#include <cuda.h>
#include <omp.h>
#include <cuda_runtime.h>
#include "helper_math.h"
#include <cub/device/device_scan.cuh>
#include <cub/device/device_segmented_reduce.cuh>
#include <cub/device/device_radix_sort.cuh>
static std::default_random_engine generator(1000);
const int kMaxGridNum = 65535;
#define CEIL_DIV(a, b) (((a) + (b) - 1) / (b))
#define ASSERT(x) if(!(x)) {std::cout << #x << " does not hold! File =" << __FILE__ << " Line=" << __LINE__ << std::endl;exit(0);}
#define IND2(x, y, sy) ((x) * (sy) + (y))
#define IND3(x, y, z, sy, sz) (IND2(x, y, sy) * (sz) + (z))
#define CUDA_POST_KERNEL_CHECK(x) \
do { \
cudaError err = cudaPeekAtLastError(); \
if(err != cudaSuccess) {std::cout << "Name: " << #x << " Line: " << __LINE__ << " ErrStr:" << cudaGetErrorString(err) << std::endl;exit(0);} \
} while (0)
enum SegReduceType {kSum, kMean, kMax, kMin};
enum SegTakeKCorrType { kInnerProduct, kEuclidean };
std::pair<dim3, dim3> KernelLauchParamB1G2(int total_count) {
const int thread_num = 256;
dim3 dimBlock(thread_num);
int grid_size = CEIL_DIV(total_count, thread_num);
int grid_dim_x = grid_size > kMaxGridNum ? 1024 : grid_size;
int grid_dim_y = grid_size > kMaxGridNum ? CEIL_DIV(grid_size, 1024) : 1;
dim3 dimGrid(grid_dim_x, grid_dim_y);
return std::make_pair(dimBlock, dimGrid);
}
std::pair<dim3, dim3> KernelLauchParamB1G1(int total_count) {
const int thread_num = 256;
dim3 dimBlock(thread_num);
int grid_size = CEIL_DIV(total_count, thread_num);
int grid_dim_x = grid_size > kMaxGridNum ? kMaxGridNum : grid_size;
dim3 dimGrid(grid_dim_x);
return std::make_pair(dimBlock, dimGrid);
}
class GPUTimer {
public:
GPUTimer() {
cudaEventCreate(&start_);
cudaEventCreate(&stop_);
}
~GPUTimer() {
cudaEventDestroy(start_);
cudaEventDestroy(stop_);
}
void start() {
cudaEventRecord(start_);
}
float stop() {
cudaEventRecord(stop_);
cudaEventSynchronize(stop_);
float milliseconds = 0.0f;
cudaEventElapsedTime(&milliseconds, start_, stop_);
return milliseconds;
}
private:
cudaEvent_t start_, stop_;
};
template<int UNROLL_X, typename DType>
__device__ void SumSharedMem(volatile DType* data) {
if(UNROLL_X == 1) {
if(threadIdx.x < 16) {
data[threadIdx.x] += data[threadIdx.x + 16];
data[threadIdx.x] += data[threadIdx.x + 8];
data[threadIdx.x] += data[threadIdx.x + 4];
data[threadIdx.x] += data[threadIdx.x + 2];
data[threadIdx.x] += data[threadIdx.x + 1];
}
} else {
//TODO Enable arbitrary UNROLL_X
if(UNROLL_X >= 8) {
data[threadIdx.x] += data[threadIdx.x + 128];
data[threadIdx.x + 32] += data[threadIdx.x + 128 + 32];
data[threadIdx.x + 64] += data[threadIdx.x + 128 + 64];
data[threadIdx.x + 96] += data[threadIdx.x + 128 + 96];
}
if(UNROLL_X >= 4) {
data[threadIdx.x] += data[threadIdx.x + 64];
data[threadIdx.x + 32] += data[threadIdx.x + 96];
}
data[threadIdx.x] += data[threadIdx.x + 32];
data[threadIdx.x] += data[threadIdx.x + 16];
data[threadIdx.x] += data[threadIdx.x + 8];
data[threadIdx.x] += data[threadIdx.x + 4];
data[threadIdx.x] += data[threadIdx.x + 2];
data[threadIdx.x] += data[threadIdx.x + 1];
}
}
/*Fills the selected position in the destination with the index
Important! Here we assume that the sel is sorted!
for i = 0 to seg_num - 1
if(ind_ptr[idx] == ind_ptr[idx + 1]) dst[sel[i]] = i
*/
__global__ void FillSegStartIndex(int* __restrict__ dst, const int* __restrict__ ind_ptr, int seg_num) {
for (int idx = threadIdx.x + blockDim.x * blockIdx.x; idx < seg_num; idx += blockDim.x * gridDim.x) {
if (ind_ptr[idx] != ind_ptr[idx + 1]) {
dst[ind_ptr[idx]] = idx;
}
}
}
struct GetSegId {
static size_t get_temp_bytes(int nnz) {
size_t temp_storage_bytes = 0;
cub::Max max_op;
cub::DeviceScan::InclusiveScan<int*, int*>(NULL, temp_storage_bytes, NULL, NULL, max_op, nnz);
return temp_storage_bytes;
}
static void compute(int* seg_ids, const int* ind_ptr, int seg_num, int nnz, char* temp_storage, size_t temp_storage_bytes, cudaStream_t stream) {
cudaMemsetAsync(seg_ids, 0, sizeof(int) * nnz, stream);
std::pair<dim3, dim3> block_grid_dim3 = KernelLauchParamB1G1(seg_num);
FillSegStartIndex <<<block_grid_dim3.second, block_grid_dim3.first, 0, stream>>> (seg_ids, ind_ptr, seg_num);
cub::Max max_op;
cub::DeviceScan::InclusiveScan(temp_storage, temp_storage_bytes, seg_ids, seg_ids, max_op, nnz, stream);
return;
}
};
struct MinOpCUB {
cub::Min cub_op;
template<typename DType>
__forceinline__ DType init_value() const {
return std::numeric_limits<DType>::max();
}
};
struct MaxOpCUB {
cub::Max cub_op;
template<typename DType>
__forceinline__ DType init_value() const {
return std::numeric_limits<DType>::lowest();
}
};
struct SumOpCUB {
cub::Sum cub_op;
template<typename DType>
__forceinline__ DType init_value() const {
return static_cast<DType>(0);
}
};
/*Expand the indptr to make it repeat k times and increase the values of the indices during the expansion
for i = 0 to repeat_num - 1
for j = 0 to seg_num - 1
dst[i * seg_num + j] = i * nnz + ind_ptr[j];
dst[repeat_num * seg_num] = repeat_num * nnz
*/
__global__ void ExpandIndptr(int* __restrict__ dst, const int* __restrict__ ind_ptr, int seg_num, int nnz, int repeat_num) {
for (int idx = threadIdx.x + blockDim.x * blockIdx.x; idx <= seg_num * repeat_num; idx += blockDim.x * gridDim.x) {
if (idx < seg_num * repeat_num) {
int seg_id = idx % seg_num;
int repeat_id = idx / seg_num;
dst[idx] = ind_ptr[seg_id] + repeat_id * nnz;
} else {
dst[idx] = repeat_num * nnz;
}
}
}
template<typename ReduceOp, typename DType>
struct BatchSegReduceContig {
static size_t get_temp_bytes(int batch_num, int nnz, int seg_num) {
size_t temp_storage_bytes = 0;
ReduceOp op;
cub::DeviceSegmentedReduce::Reduce<DType*, DType*>(NULL, temp_storage_bytes, NULL, NULL,
batch_num * seg_num, NULL, NULL,
op.cub_op, op.template init_value<DType>());
temp_storage_bytes += sizeof(int) * (seg_num * batch_num + 1); // Size for the new expaned ind_ptr
return temp_storage_bytes;
}
static void compute(DType* dst, const DType* src, const int* ind_ptr, int batch_num, int nnz, int seg_num, char* temp_storage, size_t temp_storage_bytes, cudaStream_t stream) {
int expanded_ind_ptr_size = seg_num * batch_num + 1;
ASSERT(temp_storage_bytes >= sizeof(int) * expanded_ind_ptr_size);
ReduceOp op;
int* expanded_ind_ptr = reinterpret_cast<int*>(temp_storage);
std::pair<dim3, dim3> block_grid_dim3 = KernelLauchParamB1G1(expanded_ind_ptr_size);
ExpandIndptr <<<block_grid_dim3.second, block_grid_dim3.first, 0, stream>>> (expanded_ind_ptr, ind_ptr, seg_num, nnz, batch_num);
//TODO(sxjscience) Use MSHADOW_CUDA_POST_KERNEL_CHECK
CUDA_POST_KERNEL_CHECK(ExpandIndptr);
temp_storage_bytes -= sizeof(int) * expanded_ind_ptr_size;
char* cub_temp_storage = temp_storage + sizeof(int) * expanded_ind_ptr_size;
cub::DeviceSegmentedReduce::Reduce(cub_temp_storage,
temp_storage_bytes,
src, dst, batch_num * seg_num,
expanded_ind_ptr, expanded_ind_ptr + 1, op.cub_op, op.template init_value<DType>(), stream);
}
};
struct plus {
template<typename DType>
__host__ __device__ __forceinline__ static DType Map(DType a, DType b) {
return a + b;
}
};
struct minus {
template<typename DType>
__host__ __device__ __forceinline__ static DType Map(DType a, DType b) {
return a - b;
}
};
struct div {
template<typename DType>
__host__ __device__ __forceinline__ static DType Map(DType a, DType b) {
return a / b;
}
};
struct mul {
template<typename DType>
__host__ __device__ __forceinline__ static DType Map(DType a, DType b) {
return a * b;
}
};
struct right {
template<typename DType>
__host__ __device__ __forceinline__ static DType Map(DType a, DType b) {
return b;
}
};
struct diff_square {
template<typename DType>
__host__ __device__ __forceinline__ static DType Map(DType a, DType b) {
return (a - b) * (a - b);
}
};
/* Compute broadcast rhs and apply the binary OP between lhs and rhs. Add the result to dst
dst : Shape (batch_num, nnz)
lhs : Shape (batch_num, nnz)
rhs : Shape (batch_num, seg_num)
seg_ids: Shape(nnz,)
for i = 0 to batch_num - 1
for j = 0 to nnz - 1
dst[i, j] += OP::Map(lhs[i, j], rhs[i, seg_ids[j]])
*/
template<typename OP, bool add_to, typename DType>
__global__ void SegBroadcastBinaryContigKernel(DType* __restrict__ dst,
const DType* lhs,
const DType* rhs,
const int* seg_ids,
int batch_num, int nnz, int seg_num) {
for(int idx= threadIdx.x + blockDim.x * blockIdx.x; idx < batch_num * nnz; idx += blockDim.x * gridDim.x) {
int batch_id = idx / nnz;
int ele_id = idx % nnz;
DType res = OP::Map(lhs[idx], rhs[batch_id * seg_num + seg_ids[ele_id]]);
if(add_to) {
dst[idx] += res;
} else {
dst[idx] = res;
}
}
}
struct BatchSegBroadcastBinaryContig {
static size_t get_temp_bytes(int nnz) {
size_t temp_storage_bytes = GetSegId::get_temp_bytes(nnz);
temp_storage_bytes += sizeof(int) * nnz; // Size of temp seg_ids
return temp_storage_bytes;
}
template<typename OP, bool add_to, typename DType>
static void compute(DType* dst, const DType* lhs, const DType* rhs, const int* ind_ptr, int batch_num, int nnz, int seg_num,
char* temp_storage, size_t temp_storage_bytes, cudaStream_t stream) {
int* seg_ids = reinterpret_cast<int*>(temp_storage);
GetSegId::compute(seg_ids, ind_ptr, seg_num, nnz,
temp_storage + sizeof(int) * nnz, temp_storage_bytes - sizeof(int) * nnz, stream);
std::pair<dim3, dim3> block_grid_dim3 = KernelLauchParamB1G1(batch_num * nnz);
SegBroadcastBinaryContigKernel<OP, add_to> <<<block_grid_dim3.second, block_grid_dim3.first, 0, stream>>>
(dst, lhs, rhs, seg_ids, batch_num, nnz, seg_num);
CUDA_POST_KERNEL_CHECK(SegBroadcastBinaryContigKernel);
}
};
struct BatchSegSoftmaxContig {
static size_t get_temp_bytes(int batch_num, int nnz, int seg_num) {
return 0;
}
};
struct BatchSegSoftmaxBackwardContig {
static size_t get_temp_bytes(int batch_num, int nnz, int seg_num) {
return 0;
}
};
/*For all the nodes, computes the inner product between the node and it's neighborhoods and add to dst.
dst: Shape (K, nnz)
embed1: Shape (K, node_num, feat_dim)
embed2: Shape (K, neighbor_node_num, feat_dim)
neighbor_ids: Shape (nnz, )
neighbor_ind_ptr: Shape(node_num + 1, )
rev_node_ids : Shape(nnz, ), The reverse mapping from 0->nnz-1 to node_ids
use mul to compute the inner-product and use squared_diff to compute the squared distance.
for k = 0 to K-1
for i = 0 to node_num - 1
for j = ind_ptr[i] to ind_ptr[i+1] - 1
neighbor_id = neighbor_ids[j]
dst[k, j] += InnerProduct(embed1[k, i], embed2[k, neighbor_id]) or ||embed1[k, i] - embed2[k, neighbor_id]||^2_2
*/
template<typename OP, int UNROLL_NODE = 1, int TY_SZ = 16, int UNROLL_Y = 4, int UNROLL_X = 4, int WARP_SZ = 32>
__global__ void
__launch_bounds__(TY_SZ * WARP_SZ)
SegTakeKCorrKernel(float* __restrict__ dst,
const float* embed1,
const float* embed2,
const int* neighbor_ids,
const int* neighbor_ind_ptr,
const int* rev_node_ids,
int K, int node_num, int neighbor_node_num,
int nnz, int feat_dim) {
int k = blockIdx.y;
__shared__ float embed1_shared[UNROLL_NODE * TY_SZ][WARP_SZ * UNROLL_X];
__shared__ float embed2_shared[TY_SZ][WARP_SZ * UNROLL_X];
__shared__ int rev_node_ids_shared[UNROLL_Y * TY_SZ];
__shared__ int neighbor_ids_shared[UNROLL_Y * TY_SZ];
__shared__ float dst_shared[UNROLL_Y * TY_SZ]; // Shared variable to store the result that should be saved to dst
for(int c_begin = 0; c_begin < feat_dim; c_begin += WARP_SZ * UNROLL_X) { // We deal with a bunch of channels and scan through the neighboring nodes to write to the destination
for(int b_nid = UNROLL_NODE * TY_SZ * blockIdx.x; b_nid < node_num; b_nid += UNROLL_NODE * TY_SZ * gridDim.x) {
int e_nid = min(b_nid + UNROLL_NODE * TY_SZ, node_num);
int b_neighbor_ind = neighbor_ind_ptr[b_nid];
int e_neighbor_ind = neighbor_ind_ptr[e_nid];
// 1. Load embed1 to shared memory
#pragma unroll
for(int j = 0; j < UNROLL_NODE; j++) {
int nid_delta = j * TY_SZ + threadIdx.y;
#pragma unroll
for(int i = 0; i < UNROLL_X; i++) {
int c_delta = i * WARP_SZ + threadIdx.x;
if(c_begin + c_delta < feat_dim && b_nid + nid_delta < e_nid) {
embed1_shared[nid_delta][c_delta] = embed1[IND3(k, b_nid + nid_delta, c_begin + c_delta, node_num, feat_dim)];
} else {
embed1_shared[nid_delta][c_delta] = 0.0f;
}
}
}
// 2. Compute the inner product between embed1 and embed2
for(int b_ind_inner = b_neighbor_ind; b_ind_inner < e_neighbor_ind; b_ind_inner += UNROLL_Y * TY_SZ) {
int e_ind_inner = min(b_ind_inner + UNROLL_Y * TY_SZ, e_neighbor_ind);
// 2.1 Initilaize the shared dst variables to zero.
#pragma unroll
for(int i = 0; i < CEIL_DIV(UNROLL_Y * TY_SZ, WARP_SZ); i++) {
if(threadIdx.y == 0) dst_shared[i * WARP_SZ + threadIdx.x] = 0.0f;
}
// 2.2 Load the rev_node_ids and neighbor_node_ids to shared memory
if(threadIdx.y == 0) {
#pragma unroll
for(int i = 0; i < CEIL_DIV(UNROLL_Y * TY_SZ, WARP_SZ); i++) {
if (b_ind_inner + i * WARP_SZ + threadIdx.x < e_ind_inner && i * WARP_SZ + threadIdx.x < UNROLL_Y * TY_SZ) {
rev_node_ids_shared[i * WARP_SZ + threadIdx.x] = rev_node_ids[b_ind_inner + i * WARP_SZ + threadIdx.x] - b_nid;
neighbor_ids_shared[i * WARP_SZ + threadIdx.x] = neighbor_ids[b_ind_inner + i * WARP_SZ + threadIdx.x];
}
}
}
__syncthreads();
// 2.3 Load embed2 to shared memory and do the computation
#pragma unroll
for(int j = 0; j < UNROLL_Y; j++) {
int ind_inner_delta = j * TY_SZ + threadIdx.y;
// 2.3.1 Perform the loading
#pragma unroll
for(int i = 0; i < UNROLL_X; i++) {
int c_delta = i * WARP_SZ + threadIdx.x;
if(c_delta + c_begin < feat_dim && b_ind_inner + ind_inner_delta < e_ind_inner) {
// Load and perform the binary operator
// TODO(sxjscience) potential overflow problem, consider use size_t instead
embed2_shared[threadIdx.y][c_delta] = OP::Map(embed2[IND3(k, neighbor_ids_shared[ind_inner_delta], c_delta + c_begin, neighbor_node_num, feat_dim)],
embed1_shared[rev_node_ids_shared[ind_inner_delta]][c_delta]);
} else {
embed2_shared[threadIdx.y][c_delta] = 0.0f;
}
}
// 2.3.2 Perform the reduction
SumSharedMem<UNROLL_X>(embed2_shared[threadIdx.y]);
// 2.3.3 Accumulate the result to the local dst variable
if(threadIdx.x == 0) dst_shared[j * TY_SZ + threadIdx.y] += embed2_shared[threadIdx.y][0];
__syncthreads();
}
// 2.4 Write the shared variable back to the global memory
if(threadIdx.y == 0) {
#pragma unroll
for (int i = 0; i < CEIL_DIV(UNROLL_Y * TY_SZ, WARP_SZ); i++) {
if (b_ind_inner + i * WARP_SZ + threadIdx.x < e_ind_inner && i * WARP_SZ + threadIdx.x < UNROLL_Y * TY_SZ) {
dst[k * nnz + b_ind_inner + i * WARP_SZ + threadIdx.x] += dst_shared[i * WARP_SZ + threadIdx.x];
}
}
}
__syncthreads();
}
}
}
}
/*Compute the backward pass of SegTakeKCorr w.r.t embed1 when inner product is used.
dst: Shape (K, node_num, feat_dim)
g_out: Shape (K, nnz)
embed2: Shape (K, neighbor_node_num, feat_dim)
neighbor_ids: Shape (nnz, )
neighbor_ind_ptr: Shape(node_num + 1, )
for k = 0 to K-1
for i = 0 to node_num - 1
dst[k, i, :] = 0
for j = ind_ptr[i] to ind_ptr[i+1] - 1
dst[k, i, :] += g_out[k, j] * embed2[k, neighbor_ids[j], :]
*/
template<int UNROLL_X = 4, int TX_SZ = 32>
__global__ void
__launch_bounds__(TX_SZ)
SegTakeKCorrBackwardEmbed1Kernel(float* __restrict__ dst,
const float* g_out,
const float* embed2,
const int* neighbor_ids,
const int* neighbor_ind_ptr,
int K, int node_num, int neighbor_node_num,
int nnz, int feat_dim) {
int k = blockIdx.z;
int c_begin = blockIdx.y * TX_SZ * UNROLL_X; // We deal with a bunch of channels and scan through the neighboring nodes to write to the destination
float dst_local[UNROLL_X];
for (int nid = blockIdx.x; nid < node_num; nid += gridDim.x) {
int b_neighbor_ind = neighbor_ind_ptr[nid];
int e_neighbor_ind = neighbor_ind_ptr[nid + 1];
#pragma unroll
for(int i = 0; i < UNROLL_X; i++) {
int c = c_begin + i * TX_SZ + threadIdx.x;
if(c < feat_dim) {
dst_local[i] = dst[IND3(k, nid, c, node_num, feat_dim)];
}
}
for(int j = b_neighbor_ind; j < e_neighbor_ind; j++) {
#pragma unroll
for(int i = 0; i < UNROLL_X; i++) {
int c = c_begin + i * TX_SZ + threadIdx.x;
if (c < feat_dim) {
dst_local[i] += g_out[k * nnz + j] * embed2[IND3(k, neighbor_ids[j], c, neighbor_node_num, feat_dim)];
}
}
}
#pragma unroll
for(int i = 0; i < UNROLL_X; i++) {
int c = c_begin + i * TX_SZ + threadIdx.x;
if(c < feat_dim) {
dst[IND3(k, nid, c, node_num, feat_dim)] = dst_local[i];
}
}
}
}
__global__ void IdxArrayKernel(int* dst, int size) {
for(int idx = threadIdx.x + blockDim.x * blockIdx.x; idx < size; idx += blockDim.x * gridDim.x) {
dst[idx] = idx;
}
}
/*Compute the backward pass of SegTakeKCorr w.r.t embed2 when inner product is used.
dst: Shape (K, neighbor_node_num, feat_dim)
g_out: Shape (K, nnz)
embed1: Shape (K, node_num, feat_dim)
sorted_neighbor_ids: Shape (nnz, )
original_inds: Shape(nnz, )
rev_node_ids : Shape(nnz, ), The reverse mapping from 0->nnz-1 to node_ids
for k = 0 to K - 1
for i = 0 to nnz - 1
dst_ind = sorted_neighbor_ids[i]
src_ind = sorted_inds[i]
dst[k, dst_ind, :] += g_out[k, src_ind] * embed1[k, rev_node_ids[src_ind], :]
*/
template<int UNROLL_X = 4, int TX_SZ = 32, int TY_SZ = 1>
__global__ void
__launch_bounds__(TX_SZ * TY_SZ)
SegTakeKCorrBackwardEmbed2Kernel(float* dst,
const float* g_out,
const float* embed1,
const int* sorted_neighbor_ids,
const int* sorted_inds,
const int* rev_node_ids,
int K, int node_num, int neighbor_node_num,
int nnz, int feat_dim) {
int k = blockIdx.z;
int c_begin = blockIdx.y * TX_SZ * UNROLL_X; // We deal with a bunch of channels and scan through the neighboring nodes to write to the destination
float dst_local[UNROLL_X];
int idx = blockIdx.x * TY_SZ + threadIdx.y;
if (idx < nnz && (idx == 0 || sorted_neighbor_ids[idx] != sorted_neighbor_ids[idx - 1])) {
const int dst_ind = sorted_neighbor_ids[idx];
#pragma unroll
for (int i = 0; i < UNROLL_X; i++) {
int c = c_begin + i * TX_SZ + threadIdx.x;
if (c < feat_dim) {
dst_local[i] = dst[IND3(k, dst_ind, c, neighbor_node_num, feat_dim)];
}
}
do {
const int src_ind = sorted_inds[idx];
#pragma unroll
for(int i = 0; i < UNROLL_X; i++) {
int c = c_begin + i * TX_SZ + threadIdx.x;
if (c < feat_dim) {
dst_local[i] += g_out[k * nnz + src_ind] * embed1[IND3(k, rev_node_ids[src_ind], c, node_num, feat_dim)];
}
}
idx++;
} while (idx < nnz && (sorted_neighbor_ids[idx] == sorted_neighbor_ids[idx - 1]));
#pragma unroll
for (int i = 0; i < UNROLL_X; i++) {
int c = c_begin + i * TX_SZ + threadIdx.x;
if (c < feat_dim) {
dst[IND3(k, dst_ind, c, neighbor_node_num, feat_dim)] = dst_local[i];
}
}
}
}
/*Compute the backward pass of SegTakeKCorr w.r.t embed2 when inner product is used.
dst: Shape (K, neighbor_node_num, feat_dim)
g_out: Shape (K, nnz)
embed1: Shape (K, node_num, feat_dim)
neighbor_ids: Shape (nnz, )
neighbor_ind_ptr: Shape(node_num + 1, )
rev_node_ids : Shape(nnz, ), The reverse mapping from 0->nnz-1 to node_ids
for k = 0 to K-1
for i = 0 to node_num - 1
for j = ind_ptr[i] to ind_ptr[i+1] - 1
dst[k, neighbor_ids[j], :] += g_out[k, j] * embed1[k, rev_node_ids[j], :]
TODO(sxjscience) Optimize the speed of this function
First reorganize the data in neighbor_ids, g_out, embed1, ...
for k = 0 to K-1
for i = 0 to neighbor_node_num - 1
for j = rev_ind_ptr[i] to rev_ind_ptr[i + 1] - 1
dst[k, i, :] += reorder_g_out[k, j] * embed1[k, node_ids[j], :]
*/
template<int TY_SZ = 16, int UNROLL_Y = 4, int UNROLL_X = 4, int WARP_SZ = 32>
__global__ void
__launch_bounds__(TY_SZ * WARP_SZ)
SegTakeKCorrBackwardEmbed2KernelAtomic(float* __restrict__ dst,
const float* g_out,
const float* embed1,
const int* neighbor_ids,
const int* neighbor_ind_ptr,
const int* rev_node_ids,
int K, int node_num, int neighbor_node_num,
int nnz, int feat_dim) {
int k = blockIdx.z;
int c_begin = blockIdx.y * WARP_SZ * UNROLL_X; // We deal with a bunch of channels and scan through the neighboring nodes to write to the destination
__shared__ float embed1_shared[TY_SZ][WARP_SZ * UNROLL_X];
__shared__ float g_out_shared[TY_SZ * UNROLL_Y];
__shared__ int neighbor_ids_shared[TY_SZ * UNROLL_Y];
__shared__ int rev_node_ids_shared[TY_SZ * UNROLL_Y];
for(int b_nid = TY_SZ * blockIdx.x; b_nid < node_num; b_nid += TY_SZ * gridDim.x) {
int e_nid = min(b_nid + TY_SZ, node_num);
int b_neighbor_ind = neighbor_ind_ptr[b_nid];
int e_neighbor_ind = neighbor_ind_ptr[e_nid];
// 1. Load embed1 to shared memory
#pragma unroll
for(int i = 0; i < UNROLL_X; i++) {
int c_delta = i * WARP_SZ + threadIdx.x;
if(c_begin + c_delta < feat_dim && b_nid + threadIdx.y < e_nid) {
embed1_shared[threadIdx.y][c_delta] = embed1[IND3(k, b_nid + threadIdx.y, c_begin + c_delta, node_num, feat_dim)];
} else {
embed1_shared[threadIdx.y][c_delta] = 0.0f;
}
}
// 2. Write back the gradient, use atomic write
for(int b_ind_inner = b_neighbor_ind; b_ind_inner < e_neighbor_ind; b_ind_inner += TY_SZ * UNROLL_Y) {
int e_ind_inner = min(b_ind_inner + UNROLL_Y * TY_SZ, e_neighbor_ind);
// 2.1 Load g_out
if(threadIdx.y == 0) {
#pragma unroll
for (int i = 0; i < CEIL_DIV(UNROLL_Y * TY_SZ, WARP_SZ); i++) {
int ind_delta = i * WARP_SZ + threadIdx.x;
if (b_ind_inner + ind_delta < e_ind_inner && ind_delta < UNROLL_Y * TY_SZ) {
g_out_shared[ind_delta] = g_out[k * nnz + b_ind_inner + ind_delta];
neighbor_ids_shared[ind_delta] = neighbor_ids[b_ind_inner + ind_delta];
rev_node_ids_shared[ind_delta] = rev_node_ids[b_ind_inner + ind_delta] - b_nid;
}
}
}
__syncthreads();
#pragma unroll
for(int j = 0; j < UNROLL_Y; j++) {
int ind_delta = j * TY_SZ + threadIdx.y;
float g_out_ele = g_out_shared[ind_delta];
int neighbor_id = neighbor_ids_shared[ind_delta];
int rev_node_id = rev_node_ids_shared[ind_delta];
#pragma unroll
for(int i = 0; i < UNROLL_X; i++) {
int c_delta = i * WARP_SZ + threadIdx.x;
if(c_delta + c_begin < feat_dim && b_ind_inner + ind_delta < e_ind_inner) {
atomicAdd(dst + IND3(k, neighbor_id, c_delta + c_begin, neighbor_node_num, feat_dim),
g_out_ele * embed1_shared[rev_node_id][c_delta]);
}
}
}
__syncthreads();
}
}
}
struct SegTakeKCorr {
static size_t get_temp_bytes(int nnz) {
size_t temp_storage_bytes = GetSegId::get_temp_bytes(nnz);
temp_storage_bytes += sizeof(int) * nnz; // Size of temp seg_ids
return temp_storage_bytes;
}
static size_t get_sort_temp_bytes(int nnz, int node_num) {
size_t temp_storage_bytes = 0;
cub::DeviceRadixSort::SortPairs<int, int>(NULL, temp_storage_bytes, NULL, NULL, NULL, NULL, nnz);
return temp_storage_bytes;
}
static size_t get_temp_bytes_backward_embed2(int nnz, int seg_num) {
size_t temp_storage_bytes = GetSegId::get_temp_bytes(nnz);
temp_storage_bytes += get_sort_temp_bytes(nnz, seg_num);
temp_storage_bytes += sizeof(int) * nnz; // Size of temp seg_ids
temp_storage_bytes += sizeof(int) * nnz; // Size of temp sorted_neighbor_ids
temp_storage_bytes += sizeof(int) * nnz; // Size of temp value_in
temp_storage_bytes += sizeof(int) * nnz; // Size of temp value_out
return temp_storage_bytes;
}
template<bool add_to>
static void compute(float* dst, const float* embed1, const float* embed2,
const int* neighbor_ids, const int* neighbor_ind_ptr,
int K, int node_num, int neighbor_node_num, int nnz, int feat_dim, int type,
char* temp_storage, size_t temp_storage_bytes, cudaStream_t stream) {
long long K_ll = static_cast<long long>(K);
long long node_num_ll = static_cast<long long>(node_num);
long long neighbor_node_num_ll = static_cast<long long>(neighbor_node_num);
long long feat_dim_ll = static_cast<long long>(feat_dim);
long long int_max_ll = static_cast<long long>(std::numeric_limits<int>::max());
ASSERT(K_ll * node_num_ll * feat_dim_ll < int_max_ll);
ASSERT(K_ll * neighbor_node_num_ll * feat_dim_ll < int_max_ll);
if(!add_to) {
cudaMemsetAsync(dst, 0, sizeof(float) * K * nnz, stream);
}
int* rev_node_ids = reinterpret_cast<int*>(temp_storage);
GetSegId::compute(rev_node_ids, neighbor_ind_ptr, node_num, nnz,
temp_storage + sizeof(int) * nnz, temp_storage_bytes - sizeof(int) * nnz, stream);
static const int UNROLL_NODE = 1;
static const int TY_SZ = 16;
static const int UNROLL_Y = 4;
static const int UNROLL_X = 4;
static const int WARP_SZ = 32;
dim3 dimBlock(WARP_SZ, TY_SZ);
dim3 dimGrid(CEIL_DIV(node_num, UNROLL_NODE * TY_SZ), K);
if (type == SegTakeKCorrType::kInnerProduct) {
SegTakeKCorrKernel<mul, UNROLL_NODE, TY_SZ, UNROLL_Y, UNROLL_X, WARP_SZ> <<<dimGrid, dimBlock, 0, stream >>>
(dst, embed1, embed2, neighbor_ids, neighbor_ind_ptr, rev_node_ids, K, node_num, neighbor_node_num, nnz, feat_dim);
} else if (type == SegTakeKCorrType::kEuclidean) {
SegTakeKCorrKernel<diff_square, UNROLL_NODE, TY_SZ, UNROLL_Y, UNROLL_X, WARP_SZ> <<<dimGrid, dimBlock, 0, stream >>>
(dst, embed1, embed2, neighbor_ids, neighbor_ind_ptr, rev_node_ids, K, node_num, neighbor_node_num, nnz, feat_dim);
} else {
ASSERT(false);
}
CUDA_POST_KERNEL_CHECK(SegTakeKCorrKernel);
}
template<bool add_to>
static void compute_grad_embed1(float* dst, const float* g_out, const float* embed2,
const int* neighbor_ids, const int* neighbor_ind_ptr,
int K, int node_num, int neighbor_node_num, int nnz, int feat_dim, int type, cudaStream_t stream) {
ASSERT(type == SegTakeKCorrType::kInnerProduct);
long long K_ll = static_cast<long long>(K);
long long node_num_ll = static_cast<long long>(node_num);
long long neighbor_node_num_ll = static_cast<long long>(neighbor_node_num);
long long feat_dim_ll = static_cast<long long>(feat_dim);
long long int_max_ll = static_cast<long long>(std::numeric_limits<int>::max());
ASSERT(K_ll * node_num_ll * feat_dim_ll < int_max_ll);
ASSERT(K_ll * neighbor_node_num_ll * feat_dim_ll < int_max_ll);
if(!add_to) {
cudaMemsetAsync(dst, 0, sizeof(float) * K * node_num * feat_dim, stream);
}
static const int UNROLL_X = 4;
static const int TX_SZ = 32;
dim3 dimBlock(TX_SZ);
dim3 dimGrid(node_num, CEIL_DIV(feat_dim, TX_SZ * UNROLL_X), K);
SegTakeKCorrBackwardEmbed1Kernel<UNROLL_X, TX_SZ> <<<dimGrid, dimBlock, 0, stream >>>
(dst, g_out, embed2, neighbor_ids, neighbor_ind_ptr, K, node_num,
neighbor_node_num, nnz, feat_dim);
CUDA_POST_KERNEL_CHECK(SegTakeKCorrBackwardEmbed1Kernel);
}
template<bool add_to>
static void compute_grad_embed2(float* dst, const float* g_out, const float* embed1,
const int* neighbor_ids, const int* neighbor_ind_ptr,
int K, int node_num, int neighbor_node_num, int nnz, int feat_dim, int type,
char* temp_storage, size_t temp_storage_bytes, cudaStream_t stream) {
ASSERT(type == SegTakeKCorrType::kInnerProduct);
long long K_ll = static_cast<long long>(K);
long long node_num_ll = static_cast<long long>(node_num);
long long neighbor_node_num_ll = static_cast<long long>(neighbor_node_num);
long long feat_dim_ll = static_cast<long long>(feat_dim);
long long int_max_ll = static_cast<long long>(std::numeric_limits<int>::max());
ASSERT(K_ll * node_num_ll * feat_dim_ll < int_max_ll);
ASSERT(K_ll * neighbor_node_num_ll * feat_dim_ll < int_max_ll);
if (!add_to) {
cudaMemsetAsync(dst, 0, sizeof(float) * K * neighbor_node_num * feat_dim, stream);
}
int* rev_node_ids = reinterpret_cast<int*>(temp_storage);
temp_storage += sizeof(int) * nnz;
int* sorted_neighbor_ids = reinterpret_cast<int*>(temp_storage);
temp_storage += sizeof(int) * nnz;
int* temp_ind_in = reinterpret_cast<int*>(temp_storage);
temp_storage += sizeof(int) * nnz;
int* sorted_ind = reinterpret_cast<int*>(temp_storage);
temp_storage += sizeof(int) * nnz;
// 1. Sort the neighbor_ids
std::pair<dim3, dim3> block_grid_dim3 = KernelLauchParamB1G1(nnz);
IdxArrayKernel <<<block_grid_dim3.second, block_grid_dim3.first, 0, stream>>> (temp_ind_in, nnz);
size_t temp_sort_bytes = get_sort_temp_bytes(nnz, node_num);
cub::DeviceRadixSort::SortPairs(temp_storage, temp_sort_bytes,
neighbor_ids, sorted_neighbor_ids,
temp_ind_in, sorted_ind, nnz, 0, sizeof(int) * 8, stream);
temp_storage += get_sort_temp_bytes(nnz, node_num);
// 2. Compute the rev mapping
GetSegId::compute(rev_node_ids, neighbor_ind_ptr, node_num, nnz, temp_storage, GetSegId::get_temp_bytes(nnz), stream);
// 3. Run the kernel
static const int UNROLL_X = 4;
static const int TX_SZ = 32;
static const int TY_SZ = 1;
dim3 dimBlock(TX_SZ, TY_SZ);
dim3 dimGrid(CEIL_DIV(nnz, TY_SZ), CEIL_DIV(feat_dim, TX_SZ * UNROLL_X), K);
SegTakeKCorrBackwardEmbed2Kernel<UNROLL_X, TX_SZ, TY_SZ> <<<dimGrid, dimBlock, 0, stream >>>
(dst, g_out, embed1, sorted_neighbor_ids, sorted_ind, rev_node_ids, K, node_num,
neighbor_node_num, nnz, feat_dim);
CUDA_POST_KERNEL_CHECK(SegTakeKCorrBackwardEmbed2Kernel);
}
};
template<typename OP>
void SegTakeKCorrCPU(float* dst, const float* embed1, const float* embed2,
const int* neighbor_ids, const int* neighbor_ind_ptr,
int K, int node_num, int neighbor_node_num, int nnz, int feat_dim) {
for(int k = 0; k < K; k++) {
#pragma omp parallel for
for(int i = 0; i < node_num; i++) {
for (int j = neighbor_ind_ptr[i]; j < neighbor_ind_ptr[i + 1]; j++) {
// Calculate the distance between embed1[k, i, :] and embed2[k, neighbor_ids[j], :]
dst[k * nnz + j] = 0;
for(int c = 0; c < feat_dim; c++) {
dst[k * nnz + j] += OP::Map(embed1[IND3(k, i, c, node_num, feat_dim)],
embed2[IND3(k, neighbor_ids[j], c, neighbor_node_num, feat_dim)]);
}
}
}
}
return;
}
void SegTakeKCorrCPUBackwardEmbed1(float* dst, const float* g_out, const float* embed2,
const int* neighbor_ids, const int* neighbor_ind_ptr,
int K, int node_num, int neighbor_node_num, int nnz, int feat_dim) {
for(int k = 0; k < K; k++) {
#pragma omp parallel for
for(int i = 0; i < node_num; i++) {
for(int c = 0; c < feat_dim; c++) {
dst[IND3(k, i, c, node_num, feat_dim)] = 0;
}
for(int j = neighbor_ind_ptr[i]; j < neighbor_ind_ptr[i + 1]; j++) {
for(int c = 0; c < feat_dim; c++) {
dst[IND3(k, i, c, node_num, feat_dim)] += g_out[k * nnz + j] * embed2[IND3(k, neighbor_ids[j], c, neighbor_node_num, feat_dim)];
}
}
}
}
}
void SegTakeKCorrCPUBackwardEmbed2(float* dst, const float* g_out, const float* embed1,
const int* neighbor_ids, const int* neighbor_ind_ptr,
int K, int node_num, int neighbor_node_num, int nnz, int feat_dim) {
std::vector<int> seg_ids(nnz);
for(int i = 0; i < node_num; i++) {
for(int j = neighbor_ind_ptr[i]; j < neighbor_ind_ptr[i + 1]; j++) {
seg_ids[j] = i;
}
}
std::memset(dst, 0, sizeof(float) * K * neighbor_node_num * feat_dim);
#pragma omp parallel for
for(int k = 0; k < K; k++) {
for(int i = 0; i < nnz; i++) {
for(int c = 0; c < feat_dim; c++) {
dst[IND3(k, neighbor_ids[i], c, neighbor_node_num, feat_dim)] += g_out[k * nnz + i] * embed1[IND3(k, seg_ids[i], c, node_num, feat_dim)];
}
}
}
}
/*Divide the elements in a segmentation by the length of the segmentation.
If the length of the segmentation is zero, no division will take place.
data: Shape(batch_num, seg_num, feat_dim)
indptr: Shape(seg_num + 1,)
*/
template<int UNROLL_X = 4, int WARP_SZ = 32>
__global__ void
__launch_bounds__(WARP_SZ)
BatchDivSegLength(float* data, const int* indptr, int batch_num, int seg_num, int feat_dim) {
int batch_id = blockIdx.z;
int c_begin = blockIdx.y * UNROLL_X * WARP_SZ;
for (int seg_id = blockIdx.x; seg_id < seg_num; seg_id += gridDim.x) {
int ind_end = indptr[seg_id + 1];
int ind_begin = indptr[seg_id];
#pragma unroll
for(int i = 0; i < UNROLL_X; i++) {
int c = c_begin + i * WARP_SZ + threadIdx.x;
if (c < feat_dim && ind_end > ind_begin) {
data[IND3(batch_id, seg_id, c, seg_num, feat_dim)] /= (ind_end - ind_begin);
}
}
}
}
/*Take the sum/mean/max/min of the data within the segments
dst_value: Shape (batch_num, seg_num, feat_dim)
dst_index: Shape (batch_num, seg_num, feat_dim)
data: Shape (batch_num, total_ind_num, feat_dim)
indices: Shape (nnz, )
indptr: Shape (seg_num + 1, )
for k = 0 to batch_num - 1
for i = 0 to seg_num - 1
if max or min:
initialize dst_index to -1
for j = indptr[i] to indptr[i+1] - 1
if sum:
dst_value[k, i, :] += data[k, indices[j], :]
else if max:
if(dst_value[k, i, :] > data[k, indices[j], :]
dst_value[k, i, :] = data[k, indices[j], :]
dst_index[k, i, :] = indices[j]
else if min:
if(dst_value[k, i, :] < data[k, indices[j], :]
dst_value[k, i, :] = data[k, indices[j], :]
dst_index[k, i, :] = indices[j]
*/
template<int reduce_type, int UNROLL_X = 4, int WARP_SZ = 32>
__global__ void
__launch_bounds__(WARP_SZ)
SegPoolKernel(float* dst_value, int* dst_index,
const float* data, const int* indices, const int* indptr,
int batch_num, int seg_num, int feat_dim, int total_ind_num, int nnz) {
int batch_id = blockIdx.z;
int c_begin = blockIdx.y * UNROLL_X * WARP_SZ;
float dst_value_local[UNROLL_X];
int dst_index_local[UNROLL_X];
for(int seg_id = blockIdx.x; seg_id < seg_num; seg_id += gridDim.x) {
int ind_begin = indptr[seg_id];
int ind_end = indptr[seg_id + 1];
#pragma unroll
for(int i = 0; i < UNROLL_X; i++) {
if(reduce_type == SegReduceType::kSum || reduce_type == SegReduceType::kMean) {
dst_value_local[i] = 0;
} else if(reduce_type == SegReduceType::kMax) {
dst_value_local[i] = -FLT_MAX;
dst_index_local[i] = -1;
} else if(reduce_type == SegReduceType::kMin) {
dst_value_local[i] = FLT_MAX;
dst_index_local[i] = -1;
}
}
for(int j = ind_begin; j < ind_end; j++) {
int data_ind = indices[j];
// Perform the reduction
#pragma unroll
for(int i = 0; i < UNROLL_X; i++) {
int c = c_begin + i * WARP_SZ + threadIdx.x;
if(c < feat_dim) {
float data_val = data[IND3(batch_id, data_ind, c, total_ind_num, feat_dim)];
if (reduce_type == SegReduceType::kSum || reduce_type == SegReduceType::kMean) {
dst_value_local[i] += data_val;
} else if (reduce_type == SegReduceType::kMax) {
if(data_val > dst_value_local[i]) {
dst_value_local[i] = data_val;
dst_index_local[i] = j;
}
} else if (reduce_type == SegReduceType::kMin) {
if (data_val < dst_value_local[i]) {
dst_value_local[i] = data_val;
dst_index_local[i] = j;
}
}
}
}
}
if(reduce_type == SegReduceType::kMean) {
#pragma unroll
for(int i = 0; i < UNROLL_X; i++) {
int c = c_begin + i * WARP_SZ + threadIdx.x;
if (c < feat_dim && ind_end - ind_begin > 0) {
dst_value_local[i] /= (ind_end - ind_begin);
}
}
}
#pragma unroll
for(int i = 0; i < UNROLL_X; i++) {
int c = c_begin + i * WARP_SZ + threadIdx.x;
if(c < feat_dim) {
int dst_ind = IND3(batch_id, seg_id, c, seg_num, feat_dim);
dst_value[dst_ind] = dst_value_local[i];
if(reduce_type == SegReduceType::kMax || reduce_type == SegReduceType::kMin) {
dst_index[dst_ind] = dst_index_local[i];
}
}
}
}
}
/*Backward pass of the SegPool operator when sum is used
dst: Shape (batch_num, total_ind_num, feat_dim)
g_out: Shape(batch_num, seg_num, feat_dim)
out_index: Shape (batch_num, seg_num, feat_dim)
sorted_indices : Shape (nnz,)
sorted_orig_inds: Shape (nnz, )
seg_ids: Shape(nnz,)
indptr: Shape (seg_num + 1, )
for k = 0 to batch_num - 1
for i = 0 to seg_num - 1
for j = indptr[i] to indptr[i+1] - 1
if sum:
dst[k, indices[j], :] += g_out[k, i, :]
elif mean:
dst[k, indices[j], :] += g_out[k, i, :] / (indptr[i+1] - indptr[i])
else:
dst[k, indices[j], :] += g_out[k, i, :] * (out_index[k, i, :] == indices[j])
Sorted Case ==>
for k = 0 to batch_num - 1
for i = 0 to nnz - 1
dst_ind = sorted_indices[i] --> indices[j]
orig_ind = sorted_orig_inds[i] --> j
seg_id = seg_ids[orig_ind] --> i
if sum:
dst[k, dst_ind, :] += g_out[k, seg_id, :]
elif mean:
dst[k, dst_ind, :] += g_out[k, seg_id, :] / (indptr[seg_id + 1] - indptr[seg_id])
else:
orig_ind = sorted_orig_inds[i] --> j
dst[k, dst_ind, :] += g_out[k, seg_id, :] * (out_index[k, seg_id, :] == orig_ind)