Update Triton decode backend interface (#3292)

This commit is contained in:
Ke Bao
2025-02-04 23:26:04 +08:00
committed by GitHub
parent 2c1a695ff1
commit a07364ccc5
3 changed files with 129 additions and 77 deletions
@@ -49,11 +49,9 @@ def _fwd_kernel_stage1(
K_Buffer,
V_Buffer,
sm_scale,
Req_to_tokens,
B_req_idx,
B_Seqlen,
kv_indptr,
kv_indices,
Att_Out,
stride_req_to_tokens_b,
stride_qbs,
stride_qh,
stride_buf_kbs,
@@ -82,8 +80,9 @@ def _fwd_kernel_stage1(
offs_dv = tl.arange(0, BLOCK_DV)
mask_d = offs_d < Lk
mask_dv = offs_dv < Lv
cur_batch_seq_len = tl.load(B_Seqlen + cur_batch)
cur_batch_req_idx = tl.load(B_req_idx + cur_batch)
cur_batch_kv_start_idx = tl.load(kv_indptr + cur_batch)
cur_batch_seq_len = tl.load(kv_indptr + cur_batch + 1) - cur_batch_kv_start_idx
off_q = cur_batch * stride_qbs + cur_head * stride_qh + offs_d
q = tl.load(Q + off_q, mask=mask_d, other=0.0)
@@ -100,7 +99,7 @@ def _fwd_kernel_stage1(
for start_n in range(split_kv_start, split_kv_end, BLOCK_N):
offs_n = start_n + tl.arange(0, BLOCK_N)
kv_loc = tl.load(
Req_to_tokens + stride_req_to_tokens_b * cur_batch_req_idx + offs_n,
kv_indices + cur_batch_kv_start_idx + offs_n,
mask=offs_n < split_kv_end,
other=0,
)
@@ -173,9 +172,8 @@ def _decode_att_m_fwd(
k_buffer,
v_buffer,
att_out,
Req_to_tokens,
B_req_idx,
B_Seqlen,
kv_indptr,
kv_indices,
num_kv_splits,
sm_scale,
logit_cap,
@@ -188,7 +186,7 @@ def _decode_att_m_fwd(
Lk = k_buffer.shape[-1]
Lv = v_buffer.shape[-1]
batch, head_num = B_req_idx.shape[0], q.shape[1]
batch, head_num = kv_indptr.shape[0] - 1, q.shape[1]
grid = (batch, head_num, NUM_KV_SPLITS)
kv_group_num = q.shape[1] // k_buffer.shape[1]
@@ -208,11 +206,9 @@ def _decode_att_m_fwd(
k_buffer,
v_buffer,
sm_scale,
Req_to_tokens,
B_req_idx,
B_Seqlen,
kv_indptr,
kv_indices,
att_out,
Req_to_tokens.stride(0),
q.stride(0),
q.stride(1),
k_buffer.stride(0),
@@ -241,11 +237,9 @@ def _fwd_grouped_kernel_stage1(
K_Buffer,
V_Buffer,
sm_scale,
Req_to_tokens,
B_req_idx,
B_Seqlen,
kv_indptr,
kv_indices,
Att_Out,
stride_req_to_tokens_b,
stride_qbs,
stride_qh,
stride_buf_kbs,
@@ -284,8 +278,9 @@ def _fwd_grouped_kernel_stage1(
offs_dv = tl.arange(0, BLOCK_DV)
mask_d = offs_d < Lk
mask_dv = offs_dv < Lv
cur_batch_seq_len = tl.load(B_Seqlen + cur_batch)
cur_batch_req_idx = tl.load(B_req_idx + cur_batch)
cur_batch_kv_start_idx = tl.load(kv_indptr + cur_batch)
cur_batch_seq_len = tl.load(kv_indptr + cur_batch + 1) - cur_batch_kv_start_idx
offs_q = cur_batch * stride_qbs + cur_head[:, None] * stride_qh + offs_d[None, :]
q = tl.load(Q + offs_q, mask=(mask_h[:, None]) & (mask_d[None, :]), other=0.0)
@@ -312,7 +307,7 @@ def _fwd_grouped_kernel_stage1(
for start_n in range(split_kv_start, split_kv_end, BLOCK_N):
offs_n = start_n + tl.arange(0, BLOCK_N)
kv_loc = tl.load(
Req_to_tokens + stride_req_to_tokens_b * cur_batch_req_idx + offs_n,
kv_indices + cur_batch_kv_start_idx + offs_n,
mask=offs_n < split_kv_end,
other=0,
)
@@ -400,9 +395,8 @@ def _decode_grouped_att_m_fwd(
k_buffer,
v_buffer,
att_out,
Req_to_tokens,
B_req_idx,
B_Seqlen,
kv_indptr,
kv_indices,
num_kv_splits,
sm_scale,
logit_cap,
@@ -426,7 +420,7 @@ def _decode_grouped_att_m_fwd(
BLOCK_DPE = 0
BLOCK_DV = triton.next_power_of_2(Lv)
batch, head_num = B_req_idx.shape[0], q.shape[1]
batch, head_num = kv_indptr.shape[0] - 1, q.shape[1]
kv_group_num = q.shape[1] // k_buffer.shape[1]
BLOCK_H = 16
@@ -450,11 +444,9 @@ def _decode_grouped_att_m_fwd(
k_buffer,
v_buffer,
sm_scale,
Req_to_tokens,
B_req_idx,
B_Seqlen,
kv_indptr,
kv_indices,
att_out,
Req_to_tokens.stride(0),
q.stride(0),
q.stride(1),
k_buffer.stride(0),
@@ -485,7 +477,7 @@ def _decode_grouped_att_m_fwd(
def _fwd_kernel_stage2(
Mid_O,
O,
B_Seqlen,
kv_indptr,
stride_mid_ob,
stride_mid_oh,
stride_mid_os,
@@ -498,7 +490,9 @@ def _fwd_kernel_stage2(
cur_batch = tl.program_id(0)
cur_head = tl.program_id(1)
cur_batch_seq_len = tl.load(B_Seqlen + cur_batch)
cur_batch_seq_len = tl.load(kv_indptr + cur_batch + 1) - tl.load(
kv_indptr + cur_batch
)
offs_d = tl.arange(0, BLOCK_DV)
mask_d = offs_d < Lv
@@ -542,7 +536,7 @@ def _decode_softmax_reducev_fwd(
q,
o,
v_buffer,
b_seq_len,
kv_indptr,
num_kv_splits,
):
batch, head_num = q.shape[0], q.shape[1]
@@ -561,7 +555,7 @@ def _decode_softmax_reducev_fwd(
_fwd_kernel_stage2[grid](
logits,
o,
b_seq_len,
kv_indptr,
logits.stride(0),
logits.stride(1),
logits.stride(2),
@@ -581,9 +575,8 @@ def decode_attention_fwd_normal(
k_buffer,
v_buffer,
o,
req_to_token,
b_req_idx,
b_seq_len,
kv_indptr,
kv_indices,
attn_logits,
num_kv_splits,
sm_scale,
@@ -594,14 +587,13 @@ def decode_attention_fwd_normal(
k_buffer,
v_buffer,
attn_logits,
req_to_token,
b_req_idx,
b_seq_len,
kv_indptr,
kv_indices,
num_kv_splits,
sm_scale,
logit_cap,
)
_decode_softmax_reducev_fwd(attn_logits, q, o, v_buffer, b_seq_len, num_kv_splits)
_decode_softmax_reducev_fwd(attn_logits, q, o, v_buffer, kv_indptr, num_kv_splits)
def decode_attention_fwd_grouped(
@@ -609,9 +601,8 @@ def decode_attention_fwd_grouped(
k_buffer,
v_buffer,
o,
req_to_token,
b_req_idx,
b_seq_len,
kv_indptr,
kv_indices,
attn_logits,
num_kv_splits,
sm_scale,
@@ -622,14 +613,13 @@ def decode_attention_fwd_grouped(
k_buffer,
v_buffer,
attn_logits,
req_to_token,
b_req_idx,
b_seq_len,
kv_indptr,
kv_indices,
num_kv_splits,
sm_scale,
logit_cap,
)
_decode_softmax_reducev_fwd(attn_logits, q, o, v_buffer, b_seq_len, num_kv_splits)
_decode_softmax_reducev_fwd(attn_logits, q, o, v_buffer, kv_indptr, num_kv_splits)
def decode_attention_fwd(
@@ -637,9 +627,8 @@ def decode_attention_fwd(
k_buffer,
v_buffer,
o,
req_to_token,
b_req_idx,
b_seq_len,
kv_indptr,
kv_indices,
attn_logits,
num_kv_splits,
sm_scale,
@@ -655,9 +644,8 @@ def decode_attention_fwd(
k_buffer,
v_buffer,
o,
req_to_token,
b_req_idx,
b_seq_len,
kv_indptr,
kv_indices,
attn_logits,
num_kv_splits,
sm_scale,
@@ -670,9 +658,8 @@ def decode_attention_fwd(
k_buffer,
v_buffer,
o,
req_to_token,
b_req_idx,
b_seq_len,
kv_indptr,
kv_indices,
attn_logits,
num_kv_splits,
sm_scale,