RISC-V AI · SpacemiT K3 · MNN

MNN LLM Inference on SpacemiT K3 — IME2 W4B64 Quantization and Benchmarks

📅 2026-09-17 ⏱ 11 min read 🔗 Source: MNN on GitCode (2026-09-13), SpacemiT K3 product brief

On September 13, 2026, Alibaba's MNN inference engine team published a deep-dive implementation note covering end-to-end LLM optimization on the SpacemiT K3 RISC-V AI CPU. The work focuses on asymmetric W4B64 quantization accelerated by K3's IME2 matrix extension, with reproducible llm_bench numbers for Qwen3 and Qwen3.5 models. For engineers evaluating K3 as a local-LLM host, this is the most detailed public recipe yet for squeezing usable throughput out of the A100 AI cores.

What this article is. A technical summary of publicly documented MNN work on SpacemiT K3, with build flags, benchmark methodology and source-code references. All performance figures are taken directly from the MNN GitCode article; we reproduce the test commands so you can rerun them.

Why K3 + MNN Matters for Local LLMs

SpacemiT K3 is marketed as an RVA23 AI CPU: eight X100 general-purpose cores plus eight A100 AI compute cores on the same ISA. Unlike a discrete NPU, the A100 cores run standard RISC-V Linux threads and share the RVV 1.0 programming model with the X100 cores. That means an inference engine can treat the chip as a uniform RISC-V host and simply dispatch matrix-heavy kernels to the A100 cluster via the IME (Integrated Matrix Extension) instruction set.

MNN's K3 target exploits this unified model. It adds an IME2 backend that compiles into the A100 tensor units while keeping ARM/x86/standard-RVV paths untouched. The result is a layered build where enabling K3 support is a single CMake flag.

SpacemiT K3 Hardware Baseline

ComponentSpecification
General cores8× SpacemiT X100, 4-issue OoO, RVA23, RVV 1.0 (VLEN 256), up to 2.4 GHz
AI cores8× SpacemiT A100, RVV 1.0 (VLEN 1024), IME matrix extension, up to 2.0 GHz
Peak INT4 / INT860 TOPS sparse (4:2) / 30 TOPS dense INT8; 30 TOPS sparse / 15 TOPS dense INT8
Real-time cores2× RT24 for system management and low-power control
Memory64-bit LPDDR5-6400, up to ~51 GB/s peak
On-chip AI memory2 MiB cluster L2 + 3 MiB TCM (software-managed scratchpad) across A100 clusters
Typical power15–25 W

The A100 cluster is organized into two clusters of four scalar cores each, with two vector cores and one shared IME2 Tensor Core per pair of vector cores. A single core cannot continuously feed the shared Tensor Core; MNN therefore uses worker pairs to keep the matrix unit busy.

Benchmark Results

MNN ran all tests with 8 threads, prefill at pp512 (512 input tokens) and decode at tg128 (128 generated tokens). Each benchmark repeated 5 times inside the process and was averaged across 3 fresh processes.

Modelpp512 (tok/s)tg128 (tok/s)
Qwen3-0.6B381.2454.49
Qwen3-1.7B169.2924.90
Qwen3.5-0.8B127.5733.85
Qwen3.5-2B85.6017.68

The Qwen3.5 models use a mixed LinearAttention/full-Attention architecture, so their numbers are not directly comparable to Qwen3 on a single-kernel basis. The article explicitly notes that Flash Attention was enabled for Qwen3 (-fa 1) but disabled for Qwen3.5 (-fa 0).

Build MNN with K3 IME2 Support

The only K3-specific switch is -DMNN_RVV_SPACEMIT_IME2=ON. With it, MNN builds two additional object libraries:

When IME2 is enabled, the standard RVV fast-path registration is replaced by K3's own registration entry, so the two paths do not collide.

# Typical K3 build command
cmake -S . -B build-k3 \
  -DCMAKE_BUILD_TYPE=Release \
  -DMNN_RVV_SPACEMIT_IME2=ON \
  -DMNN_BUILD_LLM=ON \
  -DMNN_BUILD_CONVERTER=OFF \
  -DMNN_OPENCL=OFF \
  -DMNN_VULKAN=OFF \
  -DMNN_CUDA=OFF
cmake --build build-k3 -j$(nproc)

Reproducing the Benchmarks

MNN uses its own llm_bench tool. The article publishes the exact command lines for prefill and decode.

Qwen3 prefill (Flash Attention on):

./llm_bench -m <MNN_CONFIG> \
  -p 512 -n 0 -rep 5 -t 8 \
  -load false -fa 1 -kv false

Qwen3.5 prefill (Flash Attention off):

./llm_bench -m <MNN_CONFIG> \
  -p 512 -n 0 -rep 5 -t 8 \
  -load false -fa 0 -kv false

Decode for all models:

./llm_bench -m <MNN_CONFIG> \
  -p 1 -n 128 -rep 5 -t 8 \
  -load false -fa 0 -kv true

The IME2 path is selected at build time; no runtime environment variable is required.

Key Optimizations

1. Asymmetric W4B64 IME2 kernel

Weights are quantized to 4 bits per value in 64-element blocks (W4B64) with per-block FP16 scale and offset. Activations are kept at INT8. Because IME2's native instructions operate on same-width integer tiles, the kernel splits each INT8 activation into signed-high and unsigned-low 4-bit halves and issues two IME2 INT4 dot-product instructions (vmadotsu and vmadotu), then merges the partial sums. A block-scale variant (*.hp) reduces scaling overhead.

2. Fused dynamic quantization and A packing

Instead of scanning activations twice (once for absmax/scale, once for packing), the optimized path computes the dynamic scale, packs the activation tile and produces the kernel sum in a single pass.

3. Prefill: strided M4 + direct-C4 epilogue

Prefill uses an M4 kernel that computes four rows of activation at once. Workers are assigned strided row blocks to avoid fine-grained task dispatch. Where shape constraints allow, the kernel writes C4 output directly, skipping an intermediate buffer and layout conversion.

4. Decode: M1 asym-pair + worker-pair TCM pipeline

Decode is memory-bound: each token streams most of the weights through LPDDR5. MNN uses a worker-pair copy/compute double-buffer over the 3 MiB TCM:

Worker A: copy B tile 0 -> compute tile 0 -> copy tile 2 -> ...
Worker B:                copy tile 1 -> compute tile 1 -> ...

This raised Qwen3-1.7B decode from ~22.5 tok/s to ~24.9 tok/s. The implementation only enables TCM pipelining when the packed-B is at least 2 MiB and K is at least 2048; smaller matrices stay on the DRAM path to avoid synchronization overhead.

5. Attention and KV Cache

Quantized Linear layers dominate runtime (about 93.9% for Qwen3-1.7B decode). Attention uses a standard RVV direct-matvec path, plus a gated K3 fused Attention path that keeps QK, online softmax and PV in one tiled loop. KV Cache updates are parallelized for contiguous FP32 key/value data.

Source Code Map

FilePurpose
source/backend/cpu/riscv/CMakeLists.txtStandard RVV vs. K3 IME2 target isolation
MNNSpacemitIme2ConvInt8Executor.cppK3 Linear execution and prefill/decode routing
MNNSpacemitIme2GemmInt8.cppA/B packing, worker dispatch, TCM pipeline
MNNSpacemitIme2GemmI8I4Local.cppIME2 W4B64 kernel assembly/intrinsics
MNNSpacemitIme2AttentionFunctions.cppK3 fused Attention
MNNRvvAttentionFunctions.cppStandard RVV decode Attention fallback

Honest Limitations

Bottom line. MNN on SpacemiT K3 demonstrates that a stock RISC-V inference engine can hit tens of tokens per second on sub-2B parameter LLMs using native IME2 matrix instructions and careful memory-aware scheduling. It is not a magic "60 TOPS" guarantee, but it is a concrete, reproducible starting point for local LLM deployment on K3.

2026年9月13日,阿里巴巴 MNN 推理引擎团队发布了一篇在 SpacemiT K3 RISC-V AI CPU 上的端到端大语言模型(LLM)优化深度实现笔记。文章聚焦于利用 K3 的 IME2 矩阵扩展加速 非对称 W4B64 量化,并给出了 Qwen3 / Qwen3.5 系列模型可复现的 llm_bench 性能数据。对于正在评估 K3 作为本地 LLM 主机的工程师来说,这是目前公开的、在 A100 AI 核心上榨取可用吞吐量的最详细方案。

本文定位。 对 MNN 在 SpacemiT K3 上公开技术工作的技术摘要,包含构建开关、测试方法和源码引用。所有性能数据直接来自 MNN GitCode 文章,并附上测试命令供复现。

为什么 K3 + MNN 对本地 LLM 重要

SpacemiT K3 的定位是 RVA23 AI CPU:八颗 X100 通用核心加八颗 A100 AI 计算核心共享同一套指令集。与独立 NPU 不同,A100 核心运行标准 RISC-V Linux 线程,并与 X100 核心共享 RVV 1.0 编程模型。这意味着推理引擎可以把整颗芯片当作统一的 RISC-V 主机,只需通过 IME(Integrated Matrix Extension,集成矩阵扩展) 指令集把矩阵密集型算子派发到 A100 集群。

MNN 的 K3 target 正是利用了这一统一模型。它在不改动 ARM/x86/标准 RVV 路径的前提下,新增了一个编译到 A100 张量单元的 IME2 后端。构建时开启 K3 支持只需要一个 CMake 选项。

SpacemiT K3 硬件基线

组件规格
通用核心8× SpacemiT X100,4 发射乱序,RVA23,RVV 1.0(VLEN 256),最高 2.4 GHz
AI 核心8× SpacemiT A100,RVV 1.0(VLEN 1024),IME 矩阵扩展,最高 2.0 GHz
峰值 INT4 / INT860 TOPS 稀疏(4:2)/ 30 TOPS 稠密 INT8;30 TOPS 稀疏 / 15 TOPS 稠密 INT8
实时核心2× RT24,用于系统管理与低功耗控制
内存64-bit LPDDR5-6400,峰值约 51 GB/s
片上 AI 存储两个 A100 cluster 共 2 MiB L2 + 3 MiB TCM(软件管理的 scratchpad)
典型功耗15–25 W

A100 集群分为两个 cluster,每个 cluster 四颗标量核心,每对向量核心共享一个 IME2 Tensor Core。单颗核心无法持续喂饱共享 Tensor Core,因此 MNN 采用 worker-pair(成对工作线程) 设计来保持矩阵单元忙碌。

性能数据

所有测试均使用 8 线程,prefill 采用 pp512(512 个输入 token),decode 采用 tg128(连续生成 128 个 token)。每个 benchmark 在进程内重复 5 次,最终结果取 3 个独立进程的算术平均。

模型pp512 (tok/s)tg128 (tok/s)
Qwen3-0.6B381.2454.49
Qwen3-1.7B169.2924.90
Qwen3.5-0.8B127.5733.85
Qwen3.5-2B85.6017.68

Qwen3.5 系列采用 LinearAttention 与全 Attention 混合架构,因此不能与 Qwen3 在单一 kernel 层面直接比较。文章明确说明 Qwen3 开启 Flash Attention(-fa 1),而 Qwen3.5 关闭(-fa 0)。

构建支持 K3 IME2 的 MNN

唯一的 K3 专用开关是 -DMNN_RVV_SPACEMIT_IME2=ON。开启后 MNN 会额外构建两个 object library:

启用 IME2 时,标准 RVV fast-path 注册会被 K3 专用注册入口替换,避免两条路径冲突。

# 典型的 K3 构建命令
cmake -S . -B build-k3 \
  -DCMAKE_BUILD_TYPE=Release \
  -DMNN_RVV_SPACEMIT_IME2=ON \
  -DMNN_BUILD_LLM=ON \
  -DMNN_BUILD_CONVERTER=OFF \
  -DMNN_OPENCL=OFF \
  -DMNN_VULKAN=OFF \
  -DMNN_CUDA=OFF
cmake --build build-k3 -j$(nproc)

复现测试

MNN 使用自带的 llm_bench 工具,文章中给出了 prefill 和 decode 的精确命令。

Qwen3 prefill(开启 Flash Attention):

./llm_bench -m <MNN_CONFIG> \
  -p 512 -n 0 -rep 5 -t 8 \
  -load false -fa 1 -kv false

Qwen3.5 prefill(关闭 Flash Attention):

./llm_bench -m <MNN_CONFIG> \
  -p 512 -n 0 -rep 5 -t 8 \
  -load false -fa 0 -kv false

所有模型的 decode:

./llm_bench -m <MNN_CONFIG> \
  -p 1 -n 128 -rep 5 -t 8 \
  -load false -fa 0 -kv true

IME2 路径在构建时确定,运行时无需设置环境变量。

关键优化点

1. 非对称 W4B64 IME2 kernel

权重量化为每 64 个元素一个 block 的 4 bit(W4B64),每个 block 配 FP16 scale 与 offset;激活保持 INT8。由于 IME2 原生指令要求同位宽整型 tile,kernel 把每个 INT8 激活拆分为有符号高 4 bit 和无符号低 4 bit,分别执行 vmadotsuvmadotu 两次 IME2 INT4 点积指令后再合并。block-scale 变体(*.hp)可减少缩放相关指令。

2. 动态量化与 A packing 融合

优化路径把动态 scale 计算、激活 tile packing 和 kernel sum 产生合并为一次遍历,避免对激活做两次全量扫描。

3. Prefill:strided M4 + direct-C4 epilogue

Prefill 使用 M4 kernel,一次计算 4 行激活;worker 以 strided row 方式分配,避免细粒度任务分发。满足条件时直接写 C4 输出,跳过中间 buffer 和布局转换。

4. Decode:M1 asym-pair + worker-pair TCM 双缓冲流水

Decode 是内存带宽敏感场景:每个 token 都要把大部分权重从 LPDDR5 流过。MNN 利用 3 MiB TCM 做 worker-pair copy/compute 双缓冲

Worker A: copy B tile 0 -> compute tile 0 -> copy tile 2 -> ...
Worker B:                copy tile 1 -> compute tile 1 -> ...

这使 Qwen3-1.7B decode 从约 22.5 tok/s 提升到约 24.9 tok/s。TCM 流水仅在 packed-B 不小于 2 MiB 且 K 不小于 2048 时启用;小矩阵仍走 DRAM 路径,避免同步开销。

5. Attention 与 KV Cache

量化 Linear 占 decode 绝大部分时间(Qwen3-1.7B decode 约 93.9%)。Attention 使用标准 RVV direct-matvec 路径,另有一个带门禁的 K3 fused Attention 路径,将 QK、online softmax 和 PV 放在同一个分块循环中执行。KV Cache 更新针对连续 FP32 key/value 数据做了并行化。

源码地图

文件作用
source/backend/cpu/riscv/CMakeLists.txt标准 RVV 与 K3 IME2 target 隔离
MNNSpacemitIme2ConvInt8Executor.cppK3 Linear 执行与 prefill/decode 路由
MNNSpacemitIme2GemmInt8.cppA/B packing、worker 调度、TCM 流水
MNNSpacemitIme2GemmI8I4Local.cppIME2 W4B64 kernel 汇编/内联实现
MNNSpacemitIme2AttentionFunctions.cppK3 fused Attention
MNNRvvAttentionFunctions.cpp标准 RVV decode Attention 回退

诚实说明的限制

结论。 MNN 在 SpacemiT K3 上的工作证明,借助原生 IME2 矩阵指令和注重内存的调度,通用 RISC-V 推理引擎可以在 K3 上让小于 2B 参数的 LLM 达到每秒数十 token 的 decode 速度。这不是对 "60 TOPS" 的神奇兑现,而是一个具体、可复现的 K3 本地 LLM 部署起点。

Краткое резюме (RU)

Команда Alibaba MNN опубликовала 13 сентября 2026 года реализацию вывода LLM на SpacemiT K3 с использованием асимметричной квантизации W4B64 и расширения IME2. На 8 потоках Qwen3-0.6B достигает 381 tok/s при prefill и 54,5 tok/s при decode. Сборка включает флаг -DMNN_RVV_SPACEMIT_IME2=ON. Ключевые оптимизации: worker-pair TCM pipeline, direct-C4/direct-output epilogue и fused Attention. Источник: MNN on GitCode.

Resumen (ES)

El equipo de Alibaba MNN publicó el 13 de septiembre de 2026 una implementación de inferencia LLM en SpacemiT K3 usando cuantización asimétrica W4B64 y la extensión matricial IME2. Con 8 hilos, Qwen3-0.6B alcanza 381 tok/s en prefill y 54,49 tok/s en decode. La compilación usa -DMNN_RVV_SPACEMIT_IME2=ON. Optimizaciones clave: pipeline TCM con pares de workers, epílogo direct-C4/direct-output y Attention fusionado. Fuente: MNN on GitCode.

Résumé (FR)

L'équipe MNN d'Alibaba a publié le 13 septembre 2026 une implémentation d'inférence LLM sur SpacemiT K3 utilisant la quantification asymétrique W4B64 et l'extension matricielle IME2. Avec 8 threads, Qwen3-0.6B atteint 381 tok/s en prefill et 54,49 tok/s en decode. La compilation utilise -DMNN_RVV_SPACEMIT_IME2=ON. Optimisations clés : pipeline TCM double-tampon avec paires de workers, épilogue direct-C4/sortie directe et Attention fusionnée. Source : MNN on GitCode.

Zusammenfassung (DE)

Das Alibaba-MNN-Team veröffentlichte am 13. September 2026 eine LLM-Inferenz-Implementierung auf SpacemiT K3 mit asymmetrischer W4B64-Quantisierung und der IME2-Matrixerweiterung. Bei 8 Threads erreicht Qwen3-0.6B 381 tok/s (prefill) und 54,49 tok/s (decode). Der Build verwendet -DMNN_RVV_SPACEMIT_IME2=ON. Wichtige Optimierungen: TCM-Double-Buffer-Pipeline mit Worker-Paaren, direct-C4/direct-Output-Epilog und fused Attention. Quelle: MNN on GitCode.

خلاصه (FA)

تیم MNN علی‌بابا در ۱۳ سپتامبر ۲۰۲۶ پیاده‌سازی استنتاج LLM روی SpacemiT K3 را با کوانتیزاسیون نامتقارن W4B64 و افزونه ماتریسی IME2 منتشر کرد. با ۸ ترد، مدل Qwen3-0.6B به ۳۸۱ توکن‌برثانیه در prefill و ۵۴٫۴۹ توکن‌برثانیه در decode می‌رسد. برای ساخت از -DMNN_RVV_SPACEMIT_IME2=ON استفاده می‌شود. بهینه‌سازی‌های کلیدی: لوله‌کشی دوبل‌بافر TCM با جفت worker، epilogue direct-C4/direct-output و Attention یکپارچه. منبع: MNN on GitCode.

Sources / 参考来源

← Back to Tech Blog