ExecuTorch 在 Arm Ethos-U 上的端侧部署入门
面向 Arm Ethos-U 后端的 ExecuTorch 入门流程,涵盖用 FVP 模拟 Corstone-300/320 硬件、AOT 导出 .pte 二进制以及用脚本在模拟硬件上运行可执行文件。
## 开始教学[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#getting-started-tutorial>) 我们建议你在这之前完成课程: - [ExecuTorch介绍](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#intro-how-it-works.md>) - [开始](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#getting-started.md>) - [使用 CMake 构建 ExecuTorch](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#using-executorch-building-from-source.md>) 在这个教程中你会学到什么 : 在此教程中, 您将学习如何为 PyTorch 后端导出一个简单的 ExecuTorch 模型 。 提示( T) 如果你已经熟悉这个代表,你可能希望直接跳到例子上: - [ExecuTorch寄存器中的例子](<https://github.com/pytorch/executorch/tree/main/examples/arm>) - [用于快速测试和实例模型的命令行编译器](<https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py>) 这个教程作为使用ExecuTorch在Arm® EthosTM-U目标上部署PyTorch模型的介绍. `ethos_u_minimal_example.ipynb`,在Arm的示例文件夹中提供。 ## 先决条件[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#prerequisites>) ### 硬件[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#hardware>) 要成功完成此教程,您需要一台具有arch64或x86_64处理器架构的Linux机,或一台带有Apple®硅的macOS TM机. 为了在没有具体发展理事会的情况下促进发展,我们将利用 [固定虚拟平台(FVP).](<https://www.arm.com/products/development-tools/simulation/fixed-virtual-platforms>)模拟 [Arm® CorstoneTM - 300 火箭炮](<https://developer.arm.com/Processors/Corstone-300>)(cs300)和 (cs300) (中文(简体) ). [Arm® CorstoneTM - 320 火箭炮](<https://developer.arm.com/Processors/Corstone-320>)(cs320)系统,把它看作是虚拟硬件. ### 软件[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#software>) 首先, 您需要安装 ExecuTorch 。 请遵循推荐的教程来设置一个有效的 ExecuTorch 开发环境 。 除此之外,您还需要安装一些用于生成 Ethos- U 命令流的 SDK 依赖性。 用于自动化此命令流的脚本在主中可用 。 [ExecuTorch 仓库](<https://github.com/pytorch/executorch/tree/main/examples/arm/>)。安装 Ethos-U 依赖,运行 ``` ./examples/arm/setup.sh --i-agree-to-the-contained-eula ``` 这将安装 : - [TOSA 序列化库](<https://gitlab.arm.com/tosa/tosa-tools>) 用于将 Exir IR 图表序列化为 TOSA IR . - [Ethos- U Vela 图表编译器](<https://pypi.org/project/ethos-u-vela/>) 用于将 TOSA 平流器编译成 Ethos-U 命令流。 - [武装 GNU 工具链](<https://developer.arm.com/Tools%20and%20Software/GNU%20Toolchain>) 用于交叉编译。 - [Corstone SSE-300 FVP 维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球.](<https://developer.arm.com/documentation/100966/1128/Arm--Corstone-SSE-300-FVP>) 用于在Ethos-U55参考设计上的测试. - [Corstone SSE-320 FVP 维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球.](<https://developer.arm.com/documentation/109760/0000/SSE-320-FVP>) 用于在Ethos-U85参考设计上的测试. ## 设置开发者环境[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#set-up-the-developer-environment>) 设置.sh 脚本生成一个 setup_path.sh 脚本,当您重新启动您的 shell 时您需要源代码。 运行 : 简单的检查环境设置是否正确, 运行 `which FVP_Corstone_SSE-320` 并且确保可执行文件位于您期望的地方,位于 `examples/arm` 树。 ## 构建[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#build>) ### 前期( AOT ) 组件[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#ahead-of-time-aot-components>) ExecuTorch前期(AOT)管道采用PyTorch型号(a). `torch.nn.Module`)并生成 `.pte` 二进制文件, 然后由 ExecuTorch 运行时消耗。 this [文档](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#getting-started-architecture.md>) 更深入地介绍ExecuTorch软件堆栈,既用于AoT,也用于运行时间. 下面的例子说明了如何将一个由单一添加组成的模型量化,然后使用EthosU后端通过AOT流量导出. 详见: `examples/arm/ethos_u_minimal_example.ipynb`. ``` import torch class Add(torch.nn.Module): def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: return x + y example_inputs = (torch.ones(1,1,1,1),torch.ones(1,1,1,1)) model = Add() model = model.eval() exported_program = torch.export.export(model, example_inputs) graph_module = exported_program.module(check_guards=False) from executorch.backends.arm.ethosu import EthosUCompileSpec from executorch.backends.arm.quantizer import ( EthosUQuantizer, get_symmetric_quantization_config, ) from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e # Create a compilation spec describing the target for configuring the quantizer # Some args are used by the Arm Vela graph compiler later in the example. Refer to Arm Vela documentation for an # explanation of its flags: https://gitlab.arm.com/artificial-intelligence/ethos-u/ethos-u-vela/-/blob/main/OPTIONS.md compile_spec = EthosUCompileSpec( target="ethos-u55-128", system_config="Ethos_U55_High_End_Embedded", memory_mode="Shared_Sram", ) # Create and configure quantizer to use a symmetric quantization config globally on all nodes quantizer = EthosUQuantizer(compile_spec) operator_config = get_symmetric_quantization_config() quantizer.set_global(operator_config) # Post training quantization quantized_graph_module = prepare_pt2e(graph_module, quantizer) quantized_graph_module(*example_inputs) # Calibrate the graph module with the example input quantized_graph_module = convert_pt2e(quantized_graph_module) # Create a new exported program using the quantized_graph_module quantized_exported_program = torch.export.export(quantized_graph_module, example_inputs) from executorch.backends.arm.ethosu import EthosUPartitioner from executorch.exir import ( EdgeCompileConfig, ExecutorchBackendConfig, to_edge_transform_and_lower, ) from executorch.extension.export_util.utils import save_pte_program # Create partitioner from compile spec partitioner = EthosUPartitioner(compile_spec) # Lower the exported program to the Ethos-U backend edge_program_manager = to_edge_transform_and_lower( quantized_exported_program, partitioner=[partitioner], compile_config=EdgeCompileConfig( _check_ir_validity=False, ), ) # Convert edge program to executorch executorch_program_manager = edge_program_manager.to_executorch( config=ExecutorchBackendConfig(extract_delegate_segments=False) ) # Save pte file save_pte_program(executorch_program_manager, "ethos_u_minimal_example.pte") ``` 提示( T) 快速测试,你可以使用脚本 `backends/arm/scripts/aot_arm_compiler.py` 。要生成相当于上面一个的 pte 文件,请运行 `python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`。用于生产,您应当使用上面显示的稳定的 Python API 。 ### 运行时间 :[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#runtime>) 完成 AOT 编译流程后,运行时间可以被交叉编译并与生成的连接 `.pte`- 使用 Arm 跨编译工具链的文件 。 配置独立的 Arm 执行器运行器 CMake 工程, 以拉入 ExecuTorch 构建图, 链接 Ethos- U 代表, 并生成任何非授权操作的内核绑定 。 `arm_executor_runner` 程序将运行在目标。 ``` # In ExecuTorch top-level, with sourced setup_path.sh cmake -S examples/arm/executor_runner/standalone \ -B ethos_u_minimal_example \ -DEXECUTORCH_ROOT=$(pwd) \ -DCMAKE_TOOLCHAIN_FILE=$(pwd)/examples/arm/ethos-u-setup/arm-none-eabi-gcc.cmake \ -DCMAKE_BUILD_TYPE=Release \ -DET_PTE_FILE_PATH=ethos_u_minimal_example.pte \ -DTARGET_CPU=cortex-m55 \ -DETHOSU_TARGET_NPU_CONFIG=ethos-u55-128 \ -DMEMORY_MODE=Shared_Sram \ -DSYSTEM_CONFIG=Ethos_U55_High_End_Embedded cmake --build ethos_u_minimal_example -j$(nproc) -- arm_executor_runner ``` 提示( T) 首先,你可以使用脚本 `backends/arm/scripts/build_executor_runner.sh` 以配置和构建独立的导师。要构建一个相当于上面的导师,请运行 `./backends/arm/scripts/build_executor_runner.sh --pte=ethos_u_minimal_example.pte` 下面的块图显示,在高水平上,各种建筑文物是如何生成的,并被连接在一起,以生成最终的裸金属可执行性.  ## 在 Corstone FVP 平台上运行[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#running-on-corstone-fvp-platforms>) 最后,请使用 `backends/arm/scripts/run_fvp.sh` 在模拟的Arm硬件上运行.elf文件的实用脚本. ``` backends/arm/scripts/run_fvp.sh --elf=$(find ethos_u_minimal_example -name arm_executor_runner) --target=ethos-u55-128 ``` 示例应用程序默认是用一个输入的输入来构建的,因此定量添加的预期结果应该接近2. ## 外卖[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#takeaways>) 在此教程中, 您已经学会了如何使用 ExecuTorch 将 PyTorch 模型导出到可执行文件, 可以在嵌入式目标上运行, 然后在模拟硬件上运行可执行文件 。 要学习更多, 请检查是否 [武装实用实验室的ExecuTorch](<https://github.com/arm-education/executorch_on_arm_labs>) 该系列为在Arm上与ExecuTorch一起开发提供了结构化的切入点,跨越了CPU和Ethos-U NPU. 对于快速学习路径, 显示简短的教程: - [运行Llama3在树莓皮上5与ExecuTorch](<https://learn.arm.com/learning-paths/embedded-and-microcontrollers/rpi-llama3/>) - [可视化 Ethos-U NPU 对 FVP 的性能](<https://learn.arm.com/learning-paths/embedded-and-microcontrollers/visualizing-ethos-u-performance/>) - [在NXP i.MX 93(Ethos-U65)上与ExecuTorch进行图像分类.](<https://learn.arm.com/learning-paths/embedded-and-microcontrollers/observing-ethos-u-on-nxp/>) - [Alif上的图像分类与ExecuTorch E8 DevKit(Ethos-U85 )](<https://learn.arm.com/learning-paths/embedded-and-microcontrollers/alif-image-classification/>) ## 问题[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html#faqs>) 如果您在此教程后遇到任何错误或问题, 请在此存档 。 [吉图布](<https://github.com/pytorch/executorch/issues/new>). ``` Arm is a registered trademark of Arm Limited (or its subsidiaries or affiliates). ```
查看原始来源