使用 Intel® Neural Compressor 为 PyTorch 实现易用性量化#
创建于:2022年1月11日 | 最后更新:2025年7月28日 | 最后验证:未经验证
概述#
大多数深度学习应用在推理时使用 32 位浮点精度。但像 fp8 这样的低精度数据类型因显著的性能提升而受到更多关注。采用低精度的关键问题在于在满足预定义要求的同时减轻精度损失。
Intel® Neural Compressor 旨在通过扩展 PyTorch 的精度驱动自动调优策略来解决上述问题,以帮助用户快速在 Intel 硬件上找到最佳量化模型。
Intel® Neural Compressor 是一个开源项目,位于 Github。
特性#
易于使用的 API: Intel® Neural Compressor 重用了 PyTorch 的
prepare
、convert
API 以供用户使用。精度驱动调优: Intel® Neural Compressor 支持精度驱动的自动调优过程,为用户提供了
autotune
API。量化类型: Intel® Neural Compressor 支持多种量化方法,包括经典的 INT8 量化、权重仅量化和流行的 FP8 量化。Neural compressor 还提供了最新的模拟工作研究,例如 MX 数据类型仿真量化。更多详情,请参阅 支持矩阵。
入门#
安装#
# install stable version from pip
pip install neural-compressor-pt
注意:Neural Compressor 提供自动加速器检测,包括 HPU、Intel GPU、CUDA 和 CPU。要指定目标设备,建议使用 INC_TARGET_DEVICE
,例如 export INC_TARGET_DEVICE=cpu
。
示例#
本节展示了使用 Intel® Neural compressor 的各种量化类型示例
FP8 量化#
FP8 量化由 Intel® Gaudi®2&3 AI 加速器(HPU)支持。要准备环境,请参阅 Intel® Gaudi® 文档。
运行示例,
# FP8 Quantization Example
from neural_compressor.torch.quantization import (
FP8Config,
prepare,
convert,
)
import torch
import torchvision.models as models
# Load a pre-trained ResNet18 model
model = models.resnet18()
# Configure FP8 quantization
qconfig = FP8Config(fp8_config="E4M3")
model = prepare(model, qconfig)
# Perform calibration (replace with actual calibration data)
calibration_data = torch.randn(1, 3, 224, 224).to("hpu")
model(calibration_data)
# Convert the model to FP8
model = convert(model)
# Perform inference
input_data = torch.randn(1, 3, 224, 224).to("hpu")
output = model(input_data).to("cpu")
print(output)
权重仅量化#
权重仅量化也在 Intel® Gaudi®2&3 AI 加速器上受支持。量化模型可以如下加载。
from neural_compressor.torch.quantization import load
# The model name comes from HuggingFace Model Hub.
model_name = "TheBloke/Llama-2-7B-GPTQ"
model = load(
model_name_or_path=model_name,
format="huggingface",
device="hpu",
torch_dtype=torch.bfloat16,
)
注意: Intel Neural Compressor 在首次加载时会将模型格式从 auto-gptq 转换为 hpu 格式,并将 hpu_model.safetensors 保存到本地缓存目录以便下次加载。因此,首次加载可能需要一段时间。
带 PT2E 后端的静态量化#
PT2E 路径使用 torch.dynamo
将 eager 模型捕获为 FX 图模型,然后在其上插入 observer 和 Q/QD 对。最后,它使用 torch.compile
执行模式匹配并将 Q/DQ 对替换为优化的量化算子。
使用 PT2E 后端执行 W8A8 静态量化有四个步骤:export
、prepare
、convert
和 compile
。
import torch
from neural_compressor.torch.export import export
from neural_compressor.torch.quantization import StaticQuantConfig, prepare, convert
# Prepare the float model and example inputs for export model
model = UserFloatModel()
example_inputs = ...
# Export eager model into FX graph model
exported_model = export(model=model, example_inputs=example_inputs)
# Quantize the model
quant_config = StaticQuantConfig()
prepared_model = prepare(exported_model, quant_config=quant_config)
# Calibrate
run_fn(prepared_model)
q_model = convert(prepared_model)
# Compile the quantized model and replace the Q/DQ pattern with Q-operator
from torch._inductor import config
config.freezing = True
opt_model = torch.compile(q_model)
精度驱动调优#
要利用精度驱动的自动调优,需要一个指定的调优空间。 autotune
迭代调优空间,在给定的高精度模型上应用配置,然后记录并比较其评估结果与基线。调优过程将在满足退出策略时停止。
from neural_compressor.torch.quantization import RTNConfig, TuningConfig, autotune
def eval_fn(model) -> float:
return ...
tune_config = TuningConfig(
config_set=RTNConfig(use_sym=[False, True], group_size=[32, 128]),
tolerable_loss=0.2,
max_trials=10,
)
q_model = autotune(model, tune_config=tune_config, eval_fn=eval_fn)
教程#
更详细的教程可在 Intel® Neural Compressor 官方 文档 中找到。