ExecuTorch 的 Arm Ethos-U 后端编译与量化流程
该文档介绍 ExecuTorch 的 Arm Ethos-U 后端,说明其算子委托、量化器与运行时集成特性,并给出以 EthosUCompileSpec 为核心的编译配置方式,以及图像分类和 MobileSAM 两个端到端示例。
## 武装 Ethos- U 后端[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-overview.html#arm-ethos-u-backend>) Arm® EthosTM-U后端目标为Edge/IoT型AI使用套件,实现最佳执行量子化模型. [Arm® EthosTM - U55 NPU (英语).](<https://www.arm.com/products/silicon-ip-cpu/ethos/ethos-u55>), [Arm® EthosTM - U65 NPU (英语).](<https://www.arm.com/products/silicon-ip-cpu/ethos/ethos-u65>),以及 [Arm® EthosTM - U85 NPU (英语).](<https://www.arm.com/products/silicon-ip-cpu/ethos/ethos-u85>),发挥杠杆作用 [TOSA](<https://github.com/arm/tosa-specification>) 页:1 [ethos-u-vela](<https://pypi.org/project/ethos-u-vela/>) 图表编译器。此文档是使用 Ethos- U 后端的技术参考,用于带有代码示例的顶层视图。请参考 [武装 Ethos- U 后端教程](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html>). ## 特征[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-overview.html#features>) - 广泛的操作员支持将大部型号下放到高度优化和低功率的Ethos-U NPU. - 为NPU目标优化量子化的量子. - 简单硬件的运行时间集成实例 。 ## 目标所需资源[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-overview.html#target-requirements>) 目标系统必须包括一个Ethos-U NPU. ## 发展要求[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-overview.html#development-requirements>) 提示( T) 所有要求都可用下列方式下载: `examples/arm/setup.sh --i-agree-to-the-contained-eula` 并使用 `source examples/arm/arm-scratch/setup_path.sh`. 注意这意味着接受使用下载软件所需的最终用户许可协议(EULA:s)。 对于 AOT 流, 汇编一个模型到 `.pte` 使用 Ethos-U 后端的格式,要求是: - [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 命令流。 用于构建和运行可用示例应用程序 `examples/arm/executor_runner/` 通过独立的 CMake 切入点: - [武装 GNU 工具链](<https://developer.arm.com/Tools%20and%20Software/GNU%20Toolchain>) 用于交叉编译。 - [阿尔姆-科尔斯通TMSSE-300FVP](<https://developer.arm.com/documentation/100966/1128/Arm--Corstone-SSE-300-FVP>) 用于在Arm® Cortex®-M55 +Ethos-U55参考设计上进行测试. - [阿尔姆-科尔斯通TMSSE-320FVP](<https://developer.arm.com/documentation/109760/0000/SSE-320-FVP>) 用于在Arm® Cortex®-M85 +Ethos-U85参考设计上进行测试. 固定虚拟平台(FVPs)是Arm为方便嵌入式开发提供的免费模拟器,不需要物理开发板. ## 使用 Arm Ethos- U 后端[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-overview.html#using-the-arm-ethos-u-backend>) 降低的主要配置点是: `EthosUCompileSpec` 由分区器和定量器消耗。完整用户化的API在下文中记录。 ``` class EthosUCompileSpec(target: str, system_config: str | None = None, memory_mode: str | None = None, extra_flags: list[str] | None = None, config_ini: str | None = 'Arm/vela.ini', external_block_placements: executorch.backends.arm.ethosu.compile_spec.VelaExternalBlockPlacements | None = None) ``` 规范 Ethos-U 编译配置和编译器旗. 参数 : - **目标( str)**: Ethos-U加速器配置(例如, `"ethos-u55-128"`). - **system_config (str) (中文(简体) ). | 无)**: 来自 Vela 配置文件的系统配置名称。默认基于 `target` 当省略时。 - **memory_mode (str) (中文(简体) ). | 无)**: 从 Vela 配置文件中选择内存模式。 默认基于 `target` 当省略时。 - **extra_flags(名单)[字符串] | 无)**: Vela 附加命令行旗. - **config_ini (str) (中文(简体) ). | 无)**: Vela.ini 配置文件的路径。默认为 `"Arm/vela.ini"`. - **external_block_placements (VelaExternalBlockPlacements | 无)**: 命令和重量数据,用其放置标记作为命名数据发出. ``` def EthosUCompileSpec.dump_debug_info(self, debug_mode: executorch.backends.arm.common.arm_compile_spec.ArmCompileSpec.DebugMode | None): ``` 倾斜调试信息进入中间路段. 参数 : - **debug_mode**:用于倾倒调试信息的调试模式. ``` def EthosUCompileSpec.dump_intermediate_artifacts_to(self, output_path: str | None): ``` 设定一个倾倒TOSA和PTE中间结果的路径. 参数 : - **output_path**: 向中间结果倾斜路径。 ``` def EthosUCompileSpec.set_pass_pipeline_config(self, config: executorch.backends.arm.common.pipeline_config.ArmPassPipelineConfig) -> None: ``` 设定 Arm pass 管道的配置 。 参数 : - **配置**: 设置的自定义 ArmPassPipelineConfig. ``` class VelaExternalBlockPlacements(cmd_data: str | None = None, weight_data: str | None = None) -> None ``` 外部 Vela 命令和重量数据的放置标记 。 标记是任意的构建时间标识符,用于将命名数据分组到外部的 PTD 输出中。部署决定这些文物的居住地点,并将其供给运行时间 。 属性 : - **cmd_data**: 命令数据定位标签,或 `None` 保持内线。 - **weight_data**: 重量数据定位标记,或 `None` 保持内线。 ``` def VelaExternalBlockPlacements.to_block_placements(self) -> dict[str, str]: ``` 返回按 Vela 块名键的配置的放置标记 。 ### 分区器 API[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-overview.html#partitioner-api>) 见 [分区器 API](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-partitioner.html>) 用于了解分区员API的更多信息。 ## 量化[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-overview.html#quantization>) 由于 Ethos- U 后端是纯整数的,所以在 NPU 上要执行的所有运算符都需要定量。 Ethos- U 的定量器支持 [培训后量化(PT2E)](<https://docs.pytorch.org/ao/main/pt2e_quantization/pt2e_quant_ptq.html>) 财务报告和财务报告 [量化-智能培训(QAT).](<https://docs.pytorch.org/ao/main/pt2e_quantization/pt2e_quant_qat.html>) 量论. 有关量化的更多信息,请参见: [量化](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-quantization.html>) ## 运行时间集成[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-overview.html#runtime-integration>) 运行时间应用程序的示例 [examples/arm/executor_runner](<https://github.com/pytorch/executorch/blob/main/examples/arm/executor_runner/>),带有独立的 CMake 切入点 `examples/arm/executor_runner/standalone`在FVP上建造和部署该设备所需的步骤已在上文提到的表格中加以说明。 [武装 Ethos- U 后端教程](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html>)。该实例应用程序被推荐用于测试您降低的模型的基本功能,以及开发针对您自己的目标的运行时集成的起点。对于executor_runner的架构和进行这种集成所需的步骤,请参见: [Ethos-U 端口指南](<https://github.com/pytorch/executorch/blob/main/examples/arm/ethos-u-porting-guide.md>). ### 示例:图像分类流程[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-overview.html#example-image-classification-flow>) [`examples/arm/image_classification_example_ethos_u`](<https://github.com/pytorch/executorch/tree/main/examples/arm/image_classification_example_ethos_u>) 包含一个完全基于 DeiT 的导出和运行时间走过。 README 显示如何运行 `model_export/export_deit.py`,构建样本固件,并将测试图像转换成C阵列,这样本指南中描述的工作流程就可以尝试结束。 [`examples/arm/mobilesam_prompt_segmentation_example_ethos_u`](<https://github.com/pytorch/executorch/tree/main/examples/arm/mobilesam_prompt_segmentation_example_ethos_u>) 包含一个完整MobileSAMEthos 的快速分区工作流程U85,包括固定即时出口,PT2E量化、变压器降低、调试口罩和上盖,一种光金属Corstone-320运行时间应用程序,以及FVP执行。 ### Ethos- U 内存模式[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-overview.html#ethos-u-memory-modes>) Ethos-U NPU提供了两种截然不同的内存界面: - 一个接口 **低频带宽存储器**. - 在所有Ethos-U NPU上(Ethos-U55,Ethos-U65,Ethos-U85),低纬度内存通常为SoC的SRAM. - 一个接口 **频率较高、带宽较低的内存**,典型的外部(离芯片)内存. - 在低功率微控制器上,外部内存通常为Flash. - 在有Arm® CortexTM-A和丰富的操作系统的系统中,外部内存一般是DRAM. 运行推论时, Ethos-U 编译器和 Ethos-U 驱动程序使用三个逻辑内存区域: - Ethos-U抓痕缓冲器 - NPU用来存储推论中产生和消耗的中间伸缩器的一个毗连的内存块. - 神经网络(Neural Network) - 一个毗连的内存区块,存储恒定数据,如权重,偏差,进行推论所需的定量参数. - Ethos-U快速抓取缓冲器——一个毗连的内存块,假设存在于芯片内存中,以隐藏外部内存的更高延迟/较低的带宽. Ethos-U65和Ethos-U85适用于带有Cortex-A的系统,外部内存被假定为DRAM. 抓取缓冲器和神经网络的放置决定了用于 `EthosUCompileSpec` 和在构建executor_runner时支持三种不同的内存模式: | 内存模式 | Ethos-U 缓冲设置 | 神经网络定位 | 何时使用 | 权衡 | | --- | --- | --- | --- | --- | | **仅限SRAM** | 芯片 SRAM | 芯片 SRAM | 当ML模型、Ethos-U抓痕缓冲器和更大的软件堆栈符合SRAM会 议 日 程 和 议 程SoC | 受SRAM大小限制;对于较大的NN来说往往不可行 | | **共享 - SRAM** | 芯片 SRAM | 外部内存( Flash/ DRAM ) | Cortex-M和Ethos-U系统中最常见的模式;平衡良好的性能和SRAM使用 | 需要足够的 SRAM 来持有最大的中间伸缩器 | | **专用- SRAM** | 外部内存 | 外部内存( Flash/ DRAM ) | Cortex- A 和 Ethos- U 系统最常见的模式。 对于峰值中间体无法适应SRAM 的非常大的模式 | 需要高带宽的外部内存来提供良好的性能 | 以下是对不同模式的深入解释: #### 1. 仅存储模式[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-overview.html#sram-only-memory-mode>) - Ethos-U刮痕缓冲器位于SRAM. - 神经网络位于SRAM. - Ethos-U快速刮伤缓冲器没有使用. - 特征 : - 提供自所有内存流量通过低纬度/高带宽内存后的最佳性能. - 业绩提升对于外部接口内存工作量尤其显著。 - 可在Ethos-U55,Ethos-U65和Ethos-U85上查阅. - 限制: - 嵌入式SoCs经常有有限的SRAM,NNs也变得越来越大,相对于SoC上可用的SRAM数量来说,这种内存模式对于运行一个大模型的系统可能不合适. 下面可以看到Sram Only配置的两个逻辑内存区域的位置的视觉表现.  #### 2. 共享 Sram 内存模式[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-overview.html#shared-sram-memory-mode>) - Ethos-U刮痕缓冲器位于SRAM. - 神经网络位于外部记忆中。 - Ethos-U快速刮伤缓冲器没有使用. - 特征 : - 中间的抗震器被储存在SRAM中,利用它的低纬度和高带宽. - Ethos-U编译器可以提前将权重从外部内存到SRAM,这样当NPU需要数据时,它就已经是芯片内存中的avaialbe. - 在此模式下,外部界面为 Read-Only, 芯片内存界面为 Read/ Write - 共享Sram在性能和低SRAM使用之间提供了极大的平衡. - 可在Ethos-U55,Ethos-U65和Ethos-U85上查阅. - 限制: - 您需要在 SRAM 中有足够的空间来保持峰值中间角。 下面可以看到对 Shared_Sram 配置的两个逻辑内存区域的位置的视觉表现 。  #### 3. 专用- Sram 内存模式[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-overview.html#dedicated-sram-memory-mode>) - Ethos-U抓痕缓冲器位于外部内存中. - 神经网络位于外部记忆中。 - Ethos-U快速刮伤缓冲器位于芯片记忆中. - 特征 : - 当峰值中间拉伸器太大,无法融入芯片存储器时使用. - 启用大型模型的硅加速. - NPU将中间计算的结果存储在外部内存中. - 专用的SRAM起到软件管理缓存的作用,通过在芯片内存上对经常访问的连载器进行预比来改进性能. - 可在Ethos-U65和Ethos-U85上查阅. - 限制: - SRAM空间必须被快速指定给Ethos-U(主机处理器不应该访问它). - 在Ethos-U55上不可用. 下面可以看到Shared_Sram配置的两个逻辑内存区域的位置的视觉表现.  内存模式定义于 [vela.ini 文件](<https://gitlab.arm.com/artificial-intelligence/ethos-u/ethos-u-vela/-/blob/main/ethosu/config_files/Arm/vela.ini?ref_type=heads>)。当您为 Ethos-U 后端安装 ExecuTorch 时,您会自动安装包含 vela.ini 文件的编译器,这样您就可以直接用这些内存模式创建编译规格. ## 参考[#](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-overview.html#reference>) **→[分区器 API](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-partitioner.html>) - 分区选择。** **→[量化](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-quantization.html>) 支持的量化计划。** **→[武装Ethos-U 解决问题](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/arm-ethos-u-troubleshooting.html>) - 解决问题和共同问题。** **→[开始教学](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/tutorials/ethos-u-getting-started.html>) ——开始教书.** **→[EdgeIR 操作员支持 U55 后端](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/U55_op_support.html>) ——Ethos - U55支持操作员.** **→[EdgeIR 操作员支持 U85 后端](<https://docs.pytorch.org/executorch/stable/backends/arm-ethos-u/U85_op_support.html>) ——Ethos - U85支持操作员.**
查看原始来源