test_bitsandbytes.py 6.55 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
3
4
5
'''Tests whether bitsandbytes computation is enabled correctly.

Run `pytest tests/quantization/test_bitsandbytes.py`.
'''
6
7

import gc
8
import os
9

10
11
12
import pytest
import torch

13
from tests.quantization.utils import is_quant_method_supported
zhuwenwen's avatar
zhuwenwen committed
14
15
from ..utils import models_path_prefix
from vllm.platforms import current_platform
16

17

18
from ..utils import compare_two_settings, create_new_process_for_each_test
youkaichao's avatar
youkaichao committed
19

20
models_4bit_to_test = [
zhuwenwen's avatar
zhuwenwen committed
21
    (os.path.join(models_path_prefix, "facebook/opt-125m"), "quantize opt model inflight"),
zhuwenwen's avatar
zhuwenwen committed
22
    (os.path.join(models_path_prefix, "mistralai/Mistral-7B-Instruct-v0.3"),
23
     "quantize inflight model with both HF and Mistral format weights")
24
25
]

26
models_pre_qaunt_4bit_to_test = [
27
    (os.path.join(models_path_prefix, 'PrunaAI/Einstein-v6.1-Llama3-8B-bnb-4bit-smashed'),
28
     'read pre-quantized 4-bit FP4 model'),
zhuwenwen's avatar
zhuwenwen committed
29
    (os.path.join(models_path_prefix, 'poedator/opt-125m-bnb-4bit'), 'read pre-quantized 4-bit NF4 opt model'),
30
31
32
]

models_pre_quant_8bit_to_test = [
zhuwenwen's avatar
zhuwenwen committed
33
    (os.path.join(models_path_prefix, 'meta-llama/Llama-Guard-3-8B-INT8'),
34
     'read pre-quantized llama 8-bit model'),
zhuwenwen's avatar
zhuwenwen committed
35
    (os.path.join(models_path_prefix, "yec019/fbopt-350m-8bit"), "read pre-quantized 8-bit opt model"),
36
37
38
]


zhuwenwen's avatar
zhuwenwen committed
39
@pytest.mark.skipif(not is_quant_method_supported("bitsandbytes") or current_platform(),
40
41
                    reason='bitsandbytes is not supported on this GPU type.')
@pytest.mark.parametrize("model_name, description", models_4bit_to_test)
42
@create_new_process_for_each_test()
43
44
45
46
47
48
49
50
def test_load_4bit_bnb_model(hf_runner, vllm_runner, example_prompts,
                             model_name, description) -> None:

    hf_model_kwargs = {"load_in_4bit": True}
    validate_generated_texts(hf_runner, vllm_runner, example_prompts[:1],
                             model_name, hf_model_kwargs)


zhuwenwen's avatar
zhuwenwen committed
51
@pytest.mark.skipif(not is_quant_method_supported("bitsandbytes") or current_platform(),
52
53
54
                    reason='bitsandbytes is not supported on this GPU type.')
@pytest.mark.parametrize("model_name, description",
                         models_pre_qaunt_4bit_to_test)
55
@create_new_process_for_each_test()
56
57
58
59
60
61
def test_load_pre_quant_4bit_bnb_model(hf_runner, vllm_runner, example_prompts,
                                       model_name, description) -> None:

    validate_generated_texts(hf_runner, vllm_runner, example_prompts[:1],
                             model_name)

62

zhuwenwen's avatar
zhuwenwen committed
63
@pytest.mark.skipif(not is_quant_method_supported("bitsandbytes") or current_platform(),
64
                    reason='bitsandbytes is not supported on this GPU type.')
65
66
@pytest.mark.parametrize("model_name, description",
                         models_pre_quant_8bit_to_test)
67
@create_new_process_for_each_test()
68
69
70
71
72
73
74
def test_load_8bit_bnb_model(hf_runner, vllm_runner, example_prompts,
                             model_name, description) -> None:

    validate_generated_texts(hf_runner, vllm_runner, example_prompts[:1],
                             model_name)


75
76
@pytest.mark.skipif(torch.cuda.device_count() < 2,
                    reason='Test requires at least 2 GPUs.')
zhuwenwen's avatar
zhuwenwen committed
77
@pytest.mark.skipif(not is_quant_method_supported("bitsandbytes") or current_platform(),
78
79
                    reason='bitsandbytes is not supported on this GPU type.')
@pytest.mark.parametrize("model_name, description", models_4bit_to_test)
80
@create_new_process_for_each_test()
81
82
83
84
85
86
87
88
89
90
91
92
def test_load_tp_4bit_bnb_model(hf_runner, vllm_runner, example_prompts,
                                model_name, description) -> None:

    hf_model_kwargs = {"load_in_4bit": True}
    validate_generated_texts(hf_runner,
                             vllm_runner,
                             example_prompts[:1],
                             model_name,
                             hf_model_kwargs,
                             vllm_tp_size=2)


93
94
95
96
97
@pytest.mark.skipif(torch.cuda.device_count() < 2,
                    reason='Test requires at least 2 GPUs.')
@pytest.mark.skipif(not is_quant_method_supported("bitsandbytes"),
                    reason='bitsandbytes is not supported on this GPU type.')
@pytest.mark.parametrize("model_name, description", models_4bit_to_test)
98
@create_new_process_for_each_test()
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def test_load_pp_4bit_bnb_model(model_name, description) -> None:
    common_args = [
        "--disable-log-stats",
        "--disable-log-requests",
        "--dtype",
        "bfloat16",
        "--enable-prefix-caching",
        "--quantization",
        "bitsandbytes",
        "--gpu-memory-utilization",
        "0.7",
    ]
    pp_args = [
        *common_args,
        "--pipeline-parallel-size",
        "2",
    ]
    compare_two_settings(model_name, common_args, pp_args)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def log_generated_texts(prompts, outputs, runner_name):
    logged_texts = []
    for i, (_, generated_text) in enumerate(outputs):
        log_entry = {
            "prompt": prompts[i],
            "runner_name": runner_name,
            "generated_text": generated_text,
        }
        logged_texts.append(log_entry)
    return logged_texts


def validate_generated_texts(hf_runner,
                             vllm_runner,
                             prompts,
                             model_name,
135
136
                             hf_model_kwargs=None,
                             vllm_tp_size=1):
137

youkaichao's avatar
youkaichao committed
138
139
    # NOTE: run vLLM first, as it requires a clean process
    # when using distributed inference
140
    with vllm_runner(model_name,
141
                     quantization='bitsandbytes',
142
                     tensor_parallel_size=vllm_tp_size,
143
                     enforce_eager=False) as llm:
144
145
146
147
148
149
150
        vllm_outputs = llm.generate_greedy(prompts, 8)
        vllm_logs = log_generated_texts(prompts, vllm_outputs, "VllmRunner")

    # Clean up the GPU memory for the next test
    gc.collect()
    torch.cuda.empty_cache()

youkaichao's avatar
youkaichao committed
151
152
153
154
155
156
157
158
159
160
161
162
    if hf_model_kwargs is None:
        hf_model_kwargs = {}

    # Run with HF runner
    with hf_runner(model_name, model_kwargs=hf_model_kwargs) as llm:
        hf_outputs = llm.generate_greedy(prompts, 8)
        hf_logs = log_generated_texts(prompts, hf_outputs, "HfRunner")

    # Clean up the GPU memory for the next test
    gc.collect()
    torch.cuda.empty_cache()

163
164
165
166
167
    # Compare the generated strings
    for hf_log, vllm_log in zip(hf_logs, vllm_logs):
        hf_str = hf_log["generated_text"]
        vllm_str = vllm_log["generated_text"]
        prompt = hf_log["prompt"]
168

169
170
171
172
173
        assert hf_str == vllm_str, (f"Model: {model_name}"
                                    f"Mismatch between HF and vLLM outputs:\n"
                                    f"Prompt: {prompt}\n"
                                    f"HF Output: '{hf_str}'\n"
                                    f"vLLM Output: '{vllm_str}'")