@support_torch_compile
class NemotronShortcutMTP(
LocalArgmaxMixin,
nn.Module,
SupportsPP,
SupportsQuant,
SupportsMultiModalEmbeddings,
):
"""The draft head is trained on the target's input stream, so on image and
video prompts the placeholder positions carry the target's vision
embeddings, not the text embedding of the placeholder token."""
# The draft head consumes only the `mtp.*` subtree plus the shared embedding
# and head; everything else in the checkpoint belongs to the target.
hf_to_vllm_mapper = WeightsMapper(
orig_to_new_prefix={
"model.language_model.mtp.": "model.",
"model.language_model.embed_tokens.": "model.embed_tokens.",
"model.language_model.": None,
"model.vision_tower.": None,
"model.multi_modal_projector.": None,
"model.video_patch_conv.": None,
},
orig_to_new_stacked={
".self_attn.q_proj": (".self_attn.qkv_proj", "q"),
".self_attn.k_proj": (".self_attn.qkv_proj", "k"),
".self_attn.v_proj": (".self_attn.qkv_proj", "v"),
},
)
packed_modules_mapping = {"qkv_proj": ["q_proj", "k_proj", "v_proj"]}
def __init__(self, *, vllm_config: VllmConfig, prefix: str = "") -> None:
super().__init__()
speculative_config = vllm_config.speculative_config
assert speculative_config is not None
draft_model_config = speculative_config.draft_model_config
assert draft_model_config is not None
config = draft_model_config.hf_config.get_text_config()
self.config = config
self.vllm_config = vllm_config
# The draft head attends in the target's KV cache group, with the
# target's SDSA config. An hf_overrides of the target's config does not
# reach the draft's config, so take the target's SDSA fields.
target_config = vllm_config.model_config.hf_text_config
config.index_topk = target_config.index_topk
config.sdsa_indexer_dtype = getattr(target_config, "sdsa_indexer_dtype", "auto")
config.sdsa_decode_core = getattr(target_config, "sdsa_decode_core", "auto")
config.sdsa_decode_fused = getattr(target_config, "sdsa_decode_fused", "auto")
config.sdsa_prefill_core = getattr(target_config, "sdsa_prefill_core", "auto")
config.sdsa_prefill_fused = getattr(target_config, "sdsa_prefill_fused", "auto")
self.model = NemotronShortcutMultiTokenPredictor(
vllm_config=vllm_config,
quant_config=self.quant_config,
prefix=maybe_prefix(prefix, "model"),
)
self.lm_head = ParallelLMHead(
config.vocab_size,
config.hidden_size,
quant_config=self.quant_config,
prefix=maybe_prefix(prefix, "lm_head"),
)
self.logits_processor = LogitsProcessor(config.vocab_size)
@staticmethod
def _find_quant_config(*args, **kwargs) -> QuantizationConfig | None:
# The draft head takes the draft model's quantization, as NemotronHMTP.
vllm_config = kwargs.get("vllm_config")
assert isinstance(vllm_config, VllmConfig)
return get_draft_quant_config(vllm_config)
def get_input_embeddings(self, input_ids: torch.Tensor) -> torch.Tensor:
return self.model.get_input_embeddings(input_ids)
def embed_input_ids(
self,
input_ids: torch.Tensor,
multimodal_embeddings: MultiModalEmbeddings | None = None,
*,
is_multimodal: torch.Tensor | None = None,
) -> torch.Tensor:
inputs_embeds = self.get_input_embeddings(input_ids)
if multimodal_embeddings is None or len(multimodal_embeddings) == 0:
return inputs_embeds
return _merge_multimodal_embeddings(
inputs_embeds,
multimodal_embeddings,
_require_is_multimodal(is_multimodal),
)
def forward(
self,
input_ids: torch.Tensor | None,
positions: torch.Tensor,
hidden_states: torch.Tensor | None = None,
inputs_embeds: torch.Tensor | None = None,
spec_step_idx: int = 0,
**kwargs: object,
) -> torch.Tensor:
assert hidden_states is not None
return self.model(
input_ids, positions, hidden_states, inputs_embeds, spec_step_idx
)
def compute_logits(self, hidden_states: torch.Tensor) -> torch.Tensor | None:
return self.logits_processor(self.lm_head, hidden_states)
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
return load_nemotron_shortcut_weights(self, weights, self.hf_to_vllm_mapper)