-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
[Core] Support LoRA on quantized models #4012
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| # Adapted from | ||
| # https://github.com/fmmoret/vllm/blob/fm-support-lora-on-quantized-models/tests/lora/test_llama.py | ||
| from dataclasses import dataclass | ||
| from typing import List | ||
|
|
||
| import pytest | ||
|
|
||
| import vllm | ||
| from vllm.lora.request import LoRARequest | ||
|
|
||
| from .conftest import cleanup | ||
|
|
||
|
|
||
| @dataclass | ||
| class ModelWithQuantization: | ||
| model_path: str | ||
| quantization: str | ||
|
|
||
|
|
||
| MODELS: List[ModelWithQuantization] = [ | ||
| ModelWithQuantization(model_path="TheBloke/TinyLlama-1.1B-Chat-v0.3-AWQ", | ||
| quantization="AWQ"), | ||
| ModelWithQuantization(model_path="TheBloke/TinyLlama-1.1B-Chat-v0.3-GPTQ", | ||
| quantization="GPTQ"), | ||
| ] | ||
|
|
||
|
|
||
| def do_sample(llm, lora_path: str, lora_id: int, max_tokens=256): | ||
| raw_prompts = [ | ||
| "Give me an orange-ish brown color", | ||
| "Give me a neon pink color", | ||
| ] | ||
|
|
||
| def format_prompt_tuples(prompt): | ||
| return f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n" | ||
|
|
||
| prompts = [format_prompt_tuples(p) for p in raw_prompts] | ||
|
|
||
| sampling_params = vllm.SamplingParams(temperature=0, | ||
| max_tokens=max_tokens, | ||
| stop=["<|im_end|>"]) | ||
| outputs = llm.generate( | ||
| prompts, | ||
| sampling_params, | ||
| lora_request=LoRARequest(str(lora_id), lora_id, lora_path) | ||
| if lora_id else None) | ||
| # Print the outputs. | ||
| generated_texts = [] | ||
| for output in outputs: | ||
| prompt = output.prompt | ||
| generated_text = output.outputs[0].text | ||
| generated_texts.append(generated_text) | ||
| print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") | ||
| return generated_texts | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", MODELS) | ||
| @pytest.mark.parametrize("tp_size", [1]) | ||
| def test_quant_model_lora(tinyllama_lora_files, model, tp_size): | ||
| # Cannot use as it will initialize torch.cuda too early... | ||
| # if torch.cuda.device_count() < tp_size: | ||
| # pytest.skip(f"Not enough GPUs for tensor parallelism {tp_size}") | ||
|
|
||
| llm = vllm.LLM(model=model.model_path, | ||
| enable_lora=True, | ||
| max_num_seqs=16, | ||
| max_loras=4, | ||
| max_model_len=400, | ||
| tensor_parallel_size=tp_size, | ||
| quantization=model.quantization, | ||
| trust_remote_code=True) | ||
|
|
||
| if model.quantization is None: | ||
| expected_no_lora_output = [ | ||
| "Here are some examples of orange-brown colors", | ||
| "I'm sorry, I don't have" | ||
| ] | ||
| expected_lora_output = [ | ||
| "#ff8050", | ||
| "#ff8080", | ||
| ] | ||
| elif model.quantization == "AWQ": | ||
| expected_no_lora_output = [ | ||
| "I'm sorry, I don't understand", | ||
| "I'm sorry, I don't understand", | ||
| ] | ||
| expected_lora_output = [ | ||
| "#f07700: A v", | ||
| "#f00000: A v", | ||
| ] | ||
| elif model.quantization == "GPTQ": | ||
| expected_no_lora_output = [ | ||
| "I'm sorry, I don't have", | ||
| "I'm sorry, I don't have", | ||
| ] | ||
| expected_lora_output = [ | ||
| "#f08800: This is", | ||
| "#f07788 \n#", | ||
| ] | ||
|
|
||
| def expect_match(output, expected_output): | ||
| # HACK: GPTQ lora outputs are just incredibly unstable. | ||
| # Assert that the outputs changed. | ||
| if (model.quantization == "GPTQ" | ||
| and expected_output is expected_lora_output): | ||
| assert output != expected_no_lora_output | ||
| for i, o in enumerate(output): | ||
| assert o.startswith( | ||
| '#'), f"Expected example {i} to start with # but got {o}" | ||
| return | ||
| assert output == expected_output | ||
|
|
||
| max_tokens = 10 | ||
|
|
||
| print("lora adapter created") | ||
| output = do_sample(llm, | ||
| tinyllama_lora_files, | ||
| lora_id=0, | ||
| max_tokens=max_tokens) | ||
| expect_match(output, expected_no_lora_output) | ||
|
|
||
| print("lora 1") | ||
| output = do_sample(llm, | ||
| tinyllama_lora_files, | ||
| lora_id=1, | ||
| max_tokens=max_tokens) | ||
| expect_match(output, expected_lora_output) | ||
|
|
||
| print("no lora") | ||
| output = do_sample(llm, | ||
| tinyllama_lora_files, | ||
| lora_id=0, | ||
| max_tokens=max_tokens) | ||
| expect_match(output, expected_no_lora_output) | ||
|
|
||
| print("lora 2") | ||
| output = do_sample(llm, | ||
| tinyllama_lora_files, | ||
| lora_id=2, | ||
| max_tokens=max_tokens) | ||
| expect_match(output, expected_lora_output) | ||
|
|
||
| print("removing lora") | ||
|
|
||
| del llm | ||
| cleanup() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", MODELS) | ||
| @pytest.mark.skip("Requires multiple GPUs") | ||
| def test_quant_model_tp_equality(tinyllama_lora_files, model): | ||
| # Cannot use as it will initialize torch.cuda too early... | ||
| # if torch.cuda.device_count() < 2: | ||
| # pytest.skip(f"Not enough GPUs for tensor parallelism {2}") | ||
|
|
||
| llm_tp1 = vllm.LLM(model=model.model_path, | ||
| enable_lora=True, | ||
| max_num_seqs=16, | ||
| max_loras=4, | ||
| tensor_parallel_size=1, | ||
| quantization=model.quantization, | ||
| trust_remote_code=True) | ||
| output_tp1 = do_sample(llm_tp1, tinyllama_lora_files, lora_id=1) | ||
|
|
||
| del llm_tp1 | ||
| cleanup() | ||
|
|
||
| llm_tp2 = vllm.LLM(model=model.model_path, | ||
| enable_lora=True, | ||
| max_num_seqs=16, | ||
| max_loras=4, | ||
| tensor_parallel_size=2, | ||
| quantization=model.quantization) | ||
| output_tp2 = do_sample(llm_tp2, tinyllama_lora_files, lora_id=1) | ||
|
|
||
| del llm_tp2 | ||
| cleanup() | ||
|
|
||
| assert output_tp1 == output_tp2 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@Yard1 @jeejeelee I'm not able to release GPU memory usage using
cleanupordel llm_tp1. Are you sure this would work (if CI had multiple GPUs and we could enable this test)?On
0.4.2version, if I startvllm.LLMI can't find any way to release the GPU memory again. I have to kill the process. Do you know any other 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.
I forgot a bit, but I'm quite sure
test_baichuan.pycan be tested using tp=2. I've tested it myself.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 have tried it using the following code, GPU memory can release normly, however, I didn't use the released 0.4.2 version, I used f12c3b5 instead.
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 don't have any multi-GPU machines available right now, I will use
tp=2for testing again later.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.
Great, thanks for checking that. I believe the
tensor_parallel_size=2usesmultiprocessingand so the behavior is different from what I experimented with. I'll post a new issue.