DeepSeek-V3.2: Add Adaptive MHA Attention Pathway for Short-Sequence Prefill (#11892)
This commit is contained in:
@@ -242,6 +242,30 @@ class Indexer(CustomOp):
|
||||
|
||||
return query, key, weights
|
||||
|
||||
def _get_k_bf16(
|
||||
self,
|
||||
x: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
enable_dual_stream: bool,
|
||||
):
|
||||
# Compute only key, skip query and weights (weights is discarded if fused)
|
||||
if self.fuse_wk_and_weights_proj:
|
||||
key, _ = self.fused_wk_and_weights_proj(x)[0].split(
|
||||
[self.head_dim, self.n_heads], dim=-1
|
||||
)
|
||||
else:
|
||||
key, _ = self.wk(x)
|
||||
key = self.k_norm(key)
|
||||
k_rope, _ = torch.split(
|
||||
key, [self.rope_head_dim, self.head_dim - self.rope_head_dim], dim=-1
|
||||
)
|
||||
|
||||
_, k_rope = self.rotary_emb(positions, k_rope, k_rope)
|
||||
key[..., : self.rope_head_dim] = k_rope
|
||||
key = rotate_activation(key)
|
||||
|
||||
return key
|
||||
|
||||
def _get_topk_paged(
|
||||
self,
|
||||
forward_batch: ForwardBatch,
|
||||
@@ -375,6 +399,45 @@ class Indexer(CustomOp):
|
||||
topk_result[:offset] = raw_topk_result
|
||||
return topk_result
|
||||
|
||||
def _forward_cuda_k_only(
|
||||
self,
|
||||
x: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
forward_batch: ForwardBatch,
|
||||
layer_id: int,
|
||||
act_quant,
|
||||
enable_dual_stream: bool,
|
||||
metadata: BaseIndexerMetadata,
|
||||
return_indices: bool = True,
|
||||
) -> Optional[torch.Tensor]:
|
||||
# Fast path: only compute and store k cache, skip all q and weights ops
|
||||
key = self._get_k_bf16(x, positions, enable_dual_stream)
|
||||
k_fp8, k_scale = act_quant(key, self.block_size, self.scale_fmt)
|
||||
|
||||
if not forward_batch.out_cache_loc.is_contiguous():
|
||||
forward_batch.out_cache_loc = forward_batch.out_cache_loc.contiguous()
|
||||
forward_batch.token_to_kv_pool.set_index_k_and_scale_buffer(
|
||||
layer_id=layer_id,
|
||||
loc=forward_batch.out_cache_loc,
|
||||
index_k=k_fp8,
|
||||
index_k_scale=k_scale,
|
||||
)
|
||||
|
||||
# MHA doesn't need topk_indices
|
||||
if not return_indices:
|
||||
return None
|
||||
|
||||
# MLA: use dummy logits with topk kernel's fast path to generate indices
|
||||
# When length <= 2048, naive_topk_cuda directly generates [0,1,...,length-1,-1,...]
|
||||
seq_lens_expanded = metadata.get_seqlens_expanded()
|
||||
dummy_logits = torch.zeros(
|
||||
seq_lens_expanded.shape[0],
|
||||
self.index_topk,
|
||||
dtype=torch.float32,
|
||||
device=x.device,
|
||||
)
|
||||
return metadata.topk_transform(dummy_logits, self.index_topk)
|
||||
|
||||
def forward_indexer(
|
||||
self,
|
||||
q_fp8: torch.Tensor,
|
||||
@@ -465,6 +528,7 @@ class Indexer(CustomOp):
|
||||
positions: torch.Tensor,
|
||||
forward_batch: ForwardBatch,
|
||||
layer_id: int,
|
||||
return_indices: bool = True,
|
||||
) -> Optional[torch.Tensor]:
|
||||
if is_hip():
|
||||
from sglang.srt.layers.attention.nsa.tilelang_kernel import act_quant
|
||||
@@ -490,6 +554,26 @@ class Indexer(CustomOp):
|
||||
if metadata is None:
|
||||
return None
|
||||
|
||||
# Determine if should skip topk based on sequence length
|
||||
should_skip = False
|
||||
if not forward_batch.forward_mode.is_decode_or_idle():
|
||||
if forward_batch.seq_lens_cpu is not None:
|
||||
max_kv_len = forward_batch.seq_lens_cpu.max().item()
|
||||
should_skip = max_kv_len <= self.index_topk
|
||||
|
||||
# Optimization: fast path when skipping topk computation
|
||||
if should_skip:
|
||||
return self._forward_cuda_k_only(
|
||||
x,
|
||||
positions,
|
||||
forward_batch,
|
||||
layer_id,
|
||||
act_quant,
|
||||
enable_dual_stream,
|
||||
metadata,
|
||||
return_indices,
|
||||
)
|
||||
|
||||
query, key, weights = self._get_q_k_bf16(
|
||||
q_lora, x, positions, enable_dual_stream
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user