2025-02-02 14:58:18 -05:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2024-06-01 13:51:10 -07:00
|
|
|
'''Tests whether bitsandbytes computation is enabled correctly.
|
|
|
|
|
|
|
|
Run `pytest tests/quantization/test_bitsandbytes.py`.
|
|
|
|
'''
|
2024-08-29 16:09:08 -07:00
|
|
|
|
|
|
|
import gc
|
|
|
|
|
2024-06-01 13:51:10 -07:00
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
|
2024-06-13 11:18:08 -04:00
|
|
|
from tests.quantization.utils import is_quant_method_supported
|
2025-03-17 19:33:35 +08:00
|
|
|
|
|
|
|
from ..utils import compare_two_settings, create_new_process_for_each_test
|
2024-09-13 02:20:14 -07:00
|
|
|
|
2024-08-29 16:09:08 -07:00
|
|
|
models_4bit_to_test = [
|
2024-10-08 18:52:19 -07:00
|
|
|
("facebook/opt-125m", "quantize opt model inflight"),
|
2025-03-18 00:27:26 +01:00
|
|
|
("mistralai/Mistral-7B-Instruct-v0.3",
|
|
|
|
"quantize inflight model with both HF and Mistral format weights")
|
2024-07-23 16:45:09 -07:00
|
|
|
]
|
|
|
|
|
2024-08-29 16:09:08 -07:00
|
|
|
models_pre_qaunt_4bit_to_test = [
|
|
|
|
('PrunaAI/Einstein-v6.1-Llama3-8B-bnb-4bit-smashed',
|
|
|
|
'read pre-quantized 4-bit FP4 model'),
|
2024-10-08 18:52:19 -07:00
|
|
|
('poedator/opt-125m-bnb-4bit', 'read pre-quantized 4-bit NF4 opt model'),
|
2024-08-29 16:09:08 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
models_pre_quant_8bit_to_test = [
|
2024-10-08 18:52:19 -07:00
|
|
|
('meta-llama/Llama-Guard-3-8B-INT8',
|
|
|
|
'read pre-quantized llama 8-bit model'),
|
|
|
|
("yec019/fbopt-350m-8bit", "read pre-quantized 8-bit opt model"),
|
2024-08-29 16:09:08 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@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)
|
2025-03-17 19:33:35 +08:00
|
|
|
@create_new_process_for_each_test()
|
2024-08-29 16:09:08 -07:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
@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_pre_qaunt_4bit_to_test)
|
2025-03-17 19:33:35 +08:00
|
|
|
@create_new_process_for_each_test()
|
2024-08-29 16:09:08 -07:00
|
|
|
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)
|
|
|
|
|
2024-06-01 13:51:10 -07:00
|
|
|
|
2024-06-13 11:18:08 -04:00
|
|
|
@pytest.mark.skipif(not is_quant_method_supported("bitsandbytes"),
|
|
|
|
reason='bitsandbytes is not supported on this GPU type.')
|
2024-08-29 16:09:08 -07:00
|
|
|
@pytest.mark.parametrize("model_name, description",
|
|
|
|
models_pre_quant_8bit_to_test)
|
2025-03-17 19:33:35 +08:00
|
|
|
@create_new_process_for_each_test()
|
2024-08-29 16:09:08 -07:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2024-09-17 08:09:12 -07:00
|
|
|
@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)
|
2025-03-17 19:33:35 +08:00
|
|
|
@create_new_process_for_each_test()
|
2024-09-17 08:09:12 -07:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2024-11-14 00:56:39 +08:00
|
|
|
@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)
|
2025-03-17 19:33:35 +08:00
|
|
|
@create_new_process_for_each_test()
|
2024-11-14 00:56:39 +08:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2024-08-29 16:09:08 -07:00
|
|
|
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,
|
2024-09-17 08:09:12 -07:00
|
|
|
hf_model_kwargs=None,
|
|
|
|
vllm_tp_size=1):
|
2024-08-29 16:09:08 -07:00
|
|
|
|
2024-09-13 02:20:14 -07:00
|
|
|
# NOTE: run vLLM first, as it requires a clean process
|
|
|
|
# when using distributed inference
|
2024-07-23 16:45:09 -07:00
|
|
|
with vllm_runner(model_name,
|
2024-06-08 01:59:20 -07:00
|
|
|
quantization='bitsandbytes',
|
2024-09-17 08:09:12 -07:00
|
|
|
tensor_parallel_size=vllm_tp_size,
|
2024-10-18 20:25:19 -05:00
|
|
|
enforce_eager=False) as llm:
|
2024-08-29 16:09:08 -07:00
|
|
|
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()
|
|
|
|
|
2024-09-13 02:20:14 -07:00
|
|
|
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()
|
|
|
|
|
2024-08-29 16:09:08 -07:00
|
|
|
# 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"]
|
2024-10-08 18:52:19 -07:00
|
|
|
|
2024-08-29 16:09:08 -07:00
|
|
|
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}'")
|