test_tensorizer_entrypoint.py 2.98 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
import gc
4
import os
5
6
7
8
9
10
11
12
13
import tempfile

import openai
import pytest
import pytest_asyncio
import torch.cuda

from vllm.engine.arg_utils import EngineArgs
from vllm.model_executor.model_loader.tensorizer import (
14
15
16
17
    TensorizerConfig,
    tensorize_lora_adapter,
    tensorize_vllm_model,
)
18
from vllm.platforms import current_platform
19
20
21
22
23
24
25
26
27

from ...utils import RemoteOpenAIServer

MODEL_NAME = "unsloth/llama-3.2-1b-Instruct"
LORA_PATH = "davzoku/finqa_adapter_1b"


def _cleanup():
    gc.collect()
28
    torch.accelerator.empty_cache()
29
30
31
32
33
34
35


@pytest.fixture(autouse=True)
def cleanup():
    _cleanup()


36
@pytest.fixture(scope="module")
37
38
39
40
41
def tmp_dir():
    with tempfile.TemporaryDirectory() as path:
        yield path


42
@pytest.fixture(scope="module")
43
44
45
46
47
48
def model_uri(tmp_dir):
    yield f"{tmp_dir}/model.tensors"


@pytest.fixture(scope="module")
def tensorize_model_and_lora(tmp_dir, model_uri):
49
    tensorizer_config = TensorizerConfig(tensorizer_uri=model_uri, lora_dir=tmp_dir)
50
    args = EngineArgs(model=MODEL_NAME)
51
52
53
54
55
56
57
58
59
60
61
62
63

    tensorize_lora_adapter(LORA_PATH, tensorizer_config)
    tensorize_vllm_model(args, tensorizer_config)

    # Manually invoke a _cleanup() here, as the cleanup()
    # fixture won't be guaranteed to be called after this
    # when this fixture is used for a test
    _cleanup()
    yield


@pytest.fixture(scope="module")
def server(model_uri, tensorize_model_and_lora):
64
65
66
67
68
    # In this case, model_uri is a directory with a model.tensors
    # file and all necessary model artifacts, particularly a
    # HF `config.json` file. In this case, Tensorizer can infer the
    # `TensorizerConfig` so --model-loader-extra-config can be completely
    # omitted.
69
70
71

    ## Start OpenAI API server
    args = [
72
73
74
75
76
        "--load-format",
        "tensorizer",
        "--served-model-name",
        MODEL_NAME,
        "--enable-lora",
77
    ]
78
79
    if current_platform.is_rocm():
        args += ["--attention-backend", "TRITON_ATTN"]
80

81
82
    model_dir = os.path.dirname(model_uri)
    with RemoteOpenAIServer(model_dir, args) as remote_server:
83
84
85
86
87
88
89
90
91
92
93
94
95
        yield remote_server


@pytest_asyncio.fixture
async def client(server):
    async with server.get_async_client() as async_client:
        yield async_client


@pytest.mark.asyncio
@pytest.mark.parametrize("model_name", [MODEL_NAME])
async def test_single_completion(client: openai.AsyncOpenAI, model_name: str):
    _cleanup()
96
97
98
    completion = await client.completions.create(
        model=model_name, prompt="Hello, my name is", max_tokens=5, temperature=0.0
    )
99
100
101
102
103
104
105
106

    assert completion.id is not None
    assert completion.choices is not None and len(completion.choices) == 1
    assert completion.model == MODEL_NAME
    assert len(completion.choices) == 1
    assert len(completion.choices[0].text) >= 5
    assert completion.choices[0].finish_reason == "length"
    assert completion.usage == openai.types.CompletionUsage(
107
108
        completion_tokens=5, prompt_tokens=6, total_tokens=11
    )