[feature] add index share feature for DSA MTP (#44420)
Signed-off-by: JaredforReal <w13431838023@gmail.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
9c7f7741d4
commit
32f34d3935
@@ -70,6 +70,7 @@ class SpecDecodeBaseProposer:
|
||||
self.draft_model_config = self.speculative_config.draft_model_config
|
||||
self.method = self.speculative_config.method
|
||||
self.pass_hidden_states_to_model = pass_hidden_states_to_model
|
||||
self._share_mtp_indices = False
|
||||
|
||||
self.device = device
|
||||
self.dtype = vllm_config.model_config.dtype
|
||||
@@ -490,6 +491,11 @@ class SpecDecodeBaseProposer:
|
||||
model_kwargs, slot_mapping_size = self.build_model_inputs_first_pass(
|
||||
num_tokens, num_input_tokens, mm_embed_inputs
|
||||
)
|
||||
# Step 0 of index_share_for_mtp_iteration: let the MTP layer
|
||||
# compute its own indices (skip_topk=False) so subsequent steps
|
||||
# can reuse them.
|
||||
if self._share_mtp_indices and hasattr(self.model.model, "set_skip_topk"):
|
||||
self.model.model.set_skip_topk(False)
|
||||
|
||||
with set_forward_context(
|
||||
per_layer_attn_metadata,
|
||||
@@ -508,6 +514,11 @@ class SpecDecodeBaseProposer:
|
||||
else:
|
||||
last_hidden_states, hidden_states = ret_hidden_states
|
||||
|
||||
# After step 0: switch to reuse mode so steps 1+ skip the indexer
|
||||
# and read the indices that step 0 just wrote into the shared buffer.
|
||||
if self._share_mtp_indices and hasattr(self.model.model, "set_skip_topk"):
|
||||
self.model.model.set_skip_topk(True)
|
||||
|
||||
sample_hidden_states = last_hidden_states[token_indices_to_sample]
|
||||
|
||||
# Early exit if there is only one draft token to be generated.
|
||||
@@ -1418,16 +1429,34 @@ class SpecDecodeBaseProposer:
|
||||
)
|
||||
|
||||
if hasattr(target_language_model.model, "topk_indices_buffer"):
|
||||
target_buffer = target_language_model.model.topk_indices_buffer
|
||||
if hasattr(self.model.model, "topk_indices_buffer"):
|
||||
del self.model.model.topk_indices_buffer
|
||||
self.model.model.topk_indices_buffer = (
|
||||
target_language_model.model.topk_indices_buffer
|
||||
)
|
||||
self.model.model.topk_indices_buffer = target_buffer
|
||||
# Also share at per-module level so that the indexer and
|
||||
# sparse-attention backends in each MTP layer read from
|
||||
# the target model's buffer.
|
||||
for _, module in self.model.model.named_modules():
|
||||
if hasattr(module, "topk_indices_buffer"):
|
||||
module.topk_indices_buffer = target_buffer
|
||||
logger.info(
|
||||
"Detected MTP model with topk_indices_buffer. "
|
||||
"Sharing target model topk_indices_buffer with the draft model."
|
||||
)
|
||||
|
||||
# Detect index_share_for_mtp_iteration: when True, the proposer
|
||||
# toggles skip_topk so step 0 computes MTP's own indices and
|
||||
# steps 1+ reuse them.
|
||||
spec_config = self.vllm_config.speculative_config
|
||||
draft_hf_config = (
|
||||
spec_config.draft_model_config.hf_config
|
||||
if spec_config is not None
|
||||
else None
|
||||
)
|
||||
self._share_mtp_indices = getattr(
|
||||
draft_hf_config, "index_share_for_mtp_iteration", False
|
||||
)
|
||||
|
||||
if self.use_local_argmax_reduction:
|
||||
if not hasattr(self.model, "get_top_tokens"):
|
||||
raise ValueError(
|
||||
|
||||
Reference in New Issue
Block a user