test_add_lora.py 3.91 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
5
6
7
8
import asyncio
import time

import pytest

from vllm.engine.arg_utils import AsyncEngineArgs
9
from vllm.entrypoints.openai.api_server import (
10
11
    build_async_engine_client_from_engine_args,
)
12
13
14
15
16
from vllm.inputs import TextPrompt
from vllm.lora.request import LoRARequest
from vllm.sampling_params import SamplingParams
from vllm.utils import merge_async_iterators

17
MODEL_PATH = "zai-org/chatglm3-6b"
18
19
LORA_RANK = 64
DEFAULT_MAX_LORAS = 4 * 3
20
21


22
def get_lora_requests(lora_path) -> list[LoRARequest]:
23
    lora_requests: list[LoRARequest] = [
24
        LoRARequest(lora_name=f"{i}", lora_int_id=i, lora_path=lora_path)
25
26
27
28
29
        for i in range(1, DEFAULT_MAX_LORAS + 1)
    ]
    return lora_requests


30
31
32
33
async def requests_processing_time(llm, lora_requests: list[LoRARequest]) -> float:
    sampling_params = SamplingParams(
        n=1, temperature=0.0, top_p=1.0, ignore_eos=True, max_tokens=1
    )
34
35
36
37
38
39
40

    generators = []
    start = time.perf_counter()

    for lora_request in lora_requests:
        lora_int_id = lora_request.lora_int_id
        generator = llm.generate(
41
            prompt=TextPrompt(prompt=f"hello {lora_int_id}", multi_modal_data=None),  # type: ignore
42
43
            sampling_params=sampling_params,
            lora_request=lora_request,
44
45
            request_id=f"test{lora_int_id}",
        )
46
47
48
49
50
51
52
53
54
55
56
        generators.append(generator)

    all_gens = merge_async_iterators(*generators)
    async for i, res in all_gens:
        pass

    end = time.perf_counter()
    return end - start


@pytest.mark.asyncio
57
async def test_add_lora(chatglm3_lora_files):
58
    """
59
    The add_lora function is used to preload some LoRA adapters into the
60
61
    engine in anticipation of future requests using these adapters. To test
    this functionality, we use the async engine to process some requests - We
62
    do it twice, once with add_lora() preloading and once without.
63

64
    We measure the request processing time in both cases and expect the time
65
66
    to be lesser in the case with add_lora() calls.
    """
67
    lora_requests: list[LoRARequest] = get_lora_requests(chatglm3_lora_files)
68
69
70
71
72
73
74
75
76
77

    max_loras = len(set([lr.lora_int_id for lr in lora_requests]))
    # Create engine in eager-mode. Due to high max_loras, the CI can
    # OOM during cuda-graph capture.
    engine_args = AsyncEngineArgs(
        model=MODEL_PATH,
        enable_lora=True,
        max_loras=max_loras,
        max_lora_rank=LORA_RANK,
        max_model_len=128,
78
        gpu_memory_utilization=0.8,  # avoid OOM
79
        trust_remote_code=True,
80
81
        enforce_eager=True,
    )
82
83
84
85

    # split lora_requests into 3 parts
    part_size = len(lora_requests) // 3
    dummy_run_requests = lora_requests[:part_size]
86
87
    warmup_run_requests = lora_requests[part_size : part_size * 2]
    cold_run_requests = lora_requests[part_size * 2 :]
88
89
90
91
92
93
94

    async with build_async_engine_client_from_engine_args(engine_args) as llm:
        # Dummy run - So any 1-time functionality like triton kernel compilation
        # is complete here.
        await requests_processing_time(llm, dummy_run_requests)

        # Run with warmup
95
96
        add_lora_tasks = [llm.add_lora(lr) for lr in warmup_run_requests]
        add_lora_results = await asyncio.gather(*add_lora_tasks)
97
98
99
100

        # Test that all all_lora calls are successful.
        assert all(add_lora_results)

101
        time_with_add_lora = await requests_processing_time(llm, warmup_run_requests)
102
103

        # Run without any warmup
104
        time_cold_start = await requests_processing_time(llm, cold_run_requests)
105

106
    print(f"time hot-start {time_with_add_lora} vs time cold-start {time_cold_start} ")
107
108
109
110
111

    assert time_with_add_lora < time_cold_start, (
        f"time_with_add_lora={time_with_add_lora}, "
        f"time_cold_start={time_cold_start}"
        "The engine request processing time with LoRA pre-loading "
112
113
        "must be less than the version that does on-demand LoRA loading."
    )