[Deepseek V3.2] Only skip Indexer logits computation when is_extend_without_speculative (#12816)
Signed-off-by: Hao Lu <14827759+hlu1@users.noreply.github.com>
This commit is contained in:
@@ -410,6 +410,8 @@ class Indexer(CustomOp):
|
|||||||
metadata: BaseIndexerMetadata,
|
metadata: BaseIndexerMetadata,
|
||||||
return_indices: bool = True,
|
return_indices: bool = True,
|
||||||
) -> Optional[torch.Tensor]:
|
) -> Optional[torch.Tensor]:
|
||||||
|
assert forward_batch.forward_mode.is_extend_without_speculative()
|
||||||
|
|
||||||
# Fast path: only compute and store k cache, skip all q and weights ops
|
# Fast path: only compute and store k cache, skip all q and weights ops
|
||||||
key = self._get_k_bf16(x, positions, enable_dual_stream)
|
key = self._get_k_bf16(x, positions, enable_dual_stream)
|
||||||
k_fp8, k_scale = act_quant(key, self.block_size, self.scale_fmt)
|
k_fp8, k_scale = act_quant(key, self.block_size, self.scale_fmt)
|
||||||
@@ -555,14 +557,15 @@ class Indexer(CustomOp):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
# Determine if should skip topk based on sequence length
|
# Determine if should skip topk based on sequence length
|
||||||
should_skip = False
|
# We can only skip the logits computation if cuda graph is not involved
|
||||||
if not forward_batch.forward_mode.is_decode_or_idle():
|
skip_logits_computation = False
|
||||||
|
if forward_batch.forward_mode.is_extend_without_speculative():
|
||||||
if forward_batch.seq_lens_cpu is not None:
|
if forward_batch.seq_lens_cpu is not None:
|
||||||
max_kv_len = forward_batch.seq_lens_cpu.max().item()
|
max_kv_len = forward_batch.seq_lens_cpu.max().item()
|
||||||
should_skip = max_kv_len <= self.index_topk
|
skip_logits_computation = max_kv_len <= self.index_topk
|
||||||
|
|
||||||
# Optimization: fast path when skipping topk computation
|
# Optimization: fast path when skipping topk computation
|
||||||
if should_skip:
|
if skip_logits_computation:
|
||||||
return self._forward_cuda_k_only(
|
return self._forward_cuda_k_only(
|
||||||
x,
|
x,
|
||||||
positions,
|
positions,
|
||||||
|
|||||||
@@ -141,6 +141,13 @@ class ForwardMode(IntEnum):
|
|||||||
def is_split_prefill(self):
|
def is_split_prefill(self):
|
||||||
return self == ForwardMode.SPLIT_PREFILL
|
return self == ForwardMode.SPLIT_PREFILL
|
||||||
|
|
||||||
|
def is_extend_without_speculative(self):
|
||||||
|
return (
|
||||||
|
self.is_extend()
|
||||||
|
and not self.is_target_verify()
|
||||||
|
and not self.is_draft_extend()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@total_ordering
|
@total_ordering
|
||||||
class CaptureHiddenMode(IntEnum):
|
class CaptureHiddenMode(IntEnum):
|
||||||
|
|||||||
@@ -308,14 +308,6 @@ def _get_sum_extend_prefix_lens(forward_batch):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _is_extend_without_speculative(forward_batch):
|
|
||||||
return (
|
|
||||||
forward_batch.forward_mode.is_extend()
|
|
||||||
and not forward_batch.forward_mode.is_target_verify()
|
|
||||||
and not forward_batch.forward_mode.is_draft_extend()
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _support_mha_one_shot(attn: DeepseekV2AttentionMLA, forward_batch, backend_name):
|
def _support_mha_one_shot(attn: DeepseekV2AttentionMLA, forward_batch, backend_name):
|
||||||
attn_supported = backend_name in ["fa3", "flashinfer", "flashmla"]
|
attn_supported = backend_name in ["fa3", "flashinfer", "flashmla"]
|
||||||
sum_seq_lens = (
|
sum_seq_lens = (
|
||||||
@@ -334,7 +326,7 @@ def _handle_attention_backend(
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
not disable_ragged
|
not disable_ragged
|
||||||
and _is_extend_without_speculative(forward_batch)
|
and forward_batch.forward_mode.is_extend_without_speculative()
|
||||||
and (
|
and (
|
||||||
(
|
(
|
||||||
sum_extend_prefix_lens >= attn.chunked_prefix_cache_threshold
|
sum_extend_prefix_lens >= attn.chunked_prefix_cache_threshold
|
||||||
@@ -377,7 +369,7 @@ def handle_attention_fa4(attn, forward_batch):
|
|||||||
|
|
||||||
def handle_attention_trtllm_mla(attn, forward_batch):
|
def handle_attention_trtllm_mla(attn, forward_batch):
|
||||||
sum_extend_prefix_lens = _get_sum_extend_prefix_lens(forward_batch)
|
sum_extend_prefix_lens = _get_sum_extend_prefix_lens(forward_batch)
|
||||||
if _is_extend_without_speculative(forward_batch) and (
|
if forward_batch.forward_mode.is_extend_without_speculative() and (
|
||||||
not attn.disable_chunked_prefix_cache or sum_extend_prefix_lens == 0
|
not attn.disable_chunked_prefix_cache or sum_extend_prefix_lens == 0
|
||||||
):
|
):
|
||||||
return AttnForwardMethod.MHA_CHUNKED_KV
|
return AttnForwardMethod.MHA_CHUNKED_KV
|
||||||
@@ -386,7 +378,7 @@ def handle_attention_trtllm_mla(attn, forward_batch):
|
|||||||
|
|
||||||
|
|
||||||
def handle_attention_aiter(attn, forward_batch):
|
def handle_attention_aiter(attn, forward_batch):
|
||||||
if _is_extend_without_speculative(forward_batch):
|
if forward_batch.forward_mode.is_extend_without_speculative():
|
||||||
if is_dp_attention_enabled():
|
if is_dp_attention_enabled():
|
||||||
if sum(forward_batch.extend_prefix_lens_cpu) == 0:
|
if sum(forward_batch.extend_prefix_lens_cpu) == 0:
|
||||||
return AttnForwardMethod.MHA
|
return AttnForwardMethod.MHA
|
||||||
@@ -411,7 +403,7 @@ def handle_attention_nsa(attn, forward_batch):
|
|||||||
if forward_batch.forward_mode.is_decode_or_idle():
|
if forward_batch.forward_mode.is_decode_or_idle():
|
||||||
return AttnForwardMethod.MLA
|
return AttnForwardMethod.MLA
|
||||||
|
|
||||||
if _is_extend_without_speculative(forward_batch):
|
if forward_batch.forward_mode.is_extend_without_speculative():
|
||||||
assert forward_batch.seq_lens_cpu is not None
|
assert forward_batch.seq_lens_cpu is not None
|
||||||
max_kv_len = forward_batch.seq_lens_cpu.max().item()
|
max_kv_len = forward_batch.seq_lens_cpu.max().item()
|
||||||
|
|
||||||
@@ -439,7 +431,7 @@ def handle_attention_triton(attn, forward_batch):
|
|||||||
return _dispatch_mla_subtype(attn, forward_batch)
|
return _dispatch_mla_subtype(attn, forward_batch)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
_is_extend_without_speculative(forward_batch)
|
forward_batch.forward_mode.is_extend_without_speculative()
|
||||||
and sum(forward_batch.extend_prefix_lens_cpu) == 0
|
and sum(forward_batch.extend_prefix_lens_cpu) == 0
|
||||||
):
|
):
|
||||||
return AttnForwardMethod.MHA
|
return AttnForwardMethod.MHA
|
||||||
@@ -1517,7 +1509,7 @@ class DeepseekV2AttentionMLA(nn.Module):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# NSA Indexer: cache quantized keys, auto-skip topk for sequences <= nsa_index_topk
|
# NSA Indexer: cache quantized keys, auto-skip topk for sequences <= nsa_index_topk
|
||||||
if self.use_nsa and _is_extend_without_speculative(forward_batch):
|
if self.use_nsa:
|
||||||
_ = self.indexer(
|
_ = self.indexer(
|
||||||
x=hidden_states,
|
x=hidden_states,
|
||||||
q_lora=q_lora,
|
q_lora=q_lora,
|
||||||
|
|||||||
Reference in New Issue
Block a user