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

[Hardware][Intel] Add LoRA adapter support for CPU backend #4830

Open
wants to merge 32 commits into
base: main
Choose a base branch
from

Conversation

Isotr0py
Copy link
Collaborator

This PR adds the implementation of bgmv and dispatch_bgmv_low_level in pytorch.
This works for the device which doesn't satisfy compute capacity >= 8.0 to launch punica kernel.

Features

  • Alternative bgmv and dispatch_bgmv_low_level function in pytorch
  • LoRA adapter support for CPU backend

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!

@Isotr0py Isotr0py changed the title [Misc] Add LoRA adapter support for CPU backend [Hardware][Intel] Add LoRA adapter support for CPU backend May 15, 2024
@Isotr0py
Copy link
Collaborator Author

Isotr0py commented May 15, 2024

Test Result

from vllm import LLM
from vllm import SamplingParams
from vllm.lora.request import LoRARequest

llm = LLM("meta-llama/Llama-2-7b-hf", enable_lora=True)

sql_lora_path = "yard1/llama-2-7b-sql-lora-test"

prompts = [
     "[user] Write a SQL query to answer the question based on the table schema.\n\n context: CREATE TABLE table_name_74 (icao VARCHAR, airport VARCHAR)\n\n question: Name the ICAO for lilongwe international airport [/user] [assistant]",
     "[user] Write a SQL query to answer the question based on the table schema.\n\n context: CREATE TABLE table_name_11 (nationality VARCHAR, elector VARCHAR)\n\n question: When Anchero Pantaleone was the elector what is under nationality? [/user] [assistant]",
]

sampling_params = SamplingParams(temperature=0.8, top_p=0.95, max_tokens=128, stop=["[/assistant]"])

outputs = llm.generate(prompts, sampling_params, lora_request=LoRARequest("sql_adapter", 1, sql_lora_path))

# Print the outputs.
for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

Outputs:

INFO 05-16 16:05:44 llm_engine.py:103] Initializing an LLM engine (v0.4.2) with config: model='/data/LLM-model/Llama-2-7b-hf', speculative_config=None, tokenizer='/data/LLM-model/Llama-2-7b-hf', skip_tokenizer_init=False, tokenizer_mode=auto, revision=None, tokenizer_revision=None, trust_remote_code=False, dtype=torch.float16, max_seq_len=4096, download_dir=None, load_format=LoadFormat.AUTO, tensor_parallel_size=1, disable_custom_all_reduce=False, quantization=None, enforce_eager=False, kv_cache_dtype=auto, quantization_param_path=None, device_config=cpu, decoding_config=DecodingConfig(guided_decoding_backend='outlines'), seed=0, served_model_name=/data/LLM-model/Llama-2-7b-hf)
WARNING 05-16 16:05:44 cpu_executor.py:112] float16 is not supported on CPU, casting to bfloat16.
WARNING 05-16 16:05:44 cpu_executor.py:115] CUDA graph is not supported on CPU, fallback to the eager mode.
WARNING 05-16 16:05:44 cpu_executor.py:142] Environment variable VLLM_CPU_KVCACHE_SPACE (GB) for CPU backend is not set, using 4 by default.
WARNING 05-16 16:05:44 punica.py:14] punica LoRA kernels require a GPU to run. But you are using the CPU version vLLM
INFO 05-16 16:05:45 selector.py:52] Using Torch SDPA backend.
INFO 05-16 16:05:53 cpu_executor.py:71] # CPU blocks: 512
Processed prompts: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:46<00:00, 23.45s/it, Generation Speed: 1.13 toks/s]
Prompt: '[user] Write a SQL query to answer the question based on the table schema.\n\n context: CREATE TABLE table_name_74 (icao VARCHAR, airport VARCHAR)\n\n question: Name the ICAO for lilongwe international airport [/user] [assistant]', Generated text: "  SELECT icao FROM table_name_74 WHERE airport = 'lilongwe international airport' "
Prompt: '[user] Write a SQL query to answer the question based on the table schema.\n\n context: CREATE TABLE table_name_11 (nationality VARCHAR, elector VARCHAR)\n\n question: When Anchero Pantaleone was the elector what is under nationality? [/user] [assistant]', Generated text: "  SELECT nationality FROM table_name_11 WHERE elector = 'Anchero Pantaleone' "

@Isotr0py Isotr0py marked this pull request as ready for review May 16, 2024 08:01
@zhouyuan
Copy link
Contributor

@Isotr0py Hi, thanks for enabling the LoRA feature on CPU! The patch seems outdated, could you please help to do a rebase in case you are available?

thanks, -yuan

@Isotr0py
Copy link
Collaborator Author

@zhouyuan I have rebased the code. The native lora kernel should work again.

@WoosukKwon
Copy link
Collaborator

@zhouyuan @bigPYJ1151 Could you please review this PR? Thanks!

tests/lora/conftest.py Outdated Show resolved Hide resolved
.buildkite/run-cpu-test.sh Show resolved Hide resolved
.buildkite/run-cpu-test.sh Show resolved Hide resolved
.buildkite/run-cpu-test.sh Outdated Show resolved Hide resolved
vllm/lora/models.py Outdated Show resolved Hide resolved
vllm/lora/models.py Outdated Show resolved Hide resolved
vllm/lora/punica.py Outdated Show resolved Hide resolved
vllm/lora/native_kernels.py Outdated Show resolved Hide resolved
vllm/worker/cpu_model_runner.py Outdated Show resolved Hide resolved
vllm/lora/punica.py Outdated Show resolved Hide resolved
@bigPYJ1151
Copy link
Contributor

LGTM, thanks for your efforts to make LoRA work on the CPU backend, let's find a way to make it more efficient.
Hi @WoosukKwon, would you please help to check this PR? It looks good to me. Thanks!

Copy link
Collaborator

@WoosukKwon WoosukKwon left a comment

Choose a reason for hiding this comment

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

@Isotr0py Thanks for submitting the PR!

Overall, I'm good with the implementation here. My only concern is, I'm not sure whether we'd like to a feature with (extremely) unoptimized performance. We've seen that people sometimes misunderstand the performance of vLLM due to such unoptimized features.

Let me discuss more with the team and get back to you.

vllm/lora/punica.py Outdated Show resolved Hide resolved
vllm/lora/punica.py Outdated Show resolved Hide resolved
"The CPU backend does not support custom kernels for LoRA. "
"Falling back to unoptimized PyTorch-native implementation, "
"which may lead to performance drop.")
elif torch.cuda.get_device_capability() < (8, 0):
Copy link
Collaborator

Choose a reason for hiding this comment

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

QQ: Is this implementation compatible with CUDA graphs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This implementation works with enforce_eager=False, and the CUDA graphs is captured successfully:

(VllmWorkerProcess pid=6403) WARNING 06-26 03:11:20 punica.py:20] punica LoRA kernels require compute capability >= 8.0, but you are running on device with compute capability < 8.0. Falling back to unoptimized PyTorch-native implementation, which may lead to performance drop.
(VllmWorkerProcess pid=6403) INFO 06-26 03:11:25 selector.py:142] Cannot use FlashAttention-2 backend for Volta and Turing GPUs.
(VllmWorkerProcess pid=6403) INFO 06-26 03:11:25 selector.py:52] Using XFormers backend.
(VllmWorkerProcess pid=6403) INFO 06-26 03:11:26 multiproc_worker_utils.py:215] Worker ready; awaiting tasks
(VllmWorkerProcess pid=6403) INFO 06-26 03:11:27 utils.py:672] Found nccl from library libnccl.so.2
(VllmWorkerProcess pid=6403) INFO 06-26 03:11:27 pynccl.py:63] vLLM is using nccl==2.20.5
(VllmWorkerProcess pid=6403) INFO 06-26 03:11:27 custom_all_reduce_utils.py:208] reading GPU P2P access cache from /root/.config/vllm/gpu_p2p_access_cache_for_0,1.json
(VllmWorkerProcess pid=6403) WARNING 06-26 03:11:27 custom_all_reduce.py:175] Custom allreduce is disabled because your platform lacks GPU P2P capability or P2P test failed. To silence this warning, specify disable_custom_all_reduce=True explicitly.
(VllmWorkerProcess pid=6403) INFO 06-26 03:11:27 selector.py:142] Cannot use FlashAttention-2 backend for Volta and Turing GPUs.
(VllmWorkerProcess pid=6403) INFO 06-26 03:11:27 selector.py:52] Using XFormers backend.
(VllmWorkerProcess pid=6403) INFO 06-26 03:11:28 weight_utils.py:218] Using model weights format ['*.safetensors']
(VllmWorkerProcess pid=6403) INFO 06-26 03:11:29 model_runner.py:160] Loading model weights took 2.6537 GB
INFO 06-26 03:11:33 distributed_gpu_executor.py:56] # GPU blocks: 1912, # CPU blocks: 1638
(VllmWorkerProcess pid=6403) INFO 06-26 03:11:36 model_runner.py:889] Capturing the model for CUDA graphs. This may lead to unexpected consequences if the model is not static. To run the model in eager mode, set 'enforce_eager=True' or use '--enforce-eager' in the CLI.
(VllmWorkerProcess pid=6403) INFO 06-26 03:11:36 model_runner.py:893] CUDA graphs can take additional 1~3 GiB memory per GPU. If you are running out of memory, consider decreasing `gpu_memory_utilization` or enforcing eager mode. You can also reduce the `max_num_seqs` as needed to decrease memory usage.
(VllmWorkerProcess pid=6403) INFO 06-26 03:12:15 model_runner.py:965] Graph capturing finished in 38 secs.

So I think this should be compatible with CUDA graphs.

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

Successfully merging this pull request may close these issues.

4 participants