r/esp32 • u/Responsible_Bid2834 • 1h ago
I made a thing! I Ran My 30M Parameter Model locally on an ESP32 at 1.12 Tokens/s (8 MB Flash, 8 MB PSRAM)
I’ve been building this project for a few months. It started because I kept hearing that running a language model on an ESP32 was impossible (like literally impossible???)
I didn’t like the sound of that, so this is what I decided to do with several months of my life 😂
I ended up with a 30M parameter model that runs offline on a XIAO ESP32-S3. It generates 1.12 tokens/s using the board’s flash and PSRAM.
Dataset: I used FineWeb-Edu and TinyStories for general text, and SODA, OASST1, UltraChat, and smol-smoltalk for dialogue.
Tokenizer: SentencePiece BPE with a vocabulary of 6,144 tokens.
Architecture: I wanted to try different architectures just out of curiosity. I also wanted to understand why people use GQA in language models in the first place so I chose Transformer GQA, Shared Transformer, GRU, GRU with Local Attention, Mamba-1, RWKV-7, Gated DeltaNet, and Causal TCN. I gave all 8 the same small amount of training data (10M tokens) to see which one would do best.
I tried tests like these:
- Predicting the next token in text the model hadn’t seen before
- Picking which sentence sounds right or which answer makes sense in context
- Answering everyday reasoning questions
- Looking up a number or remembering two facts
- Seeing how well each model did with 20 seconds to train
Transformer GQA did best overall, so I ended up sticking with it. I checked how my model was doing at 50M, 100M, 200M, and 300M tokens. (By my estimate, getting to 300M tokens took about seven and a half hours on my computer). At that point, I went with that checkpoint and focused on making the replies better.
After I shared the project online for the first time , I got a lot of useful comments. So I decided to improve replies and get the model to run faster.
What helped to improve quality of my model's replies:
- I gave it more examples of the kinds of replies I wanted it to give.
- I gave it good and bad answers to the same question and trained it to prefer the better one.
- I went through the mistakes it kept making and gave it more practice on those, using the same chat format it sees on the device.
What helped to improve speed:
- I processed several input tokens together so the board could reuse the weights it had just read.
- I used INT8 SIMD so the chip could do several calculations at once when scoring the next token.
- The biggest change was compressing some weights to 3 bits so they’d fit in PSRAM. That let the board generate the whole reply using flash and PSRAM, with the microSD card used at startup.
With the same prompts on the same board, generation went from 0.76 to 1.12 tokens/s.
For a 30M model I thought the replies were pretty good! It could get what I was talking about, give a relevant answer and stop on its own. Here are some examples:
Me: - i baked a cake!
AI: - That sounds delicious.
Me: - my paper kite finally stayed in the air!
AI: - That sounds amazing.
Me: - my paper kite tore before it got off the ground.
AI: That sounds hard.
I don’t work in this field, so I’m not sure how companies approach projects like this. This was just a personal project I did out of curiosity and for fun! For some reason, I learn best by trying things myself and I’m sure I’ve made a few mistakes along the way 😅
GitHub code, model files, and setup guide
Training, speed improvements, and the live touchscreen demo
My first PocketGPT build













