Skip to content

Commit f7fee54

Browse files
authored
[Feature](ms_deform_attn_forward): Optimize IO eff for kernels. (Cambricon#1240)
1 parent f1baa98 commit f7fee54

1 file changed

Lines changed: 38 additions & 7 deletions

File tree

kernels/ms_deform_attn/ms_deform_attn_forward/msda_forward_fast_union1.mlu

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,15 +1016,18 @@ __mlu_func__ void forwardStageTwoLoop(
10161016
const int32_t output_stride_2, const int32_t num_heads,
10171017
const int32_t channels, const int32_t num_levels,
10181018
const int32_t num_points) {
1019+
__bang_write_value(value_ping_nram, 64, (T)0);
1020+
__bang_write_value(value_ping_nram, 1, (T)1);
1021+
__memcpy(wram_buffer, value_ping_nram, 64 * sizeof(T), NRAM2WRAM,
1022+
MAX_WRAM_SIZE / 64, 0, 63);
10191023
int32_t loop_num = (total_deal_n + max_deal_n - 1) / max_deal_n;
10201024
int32_t num_levels_points = num_levels * num_points;
10211025
int32_t sram_src_stride = total_deal_n * num_levels_points * sizeof(T);
10221026
T* value_nram[2] = {value_ping_nram, value_pong_nram};
10231027
int32_t* offset_zero_nram_stg2 =
10241028
offset_nram_stg2 + 4 * max_deal_n * num_levels_points;
1025-
for (int32_t i = 0; i < loop_num + 1; i++) {
1029+
for (int32_t i = 0; i < loop_num + 2; i++) {
10261030
int32_t compute_idx = i - 1;
1027-
int32_t compute_offset = compute_idx * max_deal_n;
10281031
int32_t load_n = std::min(total_deal_n - i * max_deal_n, max_deal_n);
10291032
int32_t compute_n =
10301033
std::min(total_deal_n - compute_idx * max_deal_n, max_deal_n);
@@ -1038,7 +1041,7 @@ __mlu_func__ void forwardStageTwoLoop(
10381041
T* v_load = value_nram[i % 2];
10391042
int8_t* cond_nram_stg2_reverse = (int8_t*)cond_nram_stg2 + gather_mask_size;
10401043

1041-
if (i > 0) {
1044+
if (i > 0 && i < loop_num + 1) {
10421045
int32_t copy_size_1 = compute_n * num_levels_points * sizeof(T);
10431046
int32_t sram_src_offset = compute_idx * max_deal_n * num_levels_points;
10441047
__memcpy_async(weight_polation_nram_stg2,
@@ -1067,16 +1070,46 @@ __mlu_func__ void forwardStageTwoLoop(
10671070

10681071
__sync_io_move_compute();
10691072

1073+
if (i > 1) {
1074+
int32_t output_idx = i - 2;
1075+
int32_t output_offset = output_idx * max_deal_n;
1076+
int32_t output_n =
1077+
std::min(total_deal_n - output_idx * max_deal_n, max_deal_n);
1078+
__memcpy_async(output_gdram + output_offset * output_stride_2, v_load,
1079+
channels * sizeof(T), NRAM2GDRAM, output_stride_2 * sizeof(T),
1080+
channels * sizeof(T), output_n - 1);
1081+
__sync_io_move_compute(true, false, false, false, true, false);
1082+
}
1083+
10701084
if (i < loop_num) {
10711085
gatherAsync<T>(v_load, zeros_nram, (unsigned int*)offset_zero_nram_stg2,
10721086
cond_nram_stg2_reverse, channels * sizeof(T), NRAM2NRAM,
10731087
channels * sizeof(T), load_point_num);
1088+
#if __BANG_ARCH__ == 592
1089+
__bang_gather_async(
1090+
v_load, data_value_gdram, (unsigned int*)offset_nram_stg2,
1091+
cond_nram_stg2, channels * sizeof(T), channels * sizeof(T),
1092+
load_point_num);
1093+
#else
10741094
gatherAsync<T>(v_load, data_value_gdram, (unsigned int*)offset_nram_stg2,
10751095
cond_nram_stg2, channels * sizeof(T), GDRAM2NRAM,
10761096
channels * sizeof(T), load_point_num);
1097+
#endif
10771098
}
10781099

1079-
if (i > 0) {
1100+
if (i > 0 && i < loop_num + 1) {
1101+
#if __BANG_ARCH__ == 592
1102+
__bang_cycle_mul(weight_polation_nram_stg2, weight_polation_nram_stg2,
1103+
weight_attn_nram_stg2, nq_nlp_4, nq_nlp);
1104+
__bang_expand_channel_mul(
1105+
(T*)v_compute, (T*)weight_polation_nram_stg2,
1106+
(T*)wram_buffer, (T*)v_compute,
1107+
compute_n * 4 * num_levels_points, channels);
1108+
__bang_sumpool(compute_buffer_nram, v_compute,
1109+
nq_nlp * channels, 1, 4, 1, 4, 1, 1);
1110+
__bang_sumpool(v_compute, compute_buffer_nram, channels, compute_n,
1111+
num_levels_points, 1, num_levels_points, 1, 1);
1112+
#else
10801113
__bang_transpose(compute_buffer_nram, v_compute, nq_nlp_4, channels);
10811114
__bang_cycle_mul(compute_buffer_nram, compute_buffer_nram,
10821115
weight_polation_nram_stg2, channels * nq_nlp_4,
@@ -1088,9 +1121,7 @@ __mlu_func__ void forwardStageTwoLoop(
10881121
__bang_transpose(compute_buffer_nram, v_compute, channels, nq_nlp);
10891122
__bang_sumpool(v_compute, compute_buffer_nram, channels, compute_n,
10901123
num_levels_points, 1, num_levels_points, 1, 1);
1091-
__memcpy(output_gdram + compute_offset * output_stride_2, v_compute,
1092-
channels * sizeof(T), NRAM2GDRAM, output_stride_2 * sizeof(T),
1093-
channels * sizeof(T), compute_n - 1);
1124+
#endif
10941125
}
10951126
__sync_io_move_compute();
10961127
}

0 commit comments

Comments
 (0)