训练依赖 PyTorch,README 未给出具体版本要求;推理前需先运行 exportquant.py 生成 BitNetMCU_model.h;在 MCU 上运行需按 mcu/readme.md 单独配置工具链。
BitNetMCU
cpldcpuGPL-3.0活跃维护在 CH32V003 这类低端 32 位单片机上跑低比特权重量化神经网络,把 TinyML 推理压进几毛钱的芯片里。
Neural Networks with low bit weights on low end 32 bit microcontrollers such as the CH32V003 RISC-V Microcontroller and others
pip install torch pyyaml
python training.py
python exportquant.py目标平台为 CH32V003 等低端 32 位单片机;无乘法推理版本仅需 2KB RAM 和 16KB Flash,CNN 版本需 16KB Flash 和 4KB RAM 且要求硬件乘法器(常见于低端 RISC-V 和 ARM Cortex-M0)。README 未提及显存或桌面 GPU 要求。
在 CH32V003 等低端 32 位单片机上训练并运行低比特量化神经网络
在 CH32V003 这类低端 32 位单片机上跑低比特权重量化神经网络,把 TinyML 推理压进几毛钱的芯片里。
核心能力
- 用 QAT 训练二值、三值、2/4/8 比特量化模型
- 提供 Ansi-C 推理引擎,可移植到其他单片机
- 支持无乘法指令推理,仅需 2KB RAM 和 16KB Flash
- 深度可分离卷积 CNN 架构达到 99.55% MNIST 精度
- 支持 FP1.3.0、NF4、非对称 4bit 等量化方案
容易踩的坑
- 0.2aVersion 0.2a最新正式版2024-05-26 · 2024-05-26 04:58 UTC
- Support for new quantization schemes to reduce inference time on multiplierless MCUs and also support efficient implementation on MCUs with multiplier (CM0, RV32ECzMMu
- FP130: Inference using only shifts
- 4bit: encoding allows to use mul instruction
- 0.1apre-release2024-05-02 · 2024-05-02 09:15 UTC
查看更新说明
Functional demonstrator on CH32V003 completed.
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
BitNetMCU: High Accuracy Low-Bit Quantized Neural Networks on a low-end Microcontroller
BitNetMCU is a project focused on the training and inference of low-bit quantized neural networks, specifically designed to run efficiently on low-end microcontrollers like the CH32V003. Quantization aware training (QAT) and fine-tuning of model structure and inference code allowed surpassing 99% Test accuracy on a 16x16 MNIST dataset without using multiplication instructions and in only 2kb of RAM and 16kb of Flash.
Update Introducing a new model architecture based on deep separable convolutions allowed to push the accuracy even further to 99.55% accuracy, meeting state-of-the-art MNIST accuracy for CNNs while still fitting into the same memory constraints. This model requires a hardware multiplier, which is available in many low-end RISC-V and ARM Cortex-M0 microcontrollers.
The training pipeline is based on PyTorch and should run anywhere. The inference engine is implemented in Ansi-C and can be easily ported to any Microcontroller.
You can my buildlogs on the project in the docs/ directory here and CNN details here. Latest update on optimize fc here.
Also see BitNetMCU blog articles
Project Structure
BitNetMCU/
│
├── docs/ # Report
├── mcu/ # MCU specific code for CH32V003
├── modeldata/ # Pre-trained models
│
├── BitNetMCU.py # Pytorch model and QAT classes
├── BitNetMCU_inference.c # C code for inference
├── BitNetMCU_inference.h # Header file for C inference code
├── BitNetMCU_MNIST_test.c # Test script for MNIST dataset
├── BitNetMCU_MNIST_test_data.h# MNIST test data in header format (generated)
├── BitNetMCU_model.h # Model data in C header format (generated)
├── exportquant.py # Script to convert trained model to quantized format
├── test_inference.py # Script to test C implementation of inference
├── training.py # Training script for the neural network
└── trainingparameters.yaml # Configuration file for training parameters
Training Pipeline
The data pipeline is split into several Python scripts for flexibility:
-
Configuration: Modify
trainingparameters.yamlto set all hyperparameters for training the model. -
Training the Model: The
training.pyscript is used to train the model and store the weights as a.pthfile in themodeldata/folder. The model weights are still in float format at this stage, as they are quantized on-the-fly during training. -
Exporting the Quantized Model: The
exportquant.pyscript is used to convert the model into a quantized format. The quantized model weights are exported to the C header fileBitNetMCU_model.h. -
Optional: Testing the C-Model: Compile and execute
BitNetMCU_MNIST_test.cto test inference of ten digits. The model data is included fromBitNetMCU_MNIST_test_data.h, and the test data is included from theBitNetMCU_MNIST_test_data.hfile. -
Optional: Verification C vs Python Model on full dataset: The inference code, along with the model data, is compiled into a DLL. The
test-inference.pyscript calls the DLL and compares the results with the original Python model. This allows for an accurate comparison to the entire MNIST test data set of 10,000 images. -
Optional: Testing inference on the MCU: follow the instructions in
mcu/readme.md. Porting to architectures other than CH32V003 is straightforward and the files in themcudirectory can serve as a reference.
Updates
- 24th April 2024 - First release with Binary, Ternary, 2 bit, 4 bit and 8 bit quantization.
- 2nd May 2024 - tagged version 0.1a
- 8th May 2024 - Added FP1.3.0 Quantization to allow fully multiplication-free inference with 98.9% accuracy.
- 11th May 2024 - Fixes for Linux. Thanks to @donn
- 19th May 2024 - Add support for non-symmetric 4bit quantization scheme that allows for easier inference on MCUs with multiplier. The inference code will now use code optimized for multiplierless MCUs only on RV32 architectures without multiplier.
- 20th May 2024 - Added
quantscaleas a hyperparameter to influence weight scaling. Updated documentation on new quantization schemes. - 26th May 2024 - tagged version 0.2a
- 19th July 2024 - Added octav algorithm to calculate optimal clipping and quantization parameters.
- 26th July 2024 - Added support for NormalFloat4 (NF4) Quantization. Updated documentation
- 7th September 2025 - New CNN architecture based on sequential depthwise separable convolutions allows to reach 99.55% accuracy while still fitting into 16kb Flash and 4kb RAM. See documentation for details.
- 2nd January 2026 - Add export and inference code for ternary quantization. See documentation for details.
- 6th September 2026 - Optimization of fc models, achieving 99.37% with Muon, NF4A4 / S4A8, activiation quantization and patch-wise processing. See documentation.