-
Notifications
You must be signed in to change notification settings - Fork 468
[Refactor] optimize _prepare_inputs method in eagle_proposer #3296
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
Signed-off-by: lio <[email protected]>
👋 Hi! Thank you for contributing to the vLLM Ascend project. The following points will speed up your PR merge:
If CI fails, you can run linting and testing checks locally according Contributing and Testing. |
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 refactors the _prepare_inputs
method in eagle_proposer.py
to improve performance by replacing a sequential, on-device loop with vectorized numpy operations on the CPU. This is a solid optimization. I've found one area for further improvement: there's a redundant calculation that can be removed to make the code even more efficient.
# need use npu | ||
query_len_per_req = (cu_target_query_lens[1:] - | ||
cu_target_query_lens[:-1]) | ||
# [a, b, c] -> [a - n1, b - n2, c - n3] | ||
num_tokens_per_req = query_len_per_req - num_rejected_tokens | ||
|
||
# [a - n1, b - n2, c - n3] -> | ||
# [0, a - n1, a + b - n1 - n2, a + b + c - n1 - n2 - n3] | ||
cu_num_tokens = torch.zeros_like(cu_target_query_lens) | ||
torch.cumsum(num_tokens_per_req, dim=0, out=cu_num_tokens[1:]) |
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 calculation of cu_num_tokens
on the device is redundant. The same value has already been computed on the CPU as new_query_start_loc_cpu
. Instead of re-calculating it on the device, you can simply transfer the CPU tensor to the device. This avoids unnecessary computation and better aligns with the goal of optimizing performance.
# need use npu | |
query_len_per_req = (cu_target_query_lens[1:] - | |
cu_target_query_lens[:-1]) | |
# [a, b, c] -> [a - n1, b - n2, c - n3] | |
num_tokens_per_req = query_len_per_req - num_rejected_tokens | |
# [a - n1, b - n2, c - n3] -> | |
# [0, a - n1, a + b - n1 - n2, a + b + c - n1 - n2 - n3] | |
cu_num_tokens = torch.zeros_like(cu_target_query_lens) | |
torch.cumsum(num_tokens_per_req, dim=0, out=cu_num_tokens[1:]) | |
cu_num_tokens = new_query_start_loc_cpu.to(device, non_blocking=True) |
What this PR does / why we need it?
We optimized the _prepare_input method in eagle_proposer and no longer use the _prepare_eagle_input_sequential method, improving the performance of eagle-3.
Does this PR introduce any user-facing change?
No
How was this patch tested?
Co-authored-by: QilaiZhang ([email protected] )