ExecuTorch 的 XNNPACK 移动端 CPU 后端
XNNPACK 是 ExecuTorch 在移动端 CPU 上执行模型的委托后端,为 Arm 与 x86 提供优化算子,支持多种量化方案及 fp32、fp16 激活,并可通过分区器在导出阶段接入。
## XNNPACK 后端软件[#](<https://docs.pytorch.org/executorch/stable/android-xnnpack.html#xnnpack-backend>) XNNPACK代表作是移动CPU上用于CPU执行的ExecuTorch解决方案. [XNNPACK](<https://github.com/google/XNNPACK/tree/master>) 是一个库,为Arm和x86 CPU上的机器学习操作员提供优化的内核. ## 特征[#](<https://docs.pytorch.org/executorch/stable/android-xnnpack.html#features>) - 在Arm和x86 CPU上提供宽接线员支持,任何现代手机上都可以使用. - 支持多种量化计划和量化操作人员. - 支持fp32和fp16激活. - 支持 8 - 位量化 。 ## 目标所需资源[#](<https://docs.pytorch.org/executorch/stable/android-xnnpack.html#target-requirements>) - ARM64在Android,iOS,macOS,Linux,和Windows上发布. - ARMv7(带有NEON)在Android上发布. - 在Linux上的ARMv6(与VFPv2). - x86和x86-64(最高为AVX512)在Windows,Linux,Android上. ## 发展要求[#](<https://docs.pytorch.org/executorch/stable/android-xnnpack.html#development-requirements>) XNNPACK代表除了核心ExecuTorch运行时间的要求外,没有引入任何开发系统要求. --- ## 使用 XNNPACK 后端[#](<https://docs.pytorch.org/executorch/stable/android-xnnpack.html#using-the-xnnpack-backend>) 要在导出和降低过程中瞄准 XNNPACK 后端, 请通过一个实例 `XnnpackPartitioner` 改为: `to_edge_transform_and_lower`下面的例子说明了使用火炬视觉中的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.backends.xnnpack.utils.configs import get_transform_passes 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()], transform_passes=get_transform_passes(), ).to_executorch() with open("mv2_xnnpack.pte", "wb") as file: et_program.write_to_file(file) ``` 那个... `transform_passes` 在一般降低的 API 中,舞台是可选的。对于 XNNPACK , `get_transform_passes()` 被一般推荐,因为它运行预分解图转换,将支持的图案暴露在分区器上. 见 [分区器 API](<https://docs.pytorch.org/executorch/stable/backends/xnnpack/xnnpack-partitioner.html>) ,以获取可用分区器选项的引用。 --- ## 量化[#](<https://docs.pytorch.org/executorch/stable/android-xnnpack.html#quantization>) XNNPACK 代表器也可以用作执行对称定量模型的后端. See [XNNPACK 量化](<https://docs.pytorch.org/executorch/stable/backends/xnnpack/xnnpack-quantization.html>) 关于现有量化计划和API的更多信息。 --- ## 运行时间集成[#](<https://docs.pytorch.org/executorch/stable/android-xnnpack.html#runtime-integration>) 要运行device上的模型,使用标准的ExecuTorch运行时API. XNNPACK 代表默认包含在已出版的Android, iOS, 和 pip 套件中. 在从源头构建时, 通过 `-DEXECUTORCH_BUILD_XNNPACK=ON` 当配置 CMake 构建以编译 XNNPACK 后端时。见 [在设备上运行](<https://docs.pytorch.org/executorch/stable/getting-started.html#running-on-device>) 以获取更多信息。 要链接到后端,请添加 `executorch_backends` CMake 目标作为构建的依赖性,或直接与 `libxnnpack_backend`。由于使用静态登记,可能有必要与全归档系统联系起来。这通常可以通过 `"$<LINK_LIBRARY:WHOLE_ARCHIVE,xnnpack_backend>"` 改为: `target_link_libraries`. ``` # CMakeLists.txt add_subdirectory("executorch") ... target_link_libraries( my_target PRIVATE executorch executorch_backends ... ) ``` 不需要额外的步骤来使用连接目标以外的后端。 任何 XNNPACK 授权的. pte 文件都会在注册后端上自动运行 。 ## 参考[#](<https://docs.pytorch.org/executorch/stable/android-xnnpack.html#reference>) **→[解决问题](<https://docs.pytorch.org/executorch/stable/backends/xnnpack/xnnpack-troubleshooting.html>) ——调试常见问题.** **→[分区器 API](<https://docs.pytorch.org/executorch/stable/backends/xnnpack/xnnpack-partitioner.html>) ——分区选项和支持的运算符.** **→[量化](<https://docs.pytorch.org/executorch/stable/backends/xnnpack/xnnpack-quantization.html>) 支持的量化计划。** **→[建筑和内部](<https://docs.pytorch.org/executorch/stable/backends/xnnpack/xnnpack-arch-internals.html>) ——XNNPACK后端内置.**
查看原始来源