vllm icon indicating copy to clipboard operation
vllm copied to clipboard

[Core] Faster logit_bias_logits_processor

Open xu-song opened this issue 9 months ago • 3 comments

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

xu-song avatar Feb 15 '25 13:02 xu-song

👋 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.

🚀

github-actions[bot] avatar Feb 15 '25 13:02 github-actions[bot]

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?

imkero avatar Feb 15 '25 20:02 imkero

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?

@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

xu-song avatar Feb 17 '25 02:02 xu-song

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

mergify[bot] avatar Apr 02 '25 23:04 mergify[bot]

@njhill conflicts have been resolved.

xu-song avatar Apr 05 '25 02:04 xu-song

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!

github-actions[bot] avatar Jul 04 '25 02:07 github-actions[bot]

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!

github-actions[bot] avatar Aug 04 '25 02:08 github-actions[bot]