Skip to content
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

[Core] Use numpy to speed up padded token processing #6442

Merged
merged 3 commits into from
Jul 16, 2024

Conversation

peng1999
Copy link
Contributor

This small change replaces list comprehension with NumPy operations. It results in approximately a 50% speedup for the sampler when processing large batch sizes. This improvement will enhance throughput when running small models. (See benchmark results below)

Related: #249 #5289

Benchmarks

The following test was conducted on an NVIDIA A100 80GB GPU.

Sampler latency

This specifically measures the latency of the sampler component.

图片

When prompt token is long, the optimization will have large benefits.

图片

For short prompts, the optimization yields a modest speedup. However, it still outperforms the original version.

Throughput

The output is obtained by running the following script:

from vllm import LLM, SamplingParams

model_path = "/mnt/data/models/Qwen2-0.5B-Instruct"
llm = LLM(model=model_path, disable_log_stats=False, max_num_seqs=2048, trust_remote_code=True)
tokenizer = llm.get_tokenizer()

with open("text.txt", "r") as file:
    text = file.read()
length = 1024
text = tokenizer.decode(tokenizer.encode(text)[:length])
prompts = [text] * 2048
sampling_params = SamplingParams(temperature=1.0, top_p=0.5, top_k=20, max_tokens=128)

output = llm.generate(prompts, sampling_params)
main version
Processed prompts:   0%|                                                                                   | 0/2048 [00:00<?, ?it/s]INFO 07-15 17:06:37 metrics.py:334] Avg prompt throughput: 5243.8 tokens/s, Avg generation throughput: 5.1 tokens/s, Running: 32 reqs, Swapped: 0 reqs, Pending: 2016 reqs, GPU KV cache usage: 0.6%, CPU KV cache usage: 0.0%
INFO 07-15 17:06:42 metrics.py:334] Avg prompt throughput: 192507.0 tokens/s, Avg generation throughput: 188.0 tokens/s, Running: 992 reqs, Swapped: 0 reqs, Pending: 1056 reqs, GPU KV cache usage: 19.1%, CPU KV cache usage: 0.0%
INFO 07-15 17:06:47 metrics.py:334] Avg prompt throughput: 187176.6 tokens/s, Avg generation throughput: 182.8 tokens/s, Running: 1920 reqs, Swapped: 0 reqs, Pending: 128 reqs, GPU KV cache usage: 36.9%, CPU KV cache usage: 0.0%
INFO 07-15 17:06:52 metrics.py:334] Avg prompt throughput: 26000.9 tokens/s, Avg generation throughput: 3681.8 tokens/s, Running: 2048 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 40.0%, CPU KV cache usage: 0.0%
INFO 07-15 17:06:57 metrics.py:334] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 3821.7 tokens/s, Running: 2045 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 40.6%, CPU KV cache usage: 0.0%
INFO 07-15 17:07:02 metrics.py:334] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 3997.1 tokens/s, Running: 2045 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 40.6%, CPU KV cache usage: 0.0%
INFO 07-15 17:07:08 metrics.py:334] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 3947.8 tokens/s, Running: 2043 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 41.2%, CPU KV cache usage: 0.0%
INFO 07-15 17:07:13 metrics.py:334] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 3580.9 tokens/s, Running: 2043 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 41.8%, CPU KV cache usage: 0.0%
INFO 07-15 17:07:19 metrics.py:334] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 3820.1 tokens/s, Running: 2043 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 41.8%, CPU KV cache usage: 0.0%
INFO 07-15 17:07:24 metrics.py:334] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 3637.1 tokens/s, Running: 2043 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 42.4%, CPU KV cache usage: 0.0%
INFO 07-15 17:07:29 metrics.py:334] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 3586.9 tokens/s, Running: 2042 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 42.4%, CPU KV cache usage: 0.0%
INFO 07-15 17:07:34 metrics.py:334] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 3468.2 tokens/s, Running: 2041 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 43.0%, CPU KV cache usage: 0.0%
INFO 07-15 17:07:39 metrics.py:334] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 3477.8 tokens/s, Running: 2041 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 43.0%, CPU KV cache usage: 0.0%
INFO 07-15 17:07:45 metrics.py:334] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 3415.1 tokens/s, Running: 2041 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 43.6%, CPU KV cache usage: 0.0%
INFO 07-15 17:07:50 metrics.py:334] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 3548.2 tokens/s, Running: 2041 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 44.2%, CPU KV cache usage: 0.0%
INFO 07-15 17:07:55 metrics.py:334] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 3437.2 tokens/s, Running: 2041 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 44.2%, CPU KV cache usage: 0.0%
Processed prompts: 100%|████████████████████████████████████████████████████████████████████████| 2048/2048 [01:22<00:00, 24.82it/s]
optimized version
INFO 07-15 17:20:55 metrics.py:295] Avg prompt throughput: 31913.1 tokens/s, Avg generation throughput: 31.2 tokens/s, Running: 160 reqs, Swapped: 0 reqs, Pending: 1888 reqs, GPU KV cache usage: 3.1%, CPU KV cache usage: 0.0%.
INFO 07-15 17:21:00 metrics.py:295] Avg prompt throughput: 190974.6 tokens/s, Avg generation throughput: 186.5 tokens/s, Running: 1120 reqs, Swapped: 0 reqs, Pending: 928 reqs, GPU KV cache usage: 21.5%, CPU KV cache usage: 0.0%.
INFO 07-15 17:21:05 metrics.py:295] Avg prompt throughput: 187246.9 tokens/s, Avg generation throughput: 182.9 tokens/s, Running: 2048 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 39.3%, CPU KV cache usage: 0.0%.
INFO 07-15 17:21:10 metrics.py:295] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 6416.5 tokens/s, Running: 2046 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 39.9%, CPU KV cache usage: 0.0%.
INFO 07-15 17:21:16 metrics.py:295] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 6046.0 tokens/s, Running: 2045 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 40.5%, CPU KV cache usage: 0.0%.
INFO 07-15 17:21:21 metrics.py:295] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 6079.2 tokens/s, Running: 2045 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 41.1%, CPU KV cache usage: 0.0%.
INFO 07-15 17:21:26 metrics.py:295] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 5687.9 tokens/s, Running: 2045 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 41.7%, CPU KV cache usage: 0.0%.
INFO 07-15 17:21:31 metrics.py:295] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 5591.1 tokens/s, Running: 2045 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 42.3%, CPU KV cache usage: 0.0%.
INFO 07-15 17:21:36 metrics.py:295] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 4830.2 tokens/s, Running: 2044 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 42.9%, CPU KV cache usage: 0.0%.
INFO 07-15 17:21:42 metrics.py:295] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 5256.2 tokens/s, Running: 2043 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 43.5%, CPU KV cache usage: 0.0%.
INFO 07-15 17:21:47 metrics.py:295] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 5286.0 tokens/s, Running: 2042 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 44.1%, CPU KV cache usage: 0.0%.
INFO 07-15 17:21:52 metrics.py:295] Avg prompt throughput: 0.0 tokens/s, Avg generation throughput: 5070.9 tokens/s, Running: 0 reqs, Swapped: 0 reqs, Pending: 0 reqs, GPU KV cache usage: 0.0%, CPU KV cache usage: 0.0%.
Processed prompts: 100%|█| 2048/2048 [00:57<00:00, 35.36it/s, est. speed input: 36204.36 toks/s, output: 4517.

BEFORE SUBMITTING, PLEASE READ THE CHECKLIST BELOW AND FILL IN THE DESCRIPTION ABOVE


PR Checklist (Click to Expand)

Thank you for your contribution to vLLM! Before submitting the pull request, please ensure the PR meets the following criteria. This helps vLLM maintain the code quality and improve the efficiency of the review process.

PR Title and Classification

Only specific types of PRs will be reviewed. The PR title is prefixed appropriately to indicate the type of change. Please use one of the following:

  • [Bugfix] for bug fixes.
  • [CI/Build] for build or continuous integration improvements.
  • [Doc] for documentation fixes and improvements.
  • [Model] for adding a new model or improving an existing model. Model name should appear in the title.
  • [Frontend] For changes on the vLLM frontend (e.g., OpenAI API server, LLM class, etc.)
  • [Kernel] for changes affecting CUDA kernels or other compute kernels.
  • [Core] for changes in the core vLLM logic (e.g., LLMEngine, AsyncLLMEngine, Scheduler, etc.)
  • [Hardware][Vendor] for hardware-specific changes. Vendor name should appear in the prefix (e.g., [Hardware][AMD]).
  • [Misc] for PRs that do not fit the above categories. Please use this sparingly.

Note: If the PR spans more than one category, please include all relevant prefixes.

Code Quality

The PR need to meet the following code quality standards:

  • We adhere to Google Python style guide and Google C++ style guide.
  • Pass all linter checks. Please use format.sh to format your code.
  • The code need to be well-documented to ensure future contributors can easily understand the code.
  • Include sufficient tests to ensure the project to stay correct and robust. This includes both unit tests and integration tests.
  • Please add documentation to docs/source/ if the PR modifies the user-facing behaviors of vLLM. It helps vLLM user understand and utilize the new features or changes.

Notes for Large Changes

Please keep the changes as concise as possible. For major architectural changes (>500 LOC excluding kernel/data/config/test), we would expect a GitHub issue (RFC) discussing the technical design and justification. Otherwise, we will tag it with rfc-required and might not go through the PR.

What to Expect for the Reviews

The goal of the vLLM team is to be a transparent reviewing machine. We would like to make the review process transparent and efficient and make sure no contributor feel confused or frustrated. However, the vLLM team is small, so we need to prioritize some PRs over others. Here is what you can expect from the review process:

  • After the PR is submitted, the PR will be assigned to a reviewer. Every reviewer will pick up the PRs based on their expertise and availability.
  • After the PR is assigned, the reviewer will provide status update every 2-3 days. If the PR is not reviewed within 7 days, please feel free to ping the reviewer or the vLLM team.
  • After the review, the reviewer will put an action-required label on the PR if there are changes required. The contributor should address the comments and ping the reviewer to re-review the PR.
  • Please respond to all comments within a reasonable time frame. If a comment isn't clear or you disagree with a suggestion, feel free to ask for clarification or discuss the suggestion.

Thank You

Finally, thank you for taking the time to read these guidelines and for your interest in contributing to vLLM. Your contributions make vLLM a great tool for everyone!

Copy link

👋 Hi! Thank you for contributing to the vLLM project.
Just a reminder: PRs would not trigger full CI run by default. Instead, it would only trigger fastcheck CI to run, which consists only a small and essential subset of tests to quickly catch errors with the flexibility to run extra individual tests on top (you can do this by unblocking test steps in the Buildkite run).

Full CI run is still required to merge this PR so once the PR is ready to go, please make sure to run it. If you need all test signals in between PR commits, you can trigger full CI as well.

To run full CI, you can do one of these:

  • Comment /ready on the PR
  • Add ready label to the PR
  • Enable auto-merge.

🚀

@DarkLight1337 DarkLight1337 requested a review from comaniac July 15, 2024 10:29
@DarkLight1337
Copy link
Member

This seems to share quite a bit in common with vllm.utils.make_tensor_with_pad (recently optimized by #4196). I wonder why the utils function wasn't being used in the first place?

@comaniac comaniac enabled auto-merge (squash) July 16, 2024 02:40
@github-actions github-actions bot added the ready ONLY add when PR is ready to merge/full CI is needed label Jul 16, 2024
@DarkLight1337 DarkLight1337 disabled auto-merge July 16, 2024 03:01
@DarkLight1337 DarkLight1337 enabled auto-merge (squash) July 16, 2024 03:08
@simon-mo simon-mo merged commit 2bb0489 into vllm-project:main Jul 16, 2024
71 of 73 checks passed
@peng1999 peng1999 deleted the sampler-opt branch July 17, 2024 08:42
dtrifiro pushed a commit to opendatahub-io/vllm that referenced this pull request Jul 17, 2024
fialhocoelho pushed a commit to opendatahub-io/vllm that referenced this pull request Jul 19, 2024
xjpang pushed a commit to xjpang/vllm that referenced this pull request Jul 24, 2024
Alvant pushed a commit to compressa-ai/vllm that referenced this pull request Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready ONLY add when PR is ready to merge/full CI is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants