nano-rust-py 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nano_rust_py-0.1.0/Cargo.lock +194 -0
- nano_rust_py-0.1.0/Cargo.toml +9 -0
- nano_rust_py-0.1.0/LICENSE +21 -0
- nano_rust_py-0.1.0/PKG-INFO +409 -0
- nano_rust_py-0.1.0/README.md +371 -0
- nano_rust_py-0.1.0/core/Cargo.toml +21 -0
- nano_rust_py-0.1.0/core/src/arena.rs +138 -0
- nano_rust_py-0.1.0/core/src/error.rs +49 -0
- nano_rust_py-0.1.0/core/src/layers/activations.rs +191 -0
- nano_rust_py-0.1.0/core/src/layers/conv.rs +116 -0
- nano_rust_py-0.1.0/core/src/layers/dense.rs +312 -0
- nano_rust_py-0.1.0/core/src/layers/flatten.rs +36 -0
- nano_rust_py-0.1.0/core/src/layers/mod.rs +123 -0
- nano_rust_py-0.1.0/core/src/layers/pooling.rs +81 -0
- nano_rust_py-0.1.0/core/src/lib.rs +63 -0
- nano_rust_py-0.1.0/core/src/math.rs +653 -0
- nano_rust_py-0.1.0/core/src/model.rs +155 -0
- nano_rust_py-0.1.0/core/src/tensor.rs +114 -0
- nano_rust_py-0.1.0/core/tests/general_purpose_test.rs +510 -0
- nano_rust_py-0.1.0/py_binding/Cargo.toml +14 -0
- nano_rust_py-0.1.0/py_binding/src/lib.rs +303 -0
- nano_rust_py-0.1.0/pyproject.toml +46 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# This file is automatically @generated by Cargo.
|
|
2
|
+
# It is not intended for manual editing.
|
|
3
|
+
version = 4
|
|
4
|
+
|
|
5
|
+
[[package]]
|
|
6
|
+
name = "autocfg"
|
|
7
|
+
version = "1.5.0"
|
|
8
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
9
|
+
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
|
10
|
+
|
|
11
|
+
[[package]]
|
|
12
|
+
name = "cfg-if"
|
|
13
|
+
version = "1.0.4"
|
|
14
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
15
|
+
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
|
16
|
+
|
|
17
|
+
[[package]]
|
|
18
|
+
name = "heck"
|
|
19
|
+
version = "0.5.0"
|
|
20
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
21
|
+
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
|
22
|
+
|
|
23
|
+
[[package]]
|
|
24
|
+
name = "indoc"
|
|
25
|
+
version = "2.0.7"
|
|
26
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
27
|
+
checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706"
|
|
28
|
+
dependencies = [
|
|
29
|
+
"rustversion",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[[package]]
|
|
33
|
+
name = "libc"
|
|
34
|
+
version = "0.2.182"
|
|
35
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
36
|
+
checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112"
|
|
37
|
+
|
|
38
|
+
[[package]]
|
|
39
|
+
name = "libm"
|
|
40
|
+
version = "0.2.16"
|
|
41
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
42
|
+
checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
|
|
43
|
+
|
|
44
|
+
[[package]]
|
|
45
|
+
name = "memoffset"
|
|
46
|
+
version = "0.9.1"
|
|
47
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
48
|
+
checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a"
|
|
49
|
+
dependencies = [
|
|
50
|
+
"autocfg",
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
[[package]]
|
|
54
|
+
name = "nano-rust-core"
|
|
55
|
+
version = "0.1.0"
|
|
56
|
+
dependencies = [
|
|
57
|
+
"libm",
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
[[package]]
|
|
61
|
+
name = "nano-rust-py"
|
|
62
|
+
version = "0.1.0"
|
|
63
|
+
dependencies = [
|
|
64
|
+
"nano-rust-core",
|
|
65
|
+
"pyo3",
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
[[package]]
|
|
69
|
+
name = "once_cell"
|
|
70
|
+
version = "1.21.3"
|
|
71
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
72
|
+
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
|
73
|
+
|
|
74
|
+
[[package]]
|
|
75
|
+
name = "portable-atomic"
|
|
76
|
+
version = "1.13.1"
|
|
77
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
78
|
+
checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"
|
|
79
|
+
|
|
80
|
+
[[package]]
|
|
81
|
+
name = "proc-macro2"
|
|
82
|
+
version = "1.0.106"
|
|
83
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
84
|
+
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
|
85
|
+
dependencies = [
|
|
86
|
+
"unicode-ident",
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
[[package]]
|
|
90
|
+
name = "pyo3"
|
|
91
|
+
version = "0.22.6"
|
|
92
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
93
|
+
checksum = "f402062616ab18202ae8319da13fa4279883a2b8a9d9f83f20dbade813ce1884"
|
|
94
|
+
dependencies = [
|
|
95
|
+
"cfg-if",
|
|
96
|
+
"indoc",
|
|
97
|
+
"libc",
|
|
98
|
+
"memoffset",
|
|
99
|
+
"once_cell",
|
|
100
|
+
"portable-atomic",
|
|
101
|
+
"pyo3-build-config",
|
|
102
|
+
"pyo3-ffi",
|
|
103
|
+
"pyo3-macros",
|
|
104
|
+
"unindent",
|
|
105
|
+
]
|
|
106
|
+
|
|
107
|
+
[[package]]
|
|
108
|
+
name = "pyo3-build-config"
|
|
109
|
+
version = "0.22.6"
|
|
110
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
111
|
+
checksum = "b14b5775b5ff446dd1056212d778012cbe8a0fbffd368029fd9e25b514479c38"
|
|
112
|
+
dependencies = [
|
|
113
|
+
"once_cell",
|
|
114
|
+
"target-lexicon",
|
|
115
|
+
]
|
|
116
|
+
|
|
117
|
+
[[package]]
|
|
118
|
+
name = "pyo3-ffi"
|
|
119
|
+
version = "0.22.6"
|
|
120
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
121
|
+
checksum = "9ab5bcf04a2cdcbb50c7d6105de943f543f9ed92af55818fd17b660390fc8636"
|
|
122
|
+
dependencies = [
|
|
123
|
+
"libc",
|
|
124
|
+
"pyo3-build-config",
|
|
125
|
+
]
|
|
126
|
+
|
|
127
|
+
[[package]]
|
|
128
|
+
name = "pyo3-macros"
|
|
129
|
+
version = "0.22.6"
|
|
130
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
131
|
+
checksum = "0fd24d897903a9e6d80b968368a34e1525aeb719d568dba8b3d4bfa5dc67d453"
|
|
132
|
+
dependencies = [
|
|
133
|
+
"proc-macro2",
|
|
134
|
+
"pyo3-macros-backend",
|
|
135
|
+
"quote",
|
|
136
|
+
"syn",
|
|
137
|
+
]
|
|
138
|
+
|
|
139
|
+
[[package]]
|
|
140
|
+
name = "pyo3-macros-backend"
|
|
141
|
+
version = "0.22.6"
|
|
142
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
143
|
+
checksum = "36c011a03ba1e50152b4b394b479826cad97e7a21eb52df179cd91ac411cbfbe"
|
|
144
|
+
dependencies = [
|
|
145
|
+
"heck",
|
|
146
|
+
"proc-macro2",
|
|
147
|
+
"pyo3-build-config",
|
|
148
|
+
"quote",
|
|
149
|
+
"syn",
|
|
150
|
+
]
|
|
151
|
+
|
|
152
|
+
[[package]]
|
|
153
|
+
name = "quote"
|
|
154
|
+
version = "1.0.44"
|
|
155
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
156
|
+
checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4"
|
|
157
|
+
dependencies = [
|
|
158
|
+
"proc-macro2",
|
|
159
|
+
]
|
|
160
|
+
|
|
161
|
+
[[package]]
|
|
162
|
+
name = "rustversion"
|
|
163
|
+
version = "1.0.22"
|
|
164
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
165
|
+
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
|
|
166
|
+
|
|
167
|
+
[[package]]
|
|
168
|
+
name = "syn"
|
|
169
|
+
version = "2.0.116"
|
|
170
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
171
|
+
checksum = "3df424c70518695237746f84cede799c9c58fcb37450d7b23716568cc8bc69cb"
|
|
172
|
+
dependencies = [
|
|
173
|
+
"proc-macro2",
|
|
174
|
+
"quote",
|
|
175
|
+
"unicode-ident",
|
|
176
|
+
]
|
|
177
|
+
|
|
178
|
+
[[package]]
|
|
179
|
+
name = "target-lexicon"
|
|
180
|
+
version = "0.12.16"
|
|
181
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
182
|
+
checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1"
|
|
183
|
+
|
|
184
|
+
[[package]]
|
|
185
|
+
name = "unicode-ident"
|
|
186
|
+
version = "1.0.24"
|
|
187
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
188
|
+
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
|
189
|
+
|
|
190
|
+
[[package]]
|
|
191
|
+
name = "unindent"
|
|
192
|
+
version = "0.2.4"
|
|
193
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
194
|
+
checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Niem Le
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nano-rust-py
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Classifier: Development Status :: 4 - Beta
|
|
5
|
+
Classifier: Intended Audience :: Developers
|
|
6
|
+
Classifier: Intended Audience :: Science/Research
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Programming Language :: Rust
|
|
9
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
18
|
+
Classifier: Topic :: Software Development :: Embedded Systems
|
|
19
|
+
Requires-Dist: torchaudio ; extra == 'audio'
|
|
20
|
+
Requires-Dist: soundfile ; extra == 'audio'
|
|
21
|
+
Requires-Dist: maturin ; extra == 'dev'
|
|
22
|
+
Requires-Dist: pytest ; extra == 'dev'
|
|
23
|
+
Requires-Dist: torch ; extra == 'train'
|
|
24
|
+
Requires-Dist: torchvision ; extra == 'train'
|
|
25
|
+
Requires-Dist: numpy ; extra == 'train'
|
|
26
|
+
Provides-Extra: audio
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Provides-Extra: train
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Summary: TinyML inference engine for embedded devices — Rust no_std core with Python bindings
|
|
31
|
+
Keywords: tinyml,embedded,rust,quantization,inference,esp32
|
|
32
|
+
Author: Niem Le
|
|
33
|
+
Requires-Python: >=3.8
|
|
34
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
35
|
+
Project-URL: Homepage, https://github.com/LeeNim/nano-rust
|
|
36
|
+
Project-URL: Repository, https://github.com/LeeNim/nano-rust
|
|
37
|
+
|
|
38
|
+
# 🧠 NANO-RUST-AI
|
|
39
|
+
|
|
40
|
+
**TinyML Framework for Embedded Devices — Rust `no_std` Core + Python Bindings**
|
|
41
|
+
|
|
42
|
+
Train in PyTorch → Quantize (i8) → Run on MCU (ESP32, STM32, Cortex-M)
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## ✨ Features
|
|
47
|
+
|
|
48
|
+
- **🔒 No Heap**: Pure `no_std` — zero `malloc`, zero dynamic allocation
|
|
49
|
+
- **⚡ Int8 Quantization**: All compute in i8/i32 for 4× memory savings over f32
|
|
50
|
+
- **🧊 Hybrid Memory**: Frozen weights in Flash (0 bytes RAM), trainable head in RAM
|
|
51
|
+
- **🎯 Scale-Aware Requantization**: TFLite-style `(acc × M) >> shift` for accurate i8 output
|
|
52
|
+
- **🐍 Python Bindings**: PyO3 wrapper for seamless PyTorch → NANO-RUST pipeline
|
|
53
|
+
- **📦 Arena Allocator**: User provides `&mut [u8]` buffer — library self-manages within it
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## 📋 Quick Start
|
|
58
|
+
|
|
59
|
+
### 1. Prerequisites
|
|
60
|
+
|
|
61
|
+
| Tool | Version |
|
|
62
|
+
|------|---------|
|
|
63
|
+
| Rust | 1.70+ (`rustup install stable`) |
|
|
64
|
+
| Python | 3.9+ |
|
|
65
|
+
| maturin | `pip install maturin` |
|
|
66
|
+
|
|
67
|
+
### 2. Create Virtual Environment
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
# Create and activate venv
|
|
71
|
+
python -m venv .venv
|
|
72
|
+
|
|
73
|
+
# Windows
|
|
74
|
+
.venv\Scripts\activate
|
|
75
|
+
|
|
76
|
+
# Linux/Mac
|
|
77
|
+
source .venv/bin/activate
|
|
78
|
+
|
|
79
|
+
# Install dependencies
|
|
80
|
+
pip install maturin numpy torch torchvision jupyter ipykernel
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 3. Build & Install the Library
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
# IMPORTANT: Set CARGO_TARGET_DIR outside OneDrive to avoid file locking
|
|
87
|
+
# Windows PowerShell:
|
|
88
|
+
$env:CARGO_TARGET_DIR = "$env:USERPROFILE\.nanorust_target"
|
|
89
|
+
|
|
90
|
+
# Build and install into the active venv
|
|
91
|
+
maturin develop --release
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 4. Register Jupyter Kernel (for notebooks)
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
python -m ipykernel install --user --name nanorust --display-name "NanoRust (venv)"
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Then select the **"NanoRust (venv)"** kernel in Jupyter when running notebooks.
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
## 🏗️ Architecture
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
┌──────────────────────────────────────┐
|
|
108
|
+
│ Python (PyTorch + nano_rust_utils) │ ← Train & Quantize
|
|
109
|
+
├──────────────────────────────────────┤
|
|
110
|
+
│ PyO3 Binding (nano_rust_py) │ ← Bridge
|
|
111
|
+
├──────────────────────────────────────┤
|
|
112
|
+
│ Rust Core (nano-rust-core) │ ← Inference Engine
|
|
113
|
+
│ ┌────────┐ ┌────────┐ ┌─────────┐ │
|
|
114
|
+
│ │ math.rs│ │layers/ │ │arena.rs │ │
|
|
115
|
+
│ │ matmul │ │dense │ │bump ptr │ │
|
|
116
|
+
│ │ conv2d │ │conv │ │ckpt/rst │ │
|
|
117
|
+
│ │ relu │ │pool │ └─────────┘ │
|
|
118
|
+
│ │sigmoid │ │flatten │ │
|
|
119
|
+
│ │ tanh │ │activate│ │
|
|
120
|
+
│ └────────┘ └────────┘ │
|
|
121
|
+
└──────────────────────────────────────┘
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Memory Layout on MCU
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
FLASH (4MB) RAM (320KB)
|
|
128
|
+
┌─────────────────────┐ ┌──────────────────┐
|
|
129
|
+
│ Frozen Backbone │ │ Arena Buffer │
|
|
130
|
+
│ - Conv2D weights │ │ ┌──────────────┐ │
|
|
131
|
+
│ - Dense weights │ │ │ Intermediate │ │
|
|
132
|
+
│ - Bias arrays │ │ │ activations │ │
|
|
133
|
+
│ (read-only, static) │ │ ├──────────────┤ │
|
|
134
|
+
│ │ │ │ Trainable │ │
|
|
135
|
+
│ │ │ │ Head weights │ │
|
|
136
|
+
│ │ │ └──────────────┘ │
|
|
137
|
+
└─────────────────────┘ └──────────────────┘
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## 🐍 Python API Reference
|
|
143
|
+
|
|
144
|
+
### `nano_rust_py.PySequentialModel`
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
model = nano_rust_py.PySequentialModel(
|
|
148
|
+
input_shape=[C, H, W], # or [N] for 1D
|
|
149
|
+
arena_size=32768 # bytes for scratch memory
|
|
150
|
+
)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Layer Methods
|
|
154
|
+
|
|
155
|
+
| Method | Description |
|
|
156
|
+
|--------|-------------|
|
|
157
|
+
| `add_dense(weights, bias)` | Dense layer (i8 weights/bias as lists) |
|
|
158
|
+
| `add_dense_with_requant(weights, bias, M, shift)` | Dense with calibrated requant |
|
|
159
|
+
| `add_conv2d(kernel, bias, in_ch, out_ch, kh, kw, stride, padding)` | Conv2D layer |
|
|
160
|
+
| `add_conv2d_with_requant(kernel, bias, in_ch, out_ch, kh, kw, stride, padding, M, shift)` | Conv2D with calibrated requant |
|
|
161
|
+
| `add_trainable_dense(in_features, out_features)` | Trainable Dense (RAM weights) |
|
|
162
|
+
| `add_relu()` | ReLU activation |
|
|
163
|
+
| `add_sigmoid()` | Sigmoid (fixed scale, for general use) |
|
|
164
|
+
| `add_sigmoid_scaled(scale_mult, scale_shift)` | Sigmoid with scale-aware LUT |
|
|
165
|
+
| `add_tanh()` | Tanh (fixed scale, for general use) |
|
|
166
|
+
| `add_tanh_scaled(scale_mult, scale_shift)` | Tanh with scale-aware LUT |
|
|
167
|
+
| `add_softmax()` | Softmax (pseudo-probabilities) |
|
|
168
|
+
| `add_flatten()` | Flatten 3D→1D |
|
|
169
|
+
| `add_max_pool2d(kernel, stride, padding)` | MaxPool2D |
|
|
170
|
+
|
|
171
|
+
### Inference
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
output = model.forward(input_i8_list) # Returns list of i8 values
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Python Utilities (`scripts/nano_rust_utils.py`)
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
from nano_rust_utils import quantize_to_i8, quantize_weights, calibrate_model
|
|
181
|
+
|
|
182
|
+
# Quantize input
|
|
183
|
+
q_input, input_scale = quantize_to_i8(float_array)
|
|
184
|
+
|
|
185
|
+
# Quantize model weights
|
|
186
|
+
q_weights = quantize_weights(pytorch_model)
|
|
187
|
+
|
|
188
|
+
# Calibrate requantization parameters
|
|
189
|
+
requant = calibrate_model(model, input_tensor, q_weights, input_scale)
|
|
190
|
+
# Returns dict: layer_name → (M, shift, bias_corrected) for parametric layers
|
|
191
|
+
# ('sigmoid', mult, shift) for Sigmoid
|
|
192
|
+
# ('tanh', mult, shift) for Tanh
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## 📓 Notebooks
|
|
198
|
+
|
|
199
|
+
### Validation Notebooks (`notebooks/`)
|
|
200
|
+
|
|
201
|
+
Quick-run notebooks using `_setup.py` for auto-build:
|
|
202
|
+
|
|
203
|
+
| # | File | Description |
|
|
204
|
+
|---|------|-------------|
|
|
205
|
+
| 01 | `01_pipeline_validation.ipynb` | Conv→ReLU→Flatten→Dense end-to-end |
|
|
206
|
+
| 02 | `02_mlp_classification.ipynb` | MLP (Dense→ReLU→Dense) |
|
|
207
|
+
| 03 | `03_deep_cnn.ipynb` | Deep CNN with MaxPool |
|
|
208
|
+
| 04 | `04_activation_functions.ipynb` | ReLU vs Sigmoid vs Tanh comparison |
|
|
209
|
+
| 05 | `05_transfer_learning.ipynb` | Frozen backbone + trainable head |
|
|
210
|
+
|
|
211
|
+
### Real-World Test Scripts (`notebooks-for-test/`)
|
|
212
|
+
|
|
213
|
+
GPU-accelerated training → i8 quantization → NANO-RUST verification:
|
|
214
|
+
|
|
215
|
+
| # | File | Task | Accuracy |
|
|
216
|
+
|---|------|------|----------|
|
|
217
|
+
| 06 | `run_06_mnist.py` | MNIST digit classification (CNN) | ~97% |
|
|
218
|
+
| 07 | `run_07_fashion.py` | Fashion item classification (CNN) | ~87% |
|
|
219
|
+
| 08 | `run_08_sensor.py` | Industrial sensor fusion (MLP) | ~98% |
|
|
220
|
+
| 09 | `run_09_keyword_spotting.py` | Voice keyword spotting (MFCC+MLP) | ~79% |
|
|
221
|
+
| 10 | `run_10_text_classifier.py` | Text classification (BoW+MLP) | 100% |
|
|
222
|
+
|
|
223
|
+
Run all tests:
|
|
224
|
+
```bash
|
|
225
|
+
python notebooks-for-test/run_06_mnist.py
|
|
226
|
+
# ... etc
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
## 🚀 ESP32 Deployment Guide
|
|
232
|
+
|
|
233
|
+
### Step 1: Train & Export in Python
|
|
234
|
+
|
|
235
|
+
```python
|
|
236
|
+
import torch.nn as nn
|
|
237
|
+
from nano_rust_utils import quantize_weights, calibrate_model, export_to_rust
|
|
238
|
+
|
|
239
|
+
# 1. Train your PyTorch model
|
|
240
|
+
model = nn.Sequential(
|
|
241
|
+
nn.Linear(416, 128), nn.ReLU(),
|
|
242
|
+
nn.Linear(128, 64), nn.ReLU(),
|
|
243
|
+
nn.Linear(64, 10),
|
|
244
|
+
)
|
|
245
|
+
# ... train on GPU ...
|
|
246
|
+
|
|
247
|
+
# 2. Quantize & calibrate
|
|
248
|
+
q_weights = quantize_weights(model)
|
|
249
|
+
requant = calibrate_model(model, sample_input, q_weights, input_scale)
|
|
250
|
+
|
|
251
|
+
# 3. Export to Rust source code
|
|
252
|
+
rust_code = export_to_rust(model, "keyword_model", input_shape=[416])
|
|
253
|
+
with open("model.rs", "w") as f:
|
|
254
|
+
f.write(rust_code)
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### Step 2: Use in ESP32 Rust Firmware
|
|
258
|
+
|
|
259
|
+
```rust
|
|
260
|
+
#![no_std]
|
|
261
|
+
use nano_rust_core::{Arena, model::SequentialModel};
|
|
262
|
+
|
|
263
|
+
// Generated model from export_to_rust()
|
|
264
|
+
include!("model.rs");
|
|
265
|
+
|
|
266
|
+
#[entry]
|
|
267
|
+
fn main() -> ! {
|
|
268
|
+
// Arena in RAM — size from model.estimate_arena_size()
|
|
269
|
+
let mut arena_buf = [0u8; 16384];
|
|
270
|
+
|
|
271
|
+
loop {
|
|
272
|
+
// Get sensor/audio data → quantize to i8
|
|
273
|
+
let input: [i8; 416] = read_mfcc_features();
|
|
274
|
+
|
|
275
|
+
// Run inference (< 1ms on ESP32 @ 240MHz)
|
|
276
|
+
let mut arena = Arena::new(&mut arena_buf);
|
|
277
|
+
let model = build_keyword_model(); // From generated code
|
|
278
|
+
let (output, _) = model.forward(&input, &[416], &mut arena).unwrap();
|
|
279
|
+
|
|
280
|
+
let predicted_class = output.iter()
|
|
281
|
+
.enumerate()
|
|
282
|
+
.max_by_key(|(_, v)| **v)
|
|
283
|
+
.map(|(i, _)| i)
|
|
284
|
+
.unwrap();
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### Memory Budget (ESP32)
|
|
290
|
+
|
|
291
|
+
| Component | Flash | RAM |
|
|
292
|
+
|-----------|-------|-----|
|
|
293
|
+
| Frozen weights | 60KB | 0B |
|
|
294
|
+
| Arena buffer | 0B | 16KB |
|
|
295
|
+
| Code + stack | ~20KB | ~4KB |
|
|
296
|
+
| **Total** | **~80KB** | **~20KB** |
|
|
297
|
+
| **Available** | **4MB** | **520KB** |
|
|
298
|
+
|
|
299
|
+
---
|
|
300
|
+
|
|
301
|
+
## 🔧 Rust Core API (`nano-rust-core`)
|
|
302
|
+
|
|
303
|
+
### Layers
|
|
304
|
+
|
|
305
|
+
```rust
|
|
306
|
+
use nano_rust_core::layers::*;
|
|
307
|
+
|
|
308
|
+
// Frozen (Flash) — 0 bytes RAM for weights
|
|
309
|
+
let dense = FrozenDense::new_with_requant(weights, bias, in_f, out_f, M, shift)?;
|
|
310
|
+
let conv = FrozenConv2D::new_with_requant(kernel, bias, in_ch, out_ch, kh, kw, s, p, M, shift)?;
|
|
311
|
+
|
|
312
|
+
// Trainable (RAM) — weights allocated in Arena
|
|
313
|
+
let head = TrainableDense::new(in_features, out_features);
|
|
314
|
+
|
|
315
|
+
// Activations
|
|
316
|
+
let _ = ReLULayer;
|
|
317
|
+
let _ = ScaledSigmoidLayer { scale_mult: 42, scale_shift: 8 };
|
|
318
|
+
let _ = ScaledTanhLayer { scale_mult: 84, scale_shift: 8 };
|
|
319
|
+
let _ = SoftmaxLayer;
|
|
320
|
+
|
|
321
|
+
// Structural
|
|
322
|
+
let _ = FlattenLayer;
|
|
323
|
+
let _ = MaxPool2DLayer::new(2, 2, 0)?;
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
### Arena Allocator
|
|
327
|
+
|
|
328
|
+
```rust
|
|
329
|
+
use nano_rust_core::Arena;
|
|
330
|
+
|
|
331
|
+
let mut buf = [0u8; 32768];
|
|
332
|
+
let mut arena = Arena::new(&mut buf);
|
|
333
|
+
|
|
334
|
+
// Checkpoint/restore for scratch memory reuse
|
|
335
|
+
let cp = arena.checkpoint();
|
|
336
|
+
let scratch = arena.alloc_i8_slice(1024)?;
|
|
337
|
+
arena.restore(cp); // reclaim scratch memory
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
### Sequential Model
|
|
341
|
+
|
|
342
|
+
```rust
|
|
343
|
+
use nano_rust_core::model::SequentialModel;
|
|
344
|
+
|
|
345
|
+
let mut model = SequentialModel::new();
|
|
346
|
+
model.add(Box::new(dense));
|
|
347
|
+
model.add(Box::new(ReLULayer));
|
|
348
|
+
let (output, shape) = model.forward(input, &input_shape, &mut arena)?;
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
---
|
|
352
|
+
|
|
353
|
+
## 📊 Accuracy Targets
|
|
354
|
+
|
|
355
|
+
| Model Type | Expected Max Diff (vs PyTorch) |
|
|
356
|
+
|------------|-------------------------------|
|
|
357
|
+
| Dense + ReLU | ≤ 3 |
|
|
358
|
+
| Conv + ReLU + Dense | ≤ 5 |
|
|
359
|
+
| Deep CNN + Pool | ≤ 10 |
|
|
360
|
+
| Sigmoid/Tanh (scaled) | ≤ 20 |
|
|
361
|
+
|
|
362
|
+
---
|
|
363
|
+
|
|
364
|
+
## 🗂️ Project Structure
|
|
365
|
+
|
|
366
|
+
```
|
|
367
|
+
nano-rust/
|
|
368
|
+
├── core/ # Rust no_std core library
|
|
369
|
+
│ └── src/
|
|
370
|
+
│ ├── lib.rs # Crate root
|
|
371
|
+
│ ├── arena.rs # Bump pointer allocator
|
|
372
|
+
│ ├── math.rs # Matmul, conv2d, activations
|
|
373
|
+
│ ├── error.rs # Error types
|
|
374
|
+
│ ├── model.rs # SequentialModel
|
|
375
|
+
│ └── layers/
|
|
376
|
+
│ ├── mod.rs # Layer trait + Shape
|
|
377
|
+
│ ├── dense.rs # FrozenDense + TrainableDense
|
|
378
|
+
│ ├── conv.rs # FrozenConv2D
|
|
379
|
+
│ ├── activations.rs # ReLU, Sigmoid, Tanh, Softmax
|
|
380
|
+
│ ├── flatten.rs # Flatten layer
|
|
381
|
+
│ └── pooling.rs # MaxPool2D
|
|
382
|
+
├── py_binding/ # PyO3 Python bindings
|
|
383
|
+
│ └── src/lib.rs
|
|
384
|
+
├── scripts/
|
|
385
|
+
│ ├── nano_rust_utils.py # Quantization + calibration utilities
|
|
386
|
+
│ └── export.py # CLI weight exporter
|
|
387
|
+
├── notebooks/ # Quick validation notebooks (01-05)
|
|
388
|
+
├── notebooks-for-test/ # Real-world test scripts (06-10)
|
|
389
|
+
├── pyproject.toml # pip install configuration
|
|
390
|
+
├── Cargo.toml # Workspace config
|
|
391
|
+
├── LICENSE # MIT License
|
|
392
|
+
└── README.md
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
---
|
|
396
|
+
|
|
397
|
+
## 📜 License
|
|
398
|
+
|
|
399
|
+
[MIT](LICENSE)
|
|
400
|
+
|
|
401
|
+
---
|
|
402
|
+
|
|
403
|
+
## 🔮 Roadmap
|
|
404
|
+
|
|
405
|
+
- [x] v0.1.0: Core inference engine with scale-aware requantization
|
|
406
|
+
- [ ] v0.2.0: Const Generics refactor for compile-time optimization
|
|
407
|
+
- [ ] v0.3.0: On-device training (backprop for trainable head)
|
|
408
|
+
- [ ] v0.4.0: ARM SIMD intrinsics (SMLAD) for Cortex-M
|
|
409
|
+
|