Ismail Ben Alla

Tiny BERT

A Small Encoder and Its BPE Tokenizer, From Scratch

NLP · Nov 2026 · In progress

One encoder, two heads: a small BERT and its byte-level BPE, every layer written by hand.

Built withPyTorchHugging Face DatasetsNumPyscikit-learnMatplotlibpytest

Tiny BERT — overview

Overview

Tiny BERT is a small BERT-style encoder (6 layers × 256, 4 heads, about 9M parameters) and its byte-level BPE tokenizer. I wrote and pre-trained both from scratch in PyTorch, without Hugging Face transformers.

It is pre-trained by masked-token prediction on 289M tokens of WikiText-103 and FineWeb-Edu, then fine-tuned on GLUE SST-2 and QNLI. Every result is written to one results file and shown on the project website.

It is the base of my tiny family: tiny-embedder and tiny-reranker start from its pre-trained encoder, and tiny-nn-search and tiny-rag load its published checkpoints (pretrain, sst2, qnli) from a shared model store.

The question

Can I build every piece of a BERT myself, tokenizer included, and get an encoder good enough for my embedders, rerankers and RAG to start from?

Key features

How it works

  1. Tokenizer

    256 byte tokens so nothing is unknown, then BPE merges up to 16,384 tokens, learned on half WikiText-103, half FineWeb-Edu.

  2. Pre-training

    15% of tokens are masked and the encoder predicts them through an output layer tied to the token embeddings.

  3. Fine-tuning and publishing

    A new head on the pre-trained encoder is trained on SST-2 and QNLI; the finished checkpoints go to the family’s model store and a GitHub release.

Examples

The architecture: byte-level BPE, six encoder blocks, and two heads, one for masked-token pre-training and one for classification.
The architecture: byte-level BPE, six encoder blocks, and two heads, one for masked-token pre-training and one for classification.
The model’s real guesses for a hidden word, from a partial checkpoint (4,000 of 20,000 pre-training steps).
The model’s real guesses for a hidden word, from a partial checkpoint (4,000 of 20,000 pre-training steps).
The tokenizer on four sentences: rare words split into pieces, and accents, Japanese and emoji fall back to bytes.
The tokenizer on four sentences: rare words split into pieces, and accents, Japanese and emoji fall back to bytes.

Tech stack

What I learned

Limitations

Related projects