Transformers 文档
AWQ
并获得增强的文档体验
开始使用
AWQ
激活感知权重量化 (AWQ) 保留一小部分对 LLM 性能很重要的权重,将模型压缩到 4 位,同时最大限度地减少性能下降。
有几个库可以使用 AWQ 算法量化模型,例如 llm-awq、autoawq 或 optimum-intel。Transformers 支持加载使用 llm-awq 和 autoawq 库量化的模型。本指南将向您展示如何加载使用 autoawq 量化的模型,但对于 llm-awq 量化的模型,过程类似。
运行以下命令安装 autoawq
pip install autoawq
AutoAWQ 会将 Transformers 降级到 4.47.1 版本。如果您想使用 AutoAWQ 进行推理,您可能需要在安装 AutoAWQ 后重新安装您的 Transformers 版本。
通过检查模型 config.json 文件中的 quant_method
键来识别 AWQ 量化模型。
{
"_name_or_path": "/workspace/process/huggingfaceh4_zephyr-7b-alpha/source",
"architectures": [
"MistralForCausalLM"
],
...
...
...
"quantization_config": {
"quant_method": "awq",
"zero_point": true,
"group_size": 128,
"bits": 4,
"version": "gemm"
}
}
使用 from_pretrained() 加载 AWQ 量化模型。出于性能考虑,这会自动将其他权重默认为 fp16。使用 torch_dtype
参数以不同格式加载这些其他权重。
如果模型加载在 CPU 上,请使用 device_map
参数将其移动到 GPU。
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
model = AutoModelForCausalLM.from_pretrained(
"TheBloke/zephyr-7B-alpha-AWQ",
torch_dtype=torch.float32,
device_map="cuda:0"
)
使用 attn_implementation
启用 FlashAttention2 以进一步加速推理。
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained(
"TheBloke/zephyr-7B-alpha-AWQ",
attn_implementation="flash_attention_2",
device_map="cuda:0"
)
融合模块
融合模块提供更高的精度和性能。它们对于 Llama 和 Mistral 架构的 AWQ 模块开箱即用,但您也可以为不支持的架构融合 AWQ 模块。
融合模块不能与其他优化技术(例如 FlashAttention2)结合使用。
创建 AwqConfig 并设置参数 fuse_max_seq_len
和 do_fuse=True
以启用融合模块。fuse_max_seq_len
参数是总序列长度,它应该包括上下文长度和预期的生成长度。将其设置为更大的值以确保安全。
以下示例融合了 TheBloke/Mistral-7B-OpenOrca-AWQ 模型的 AWQ 模块。
import torch
from transformers import AwqConfig, AutoModelForCausalLM
quantization_config = AwqConfig(
bits=4,
fuse_max_seq_len=512,
do_fuse=True,
)
model = AutoModelForCausalLM.from_pretrained(
"TheBloke/Mistral-7B-OpenOrca-AWQ",
quantization_config=quantization_config
).to(0)
TheBloke/Mistral-7B-OpenOrca-AWQ 模型在有或没有融合模块的情况下以 batch_size=1
进行基准测试。
批量大小 | 预填充长度 | 解码长度 | 预填充 token/秒 | 解码 token/秒 | 内存 (VRAM) |
---|---|---|---|---|---|
1 | 32 | 32 | 60.0984 | 38.4537 | 4.50 GB (5.68%) |
1 | 64 | 64 | 1333.67 | 31.6604 | 4.50 GB (5.68%) |
1 | 128 | 128 | 2434.06 | 31.6272 | 4.50 GB (5.68%) |
1 | 256 | 256 | 3072.26 | 38.1731 | 4.50 GB (5.68%) |
1 | 512 | 512 | 3184.74 | 31.6819 | 4.59 GB (5.80%) |
1 | 1024 | 1024 | 3148.18 | 36.8031 | 4.81 GB (6.07%) |
1 | 2048 | 2048 | 2927.33 | 35.2676 | 5.73 GB (7.23%) |
批量大小 | 预填充长度 | 解码长度 | 预填充 token/秒 | 解码 token/秒 | 内存 (VRAM) |
---|---|---|---|---|---|
1 | 32 | 32 | 81.4899 | 80.2569 | 4.00 GB (5.05%) |
1 | 64 | 64 | 1756.1 | 106.26 | 4.00 GB (5.05%) |
1 | 128 | 128 | 2479.32 | 105.631 | 4.00 GB (5.06%) |
1 | 256 | 256 | 1813.6 | 85.7485 | 4.01 GB (5.06%) |
1 | 512 | 512 | 2848.9 | 97.701 | 4.11 GB (5.19%) |
1 | 1024 | 1024 | 3044.35 | 87.7323 | 4.41 GB (5.57%) |
1 | 2048 | 2048 | 2715.11 | 89.4709 | 5.57 GB (7.04%) |
融合和未融合模块的速度和吞吐量也使用 optimum-benchmark 库进行了测试。


ExLlamaV2
ExLlamaV2 内核支持更快的预填充和解码。运行以下命令安装支持 ExLlamaV2 的最新版 autoawq。
pip install git+https://github.com/casper-hansen/AutoAWQ.git
在 AwqConfig 中设置 version="exllama"
以启用 ExLlamaV2 内核。
ExLlamaV2 在 AMD GPU 上受支持。
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, AwqConfig
quantization_config = AwqConfig(version="exllama")
model = AutoModelForCausalLM.from_pretrained(
"TheBloke/Mistral-7B-Instruct-v0.1-AWQ",
quantization_config=quantization_config,
device_map="auto",
)
CPU
Intel Extension for PyTorch (IPEX) 旨在实现 Intel 硬件上的性能优化。运行以下命令安装支持 IPEX 的最新版 autoawq。
pip install intel-extension-for-pytorch # for IPEX-GPU refer to https://intel.github.io/intel-extension-for-pytorch/xpu/2.5.10+xpu/
pip install git+https://github.com/casper-hansen/AutoAWQ.git
在 AwqConfig 中设置 version="ipex"
以启用 ExLlamaV2 内核。
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, AwqConfig
device = "cpu" # set to "xpu" for Intel GPU
quantization_config = AwqConfig(version="ipex")
model = AutoModelForCausalLM.from_pretrained(
"TheBloke/TinyLlama-1.1B-Chat-v0.3-AWQ",
quantization_config=quantization_config,
device_map=device,
)
资源
运行 AWQ 演示 笔记本,了解更多量化模型、将量化模型推送到 Hub 等示例。
< > 在 GitHub 上更新