-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[V1] Enable prefill optimization for Gemma3n #22628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a significant performance optimization for Gemma3n models by enabling a fast prefill path, inspired by the YOCO paper. The implementation is well-thought-out, involving a refactoring of the Gemma3n model into self-decoder and cross-decoder modules to work with torch.compile
and dynamic batch sizes. The changes to attention metadata and the use of a conditional compilation decorator are clean solutions. Overall, the changes are robust and the associated refactoring of KV cache sharing logic improves the codebase. However, there is a critical gap in testing that should be addressed.
👋 Hi! Thank you for contributing to the vLLM project. 💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels. Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging. To run CI, PR reviewers can either: Add 🚀 |
This pull request has merge conflicts that must be resolved before it can be |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution. I took a look on model runner changes. Will check gemma3m.py later.
a822572
to
bcf331a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almosst LGTM. But maybe we need more discussion about custom attention abstraction @LucasWilkinson
embed_scale: torch.Tensor, | ||
): | ||
super().__init__() | ||
self.decoder_layers = decoder_layers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will there be any hidden problem when the decoder_layers are registered with both Gemma3nTextModel.layers
and Gemma3nTextModel.self_decoder.decoder_layers
in nn.Module
? A cleaner solution would be to only register it in Gemma3nSelfDecoder
(but need to update the weight loader, can do it in a follow-up PR after the model structure is finalized)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, mostly did this for simplicity as I couldn't really think of a case where it would be problematic (though there could be). I do want to separate this to a separate PR if possible
This pull request has merge conflicts that must be resolved before it can be |
6464c15
to
21e9cac
Compare
Signed-off-by: Yong Hoon Shin <[email protected]>
Signed-off-by: Yong Hoon Shin <[email protected]>
Signed-off-by: Yong Hoon Shin <[email protected]>
Signed-off-by: Yong Hoon Shin <[email protected]>
Signed-off-by: Yong Hoon Shin <[email protected]>
Signed-off-by: Yong Hoon Shin <[email protected]>
Signed-off-by: Yong Hoon Shin <[email protected]>
0d5a442
to
1949e8b
Compare
@@ -890,6 +898,9 @@ def _prepare_inputs( | |||
max_seq_len=max_seq_len, | |||
block_table_tensor=blk_table_tensor, | |||
slot_mapping=slot_mapping, | |||
logits_indices_padded=logits_indices_padded, | |||
num_logits_indices=logits_indices.size(0), | |||
prompt_logprobs=len(self.input_batch.num_prompt_logprobs) > 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LucasWilkinson @heheda12345 WDYT? I know we discussed how we don't want the common attention metadata being a dumping ground for stuff different builders need, but in this case I'm not sure how to otherwise propagate this information from the model runner -> builder.
We need this in order to not perform the prefill optimization if at least one request requrest prompt token logprobs (as the logprobs will be invalid, similar to prefix caching). The options are:
- Raising an exception and letting user request crash a server (which we don't want)
- Silent incorrectness + warning msg (also don't want)
- Disable optimization and return correct logprobs at expense of slight perf regression (this solution)
- Don't schedule such request in first place and return an error without crashing server (AFAIK not possible to do in vLLM at the moment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I think another solution is to reject the requests with logprobs when they are adding to the server. For online infernece, it won't crash the server. For offline inference, it can crash at very beginning.
Essential Elements of an Effective PR Description Checklist
supported_models.md
andexamples
for a new model.Purpose
This PR adds an option to enable prefill optimization for Gemma3n model with
--kv-sharing-fast-prefill
.Background
In You Only Cache Once (https://arxiv.org/abs/2405.05254), self-decoder layers generate KV caches while cross-decoder layers use cross-attention and reuse the shared KV cache. As only self-decoder layers generate KV caches, cross-decoder layers don't need to do prefill. Below is a figure from the YOCO paper on the prefill optimization:
Design
In vLLM V1, the scheduler does not distinguish between prefill and decode. Instead, tokens for requests doing prefill and decode are batched together, as illustrated below (source: vLLM blog):
When we skip tokens corresponding to prefill in the cross-decoder layers, we therefore will have the batch size reduced during model forward for the cross-decoder layers:
Without optimization enabled (baseline)
With optimization enabled (--kv-sharing-fast-prefill)
With this change, we can no longer compile the top-level model for 2 reasons:
Solution: we split the layers into self- and cross-decoder layers, and compile + graph capture them separately. For Gemma3n-E2B which has 30 layers, the first 20 layers and other 10 layers will be grouped separately into independently compiled and CUDA graph captured modules.
Other changes required in this PR:
make_kv_sharing_fast_prefill_common_attn_metadata
to create an attention metadata excluding all prefill tokens. This requires passinglogits_indices
toCommonAttentionMetadata
KVSharingFastPrefillAttentionMetadata
. This has two additional metadata (logits_indices_padded
andnum_logits_indices
) which are required for indexing into hidden states in the model implementation to match the shapes that the new attention metadata expectshidden_states
shape from[altup_num_inputs, num_tokens,hidden_size]
to[num_tokens,hidden_size, altup_num_inputs]
to ensurenum_tokens
(batch size) comes at dim 0. We cannot havenum_tokens
be on dim=1 because creating a slice along dim=1 would a) cause torch.compile tensor stride assertions to fail, and b) resolving this by callingcontiguous()
on the slice would cause memory copy and therefore violate CUDA graph static address constraint.--kv-sharing-fast-prefill
flag is passed, we take a differentself.fast_prefill_forward()
path which uses thelogits_indices_padded
metadata passed to index into the subset of tokens for cross-decoder layers (i.e. batch size is reduced). We then merge it back to the output of self-decoder to get the final output.--kv-sharing-fast-prefill
flag is passed, we will compile self-decoder and cross-decoder submodules separately, and we also need to pre-allocate static buffers for CUDA graph replay. If it is not passed (default), we will still compile the top-levelGemma3TextModel
Compared to trunk, the only difference is there are extra groups for attn_groups[0] and attn_groups[4] for layers which need a separate attention metadata builder for the fast prefill path. Previously it looks like this:
Important
When
prompt_logprobs
is enabled, we can no longer use fast prefill optimization. This is because by skipping all but last prefill tokens, the logits for the prompt tokens will no longer be valid. For example, multiple choice question (MCQ) evals useprompt_logprobs
to get the logprobs of continuation tokens (e.g. lm-evaluation-harness), so using--kv-sharing-fast-prefill
will yield inaccurate results. To prevent this, we will disable half prefill for the scheduling iterations where at least one request hasprompt_logprobs
set.Follow ups
--kv-sharing-fast-prefill
is set)self_decoder_hidden_state.clone()
in Gemma3nTest Plan
Evals
ran gsm8k, mmlu, mmlu pro
Unit tests
Performance
Perform sweep over
max-concurrency
andrandom-input-len
,$num_reqs = 256
max-num-batched-tokens = 8192
max-num-seqs = 128
Test Result
Evals
Evals on par
Unit tests
Unit tests all pass
Performance
Mean TTFT and TPOT (ms):