把“思考”塞进 1 KB:我用纯 C 语言给单片机手搓了一个微型 Transformer 推理引擎

标签:TinyML、Transformer、单片机、Cortex-M、量化、KV-Cache、裸机编程


  1. 为什么要在 64 KB SRAM 的 MCU 上跑 Transformer?

2024 年以前,TinyML ≈ CNN + CMSIS-NN,做语音唤醒或简单分类就到头了。

但产品同事突然拍脑袋:

"客户想让 20 元的温控器用自然语言调温------'帮我调到 26 度,别太吵',离线响应 200 ms 以内。"

云端?断网就 GG。

大模型?STM32H743 只有 64 KB SRAM,放不下 8-bit 1B 模型。

于是我把目标锁在 完全离线、<200 ms、Flash ≤256 KB、RAM ≤64 KB 的 NLU(自然语言理解)任务上:

意图识别 + 槽位提取,词汇量 400,输出 JSON。


  1. 模型侧:把 6 层 Transformer 压成 1 层

2.1 结构手术

• 层数:6 → 1(保留最后一层)

• 隐藏维度:512 → 128

• Head 数:8 → 4

• 序列长度:128 → 32

2.2 量化四连击

方法 压缩比 精度掉点 备注

INT8 权重量化 4× 1.2 % per-channel scale

4-bit KV-Cache 2× 0.8 % 动态查表

8-bit 激活 2× 0.3 % Power-of-two scaling

合计 8× 2.3 % 在可接受范围

量化脚本(PyTorch → C header):

python 复制代码
import torch
from quantize import quantize_int8
w = model.encoder.layers[0].self_attn.q_proj.weight
w_int, scale = quantize_int8(w)
torch.save({"w_int": w_int.numpy(), "scale": scale}, "q_weight.pt")

  1. 推理引擎:1 KB 的"思考"是如何炼成的?

3.1 内存布局(Flash 240 KB + RAM 60 KB)

Flash

├── weight (INT8) 220 KB

├── embedding LUT 12 KB

└── code段 8 KB

SRAM

├── input ids 32 B

├── KV-Cache (4 bit) 4 KB

├── 激活缓存 8 KB

└── 栈 + 堆 48 KB

3.2 核心算法:手撸矩阵乘 + Softmax + LayerNorm

• GEMM:

128×128 × 128×1 → 128×1,使用 CMSIS-NN 的 arm_mat_mult_q7_q15

耗时 8 ms @400 MHz

• Softmax:

查表法 32 维 exp,表大小 256 B

耗时 0.6 ms

• LayerNorm:

查表 + 近似除法,表大小 128 B

耗时 0.4 ms

3.3 代码片段(精简到 30 行)

cs 复制代码
// tiny_transformer.h
#define H 128
#define L 32
void matmul_q8_q15(const int8_t *w, const int16_t *x,
                   int16_t *y, int rows, int cols);
void softmax_q15(int16_t *x, int len);
void layernorm_q15(int16_t *x, const int16_t *gamma,
                   const int16_t *beta, int len);

void tiny_forward(const int8_t *tokens, int seq_len,
                  int8_t intent, int8_t *slots) {
    static int16_t q[L*H], k[L*H], v[L*H];
    static int16_t kv_cache[H*L];
    // 1. Embedding lookup
    for(int i=0;i<seq_len;i++)
        memcpy(&q[i*H], &emb_table[tokens[i]*H], H*2);

    // 2. Self-Attention
    matmul_q8_q15(W_q, q, q, H, seq_len);
    matmul_q8_q15(W_k, q, k, H, seq_len);
    matmul_q8_q15(W_v, q, v, H, seq_len);
    // ... 省略 KV-Cache 更新 ...
    softmax_q15(attn_score, seq_len);

    // 3. Feed-Forward
    matmul_q8_q15(W_out, attn_out, q, H, seq_len);
    layernorm_q15(q, gamma, beta, seq_len*H);

    // 4. 分类头
    intent = argmax_int8(q);
    memcpy(slots, &q[INTENT_DIM], SLOT_DIM);
}

  1. 端到端 Benchmark

指标 数值 备注

Flash 240 KB 含模型+引擎

RAM 59 KB 实测峰值

推理延迟 184 ms 400 MHz Cortex-M7

准确率 96.1 % 测试集 2000 句

功耗 23 mW 3.3 V 运行


  1. 踩坑日记:那些没人告诉你的细节

  2. Cache Miss 地狱

128×128 GEMM 在 STM32 的 32 KB I-Cache 里来回抖动。

解决:把权重按 32×128 tile 重排,命中率从 60 % → 94 %。

  1. 4-bit KV 反量化

2 个 4-bit 打包成 1 byte,移位 + 查表,一次反量化 8 个值,耗时从 1.8 ms → 0.7 ms。

  1. 链接脚本玄学

.rodata 默认对齐 8 byte,导致 Flash 多占 5 KB。

解决:自定义 ALIGN(1),手动打包结构体。


  1. 开源 & 下一步

GitHub:

https://github.com/embeddedai/tiny-transformer

已支持:

• Keil / STM32CubeIDE 工程模板

• 一键量化脚本(PyTorch → C header)

Roadmap:

• ☐ LoRA 微调:在 MCU 里在线更新 4 KB Adapter;

• ☐ Vision Transformer:把 32×32 灰度图压缩到 1 KB Embedding;

• ☐ RISC-V 移植:跑在 25 元的 BL702 上。


  1. 结语:边缘 AI 的尽头是"硅片上的魔法"

当 20 元的温控器也能听懂"把客厅温度调到 26 度,顺便开点窗户",

你会发现 AI 不再是一行行 Python,而是 1 KB 代码里跳动的电平。

如果你也在做 TinyML,欢迎留言交流;

如果这篇文章帮到你,记得点个 Star ⭐,一起把 Transformer 塞进更小的世界!