ExecuTorch 的 Vulkan 后端概览

ExecuTorch 的 Vulkan 后端让模型通过跨平台 Vulkan API 在 GPU 上执行,主要面向 Android GPU,桌面平台支持仍属实验性质。它支持动态形状模型与 FP32、FP16 推理,并可通过分区器在导出阶段将模型下沉到该后端。

## Vulkan 后端软件[#](<https://docs.pytorch.org/executorch/stable/backends/vulkan/vulkan-overview.html#vulkan-backend>) ExecuTorch Vulkan(ET-VK)后端使ExecuTorch模型能够通过跨平台执行GPU. [武尔坎 API](<https://www.vulkan.org/>)虽然Vulkan API支持在现代GPU中几乎无处不在,但ExecuTorch Vulkan后端是开发的,重点是 **Android GPU 软件**,对桌面平台的支持是实验性的。 ## 特征[#](<https://docs.pytorch.org/executorch/stable/backends/vulkan/vulkan-overview.html#features>) - 通过树上广线操作员支持 [GLSL 计算阴影库](<https://github.com/pytorch/executorch/tree/main/backends/vulkan/runtime/graph/ops/glsl>) - 支持需要动态形状的模型 - 支持FP32和FP16推论模式 - 支持带有 8 - bit/ 4 - bit 重量和 8 - bit 动态的 quentized 激活的线性层 - 支持带有 8 - bit/ 4 - bit 重和 FP32 / FP16 激活的定量线性层 注意 Vulkan 后端正在积极开发中,其GLSL 计算遮蔽器库正在随着时间的推移不断扩展,对定量运算器(即定量折射)的额外支持,额外的定量模式正在路上. ## 目标所需资源[#](<https://docs.pytorch.org/executorch/stable/backends/vulkan/vulkan-overview.html#target-requirements>) - 支持 Vulkan 1.1 软件 ## 发展要求[#](<https://docs.pytorch.org/executorch/stable/backends/vulkan/vulkan-overview.html#development-requirements>) 向Vulkan代表作出贡献: [武尔坎 SDK](<https://vulkan.lunarg.com/sdk/home#android>) 必须安装在开发系统中。安装后, `glslc` 二进制必须在您的 `PATH` 以编译 Vulkan 阴影。可以通过运行来检查 ``` glslc --version ``` 如果在完成 Vulkan SDK 安装后情况并非如此,您可能必须进入 `~/VulkanSDK/<version>/` 运行 ``` source setup-env.sh ``` 或者说, ``` python install_vulkan.py ``` 那个... [Android NDK (英语).](<https://developer.android.com/ndk/downloads>) 也必须安装。任何NDK版本通过NDK r17c就足够了。 --- ## 使用 Vulkan 后端[#](<https://docs.pytorch.org/executorch/stable/backends/vulkan/vulkan-overview.html#using-the-vulkan-backend>) 为了在出口和降低过程中将一个模型降低到Vulkan后端,通过一个实例: `VulkanPartitioner` 改为: `to_edge_transform_and_lower`下面的例子说明了使用火炬视觉中的MobileNet V2模型的这个过程. ``` import torch import torchvision.models as models from executorch.backends.vulkan.partitioner.vulkan_partitioner import VulkanPartitioner from executorch.exir import to_edge_transform_and_lower from torchvision.models.mobilenetv2 import MobileNet_V2_Weights mobilenet_v2 = models.mobilenetv2.mobilenet_v2( weights=MobileNet_V2_Weights.DEFAULT ).eval() sample_inputs = (torch.randn(1, 3, 224, 224),) exported_program = torch.export.export(mobilenet_v2, sample_inputs) etvk_program = to_edge_transform_and_lower( exported_program, partitioner=[VulkanPartitioner()], ).to_executorch() with open("mv2_vulkan.pte", "wb") as file: etvk_program.write_to_file(file) ``` 见 [分区器 API](<https://docs.pytorch.org/executorch/stable/backends/vulkan/vulkan-partitioner.html>) ,以获取可用分区器选项的引用。 --- ## 量化[#](<https://docs.pytorch.org/executorch/stable/backends/vulkan/vulkan-overview.html#quantization>) Vulkan 代表当前支持执行已定量的线性图层 。 [Vulkan 量化](<https://docs.pytorch.org/executorch/stable/backends/vulkan/vulkan-quantization.html>) 关于现有量化计划和API的更多信息。 --- ## 运行时间集成[#](<https://docs.pytorch.org/executorch/stable/backends/vulkan/vulkan-overview.html#runtime-integration>) 要运行device上的模型,使用标准的ExecuTorch运行时API. 为了整合Android应用程序,Vulkan后端包含在 [executorch-android-vulkan](<https://mvnrepository.com/artifact/org.pytorch/executorch-android-vulkan>) 软件包。 从源头建时,通过 `-DEXECUTORCH_BUILD_VULKAN=ON` 当配置 CMake 构建以编译 Vulkan 后端时。见 [在设备上运行](<https://docs.pytorch.org/executorch/stable/getting-started.html#running-on-device>) 以获取更多信息。 要链接到后端,请添加 `executorch_backends` CMake 目标作为构建的依赖性,或直接与 `libvulkan_backend`。由于使用了静态初始化来登记可用的计算阴影和操作器,需要确保库与 `--whole-archive`. ``` # CMakeLists.txt find_package(executorch CONFIG REQUIRED COMPONENTS vulkan_backend executorch_backends) ... target_link_libraries( my_target PRIVATE executorch executorch_backends ... ) # Ensure that unused code is not discarded. The required linker options may be # different depending on the target platform. Typically, the # executorch_target_link_options_shared_lib function from # executorch/tools/cmake/Utils.cmake can be used to set the required linker # options. target_link_options( executorch_backends INTERFACE "SHELL:LINKER:--whole-archive \ $<TARGET_FILE:${target_name}> \ LINKER:--no-whole-archive" ) ``` 不需要额外的步骤来使用连接目标以外的后端。 任何 Vulkan- delegated .pte 文件都会自动运行在注册后端上 。 ## 追加资源[#](<https://docs.pytorch.org/executorch/stable/backends/vulkan/vulkan-overview.html#additional-resources>) **→[分区器 API](<https://docs.pytorch.org/executorch/stable/backends/vulkan/vulkan-partitioner.html>)** **→[量化](<https://docs.pytorch.org/executorch/stable/backends/vulkan/vulkan-quantization.html>)** **→[解决问题](<https://docs.pytorch.org/executorch/stable/backends/vulkan/vulkan-troubleshooting.html>)**

查看原始来源