Warpweft: A Transformer from Scratch in Elixir
A decoder-only language model written in plain Nx, with nothing hidden behind a framework, built to learn how the architecture actually works
Warpweft is a decoder-only transformer built from scratch in Elixir, as an exercise in understanding the architecture rather than merely using it. Nx and EXLA provide the tensor arithmetic and the compilation; everything above that layer is written out by hand. The tokeniser is a byte-level BPE trained from scratch, the training loop is hand-rolled, and the architecture is parameterised so that variants can be measured against one another.
The governing constraint is that nothing hides behind a framework’s model graph. The parameters are a plain nested map and the forward pass can be read from top to bottom; even the training step is four visible lines (forward, gradient, clip, update). Each component has a module of its own: byte-level BPE, a data pipeline that moves the corpus to a single device tensor and samples batches on the device, fused causal multi-head attention, rotary positions, a schedule of linear warmup and cosine decay, AdamW with gradient clipping in one jitted step, and self-contained checkpoint directories from which a run can be resumed.
Training and sampling
On a CPU alone, the small preset trains a 3.5M-parameter model on Shakespeare in about fifteen minutes, while a larger 12.2M-parameter preset takes around eight hours on the TinyStories corpus. A REPL loads the newest run, warms the compiled sampler and streams continuations token by token.
warpweft> Once upon a time
Once upon a time, there was a little girl named Amy. Amy loved to play
with her toys and share them with her friends.
[120 tokens, 96 ms, seed 952621]
Generation uses a KV cache, so each new token costs O(context) rather than O(context²). One detail shows where the abstraction lives. On the cached path, temperature is a runtime argument and changing it is free, whereas changing top-k forces a recompile, because Nx.top_k needs k to determine the shape of its output when the program is traced.
Looking inside
The repository is as much documentation as code, and two Mix tasks exist only to show what is happening. mix wf.explain --prompt "The cat sat on the" traces one prompt through every stage of the model, with real tensor shapes and real probabilities at each step. mix wf.attention --prompt "First Citizen:" --heatmaps reports per-head statistics for the trained model’s attention and draws each head as a shaded grid in the terminal.
Four documents accompany the code: an architecture reference giving tensor shapes and the variant matrix, a glossary of concepts, a document on technique that covers parsing, array programming, compilation and measurement, and a lab notebook recording every training run along with the mistakes worth remembering. The techniques document, as the README notes, has mostly nothing to do with machine learning and transfers directly to ordinary software.