快捷方式

XNNPACK 後端

XNNPACK 委託是 ExecuTorch 在移動 CPU 上執行 CPU 任務的解決方案。XNNPACK 是一個為 Arm 和 x86 CPU 上的機器學習運算元提供最佳化核心的庫。

特性

  • 在 Arm 和 x86 CPU 上支援廣泛的運算元,適用於任何現代智慧手機。

  • 支援多種量化方案和量化運算元。

  • 支援 fp32 和 fp16 啟用。

  • 支援 8 位量化。

目標要求

  • Android、iOS、macOS、Linux 和 Windows 上的 ARM64。

  • Android 上的 ARMv7 (帶 NEON)。

  • Linux 上的 ARMv6 (帶 VFPv2)。

  • Windows、Linux、macOS、Android 和 iOS 模擬器上的 x86 和 x86-64 (最高支援 AVX512)。

開發要求

XNNPACK 委託不會引入超出核心 ExecuTorch 執行時所需的任何開發系統要求。


使用 XNNPACK 後端

在匯出和降低過程中指定 XNNPACK 後端時,將 XnnpackPartitioner 的例項傳遞給 to_edge_transform_and_lower。下面的示例展示瞭如何使用 torchvision 中的 MobileNet V2 模型進行此過程。

import torch
import torchvision.models as models
from torchvision.models.mobilenetv2 import MobileNet_V2_Weights
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner
from executorch.exir import to_edge_transform_and_lower

mobilenet_v2 = models.mobilenetv2.mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT).eval()
sample_inputs = (torch.randn(1, 3, 224, 224), )

et_program = to_edge_transform_and_lower(
    torch.export.export(mobilenet_v2, sample_inputs),
    partitioner=[XnnpackPartitioner()],
).to_executorch()

with open("mv2_xnnpack.pte", "wb") as file:
    et_program.write_to_file(file)

分割槽器 API

XNNPACK 分割槽器 API 允許配置模型委託給 XNNPACK。傳遞一個不帶額外引數的 XnnpackPartitioner 例項,將盡可能多地在 XNNPACK 後端上執行模型。這是最常見的用例。對於高階用例,分割槽器透過建構函式暴露了以下選項

  • configs: 控制哪些運算元委託給 XNNPACK。預設情況下,所有可用運算元都會被委託。請參閱../config/__init__.py 以獲取可用運算元配置的完整列表。

  • config_precisions: 按資料型別過濾運算元。預設情況下,委託所有精度型別。可以是 ConfigPrecisionType.FP32ConfigPrecisionType.STATIC_QUANTConfigPrecisionType.DYNAMIC_QUANT 中的一個或多個。請參閱ConfigPrecisionType

  • per_op_mode: 如果為 true,則為每個運算元發出單獨的委託呼叫。這是一個高階選項,旨在在某些上下文中減少記憶體開銷,但會增加少量執行時開銷。預設為 false。

  • verbose: 如果為 true,則在降低過程中列印附加資訊。

測試模型

生成 XNNPACK 委託的 .pte 檔案後,可以使用 ExecuTorch 執行時 Python 繫結從 Python 進行模型測試。這可用於模型的健全性檢查和數值精度評估。有關更多資訊,請參閱測試模型


量化

XNNPACK 委託也可以用作後端來執行對稱量化模型。要為 XNNPACK 後端量化 PyTorch 模型,請使用 XNNPACKQuantizerQuantizers 是特定於後端的,這意味著 XNNPACKQuantizer 已配置為量化模型,以利用 XNNPACK 庫提供的量化運算元。

支援的量化方案

XNNPACK 委託支援以下量化方案

  • 8 位對稱權重和 8 位非對稱啟用(透過 PT2E 量化流程)。

    • 支援靜態和動態啟用。

    • 支援每通道和每張量方案。

    • 支援 linear、convolution、add、mul、cat 和 adaptive avg pool 2d 運算元。

XNNPACK 當前不支援僅權重量化。

使用 PT2E 流程進行 8 位量化

在匯出模型之前,使用 PT2E 流程進行 8 位量化需要執行以下步驟

  1. 建立 XnnpackQuantizer 類的一個例項。設定量化引數。

  2. 使用 torch.export.export_for_training 為量化做準備。

  3. 呼叫 prepare_pt2e 為模型量化做準備。

  4. 對於靜態量化,使用代表性樣本執行準備好的模型,以校準量化張量啟用範圍。

  5. 呼叫 convert_pt2e 量化模型。

  6. 使用標準流程匯出和降低模型。

convert_pt2e 的輸出是一個 PyTorch 模型,可以使用正常流程進行匯出和降低。由於它是一個常規的 PyTorch 模型,因此也可以使用標準的 PyTorch 技術來評估量化模型的精度。

import torch
import torchvision.models as models
from torchvision.models.mobilenetv2 import MobileNet_V2_Weights
from executorch.backends.xnnpack.quantizer.xnnpack_quantizer import XNNPACKQuantizer
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner
from executorch.exir import to_edge_transform_and_lower
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
from torch.ao.quantization.quantizer.xnnpack_quantizer import get_symmetric_quantization_config

model = models.mobilenetv2.mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT).eval()
sample_inputs = (torch.randn(1, 3, 224, 224), )

qparams = get_symmetric_quantization_config(is_per_channel=True) # (1)
quantizer = XNNPACKQuantizer()
quantizer.set_global(qparams)

training_ep = torch.export.export_for_training(model, sample_inputs).module() # (2)
prepared_model = prepare_pt2e(training_ep, quantizer) # (3)

for cal_sample in [torch.randn(1, 3, 224, 224)]: # Replace with representative model inputs
	prepared_model(cal_sample) # (4) Calibrate

quantized_model = convert_pt2e(prepared_model) # (5)

et_program = to_edge_transform_and_lower( # (6)
    torch.export.export(quantized_model, sample_inputs),
    partitioner=[XnnpackPartitioner()],
).to_executorch()

有關更多資訊,請參閱PyTorch 2 匯出訓練後量化


執行時整合

要在裝置上執行模型,請使用標準的 ExecuTorch 執行時 API。有關更多資訊,請參閱在裝置上執行

XNNPACK 委託預設包含在已釋出的 Android、iOS 和 pip 包中。從原始碼構建時,在配置 CMake 構建時傳遞 -DEXECUTORCH_BUILD_XNNPACK=ON 以編譯 XNNPACK 後端。

要連結後端,請將 xnnpack_backend CMake 目標新增為構建依賴項,或直接連結 libxnnpack_backend。由於使用了靜態註冊,可能需要使用 whole-archive 進行連結。這通常可以透過將 "$<LINK_LIBRARY:WHOLE_ARCHIVE,xnnpack_backend>" 傳遞給 target_link_libraries 來完成。

# CMakeLists.txt
add_subdirectory("executorch")
...
target_link_libraries(
    my_target
    PRIVATE executorch
    extension_module_static
    extension_tensor
    optimized_native_cpu_ops_lib
    xnnpack_backend)

除了連結目標之外,使用此後端不需要其他步驟。任何經 XNNPACK 委託的 .pte 檔案都將自動在註冊的後端上執行。


© 版權所有 2024, ExecuTorch。

使用 Sphinx 構建,主題由 Read the Docs 提供。

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並獲取問題解答

檢視資源