Transformers 文档

AWQ

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

AWQ

激活感知权重量化 (AWQ) 保留了少量对 LLM 性能至关重要的权重,以将模型压缩到 4 位,同时最大限度地减少性能下降。

有几个库可用于使用 AWQ 算法量化模型,例如 llm-awqautoawqoptimum-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"
)

融合模块

融合模块提供更高的准确性和性能。它们开箱即用地支持 LlamaMistral 架构的 AWQ 模块,但您也可以为不受支持的架构融合 AWQ 模块。

融合模块不能与其他优化技术(如 FlashAttention2)结合使用。

支持的架构
不支持的架构

创建一个 AwqConfig 并设置参数 fuse_max_seq_lendo_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 的情况下进行了基准测试,无论是否使用融合模块。

非融合模块
批大小 预填充长度 解码长度 预填充 tokens/秒 解码 tokens/秒 内存 (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%)
融合模块
批大小 预填充长度 解码长度 预填充 tokens/秒 解码 tokens/秒 内存 (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 库进行了测试。

generate throughput per batch size
前向峰值内存/批大小
forward latency per batch size
生成吞吐量/批大小

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 演示 notebook,了解有关如何量化模型、将量化模型推送到 Hub 等的更多示例。

< > 在 GitHub 上更新