在 RISC-V 芯片上,如何调用内置 CMSIS-NN?
解读
面试官问的是“Keras 训练出的模型,怎样在 RISC-V MCU 上跑起来,并且把 CMSIS-NN 的汇编算子真正调到”。
国内芯片公司(赛昉、全志、阿里玄铁、沁恒、中科蓝讯)给出的 SDK 里,CMSIS-NN 源码都在,但 RISC-V 没有 Cortex-M 的 NVIC/SCB,所以“直接 include 就编译过”是假象;真正难点在三条:
- 交叉编译链要开 RV32-IMAC + Zbb/Zbp 扩展,让 CMSIS-NN 的 dsp 宏能展开成有效指令;
- 把 Keras 模型转成 .tflite(int8)后,用 TensorFlow Lite Micro 的 “CMSIS-NN 后端” 注册表,把算子符号绑定到芯片 SDK 里重编译的 libcmsis-nn.a;
- 链接阶段把
riscv-elf-ld的 PLT 去掉,用-ffunction-sections -fdata-sections -Wl,--gc-sections把用不到的 Cortex-M 汇编段裁掉,否则 32 KB 的 MCU 马上爆。
面试时,只要能把“Keras→TFLM→CMSIS-NN→RISC-V”整条链路讲清楚,并给出可落地的编译 flag 与内存排布,就能拿到高分。
知识点
- Keras 模型量化:
- 用
tf.keras.quantization.quantize_model或representative_dataset做全整数量化,保证权重/激活都是 int8,才能喂给 CMSIS-NN。
- 用
- TensorFlow Lite Micro 的 MicroMutableOpResolver:
- 在
AllOpsResolver()里默认注册的是参考内核;必须显式.AddConv2D(cmsis_nn_conv)把算子指针换成 CMSIS-NN 版本。
- 在
- CMSIS-NN 的 RISC-V 移植层:
arm_nn_activations_q7.c里大量#ifdef __ARM_FEATURE_DSP需要改成__riscv_mul或自定义宏;arm_nntables.c的偏移表用const int8_t *即可,RISC-V 没有位带。
- 交叉编译链:
- 平头哥/赛昉 SDK 自带
riscv64-unknown-elf-gcc,必须加-march=rv32imac -mabi=ilp32 -O3 -fno-common -falign-functions=16才能对齐到 16-byte,让 CMSIS-NN 的 SIMD 循环不踩边界。
- 平头哥/赛昉 SDK 自带
- 内存布局:
- TFLM 的
RecordingMicroAllocator先跑一遍统计张量,再用__attribute__((section(".tensor_arena")))把 arena 放到 SRAM0,避免跟代码段抢 ICCM。
- TFLM 的
- 性能验证:
- 用 SDK 自带的
perf_counter或者玄铁CSI接口读mcycle/minstret,对比“参考内核”与“CMSIS-NN”的 cycle 数,一般 3×3 depthwise 能快 4~6 倍。
- 用 SDK 自带的
答案
步骤化回答,面试官能随时打断追问。
-
训练与量化
在 PC 端用 TensorFlow 2.x 训练 Keras 模型,加入量化感知训练:import tensorflow_model_optimization as tfmot q_aware = tfmot.quantization.keras.quantize_model model = q_aware(model) model.fit(...) converter = tf.lite.TFLiteConverter.from_keras_model(model) converter.optimizations = [tf.lite.Optimize.DEFAULT] converter.representative_dataset = rep_ds tflite_model = converter.convert() open('model_int8.tflite', 'wb').write(tflite_model)保证输出只有 int8 算子。
-
生成 Micro 代码
用xxd -i model_int8.tflite > model_data.cc把模型转成 C 数组;
在main.cc里创建tflite::MicroMutableOpResolver<10> resolver;
显式注册 CMSIS-NN 内核:resolver.AddConv2D(tflite::ops::micro::Register_CONV_2D_CMSIS_NN()); resolver.AddDepthwiseConv2D(tflite::ops::micro::Register_DEPTHWISE_CONV_2D_CMSIS_NN()); -
交叉编译 CMSIS-NN
把 ARM 官方 CMSIS-NN 拉到third_party/,写一份CMakeLists.txt:set(CMAKE_C_COMPILER riscv32-unknown-elf-gcc) add_definitions(-D__riscv -DNN_NO_NNPACK -DRISCV_CORE) target_compile_options(cmsis-nn PRIVATE -march=rv32imac -mabi=ilp32 -O3 -fno-common)对
arm_nn_mat_mult_kernel_q7_q15.c这种手写汇编的文件,用 C 实现兜底,或者内联 RISC-Vp.mac指令。 -
链接与裁减
在LDFLAGS加-Wl,--gc-sections -Wl,-Map=app.map把未用函数裁掉;
用size命令看 text 段,控制在 64 KB 以内。 -
上板验证
烧录后先跑Interpreter::Invoke()10 次,取平均 cycle;
再对比把 resolver 换成参考内核的 cycle,证明 CMSIS-NN 确实被调到。
一句话总结:
“Keras 模型 → 全整数量化 TFLite → TFLM MicroResolver 绑定 CMSIS-NN → 用 rv32imac 工具链重编 CMSIS-NN → 链接时裁掉 ARM 专有段 → 上板读 cycle”,就能把 CMSIS-NN 在 RISC-V 芯片里真正跑起来。
拓展思考
- 如果芯片带自研 NPU,CMSIS-NN 只做 fallback,如何把 NPU Op 注册到 TFLM?
答:继承TfLiteRegistration结构体,把invoke指针指向厂商给的npu_conv2d_entry,并在Prepare里判断若 shape 不合法就退回 CMSIS-NN。 - 当模型出现 int16 权重时,CMSIS-NN 默认不支持,怎么在 Keras 端强制回退到 int8?
答:在representative_dataset里把输入 clip 到 [-128,127],并在converter.target_spec.supported_types = [tf.int8]里加白名单。 - RISC-V 向量扩展 1.0 已经冻结,未来 CMSIS-NN 会不会出 RVV 版?
答:ARM 官方不会,但国内厂商(赛昉、玄铁)已经在tflite-micro社区提交riscv_vector_conv2d的 patch,预计 2025 前会合并主线,届时 Keras 模型无需改动,只需把-march=rv32imafcv打开即可自动调用 RVV 算子,性能可再翻 3~8 倍。