Tiny Story
A small decoder-only Transformer trained from scratch on TinyStories.
Training code lives in the project package tiny_story (model.py / CLI train). Weights on the Hub are model-only safetensors (no optimizer).
Current Hub revision: more-context-256-long — compact TinyStories vocab + longer context.
Model details
| Architecture | Decoder-only Transformer (custom PyTorch) |
| Parameters | ~7.1M with weight tying (emb=192, heads=3, blocks=3) |
| Context length | 256 |
| Vocab size | 29312 (TinyStories train-unique GPT-2 ids + UNK, padded to ×64) |
| MLP | SwiGLU (mlp_type: swiglu) |
| Tokenizer | GPT-2 BPE (tiktoken, gpt2) + vocab_compact.json remap |
| Training steps | see step in config.json |
| Best val loss | see best_val_loss in config.json |
Token ids are not raw GPT-2 ids. Encode with tiktoken, map through vocab_compact.json, then decode the reverse way (see example below). Padding rows above the live UNK id are unused.
Files
model.safetensors— model weights (state_dictonly)config.json— architecture hyperparameters (+ training step / val loss)vocab_compact.json— compact ↔ GPT-2 id map (itos,unk_id,vocab_size)
Load and generate
import json
import torch
import tiktoken
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
# Copy Transformer + helpers from the training repo:
# src/tiny_story/model.py
# src/tiny_story/data.py (load_vocab_map, encode_to_compact, decode_from_compact)
from tiny_story.model import Transformer
from tiny_story.data import decode_from_compact, encode_to_compact, load_vocab_map
repo = "wuttichai3/tiny-story"
config = json.load(open(hf_hub_download(repo, "config.json")))
weights = load_file(hf_hub_download(repo, "model.safetensors"))
vocab = load_vocab_map(hf_hub_download(repo, "vocab_compact.json"))
model = Transformer(
n_head=config["n_heads"],
n_embed=config["emb_dim"],
context_length=config["context_length"],
vocab_size=config["vocab_size"],
n_blocks=config["n_blocks"],
dropout=config["dropout"],
mlp_type=config.get("mlp_type", "swiglu"),
)
model.load_state_dict(weights)
model.eval()
prompt = "Once upon a time"
idx = torch.tensor(
[encode_to_compact(prompt, vocab, encoding_name=config.get("tokenizer", "gpt2"))],
dtype=torch.long,
)
out = model.generate(idx, max_new_tokens=80, temperature=0.75, top_k=40)
print(decode_from_compact(out[0].tolist(), vocab, encoding_name=config.get("tokenizer", "gpt2")))
Without the training package, map manually with tiktoken + vocab["itos"] / stoi built from itos (OOV → unk_id).
Hidden states for analysis:
logits, loss, hiddens = model(idx, return_hidden=True)
# hiddens: embedding, each block output, final LayerNorm
Intended use
Educational / demo story generation. Not intended for production or factual content.