bnb.md 1.7 KB
Newer Older
1
2
3
4
---
title: BitsAndBytes
---
[](){ #bits-and-bytes }
5
6
7
8
9
10
11
12

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
13
pip install bitsandbytes>=0.45.3
14
15
16
17
```

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

18
You can find bitsandbytes quantized models on <https://huggingface.co/models?search=bitsandbytes>.
19
20
And usually, these repositories have a config.json file that includes a quantization_config section.

21
## Read quantized checkpoint
22

23
24
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.

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

## Inflight quantization: load as 4bit quantization

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

37
38
39
40
41
```python
from vllm import LLM
import torch
model_id = "huggyllama/llama-7b"
llm = LLM(model=model_id, dtype=torch.bfloat16, trust_remote_code=True, \
42
quantization="bitsandbytes")
43
```
44

45
46
## OpenAI Compatible Server

47
Append the following to your model arguments for 4bit inflight quantization:
48

49
```console
50
--quantization bitsandbytes
51
```