2024-12-23 17:35:38 -05:00
|
|
|
(bits-and-bytes)=
|
|
|
|
|
|
|
|
# BitsAndBytes
|
|
|
|
|
|
|
|
vLLM now supports [BitsAndBytes](https://github.com/TimDettmers/bitsandbytes) for more efficient model inference.
|
|
|
|
BitsAndBytes quantizes models to reduce memory usage and enhance performance without significantly sacrificing accuracy.
|
|
|
|
Compared to other quantization methods, BitsAndBytes eliminates the need for calibrating the quantized model with input data.
|
|
|
|
|
|
|
|
Below are the steps to utilize BitsAndBytes with vLLM.
|
|
|
|
|
|
|
|
```console
|
2025-01-12 03:17:13 -05:00
|
|
|
pip install bitsandbytes>=0.45.0
|
2024-12-23 17:35:38 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
vLLM reads the model's config file and supports both in-flight quantization and pre-quantized checkpoint.
|
|
|
|
|
|
|
|
You can find bitsandbytes quantized models on <https://huggingface.co/models?other=bitsandbytes>.
|
|
|
|
And usually, these repositories have a config.json file that includes a quantization_config section.
|
|
|
|
|
2025-01-12 03:17:13 -05:00
|
|
|
## Read quantized checkpoint
|
2024-12-23 17:35:38 -05:00
|
|
|
|
|
|
|
```python
|
|
|
|
from vllm import LLM
|
|
|
|
import torch
|
|
|
|
# unsloth/tinyllama-bnb-4bit is a pre-quantized checkpoint.
|
|
|
|
model_id = "unsloth/tinyllama-bnb-4bit"
|
|
|
|
llm = LLM(model=model_id, dtype=torch.bfloat16, trust_remote_code=True, \
|
2025-03-21 10:17:12 +08:00
|
|
|
quantization="bitsandbytes")
|
2024-12-23 17:35:38 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
## Inflight quantization: load as 4bit quantization
|
|
|
|
|
|
|
|
```python
|
|
|
|
from vllm import LLM
|
|
|
|
import torch
|
|
|
|
model_id = "huggyllama/llama-7b"
|
|
|
|
llm = LLM(model=model_id, dtype=torch.bfloat16, trust_remote_code=True, \
|
2025-03-21 10:17:12 +08:00
|
|
|
quantization="bitsandbytes")
|
2024-12-23 17:35:38 -05:00
|
|
|
```
|
2025-01-12 03:17:13 -05:00
|
|
|
|
2025-01-04 00:29:02 -06:00
|
|
|
## OpenAI Compatible Server
|
|
|
|
|
|
|
|
Append the following to your 4bit model arguments:
|
|
|
|
|
2025-01-12 03:17:13 -05:00
|
|
|
```console
|
2025-03-21 10:17:12 +08:00
|
|
|
--quantization bitsandbytes
|
2025-01-04 00:29:02 -06:00
|
|
|
```
|