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 flashinfer sampling kernel when available #7137

Merged
merged 27 commits into from
Aug 19, 2024

Conversation

peng1999
Copy link
Contributor

@peng1999 peng1999 commented Aug 5, 2024

Flashinfer contains a combined kernel top_k_top_p_sampling_from_probs, and it is way faster than the sorting kernels used currently. This will eliminate the timely _apply_top_k_top_p function and reduce the GPU time of sampler.

main:
图片

this PR:
图片

End-to-end test (input length=1024, batch size=2048, model=qwen2-1.5b):

main:

Processed prompts: 100%|█████████████| 2048/2048 [01:08<00:00, 29.86it/s, est. speed input: 30574.62 toks/s, output: 3821.83 toks/s]

this PR:

Processed prompts: 100%|█████████████| 2048/2048 [01:02<00:00, 32.61it/s, est. speed input: 33396.84 toks/s, output: 4174.60 toks/s]

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

github-actions bot commented Aug 5, 2024

👋 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 run fastcheck CI which consists a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of default ones by unblocking the steps in your fast-check build on Buildkite UI.

Once the PR is approved and ready to go, please make sure to run full CI as it is required to merge (or just use auto-merge).

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

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

🚀

vllm/model_executor/layers/sampler.py Show resolved Hide resolved
@@ -223,8 +224,7 @@ def _prepare_seq_groups(

if seq_group_metadata.is_prompt:
if sampling_params.seed is not None:
generator = torch.Generator(device=device).manual_seed(
sampling_params.seed)
generator = np.random.default_rng(seed=sampling_params.seed)
Copy link
Contributor Author

@peng1999 peng1999 Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to NumPy generator because the per-request seed random number generation is faster at CPU unless writing a dedicated GPU kernel. Also, it can overlap with other GPU work, reducing the overall latency.

Copy link
Collaborator

@comaniac comaniac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this!

tests/samplers/test_sampler.py Outdated Show resolved Hide resolved
vllm/model_executor/layers/sampler.py Outdated Show resolved Hide resolved
vllm/model_executor/layers/sampler.py Outdated Show resolved Hide resolved
vllm/model_executor/layers/sampler.py Outdated Show resolved Hide resolved
vllm/utils.py Outdated Show resolved Hide resolved
@peng1999
Copy link
Contributor Author

peng1999 commented Aug 5, 2024

The isort and yapf do not agree on the formatting of the import 🤯

isort:

$ isort --check --diff vllm/model_executor/layers/sampler.py 
ERROR: /mnt/workspace/pgw/vllm/vllm/model_executor/layers/sampler.py Imports are incorrectly sorted and/or formatted.
--- /mnt/workspace/pgw/vllm/vllm/model_executor/layers/sampler.py:before        2024-08-05 17:48:41.674248
+++ /mnt/workspace/pgw/vllm/vllm/model_executor/layers/sampler.py:after 2024-08-05 17:53:58.759998
@@ -24,8 +24,8 @@
 from vllm.utils import async_numpy_to_tensor
 
 try:
-    from flashinfer.sampling import (top_k_top_p_sampling_from_probs as
-                                     flashinfer_top_k_top_p_sampling)
+    from flashinfer.sampling import (
+        top_k_top_p_sampling_from_probs as flashinfer_top_k_top_p_sampling)
 except ImportError:
     flashinfer_top_k_top_p_sampling = None

yapf:

$ yapf --diff vllm/model_executor/layers/sampler.py 
--- vllm/model_executor/layers/sampler.py       (original)
+++ vllm/model_executor/layers/sampler.py       (reformatted)
@@ -24,8 +24,8 @@
 from vllm.utils import async_numpy_to_tensor
 
 try:
-    from flashinfer.sampling import (
-        top_k_top_p_sampling_from_probs as flashinfer_top_k_top_p_sampling)
+    from flashinfer.sampling import (top_k_top_p_sampling_from_probs as
+                                     flashinfer_top_k_top_p_sampling)
 except ImportError:
     flashinfer_top_k_top_p_sampling = None

@comaniac
Copy link
Collaborator

comaniac commented Aug 5, 2024

@simon-mo what do we do if isort and yapf are conflicting?

@simon-mo
Copy link
Collaborator

simon-mo commented Aug 5, 2024

Yapf ignore

Copy link
Collaborator

@comaniac comaniac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM

.buildkite/test-pipeline.yaml Outdated Show resolved Hide resolved
vllm/model_executor/layers/sampler.py Show resolved Hide resolved
@peng1999
Copy link
Contributor Author

peng1999 commented Aug 6, 2024

Switched the numpy generator back to torch generator, because the numpy generator is too slow for the original sampler. The flashinfer sampler will become slower but still has improvment in end-to-end test:

Processed prompts: 100%|█████████████| 2048/2048 [01:03<00:00, 32.36it/s, est. speed input: 33138.13 toks/s, output: 4142.27 toks/s]

In the future, we can write a dedicated kernel to improve the random seed generation.

vllm/envs.py Outdated Show resolved Hide resolved
vllm/utils.py Outdated Show resolved Hide resolved
vllm/envs.py Outdated Show resolved Hide resolved
@peng1999
Copy link
Contributor Author

peng1999 commented Aug 8, 2024

I noticed that the top-k-top-p sampler in flashinfer has incompatible semantics compared to vLLM's. vLLM applys top-k first and then top-p (See #1885), while flashinfer applys top-k and top-p filter simultaneously.

Example:

probs = torch.arange(0.2, 0.6, step=0.05, dtype=torch.float32, device="cuda").softmax(dim=-1)[None, :]
# probs: [[0.1042, 0.1096, 0.1152, 0.1211, 0.1273, 0.1339, 0.1407, 0.1479]]
uniform_samples = torch.tensor([[0.51, 0.6, 0.8, 0.9]], device="cuda")
top_k = torch.tensor([4] * 4, device="cuda")
top_p = torch.tensor([0.5] * 4, device="cuda")

top_k_top_p_sampling_from_probs(probs.expand(4, -1).contiguous(), uniform_samples, top_k, top_p)
# output:
# [tensor([4, 5, 6, 7], device='cuda:0', dtype=torch.int32),
#  tensor([True, True, True, True], device='cuda:0')]

while in vllm only 6 and 7 can be selected:

from vllm.model_executor.layers.sampler import _apply_top_k_top_p
_apply_top_k_top_p(probs, p=top_p[:1], k=top_k[:1])
# output: [[  -inf,   -inf,   -inf,   -inf,   -inf,   -inf, 0.1407, 0.1479]]

Given this, the fallback strategy becomes meaningless. I propose to do the following:

  • We keep the original sampler behaviour (the huggingface behaviour) as default
  • Add a VLLM_FLASHINFER_SAMPLER flag for users who accept the changed behaviour to enable faster sampling
  • Clearly document the behaviour difference.

@yzh119
Copy link

yzh119 commented Aug 8, 2024

@peng1999 thanks so much for the notification.
I can add the implementations for first top-k then top-p to flashinfer as well.

@peng1999
Copy link
Contributor Author

@comaniac I think it's now ready to be reviewed.

Copy link
Collaborator

@comaniac comaniac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@comaniac
Copy link
Collaborator

@peng1999 can you look into the CI failure?

@peng1999
Copy link
Contributor Author

@peng1999 can you look into the CI failure?

It seems that when GPTQ is enabled, the logits between inference has some small difference even if the same seed is given. And flashinfer's algorithm is sensitive to small differences, hence the output mismatch.

The following is top-9 probs value and its index using batch=10, seed=523, input="Hello, my name is" on Qwen2-1.5B-GPTQ-Int4 model.

tensor([[0.0141, 0.0109, 0.0092, 0.0078, 0.0077, 0.0072, 0.0068, 0.0065, 0.0063],
        [0.0142, 0.0110, 0.0093, 0.0078, 0.0078, 0.0071, 0.0068, 0.0065, 0.0063],
        [0.0141, 0.0110, 0.0092, 0.0078, 0.0078, 0.0071, 0.0068, 0.0065, 0.0063],
        [0.0142, 0.0110, 0.0092, 0.0078, 0.0078, 0.0071, 0.0067, 0.0065, 0.0063],
        [0.0141, 0.0110, 0.0092, 0.0078, 0.0078, 0.0071, 0.0068, 0.0066, 0.0063],
        [0.0142, 0.0110, 0.0093, 0.0078, 0.0078, 0.0071, 0.0068, 0.0065, 0.0063],
        [0.0141, 0.0110, 0.0092, 0.0078, 0.0077, 0.0071, 0.0067, 0.0066, 0.0063],
        [0.0141, 0.0110, 0.0093, 0.0078, 0.0078, 0.0071, 0.0068, 0.0066, 0.0063],
        [0.0142, 0.0110, 0.0092, 0.0078, 0.0078, 0.0071, 0.0068, 0.0066, 0.0063],
        [0.0141, 0.0110, 0.0092, 0.0078, 0.0078, 0.0071, 0.0068, 0.0066, 0.0063]],
       device='cuda:0')
tensor([[20445,  3757,  6798,   730,  7937,   619,   386, 23016,   350],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016, 11266],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016, 11266],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016, 11266],
        [20445,  3757,  6798,   730,  7937,   619,   386, 23016,   350],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016, 11266],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016, 11266],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016,   350],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016, 11266],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016, 11266]],
       device='cuda:0')

@comaniac Is this behaviour of gptq expected?

@comaniac
Copy link
Collaborator

That's understandable. We could disable FlashInfer sampling in this case. Meanwhile we may want to note somewhere to encourage users to disable it when they found discrepancy (and unacceptable) outputs.

Also cc @robertgshaw2-neuralmagic @mgoin for the GPTQ accuracy issue visibility.

@comaniac comaniac added the ready ONLY add when PR is ready to merge/full CI is needed label Aug 15, 2024
@peng1999
Copy link
Contributor Author

The PP test and 2-Node test seems to have the same problem as GPTQ. But I have problem running the test locally to verify my guess.

@comaniac
Copy link
Collaborator

Hmm I suppose this would be the case everywhere then...I'll then suggest the following:

  1. We disable FlashInfer sampling by default and use the env variable to enable it in this PR.
  2. We could have follow-up PRs to improve GPTQ tests. Specifically they should check logprobs instead of tokens.
  3. Then we re-enable FlashInfer sampling by default.
  4. To avoid changing the API (env variable name, we could use "USE_FLASHINFER_SAMPLER=0/1".

@comaniac comaniac enabled auto-merge (squash) August 16, 2024 02:26
Copy link
Sponsor Collaborator

@mgoin mgoin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@comaniac it looked like he shared top-logprobs already from the gptq test? If it isn't using logprobs, I agree we should change that

vllm/envs.py Outdated Show resolved Hide resolved
@comaniac
Copy link
Collaborator

@comaniac it looked like he shared top-logprobs already from the gptq test? If it isn't using logprobs, I agree we should change that

Yeah ideally we should leverage logprobs for all tests with this issue, but I don't expect it to be done in this PR

Co-authored-by: Michael Goin <michael@neuralmagic.com>
auto-merge was automatically disabled August 17, 2024 01:11

Head branch was pushed to by a user without write access

@comaniac comaniac enabled auto-merge (squash) August 17, 2024 01:33
@comaniac comaniac merged commit f710fb5 into vllm-project:main Aug 19, 2024
64 checks passed
@peng1999 peng1999 deleted the opt-topk branch August 19, 2024 06:06
@yzh119
Copy link

yzh119 commented Aug 20, 2024

And flashinfer's algorithm is sensitive to small differences, hence the output mismatch.

@peng1999 we improved the implementation of top-k renorm kernels (flashinfer-ai/flashinfer#456) and the issues might be resolved in the next release.

yzh119 added a commit to flashinfer-ai/flashinfer that referenced this pull request Aug 20, 2024
Add an option to check whether there are nan inputs.
This PR also removes all `eps` arguments in renorm kernels: previously
we pre-set a eps constant to determine when to stop the binary search,
however, this might not be accuracy when vocabulary size grows (e.g. >=
1e6 in llama3 where our eps might be set to 1e-5).
In this PR, we implement a loop variant which do not rely on any
external eps, and it can help us address some of the issues such as
vllm-project/vllm#7137 (comment) .
zifeitong pushed a commit to zifeitong/vllm that referenced this pull request Aug 20, 2024
Co-authored-by: Michael Goin <michael@neuralmagic.com>
fialhocoelho pushed a commit to opendatahub-io/vllm that referenced this pull request Aug 22, 2024
Co-authored-by: Michael Goin <michael@neuralmagic.com>
omrishiv pushed a commit to omrishiv/vllm that referenced this pull request Aug 26, 2024
Co-authored-by: Michael Goin <michael@neuralmagic.com>
@yzh119
Copy link

yzh119 commented Aug 30, 2024

@peng1999 can you look into the CI failure?

It seems that when GPTQ is enabled, the logits between inference has some small difference even if the same seed is given. And flashinfer's algorithm is sensitive to small differences, hence the output mismatch.

The following is top-9 probs value and its index using batch=10, seed=523, input="Hello, my name is" on Qwen2-1.5B-GPTQ-Int4 model.

tensor([[0.0141, 0.0109, 0.0092, 0.0078, 0.0077, 0.0072, 0.0068, 0.0065, 0.0063],
        [0.0142, 0.0110, 0.0093, 0.0078, 0.0078, 0.0071, 0.0068, 0.0065, 0.0063],
        [0.0141, 0.0110, 0.0092, 0.0078, 0.0078, 0.0071, 0.0068, 0.0065, 0.0063],
        [0.0142, 0.0110, 0.0092, 0.0078, 0.0078, 0.0071, 0.0067, 0.0065, 0.0063],
        [0.0141, 0.0110, 0.0092, 0.0078, 0.0078, 0.0071, 0.0068, 0.0066, 0.0063],
        [0.0142, 0.0110, 0.0093, 0.0078, 0.0078, 0.0071, 0.0068, 0.0065, 0.0063],
        [0.0141, 0.0110, 0.0092, 0.0078, 0.0077, 0.0071, 0.0067, 0.0066, 0.0063],
        [0.0141, 0.0110, 0.0093, 0.0078, 0.0078, 0.0071, 0.0068, 0.0066, 0.0063],
        [0.0142, 0.0110, 0.0092, 0.0078, 0.0078, 0.0071, 0.0068, 0.0066, 0.0063],
        [0.0141, 0.0110, 0.0092, 0.0078, 0.0078, 0.0071, 0.0068, 0.0066, 0.0063]],
       device='cuda:0')
tensor([[20445,  3757,  6798,   730,  7937,   619,   386, 23016,   350],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016, 11266],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016, 11266],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016, 11266],
        [20445,  3757,  6798,   730,  7937,   619,   386, 23016,   350],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016, 11266],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016, 11266],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016,   350],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016, 11266],
        [20445,  3757,  6798,  7937,   730,   619,   386, 23016, 11266]],
       device='cuda:0')

@comaniac Is this behaviour of gptq expected?

Hi @peng1999 would you mind trying flashinfer v0.1.6 and see if the problem still exists?

@peng1999
Copy link
Contributor Author

Hi @peng1999 would you mind trying flashinfer v0.1.6 and see if the problem still exists?

Unfortunately the test still not passed using flashinfer v0.1.6.

Tested locally using VLLM_USE_FLASHINFER_SAMPLER=1 pytest -x tests/quantization/test_cpu_offload.py -k test_cpu_offload_gptq:

E           AssertionError: Results for model='Qwen/Qwen2-1.5B-Instruct-GPTQ-Int4' are not the same with arg1=['--quantization', 'gptq'] and arg2=['--quantization', 'gptq', '--cpu-offload-gb', '1']. arg1_result={'test': 'seeded_sampling', 'text': ' Amanda Bunnell.', 'finish_reason': 'length', 'usage': CompletionUsage(completion_tokens=5, prompt_tokens=5, total_tokens=10)} != arg2_result={'test': 'seeded_sampling', 'text': ' Ruth Yaireen.', 'finish_reason': 'length', 'usage': CompletionUsage(completion_tokens=5, prompt_tokens=5, total_tokens=10)}

tests/utils.py:275: AssertionError

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.

6 participants