[Spec] Fix hidden_states size mismatch in STANDALONE speculative decoding#14563
[Spec] Fix hidden_states size mismatch in STANDALONE speculative decoding#14563alisonshao wants to merge 17 commits intomainfrom
Conversation
Use spec_info.draft_token_num instead of self.speculative_num_steps + 1 for num_tokens_per_batch in the verify() method. The previous value caused incorrect batch size calculation when draft_token_num differs from speculative_num_steps + 1, leading to tensor size mismatches during Mamba2 state updates.
Summary of ChangesHello @alisonshao, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request implements a critical fix for the speculative decoding verification process in Mamba2 models. By adjusting how the batch size is determined during the verification phase, it ensures that the system correctly accounts for the number of draft tokens per sequence. This prevents tensor dimension mismatches and enhances the stability and reliability of the model's inference, particularly in configurations where the number of speculative steps and draft tokens might vary. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request addresses a bug in the batch size calculation for the verification phase of speculative decoding, particularly affecting Mamba2 models. The change correctly uses spec_info.draft_token_num for num_tokens_per_batch instead of the hardcoded assumption of self.speculative_num_steps + 1. This is the correct approach, as draft_token_num accurately reflects the number of tokens per sequence in the verification batch, resolving potential tensor size mismatches when this value differs from the number of speculative steps. The fix is clear, logical, and directly solves the described issue. The change is approved.
|
/tag-and-rerun-ci |
…peculative decoding When creating idle batch spec_info, use the target model's hidden_size instead of the draft model's hidden_size. The hidden_states are used during verification with the target model, so they need to match the target model's dimensions. This fixes tensor size mismatch errors (e.g., 4480 vs 2048) when merging batches during speculative decoding with different draft and target model architectures.
…culative decoding Use target model's hidden_size instead of draft model's hidden_size when creating hidden_states buffer in EagleDraftExtendCudaGraphRunner. The hidden_states are used during verification with the target model.
This comment was marked as outdated.
This comment was marked as outdated.
…LONE mode Use target model's hidden_size for hidden_states buffer allocation to prevent tensor size mismatch during batch merging.
…ding For STANDALONE mode with different target/draft model architectures, use draft model's hidden_size consistently throughout: - CUDA graph buffers use draft model's hidden_size - Idle batch creation uses draft model's hidden_size - Add conditional check for hidden_states copy when sizes differ This prevents tensor size mismatch errors when merging batches.
This reverts commit bef28aa.
…or STANDALONE mode For STANDALONE speculative decoding, create placeholder hidden_states with draft model's hidden_size instead of using target model's hidden_states. This fixes the merge_batch error when hidden_states sizes don't match.
|
covered by: #14733; the current PR will be kept for reference |
|
Why did we skip the tests instead of fixing it like this PR did? |
Summary
Fix tensor size mismatch errors for STANDALONE speculative decoding with different draft and target model architectures (e.g., Nemotron-9B target + Llama-3.2-1B draft).
Problem
When using
--max-running-requests 8, batches get merged viamerge_batch(). This failed with:The issue: idle batches were created with target model's hidden_size (4480), but after draft model forward pass,
spec_info.hidden_stateshad draft model's hidden_size (2048). When merging these batches, the concatenation failed.Root Cause
For STANDALONE mode:
The draft model produces its own hidden_states (size 2048), not the target model's. So all
EagleDraftInput.hidden_statesshould consistently use the draft model's hidden_size.Changes
eagle_draft_cuda_graph_runner.py:
eagle_draft_extend_cuda_graph_runner.py:
eagle_worker.py:
_draft_preprocess_idle(): Use draft model's hidden_size for idle batchesforward_draft_extend_after_decode(): Use draft model's hidden_size for idle batchesspec_info.draft_token_numfornum_tokens_per_batchin verify phaseLogic
target_hidden_sizefrom hf_config (EAGLE head uses target's hidden states as input)self.model_runner.model_config.hidden_size(draft model produces its own hidden states)Test plan
TestNvidiaNemotronNanoV2SpeculativeDecodingintest_nvidia_nemotron_nano_v2.py