GPTQModel¶
要建立新的 4 位或 8 位 GPTQ 量化模型,您可以利用 ModelCloud.AI 的 GPTQModel。
量化將模型的精度從 BF16/FP16 (16 位) 降低到 INT4 (4 位) 或 INT8 (8 位),這顯著減少了模型的總記憶體佔用,同時提高了推理效能。
相容的 GPTQModel 量化模型可以利用 Marlin
和 Machete
vLLM 自定義核心,以最大限度地提高 Ampere (A100+) 和 Hopper (H100+) Nvidia GPU 的每秒批處理事務數 (tps
) 和令牌延遲效能。這兩個核心經過 vLLM 和 NeuralMagic (現為 Redhat 的一部分) 的高度最佳化,以實現量化 GPTQ 模型的全球領先推理效能。
GPTQModel 是全球少數支援 動態
逐模組量化的工具包之一,它允許對 LLM 模型中的不同層和/或模組使用自定義量化引數進行進一步最佳化。動態
量化已完全整合到 vLLM 中,並得到 ModelCloud.AI 團隊的支援。有關此功能及其他高階功能的更多詳細資訊,請參閱 GPTQModel readme。
安裝¶
您可以透過安裝 GPTQModel 或選擇 Huggingface 上的 5000 多個模型之一來量化您自己的模型。
量化模型¶
安裝 GPTQModel 後,您就可以開始量化模型了。有關更多詳細資訊,請參閱 GPTQModel readme。
以下是如何量化 meta-llama/Llama-3.2-1B-Instruct
的示例
程式碼
from datasets import load_dataset
from gptqmodel import GPTQModel, QuantizeConfig
model_id = "meta-llama/Llama-3.2-1B-Instruct"
quant_path = "Llama-3.2-1B-Instruct-gptqmodel-4bit"
calibration_dataset = load_dataset(
"allenai/c4",
data_files="en/c4-train.00001-of-01024.json.gz",
split="train"
).select(range(1024))["text"]
quant_config = QuantizeConfig(bits=4, group_size=128)
model = GPTQModel.load(model_id, quant_config)
# increase `batch_size` to match gpu/vram specs to speed up quantization
model.quantize(calibration_dataset, batch_size=2)
model.save(quant_path)
使用 vLLM 執行量化模型¶
要使用 vLLM 執行 GPTQModel 量化模型,您可以使用 DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v2 並使用以下命令
python examples/offline_inference/llm_engine_example.py \
--model ModelCloud/DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v2
透過 vLLM 的 Python API 使用 GPTQModel¶
GPTQModel 量化模型也透過 LLM 入口點直接支援
程式碼
from vllm import LLM, SamplingParams
# Sample prompts.
prompts = [
"Hello, my name is",
"The president of the United States is",
"The capital of France is",
"The future of AI is",
]
# Create a sampling params object.
sampling_params = SamplingParams(temperature=0.6, top_p=0.9)
# Create an LLM.
llm = LLM(model="ModelCloud/DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v2")
# Generate texts from the prompts. The output is a list of RequestOutput objects
# that contain the prompt, generated text, and other information.
outputs = llm.generate(prompts, sampling_params)
# Print the outputs.
print("-"*50)
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}\nGenerated text: {generated_text!r}")
print("-"*50)