vllm
vllm copied to clipboard
[Core] Faster logit_bias_logits_processor
Change python ops to tensor ops.
Before
for token_id, bias in logit_bias.items():
logits[token_id] += bias
The above approach is time consuming especially when len(logit_bias) is very large.
After
logits.index_add_(0, logit_bias["index"], logit_bias["value"])
Time Cost
| len(logit_bias) | time cost (ms) |
|---|---|
| 1 | 4.5 -> 0.3 |
| 20 | 4.5 -> 0.3 |
| 100 | 5.3 -> 0.3 |
| 1000 | 14.4 -> 0.3 |
| 10000 | 106 -> 0.4 |
experiment settings:
GPU: A100
model: Llama-3.2-1B-Instruct
impl history
- v1: no cache
- v2: cache in single request: https://github.com/vllm-project/vllm/blob/62a74e3d1bf3b9b34d17cd5fb46e36f0be756d89/vllm/entrypoints/openai/logits_processors.py#L50-L53
- v3(current): cache across different requests https://github.com/vllm-project/vllm/blob/cd9f33fa750fbeabfd8ee8526b5a52ae99580f28/vllm/entrypoints/openai/logits_processors.py#L70-L74
👋 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 fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.
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 ready label to the PR or enable auto-merge.
🚀
If len(logit_bias) is large, maybe we can keep the copy of logit_bias["index"] and logit_bias["value"] in the device memory ahead of time (or in the first sample step), and re-use it in the following sample steps, to avoid duplicated tensor copy?
If
len(logit_bias)is large, maybe we can keep the copy oflogit_bias["index"]andlogit_bias["value"]in the device memory ahead of time (or in the first sample step), and re-use it in the following sample steps, to avoid duplicated tensor copy?
@imkero Thanks for your suggestion, a new commit has been added, which avoid duplicated tensor copy.
After this change, the time_cost is reduced to 0.01ms
| len(logit_bias) | time cost (ms) |
|---|---|
| 1 | 4.5 -> 0.01 |
| 20 | 4.5 -> 0.01 |
| 100 | 5.3 -> 0.01 |
| 1000 | 14.4 -> 0.01 |
| 10000 | 106 -> 0.01 |
This pull request has merge conflicts that must be resolved before it can be merged. Please rebase the PR, @xu-song.
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork
@njhill conflicts have been resolved.
This pull request has been automatically marked as stale because it has not had any activity within 90 days. It will be automatically closed if no further activity occurs within 30 days. Leave a comment if you feel this pull request should remain open. Thank you!
This pull request has been automatically closed due to inactivity. Please feel free to reopen if you intend to continue working on it. Thank you!