PythiloLesson 8 of 9 · progress kept in this browser

Practice in Lab: Project: train and reload a tiny causal language model →

Project: train and reload a tiny causal language model

This project trains a complete tiny decoder instead of inspecting fixed logits. Token and position embeddings feed a causal multihead block, and the output projection shares the token embedding weights. Training pairs each position with the next token and clips actual gradients before AdamW updates. An owned JSON-compatible artifact reloads for independent inference and a causal perturbation test. The synthetic cyclic vocabulary is deliberately small enough for CPU execution in Pythilo. Its training loss demonstrates optimization, not useful language understanding or the quality of a large language model.

import torch
ids = torch.tensor([[0, 1, 2, 0]])
print(ids[:, :-1])
print(ids[:, 1:])
This example uses torch. Run it in the Pythilo app, where these features are available.
Your task

Implement TinyDecoder(vocab) with token/32-position embeddings of width 8; pre-norm two-head attention (head width 4), bias-free qkv/output projections, residual; pre-norm 8→16→8 GELU feed-forward residual; final LayerNorm and output tied to token.weight. Train rectangular in-vocabulary sequences length 2..32 using shifted mean CE, AdamW lr=.03 weight_decay=.01, global gradient cap 1, positive steps and fork_rng seed. Return artifact (vocab/state lists), initial_loss, loss, perplexity and pre-update history. Also implement tiny_lm_logits(artifact,tokens) for detached eval. Trace uses four updates; tests train longer.

This lesson uses torch, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.