# slvdev/esp32-ai — AlpLab Radar Static Code Audit

- Ring: `trial`
- Category: `ai-models`
- Overall score: `3.7/5`
- Implementation check: `verified`
- Maturity: `early`
- Audited commit: `52ebcbbd9052` (read-only; nothing from the repository was executed)
- Reviewed at: 2026-12-01T20:26:55Z
- Repository: https://github.com/slvdev/esp32-ai
- Radar entry: https://radar.alplab.co/#repo/slvdev/esp32-ai
- License: `MIT`
- Tags: `esp32`, `llm`, `edge-ai`, `quantization`, `firmware`, `connectome`

## Summary

A complete train->export->deploy pipeline (PyTorch training/ablations, portable C runtime, Arduino firmware) that runs a 28.9M-parameter Per-Layer-Embeddings TinyStories LLM at 4-bit in flash and a 48,311-neuron fruit-fly connectome escape demo entirely on an ESP32-S3, with pinned-hash artifact downloads and host golden gates before any flash write.

## Implementation Check

Every core README claim traces to working code: the flash-resident 25M PLE table and three-tier memory split are implemented in src/model.py (TinyLM, param_budget core/stream/table) and firmware/esp32_barista/esp32_barista.ino (sram_or_ps/ps/copy_norms_to_sram), the exporter's byte format in research/tinystories/export.py (quant_pack, group-128 int4 + fp16 scales, 56-byte header) matches runtime/llm.h's llm_load binding order exactly, and the untrained fly escape (LC4/LPLC2 inputs, DNp01/DNp02/DNp04/DNp11 outputs) is real in research/fly/circuit.py and runtime/connectome_fast.h; only the measured tok/s figures are author-reported and unverifiable statically.

## When to Use

Use as a working reference for memory-tiered LLM inference on microcontrollers, to reproduce the PLE ablations and quantization study, or to extend the connectome demo to your own circuits. Not usable without an ESP32-S3 N16R8 (16MB flash, 8MB PSRAM) and Arduino core 3.3.10, and the model is TinyStories-domain only: no question answering, facts, or instruction following.

## Sub-scores

| Dimension | Score |
|---|---|
| quality | 4.5/5 |
| usability | 3.5/5 |
| reliability | 3.0/5 |
| security | 4.5/5 |
| integration | 3.0/5 |

## Code Evidence

src/model.py TinyLM implements all five ablation arms with faithful Gemma-style PLE injection ((RMSNorm(proj(x)/sqrt(D)) + table[tok]*sqrt(P))/sqrt(2) gated per layer) and a param_budget() that separates core/stream/table exactly as RESULTS.md accounts them; research/tinystories/export.py quant_pack() writes ragged group-128 int4 nibbles + fp16 scales behind a magic-0x00454C50 56-byte header that runtime/llm.h llm_load() binds field-for-field and validates (version, flag bits, dim%n_heads, tied-head row bound) before any pointer is taken; runtime/llm.h llm_forward() reproduces model.py op-for-op (split-half RoPE, erf-GELU, SiLU-SwiGLU, RMSNorm) plus an int8-activation path (quantize_act/matvec_i8_range) with the noinline regression documented; firmware/esp32_barista/esp32_barista.ino enforces the SRAM/PSRAM/flash split (sram_or_ps, KV in PSRAM, norms copied to internal RAM), a dual-core layer_matvec_par worker via task notifications, and an FNV-1a weights fingerprint printed at boot; runtime/connectome_fast.h fast_prepare() fully validates the compressed graph (degree<=65535, weight<=32767, monotone varint indices, exact block-end accounting) before unchecked fast_rows() runs, and firmware/esp32_fly/esp32_fly.ino checksums the whole partition, runs a SIMD self-test on both cores, and gates every INIT/STEP on FNV-1a checksums with 30s timeouts; scripts/fetch_model.sh pins per-file SHA-256+size, cross-checks the release metadata.json, and installs all-or-nothing with a signal-safe restore() rollback; scripts/deploy.sh runs the PyTorch-golden gate (runtime/host_verify/verify.c) and the staged-int8 gate (runtime/host_verify/staging_verify.c, which also fuzzes malformed headers and the untied-head path) before compiling, and compiles before either flash write; tests/test_deploy.py and tests/test_fetch_model.py exercise ordering, refusal, lock and rollback behavior against stubbed hf/arduino-cli/esptool/mv including injected interrupts; runtime/bpe_tokenizer.h bpe_tokenizer_load() bounds-checks the BTK1 asset (merge_count, merge_base, total size) before use and rejects non-ASCII input explicitly.

## Issues Found

No CI config exists despite ten test modules (tests/*.py, research/fly/test_*.py), so nothing enforces the suite on push; scripts/deploy.sh find_esptool() and port autodetection assume macOS paths first ($HOME/Library/Arduino15, /dev/cu.usbmodem*), leaving Linux without explicit PORT/ARDUINO_DATA_DIR setup.

## Key Risks

Nothing was executed: the 9.88 tok/s, 1.7s fly decision, and bandwidth figures are author-reported measurements in RESULTS.md and firmware/esp32_fly/README.md; the profiling code that produces them (LLM_PROFILE hooks, scripts/benchmark_device.py) exists but its outputs were not reproduced. Hardware-gated: needs an ESP32-S3 N16R8, Arduino esp32 core 3.3.10 and three Adafruit display libraries, plus cc, zlib and sanitizers for the host gold gates. scripts/deploy.sh autodetects only /dev/cu.usbmodem* (macOS), so Linux users must set PORT. Released weights live in separate Hugging Face repos trusted via in-script pins. Early-stage, single contributor, no CI, no releases; the fly graph is a selected subgraph, not the full nervous system.

## Reviewed Files

- `README.md`
- `pyproject.toml`
- `RESULTS.md`
- `src/model.py`
- `src/quantize.py`
- `scripts/fetch_model.sh`
- `scripts/deploy.sh`
- `runtime/llm.h`
- `firmware/esp32_barista/esp32_barista.ino`
- `firmware/esp32_fly/esp32_fly.ino`
- `firmware/esp32_fly/README.md`
- `research/tinystories/train.py`
- `research/tinystories/export.py`
- `research/fly/circuit.py`
- `research/fly/test_circuit.py`
- `runtime/bpe_tokenizer.h`
- `runtime/connectome_fast.h`
- `runtime/host_verify/verify.c`
- `runtime/host_verify/staging_verify.c`
- `scripts/benchmark_device.py`
- `tests/test_model_head.py`
- `tests/test_quantization.py`
- `tests/test_deploy.py`
- `tests/test_fetch_model.py`
