GPTQ
GPT-QModel 项目(Python 包 gptqmodel)实现了 GPTQ 算法,这是一种训练后量化技术,它独立地对权重矩阵的每一行进行量化,以找到使误差最小化的权重版本。这些权重被量化为 int4,但在推理过程中会即时恢复为 fp16。由于 int4 权重是在融合内核中反量化,而不是在 GPU 的全局内存中,因此内存占用可以减少 4 倍。此外,由于更低的位宽需要更少的通信时间,推理速度也更快。
AutoGPTQ 不再受 Transformers 支持。请改为安装 GPT-QModel。
请先安装 Accelerate、Transformers 和 Optimum。
pip install --upgrade accelerate optimum transformers
然后运行以下命令来安装 GPT-QModel。
pip install gptqmodel --no-build-isolation
创建一个 GPTQConfig 类,并设置要量化的位数、用于校准量化权重的数据集,以及用于准备数据集的 tokenizer。
from transformers import AutoModelForCausalLM, AutoTokenizer, GPTQConfig
tokenizer = AutoTokenizer.from_pretrained("facebook/opt-125m")
gptq_config = GPTQConfig(bits=4, dataset="c4", tokenizer=tokenizer)您可以将自定义数据集作为字符串列表传入,但强烈建议使用 GPTQ 论文中相同的数据集。
dataset = ["gptqmodel is an easy-to-use model quantization library with user-friendly apis, based on the GPTQ algorithm."]
gptq_config = GPTQConfig(bits=4, dataset=dataset, tokenizer=tokenizer)Load a model to quantize and pass GPTQConfig to from_pretrained(). Set device_map="auto" to automatically offload the model to a CPU to help fit the model in memory, and allow the model modules to be moved between the CPU and GPU for quantization.
quantized_model = AutoModelForCausalLM.from_pretrained("facebook/opt-125m", device_map="auto", quantization_config=gptq_config)如果由于数据集过大而导致内存不足(不支持磁盘卸载),请尝试传入 max_memory 参数,以分配在设备(GPU 和 CPU)上使用的内存量。
quantized_model = AutoModelForCausalLM.from_pretrained(
"facebook/opt-125m",
device_map="auto",
max_memory={0: "30GiB", 1: "46GiB", "cpu": "30GiB"},
quantization_config=gptq_config
)根据您的硬件配置,从头量化模型可能需要一些时间。在免费层级的 Google Colab GPU 上量化 facebook/opt-350m 模型大约需要 5 分钟,而在 NVIDIA A100 上量化一个 175B 参数的模型大约需要 4 小时。在量化模型之前,建议先检查 Hub 上是否已存在该模型的 GPTQ 量化版本。
模型量化完成后,您可以使用 push_to_hub() 将模型和分词器推送到 Hub,以便轻松共享和访问。此操作会保存 GPTQConfig。
quantized_model.push_to_hub("opt-125m-gptq")
tokenizer.push_to_hub("opt-125m-gptq")save_pretrained() 用于在本地保存量化后的模型。如果模型是使用 device_map 参数进行量化的,请在保存前将整个模型移至 GPU 或 CPU。以下示例展示了如何在 CPU 上保存模型。
quantized_model.save_pretrained("opt-125m-gptq")
tokenizer.save_pretrained("opt-125m-gptq")
# if quantized with device_map set
quantized_model.to("cpu")
quantized_model.save_pretrained("opt-125m-gptq")使用 from_pretrained() 重新加载量化模型,并设置 device_map="auto",以自动将模型分布到所有可用的 GPU 上,从而在不使用超出所需内存的情况下更快地加载模型。
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained("{your_username}/opt-125m-gptq", device_map="auto")Marlin
Marlin 是一个仅支持 4-bit 的 CUDA GPTQ 内核,针对 NVIDIA A100 GPU(Ampere)架构进行了高度优化。其加载、反量化以及反量化后权重的执行过程均实现了高度并行化,相较于原始 CUDA GPTQ 内核,可带来显著的推理性能提升。Marlin 仅支持量化推理,不支持模型量化。
可以通过 GPTQConfig 中的 backend 参数启用 Marlin 推理。
from transformers import AutoModelForCausalLM, GPTQConfig
model = AutoModelForCausalLM.from_pretrained("{your_username}/opt-125m-gptq", device_map="auto", quantization_config=GPTQConfig(bits=4, backend="marlin"))GPT-QModel
GPT-QModel 是 Transformers 中 GPTQ 的活跃维护后端。它最初是从 AutoGPTQ 分支而来,但此后已显著分化,并引入了多项改进,例如更快的量化速度、更低的内存占用以及更准确的默认设置。
GPT-QModel 提供非对称量化,与对称量化相比,它有可能降低量化误差。它与旧版 AutoGPTQ 检查点不向后兼容,并且并非所有内核(Marlin)都支持非对称量化。
GPT-QModel 还更广泛地支持最新的 LLM 模型、多模态模型(Qwen2-VL 和 Ovis1.6-VL)、平台(Linux、macOS、Windows 11)以及硬件(AMD ROCm、Apple Silicon、Intel/AMD CPU,以及 Intel Datacenter Max/Arc GPU 等)。
Marlin 内核也已针对 A100 GPU 进行了更新,其他内核也进行了更新,以支持旧模型以及输入/输出特征维度不一致的模型的自动填充。
资源
运行 GPTQ 量化与 PEFT 的 notebook,以获得实际操作体验。
Update on GitHub