psamask_mlu_kernel.mlu 25.9 KB
Newer Older
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
/*************************************************************************
 * Copyright (C) 2022 Cambricon.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *************************************************************************/
#include "common_mlu_helper.hpp"
#include "psamask_utils.hpp"

#define COMPUTE_COUNT_ALIGN 64

__nram__ char buf[MAX_NRAM_SIZE];

template <typename T>
__mlu_func__ void swap(T &a, T &b) {
  T tmp = a;
  a = b;
  b = tmp;
}

template <typename T>
__mlu_func__ void storeDataFromNramToDram(T *dst, const T *src,
                                          const PositionInCore &position,
                                          const Shape &shape_full) {
  int n_offset = shape_full.h * shape_full.w * shape_full.c;
  int h_offset = shape_full.w * shape_full.c;
  int w_offset = shape_full.c;
  int n_seg = position.n_end - position.n_start;
  int h_seg = position.h_end - position.h_start;
  int w_seg = position.w_end - position.w_start;
  int size = h_seg * w_seg * shape_full.c;

  __memcpy(dst + position.n_start * n_offset + position.h_start * h_offset +
               position.w_start * w_offset,
           src, size * sizeof(T), NRAM2GDRAM, n_offset * sizeof(T),
           size * sizeof(T), n_seg - 1);
}

template <typename T>
__mlu_func__ void loadDataFromDramToNram(T *dst, const T *src,
                                         const PositionInCore &position,
                                         const Shape &shape_full) {
  int n_offset = shape_full.h * shape_full.w * shape_full.c;
  int h_offset = shape_full.w * shape_full.c;
  int w_offset = shape_full.c;
  int n_seg = position.n_end - position.n_start;
  int h_seg = position.h_end - position.h_start;
  int w_seg = position.w_end - position.w_start;
  int size = h_seg * w_seg * shape_full.c;

56
57
  __memcpy(dst, src + position.n_start * n_offset +
                    position.h_start * h_offset + position.w_start * w_offset,
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
           size * sizeof(T), GDRAM2NRAM, size * sizeof(T), n_offset * sizeof(T),
           n_seg - 1);
}

// transpose the data from A*B*C*(D*E) to A*D*E*(B*C)
template <typename T>
__mlu_func__ void transposeData(T *dst, T *src, const Shape &shape_seg) {
  int align_c = CEIL_ALIGN(shape_seg.c, COMPUTE_COUNT_ALIGN / sizeof(T));
  int align_hw =
      CEIL_ALIGN(shape_seg.h * shape_seg.w, COMPUTE_COUNT_ALIGN / sizeof(T));
  for (int i = 0; i < shape_seg.n; ++i) {
    __bang_transpose(dst, src, align_hw, align_c);
    dst += align_hw * align_c;
    src += align_hw * align_c;
  }
}

template <typename T>
__mlu_func__ void psamaskCollectForward(
    const T *x_dram, T *y_dram, const PositionInCore &position,
    const Shape &x_full, const Shape &y_full, const Shape &shape_seg,
    const int h_mask, const int w_mask, const int half_h_mask,
    const int half_w_mask) {
  T *x_nram = (T *)buf;
  T *y_nram =
      x_nram + CEIL_ALIGN(shape_seg.n * shape_seg.h * shape_seg.w * x_full.c,
                          COMPUTE_COUNT_ALIGN / sizeof(T));
  loadDataFromDramToNram(x_nram, x_dram, position, x_full);

  // fill zeros to output
  int elem_count =
      CEIL_ALIGN(shape_seg.n * shape_seg.h * shape_seg.w * y_full.c,
                 NFU_ALIGN_SIZE / sizeof(T));
91
  __bang_write_value(y_nram, elem_count, (T)0);
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

  int y_n_offset = shape_seg.h * shape_seg.w * shape_seg.c;
  int y_h_offset = shape_seg.w * shape_seg.c;
  int y_w_offset = shape_seg.c;
  int x_n_offset = shape_seg.h * shape_seg.w * x_full.c;
  int y_c_offset = 1;
  int x_h_offset = shape_seg.w * x_full.c;
  int x_w_offset = x_full.c;
  int x_c_offset = 1;
  int x_start = 0;
  int y_start = 0;
  for (int nidx = 0; nidx < shape_seg.n; ++nidx) {
    for (int hidx = 0; hidx < shape_seg.h; ++hidx) {
      for (int widx = 0; widx < shape_seg.w; ++widx) {
        int h_abs = hidx + position.h_start;
        int w_abs = widx + position.w_start;
        int y_offset = y_start;
        int x_offset = x_start;
        y_offset += hidx * y_h_offset + widx * y_w_offset;
        x_offset += hidx * x_h_offset + widx * x_w_offset;

        const int hstart = half_h_mask - h_abs > 0 ? half_h_mask - h_abs : 0;
        const int hend = x_full.h + half_h_mask - h_abs < h_mask
                             ? x_full.h + half_h_mask - h_abs
                             : h_mask;
        const int wstart = half_w_mask - w_abs > 0 ? half_w_mask - w_abs : 0;
        const int wend = x_full.w + half_w_mask - w_abs < w_mask
                             ? x_full.w + half_w_mask - w_abs
                             : w_mask;
        // (h,                      w                  ) with mask-indexed
        // (h + hidx - half_h_mask, w + widx - half_w_mask) with feature-indexed
        y_offset += ((hstart + h_abs - half_h_mask) * x_full.w + wstart +
                     w_abs - half_w_mask) *
                    y_c_offset;
        x_offset += (hstart * w_mask + wstart) * x_c_offset;
        int count = wend - wstart;
        __memcpy(y_nram + y_offset, x_nram + x_offset, count * sizeof(T),
                 NRAM2NRAM, y_c_offset * x_full.w * sizeof(T),
                 x_c_offset * w_mask * sizeof(T), hend - hstart - 1);
      }
    }
    y_start += y_n_offset;
    x_start += x_n_offset;
  }
  storeDataFromNramToDram(y_dram, y_nram, position, y_full);
}

template <typename T>
__mlu_func__ void psamaskDistributeForward(
    const T *x_dram, T *y_dram, const PositionInCore &position,
    const Shape &x_full, const Shape &y_full, const Shape &shape_seg,
    const int h_mask, const int w_mask, const int half_h_mask,
    const int half_w_mask) {
  T *x_nram = (T *)buf;
  T *y_nram_temp =
      x_nram + CEIL_ALIGN(shape_seg.n * shape_seg.h * shape_seg.w * x_full.c,
                          COMPUTE_COUNT_ALIGN / sizeof(T));
  loadDataFromDramToNram(x_nram, x_dram, position, x_full);

  // fill zeros to output
  int align_c = CEIL_ALIGN(y_full.c, COMPUTE_COUNT_ALIGN / sizeof(T));
  int align_hw =
      CEIL_ALIGN(shape_seg.h * shape_seg.w, COMPUTE_COUNT_ALIGN / sizeof(T));
  int elem_count =
      CEIL_ALIGN(shape_seg.n * align_c * align_hw, NFU_ALIGN_SIZE / sizeof(T));
157
  __bang_write_value(y_nram_temp, elem_count, (T)0);
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

  int y_n_offset = align_hw * align_c;
  int y_h_offset = shape_seg.w * align_c;
  int y_w_offset = align_c;
  int y_c_offset = 1;
  int x_n_offset = shape_seg.h * shape_seg.w * x_full.c;
  int x_h_offset = shape_seg.w * x_full.c;
  int x_w_offset = x_full.c;
  int x_c_offset = 1;
  int h_feature = y_full.h;
  int w_feature = y_full.w;

  int y_start = 0;
  int x_start = 0;
  for (int nidx = 0; nidx < shape_seg.n; ++nidx) {
    for (int hidx = 0; hidx < shape_seg.h; ++hidx) {
      for (int widx = 0; widx < shape_seg.w; ++widx) {
        int h_abs = hidx + position.h_start;
        int w_abs = widx + position.w_start;
        int y_offset = y_start;
        int x_offset = x_start;
        y_offset += hidx * y_h_offset + widx * y_w_offset;
        x_offset += hidx * x_h_offset + widx * x_w_offset;
        const int hstart = half_h_mask - h_abs > 0 ? half_h_mask - h_abs : 0;
        const int hend = h_feature + half_h_mask - h_abs < h_mask
                             ? h_feature + half_h_mask - h_abs
                             : h_mask;
        const int wstart = half_w_mask - w_abs > 0 ? half_w_mask - w_abs : 0;
        const int wend = w_feature + half_w_mask - w_abs < w_mask
                             ? w_feature + half_w_mask - w_abs
                             : w_mask;
        // (h,                      w                     ) with mask-indexed
        // (h + hidx - half_h_mask, w + widx - half_w_mask) with feature-indexed
        y_offset += ((hstart + h_abs - half_h_mask) * x_full.w + wstart +
                     w_abs - half_w_mask) *
                    y_c_offset;
        x_offset += (hstart * w_mask + wstart) * x_c_offset;
        int count = wend - wstart;
        __memcpy(y_nram_temp + y_offset, x_nram + x_offset, count * sizeof(T),
                 NRAM2NRAM, y_c_offset * w_feature * sizeof(T),
                 x_c_offset * w_mask * sizeof(T), hend - hstart - 1);
      }
    }
    y_start += y_n_offset;
    x_start += x_n_offset;
  }
  // transpose y
  T *y_nram = y_nram_temp + shape_seg.n * align_hw * align_c;
  Shape y_seg{shape_seg.n, shape_seg.h, shape_seg.w, y_full.c};
  transposeData(y_nram, y_nram_temp, y_seg);
  swap(align_c, align_hw);
  // store y from nram to dram
  int y_n_offset_full = y_full.h * y_full.w * y_full.c;
  int y_w_offset_full = y_full.c;
  int y_c_offset_full = 1;

  int y_dram_start =
      position.n_start * y_n_offset_full +
      (position.h_start * y_full.w + position.w_start) * y_c_offset_full;
  int y_nram_start = 0;
  for (int nidx = 0; nidx < shape_seg.n; ++nidx) {
    int y_dram_offset = y_dram_start + nidx * y_n_offset_full;
    int y_nram_offset = y_nram_start + nidx * align_hw * align_c;
    __memcpy(y_dram + y_dram_offset, y_nram + y_nram_offset,
             shape_seg.h * shape_seg.w * sizeof(T), NRAM2GDRAM,
             y_w_offset_full * sizeof(T), align_c * sizeof(T),
             h_feature * w_feature - 1);
  }
}

template <typename T>
__mlu_func__ void psamaskCollectBackward(
    const T *dy_dram, T *dx_dram, const PositionInCore &position,
    const Shape &dy_full, const Shape &dx_full, const Shape &shape_seg,
    const int h_mask, const int w_mask, const int half_h_mask,
    const int half_w_mask) {
  T *dy_nram = (T *)buf;
  T *dx_nram =
      dy_nram + CEIL_ALIGN(shape_seg.n * shape_seg.h * shape_seg.w * dy_full.c,
                           COMPUTE_COUNT_ALIGN / sizeof(T));
  loadDataFromDramToNram(dy_nram, dy_dram, position, dy_full);

  // fill zeros to output
  int elem_count =
      CEIL_ALIGN(shape_seg.n * shape_seg.h * shape_seg.w * shape_seg.c,
                 NFU_ALIGN_SIZE / sizeof(T));
244
  __bang_write_value(dx_nram, elem_count, (T)0);
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

  int dy_n_offset = shape_seg.h * shape_seg.w * dy_full.c;
  int dy_h_offset = shape_seg.w * dy_full.c;
  int dy_w_offset = dy_full.c;
  int dy_c_offset = 1;
  int dx_n_offset = shape_seg.h * shape_seg.w * dx_full.c;
  int dx_h_offset = shape_seg.w * dx_full.c;
  int dx_w_offset = dx_full.c;
  int dx_c_offset = 1;
  int h_feature = dy_full.h;
  int w_feature = dy_full.w;

  int dy_start = 0;
  int dx_start = 0;
  for (int nidx = 0; nidx < shape_seg.n; ++nidx) {
    for (int hidx = 0; hidx < shape_seg.h; ++hidx) {
      for (int widx = 0; widx < shape_seg.w; ++widx) {
        int h_abs = hidx + position.h_start;
        int w_abs = widx + position.w_start;
        int dy_offset = dy_start;
        int dx_offset = dx_start;
        dy_offset += hidx * dy_h_offset + widx * dy_w_offset;
        dx_offset += hidx * dx_h_offset + widx * dx_w_offset;

        const int hstart = half_h_mask - h_abs > 0 ? half_h_mask - h_abs : 0;
        const int hend = h_feature + half_h_mask - h_abs < h_mask
                             ? h_feature + half_h_mask - h_abs
                             : h_mask;
        const int wstart = half_w_mask - w_abs > 0 ? half_w_mask - w_abs : 0;
        const int wend = w_feature + half_w_mask - w_abs < w_mask
                             ? w_feature + half_w_mask - w_abs
                             : w_mask;
        // (h,                       w                      ) with mask-indexed
        // (h + h_abs - half_h_mask, w + w_abs - half_w_mask) with
        // feature-indexed
        dy_offset += ((hstart + h_abs - half_h_mask) * w_feature + wstart +
                      w_abs - half_w_mask) *
                     dy_c_offset;
        dx_offset += (hstart * w_mask + wstart) * dx_c_offset;
        int count = wend - wstart;
        __memcpy(dx_nram + dx_offset, dy_nram + dy_offset, count * sizeof(T),
                 NRAM2NRAM, dx_c_offset * w_mask * sizeof(T),
                 dy_c_offset * w_feature * sizeof(T), hend - hstart - 1);
      }
    }
    dy_start += dy_n_offset;
    dx_start += dx_n_offset;
  }
  storeDataFromNramToDram(dx_dram, dx_nram, position, dx_full);
}

template <typename T>
__mlu_func__ void psamaskDistributeBackward(
    const T *dy_dram, T *dx_dram, const PositionInCore &position,
    const Shape &dy_full, const Shape &dx_full, const Shape &shape_seg,
    const int h_mask, const int w_mask, const int half_h_mask,
    const int half_w_mask) {
  // load dy from dram to nram
  T *dy_nram_temp = (T *)buf;
  int dy_n_offset_full = dy_full.h * dy_full.w * dy_full.c;
  int dy_c_offset_full = 1;
  int h_feature = dy_full.h;
  int w_feature = dy_full.w;
  int align_c =
      CEIL_ALIGN(shape_seg.h * shape_seg.w, COMPUTE_COUNT_ALIGN / sizeof(T));
  int align_hw =
      CEIL_ALIGN(h_feature * w_feature, COMPUTE_COUNT_ALIGN / sizeof(T));

  int dy_dram_start =
      position.n_start * dy_n_offset_full +
      (position.h_start * w_feature + position.w_start) * dy_c_offset_full;
  int dy_nram_start = 0;
  for (int i = 0; i < shape_seg.n; ++i) {
    int dy_nram_offset = dy_nram_start + i * (align_hw * align_c);
    int dy_dram_offset = dy_dram_start + i * dy_n_offset_full;
    __memcpy(dy_nram_temp + dy_nram_offset, dy_dram + dy_dram_offset,
             shape_seg.h * shape_seg.w * sizeof(T), GDRAM2NRAM,
             align_c * sizeof(T), dy_full.c * sizeof(T),
             h_feature * w_feature - 1);
  }
  T *dy_nram = dy_nram_temp + shape_seg.n * align_hw * align_c;
  Shape dy_seg{shape_seg.n, h_feature, w_feature, shape_seg.h * shape_seg.w};
  transposeData(dy_nram, dy_nram_temp, dy_seg);
  swap(align_c, align_hw);

  // fill zeros to dx
  T *dx_nram = dy_nram + shape_seg.n * align_hw * align_c;
  int dx_size = shape_seg.n * shape_seg.h * shape_seg.w * dx_full.c;
333
334
  __bang_write_value(dx_nram, CEIL_ALIGN(dx_size, NFU_ALIGN_SIZE / sizeof(T)),
                     (T)0);
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

  int dy_n_offset_seg = align_hw * align_c;
  int dy_h_offset_seg = shape_seg.w * align_c;
  int dy_w_offset_seg = align_c;
  int dy_c_offset_seg = 1;
  int dx_n_offset_seg = shape_seg.h * shape_seg.w * shape_seg.c;
  int dx_h_offset_seg = shape_seg.w * shape_seg.c;
  int dx_w_offset_seg = shape_seg.c;
  int dx_c_offset_seg = 1;

  int dy_start = 0;
  int dx_start = 0;
  for (int nidx = 0; nidx < shape_seg.n; ++nidx) {
    for (int hidx = 0; hidx < shape_seg.h; ++hidx) {
      for (int widx = 0; widx < shape_seg.w; ++widx) {
        int h_abs = hidx + position.h_start;
        int w_abs = widx + position.w_start;
        int dy_offset = dy_start;
        int dx_offset = dx_start;
        dy_offset += hidx * dy_h_offset_seg + widx * dy_w_offset_seg;
        dx_offset += hidx * dx_h_offset_seg + widx * dx_w_offset_seg;
        const int hstart = half_h_mask - h_abs > 0 ? half_h_mask - h_abs : 0;
        const int hend = h_feature + half_h_mask - h_abs < h_mask
                             ? h_feature + half_h_mask - h_abs
                             : h_mask;
        const int wstart = half_w_mask - w_abs > 0 ? half_w_mask - w_abs : 0;
        const int wend = w_feature + half_w_mask - w_abs < w_mask
                             ? w_feature + half_w_mask - w_abs
                             : w_mask;
        // (h,                       w                      ) with mask-indexed
        // (h + h_abs - half_h_mask, w + w_abs - half_w_mask) with
        // feature-indexed
        dy_offset += ((hstart + h_abs - half_h_mask) * w_feature + wstart +
                      w_abs - half_w_mask) *
                     dy_c_offset_seg;
        dx_offset += (hstart * w_mask + wstart) * dx_c_offset_seg;
        int count = wend - wstart;
        __memcpy(dx_nram + dx_offset, dy_nram + dy_offset, count * sizeof(T),
                 NRAM2NRAM, w_mask * dx_c_offset_seg * sizeof(T),
                 w_feature * dy_c_offset_seg * sizeof(T), hend - hstart - 1);
      }
    }
    dy_start += dy_n_offset_seg;
    dx_start += dx_n_offset_seg;
  }
  storeDataFromNramToDram(dx_dram, dx_nram, position, dx_full);
}

template <typename T>
__mlu_func__ void psamaskBase(const T *input_dram, T *output_dram,
                              const Shape &input_full, const Shape &output_full,
                              LimitParam &limit, const PsamaskType psa_type,
                              const DimPartitionType core_partition,
                              const DimPartitionType cluster_partition,
                              const bool is_forward, const int h_mask,
                              const int w_mask, const int half_h_mask,
                              const int half_w_mask, const int n_per_core,
                              const int h_per_core, const int n_per_cluster,
                              const int h_per_cluster) {
  PositionInCore position_full;
  PositionInCore position_seg;
  position_full.w_start = 0;
  position_full.w_end = output_full.w;
  int n_num_in_cluster = n_per_cluster;
  int h_num_in_cluster = h_per_cluster;

  switch (cluster_partition) {
    case PARTITION_N: {
      position_full.h_start = 0;
      position_full.h_end = input_full.h;
      position_full.n_start = taskIdY * n_per_cluster;
      int cluster_need = (input_full.n + n_per_cluster - 1) / n_per_cluster;
      if (taskIdY >= cluster_need) return;
      int n_remainder = input_full.n - (cluster_need - 1) * n_per_cluster;
      n_num_in_cluster =
          (taskIdY == cluster_need - 1) ? n_remainder : n_per_cluster;
      position_full.n_end = position_full.n_start + n_num_in_cluster;
    }; break;
    case PARTITION_H: {
      position_full.n_start = 0;
      position_full.n_end = input_full.n;
      position_full.h_start = taskIdY * h_per_cluster;
      int cluster_need = (input_full.h + h_per_cluster - 1) / h_per_cluster;
      if (taskIdY >= cluster_need) return;
      int h_remainder = input_full.h - (cluster_need - 1) * h_per_cluster;
      h_num_in_cluster =
          (taskIdY == cluster_need - 1) ? h_remainder : h_per_cluster;
      position_full.h_end = position_full.h_start + h_num_in_cluster;
    }; break;
  }
  switch (core_partition) {
    case PARTITION_N: {
      position_full.n_start += taskIdX * n_per_core;
      int core_need = (n_num_in_cluster + n_per_core - 1) / n_per_core;
      if (taskIdX >= core_need) return;
      int n_remainder = n_num_in_cluster - (core_need - 1) * n_per_core;
      position_full.n_end =
          position_full.n_start +
          ((taskIdX == core_need - 1) ? n_remainder : n_per_core);
    }; break;
    case PARTITION_H: {
      position_full.h_start += taskIdX * h_per_core;
      int core_need = (h_num_in_cluster + h_per_core - 1) / h_per_core;
      if (taskIdX >= core_need) return;
      int h_remainder = h_num_in_cluster - (core_need - 1) * h_per_core;
      position_full.h_end =
          position_full.h_start +
          ((taskIdX == core_need - 1) ? h_remainder : h_per_core);
    }; break;
  }
  // the count of n ,h and w need to be processed in the current core
  int shape_core_n = position_full.n_end - position_full.n_start;
  int shape_core_h = position_full.h_end - position_full.h_start;
  int shape_core_w = input_full.w;

  limit.n = limit.n < shape_core_n ? limit.n : shape_core_n;
  limit.h = limit.h < shape_core_h ? limit.h : shape_core_h;
  limit.w = limit.w < shape_core_w ? limit.w : shape_core_w;

  // load the data to nram according to the limit
  for (int nidx = position_full.n_start; nidx < position_full.n_end;
       nidx += limit.n) {
    position_seg.n_start = nidx;
    position_seg.n_end =
        position_seg.n_start + (position_full.n_end - nidx < limit.n
                                    ? position_full.n_end - nidx
                                    : limit.n);
    for (int hidx = position_full.h_start; hidx < position_full.h_end;
         hidx += limit.h) {
      position_seg.h_start = hidx;
      position_seg.h_end =
          position_seg.h_start + (position_full.h_end - hidx < limit.h
                                      ? position_full.h_end - hidx
                                      : limit.h);
      for (int widx = position_full.w_start; widx < position_full.w_end;
           widx += limit.w) {
        position_seg.w_start = widx;
        position_seg.w_end =
            position_seg.w_start + (position_full.w_end - widx < limit.w
                                        ? position_full.w_end - widx
                                        : limit.w);

        // record the segment of output except the size of channel
        // channel segments of output and input are the same
        Shape shape_seg;
        shape_seg.n = position_seg.n_end - position_seg.n_start;
        shape_seg.h = position_seg.h_end - position_seg.h_start;
        shape_seg.w = position_seg.w_end - position_seg.w_start;
        shape_seg.c = output_full.c;

        switch (psa_type) {
          case COLLECT: {
            if (is_forward) {
              psamaskCollectForward(input_dram, output_dram, position_seg,
                                    input_full, output_full, shape_seg, h_mask,
                                    w_mask, half_h_mask, half_w_mask);
            } else {
              psamaskCollectBackward(input_dram, output_dram, position_seg,
                                     input_full, output_full, shape_seg, h_mask,
                                     w_mask, half_h_mask, half_w_mask);
            }
          } break;
          case DISTRIBUTE: {
            if (is_forward) {
              psamaskDistributeForward(input_dram, output_dram, position_seg,
                                       input_full, output_full, shape_seg,
                                       h_mask, w_mask, half_h_mask,
                                       half_w_mask);
            } else {
              psamaskDistributeBackward(input_dram, output_dram, position_seg,
                                        input_full, output_full, shape_seg,
                                        h_mask, w_mask, half_h_mask,
                                        half_w_mask);
            }
          } break;
        }
      }
    }
  }
}

template <typename T>
__mlu_global__ void MLUUnion1KernelPsamaskForward(
    const T *x, T *y, const PsamaskType psa_type,
    const DimPartitionType core_partition,
    const DimPartitionType cluster_partition, const int batch,
    const int h_feature, const int w_feature, const int h_mask,
    const int w_mask, const int x_c, const int y_c, const int half_h_mask,
    const int half_w_mask, const int n_per_core, const int h_per_core,
    const int n_per_cluster, const int h_per_cluster, const int limit_n_seg,
    const int limit_h_seg, const int limit_w_seg) {
  if (coreId == 0x80) {
    return;
  }
  Shape x_full, y_full;
  x_full.n = batch;
  x_full.h = h_feature;
  x_full.w = w_feature;
  x_full.c = x_c;
  y_full.n = batch;
  y_full.h = h_feature;
  y_full.w = w_feature;
  y_full.c = y_c;

  LimitParam limit;
  limit.n = limit_n_seg;
  limit.h = limit_h_seg;
  limit.w = limit_w_seg;

  psamaskBase(x, y, x_full, y_full, limit, psa_type, core_partition,
              cluster_partition, true, h_mask, w_mask, half_h_mask, half_w_mask,
              n_per_core, h_per_core, n_per_cluster, h_per_cluster);
}

template <typename T>
__mlu_global__ void MLUUnion1KernelPsamaskBackward(
    const T *dy, T *dx, const PsamaskType psa_type,
    const DimPartitionType core_partition,
    const DimPartitionType cluster_partition, const int batch,
    const int h_feature, const int w_feature, const int h_mask,
    const int w_mask, const int dx_c, const int dy_c, const int half_h_mask,
    const int half_w_mask, const int n_per_core, const int h_per_core,
    const int n_per_cluster, const int h_per_cluster, const int limit_n_seg,
    const int limit_h_seg, const int limit_w_seg) {
  if (coreId == 0x80) {
    return;
  }
  Shape dy_full, dx_full;
  dx_full.n = batch;
  dx_full.h = h_feature;
  dx_full.w = w_feature;
  dx_full.c = dx_c;
  dy_full.n = batch;
  dy_full.h = h_feature;
  dy_full.w = w_feature;
  dy_full.c = dy_c;

  LimitParam limit;
  limit.n = limit_n_seg;
  limit.h = limit_h_seg;
  limit.w = limit_w_seg;

  psamaskBase(dy, dx, dy_full, dx_full, limit, psa_type, core_partition,
              cluster_partition, false, h_mask, w_mask, half_h_mask,
              half_w_mask, n_per_core, h_per_core, n_per_cluster,
              h_per_cluster);
}

void KernelPsamaskForward(
    cnrtDim3_t k_dim, cnrtFunctionType_t k_type, cnrtQueue_t queue,
    const void *x, void *y, const PsamaskType psa_type,
    const DimPartitionType core_partition,
    const DimPartitionType cluster_partition, const int batch,
    const int h_feature, const int w_feature, const int h_mask,
    const int w_mask, const int x_c, const int y_c, const int half_h_mask,
    const int half_w_mask, const int n_per_core, const int h_per_core,
    const int n_per_cluster, const int h_per_cluster, const int limit_n_seg,
    const int limit_h_seg, const int limit_w_seg) {
  MLUUnion1KernelPsamaskForward<<<k_dim, k_type, queue>>>(
      static_cast<const float *>(x), static_cast<float *>(y), psa_type,
      core_partition, cluster_partition, batch, h_feature, w_feature, h_mask,
      w_mask, x_c, y_c, half_h_mask, half_w_mask, n_per_core, h_per_core,
      n_per_cluster, h_per_cluster, limit_n_seg, limit_h_seg, limit_w_seg);
}

void KernelPsamaskBackward(
    cnrtDim3_t k_dim, cnrtFunctionType_t k_type, cnrtQueue_t queue,
    const void *dy, void *dx, const PsamaskType psa_type,
    const DimPartitionType core_partition,
    const DimPartitionType cluster_partition, const int batch,
    const int h_feature, const int w_feature, const int h_mask,
    const int w_mask, const int dx_c, const int dy_c, const int half_h_mask,
    const int half_w_mask, const int n_per_core, const int h_per_core,
    const int n_per_cluster, const int h_per_cluster, const int limit_n_seg,
    const int limit_h_seg, const int limit_w_seg) {
  MLUUnion1KernelPsamaskBackward<<<k_dim, k_type, queue>>>(
      static_cast<const float *>(dy), static_cast<float *>(dx), psa_type,
      core_partition, cluster_partition, batch, h_feature, w_feature, h_mask,
      w_mask, dx_c, dy_c, half_h_mask, half_w_mask, n_per_core, h_per_core,
      n_per_cluster, h_per_cluster, limit_n_seg, limit_h_seg, limit_w_seg);
}