Build your own GPT in Jennifer

Jennifer

From a scalar autodiff engine to a transformer that invents names: microGPT, written from scratch, no libraries.

November 2026, by @mplx.
Based on Jennifer 0.27.0

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 19

What we build

1328 parameters, a 27-character vocabulary, trained on 32,033 names.

2 / 19

Two repositories, read in order

demo-micrograd builds the engine in eight editions; demo-microgpt uses it

3 / 19

Every number is a node

The neuron tanh(x * w + b) as a graph of six nodes

out = tanh(x * w + b): three leaves, three operations, six nodes.

4 / 19

No closures, no shared objects

def enum Op { Leaf, Add, Sub, Mul, AddC, MulC, Tanh, Pow, Log, Exp, Relu };
def struct Node { data as float, grad as float, op as Op, a as int, b as int, c as float };
def tape as list of Node init [];

A node names its operands by index into one global tape.

5 / 19

The tape is already sorted

$tape[$out].grad = 1.0;
def i as int init len($tape) - 1;
while ($i >= 0) {
    def n as Node init $tape[$i];
    # push n.grad to n.a and n.b by the chain rule
    $i = $i - 1;
}
6 / 19

One match, one rule per operation

match ($n.op) {
    when Mul {
        accum($n.a, $tape[$n.b].data * $n.grad);
        accum($n.b, $tape[$n.a].data * $n.grad);
    }
    when Tanh {
        # d/dx tanh(x) = 1 - tanh(x)^2, and n.data == tanh(operand)
        accum($n.a, (1.0 - $n.data * $n.data) * $n.grad);
    }
    # ... one branch per Op
}
7 / 19

Parameters lead the tape

The parameters #0 to #1327 stay; each step's graph after them is dropped

resetTo(numParams) drops last step's graph and keeps the weights.

8 / 19

Measure before you optimize

9 / 19

Forward mode: the other half

10 / 19

From a neuron to a GPT

The model: embed, attention, MLP, predict

Each block also starts with an RMSNorm.

11 / 19

Attention, one head

def t as int init 0;
while ($t < $steps) {                      # every earlier position
    def keyVec as list of int init $keysCache[$li][$t];
    def dotacc as int init mul($q[$hs], $keyVec[$hs]);
    # ... the rest of the dot product q . k
    $attnLogits[] = mulc($dotacc, $invSqrt);
    $t = $t + 1;
}
def weights as list of int init softmaxNodes($attnLogits);

From attentionHead in model.inc.j; then the weighted sum of the values.

12 / 19

The loss: guess the next letter

13 / 19

Training on every core

Each step spawns one worker per name; their gradients are averaged

14 / 19

What parallelism really bought

15 / 19

Train once, sample often

train.j writes a checkpoint; sample.j rebuilds the model from it

A checkpoint is the shape, the vocabulary and the 1328 floats, read off the front of the tape.

16 / 19

Run it

cd code
jennifer run neuron.j                                # the tape and its gradients
jennifer run sample.j --load 32k1000.json --samples 5
jennifer run train.j --steps 50 --batch 8 --save model.json
sample 1: dareen
sample 2: elina
sample 3: arin
17 / 19

What it is, and what it is not

18 / 19

Take it home

19 / 19