-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Use trtllm mha decode kernel for target_verify in speculative decoding #13976
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
Conversation
Summary of ChangesHello @Qiaolin-Yu, 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 significantly enhances the performance of speculative decoding, particularly for models handling very long contexts. By conditionally employing a specialized TRT-LLM MHA decode kernel for the 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.
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 performance optimization for speculative decoding by using the trtllm_mha_decode_kernel for the target_verify step. This is a sensible change, as the decode kernel is better optimized for the fixed query length characteristic of the verification phase. The changes correctly dispatch to the appropriate kernel based on the forward mode and ensure the necessary metadata is updated for CUDA graph replay. I have one suggestion to improve code maintainability by reducing duplication.
| if forward_batch.forward_mode.is_target_verify(): | ||
| o = flashinfer.decode.trtllm_batch_decode_with_kv_cache( | ||
| query=q, | ||
| kv_cache=kv_cache, | ||
| workspace_buffer=self.workspace_buffer, | ||
| block_tables=self.forward_metadata.page_table, | ||
| seq_lens=self.forward_metadata.cache_seqlens_int32, | ||
| max_seq_len=self.max_context_len, | ||
| bmm1_scale=bmm1_scale, | ||
| bmm2_scale=bmm2_scale, | ||
| window_left=layer.sliding_window_size, | ||
| # TODO: add attention_sink operation or nvfp4 scale factor if needed | ||
| sinks=attention_sink, | ||
| out_dtype=self.q_data_type, # model_runner.dtype | ||
| q_len_per_req=self.forward_metadata.max_seq_len_q, | ||
| ) | ||
| else: | ||
|
|
||
| o = flashinfer.prefill.trtllm_batch_context_with_kv_cache( | ||
| query=q, | ||
| kv_cache=kv_cache, | ||
| workspace_buffer=self.workspace_buffer, | ||
| block_tables=self.forward_metadata.page_table, | ||
| seq_lens=self.forward_metadata.cache_seqlens_int32, | ||
| max_q_len=self.forward_metadata.max_seq_len_q, | ||
| max_kv_len=self.max_context_len, | ||
| bmm1_scale=bmm1_scale, | ||
| bmm2_scale=bmm2_scale, | ||
| batch_size=forward_batch.batch_size, | ||
| cum_seq_lens_q=self.forward_metadata.cu_seqlens_q, | ||
| cum_seq_lens_kv=self.forward_metadata.cu_seqlens_k, | ||
| window_left=layer.sliding_window_size, | ||
| # TODO: add attention_sink operation or nvfp4 scale factor if needed | ||
| sinks=attention_sink, | ||
| out_dtype=self.q_data_type, # model_runner.dtype | ||
| ) |
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.
There is significant code duplication in the arguments passed to trtllm_batch_decode_with_kv_cache and trtllm_batch_context_with_kv_cache. To improve readability and maintainability, you could extract the common arguments into a dictionary.
common_args = {
"query": q,
"kv_cache": kv_cache,
"workspace_buffer": self.workspace_buffer,
"block_tables": self.forward_metadata.page_table,
"seq_lens": self.forward_metadata.cache_seqlens_int32,
"bmm1_scale": bmm1_scale,
"bmm2_scale": bmm2_scale,
"window_left": layer.sliding_window_size,
# TODO: add attention_sink operation or nvfp4 scale factor if needed
"sinks": attention_sink,
"out_dtype": self.q_data_type, # model_runner.dtype
}
if forward_batch.forward_mode.is_target_verify():
o = flashinfer.decode.trtllm_batch_decode_with_kv_cache(
**common_args,
max_seq_len=self.max_context_len,
q_len_per_req=self.forward_metadata.max_seq_len_q,
)
else:
o = flashinfer.prefill.trtllm_batch_context_with_kv_cache(
**common_args,
max_q_len=self.forward_metadata.max_seq_len_q,
max_kv_len=self.max_context_len,
batch_size=forward_batch.batch_size,
cum_seq_lens_q=self.forward_metadata.cu_seqlens_q,
cum_seq_lens_kv=self.forward_metadata.cu_seqlens_k,
)|
/tag-and-rerun-ci |
sgl-project#13976) Co-authored-by: Kangyan-Zhou <[email protected]>
Motivation
For long context (e.g., 50000 input len, 10000 output len), target_verify will have very bad performance when using prefill kernel.
Modifications
Accuracy Tests
Benchmarking and Profiling
Checklist