Build a tiny language model from first principles: tokenizer → embeddings → causal attention → transformer block → training → generation.
MiniLLM is an educational, NumPy-only project inspired by TinyTorch. It deliberately avoids PyTorch, TensorFlow, JAX, and automatic differentiation so every important operation is visible and testable.
text
↓
character tokenizer
↓
token + position embeddings
↓
causal multi-head self-attention
↓
residual + LayerNorm + MLP
↓
next-token logits
↓
cross-entropy + manual backpropagation
↓
Adam updates
The model is small enough to train on a laptop and is intended to learn short patterns from a tiny text corpus. It is not a useful general-purpose LLM.
- Python 3.10+
- NumPy
Create the project virtual environment once:
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txtWith the environment activated, run the checks and example:
python -m pytest -q
python -m examples.train_tiny --steps 400Generate text after training:
python -m examples.generate --checkpoint artifacts/minillm.npz --prompt "the "On the next terminal session, activate the existing environment with
source .venv/bin/activate before running project commands. The .gitignore
keeps .venv/, caches, logs, and generated checkpoints out of version control.
- Read
minillm/tokenizer.py: text becomes integer token IDs. - Read
minillm/nn.py: inspect softmax, GELU, LayerNorm, and Adam. - Read
minillm/model.py: follow the forward pass and its explicit backward pass. - Run
python -m pytest -q: the tests check shapes, masking, gradients, and learning. - Change the corpus, context length, or model size and observe the trade-offs.
For a complete explanation of the architecture, tensor shapes, equations, training loop, generation, checkpoints, tests, and extensions, see the MiniLLM guide.
The implementation favors small classes, descriptive names, type hints, and docstrings on every function and method. ForwardCache gives names to intermediate tensors used by backpropagation, while Trainer owns the optimizer and batch generator. Read the docstrings first; they describe the contract of each public operation before the implementation details.
minillm/
tokenizer.py character-level vocabulary and encode/decode
nn.py NumPy primitives and optimizer
model.py causal transformer and manual gradients
data.py next-token training batches
train.py reproducible training loop and checkpoints
generate.py temperature/top-k inference
examples/
train_tiny.py
generate.py
tests/
- Character tokens make tokenization transparent and require no downloaded vocabulary.
- A single transformer block keeps the equations approachable.
- Manual gradients make the training mechanics explicit.
tests/test_gradients.pycompares selected derivatives against finite differences. - NumPy arrays use batch-first shapes:
(batch, time, features).
The structure follows the same educational idea as TinyTorch: build a narrow vertical slice, test each layer, then connect the pieces into a working system. TinyTorch’s broader curriculum covers a full tensor/autograd framework and many more architectures; MiniLLM focuses on the smallest complete language-model path.
MIT. The reference TinyTorch material is linked for inspiration; this implementation is original and independent.