An AI chatbot for the ESP32-S3
The model you chat with on this website is the same affan-ai-engine-nano-int4-v4.afn file that is prepared for the ESP32-S3 microcontroller. No conversion step, no server.
What you need
- An ESP32-S3 board with 8 MB of PSRAM and 16 MB of flash.
- The int4 model file,
affan-ai-engine-nano-int4-v4.afn(8.44 MB), flashed into a data partition. - Firmware that implements the AEN algorithm (AEN.cpp is the reference, and the web edition on this site is a second, tested implementation).
If your flash partition is larger, there is also an int8 file of about 15 MB that is practically lossless.
The file format
The .afn file begins with the magic AFNESP01, then a JSON header (model configuration, metadata and a tensor table), the tokenizer as JSON, and the weights, aligned to 4096 bytes so the data can be memory-mapped straight from flash. A CRC32 trailer protects the file.
- Weights: int4, two per byte, low nibble first, value = nibble - 8, one fp16 scale per group of 32 weights (read the group size per tensor from the header).
- Embeddings: the token embedding is tied with the output head, and is int4 in the int4 file.
- Norm gains: fp16.
- Activations: int8 per group of 32 at run time, exactly as AEN.cpp does it.
- Metadata: the header carries the model name, licence and the chat system prompt the model was trained with.
What the firmware does
- Memory-map the model from the flash partition and read weights row by row, so the 8.4 MB of weights never have to fit in RAM.
- Tokenise the prompt with the byte-level BPE tokenizer stored in the file, using the chat format with the system prompt.
- Run the transformer one token at a time (int8 activations times int4 weights), keep the key-value cache in PSRAM, and sample the next token.
- Run the calculator tool: when the model writes
<|calc|>...<|/calc|>, evaluate the expression and feed back<|result|>value<|/result|>.
Speed on the chip depends on the firmware port. We have not published ESP32 speed figures yet, and we will add them once they are measured on real hardware.
Try the same model now
You can chat with exactly this model in your browser. It is the quickest way to see what the ESP32 file can do. Read how it works for the architecture and the 4-bit quantisation, or the FAQ.