-
Notifications
You must be signed in to change notification settings - Fork 472
feat: enable trtllm-gen attn speculative decoding verify by decode #1453
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
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.
Summary of Changes
Hello @yyihuang, 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 introduces support for multi-token prediction (MTP) within the trtllm-gen
attention mechanism. My changes primarily involve modifying the expected shape of the query
tensor to accommodate multiple tokens per request, moving away from a single-token assumption. This enables more efficient speculative decoding by allowing the attention operation to process several tokens simultaneously. I've also updated the relevant test cases to validate this new functionality across various query lengths.
Highlights
- Core Logic Change for Multi-Token Prediction: I've updated the
_paged_run
andtrtllm_batch_decode_with_kv_cache
functions inflashinfer/decode.py
to remove theunsqueeze(1)
call on thequery
tensor. This change allows thequery
tensor to accept a multi-token dimension, moving from a[B, 1, H, D]
shape to[B, S, H, D]
, whereS
represents the sequence length or number of tokens per request. This is crucial for enabling multi-token prediction (MTP) within thetrtllm-gen
attention mechanism. - Docstring Update for Query Tensor Shape: To support the new multi-token
query
shape, I've updated the docstring for thequery
parameter intrtllm_batch_decode_with_kv_cache
to clearly state its new expected dimensions:[batch_size, q_len_per_request, num_heads, head_dim]
. This ensures clarity and correctness for future usage and development. - Expanded Test Coverage for Multi-Token Queries: I've modified
tests/test_trtllm_gen_decode.py
to includeq_len_per_request
as a parameter intest_trtllm_batch_decode_fmha
. This allows the test suite to validate thetrtllm-gen
attention mechanism with varying numbers of tokens per request, ensuring the new multi-token prediction capabilities work as expected across different scenarios. - Test Adaptation for Multi-Token Output: In the test
test_trtllm_batch_decode_fmha
, I've adjusted theq
tensor creation and removed theoutput.squeeze(1)
operation. These changes align the test's data handling with the new multi-tokenquery
tensor shape, ensuring that the test correctly processes and verifies outputs from the multi-token prediction enabled attention.
Using Gemini Code Assist
The 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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
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 .gemini/
folder in the base of the repository. Detailed instructions can be found here.
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 or fill out our survey to provide feedback.
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
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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 enables speculative decoding for trtllm-gen
attention by allowing the query tensor to have a sequence length dimension. The changes in flashinfer/decode.py
are consistent with this goal. However, the corresponding test file tests/test_trtllm_gen_decode.py
has a critical issue where the validation logic is disabled, making the test ineffective. Additionally, some debugging code has been left in the test file which should be removed.
tests/test_trtllm_gen_decode.py
Outdated
# output_ref = wrapper.run(ref_q, ref_kv_cache) # todo(Yingyi): fix mtp here | ||
# tmp | ||
output_ref = output |
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.
The reference implementation call to wrapper.run
is commented out, and output_ref
is assigned the value of output
. This effectively disables the test's validation logic, as it compares the output against itself. The test will pass as long as the function doesn't crash, but it doesn't verify the correctness of the computation. This should be fixed before merging. The todo
comment indicates this is a known issue that needs to be addressed.
tests/test_trtllm_gen_decode.py
Outdated
if __name__ == "__main__": | ||
test_trtllm_batch_decode_fmha( | ||
kv_layout="HND", | ||
batch_size=4, | ||
q_len_per_request=2, | ||
page_size=32, | ||
num_kv_heads=2, | ||
head_grp_size=1, | ||
window_left=-1, | ||
q_dtype="half", | ||
o_dtype="half", | ||
kv_cache_dtype="half", | ||
) |
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.
tests/test_trtllm_gen_attention.py
Outdated
@@ -196,6 +204,51 @@ def get_last_page_len(seq_lens, page_size): | |||
return kv_last_page_len | |||
|
|||
|
|||
def assert_close_with_mismatch_tolerance( |
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.
consider moving this to conftests
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.
fixed
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.
Would be great if you can work on the remaining item I mentioned in later PRs.
output.float(), output_wrapper.float(), rtol=1e-1, atol=1e-1 | ||
) | ||
# todo(Yingyi): fix precision issue with this test | ||
if not ( |
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.
I still have deep concern about the special handling of precision here, a more fundamental solution could be a PrecisionManager to centrialize the handling of tolerance for different data types.
Also, let's add a flashinfer.testing.assert_allclose() function where there could be arguments such as max mismatched elements, it will be fallback to torch.testing.assert_allclose when mismatched elements is not specified.
should unblock sgl-project/sglang#9471 now |
📌 Description
decode with q_len > 1
🔍 Related Issues
🚀 Pull Request Checklist
Thank you for contributing to FlashInfer! Before we review your pull request, please make sure the following items are complete.
✅ Pre-commit Checks
pre-commit
by runningpip install pre-commit
(or used your preferred method).pre-commit install
.pre-commit run --all-files
and fixed any reported issues.🧪 Tests
unittest
, etc.).Reviewer Notes