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.FP32
、ConfigPrecisionType.STATIC_QUANT
或ConfigPrecisionType.DYNAMIC_QUANT
中的一个或多个。请参阅ConfigPrecisionType。per_op_mode
: 如果为 true,则为每个算子发出单独的委托调用。这是一个高级选项,旨在在某些上下文中减少内存开销,但会增加少量运行时开销。默认为 false。verbose
: 如果为 true,则在降低过程中打印附加信息。
量化¶
XNNPACK 委托也可以用作后端来执行对称量化模型。要为 XNNPACK 后端量化 PyTorch 模型,请使用 XNNPACKQuantizer
。Quantizers
是特定于后端的,这意味着 XNNPACKQuantizer
已配置为量化模型,以利用 XNNPACK 库提供的量化算子。
支持的量化方案¶
XNNPACK 委托支持以下量化方案
8 位对称权重和 8 位非对称激活(通过 PT2E 量化流程)。
支持静态和动态激活。
支持每通道和每张量方案。
支持 linear、convolution、add、mul、cat 和 adaptive avg pool 2d 算子。
XNNPACK 当前不支持仅权重量化。
使用 PT2E 流程进行 8 位量化¶
在导出模型之前,使用 PT2E 流程进行 8 位量化需要执行以下步骤
创建
XnnpackQuantizer
类的一个实例。设置量化参数。使用
torch.export.export_for_training
为量化做准备。调用
prepare_pt2e
为模型量化做准备。对于静态量化,使用代表性样本运行准备好的模型,以校准量化张量激活范围。
调用
convert_pt2e
量化模型。使用标准流程导出和降低模型。
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 文件都将自动在注册的后端上运行。