FTerViT: A Fully Ternary Vision Transformer That Runs on a $10 Microcontroller
We extended ternary quantization to the layers usually left in higher precision, making a standard Vision Transformer small enough to run entirely on an ESP32-S3.
1 CSEM, Neuchâtel·2 ETH Zürich
The ESP32-S3-EYE board running FTerViT entirely on-device. Left: the cat classification on its LCD. Right: the boot screen with the CSEM and ETH Zürich logos.
Making a ViT fit
A standard Vision Transformer takes 88 MB in 32-bit floats — more than ten times the flash on a $10 microcontroller. I wanted to see if we could compress the entire architecture to fit. A ViT turns image patches into tokens, processes them through transformer blocks, and predicts a class.
Ternary quantization stores each weight as {-1, 0, +1}, with a scale to restore its magnitude. Packing these values in 2 bits gives a theoretical 16× weight compression. But earlier ternary ViTs leave the patch embedding, LayerNorm, and classifier head in higher precision. As the transformer blocks shrink, those “leftover” layers take a larger share of storage.
With FTerViT, we extended ternarization to those layers too. On the same DeiT-S backbone, we reach 77.47% ImageNet-1K top-1 at 5.81 MB, compared with TerViT’s 74.2% at 6.0 MB. Top-1 measures how often the model’s highest-ranked prediction is correct.
Interactive: hover or tap any bar to explore the storage breakdown. In DeiT-Tiny the higher-precision leftover grows from 14% of the bytes at INT8 to 39% once only the encoder is ternary. Ternarizing it too brings it down to 10%, and the whole model from 22.9 MB to 1.6 MB (paper Figure 1b).
Why the first and last layers are the hard part
The patch embedding is the model’s first view of an image, and the classifier makes the final decision. TerViT reported a 22.4 percentage point accuracy drop when ternarizing both. Keeping them at higher precision was an understandable choice.
We used Taylor first-order importance, which combines a weight’s value with its loss gradient, to estimate sensitivity to changes in each component. Parameter count alone gives a misleading picture:
| Component | Share of params | Share of importance (Taylor-FO) |
|---|---|---|
| LayerNorm | < 0.2% | 34–39% |
| Patch embedding | 1–3% | most important single layer in DeiT-S |
| Classifier head | 1.7–3.4% | ≤ 1.3% |
LayerNorm’s importance is spread across many layers. Patch embedding ranks highest among individual quantizable layers in DeiT-S. These are sensitivity estimates, rather than measured accuracy losses, but they show why small components deserve attention.
How we ternarize the remaining layers
We use dedicated operators for the different kinds of layers:
- TernaryBitLinear scales fully connected weights by their mean absolute value before rounding and clipping them to ternary codes. Using the largest weight instead would push too many into the zero bin. During training, input activations are quantized to 8 bits.
- TernaryBitConv2d handles patch embedding with a separate scale per channel, so a large filter does not overwhelm smaller ones.
- TernaryLayerNorm ternarizes the learned scale γ and shift β. The mean and variance computed for each token stay FP32.
“Fully ternary” describes this extension beyond the encoder. Some parameters in BitLinear’s input normalization and other scaling operations remain floating point, as do the weight scales and positional and class embeddings. We initialize the student from a frozen FP32 teacher of the same architecture, then train it to match the teacher’s output probabilities:
- Phase 1: train at learning rate 1e-4 with cosine decay for 250 epochs.
- Phase 2: restart at 1e-5 and fine-tune for 10 epochs.
The paper’s recipe uses only forward-KL loss, which compares the teacher’s and student’s probability distributions. Phase 2 also changes batch size and weight decay. On DeiT-S, this short recovery adds 2.42 percentage points. In the curves below, another 150 epochs of Phase 1 brought no improvement in final accuracy.
Interactive: hover or tap for a synced readout across the curves, and click a legend chip to toggle one. These DeiT-III-S runs use ImageNet-21k pretrained checkpoints. Phase 1 levels off near 78%, below the teacher’s 83.08%. After 10 recovery epochs, the epoch-250 checkpoint reaches 79.64% and the epoch-400 checkpoint reaches 79.61%.
Results
The matched DeiT-S comparison gives a 3.27 percentage point gain over TerViT. The stronger DeiT-III checkpoints were ImageNet-21k pretrained before ImageNet-1K fine-tuning. Their higher scores also reflect that advantage. The table includes different backbones and activation precisions, so the rows need that context.
Interactive: hover or tap a marker for its model, bit width, size, and accuracy. Stars mark FTerViT. Selected results are listed below, with W/A denoting weight and activation bits.
| Method | W/A bits | Model | Size | Top-1 |
|---|---|---|---|---|
| TerViT | 2 / 8 | DeiT-S | 6.0 MB | 74.2% |
| ViT-1.58b | 2 / 8 | ViT-L | 57 MB | 74.25% |
| Q-ViT | 2 / 2 | DeiT-S | 6.0 MB | 72.1% |
| FTerViT (ours) | 2 / 8 | DeiT-S | 5.81 MB | 77.47% |
| FTerViT (ours) | 2 / 8 | DeiT-III-S²²⁴ | 5.81 MB | 79.64% |
| FTerViT (ours) | 2 / 8 | DeiT-III-S³⁸⁴ | 6.09 MB | 82.43% |
At 384×384, the ImageNet-21k pretrained DeiT-III-S model reaches 82.43% at 6.09 MB, a 14.6× reduction in storage with a 2.42 percentage point gap to FP32. In the 224×224 variant, about 37% of ternary weights are zero, although our current kernel still processes those entries. Its patch-embedding features retain a mean cosine similarity of 0.88 to FP32. The recipe still leaves a larger accuracy gap on DeiT-Tiny, which loses 9.19 percentage points on ImageNet-1K.
Running it on a $10 board
This is the part I find most fun. We deployed the ImageNet-21k pretrained DeiT-III-S model at 224×224 on an ESP32-S3-EYE. The roughly $10 board has dual Xtensa LX7 cores at 240 MHz, 8 MB PSRAM, a 2 MP camera, and an LCD. Its 5.81 MB of weights are 15.2× smaller than the original 88.3 MB checkpoint. Including firmware, flash use is 6.30 MB out of 8 MB, or 79%.
Our standalone C engine packs 4 weights per byte and fuses the query, key, and value projections. Activations stay FP32 on the MCU. Peak PSRAM use is 2.83 MB, and a full prediction takes 21.06 seconds. That supports occasional images rather than live video. Separately, we measured one DeiT-Tiny FC1 kernel with a Nordic PPK2 power probe:
Interactive: tap Latency / Power / Energy to switch metric, then hover or tap a bar for the comparison with FP32. For this kernel, packed ternary is 1.71× faster (804 ms vs. 1376 ms). Energy after subtracting board idle power falls by about 55% (59 mJ vs. 130 mJ). Total kernel energy falls by 46.7%.
Those kernel measurements do not establish a speedup for the whole model. The deployment shows that compressing an existing ViT can make it fit on commodity hardware, alongside approaches that redesign the architecture for a microcontroller.
Takeaways
- Count the bytes in the whole model. Small layers can become expensive once the encoder is compressed.
- A same-architecture teacher and a short recovery phase helped us retain accuracy in the sensitive layers.
- Check runtime as well as model size. Fitting in flash is a useful step, and latency determines which applications can use it.
Code and checkpoints are public on GitHub and Hugging Face, and the paper is on arXiv. This work is supported by the Swiss National Science Foundation and the SwissChips initiative (ETH Zürich, EPFL, CSEM).