在 RISC-V 芯片上,如何调用内置 CMSIS-NN?

解读

面试官问的是“Keras 训练出的模型,怎样在 RISC-V MCU 上跑起来,并且把 CMSIS-NN 的汇编算子真正调到”。
国内芯片公司(赛昉、全志、阿里玄铁、沁恒、中科蓝讯)给出的 SDK 里,CMSIS-NN 源码都在,但 RISC-V 没有 Cortex-M 的 NVIC/SCB,所以“直接 include 就编译过”是假象;真正难点在三条:

  1. 交叉编译链要开 RV32-IMAC + Zbb/Zbp 扩展,让 CMSIS-NN 的 dsp 宏能展开成有效指令;
  2. 把 Keras 模型转成 .tflite(int8)后,用 TensorFlow Lite Micro 的 “CMSIS-NN 后端” 注册表,把算子符号绑定到芯片 SDK 里重编译的 libcmsis-nn.a;
  3. 链接阶段把 riscv-elf-ld 的 PLT 去掉,用 -ffunction-sections -fdata-sections -Wl,--gc-sections 把用不到的 Cortex-M 汇编段裁掉,否则 32 KB 的 MCU 马上爆。
    面试时,只要能把“Keras→TFLM→CMSIS-NN→RISC-V”整条链路讲清楚,并给出可落地的编译 flag 与内存排布,就能拿到高分。

知识点

  1. Keras 模型量化:
    • 用 tf.keras.quantization.quantize_model 或 representative_dataset 做全整数量化,保证权重/激活都是 int8,才能喂给 CMSIS-NN。
  2. TensorFlow Lite Micro 的 MicroMutableOpResolver:
    • 在 AllOpsResolver() 里默认注册的是参考内核;必须显式 .AddConv2D(cmsis_nn_conv) 把算子指针换成 CMSIS-NN 版本。
  3. CMSIS-NN 的 RISC-V 移植层:
    • arm_nn_activations_q7.c 里大量 #ifdef __ARM_FEATURE_DSP 需要改成 __riscv_mul 或自定义宏;
    • arm_nntables.c 的偏移表用 const int8_t * 即可,RISC-V 没有位带。
  4. 交叉编译链:
    • 平头哥/赛昉 SDK 自带 riscv64-unknown-elf-gcc,必须加 -march=rv32imac -mabi=ilp32 -O3 -fno-common -falign-functions=16 才能对齐到 16-byte,让 CMSIS-NN 的 SIMD 循环不踩边界。
  5. 内存布局:
    • TFLM 的 RecordingMicroAllocator 先跑一遍统计张量,再用 __attribute__((section(".tensor_arena"))) 把 arena 放到 SRAM0,避免跟代码段抢 ICCM。
  6. 性能验证:
    • 用 SDK 自带的 perf_counter 或者玄铁 CSI 接口读 mcycle/minstret,对比“参考内核”与“CMSIS-NN”的 cycle 数,一般 3×3 depthwise 能快 4~6 倍。

答案

步骤化回答,面试官能随时打断追问。

  1. 训练与量化
    在 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 算子。

  2. 生成 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());
    
  3. 交叉编译 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-V p.mac 指令。

  4. 链接与裁减
    在 LDFLAGS 加 -Wl,--gc-sections -Wl,-Map=app.map 把未用函数裁掉;
    用 size 命令看 text 段,控制在 64 KB 以内。

  5. 上板验证
    烧录后先跑 Interpreter::Invoke() 10 次,取平均 cycle;
    再对比把 resolver 换成参考内核的 cycle,证明 CMSIS-NN 确实被调到。

一句话总结:
“Keras 模型 → 全整数量化 TFLite → TFLM MicroResolver 绑定 CMSIS-NN → 用 rv32imac 工具链重编 CMSIS-NN → 链接时裁掉 ARM 专有段 → 上板读 cycle”,就能把 CMSIS-NN 在 RISC-V 芯片里真正跑起来。

拓展思考

  1. 如果芯片带自研 NPU,CMSIS-NN 只做 fallback,如何把 NPU Op 注册到 TFLM?
    答:继承 TfLiteRegistration 结构体,把 invoke 指针指向厂商给的 npu_conv2d_entry,并在 Prepare 里判断若 shape 不合法就退回 CMSIS-NN。
  2. 当模型出现 int16 权重时,CMSIS-NN 默认不支持,怎么在 Keras 端强制回退到 int8?
    答:在 representative_dataset 里把输入 clip 到 [-128,127],并在 converter.target_spec.supported_types = [tf.int8] 里加白名单。
  3. RISC-V 向量扩展 1.0 已经冻结,未来 CMSIS-NN 会不会出 RVV 版?
    答:ARM 官方不会,但国内厂商(赛昉、玄铁)已经在 tflite-micro 社区提交 riscv_vector_conv2d 的 patch,预计 2025 前会合并主线,届时 Keras 模型无需改动,只需把 -march=rv32imafcv 打开即可自动调用 RVV 算子,性能可再翻 3~8 倍。