-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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][5/N] Fully working chunked prefill e2e #3884
[Core][5/N] Fully working chunked prefill e2e #3884
Conversation
# higher throughput. | ||
self.max_num_batched_tokens = max(max_model_len, 2048) | ||
if enable_chunked_prefill: | ||
# For chunked prefill, choose the well-tuned batch size. |
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.
This is arbitrary value. We should profile again.
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.
I would imagine this is hardware/model dependent. @AgrawalAmey any suggestion for a good default value?
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.
actually, there is a no good default -- all depends on application requirements, model, hw, etc. I have an experimental auto-tuner which sets the maximum chunk size that can satisfy the application requirements. It is pretty straight forward, it just takes tbt target and find out the max token budget that can satisfy it.
@@ -49,17 +50,14 @@ def copy_blocks( | |||
|
|||
|
|||
@dataclass | |||
class TorchSDPAMetadata(AttentionMetadata, PagedAttentionMetadata): | |||
class TorchSDPAMetadata(AttentionMetadataPerStage, PagedAttentionMetadata): |
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.
QQ: Currently, do we run all attention backend in tests?
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.
PR welcomed!
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.
I will create a PR for it
tests/worker/test_model_runner.py
Outdated
assert len(return_prompt_lens) == 0 | ||
|
||
|
||
# SANG-TODO Test chunked prefill case. |
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.
This one is WIP
Initial throughput benchmark on 7B, 1 GPU
I am going to start further benchmark at better machines and higher TP next week |
for the benchmarks, does it enable cuda-graph? also worth noting the detail of benchmarks (i.e. type of GPU and benchmark setup) |
@rkooo567 If this PR is ready, I'm happy to test it with some benchmarking as well! |
@ywang96 I think it may actually work. The test failures now may be just test specific issues. The basic_correctness_test seems pass when I ran it (including cuda graph + tp > 1). @scv119 actually the original RFC doesn't include cuda graph yet after talking with Woosuk (he mentioned he didn't see much improvement in cuda graph if batch size > 256). I will run our internal with decode-only cuda graph (oss status quo) vs whole cuda graph to see the actual impact and do a follow up. Have you observed big perf difference here? |
also want to emphaisze the benchmark above runs 1 A10 GPU + llama 7B. It was just a dry run. I will do more serious benchmark next week, and we may need cuda graph for chunked prefill to (now it is enabled only when batch only contains decode) |
my sense is cuda-graph will help with tp > 1 cases regardless of chunk-prefill enabled or not; but better just run some benchmarks. |
@@ -49,17 +50,14 @@ def copy_blocks( | |||
|
|||
|
|||
@dataclass | |||
class TorchSDPAMetadata(AttentionMetadata, PagedAttentionMetadata): | |||
class TorchSDPAMetadata(AttentionMetadataPerStage, PagedAttentionMetadata): |
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.
PR welcomed!
vllm/attention/backends/xformers.py
Outdated
if num_prefill_tokens > 0: | ||
prefill_meta = attn_metadata.prefill_metadata | ||
assert prefill_meta is not None |
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.
Should we the invariant check in the AttentionMetadata
data class?
if num_prefill_tokens > 0: | |
prefill_meta = attn_metadata.prefill_metadata | |
assert prefill_meta is not None | |
if prefill_meta: = attn_metadata.prefill_metadata: | |
assert prefill_meta is not None |
Python 3.8's valrus operator is very useful here. But it does need num_prefill_tokens
invariant to be verified somewhere else?
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.
moved invariant check and used warlus operator
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.
I'm not a fan of Walrus operator in python and I believe it's pretty controversial. I would vote for limiting it's use in vLLM. @simon-mo what do you think?
But this is a small thing. I don't think we should block any PR with this.
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.
@WoosukKwon @zhuohan123 would love your feedback on this interface change where the new AttentionMetadata
can contains both prefill and decode stage metadata. I think this is a necessary change but would like to hear your thought on the interface design.
vllm/attention/backends/abstract.py
Outdated
slot_mapping: torch.Tensor | ||
kv_cache_dtype: str |
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.
group these two separately from the prefill/decode. i guess it make sense here as a general arguments instead of the per stage ones.
# higher throughput. | ||
self.max_num_batched_tokens = max(max_model_len, 2048) | ||
if enable_chunked_prefill: | ||
# For chunked prefill, choose the well-tuned batch size. |
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.
I would imagine this is hardware/model dependent. @AgrawalAmey any suggestion for a good default value?
attn_metadata.prompt_lens_tensor, | ||
attn_metadata.context_lens, | ||
attn_metadata.max_subquery_len, | ||
prefill_meta.block_tables, |
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.
@rkooo567 you mentioned this kernel is really slow right? should we show some warning if someone uses this backend with chunked prefills?
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.
Hmm feel like that's a bit weird because there's no alternative option. I think we should probably just mark it as experimental feature for 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.
I can also do some additional e2e benchmark. When I ran it with 7B, the perf was actually not very different
else: | ||
# prefix-enabled attention | ||
# TODO(Hai) this triton kernel has regression issue (broke) to | ||
# deal with different data types between KV and FP8 KV cache, | ||
# to be addressed separately. | ||
output = PagedAttention.forward_prefix( | ||
output[:num_prefill_tokens] = PagedAttention.forward_prefix( |
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.
with flash_attn_varlen_func, we can do both prefill and decode attention together right?
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.
Yes! I heard that it doesn't make much perf change, but we can iterate on this once the API supports larger page size
# for higher throughput. | ||
self.max_num_batched_tokens = max(max_model_len, 2048) | ||
if enable_chunked_prefill: | ||
logger.info("Chunked prefill is enabled (EXPERIMENTAL).") |
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.
cc @AgrawalAmey
All comments are addressed, and I believe the tests are passing 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.
@rkooo567 Let's chat offline about this PR? My high-level comment: with chunked prefill, we should no longer distinguish prefill and decode outside the attention layer, which should simplify the code significantly.
assert query.shape[0] == num_prefill_tokens | ||
assert decode_query.shape[0] == num_decode_tokens | ||
|
||
if prefill_meta := attn_metadata.prefill_metadata: |
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.
Small Python style question: is :=
widely used now? I personally prefer the more old-school way.
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.
This was based on simon's feedback. #3884 (comment). Lmk if you want to just revert this
async_handles = [] | ||
for key, value in metadata_list: | ||
if isinstance(value, TensorMetadata): | ||
tensor = tensor_dict[key] | ||
torch.distributed.broadcast(tensor, src=src, group=group) | ||
async_handles.append( | ||
torch.distributed.broadcast(tensor, | ||
src=src, | ||
group=group, | ||
async_op=True)) | ||
for async_handle in async_handles: | ||
async_handle.wait() | ||
|
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.
Thanks for optimizing this!
vllm/worker/model_runner.py
Outdated
class PreparePromptMetadata(NamedTuple): | ||
input_tokens: List[int] | ||
input_positions: List[int] | ||
attn_metadata: Optional[AttentionMetadataPerStage] | ||
prompt_lens: List[int] | ||
subquery_lens: List[int] | ||
lora_index_mapping: List[int] | ||
lora_prompt_mapping: List[int] | ||
lora_requests: Set[LoRARequest] | ||
multi_modal_input: Optional[torch.Tensor] | ||
slot_mapping: List[int] | ||
|
||
|
||
class PrepareDecodeMetadata(NamedTuple): | ||
input_tokens: List[int] | ||
input_positions: List[int] | ||
attn_metadata: Optional[AttentionMetadata] | ||
lora_index_mapping: List[int] | ||
lora_prompt_mapping: List[int] | ||
lora_requests: Set[LoRARequest] | ||
slot_mapping: List[int] |
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.
How is chunked prefill handled here?
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.
This addresses #3884 (comment) comment. It is combined inside prepare_input_tensors
@zhuohan123 I believe the current style is the best iterative step. (it is also the way it is done in our internal repo). I'd love to do unification as a followup, but I feel like we should do it step by step instead of all in the current PR;
Let's talk more in details tmrw! |
Discussed offline.
|
attn_metadata.kv_cache_dtype, | ||
self.num_kv_heads, | ||
self.scale, | ||
self.alibi_slopes, | ||
kv_scale, | ||
) | ||
assert out.shape == output[num_prefill_tokens:].shape | ||
output[num_prefill_tokens:] |
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.
should be output[num_prefill_tokens:] = out
here?
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.
oops that;s correct. I don't know how the test passes...
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.
oh it is handled 8afca50
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.
We didn't enable much test for cpu path, we will try to add step by step later. thanks for your confirming!:)
Can we optimize this to allow many more chunks at a time? I thought the main optimization of DeepSpeed-MII was the chunked prefilling, which greatly speeds up the context processing and therefore the throughput. |
@casper-hansen that's totally doable. Do you happen to know the perf benefits it can bring or a reference sections in a paper that explaisn it? I followed the policy from https://arxiv.org/abs/2308.16369. Also cc @AgrawalAmey for thoughts |
This PR is a part of the RFC #3130.
This PR enables chunked prefill e2e. Note that chunked prefill is an experimental feature now (though it is actively used within Anyscale), and I will start serious benchmark with this PR.
The feature can be enabled by using
enable_chunked_prefill
. The chunking is done based onmax_num_batched_tokens
(a.k.a token budget). This is the same way as described in SARATHI paper https://arxiv.org/abs/2308.16369. It also means we can have up to maximum 2 chunked prefill at any given time.This PR
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!