[Qwen3.8-Flash-Next] Fuse PLE residual and QSA output gate (#55309)
Signed-off-by: Canlin <canlinguosdu@gmail.com> Signed-off-by: zjy0516 <riverclouds.zhu@qq.com> Co-authored-by: zjy0516 <riverclouds.zhu@qq.com> Co-authored-by: Kimi Code <noreply@moonshot.cn>
This commit is contained in:
co-authored by
zjy0516
Kimi Code
parent
c612e2bff0
commit
3f55ad2f07
@@ -322,6 +322,7 @@ def _ple_conv_kernel(
|
||||
state_ptr,
|
||||
w_ptr,
|
||||
residual_ptr,
|
||||
outer_residual_ptr,
|
||||
state_idx_ptr,
|
||||
qsl_ptr,
|
||||
num_acc_ptr,
|
||||
@@ -442,11 +443,21 @@ def _ple_conv_kernel(
|
||||
mask=c_mask,
|
||||
other=0.0,
|
||||
)
|
||||
# Preserve the original eager operation boundaries: short convolution is
|
||||
# first accumulated into the BF16/FP16 PLE output, then the outer residual
|
||||
# is added to that rounded value.
|
||||
ple_output = (residual + conv_output).to(residual_ptr.dtype.element_ty)
|
||||
outer_residual = tl.load(
|
||||
outer_residual_ptr + output_t * C + c_offs,
|
||||
mask=c_mask,
|
||||
other=0.0,
|
||||
)
|
||||
ple_output = outer_residual.to(tl.float32) + ple_output.to(tl.float32)
|
||||
if launch_pdl:
|
||||
tl.extra.cuda.gdc_launch_dependents()
|
||||
tl.store(
|
||||
residual_ptr + output_t * C + c_offs,
|
||||
residual + conv_output,
|
||||
ple_output,
|
||||
mask=c_mask,
|
||||
)
|
||||
|
||||
@@ -564,6 +575,7 @@ def ple_conv(
|
||||
conv_state: torch.Tensor,
|
||||
conv_weights: torch.Tensor,
|
||||
state_indices: torch.Tensor,
|
||||
outer_residual: torch.Tensor,
|
||||
*,
|
||||
mode: Literal["decode", "spec", "prefill"],
|
||||
dilation: int,
|
||||
@@ -573,7 +585,7 @@ def ple_conv(
|
||||
spec_query_len: int = 1,
|
||||
token_indices: torch.Tensor | None = None,
|
||||
) -> None:
|
||||
"""Add short-convolution output to ``residual`` and update its state."""
|
||||
"""Add short convolution and the outer residual; update state."""
|
||||
BLOCK_C = 512
|
||||
kernel_spec_query_len = spec_query_len if mode == "spec" else 1
|
||||
T, C = inputs.shape
|
||||
@@ -624,6 +636,7 @@ def ple_conv(
|
||||
conv_state,
|
||||
conv_weights,
|
||||
residual,
|
||||
outer_residual,
|
||||
state_indices,
|
||||
query_start_loc,
|
||||
num_accepted_tokens,
|
||||
|
||||
@@ -24,6 +24,7 @@ def _qsa_sparse_paged_gqa_splitk_kernel(
|
||||
partial_output_ptr,
|
||||
partial_lse_ptr,
|
||||
output_ptr,
|
||||
output_gate_ptr,
|
||||
stride_q_row,
|
||||
stride_q_head,
|
||||
stride_k_block,
|
||||
@@ -36,6 +37,8 @@ def _qsa_sparse_paged_gqa_splitk_kernel(
|
||||
stride_table_req,
|
||||
stride_output_row,
|
||||
stride_output_head,
|
||||
stride_output_gate_row,
|
||||
stride_output_gate_head,
|
||||
num_rows,
|
||||
num_cache_blocks,
|
||||
num_requests,
|
||||
@@ -151,6 +154,18 @@ def _qsa_sparse_paged_gqa_splitk_kernel(
|
||||
)
|
||||
output_mask = head_offsets[:, None] < GROUP_SIZE
|
||||
if NUM_SPLITS == 1:
|
||||
# Preserve the unfused path's BF16 attention-output rounding before
|
||||
# applying the gate in FP32.
|
||||
normalized_output = normalized_output.to(output_ptr.dtype.element_ty)
|
||||
output_gate = tl.load(
|
||||
output_gate_ptr
|
||||
+ row * stride_output_gate_row
|
||||
+ (first_head + head_offsets[:, None]) * stride_output_gate_head
|
||||
+ dim_offsets[None, :],
|
||||
mask=output_mask,
|
||||
other=0.0,
|
||||
).to(tl.float32)
|
||||
normalized_output = normalized_output.to(tl.float32) * tl.sigmoid(output_gate)
|
||||
tl.store(
|
||||
output_ptr
|
||||
+ row * stride_output_row
|
||||
@@ -186,8 +201,11 @@ def _qsa_merge_splitk_kernel(
|
||||
partial_output_ptr,
|
||||
partial_lse_ptr,
|
||||
output_ptr,
|
||||
output_gate_ptr,
|
||||
stride_output_row,
|
||||
stride_output_head,
|
||||
stride_output_gate_row,
|
||||
stride_output_gate_head,
|
||||
num_rows,
|
||||
HEAD_DIM: tl.constexpr,
|
||||
NUM_QUERY_HEADS: tl.constexpr,
|
||||
@@ -219,6 +237,16 @@ def _qsa_merge_splitk_kernel(
|
||||
)
|
||||
merged = tl.sum(partial_output * weights[:, None], axis=0)
|
||||
merged = tl.where(denominator > 0, merged / denominator, 0.0)
|
||||
# Preserve the unfused path's BF16 attention-output rounding before
|
||||
# applying the gate in FP32.
|
||||
merged = merged.to(output_ptr.dtype.element_ty)
|
||||
output_gate = tl.load(
|
||||
output_gate_ptr
|
||||
+ row * stride_output_gate_row
|
||||
+ head * stride_output_gate_head
|
||||
+ dim_offsets
|
||||
).to(tl.float32)
|
||||
merged = merged.to(tl.float32) * tl.sigmoid(output_gate)
|
||||
tl.store(
|
||||
output_ptr + row * stride_output_row + head * stride_output_head + dim_offsets,
|
||||
merged,
|
||||
@@ -455,6 +483,8 @@ def qsa_sparse_paged_attention(
|
||||
token_to_req: torch.Tensor,
|
||||
use_prefill_config: bool,
|
||||
out: torch.Tensor | None = None,
|
||||
*,
|
||||
output_gate: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
"""Run sparse GQA directly over paged BF16 K/V caches.
|
||||
|
||||
@@ -496,6 +526,8 @@ def qsa_sparse_paged_attention(
|
||||
raise ValueError("QSA sparse output must match its query")
|
||||
assert out.dtype == q.dtype and out.device == q.device
|
||||
assert out.stride(2) == 1
|
||||
assert output_gate.is_contiguous()
|
||||
output_gate_view = output_gate.view_as(q)
|
||||
if not q.shape[0]:
|
||||
return out
|
||||
|
||||
@@ -533,6 +565,7 @@ def qsa_sparse_paged_attention(
|
||||
partial_output,
|
||||
partial_lse,
|
||||
out,
|
||||
output_gate_view,
|
||||
q.stride(0),
|
||||
q.stride(1),
|
||||
k_cache.stride(0),
|
||||
@@ -545,6 +578,8 @@ def qsa_sparse_paged_attention(
|
||||
block_table.stride(0),
|
||||
out.stride(0),
|
||||
out.stride(1),
|
||||
output_gate_view.stride(0),
|
||||
output_gate_view.stride(1),
|
||||
q.shape[0],
|
||||
k_cache.shape[0],
|
||||
block_table.shape[0],
|
||||
@@ -568,8 +603,11 @@ def qsa_sparse_paged_attention(
|
||||
partial_output,
|
||||
partial_lse,
|
||||
out,
|
||||
output_gate_view,
|
||||
out.stride(0),
|
||||
out.stride(1),
|
||||
output_gate_view.stride(0),
|
||||
output_gate_view.stride(1),
|
||||
q.shape[0],
|
||||
HEAD_DIM=q.shape[2],
|
||||
NUM_QUERY_HEADS=q.shape[1],
|
||||
@@ -632,6 +670,10 @@ def warmup_qsa_sparse_paged_attention(
|
||||
output_ptr = TritonWarmupTensor(
|
||||
torch.bfloat16, shape=(num_rows, num_query_heads, head_dim)
|
||||
)
|
||||
# The output gate is mandatory at runtime; warm the gated specialization.
|
||||
output_gate_ptr = TritonWarmupTensor(
|
||||
torch.bfloat16, shape=(num_rows, num_query_heads, head_dim)
|
||||
)
|
||||
head_stride = head_dim
|
||||
row_stride = num_query_heads * head_dim
|
||||
num_cache_blocks = triton_scalar_specialization_rep(kv_cache.shape[0])
|
||||
@@ -659,6 +701,7 @@ def warmup_qsa_sparse_paged_attention(
|
||||
partial_output_ptr,
|
||||
partial_lse_ptr,
|
||||
output_ptr,
|
||||
output_gate_ptr,
|
||||
row_stride,
|
||||
head_stride,
|
||||
key_cache.stride(0),
|
||||
@@ -671,6 +714,8 @@ def warmup_qsa_sparse_paged_attention(
|
||||
block_table.stride(0),
|
||||
row_stride,
|
||||
head_stride,
|
||||
row_stride,
|
||||
head_stride,
|
||||
num_rows,
|
||||
num_cache_blocks,
|
||||
num_requests,
|
||||
@@ -693,6 +738,9 @@ def warmup_qsa_sparse_paged_attention(
|
||||
partial_output_ptr,
|
||||
partial_lse_ptr,
|
||||
output_ptr,
|
||||
output_gate_ptr,
|
||||
row_stride,
|
||||
head_stride,
|
||||
row_stride,
|
||||
head_stride,
|
||||
num_rows,
|
||||
|
||||
Reference in New Issue
Block a user