跳至内容

BitsAndBytes

vLLM 现已支持 BitsAndBytes 以实现更高效的模型推理。 BitsAndBytes 通过量化模型来减少内存占用并提升性能,同时不会显著影响精度。 与其他量化方法相比,BitsAndBytes 无需使用输入数据对量化后的模型进行校准。

以下是使用vLLM结合BitsAndBytes的步骤。

pip install bitsandbytes>=0.45.3

vLLM读取模型的配置文件,并支持实时量化和预量化检查点。

你可以在Hugging Face上找到bitsandbytes量化的模型。 通常这些代码库会包含一个config.json文件,其中包含quantization_config配置部分。

读取量化检查点

对于预量化的检查点,vLLM会尝试从配置文件中推断量化方法,因此您无需显式指定量化参数。

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
)

动态量化:以4位量化加载

对于使用BitsAndBytes进行实时4位量化,您需要明确指定量化参数。

from vllm import LLM
import torch
model_id = "huggyllama/llama-7b"
llm = LLM(
    model=model_id,
    dtype=torch.bfloat16,
    trust_remote_code=True,
    quantization="bitsandbytes"
)

OpenAI兼容服务器

为4比特动态量化模型参数添加以下内容:

--quantization bitsandbytes