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 上更新