bnb.md 1.72 KB
Newer Older
1
2
3
---
title: BitsAndBytes
---
4
5
6
7
8
9
10

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.

11
```bash
12
pip install bitsandbytes>=0.46.1
13
14
15
16
```

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

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

20
## Read quantized checkpoint
21

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

24
25
26
27
28
```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
29
30
31
32
33
llm = LLM(
    model=model_id,
    dtype=torch.bfloat16,
    trust_remote_code=True
)
34
35
36
37
```

## Inflight quantization: load as 4bit quantization

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

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

52
53
## OpenAI Compatible Server

54
Append the following to your model arguments for 4bit inflight quantization:
55

56
```bash
57
--quantization bitsandbytes
58
```