跳到内容

BitsAndBytes

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

以下是在 vLLM 中使用 BitsAndBytes 的步骤。

pip install bitsandbytes>=0.49.2

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,
)

实时量化:以 4bit 量化加载

对于使用 BitsAndBytes 进行实时 4bit 量化,你需要显式指定量化参数。

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 的服务器

若要进行 4bit 实时量化,请在模型参数中追加以下内容

--quantization bitsandbytes