Add speculative decoding support with MTP layers#3594
Merged
santhnm2 merged 114 commits intoNVIDIA:mainfrom Mar 11, 2026
Merged
Add speculative decoding support with MTP layers#3594santhnm2 merged 114 commits intoNVIDIA:mainfrom
santhnm2 merged 114 commits intoNVIDIA:mainfrom
Conversation
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Contributor
Author
|
/ok to test 8e3710f |
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Contributor
Author
|
/ok to test 9727533 |
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Contributor
Author
|
/ok to test 6a08b28 |
Contributor
Author
|
/ok to test 3296f19 |
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Contributor
Author
|
/ok to test fade26c |
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Contributor
Author
|
/ok to test b578a6a |
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Contributor
Author
|
/ok to test 277dfba |
@santhnm2, there was an error processing your request: See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/2/ |
Contributor
Author
|
/ok to test 2169c01 |
Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com>
Contributor
Author
|
/ok to test 5ee472a |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/22976150513 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do ?
This PR adds speculative decoding support for inference.
How it works
Each generation step proceeds in phases:
Input construction — For each active decode request, 1 +
Ktokens are fed into the model: the previously sampled token plus K speculative tokens from the MTP heads of the previous step. These are interleaved across requests with matching position IDs.(
dynamic_context.py:update_requests)Forward pass — The model processes all tokens in one pass. The base decoder produces logits at every position (note:
materialize_only_last_token_logitsmust be off). The MTP heads produceKadditional sets of logits from their lightweight transformer/Mamba layers, cached inmodel._mtp_logits_cache. These are concatenated to produce a[1+K, seq_len, vocab_size]logit tensor.(
text_generation_controller.py:_dynamic_step_forward_logits)Sampling & verification — Both base and MTP logits are sampled (grouped into temperature/top_k/top_p buckets for efficiency). Then a greedy token-matching verification determines how many speculative tokens to accept: a speculative token at position
t+kis accepted iff the base model's output at positiont+k-1equals it. Acceptance is consecutive — once a mismatch occurs, all subsequent speculative tokens for that request are rejected (enforced viacummin).(
text_generation_controller.py:_dynamic_step_sample_logits_and_verify_tokens)KV cache rewind — For rejected tokens, the KV cache is rolled back: block offsets are decremented, and if the rewind crosses a block boundary, the block is released back to the allocator. For Mamba/hybrid models, SSM recurrent state is restored from intermediate snapshots captured during the Triton kernel execution.
(
text_generation_controller.py:_rewind_kv_cache)Bookkeeping — Sequence lengths advance by
accepted_count + 1(not just 1). Finish conditions (EOS, max length) are checked, and the accepted + sampled tokens are appended to each request's output. New MTP-sampled tokens are staged for the next step.MTP head architecture
Each
MultiTokenPredictionLayer(multi_token_prediction.py) takes the hidden states from the previous depth, concatenates them with shifted input embeddings via a learned projection (eh_proj), and runs the result through a transformer block (or Mamba stack). This is repeatedKtimes to produce predictions at positionst+1throught+K. Amtp_use_repeated_layeroption shares weights across all K layers.Mamba/hybrid SSM support
SSM models require special handling because they carry recurrent state that must be rollback-able:
Kslots, so no explicit save/restore is needed.mamba_ssm.py) dumps intermediate state snapshots at every speculative step into a pre-allocated[num_layers, max_requests, K+1, ...]buffer — zero extra GPU-CPU sync. On rewind, the correct snapshot is indexed by accepted token count.Contribution process
flowchart LR A[Pre-checks] --> B[PR Tests] subgraph Code Review/Approval C1[Expert Review] --> C2[Final Review] end B --> C1 C2 --> D[Merge]Pre-checks
Core 0.8)Code review
The following process is enforced via the CODEOWNERS file for changes into
megatron/core. For changes outside ofmegatron/core, it is up to the PR author whether or not to tag the Final Reviewer team.For MRs into `main` branch
Feel free to message or comment the @mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!
(Step 1): Add PR label
Expert Review(Step 2): Collect the expert reviewers reviews
Expert Reviewlabel when your PR is ready for review.Final Review might get declined if these requirements are not fulfilled.
(Step 3): Final Review
Final Reviewlabel(Optional Step 4): Cherry-pick into release branch
If this PR also needs to be merged into
core_r*release branches, after this PR has been merged, selectCherry-pickto open a new PR into the release branch.For MRs into `dev` branch
The proposed review process for `dev` branch is under active discussion.MRs are mergable after one approval by either
eharper@nvidia.comorzijiey@nvidia.com.Merging your PR
Any member of core-adlr and
core-nemowill be able to merge your PR.