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] Implementing disaggregated prefilling, and caching KV cache in CPU/disk/database. #8498

Open
wants to merge 1,869 commits into
base: main
Choose a base branch
from

Conversation

KuntaiDu
Copy link
Collaborator

@KuntaiDu KuntaiDu commented Sep 16, 2024

TL; DR: implemented disaggregated prefill with <100 core line change (and most of them are comments)

This PR is a continuation of PR #6170 , with a new design that allows future extension.

Current supported applications:

  • Disaggregated prefilling. Check examples/disagg_prefill/disagg_prefill_example.sh for an example, and benchmarks/disagg_prefill for various benchmarks. Benchmarking script are all one-click runnable (after setting HF_TOKEN)
  • Connecting to a KV cache storage service LMCache. Examples TBD.

Two roles: KV provider (e.g. prefill vLLM instance) and KV consumer (e.g. decode vLLM instance)

  • Provider side: insert: insert a KV cache to a buffer, so that it can be transferred upon request
  • Consumer side: drop_select: select a KV cache based on tokens, transfer the selected KV, and drop this KV out from the buffer

Example workflow (the buffer in the following figure is the same as insert)
image


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.

Adding or changing kernels

Each custom kernel needs a schema and one or more implementations to be registered with PyTorch.

  • Make sure custom ops are registered following PyTorch guidelines: Custom C++ and CUDA Operators and The Custom Operators Manual
  • Custom operations that return Tensors require meta-functions. Meta-functions should be implemented and registered in python so that dynamic dims can be handled automatically. See above documents for a description of meta-functions.
  • Use torch.libary.opcheck() to test the function registration and meta-function for any registered ops. See tests/kernels for examples.
  • When changing the C++ signature of an existing op, the schema must be updated to reflect the changes.
  • If a new custom type is needed, see the following document: Custom Class Support in PT2.

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 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 do one of these:

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

🚀

@ahg-g
Copy link

ahg-g commented Sep 18, 2024

/cc

@rkooo567
Copy link
Collaborator

@KuntaiDu this is ready to review?

@KuntaiDu
Copy link
Collaborator Author

@KuntaiDu this is ready to review?

Final bug fix on TP case when connecting to LMCache. Will ping you when it's ready for review (most likely tomorrow)

@zeroorhero
Copy link

It‘s a good job!

@KuntaiDu KuntaiDu added the ready ONLY add when PR is ready to merge/full CI is needed label Sep 20, 2024
@KuntaiDu
Copy link
Collaborator Author

@rkooo567 Now this code is ready for review

@KuntaiDu
Copy link
Collaborator Author

Lol huggingface is down and I will rerun the test tomorrow.

@kuangdao
Copy link

mark

1 similar comment
@lixiaolx
Copy link

mark

@KuntaiDu
Copy link
Collaborator Author

KuntaiDu commented Sep 22, 2024

All tests are now passed!

@zeroorhero
Copy link

@KuntaiDu hi !According to your commit, I implemented valkey (supporting TCP and rdma) as the kv cache storage pool in the prefill and decode nodes(#8724). Due to license reasons, I implemented valkey instead of redis, and valkey also supports rdma after version 8.0. Can you give me some suggestions?

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.

First review the changes in the vLLM core.

vllm/utils.py Outdated Show resolved Hide resolved
vllm/utils.py Outdated Show resolved Hide resolved
vllm/utils.py Outdated Show resolved Hide resolved
vllm/worker/worker.py Outdated Show resolved Hide resolved
vllm/worker/worker_base.py Outdated Show resolved Hide resolved
vllm/worker/model_runner.py Outdated Show resolved Hide resolved
vllm/worker/model_runner.py Outdated Show resolved Hide resolved
vllm/worker/model_runner.py Outdated Show resolved Hide resolved
vllm/worker/model_runner.py Outdated Show resolved Hide resolved
Comment on lines 1070 to 1071
- 2 * tp * pp
- Why: both prefill vLLM and decode vLLM is in the world
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does that mean we now only support 1-prefill + 1-decode?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes for this PR. Support on multi prefil and multi decode vllm instances with different tp/pp is on the roadmap but not in this PR.

Copy link
Collaborator Author

@KuntaiDu KuntaiDu Sep 24, 2024

Choose a reason for hiding this comment

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

I guess you are wondering if this architecture is scalable enough to support XpYd scenario. It can. Here is a planned architecture:

(prefill worker --- database frontend) --- database backend --- (database frontend --- decode worker)

The instances in the same bracket share the same world in torch.distributed.

Basically for XpYd & multiple prefill multiple decode worker scenario, I am planning to not connect prefill worker to decoder worker. Instead, I will connect them to a database and let the database handle all the network topology related optimizations and let it handle different tp/pp conversion.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So in summary if we want to achieve XpYd, then we should separately configure Xp workers and Yd workers. Can you elaborate how to configure Xp workers as an example? I suppose we will only configure KV producer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Example architecture:
image

Each gray box is inside the same torch.distributed world.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(And LMCache can be one example solution for the database. It implements this frontend and backend, and the frontend placement can be in various places (CPU, GPU, disk)).

Choose a reason for hiding this comment

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

Right now, to configure Xp workers and Yd workers will be quite messy in parallel state. For example, when we have 4 prefill workers(TP=4) and 2 decode workers(TP=2), world size will not be 4 * 2 here, and _DISAGG tuples will not be one to one. Another issue will occur in torch_distributed_pipe. Since send operations and recv operations will be doubled when prefill has twice worker as decode.

Even with a database, the database frontend will need to manage the difference between prefill workers and decode workers, and perform concat or scatter operations when necessary.

Choose a reason for hiding this comment

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

Example architecture: image

Each gray box is inside the same torch.distributed world.

Will the latency (connecting to a db) be a concern?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, but the overhead is engineeringly optimizable by, e.g., pipelining the send/recv of the KV cache with model forwarding, or by using RDMA database, etc.

Rest assured that we are planning to support both one-hop connection (i.e. prefiller directly connected to decoder) and two-hop connection (i.e. prefiller --- database --- decoder).

@KuntaiDu
Copy link
Collaborator Author

@KuntaiDu hi !According to your commit, I implemented valkey (supporting TCP and rdma) as the kv cache storage pool in the prefill and decode nodes(#8724). Due to license reasons, I implemented valkey instead of redis, and valkey also supports rdma after version 8.0. Can you give me some suggestions?

Thank you (rdma yes!). I left some comments to your PR.

@zeroorhero
Copy link

@KuntaiDu hi !According to your commit, I implemented valkey (supporting TCP and rdma) as the kv cache storage pool in the prefill and decode nodes(#8724). Due to license reasons, I implemented valkey instead of redis, and valkey also supports rdma after version 8.0. Can you give me some suggestions?

Thank you (rdma yes!). I left some comments to your PR.

Thanks!

@pizhenwei
Copy link

comments

Hi @KuntaiDu

I'm the author of Valkey Over RDMA, please feel free to contact me [email protected] on any issue or suggestion.

@KuntaiDu
Copy link
Collaborator Author

@comaniac I have fixed the comments. Thank you for your review 😄. As for the hidden states send/recv and sampler output send/recv, I need to do some extra model runner condition checks, and I will make a follow-up PR to do this (this PR is already huge).

bypass_model_exec = False
if self.need_recv_kv(model_input, kv_caches):
hidden_or_intermediate_states, bypass_model_exec, model_input = \
get_disagg_group().recv_kv_caches_and_hidden_states(

Choose a reason for hiding this comment

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

Just curious. Is this recv function blocking?

It seems to me that adding an independent thread that constantly waiting for kvcache might hide the communication cost, but I am not sure if this is feasible under current block manager logic.

Copy link
Collaborator Author

@KuntaiDu KuntaiDu Sep 24, 2024

Choose a reason for hiding this comment

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

Yeah currently it is blocking (and it is an intentional design choice).
Reasons behind this design:

  • Transfer delay is not significant (~5ms). Most of the overheads in the implementation is tokenizing the whole request twice and de-tokenizing the first token twice (~20ms). Optimizing other overheads will give us more perf improvement.
  • Non-blocking KV transfer requires adding GPU buffer at decoding vLLM side. Since decoding is memory-bounded, this is not ideal (as buffering reduces # of available GPU memory for decoding KV caches).

Choose a reason for hiding this comment

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

  • Transfer delay is not significant (~5ms). Most of the overheads in the implementation is tokenizing the whole request twice and de-tokenizing the first token twice (~20ms).

Thanks for this helpful information! I agree with your observation. Skipping sampling & output processor in prefill side and sending kvcache immediately after hidden states will be more significant.

@sjnaj
Copy link

sjnaj commented Sep 24, 2024

It would be nice if the decode part could support non-blocking reception and continious batching.
For example, when executing the step function on a batch, according to the maximum capacity obtained by the profile, the required requests can be obtained from a queue that stores some transfer completed (prefetched)kvcache and other infos, then add them to the batch.

@artetaout
Copy link

when the consumer receives a complete new request , should it work or reject it?in this case, should we add some warnings?

@KuntaiDu
Copy link
Collaborator Author

It would be nice if the decode part could support non-blocking reception and continious batching. For example, when executing the step function on a batch, according to the maximum capacity obtained by the profile, the required requests can be obtained from a queue that stores some transfer completed (prefetched)kvcache and other infos, then add them to the batch.

The goal of this PR is to create minimum core change so that people can easily understand the code when review it. We can add this optimization in future PRs.

@KuntaiDu
Copy link
Collaborator Author

when the consumer receives a complete new request , should it work or reject it?in this case, should we add some warnings?

It will redo the prefill. Currently we won't show a warning, but we do expect some logging and warning mechanism in the future.

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.

Took a pass to the kv transfer implementation. Overall is clean is easy to follow. Will take a deeper look at the performance later.

Meanwhile, @youkaichao could you take a look at the parallel states? The current way of handling torch.distributed is a bit messy, but I don't have a better way to deal with it in my mind.

@rkooo567 please also take a look at the parallel states. I believe the current implementation would have some issues with Ray integration.

vllm/utils.py Outdated Show resolved Hide resolved
Comment on lines 1070 to 1071
- 2 * tp * pp
- Why: both prefill vLLM and decode vLLM is in the world
Copy link
Collaborator

Choose a reason for hiding this comment

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

So in summary if we want to achieve XpYd, then we should separately configure Xp workers and Yd workers. Can you elaborate how to configure Xp workers as an example? I suppose we will only configure KV producer?

benchmarks/benchmark_serving.py Outdated Show resolved Hide resolved
vllm/distributed/kv_transfer/kv_lookup_buffer/base.py Outdated Show resolved Hide resolved
@@ -0,0 +1,444 @@
"""vLLM distributed KV cache transfer API.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any planned adaptor in the future?

Copy link
Collaborator Author

@KuntaiDu KuntaiDu Sep 24, 2024

Choose a reason for hiding this comment

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

Personally I feel vLLM will only have one adapter (not sure if other guys will implement new adapters in vLLM tho). But I do expect that other applications (like other serving engines, or a KV database application) implement their own adapters to integrate the transferred KV caches to their application workflow.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see that's interesting. So you're expecting someone to just from vllm.distributed import kv_transfer, and only use the kv_transfer for other frameworks?

Copy link
Collaborator Author

@KuntaiDu KuntaiDu Sep 25, 2024

Choose a reason for hiding this comment

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

Yes. I am expecting different applications to follow the same set of kv_transfer protocol so that they can communicate with vLLM (by, for example, "pretending" to be a vLLM decode instance).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also kv_transfer library is intentionally designed in a way that it has no code dependency on vLLM so that other applications can use it.

@KuntaiDu
Copy link
Collaborator Author

@comaniac Thanks for reviewing! Code changes done. Also pinged Kaichao for further review on parallel_state.py.

Performance-wise, here are some bottlenecks I know (these bottlenecks are sorted by how significant they are).

  • In current workflow (shown in the Figure), the decode vLLM will start to run after first token being generated by prefill instance, and decode vLLM instance will also generate the first token.
    • Issue: tokenize for the full sequence twice, detokenize the first token twice
    • Overhead: ~20ms.
    • Solution: detokenization overhead can be fixed easily (just send SamplerOutput to the decode vLLM), tokenization is also fixable but not easy.
  • Current recv is blocking
    • Overhead: ~5ms.
    • Not easy to fix:
      • A strawmen solution: add a buffer at the decode vLLM side and let it pre-fetch the KV cache
      • Not good: adding the buffer in GPU reduces the KV cache space for decode vLLM (and decoding phase is GPU-memory-bound). Adding the buffer in CPU will slow down the distributed communication (cannot go through NVLink).
  • Current KV cache lookup operation is O(n), n is # of cached KV caches.
    • Overhead: not sure but won't be large (the buffer size won't be (and shouldn't be) large in order to leave more GPU memory space for inference).
    • Easy to fix but needs an efficient hashing logic.

image

@mergify mergify bot added the needs-rebase label Nov 19, 2024
russellb and others added 16 commits November 19, 2024 21:05
Signed-off-by: Kuntai Du <[email protected]>
Copy link

mergify bot commented Nov 20, 2024

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @KuntaiDu.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify bot added the needs-rebase label Nov 20, 2024
@KuntaiDu
Copy link
Collaborator Author

lol something happened when I tried to sign off. I'll fix it tmrw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci/build documentation Improvements or additions to documentation frontend needs-rebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.