# Tiny causal-attention LM from scratch in NumPy: manual backprop on CPU

> Source: <https://discuss.huggingface.co/t/tiny-causal-attention-lm-from-scratch-in-numpy-manual-backprop-on-cpu/193206#post_1>
> Published: 2026-12-06 04:44:03+00:00

**Recommendation:** Build the smallest inspectable causal-attention LM in NumPy with manual backprop to learn the mechanics, but treat its in-sample loss as a teaching signal, not a benchmark, and add a validation split before drawing conclusions.
More in the playbook "Fine-tuning and data": <https://wpnews.pro/playbooks/training-data>

I put together a runnable, character-level next-token language model to make the mechanics of causal attention inspectable. It is built with NumPy and trained from random initialization, with no pretrained weights, API, GPU, or external dataset.

The notebook covers character tokenization, shifted next-token targets, a single-head causal attention mask, cross-entropy, manual gradients through attention and the embedding lookup, SGD, and autoregressive sampling. On Kaggle CPU, the complete run takes about 21 seconds.

The initial batch loss is 3.367 and the final batch loss after 150 steps is 3.177. That is an in-sample training result, not a generalization benchmark. The corpus is just 157 characters, generated text is rough, and this minimal model omits positional embeddings and feed-forward blocks. It is an educational attention LM, not ChatGPT or a practical chatbot.

Runnable notebook (Apache 2.0): [Tiny GPT-Style LM from Scratch (NumPy) | Kaggle](https://www.kaggle.com/code/jaspindersingh83/tiny-gpt-style-lm-from-scratch-numpy)

I would welcome feedback on the manual backward pass and on the best next teaching step: positional embeddings, a validation split, or a larger corpus.
