-
-
Notifications
You must be signed in to change notification settings - Fork 5k
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
[Bugfix] Enable chunked-prefill and prefix cache with flash-attn backend #6144
Conversation
5a42c51
to
3963c76
Compare
Hi @rkooo567, could you please take a look at this pull request? Thanks! |
I make a try and output consistency cannot be guaranteed under some circumstances. |
May I know more about the failed case? How could I reproduce the failure? |
It was not easy to examplify. I test it using Locust with high concurrency and some of the outputs become weird. |
I will take a look at it tomorrow. |
f5ca6a8
to
77cff4d
Compare
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.
Overall LGTM. btw can you add a test case?
vllm/worker/model_runner.py
Outdated
@@ -277,7 +277,8 @@ def add_seq_group(self, seq_group_metadata: SequenceGroupMetadata): | |||
prefix_cache_hit = (computed_block_nums is not None | |||
and len(computed_block_nums) > 0 | |||
and self.sliding_window is None and is_prompt) | |||
if self.chunked_prefill_enabled and prefix_cache_hit: | |||
if (self.chunked_prefill_enabled and prefix_cache_hit | |||
and self.attn_backend.get_name() != "flash-attn"): | |||
raise RuntimeError( | |||
"chunked prefill cannot be used with prefix caching now.") |
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.
Chunked prefill and prefix caching can only be used simultaneously with flash-attn backend
vllm/worker/model_runner.py
Outdated
# cached parts of the sequence. | ||
# - prefix_cache_len >= seq_len: | ||
# - it means the current partial sequence is fully cache | ||
# hited, and no further computation is needed. |
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.
# hited, and no further computation is needed. | |
# hit, and no further computation is needed. |
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.
Looks pretty clean! Can you add unit tests? I think it'd be great to have 1 for worker/test_model_runner.py (just to verify metadata) and 1 e2e test
vllm/worker/model_runner.py
Outdated
@@ -277,7 +277,8 @@ def add_seq_group(self, seq_group_metadata: SequenceGroupMetadata): | |||
prefix_cache_hit = (computed_block_nums is not None | |||
and len(computed_block_nums) > 0 | |||
and self.sliding_window is None and is_prompt) | |||
if self.chunked_prefill_enabled and prefix_cache_hit: | |||
if (self.chunked_prefill_enabled and prefix_cache_hit | |||
and self.attn_backend.get_name() != "flash-attn"): | |||
raise RuntimeError( | |||
"chunked prefill cannot be used with prefix caching now.") |
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.
Update the error message that it is only suppored by flash attn backend (also please write how to enable flash attn backend. I.e., VLLM_ATTENTION_BACKEND=FLASH_ATTN
)
77cff4d
to
083125e
Compare
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.
Overall LGTM! Just some miner comments for comments and refactoring.
vllm/worker/model_runner.py
Outdated
if (self.chunked_prefill_enabled and prefix_cache_hit | ||
and self.attn_backend.get_name() != "flash-attn"): | ||
raise RuntimeError( | ||
"chunked prefill cannot be used with prefix caching now.") | ||
"Chunked prefill and prefix caching can only be used " | ||
"simultaneously with flash-attn backend, try switching " | ||
"to flash-attn backend by setting the environment variable " | ||
"\"VLLM_ATTENTION_BACKEND=FLASH_ATTN\"") |
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.
prepare_input
is now backend agnostic. Can you put this checker to vllm/attention/backends/flashinfer.py
and vllm/attention/backends/utils.py
? For example in utils.py:
def _add_seq_group(
self, inter_data: "ModelInputForGPUBuilder.InterDataForSeqGroup",
chunked_prefill_enabled: bool):
is_prompt = inter_data.is_prompt
block_tables = inter_data.block_tables
computed_block_nums = inter_data.computed_block_nums
if chunked_prefill_enabled and inter_data.prefix_cache_hit:
raise RuntimerError(...)
vllm/worker/model_runner.py
Outdated
# When prefix caching meets chunked prefill, we would be in | ||
# the following three conditions: | ||
# | ||
# - prefix_cache_len <= context_len: | ||
# - do normal chunked prefill and nothing special | ||
# - context_len < prefix_cache_len < seq_len: | ||
# - advance the context_len to seq_len to perform non- | ||
# cached parts of the sequence. | ||
# - prefix_cache_len >= seq_len: | ||
# - it means the current partial sequence is fully cache | ||
# hit, and no further computation is needed. |
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.
nit
# When prefix caching meets chunked prefill, we would be in | |
# the following three conditions: | |
# | |
# - prefix_cache_len <= context_len: | |
# - do normal chunked prefill and nothing special | |
# - context_len < prefix_cache_len < seq_len: | |
# - advance the context_len to seq_len to perform non- | |
# cached parts of the sequence. | |
# - prefix_cache_len >= seq_len: | |
# - it means the current partial sequence is fully cache | |
# hit, and no further computation is needed. | |
# When prefix caching meets chunked prefill, we would be in | |
# one of the following three cases: | |
# - prefix_cache_len <= context_len: | |
# Do normal chunked prefill | |
# - context_len < prefix_cache_len < seq_len: | |
# Advance the context_len to seq_len to prefill non- | |
# cached parts of the sequence. | |
# - prefix_cache_len >= seq_len: | |
# The current partial sequence is fully cache hit, | |
# and no further computation is needed. |
Another way is embedding the comments to each branch:
if context_len < prefix_cache_len < seq_len:
# prefix_cache_len <= context_len: Do normal chunked prefill
....
elif seq_len <= prefix_cache_len:
# context_len < prefix_cache_len < seq_len: ...
...
else:
# prefix_cache_len >= seq_len:: ...
pass
I'm fine with either way
|
||
seq_lens: List[int] = [] | ||
seq_group_metadata_list: List[SequenceGroupMetadata] = [] | ||
block_tables = {0: list(range(128))} |
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.
Can you note the block size here for convenience?
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.
Fixed。
tests/worker/test_model_runner.py
Outdated
assert len(input_tokens) == 220 | ||
assert len(input_positions) == 220 |
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.
Can you note how 220 was calculated?
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.
Addressed.
d4d4f8e
to
304ba45
Compare
Hi @Juelianqvq could you take another try on this PR to see if previous problems you meet can be reproduced now. @comaniac Could you please help review changes in this PR again? thanks! |
These two failed test case is not caused by this PR. |
@sighingnow Let me test it with our inner dataset consists of high concurreny under differenct scenerios next Monday and give you feedback based on that ASAP. Have you tested with blockmanager v2 yet? |
Make sure you are working on vllm version with PR #7018 included.
Haven't tested yet, but there should be no difference. |
@sighingnow Unfortunately, cudaError is fixed but still can generate garbage sometimes which I've undergone before and made a work-around in #6819. If you don't have such dataset, you can reduce |
@Juelianqvq is it possible to provide a simple unit tests for us? |
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 PR looks good to me, but the latest comment concerns me if there are still some other issues.
@sighingnow is there any good way to create an e2e test?
@Juelianqvq Could you share the command/script that you can reproduce the failure? even if your dataset cannot be available.
@rkooo567 An e2e test cannot help the concerns here as we cannot reproduce. The only condition that may lead to incorrect result is hash collision (two non-equal sequences share the same pages). To reproduce such error, it is highly dataset dependent. |
@sighingnow Nothing special than add --enable-chunked-prefill -- enable-prefix-caching on finetuned Llama-2-13B. @sighingnow @rkooo567 Let me clarify what I have done and what problem it is, it is not related to 2-different-sequences but 1-same-sequence which helps me to discover the bug. Given that max-num-batched-tokens set to 512, if I pass a 570 prompt here, it will be chunked into 2 pieces for scheduling, 512 + 58 right? And what will happen if I pass the same request again? Of course, 570 is not fully cached and it will be 511 cached+ 1 recompute + 58 / blocksize * blocksize cached + the rest recompute right? But with this PR, the output becomes shorter than the first round. That's what I met and thought for nearly 2 weeks. I have not understood the root cause here but I can give you some hints. What confused me is that a sequence being chunked into different scheduling can have changeable cached length, that's the key point and it is related to block manager. Specifically, 570 is split into 512 + 58, and cached length is calculated based on 570(e.g 352) at first and takes effect on 512 part, but after 512 is calculated, cached length of 58(like 384, forget the real number) does not match before (e.g. 352). So I made a work-around to fix the cached length and a little modification on block manager. Hope to find out the real reason pointed out here. |
@sighingnow Chunked prefill is connected with chunk size which can be inferred from max-num-batched-tokens, so prompt length greater or less than this value should be included. The reason you have missed to keep at least 1 token for prefill is that you do not select greater prompt length which is always fine in 1-concurrency e2e, but it will cause assertion error in Moreover, v2-block-manager is another story. |
Signed-off-by: Tao He <linzhu.ht@alibaba-inc.com>
90793d5
to
099ab34
Compare
The block manager v1 case has been fixed by 099ab34 |
See also: #7619 |
099ab34
to
7a9dfe5
Compare
Signed-off-by: Tao He <linzhu.ht@alibaba-inc.com>
7a9dfe5
to
a043643
Compare
Will have a try tomorrow morning. |
@sighingnow Thanks for the great catch, have been tested with custom dataset and all sounds good! |
Thanks for the feedback! |
As the flash-attn becomes the default attention backend on NV GPUs (thanks to vllm-flash-attention fork), it is possible to enable chunked prefill with prefix caching at the same time in vLLM.
FIX #xxxx (link existing issues this PR will resolve)
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:
format.sh
to format your code.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:
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.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!