nemo_automodel.components.speculative.eagle.msd_decode
nemo_automodel.components.speculative.eagle.msd_decode
Reference multimodal speculative decoding primitives for MSD.
The module implements the feature-level EAGLE/MSD decoding contract in plain
PyTorch. It intentionally keeps the proposal tree and posterior verification
separate from serving-engine kernels: engines can use :class:MSDTreeLayout to
execute the tree in one attention pass, while the Hugging Face reference path
verifies each leaf independently for correctness and integration testing.
Module Contents
Classes
Functions
API
Batch-one reference MSD round with recursive drafting and tree verification.
Find the language token embedding module across common HF VLM layouts.
Draft and greedily verify one MSD candidate tree for a VLM prompt.
Result of one lossless speculative acceptance decision.
Lossless stochastic verification result for a linear candidate chain.
Recursively draft a top-k MSD feature tree from target prefix features.
Create a feature-level candidate tree after a target-sampled root.
shifted_inputs_embeds and shifted_image_mask use the same
next-token alignment as :class:HFMSDTargetModel. The final shifted
embedding is replaced with the target root token embedding, then each
recursive draft prediction is appended as the feature context for its
child nodes. attention_mask must be right-padded, since the prefix is
taken as its leading active slice.
Tree attention and retrieval metadata for an MSD proposal.
Node zero represents the target-sampled root token. attention_mask is
boolean and contains the root plus every ancestor for each node. Serving
backends turn its false entries into additive -inf attention bias.
One drafted text-token node in an MSD candidate tree.
A target root token plus recursively drafted candidate-tree nodes.
Return the root-to-leaf candidate-token path.
Return all leaf paths in deterministic tree order.
Greedy posterior verification result for one candidate tree.
Return accepted candidate tokens followed by the target bonus token.
Internal recursive feature-draft state for one tree node.
Return the prefix length of a batch-one right-padded attention mask.
The reference decoder slices every tensor as [:, :active_length], which
is only the real prefix when padding sits at the end. Left padding would
silently select pad positions, so reject it instead.
Reject a random generator whose device type differs from the sampling device.
torch.rand and torch.multinomial both refuse such a generator, so a
mismatch here would otherwise surface as an opaque failure part-way through
verification. The check is deliberately type-level, matching PyTorch’s own
generator check, which likewise ignores the device index.
Apply the lossless speculative acceptance rule for one candidate token.
generator must live on the same device as the logits, because both the
acceptance draw and the residual resample run there. random_value comes
from the half-open interval [0, 1) that torch.rand samples, and the
candidate is accepted on a strict draw < acceptance so a token the target
assigns zero probability can never be accepted.
Build engine-ready tree attention and root-to-leaf retrieval metadata.
Select the longest target-greedy accepted path and its bonus token.
Each logits tensor has one row for every candidate token in the matching leaf path plus one final row for the target bonus token. This is the greedy form of EAGLE/MSD posterior verification.
Reference target verification for a batch-one Hugging Face VLM.
The path-wise implementation is intentionally engine neutral and works for
VLMs whose visual tensors are prompt-only. A serving backend can instead
consume proposal.layout to verify the same tree in one tree-attention
target forward.
Losslessly verify a linear draft chain and emit the correction token.