test_gguf.py 4.95 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
3
4
5
6
7
"""
Tests gguf models against unquantized models generations
Note: To pass the test, quantization higher than Q4 should be used
"""

import os
8
from typing import List, NamedTuple, Type
9
10
11

import pytest
from huggingface_hub import hf_hub_download
12
from transformers import AutoTokenizer
13
14
15

from tests.quantization.utils import is_quant_method_supported

16
from ....conftest import VllmRunner
17
from ...utils import check_logprobs_close
18
from ....utils import models_path_prefix
19
20
21
22
23
24

os.environ["TOKENIZERS_PARALLELISM"] = "true"

MAX_MODEL_LEN = 1024


25
26
27
28
29
30
31
32
33
34
35
class GGUFTestConfig(NamedTuple):
    original_model: str
    gguf_repo: str
    gguf_filename: str

    @property
    def gguf_model(self):
        return hf_hub_download(self.gguf_repo, filename=self.gguf_filename)


LLAMA_CONFIG = GGUFTestConfig(
zhuwenwen's avatar
zhuwenwen committed
36
37
38
    original_model=os.path.join(models_path_prefix, "meta-llama/Llama-3.2-1B-Instruct"),
    gguf_repo=os.path.join(models_path_prefix, "bartowski/Llama-3.2-1B-Instruct-GGUF"),
    gguf_filename=os.path.join(models_path_prefix, "Llama-3.2-1B-Instruct-IQ4_XS.gguf"),
39
40
41
)

QWEN2_CONFIG = GGUFTestConfig(
zhuwenwen's avatar
zhuwenwen committed
42
43
44
    original_model=os.path.join(models_path_prefix, "Qwen/Qwen2.5-1.5B-Instruct"),
    gguf_repo=os.path.join(models_path_prefix, "Qwen/Qwen2.5-1.5B-Instruct-GGUF"),
    gguf_filename=os.path.join(models_path_prefix, "qwen2.5-1.5b-instruct-q6_k.gguf"),
45
46
47
)

PHI3_CONFIG = GGUFTestConfig(
zhuwenwen's avatar
zhuwenwen committed
48
49
50
    original_model=os.path.join(models_path_prefix, "microsoft/Phi-3.5-mini-instruct"),
    gguf_repo=os.path.join(models_path_prefix, "bartowski/Phi-3.5-mini-instruct-GGUF"),
    gguf_filename=os.path.join(models_path_prefix, "Phi-3.5-mini-instruct-IQ4_XS.gguf"),
51
52
53
)

GPT2_CONFIG = GGUFTestConfig(
zhuwenwen's avatar
zhuwenwen committed
54
55
56
    original_model=os.path.join(models_path_prefix, "openai-community/gpt2-large"),
    gguf_repo=os.path.join(models_path_prefix, "QuantFactory/gpt2-large-GGUF"),
    gguf_filename=os.path.join(models_path_prefix, "gpt2-large.Q4_K_M.gguf"),
57
58
59
)

STABLELM_CONFIG = GGUFTestConfig(
zhuwenwen's avatar
zhuwenwen committed
60
61
62
    original_model=os.path.join(models_path_prefix, "stabilityai/stablelm-3b-4e1t"),
    gguf_repo=os.path.join(models_path_prefix, "afrideva/stablelm-3b-4e1t-GGUF"),
    gguf_filename=os.path.join(models_path_prefix, "stablelm-3b-4e1t.q4_k_m.gguf"),
63
64
65
)

STARCODER_CONFIG = GGUFTestConfig(
zhuwenwen's avatar
zhuwenwen committed
66
67
68
    original_model=os.path.join(models_path_prefix, "bigcode/starcoder2-3b"),
    gguf_repo=os.path.join(models_path_prefix, "QuantFactory/starcoder2-3b-GGUF"),
    gguf_filename=os.path.join(models_path_prefix, "starcoder2-3b.Q6_K.gguf"),
69
70
)

71
72
DOLPHIN_CONFIG = GGUFTestConfig(
    # Test VocabParallelEmbedding sharding issue.
zhuwenwen's avatar
zhuwenwen committed
73
74
75
    original_model=os.path.join(models_path_prefix, "cognitivecomputations/TinyDolphin-2.8-1.1b"),
    gguf_repo=os.path.join(models_path_prefix, "tsunemoto/TinyDolphin-2.8-1.1b-GGUF"),
    gguf_filename=os.path.join(models_path_prefix, "tinydolphin-2.8-1.1b.Q6_K.gguf"),
76
77
)

78
MODELS = [
79
    LLAMA_CONFIG, QWEN2_CONFIG, PHI3_CONFIG, GPT2_CONFIG, STABLELM_CONFIG,
80
    DOLPHIN_CONFIG
81
82
83
84
    # STARCODER_CONFIG, # broken
]


85
86
@pytest.mark.skipif(not is_quant_method_supported("gguf"),
                    reason="gguf is not supported on this GPU type.")
87
@pytest.mark.parametrize("model", MODELS)
88
89
90
@pytest.mark.parametrize("dtype", ["half"])
@pytest.mark.parametrize("max_tokens", [32])
@pytest.mark.parametrize("num_logprobs", [5])
91
@pytest.mark.parametrize("tp_size", [1, 2])
92
def test_models(
93
94
95
96
    num_gpus_available: int,
    vllm_runner: Type[VllmRunner],
    example_prompts: List[str],
    model: GGUFTestConfig,
97
98
99
    dtype: str,
    max_tokens: int,
    num_logprobs: int,
100
    tp_size: int,
101
) -> None:
102
103
104
    if num_gpus_available < tp_size:
        pytest.skip(f"Not enough GPUs for tensor parallelism {tp_size}")

105
106
107
108
109
110
111
112
    tokenizer = AutoTokenizer.from_pretrained(model.original_model)
    if tokenizer.chat_template is not None:
        messages = [[{
            'role': 'user',
            'content': prompt
        }] for prompt in example_prompts]
        example_prompts = tokenizer.apply_chat_template(
            messages, tokenize=False, add_generation_prompt=True)
113

114
    # Run unquantized model.
115
116
117
118
119
120
    with vllm_runner(
            model_name=model.original_model,
            enforce_eager=True,  # faster tests
            dtype=dtype,
            max_model_len=MAX_MODEL_LEN,
            tensor_parallel_size=tp_size) as original_model:
121
122
123
124
        original_outputs = original_model.generate_greedy_logprobs(
            example_prompts[:-1], max_tokens, num_logprobs)

    # Run gguf model.
125
    with vllm_runner(model_name=model.gguf_model,
126
                     enforce_eager=True,
127
                     tokenizer_name=model.original_model,
128
129
                     dtype=dtype,
                     max_model_len=MAX_MODEL_LEN,
130
                     tensor_parallel_size=tp_size) as gguf_model:
131
132
133
134
135
136
137
138
139
        gguf_outputs = gguf_model.generate_greedy_logprobs(
            example_prompts[:-1], max_tokens, num_logprobs)

    check_logprobs_close(
        outputs_0_lst=original_outputs,
        outputs_1_lst=gguf_outputs,
        name_0="original",
        name_1="gguf",
    )