通过ExecuTorch将模型委托给eIQ Neutron NPU
该教程介绍在x86_64 Linux机器上使用ExecuTorch的AoT流程,把PyTorch模型转换为ExecuTorch格式,并借助eIQ Neutron后端将计算委托给Neutron NPU,同时通过NSYS行为模拟器运行验证。
## 开始使用 eIQ 中子 NPU ExecuTorch 后端[#](<https://docs.pytorch.org/executorch/stable/backends/nxp/tutorials/nxp-basic-tutorial.html#getting-started-with-eiq-neutron-npu-executorch-backend>) ## 优先性[#](<https://docs.pytorch.org/executorch/stable/backends/nxp/tutorials/nxp-basic-tutorial.html#prerequisities>) ### 硬件[#](<https://docs.pytorch.org/executorch/stable/backends/nxp/tutorials/nxp-basic-tutorial.html#hardware>) 对于此教程, 您需要一台带有 x86_64 处理器架构的 Linux 机器。 此教程演示了使用 eIQ 中子后端的中子行为模拟器, 叫做 NSYS 。 您不需要此教程的特定开发板 。 ### 软件[#](<https://docs.pytorch.org/executorch/stable/backends/nxp/tutorials/nxp-basic-tutorial.html#software>) 首先需要安装 Python 3.10 - 3.12. 您需要安装 ExecuTorch 。 请跟随教程安装 ExecuTorch [从 ExecuTorch 开始](<https://docs.pytorch.org/executorch/main/getting-started.html>) 除此之外,您还需要安装 eIQ 中子模拟器,叫做NSYS,用于生成 eIQ 中子的字节代码的中子转换器NPU,在模型转换中ExecuTorchAoT。要安装 eIQ 中子依赖,请运行 : ``` examples/nxp/setup.sh ``` 这将安装 : - 中子转换器,用于将中子 IR 转换为中子字节码 - eIQ 中子SDK,包含各种 eIQ 中子运行时间(驱动器和固件)NXPSoC和模拟器 - eIQ NSYS,中子行为模拟器 ## 为 NXP eIQ 中子后端准备模型[#](<https://docs.pytorch.org/executorch/stable/backends/nxp/tutorials/nxp-basic-tutorial.html#preparing-a-model-for-nxp-eiq-neutron-backend>) 本指南演示使用ExecuTorch AoT流将一个PyTorch模型转换为ExecuTorch格式,并将模型计算委托给eIQ中子NPU使用eIQ中子后端. ### Step 1:环境设置[#](<https://docs.pytorch.org/executorch/stable/backends/nxp/tutorials/nxp-basic-tutorial.html#step-1-environment-setup>) 此教程拟从 Linux 运行, 并使用 Conda 或 Virtual Env 进行 Python 环境管理。 完整的设置细节和系统要求请参见 [从 ExecuTorch 开始](<https://docs.pytorch.org/executorch/main/getting-started.html>). 创建康达环境并安装ExecuTorch Python包. ``` conda create -y --name executorch python=3.12 conda activate executorch ``` 并安装 ExecuTorch Python 软件包, 要么是一个预建的软件包 : ``` conda install executorch ``` 或从源创建 : ``` ./install_executorch.sh ``` 还要运行 `setup.sh` 安装 eIQ 中子依赖的脚本 : ``` $ ./examples/nxp/setup.sh ``` ### Step 2: 目标模型的制作和运行[#](<https://docs.pytorch.org/executorch/stable/backends/nxp/tutorials/nxp-basic-tutorial.html#step-2-model-preparation-and-running-the-model-on-target>) 参见实例 `aot_neutron_compile.py` 财务报告和已审计财务报表 [README](<https://github.com/pytorch/executorch/blob/main/examples/nxp/README.md>) 文档。 为了这个教程的目的,我们将使用一个简单的图像分类模型CifarNet10. ``` python -m examples.nxp.aot_neutron_compile --quantize \ --delegate -m "cifar10" ``` 另外,我们将从 Cifar 10 数据集中将少数图像丢弃到文件夹中: ``` python -m examples.nxp.experimental.cifar_net.cifar_net --store-test-data ``` 目标文件夹是 `./cifar10_test_data`. ## 使用 NSYS 运行时间[#](<https://docs.pytorch.org/executorch/stable/backends/nxp/tutorials/nxp-basic-tutorial.html#runtime-with-nsys>) ### nxp_executor_runner 实例[#](<https://docs.pytorch.org/executorch/stable/backends/nxp/tutorials/nxp-basic-tutorial.html#nxp-executor-runner-example>) 那个...end-to-end示例,说明使用 eIQ 中子NPU设于 `examples/nxp/executor_runner`在着手建造和执行之前,让我们先简单介绍一下这个例子。 应用程序运行在特定模型上的执行器。如果向跑者提供了授权的模型,例如本教程中的指示 `DelegateCall` 拥有 `NeutronBackend` ID在模型中存在. 本指令被ExecuTorch运行时间识别并移交给了NeutronBackend. NeutronBackend运行于由NSYS模拟的eIQ中子NPU上. NeutronDriver 的 API (中文(简体) ).`NeutronDriver.h` 财务报告和财务报告 `NeutronErrors.h`)是相同的,无论它是物理的IP还是NSYS. 具体内容是什么,是该软件提供的API. `NeutronEnvConfig.h`,建立以下途径: - NSYS号, - 运行的中子固件 - NSYS 配置 (.ini 文件) : ``` storeNsysConfigPath(FLAGS_nsys_config.c_str()); storeFirmwarePath(FLAGS_firmware.c_str()); storeNsysPath(FLAGS_nsys.c_str()); ``` 对应nxp_executor_runner CLI选项, `--firmware`, `--nsys`, `--nsys_config` 或由环境变量提供: | CLI 选项 | 环境变量 | 说明 | | --- | --- | --- | | `--firmware` | NSYS_FIRMWARE_PATH | NSYS固件的路径. 每个支持的平台的NSYS固件由eIQ中子SDK包提供. | | `--nsys_config` | NSYS_CONFIG_PATH | NSYS 配置的路径。 对于 i.MXRT700 SoC 可用于 `examples/nxp/exector_runner/neutron-imxrt700.ini`。用于不需要的其他平台。 | | `--nsys` | N/A | eIQ NSYS模拟器可执行文件的路径(`which nsys`) | 详细情况 `eiq_nsys` 引用 python 包中提供的用户指南。 ### 构建 nxp_executor_runner[#](<https://docs.pytorch.org/executorch/stable/backends/nxp/tutorials/nxp-basic-tutorial.html#build-the-nxp-executor-runner>) 使用所提供 `examples/nxp/executor_runner/CMakeLists.txt`。可以单独构建: ``` mkdir ./examples/nxp/executor_runner/build pushd ./examples/nxp/executor_runner/build cmake .. make nxp_executor_runner popd ``` 或作为ExecuTorch建筑的一部分: ``` cd <executorch_root_dir> mkdir build cmake .. \ -DEXECUTORCH_BUILD_NXP_NEUTRON=ON \ -DEXECUTORCH_BUILD_NXP_NEUTRON_RUNNER=ON \ -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \ -DEXECUTORCH_BUILD_KERNELS_QUANTIZED_AOT=ON \ make nxp_executorch_runner ``` ### 运行 nxp_executor_runner[#](<https://docs.pytorch.org/executorch/stable/backends/nxp/tutorials/nxp-basic-tutorial.html#run-the-nxp-executor-runner>) ``` ./examples/nxp/executor_runner/build/nxp_executor_runner \ --firmware `make -C ./examples/nxp/executor_runner/build locate_neutron_firmware | grep "NeutronFirmware.elf" ` \ --nsys `which nsys` \ --nsys_config ./examples/nxp/executor_runner/neutron-imxrt700.ini \ --model ./cifar10_nxp_delegate.pte \ --dataset ./cifar10_test_data \ --output ./cifar10_test_output ``` ## 带路[#](<https://docs.pytorch.org/executorch/stable/backends/nxp/tutorials/nxp-basic-tutorial.html#takeways>) 为了方便,您可以运行所提供的实用脚本,进行上述所有步骤的详细检查: ``` cd <executorch-root> ./examples/nxp/run.sh ``` ## 问题[#](<https://docs.pytorch.org/executorch/stable/backends/nxp/tutorials/nxp-basic-tutorial.html#faqs>) 如果您在此教程后遇到任何错误或问题, 请在此存档 。 [吉图布](<https://github.com/pytorch/executorch/issues/new>) 标签为 `module: nxp`.
查看原始来源