ZHDA204A June 2026 – October 2026 MSPM0G5187
模型经过训练并导出为 ONNX 格式后,开发生命周期的最后一步是编译。此过程可将高级 ONNX 计算图转换为针对目标微控制器 (MCU) 进行优化的机器可读代码。
TI 神经网络编译器 (NNC) 用于弥合通用 ONNX 模型与嵌入式器件之间的差距。NNC 根据您的项目要求将模型转换为两种主要部署格式之一:
编译器生成的输出文件遵循标准化的目录结构:
root_path/
├── lib0.c # Optimized source code lib0
├── lib1.c # Optimized source code lib1
├── lib2.c # Optimized source code lib2
├── mod.a # Compiled static library file (Link this to CCS)
└── tvmgen_default.h # C header file containing inference function signatures对于高级用户,tvmc 命令行界面提供了对编译过程的精细控制。有关详细文档,请参阅 TI MCU 神经网络编译器用户指南:编译说明。
要将 ONNX 模型编译为 MSPM0 (Cortex-M0+) 目标的静态库,请在终端中执行以下命令:
tvmc compile \
--target="c, ti-npu skip_normalize=true output_int=true" \
--target-c-mcpu=cortex-m0plus \
.\model.onnx \
-o artifacts_m0/mod.a \
--cross-compiler="tiarmclang" \
--cross-compiler-options="-Os -mcpu=cortex-m0plus -march=thumbv6m -mtune=cortex-m0plus -mthumb -mfloat-abi=soft -I. -Wno-return-type"下载 Tiny ML 工作流程示例 Python 脚本包,并使用其中包含的“10_test_cnn_quan_compile_npu_lib.py”脚本。该脚本围绕 tvmc 命令行界面提供了一个通用封装器。该脚本使开发人员能够轻松管理目标特定编译器标志。
使用终端执行脚本后,通过监测日志来验证训练进度:
================================================================
Output Mode : LIB
Target Device : MSPM0 (cortex-m0plus)
NNC Target : c, ti-npu skip_normalize=true output_int=true
Input Model : C:\ti\tinyml-tensorlab\UserTestFolder\model\mnist\mnist_model_int8.onnx
Output Dir : C:\ti\tinyml-tensorlab\UserTestFolder\model\mnist\outputLib
================================================================
WARNING:ti_mcu_nnc:GCONV layer NOT offloaded to TI-NPU
WARNING:ti_mcu_nnc:GCONV layer config: 8-bit input [9, 9, 16], 8-bit output [4, 4, 10], 8-bit kernel [3, 3], strides [2, 2], padding [(0, 0), (0, 0)], data layout NHWC, kernel layout HWIO
WARNING:ti_mcu_nnc:GCONV does not support 10 output channels. Please change to multiples of 4.
WARNING:ti_mcu_nnc:AVGPOOL layer NOT offloaded to TI-NPU
WARNING:ti_mcu_nnc:Global AVGPOOL layer config:8-bit input [4, 4, 10], 8-bit output [10]
WARNING:ti_mcu_nnc:Global AVGPOOL does not support 10 input channels. Please change to multiples of 4.
INFO:ti_mcu_nnc: ============================================================
INFO:ti_mcu_nnc: TI NPU Offloading Report
INFO:ti_mcu_nnc: ============================================================
INFO:ti_mcu_nnc:
INFO:ti_mcu_nnc: LAYER/OPERATOR COUNT SUMMARY
INFO:ti_mcu_nnc: ============================================================
INFO:ti_mcu_nnc:
INFO:ti_mcu_nnc: Layer Patterns Offloaded
INFO:ti_mcu_nnc: ------------------------------------------------------------
INFO:ti_mcu_nnc: Layer Pattern Count
INFO:ti_mcu_nnc: ------------------------------------------------------------
INFO:ti_mcu_nnc: FCONV 1
INFO:ti_mcu_nnc: PAD 2
INFO:ti_mcu_nnc: GCONV 1
INFO:ti_mcu_nnc:
INFO:ti_mcu_nnc: Operators not Offloaded
INFO:ti_mcu_nnc: ------------------------------------------------------------
INFO:ti_mcu_nnc: Operator Count
INFO:ti_mcu_nnc: ------------------------------------------------------------
INFO:ti_mcu_nnc: reshape 1
INFO:ti_mcu_nnc: nn.pad 2
INFO:ti_mcu_nnc: qnn.conv2d 1
INFO:ti_mcu_nnc: cast 4
INFO:ti_mcu_nnc: nn.bias_add 1
INFO:ti_mcu_nnc: multiply 2
INFO:ti_mcu_nnc: right_shift 2
INFO:ti_mcu_nnc: clip 2
INFO:ti_mcu_nnc: sum 1
INFO:ti_mcu_nnc: add 1
INFO:ti_mcu_nnc:
INFO:ti_mcu_nnc: ============================================================
INFO:ti_mcu_nnc:
INFO:ti_mcu_nnc:
INFO:ti_mcu_nnc: MODEL PARTITION - TOPOLOGICAL VIEW
INFO:ti_mcu_nnc: ============================================================
INFO:ti_mcu_nnc: Layer/Operator Offloaded
INFO:ti_mcu_nnc: ------------------------------------------------------------
INFO:ti_mcu_nnc: Input
INFO:ti_mcu_nnc: reshape [ ]
INFO:ti_mcu_nnc: nn.pad [ ]
INFO:ti_mcu_nnc: nn.pad [ ]
INFO:ti_mcu_nnc: FCONV [X]
INFO:ti_mcu_nnc: PAD [X]
INFO:ti_mcu_nnc: GCONV [X]
INFO:ti_mcu_nnc: PAD [X]
INFO:ti_mcu_nnc: qnn.conv2d [ ]
INFO:ti_mcu_nnc: cast [ ]
INFO:ti_mcu_nnc: nn.bias_add [ ]
INFO:ti_mcu_nnc: multiply [ ]
INFO:ti_mcu_nnc: right_shift [ ]
INFO:ti_mcu_nnc: clip [ ]
INFO:ti_mcu_nnc: cast [ ]
INFO:ti_mcu_nnc: cast [ ]
INFO:ti_mcu_nnc: sum [ ]
INFO:ti_mcu_nnc: add [ ]
INFO:ti_mcu_nnc: multiply [ ]
INFO:ti_mcu_nnc: right_shift [ ]
INFO:ti_mcu_nnc: clip [ ]
INFO:ti_mcu_nnc: cast [ ]
INFO:ti_mcu_nnc: Output
INFO:ti_mcu_nnc: ============================================================
INFO:ti_mcu_nnc:
INFO:ti_mcu_nnc: ============================================================
INFO:ti_mcu_nnc: TI Model Library Memory Usage (mod.a)
INFO:ti_mcu_nnc: ============================================================
INFO:ti_mcu_nnc: Code: 1952 bytes ( 1.91 KB)
INFO:ti_mcu_nnc: RO Data: 7061 bytes ( 6.90 KB)
INFO:ti_mcu_nnc: RW Data: 9540 bytes ( 9.32 KB)
INFO:ti_mcu_nnc: Total: 18553 bytes ( 18.12 KB)
INFO:ti_mcu_nnc: ============================================================