GPT-2-style-tinystories-causal-2026-10-26
A small GPT-2-style language model trained from scratch on TinyStories, with causal attention: standard softmax attention (Vaswani et al., 2017): softmax(QKᵀ/√d)·V.
Part of a comparison of attention mechanisms (causal, page cos, sigmoid, page gauss) trained under identical conditions: same data, tokenizer, architecture, hyperparameters, seed, initial weights and batches. All attention is written by hand (no FlashAttention).
Model
| parameters | 19.1 M |
| layers × heads | 6 × 6 (head dim 64) |
| d_model / d_ff | 384 / 768 (MLP: d_model → d_ff → d_ff·2 → d_model, GELU) |
| context | 256 tokens |
| positions | sinusoidal |
| normalisation | RMSNorm, pre-norm |
| tokenizer | byte-level BPE, 4096 tokens, trained on TinyStories; `< |
Training
| data | TinyStories train split (official) |
| steps | 20,000 × 64 × 256 tokens = 328 M tokens |
| optimizer | AdamW (β = 0.9, 0.95), lr 0.001, linear warmup 1000 steps then cosine to 0.0001, weight decay 0.1 on matrices, grad clip 1.0 |
| precision | bfloat16 autocast |
| seed | 0 |
| hardware | NVIDIA L4, PyTorch 2.11.0+cu130 |
| training time | 0.53 h (95 ms/step) |
Results
| final validation loss | 1.5024 |
| final validation perplexity | 4.49 |
| best validation loss | 1.5024 (step 20000) |
Validation loss is estimated on 50 random batches of the official validation split. The full curve is in history.json.
Usage
The architecture is defined in modeling.py (plain PyTorch, no transformers).
import json, sys, torch
from huggingface_hub import snapshot_download
from safetensors.torch import load_file
from tokenizers import Tokenizer
path = snapshot_download("analist/GPT-2-style-tinystories-causal-2026-10-26")
sys.path.insert(0, path)
from modeling import GPT
cfg = json.load(open(f"{path}/config.json"))
model = GPT(cfg["variant"], cfg["vocab_size"], cfg["d_model"], cfg["d_ff"], cfg["n_heads"], cfg["n_layers"],
max_len=cfg["block_size"], hid_factor=cfg["hid_factor"])
model.load_state_dict(load_file(f"{path}/model.safetensors"))
model.eval()
tok = Tokenizer.from_file(f"{path}/tokenizer.json")
torch.manual_seed(0)
ids = model.generate(torch.tensor([tok.encode("Once upon a time").ids]), max_new_tokens=200)[0].tolist()
if cfg["eot_id"] in ids:
ids = ids[:ids.index(cfg["eot_id"])]
print(tok.decode(ids))
Reference
Eldan, R. & Li, Y. (2023). TinyStories: How Small Can Language Models Be and Still Speak Coherent English? arXiv:2305.07759.