test_lora_huggingface.py 1.79 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
5
6
import pytest

from vllm.lora.models import LoRAModel
7
from vllm.lora.peft_helper import PEFTHelper
8
9
10
11
12
from vllm.lora.utils import get_adapter_absolute_path
from vllm.model_executor.models.llama import LlamaForCausalLM

# Provide absolute path and huggingface lora ids
lora_fixture_name = ["sql_lora_files", "sql_lora_huggingface_id"]
13
LLAMA_LORA_MODULES = [
14
15
16
17
18
19
    "qkv_proj",
    "o_proj",
    "gate_up_proj",
    "down_proj",
    "embed_tokens",
    "lm_head",
20
]
21
22
23
24
25
26
27
28


@pytest.mark.parametrize("lora_fixture_name", lora_fixture_name)
def test_load_checkpoints_from_huggingface(lora_fixture_name, request):
    lora_name = request.getfixturevalue(lora_fixture_name)
    packed_modules_mapping = LlamaForCausalLM.packed_modules_mapping
    embedding_modules = LlamaForCausalLM.embedding_modules
    embed_padding_modules = LlamaForCausalLM.embedding_padding_modules
29
    expected_lora_modules: list[str] = []
30
    for module in LLAMA_LORA_MODULES:
31
32
33
34
35
36
37
        if module in packed_modules_mapping:
            expected_lora_modules.extend(packed_modules_mapping[module])
        else:
            expected_lora_modules.append(module)

    lora_path = get_adapter_absolute_path(lora_name)

omahs's avatar
omahs committed
38
    # lora loading should work for either absolute path and huggingface id.
39
    peft_helper = PEFTHelper.from_local_dir(lora_path, 4096)
40
41
42
    lora_model = LoRAModel.from_local_checkpoint(
        lora_path,
        expected_lora_modules,
43
        peft_helper=peft_helper,
44
45
46
        lora_model_id=1,
        device="cpu",
        embedding_modules=embedding_modules,
47
48
        embedding_padding_modules=embed_padding_modules,
    )
49
50
51

    # Assertions to ensure the model is loaded correctly
    assert lora_model is not None, "LoRAModel is not loaded correctly"