Optimum 文件

Ryzen AI 量化

您正在檢視的是需要從原始碼安裝。如果您想進行常規的 pip 安裝,請檢視最新的穩定版本 (v1.27.0)。
Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

Ryzen AI 量化

Ryzen AI IPU 的最佳效能透過使用量化模型實現。有兩種不同的方法可以對 Ryzen AI IPU 的模型進行量化:

  • 透過 Vitis AI Quantizer,在 Optimum 的 RyzenAIOnnxQuantizer 中使用,該量化器專為 ONNX 模型量化設計。目前支援使用動態和靜態量化方法對 timm 模型進行量化。
  • 透過 Brevitas 庫,在 Optimum 的 BrevitasQuantizer 中使用。Brevitas 允許直接量化 PyTorch 模型,這些模型可以選擇匯出為 ONNX。建議使用此方法量化其他模型。

使用 RyzenAIOnnxQuantizer 進行量化

🤗 Optimum AMD 提供了一個 Ryzen AI 量化器,您可以使用 AMD Vitis AI Quantizer 對 Hugging Face Hub 上託管的許多模型應用量化。

RyzenAI Quantizer 為以 ONNX 格式儲存的預訓練模型提供了易於使用的訓練後量化 (PTQ) 流程。它生成一個量化的 ONNX 模型,可直接與 Ryzen AI 部署。

該量化器支援各種配置和功能,可對目標部署在 IPU_CNN、IPU_Transformer 和 CPU 上的模型進行量化。

RyzenAIOnnxQuantizer 可以使用 from_pretrained 方法進行初始化,無論是來自本地模型資料夾還是 Hugging Face Hub 上託管的模型。

>>> from optimum.amd.ryzenai import RyzenAIOnnxQuantizer

>>> quantizer = RyzenAIOnnxQuantizer.from_pretrained("path/to/model")

下面您將找到一個關於如何量化來自 Timm 庫的 VGG 模型的簡單端到端示例。

  • 首先,使用 Optimum Exporters 將 VGG 模型匯出為 ONNX。確保為推理指定靜態形狀。
  • 建立一個預處理函式,以處理特定的影像格式轉換並應用必要的轉換,以準備模型的輸入。
  • 初始化 RyzenAI 量化器 (RyzenAIOnnxQuantizer) 並使用 AutoQuantizationConfig 配置量化設定。部署在 IPU 上的 CNN 模型的推薦量化配置使用 ipu_cnn_config 載入。
  • 使用量化器的 get_calibration_dataset 方法獲取校準資料集。該資料集對於在量化過程中計算量化引數至關重要。
  • 使用指定的量化配置和校準資料執行量化器。在此過程中計算的量化引數將作為常量嵌入到量化模型中。
  • 生成的量化模型儲存在指定的量化目錄中。
>>> from functools import partial
>>> import timm

>>> from optimum.amd.ryzenai import AutoQuantizationConfig, RyzenAIOnnxQuantizer
>>> from optimum.exporters.onnx import main_export
>>> from transformers import PretrainedConfig

>>> # Define paths for exporting ONNX model and saving quantized model
>>> export_dir = "/path/to/vgg_onnx"
>>> quantization_dir = "/path/to/vgg_onnx_quantized"

>>> # Specify the model ID from Timm
>>> model_id = "timm/vgg11.tv_in1k"

>>> # Step 1: Export the model to ONNX format using Optimum Exporters
>>> main_export(
...     model_name_or_path=model_id,
...     output=export_dir,
...     task="image-classification",
...     opset=13,
...     batch_size=1,
...     no_dynamic_axes=True,
... )

>>> # Step 2: Preprocess configuration and data transformations
>>> config = PretrainedConfig.from_pretrained(export_dir)
>>> data_config = timm.data.resolve_data_config(pretrained_cfg=config.pretrained_cfg)
>>> transforms = timm.data.create_transform(**data_config, is_training=False)

>>> def preprocess_fn(ex, transforms):
...     image = ex["image"]
...     if image.mode == "L":
...       # Convert greyscale to RGB if needed
...       print("WARNING: converting greyscale to RGB")
...       image = image.convert("RGB")
...     pixel_values = transforms(image)
...     return {"pixel_values": pixel_values}

>>> # Step 3: Initialize the RyzenAIOnnxQuantizer with the exported model
>>> quantizer = RyzenAIOnnxQuantizer.from_pretrained(export_dir)

>>> # Step 4: Load recommended quantization config for model
>>> quantization_config = AutoQuantizationConfig.ipu_cnn_config()

>>> # Step 5: Obtain a calibration dataset for computing quantization parameters
>>> train_calibration_dataset = quantizer.get_calibration_dataset(
...     "imagenet-1k",
...     preprocess_function=partial(preprocess_fn, transforms=transforms),
...     num_samples=100,
...     dataset_split="train",
...     preprocess_batch=False,
...     streaming=True,
... )

>>> # Step 6: Run the quantizer with the specified configuration and calibration data
>>> quantizer.quantize(
...     quantization_config=quantization_config,
...     dataset=train_calibration_dataset,
...     save_dir=quantization_dir
... )

使用 BrevitasQuantizer 進行量化

即將推出。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.