ExecuTorch 端侧模型导出与运行入门
ExecuTorch 是 PyTorch 的端侧推理框架,使用前需安装 Python 包与平台运行时库,并通过 Python API 将模型导出为 .pte 格式,再借助 Java、Objective-C 或 C++ 运行时在设备上执行。
## 从 ExecuTorch 开始[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#getting-started-with-executorch>) 本节旨在描述为采用PyTorch模型并使用ExecuTorch运行而需采取的步骤. 要使用框架,您通常需要采取以下步骤: - 安装 ExecuTorch python 软件包和运行时库 。 - 输出目标硬件配置的PyTorch模型. - 在您的开发平台上使用 ExecuTorch 运行时间 API 运行模型 。 - 使用ExecuTorch运行时间将模型部署到目标平台. ## 系统要求[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#system-requirements>) 安装 ExecuTorch 主机库, 需要导出模型并从 Python 运行。 对目标终端用户设备的要求取决于后端。 请查看合适的后端文档以获取更多信息 。 - Python 3.10 - 3.14 维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球. - g++版本7或更高,clang++版本5或更高,或另一个C++版本17 - 兼容工具链. - Linux(x86_64或ARM64),macOS(ARM64),或Windows(x86_64). - 基于英特尔的macOS系统需要从源头构建PyTorch(参见: [从源建置](<https://docs.pytorch.org/executorch/stable/using-executorch-building-from-source.html>) 说明)。 - 在Windows上,视觉工作室2022或以后. ## 安装[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#installation>) 要使用 ExecuTorch,您需要同时安装 Python 软件包和合适的平台专用运行时库. Pip 是安装 ExecuTorch python 软件包的推荐方式. 这个软件包包括导出PyTorch模型所需的依赖性,以及用于模型测试和评价的Python运行时绑定。考虑在一个虚拟环境中安装ExecuTorch,例如由Python提供 [康达](<https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html#creating-environments>) 或 时 间 [阴道](<https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/#create-and-use-virtual-environments>). ``` pip install executorch ``` 为了在它们到达稳定释放之前获得最新特性,请安装一个夜间建造。 `main` 因此,一个已经着陆但尚未稳定释放的改变首先可以在那里得到。 ``` pip install executorch --pre --extra-index-url https://download.pytorch.org/whl/nightly/cpu ``` 从源头构建框架,参见: [从源建置](<https://docs.pytorch.org/executorch/stable/using-executorch-building-from-source.html>)后端代表可能需要额外的依附关系,请查看适当的后端文件以了解更多信息. > ***NOTE:*** 在 Windows 上, ExecuTorch 需要一个 [视觉工作室开发者 Powershell](<https://learn.microsoft.com/en-us/visualstudio/ide/reference/command-prompt-powershell?view=vs-2022>)。运行在开发者提示器之外,将显示为与CL.exe有关的错误。 --- ## 编写模型[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#preparing-the-model>) 导出是使用 PyTorch 模型并将其转换为 ExecuTorch 运行时使用的 .pte 文件格式的过程。这是使用 Python APIS 完成的。 用于常见模型的 PTE 文件, 如 Llama 3.2 , 可以在 HuggingFace 下找到 。 [ExecuTorch 社区](<https://huggingface.co/executorch-community>)。这些型号已经为ExecuTorch 输出和下调,可以直接部署,而无需经过下调过程。 一个完整的出口、降低和核查MobileNet V2的例子,可作为 [科拉布笔记本](<https://colab.research.google.com/drive/1qpxrXC3YdJQzly3mRg-4ayYiOjC6rue3?usp=sharing>). ### 所需经费[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#requirements>) - 一个PyTorch型号. - 示例模型输入, 通常作为 PyTorch 的 lotors。 您应该能够用这些输入成功运行 PyTorch 模型 。 - 一个或多个目标硬件后端. ### 选择后端[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#selecting-a-backend>) ExecuTorch为多种硬件提供硬件加速,最常用的后端是XNNPACK,用于Arm和x86的CPU,Core ML(用于iOS),Vulkan(用于Android GPU),以及Qualcomm(用于Qualcomm驱动的Android手机). 对于移动使用案例,考虑将Android和Core ML的XNNPACK或XNNPACK用作iOS的第一步. See. [硬件后端](<https://docs.pytorch.org/executorch/stable/backends-overview.html>) 以获取更多信息。 ### 导出[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#exporting>) 导出使用 Python APIs 完成. ExecuTorch 在导出过程中提供了高度定制,但典型的流量如下. 此示例使用 MobileNet V2 图像分类模型在火炬视像中执行,但该过程支持任意 [export-compliant](<https://pytorch.org/docs/stable/export.html>) PyTorch 模型。对于 Hugging Face 模型, 您可以在 [*huggingface/optimum-executorch*](<https://github.com/huggingface/optimum-executorch>) 复传. ``` 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 model = 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(model, sample_inputs), partitioner=[XnnpackPartitioner()] ).to_executorch() with open("model.pte", "wb") as f: f.write(et_program.buffer) ``` 如果模型需要不同的输入大小,则需要指定不同的尺寸和界限,作为 `export` 打电话,看 [模式导出和下调](<https://docs.pytorch.org/executorch/stable/using-executorch-export.html>) 以获取更多信息。 目标硬件后端由分区器参数控制到 `to_edge_transform_and_lower`。在此示例中,XnnpackPartitioner用于瞄准移动式CPU。见 [后端特定文档](<https://docs.pytorch.org/executorch/stable/backends-overview.html>) 用于了解如何使用每个后端。 在此阶段也可以进行量子化,以减少模型大小和运行时间. 量子化是后端特异性的,参见目标后端的文档,以完整描述支持的量子化方案. ### 测试模型[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#testing-the-model>) 在成功生成.pte文件后,通常使用Python运行时间API来验证开发平台上的模型,这可用于在运行在设备上之前评价模型精度. 对于此示例中使用的来自火炬视觉的MobileNet V2模型,图像输入被期望为具有(批量,通道,高度,宽度)维度的常态化,浮点32的变位器,输出为包含类对数的变位器. See. [火炬视觉. models.mobilenet_v2](<https://pytorch.org/vision/main/models/generated/torchvision.models.mobilenet_v2.html>) 以获取关于此模型的输入和输出变位器格式的更多信息。 关于图像预处理、频道第一和频道最后布局以及CV输出解码的更多指导,见 [与计算机视野模型合作](<https://docs.pytorch.org/executorch/stable/working-with-cv-models.html>). ``` import torch from executorch.runtime import Runtime from typing import List runtime = Runtime.get() input_tensor: torch.Tensor = torch.randn(1, 3, 224, 224) program = runtime.load_program("model.pte") method = program.load_method("forward") output: List[torch.Tensor] = method.execute([input_tensor]) print("Run successfully via executorch") from torchvision.models.mobilenetv2 import MobileNet_V2_Weights import torchvision.models as models eager_reference_model = models.mobilenetv2.mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT).eval() eager_reference_output = eager_reference_model(input_tensor) print("Comparing against original PyTorch module") print(torch.allclose(output[0], eager_reference_output, rtol=1e-3, atol=1e-5)) ``` 关于出口和运行模型的完整实例,请参见我们 [示例 GitHub 存储器](<https://github.com/meta-pytorch/executorch-examples/tree/main/mv2/python>). 此外,对于Hugging Face型号, [*huggingface/optimum-executorch*](<https://github.com/huggingface/optimum-executorch>) 库简化这些模型的运行end-to-end与ExecuTorch使用熟悉的Hugging FaceAPIs. 访问寄存器以获取具体实例和支持的模型. --- ## 在设备上运行[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#running-on-device>) ExecuTorch在Java,Object-C和C++中提供运行时间API. 快速链接 : - [机器人](<https://docs.pytorch.org/executorch/stable/getting-started.html#android>) - [iOS 系统](<https://docs.pytorch.org/executorch/stable/getting-started.html#ios>) - [C++](<https://docs.pytorch.org/executorch/stable/getting-started.html#c>) ### 机器人[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#android>) #### 安装[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#id1>) ExecuTorch为Android使用提供了Java绑定,从Java和Kotlin都可以消耗. 要将库添加到您的应用中,在gradle构建规则中加入以下依赖性. ``` # app/build.gradle.kts dependencies { implementation("org.pytorch:executorch-android:${executorch_version}") } # See latest available versions in https://mvnrepository.com/artifact/org.pytorch/executorch-android ``` #### 运行时间 API[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#runtime-apis>) 模型可以使用 Java 或 Kotlin 装入和运行 `Module` 班级。 ``` import org.pytorch.executorch.EValue; import org.pytorch.executorch.Module; import org.pytorch.executorch.Tensor; // … Module model = Module.load("/path/to/model.pte"); Tensor input_tensor = Tensor.fromBlob(float_data, new long[] { 1, 3, height, width }); EValue input_evalue = EValue.from(input_tensor); EValue[] output = model.forward(input_evalue); float[] scores = output[0].toTensor().getDataAsFloatArray(); ``` 请注意, [C++](<https://docs.pytorch.org/executorch/stable/getting-started.html#c>) API在瞄准Android本土时可以使用. 关于在Android上运行模型的完整例子,请参见: [DeepLabV3AndroidDemo](<https://github.com/meta-pytorch/executorch-examples/tree/main/dl3/android/DeepLabV3Demo>)。关于Android开发的更多信息,包括从源头建构,Java API的完整描述,以及使用来自Android本地代码的ExecuTorch的信息,参见: [在Android上使用 ExecuTorch 软件](<https://docs.pytorch.org/executorch/stable/using-executorch-android.html>). ### iOS 系统[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#ios>) #### 安装[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#id2>) ExecuTorch通过C++支持iOS和macOS,以及CoreML和CPU的硬件后端. iOS运行时库作为.xcframework目标集提供,并作为Swift PM软件包提供. 要从 Xcode 开始,请到文件 > 添加软件包依赖。粘贴URL会 议 日 程 和 议 程ExecuTorch重新插入搜索栏并选择它。 请确定将分支名称更改为想要的ExecuTorch格式为“swiftpm-” (例如“b”)swiftpm-1.5.1(b) 与《公约》第2条有关的其他事项。ExecuTorch依赖性也可以手动添加到软件包文件中。参见 [使用 ExecuTorch 在 iOS 上](<https://docs.pytorch.org/executorch/stable/using-executorch-ios.html>) 以获取更多信息。 #### 运行时间 API[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#id3>) 模型可以使用C++ API从Object-C加载和运行. 关于iOS集成的更多信息,包括API的参考文献,日志设置,以及从源建置,参见: [使用 ExecuTorch 在 iOS 上](<https://docs.pytorch.org/executorch/stable/using-executorch-ios.html>). ### C++[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#c>) ExecuTorch提供C++ API,可用于瞄准嵌入式或移动式设备. C++ API比其他语言绑定提供了更高的控制水平,允许高级内存管理,数据加载,以及平台集成. #### 安装[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#id4>) 在Linux和macOS上,最快速的路线是pip包,它将运行时间作为预建的库,带有头和CMake包,所以没有任何东西可以建造: ``` pip install executorch ``` ``` find_package(executorch REQUIRED COMPONENTS kernels_optimized) target_link_libraries(my_target PRIVATE executorch::runtime executorch::kernels_optimized) ``` 见 [使用 Pip 软件包中预建的库](<https://docs.pytorch.org/executorch/stable/using-executorch-cpp.html#using-the-prebuilt-libraries-from-the-pip-package>) 完全的走过,和 [使用 CUDA 软件包运行在 GPU 上](<https://docs.pytorch.org/executorch/stable/using-executorch-cpp.html#running-on-a-gpu-with-the-cuda-package>) 用于GPU支持. 对于一个平台, 软件包不覆盖, 或者要更改构建选项, 而是从源头构建 。 CMake 是 ExecuTorch C++ 运行时间的首选构建系统。 要与 CMake 一起使用, 克隆 ExecuTorch 寄存器作为您的项目的子目录, 并使用 CMake 的 `add_subdirectory("executorch")` 包括依赖。 `executorch` 目标,以及内核和后端目标将被提供链接。运行时间也可以单独构建,以支持多种工具链。见 [使用带有 C++ 的 ExecuTorch](<https://docs.pytorch.org/executorch/stable/using-executorch-cpp.html>) 财务报告和财务报告 [建置来源](<https://docs.pytorch.org/executorch/stable/using-executorch-building-from-source.html>) 详细描述建筑一体化、目标和交叉汇编。 ``` git clone -b release/1.5 https://github.com/pytorch/executorch.git ``` ``` # Set CMAKE_CXX_STANDARD to 17 or above. set(CMAKE_CXX_STANDARD 17) # CMakeLists.txt set(EXECUTORCH_BUILD_PRESET_FILE ${CMAKE_SOURCE_DIR}/executorch/tools/cmake/preset/llm.cmake) # Set other ExecuTorch options here. add_subdirectory("executorch") ... target_link_libraries( my_target PRIVATE executorch executorch::backends executorch::extensions executorch::kernels) ``` #### 运行时间 API[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#id5>) 提供高层次和低层次的C++ API. 低层次的API是平台独立的,不动态分配内存,并且最适合资源约束的嵌入式系统. 高层次API是作为便利包装器在低层次API周围提供的,并使用动态内存分配和标准库构造来降低动词. ExecuTorch使用CMake用于原生建筑. 整合一般通过克隆ExecuTorch寄存器,使用CMake add_subdirectory添加依赖性来实现. 使用高级API加载和运行一个模型,可以如下方式完成: ``` #include <executorch/extension/module/module.h> #include <executorch/extension/tensor/tensor.h> using namespace ::executorch::extension; // Load the model. Module module("/path/to/model.pte"); // Create an input tensor. float input[1 * 3 * 224 * 224]; auto tensor = from_blob(input, {1, 3, 224, 224}); // Perform an inference. const auto result = module.forward(tensor); if (result.ok()) { // Retrieve the output data. const auto output = result->at(0).toTensor().const_data_ptr<float>(); } ``` 有关C++ API的更多信息,参见: [使用 C++ 模块扩展运行一个 ExecuTorch 模型](<https://docs.pytorch.org/executorch/stable/extension-module.html>) 财务报告和财务报告 [在 C++ 中管理 Tensor 内存](<https://docs.pytorch.org/executorch/stable/extension-tensor.html>). 关于构建和运行 C++ 应用程序的全部实例,请参见我们 [示例 GitHub 存储器](<https://github.com/meta-pytorch/executorch-examples/tree/main/mv2/cpp>). --- ## 下一个步骤[#](<https://docs.pytorch.org/executorch/stable/getting-started.html#next-steps>) ExecuTorch 提供了高度自定义支持多种硬件目标。 根据您的使用大小写, 考虑探索以下一个或多个页面: - [导出和下调](<https://docs.pytorch.org/executorch/stable/using-executorch-export.html>) 用于高级模型转换选项。 - [后端概览](<https://docs.pytorch.org/executorch/stable/backends-overview.html>) 对于可用的后端和配置选项。 - [在Android上使用 ExecuTorch 软件](<https://docs.pytorch.org/executorch/stable/using-executorch-android.html>) 财务报告和财务报告 [使用 ExecuTorch 在 iOS 上](<https://docs.pytorch.org/executorch/stable/using-executorch-ios.html>) 用于移动运行时间集成. - [使用带有 C++ 的 ExecuTorch](<https://docs.pytorch.org/executorch/stable/using-executorch-cpp.html>) 用于嵌入式和移动式本土开发。 - [剖析和调试](<https://docs.pytorch.org/executorch/stable/using-executorch-troubleshooting.html>) 用于开发者的工具和调试。 - [API 参考文献](<https://docs.pytorch.org/executorch/stable/export-to-executorch-api-reference.html>) 用于完整描述可用的API。 - [实例](<https://github.com/pytorch/executorch/tree/main/examples>) 用于演示应用程序和示例代码。
查看原始来源