Tiny BERT
A Small Encoder and Its BPE Tokenizer, From Scratch
One encoder, two heads: a small BERT and its byte-level BPE, every layer written by hand.
- 9M params, 16,384-token BPE
- 42.3% masked tokens at 4k/20k steps
- 289M pre-training tokens
Built withPyTorch
Hugging Face Datasets
NumPy
scikit-learnMatplotlib
pytest
Code is in a private repository for now.

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
- Byte-level BPE: 16,384 tokens, cased, GPT-4 pre-split pattern, learned on 200M characters
- Modern blocks (RMSNorm, RoPE, SwiGLU, no biases) or the original BERT blocks with one flag
- Masked-token pre-training (15%, 80/10/10) with bf16 autocast and a metrics log
- Classification head on [CLS] or mean pooling, and an embed() for L2-normalized sentence vectors
- Benchmark against majority class and TF-IDF + logistic regression, and against the same model without pre-training
- Tests for tokenizer round-trips, masking, padding invariance and checkpoints; a 3-second sanity check
How it works
Tokenizer
256 byte tokens so nothing is unknown, then BPE merges up to 16,384 tokens, learned on half WikiText-103, half FineWeb-Edu.
Pre-training
15% of tokens are masked and the encoder predicts them through an output layer tied to the token embeddings.
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



Tech stack
The encoder, training loop and checkpoints, written by hand
Downloading WikiText-103, FineWeb-Edu and GLUE
Token streams as uint16 memory maps
TF-IDF + logistic regression baseline
Training and fine-tuning plots
Tokenizer, masking, padding and checkpoint tests
What I learned
- Writing the BPE trainer myself (a heap of pair counts) showed me how much of a model’s behaviour is decided before training, by the tokenizer.
- Keeping the original BERT blocks behind one flag makes the modern ones (RMSNorm, RoPE, SwiGLU) a real ablation, not a claim.
Limitations
- Full pre-training and the GLUE benchmark have not run yet, so there are no final accuracy numbers.
- A learning project at about 9M parameters, far from a production encoder.
Related projects

tiny-embedder — Text, Image and Audio Embeddings from Scratch
Code is in a private repository for now.
Three small embedding models built from scratch in PyTorch: text, images and sounds each become a 256-d vector, so similar things end up close. Every text tower starts from my tiny-bert.
Built with:
PyTorch
NumPy
Hugging Face Datasets
pytest
JavaScript

tiny-reranker — Text, Image and Audio Rerankers from Scratch
Code is in a private repository for now.
Three small cross-encoders that reorder the top results of a first search stage. One reads a query with a passage, one a caption with a picture, one a caption with a sound. All three are built from scratch in PyTorch on top of tiny-bert.
Built with:
PyTorch
NumPy
Hugging Face Datasets
pytest
JavaScript

Tiny NN Search — Vector and Keyword Search from Scratch
Code is in a private repository for now.
A small vector and keyword search engine I wrote from scratch in NumPy: exact search, IVF, PQ, IVF-PQ, HNSW, LSH, BM25 and hybrid fusion, with one engine each for text, images and audio. It is the retrieval engine of tiny-rag.
Built with:
NumPy
Gradio
PyTorchMatplotlib
pytest

tiny-rag — Ask Your Texts, Images and Sounds
Code is in a private repository for now.
A retrieval-augmented generation system over texts, images and sounds, built on my own tiny-* models. Each query is searched with its own kind of model, then Qwen2.5-0.5B-Instruct, run inside tiny-llm, answers from numbered sources.
Built with:
PyTorch
Gradio
NumPy
Hugging Face Datasets
pytest

Tiny LLM — A Small LLM Built From Scratch
A small LLM written layer by layer in PyTorch. The same code trains from zero or loads GPT-2, Llama (SmolLM2), Qwen2.5 and Pythia with outputs identical to Hugging Face, fine-tunes them with LoRA, and chats in a Streamlit app.
Built with:
PyTorch
Hugging Face TransformersLoRA
Streamlit
Kaggle

tiny-dlms — Tiny Decision Models That Score Instead of Generate
GitHub: tiny-dlmsLive demo: tiny-dlms
Two tiny decision models built from scratch in PyTorch. They don’t write: they score every option and pick one. Rhoa reads text and picks the answer or the intent; Mēlon reads pixels, drives a car in CarRacing and solves mazes.
Built with:
PyTorch
ONNX
JavaScriptGymnasiumMatplotlib