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 300 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!

@ShangmingCai
Copy link
Contributor

ShangmingCai commented Oct 16, 2024

Is this new feature compatible with continuous batching? I ran a test and found that when a request has not been finished yet if a new request comes in, it seems that an assertion error

vllm/vllm/engine/llm_engine.py

Lines 1038 to 1039 in 7abba39

assert len(seq_group_metadata_list) == len(
scheduler_outputs.scheduled_seq_groups)
will be reported.

It looks like when one of the requests ends, seq_group_metadata_list is not able to sync up in time and thus diverges from scheduler_outputs.scheduled_seq_groups.

@KuntaiDu
Copy link
Collaborator Author

Is this new feature compatible with continuous batching? I ran a test and found that when a request has not been finished yet if a new request comes in, it seems that an assertion error

vllm/vllm/engine/llm_engine.py

Lines 1038 to 1039 in 7abba39

assert len(seq_group_metadata_list) == len(
scheduler_outputs.scheduled_seq_groups)

will be reported.
It looks like when one of the requests ends, seq_group_metadata_list is not able to sync up in time and thus diverges from scheduler_outputs.scheduled_seq_groups.

Let me test it again -- this issue did not happen previously but maybe some recent changes in vLLM is affecting my current disagg prefill implementation.

@KuntaiDu
Copy link
Collaborator Author

@youkaichao Please review >.<

@youkaichao youkaichao self-assigned this Oct 24, 2024
@youkaichao
Copy link
Member

will review this week.

@zeroorhero
Copy link

Is this new feature compatible with continuous batching? I ran a test and found that when a request has not been finished yet if a new request comes in, it seems that an assertion error

vllm/vllm/engine/llm_engine.py

Lines 1038 to 1039 in 7abba39

assert len(seq_group_metadata_list) == len(
scheduler_outputs.scheduled_seq_groups)

will be reported.
It looks like when one of the requests ends, seq_group_metadata_list is not able to sync up in time and thus diverges from scheduler_outputs.scheduled_seq_groups.

Let me test it again -- this issue did not happen previously but maybe some recent changes in vLLM is affecting my current disagg prefill implementation.

@KuntaiDu hi, this indeed should be a bug. I discovered it when adapting valkey. When decode fails to receive kv cache, this bug occurs. Then I rolled back to the latest version of this pull request. To simulate the failure of decode to receive data, I made changes to the code at this location in the file vllm/distributed/kv_transfer/vllm_adapter.py.
bug1

And it is very easy to reproduce when executing the benchmark.

python3 benchmark_serving.py --backend vllm --dataset-name random --model /root/lcq/model/Llama-2-7b-hf --tokenizer /root/lcq/model/Llama-2-7b-hf --random-input-len 128 --random-output-len 8 --request-rate 1 --num-prompts 8

@liweiqing1997
Copy link

Could any experts discuss how to run CUDA computation and NCCL communication independently and simultaneously?

My goal is to hide NCCL transfer time within the model's forward computation. Therefore, I am using the main thread for CUDA computation in the forward pass, while a separate thread takes care of NCCL transfers for the P and D nodes. The computation and communication are executed in different CUDA streams.

However, according to the results from Nsight, there are significant time gaps between operations in CUDA computation. It seems that NCCL transfers and CUDA matrix computations are competing for resources and thus cannot effectively run concurrently.

Is this conclusion correct? How can we ensure that NCCL transfers and CUDA computations truly run simultaneously?
Results of the current independent thread scheme:

image

Results of synchronous NCCL transmission within the scheduling cycle without using independent threads.
image

@KuntaiDu
Copy link
Collaborator Author

Could any experts discuss how to run CUDA computation and NCCL communication independently and simultaneously?

My goal is to hide NCCL transfer time within the model's forward computation. Therefore, I am using the main thread for CUDA computation in the forward pass, while a separate thread takes care of NCCL transfers for the P and D nodes. The computation and communication are executed in different CUDA streams.

However, according to the results from Nsight, there are significant time gaps between operations in CUDA computation. It seems that NCCL transfers and CUDA matrix computations are competing for resources and thus cannot effectively run concurrently.

Is this conclusion correct? How can we ensure that NCCL transfers and CUDA computations truly run simultaneously? Results of the current independent thread scheme:

image

Results of synchronous NCCL transmission within the scheduling cycle without using independent threads. image

Not sure if this will work, but I heard that somebody fixed similar issue by interleavingly insert memcpy kernel during model forwarding.

@ShangmingCai
Copy link
Contributor

@KuntaiDu Is it correct that TorchDistributedPipe be implemented without heartbeats? I find that if the producer and consumer not communicating for a period of time, the gloo connection will be closed and further requests will kill the consumer service. Or should we need a greater timeout?

maybe_disagg_rank = rank + world_size
logger.debug("rank %d is KV consumer, adjust it to %d", rank,
maybe_disagg_rank)

torch.distributed.init_process_group(
Copy link
Member

Choose a reason for hiding this comment

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

we need to remove this line, and bypass the global world group. then disagg_group can have a different port to initialize, and different world size, etc. we don't even need to change this file.

Copy link

mergify bot commented Nov 3, 2024

This pull request has merge conflicts that must be resolved before it can be
merged. @KuntaiDu please rebase it. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

Copy link
Member

@youkaichao youkaichao 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 the great pr and sorry for the long delay!

I think we can simplify the code a lot, by allowing the disagg_group to have totally different initialization parameters. the user interface would be:

vllm serve ... --role [standalone, cache_produer, cache_consumer] --disagg-ratio XpYd --disagg-rank 0...(X+Y-1)

and you can have a separate disagg_config under the newly added vllm_config , which is directly passed to the model runner after #9938 .

# Sending KV cache in distributed KV cache transfer setting
# NOTE: the send operation is non-blocking
if self.need_send_kv(model_input, kv_caches):
get_disagg_group().send_kv_caches_and_hidden_states(
Copy link

Choose a reason for hiding this comment

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

does this function send all layers kv-cache to 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 if pipeline parallism == 1. For pipeline parallism > 1 it will only send out the layers corresponding to this process.

@KuntaiDu
Copy link
Collaborator Author

KuntaiDu commented Nov 7, 2024

Thanks for @youkaichao 's review! As PR #10072 is merged, now it is the time to update the implementation.

Roadmap for this small refactor:

  • move configuration to vllmconfig
  • adjust distributed initialization method
  • adjust the abstraction --- now we call anything an "connector", which basically means a connection between P and D instance. The connection can be done via torch.distributed, or by third-party library like LMCache and Mooncake
  • fatal bugfix @zeroorhero @ --- fix the crash when drop-select fails

We are also aware other bugs and feature requests --- we will fix them in future PRs.

  • torch.distributed heartbeat
  • Transfer the generated tokens in disagg prefill to bypass token sampling in decode instance
  • Implement a disaggregated prefill engine designated for disagg prefill to avoid OpenAI API server reduction
  • Pipelining
  • XpYd support
  • RDMA support

@YaoJiayi @ApostaC probably need your help.

@mergify mergify bot removed the needs-rebase label Nov 7, 2024
@KuntaiDu KuntaiDu removed the ready ONLY add when PR is ready to merge/full CI is needed label Nov 7, 2024
Copy link

mergify bot commented Nov 9, 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 9, 2024
@baifanxxx
Copy link

Thank you for your contribution to the community. I think this is very valuable PR. I have a little problem with using openai_api_server. Does the Proxy API Server Need to Be Connected to the Network? Or can we deploy Proxy API server, vLLM prefill, and vLLM decode on a local machine? Typically, we expect to deploy the entire system locally, because the server is often not connected to the external network.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.