[V1][Spec Decode] Add Dynamic SD (#32374)

Signed-off-by: Ekagra Ranjan <3116519+ekagra-ranjan@users.noreply.github.com>
Signed-off-by: Benjamin Chislett <chislett.ben@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Co-authored-by: Benjamin Chislett <chislett.ben@gmail.com>
This commit is contained in:
Ekagra Ranjan
2026-06-14 00:14:27 -07:00
committed by GitHub
co-authored by gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Harry Mellor Benjamin Chislett
parent 78e7293bb1
commit 4ef4492e9b
23 changed files with 586 additions and 7 deletions
+13
View File
@@ -434,6 +434,7 @@ class SpecDecodeBaseProposer:
def propose(
self,
num_speculative_tokens,
# [num_tokens]
target_token_ids: torch.Tensor,
# [num_tokens] or [3, num_tokens] when M-RoPE is enabled
@@ -451,6 +452,7 @@ class SpecDecodeBaseProposer:
| list[dict[str, torch.Tensor]]
| None = None,
) -> torch.Tensor:
self.num_speculative_tokens = num_speculative_tokens
self._last_draft_probs = None
batch_size = common_attn_metadata.batch_size()
@@ -521,6 +523,17 @@ class SpecDecodeBaseProposer:
sample_hidden_states = last_hidden_states[token_indices_to_sample]
# No draft tokens requested (e.g. Dynamic SD decided K=0).
# The prefill forward pass above already ran to keep the drafter
# KV cache in sync, so just return an empty tensor.
if self.num_speculative_tokens == 0:
return torch.empty(
batch_size,
0,
device=sample_hidden_states.device,
dtype=torch.int64,
)
# Early exit if there is only one draft token to be generated.
if self.num_speculative_tokens == 1 or self.parallel_drafting:
draft_token_ids, draft_probs = self._sample_draft_tokens(