Running Llama-2 at 400 Tokens Per Second on a Cheap ESP32 Chip!
Some absolute madman actually forced a tiny neural network to run on a ridiculously cheap microcontroller at blazing speeds, proving that modern engineering is just pure stubbornness.
From Tiny Stories to Silicon Sorcery
The experiment started with stories260K from Andrej Karpathy's tinyllamas collection, a miniature model trained exclusively on children's stories about a girl named Lily and her red ball. Running on an ESP32-S3 board connected to a MacBook via USB, the baseline setup initially struggled with memory bandwidth limits. Because the model in raw float32 weighed over a megabyte while the chip only packed 512 KB of internal SRAM, early implementations relied heavily on slower external PSRAM connected over SPI, yielding a sluggish 19 tokens per second.
The Int8 Diet and Assembly Hacks
To break past the hardware bottleneck, weights were converted to int8 with per-row scaling, shrinking the model footprint down to 274 KB and allowing it to sit comfortably in internal memory or execute directly from flash using XIP. Performance jumped immediately, but the real breakthrough came from leveraging ESP32-S3's obscure PIE vector instructions. Because the compiler refused to generate these vectorized MAC operations automatically, custom assembly routines were written by hand in 2026, loading the entire 64-dimensional vector into dedicated registers and processing chunks in parallel.
Unleashing the Second Core
With matrix multiplications optimized, the second CPU core was brought out of retirement to share the load. Instead of wasting cycles on heavyweight FreeRTOS semaphores for microsecond-long tasks, the worker core spins continuously on a volatile counter to split row calculations and attention heads instantly. Attention mechanisms, which traditionally choked on growing text lengths, were rewritten to fit entirely within processor registers, bypassing costly stack allocations and driving the average generation speed past the 400 tokens per second mark.
Engineering has officially reached a bizarre state where microscopic internet-of-things chips can hallucinate children's stories faster than human eyes can scan them, solving absolutely nobody's practical problems while bringing immense joy to anyone who appreciates raw, unapologetic optimization sorcery.
Source: SourceCraft
Comments
This is where the magic happens: AI reads your discussion and rewrites the article based on the most interesting comments. Each strong comment adds points to the meter below. Once the meter is full, the article updates live — no page reload needed.