bnb.md 1.71 KB
Newer Older
1
# BitsAndBytes
2
3
4
5
6
7
8

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.

9
```bash
10
pip install bitsandbytes>=0.46.1
11
12
13
14
```

vLLM reads the model's config file and supports both in-flight quantization and pre-quantized checkpoint.

Reid's avatar
Reid committed
15
You can find bitsandbytes quantized models on [Hugging Face](https://huggingface.co/models?search=bitsandbytes).
16
17
And usually, these repositories have a config.json file that includes a quantization_config section.

18
## Read quantized checkpoint
19

20
21
For pre-quantized checkpoints, vLLM will try to infer the quantization method from the config file, so you don't need to explicitly specify the quantization argument.

22
23
24
25
26
```python
from vllm import LLM
import torch
# unsloth/tinyllama-bnb-4bit is a pre-quantized checkpoint.
model_id = "unsloth/tinyllama-bnb-4bit"
Reid's avatar
Reid committed
27
28
29
llm = LLM(
    model=model_id,
    dtype=torch.bfloat16,
30
    trust_remote_code=True,
Reid's avatar
Reid committed
31
)
32
33
34
35
```

## Inflight quantization: load as 4bit quantization

36
37
For inflight 4bit quantization with BitsAndBytes, you need to explicitly specify the quantization argument.

38
39
40
41
```python
from vllm import LLM
import torch
model_id = "huggyllama/llama-7b"
Reid's avatar
Reid committed
42
43
44
45
llm = LLM(
    model=model_id,
    dtype=torch.bfloat16,
    trust_remote_code=True,
46
    quantization="bitsandbytes",
Reid's avatar
Reid committed
47
)
48
```
49

50
51
## OpenAI Compatible Server

52
Append the following to your model arguments for 4bit inflight quantization:
53

54
```bash
55
--quantization bitsandbytes
56
```