使用 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、convertAPI 供使用者使用。面向精度的調優: Intel® Neural Compressor 支援面向精度的自動調優過程,提供
autotuneAPI 供使用者使用。量化型別: 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 的官方 文件 中可以找到更詳細的教程。