Tolk

A language model built from scratch in Go, with the standard library only: no PyTorch, no BLAS, no cgo, no Python.

Tolk is built to understand how language models work, so every piece is written here: the tensors and the matrix product, a tokenizer, a transformer with a hand-written backward pass for each layer, training on the CPU and on the GPU through Metal, and fine-tuning into a chat model. Each layer's forward pass and gradient are derived on paper before they are written, and every gradient is checked against finite differences.

No outside library checks the numbers. GPT-2's own weights, loaded into Tolk's code, give the greedy text of Hugging Face's example and the score that lm-eval gives GPT-2 on LAMBADA.

The training studio: a GPT's recipe on the left; its run and its losses charted on the right.
The training studio, a local page served by Go: a GPT of 208,320 parameters trained on Shakespeare's plays character by character, on the GPU, in 38 seconds.

Run it

It needs Go 1.27. The GPU path needs macOS on Apple silicon; the rest runs anywhere Go does.

git clone https://github.com/szwalczak/tolk
cd tolk
go run ./cmd/fetch    # the data and GPT-2's weights, 2.7 GB
go test -short ./...  # the tests, without the experiments
go run ./cmd/studio   # the training studio, in the browser
go run ./cmd/sample -gpt2 -temp 0 -n 40 \
  -prompt 'I enjoy walking with my cute dog'  # GPT-2 in our code

How it is made

Claude, Anthropic's model, writes the code and derives the math behind it, in Claude Code. Szymon Walczak designs the plan, reviews each step and verifies its results. Each step ends with something that runs: a test, a benchmark or a falling loss.

Where it stands

The plan and each phase's status are in the roadmap.

The book

The cover of LLMs in Go. From scratch, by Szymon Walczak: a knotted rope on blue.

LLMs in Go. From scratch follows Tolk step by step: the math derived by hand, the Go code that mirrors it, and an experiment you can run. It is published on Leanpub chapter by chapter, while it is being written.

The book on Leanpub

Support

Tolk is free and open, under the MIT license. Sponsorship pays for what it costs beyond time: Claude, which writes the code; Apple's developer program, to sign the desktop app; the domain; and later, GPU hours for a larger model.

Sponsor

The name

Tolk means "interpreter" in Dutch, Swedish, Danish and Norwegian, from a Slavic root (tъlkъ, "interpretation").