
In this project an XIAO ESP32S3 runs PocketGPT’s 30-million-parameter language model offline, but generates replies at about 1.12 tokens per second. With the project’s code and setup instructions published, you can use this microcontroller board as a starting point for your own offline chatbot. Marina breaks down how training data, compressed model data and storage choices shape its responses. Learn how to fit the model into limited memory and discover what changes when model data moves from a memory card to the board’s own storage.
Running a Language Model on the ESP32S3
TL;DR Key Takeaways :
- PocketGPT runs a 12-layer GQA transformer with about 30 million parameters offline on the XIAO ESP32S3 microcontroller.
- Part of the model uses three bits per weight to fit in PSRAM, while the remaining weights stay in flash.
- The model still made mistakes at 300 million training tokens.
- Reading model data from flash produced the same answer about six seconds faster than reading from microSD.
- The touchscreen build generated about 1.12 tokens per second after a brief pause to read the message.
Seeed Studio’s XIAO ESP32S3 is a microcontroller board with much less memory than a computer typically used to train a language model. The Jetson Orin Nano Super has 8 GB of RAM, lists at $399 and takes about 25 times as much desk space as the XIAO. PocketGPT uses a 12-layer GQA transformer with about 30 million parameters, so its weights need careful packing and placement to fit on the board.
The standard board has 8 MB of PSRAM, 8 MB of flash storage and 512 KB of SRAM. Seeed Studio lists the unsoldered single board at $7.49, though the board used for this build cost around $5.
Pack the Weights Into Available Memory
Model weights are the values a language model learns during training. Using fewer bits to store each weight reduces the space the model needs: using every byte of PSRAM and flash for weights would give a theoretical ceiling of roughly 4 million parameters at 32 bits, or 34 million at 4 bits. You can apply the same kind of memory-ceiling calculation to any board.
In the PocketGPT build, part of the model is packed at three bits per weight to fit in PSRAM, while the rest stays in flash. Espressif’s ESP-DL toolkit separately supports per-tensor quantization, the use of lower-precision numbers for a whole set of values, with 8-bit or 16-bit weights and activations.
The tokenizer, which splits text into pieces for the model, also affects space. A vocabulary of around 6,000 tokens reduced the steps needed to write two example words from four to two, while increasing the vocabulary further would make the model take up more space.
Advance your skills in ESP32 by reading more of our detailed content.
- How to Build a Custom AI Desk Buddy with a ESP32 S3
- The ESP32-S3 Microcontroller Keeps Neural Network Training on-Chip
- The $4 ESP32 S3 Super Mini Packs a 240 MHz Dual-Core Chip
- ESP32-S3 Super Mini Costs $5 and Packs Wi-Fi and Bluetooth
- ESP32-S3 Super Mini Brings 240 MHz Dual-Core Power For $4
Prepare the Training Data
Training text was selected to help the model hold a conversation, starting with material intended to provide clear explanations. TinyStories was added to give it practice producing coherent sentences, while dialogue datasets such as SODA were included to teach conversational responses. About 90% of the training data was general text, and about 10% was dialogue.
Compare Eight Architectures
Eight architectures were tested on the same training data and with the same tokenizer, allowing their results to be compared under the same conditions. They were compared both after seeing the same small number of training tokens and after 20 seconds of training.
One memory test asked the models to recall a number while ignoring distractions; three of the eight scored perfectly. A second test checked whether the models could keep track of which details belonged to which items, and Gated Delta won that test.
Training speed was timed on a Mac, and Causal TCN processed the most training tokens per second. Other checks covered text prediction, grammar, world knowledge and everyday reasoning, as well as the models’ ability to keep conversations going.
After a short fine-tuning run, even the best models gave only one relevant answer out of 30. All eight architectures were trained to 10 million tokens in the final round, and Transformer GQA was the overall winner.
Train and Fine-Tune the Model
Training used a Mac GPU with PyTorch and Apple Metal. A GPU can perform many matrix calculations at once, although a CPU can also perform PocketGPT’s training calculations. On Windows or Linux, you can use Spyder with CUDA when a compatible NVIDIA GPU is available.
Some AMD and Intel GPUs also work for training, while Google Colab offers browser-based training using cloud resources. Training to 300 million tokens on the Mac was estimated to take about 7.5 hours.
Prompts given after 10 million tokens were revisited as training continued, and progress was uneven: the model still made mistakes at 300 million tokens. A weight-related technique was used to reduce reliance on patterns specific to the training data.
Fine-tuning marked speakers and reply endings to signal when the model should answer and stop. It began with small updates, then lowered the learning rate, while overly strong batch signals were reduced before weight updates. After fine-tuning, the model responded supportively to nervousness rather than treating it as good news.
Move Model Data From microSD to Flash
Reading model data from the board’s own flash, rather than a microSD card in the round display’s slot, reduced the wait for a reply and produced the biggest speed-up. Given the same question, the flash build produced the same answer as the microSD build about six seconds faster.
The microSD version also kept three frequently used pieces of information in faster memory, saving repeated card lookups. Keeping the file open avoided repeatedly reopening it when the board needed to read it again.
Reuse Calculations and Show Tokens Sooner
PocketGPT processes a few pieces of a message at once and shares information looked up across them. It also uses a format that lets the chip perform several multiplications at once, while changed math routines reuse some work and frequently run code sits in faster memory.
The board’s ESP32-S3 chip has two cores running at up to 240 MHz. Espressif claims the chip can perform up to 7.5 billion multiply-accumulate operations per second at that clock speed; that figure is not a language-model token rate.
Reply text appears piece by piece as soon as it is ready, so you can start reading before the answer is complete. That display change does not materially alter generation speed: the touchscreen run produced about 1.12 tokens per second after a brief pause to read the message.
The code and setup guide have been published with the project. The handheld build used a round touchscreen to enter messages and display replies, and the model ran offline on the board.
In our view, the lesson is that where the weights live matters more than clever model design. On a board this small, the biggest gain came from moving storage, which is a useful reminder to look at memory first. Flash delivered the same answer about six seconds faster than microSD, but mistakes remained after 300 million training tokens. We would weigh the reply quality before copying this. Even a well-tuned tiny model makes mistakes, so check that slow, imperfect answers are good enough for your project, and that you have a suitable GPU or cloud notebook for training.
You can use this build as a starting point for exploring how training choices and memory limits shape a small conversational model. For anyone interested in making an offline chatbot on a microcontroller, PocketGPT is a project well worth trying.
Media Credit: Marina
Disclosure: Some of our articles include affiliate links. If you buy something through one of these links, Geeky Gadgets may earn an affiliate commission. Learn about our Disclosure Policy.