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.
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
- Phases 0–12, done: tensors and a scalar autograd; a bigram model, an MLP and a GPT, with BPE; training on TinyStories, in English and Polish; sampling with a KV cache; GPT-2's weights; the GPU through Metal; RoPE, RMSNorm, SwiGLU and small scaling laws; GPT-2 fine-tuned into a chat model, and LoRA.
- Phase 12b, next: the GPU's work on NVIDIA cards under Windows, still without cgo.
- Phases 13–15: a conversation with the chat model, in the studio and in a panel of macOS's menu bar, released as an app.
- Phases 16–20: Little Tolk, a small Polish and English model trained from its own data up.
- Phase 21: a LaTeX editor in which Tolk completes and translates.
The plan and each phase's status are in the roadmap.
The book
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.
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.
The name
Tolk means "interpreter" in Dutch, Swedish, Danish and Norwegian, from a Slavic root (tъlkъ, "interpretation").
