Skip to content

12.1 GPT: the decoder-only language model

The encoder-decoder Transformer of the previous chapter maps one sequence to another. The system this book builds serves something simpler and more useful: a language model, a model that continues text. In a language model every token of a corpus is a training target for the prefix before it, and nothing needs to be encoded separately. GPT keeps exactly the half of the encoder-decoder that does this job: the decoder, with its cross-attention removed, because there is no source sequence to attend to.

Concepts from other sections (21)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

This section builds that model and follows GPT-2’s exact layout, so that a checkpoint trained with Hugging Face loads into it and produces the same logits. The loader is the bridge the rest of the book walks over: the Llama-family model loads SmolLM2 the same way, and the KV cache and generate is first built on this model.

You will need the pieces of Part 5: causal masks, multi-head attention, and learned positional encodings; the foundations of Part 0 (the Tensor, the op library, losses, layers, the training loop, and safetensors and the token stream); and from Mathematics, AdamW, cosine schedules, variance propagation and initialization, and PCG32 for seeded initialization. The reference implementation is python/tinyllm/obj/gpt.py (GPTConfig, Block, GPT with hidden and forward, clm_loss, load_hf_gpt2, fit_gpt, save_gpt, load_gpt), and its contract is course/contracts/py/tinyllm/obj/gpt.pyi.

Learning objectives

  • Explain why one causal self-attention stack trains on every position of a window in a single forward pass.
  • Compute the causal LM loss by hand, including the shift by one and padded targets.
  • Describe GPT-2’s block exactly: pre-LN, tanh GELU, final ln_f, tied head, and depth-scaled residual init.
  • Load a Hugging Face GPT-2 state dict, transposing every Conv1D weight and splitting c_attn into q, k, v.
  • Explain why only a value comparison, not a shape check, catches a missing transpose.

Here is the notation used throughout the section.

SymbolMeaningType / shape
t0…tT−1t_0 \dots t_{T-1}the token ids of one windowint[T]
V,d,L,HV, d, L, Hvocabulary, width, layers, headsint
nctxn_{ctx}the longest window (rows of wpe)int
Wte∈RV×dW_{te} \in \mathbb{R}^{V \times d}token table wtefloat32[V, d]
Wpe∈Rnctx×dW_{pe} \in \mathbb{R}^{n_{ctx} \times d}learned positions wpefloat32[n_ctx, d]
ht(ℓ)h^{(\ell)}_tthe residual stream at layer ℓ\ell, position tt[B, T, d]
zt∈RVz_t \in \mathbb{R}^Vlogits at position tt[B, T, V]
GELUtanh⁡(u)\text{GELU}_{\tanh}(u)12u(1+tanh⁡(2/π(u+0.044715u3)))\tfrac12 u (1 + \tanh(\sqrt{2/\pi}(u + 0.044715 u^3)))elementwise

A language model factors the probability of a window by the chain rule:

P(t0…tT−1)=∏t=1T−1P(tt∣t0…tt−1).P(t_0 \dots t_{T-1}) = \prod_{t=1}^{T-1} P(t_t \mid t_0 \dots t_{t-1}) .

Every factor conditions only on a prefix. With the causal mask, position tt attends to positions 0…t0 \dots t only, so its output is a function of the prefix and can be trained to predict tt+1t_{t+1}. One forward pass therefore computes all TT predictions at once. Without the mask, position tt could read tt+1t_{t+1} directly, and the loss would teach the model to copy rather than to predict.

The mask fixes what each position may see. The loss then pairs each position with the token after it:

L=1∣K∣∑t∈K−log⁡softmax⁡(zt)tt+1,K={t<T−1:tt+1 is a real token}.\mathcal{L} = \frac{1}{|K|} \sum_{t \in K} -\log \operatorname{softmax}(z_t)_{t_{t+1}}, \qquad K = \{ t < T - 1 : t_{t+1} \text{ is a real token} \}.

In code this is logits [:, :-1] against ids [:, 1:], with -100 wherever the target is padding, which cross_entropy ignores. The important discipline is that the shift happens once. Either clm_loss shifts a full window, or the data already arrives as (inputs, targets) pairs offset by one, as the TokenStream does, and forward(x, y) uses them as they are. Shifting in both places would train the model to predict two tokens ahead.

With the objective settled, the architecture follows. Each GPT-2 block is pre-LN (the norm="pre" variant of the encoder-decoder chapter):

h←h+Attn(LN1(h)),h←h+Wproj GELUtanh⁡(Wfc LN2(h)),h \leftarrow h + \text{Attn}(\text{LN}_1(h)), \qquad h \leftarrow h + W_{proj}\,\text{GELU}_{\tanh}(W_{fc}\,\text{LN}_2(h)),

followed by a final LNf\text{LN}_f after the last block, and z=LNf(h)Wte⊤z = \text{LN}_f(h) W_{te}^\top: the output layer is the token table itself (a tied head). GPT-2 used the tanh approximation of GELU. The exact erf form differs from it by up to about 10−310^{-3} at ∣u∣≈2|u| \approx 2, which is enough to fail a golden comparison against Hugging Face.

Initialization follows GPT-2 too. Tables and every Linear weight are drawn from N(0,0.022)\mathcal{N}(0, 0.02^2), biases are 0, and LayerNorms start at (1,0)(1, 0). One refinement matters at depth. Each block adds two outputs to the residual stream (attn.out_proj and mlp_proj), so after LL blocks the stream holds 2L2L such terms. Their weights start at 0.02/2L0.02/\sqrt{2L} (the scaled_residual_std of variance propagation and initialization), which keeps the stream’s variance from growing with depth.

Matching the computation is only half of loading a real checkpoint; the other half is matching the names and layouts. Hugging Face’s GPT2LMHeadModel uses the following keys and shapes.

HF key (after transformer.)ShapeIn the solution
wte.weight, wpe.weight[V, d], [n_ctx, d]same names
h.i.ln_1.*, h.i.ln_2.*, ln_f.*[d]same names
h.i.attn.c_attn.weight, .bias[d, 3d], [3d]attn.q_proj, k_proj, v_proj: columns 0..d0..d, d..2dd..2d, 2d..3d2d..3d, each transposed
h.i.attn.c_proj.weight[d, d]attn.out_proj.weight transposed
h.i.mlp.c_fc.weight[d, 4d]mlp_fc.weight transposed
h.i.mlp.c_proj.weight[4d, d]mlp_proj.weight transposed

The transposes come from a difference in conventions. Conv1D computes xW+bx W + b with WW stored [in, out]; Linear computes xW⊤+bx W^\top + b with WW stored [out, in]. The square c_proj of attention loads either way without a shape error, which is why only a comparison of values, not shapes, catches a missing transpose. Old checkpoints also carry the buffers attn.bias and attn.masked_bias, a stored causal mask; they are not weights, and the loader ignores them. Finally, lm_head.weight is the same tensor as wte.weight.

Before writing code, it helps to pin down the shift with numbers everyone can agree on. Take V=2V = 2, ids (0,1,1)(0, 1, 1), and logit rows z0=(0,ln⁡3)z_0 = (0, \ln 3), z1=(0,0)z_1 = (0, 0), z2=(5,−5)z_2 = (5, -5).

  • Position 0 predicts t1=1t_1 = 1: softmax⁡(0,ln⁡3)=(1/4,3/4)\operatorname{softmax}(0, \ln 3) = (1/4, 3/4), loss −ln⁡0.75=0.287682-\ln 0.75 = 0.287682.
  • Position 1 predicts t2=1t_2 = 1: (1/2,1/2)(1/2, 1/2), loss ln⁡2=0.693147\ln 2 = 0.693147.
  • Position 2 has no next token, so it is dropped.

The loss is L=(0.287682+0.693147)/2=0.490415\mathcal{L} = (0.287682 + 0.693147)/2 = 0.490415. Now mark the third token as padding. Position 1’s target is padding, so only position 0 counts and L=0.287682\mathcal{L} = 0.287682. The gradient on z0z_0 is softmax⁡(z0)−onehot(1)=(0.25,−0.25)\operatorname{softmax}(z_0) - \text{onehot}(1) = (0.25, -0.25), and it is zero on the other rows.

The public surface is small: a config, the model with a shared trunk and a forward pass, a standalone loss, the loader, a training loop, and save/load.

@dataclass
class GPTConfig: vocab: int; n_ctx: int; d_model: int; n_heads: int; n_layers: int; d_ff: int
dropout: float = 0.0; ln_eps: float = 1e-5; tie: bool = True
class GPT(Module):
def hidden(self, ids) -> Tensor # ln_f output [B, T, d]
def forward(self, ids, targets=None) -> tuple[Tensor, Optional[Tensor]]
def clm_loss(logits: Tensor, ids, mask=None) -> Tensor
def load_hf_gpt2(model: GPT, sd: Mapping[str, Any]) -> None
def fit_gpt(model, stream, steps, lr, warmup=0, weight_decay=0.1, clip=1.0) -> list[float]
def save_gpt(model, dir, tokenizer="bytes") / load_gpt(dir)

hidden returns the output of ln_f, the trunk that the fine-tuning heads reuse. forward takes targets that are already shifted. clm_loss takes a full window and does the shift itself.

The reasoning runs in a short chain.

  1. A language model is ∏tP(tt∣t<t)\prod_t P(t_t \mid t_{<t}). Every factor conditions only on a prefix, so a single causal self-attention stack can compute every factor in one pass: position tt‘s output predicts tt+1t_{t+1}. That is the encoder-decoder’s decoder with cross-attention deleted, because there is no source.
  2. The loss must pair logits at tt with ids at t+1t + 1. The shift is done in exactly one place: clm_loss shifts a full window, and forward(ids, targets) assumes the caller already shifted, as TokenStream does. Doing it in both would predict two tokens ahead.
  3. To load real GPT-2 weights, the module must match GPT-2’s computation exactly: pre-LN, tanh GELU, learned positions, final ln_f, tied head. Each of these is a numeric difference a golden comparison detects.
  4. Naming follows Hugging Face where possible (wte, wpe, h.i.ln_1, ln_f), so the loader is mostly a rename. Where the layouts differ (Conv1D [in, out] against Linear [out, in], and the fused c_attn), the loader transposes and splits.
  5. Depth makes the residual stream a sum of 2L2L terms. With independent terms the variances add, so each writing projection starts at std 0.02/2L0.02/\sqrt{2L} to keep the total near the single-term scale.
  6. The tied head is a matmul against the transpose of the same wte tensor, so the gradient from the output side lands on the same parameter as the gradient from the input side.

The block is pre-LN with both residuals and the GPT-2 init:

class Block(Module):
def __init__(self, cfg: GPTConfig, rng: Any) -> None:
super().__init__()
d = cfg.d_model
self.ln_1 = LayerNorm(d, cfg.ln_eps)
self.attn = MultiHeadAttention(d, cfg.n_heads, cfg.dropout, rng=rng)
self.ln_2 = LayerNorm(d, cfg.ln_eps)
self.mlp_fc = Linear(d, cfg.d_ff, rng=rng)
self.mlp_proj = Linear(cfg.d_ff, d, rng=rng)
self.drop = Dropout(cfg.dropout)
resid = scaled_residual_std(INIT_STD, cfg.n_layers) # 0.02 / sqrt(2 L)
for lin in (self.attn.q_proj, self.attn.k_proj, self.attn.v_proj, self.mlp_fc):
_init_linear(lin, INIT_STD, rng)
# The two projections that write into the residual stream.
for lin in (self.attn.out_proj, self.mlp_proj):
_init_linear(lin, resid, rng)
def forward(self, x: Tensor, mask: NDArray) -> Tensor:
h = self.ln_1(x)
x = x + self.drop(self.attn(h, h, mask))
# GPT-2's GELU is the tanh approximation, not the erf form.
m = self.mlp_proj(F.gelu(self.mlp_fc(self.ln_2(x)), approximate="tanh"))
return x + self.drop(m)

hidden is the shared trunk the heads reuse; forward adds the tied output layer:

def hidden(self, ids: ArrayLike) -> Tensor:
ids = np.asarray(ids)
# ... int [B, T] check ...
T = ids.shape[1]
if T > self.cfg.n_ctx: # wpe has no row past n_ctx
raise ValueError(f"T = {T} exceeds the context n_ctx = {self.cfg.n_ctx}")
x = self.drop(self.wpe(self.wte(ids))) # token rows + position rows
mask = causal_mask(T) # position t reads tokens 0..t
for block in self.h:
x = block(x, mask)
return self.ln_f(x)
def forward(self, ids, targets=None):
x = self.hidden(ids)
if self.cfg.tie:
# The output layer IS the embedding table: logits = x E^T.
logits = F.matmul(x, F.transpose(self.wte.weight, 0, 1))
else:
logits = self.lm_head(x)
# targets are already shifted by the caller: no shift here.
loss = None if targets is None else cross_entropy(logits, np.asarray(targets).astype(np.int64))
return logits, loss

With tie=True there is no lm_head attribute at all, so the state_dict has no duplicate key and the checkpoint shrinks by VdVd.

def clm_loss(logits, ids, mask=None):
ids = np.asarray(ids).astype(np.int64)
# ... logits [B, T, V] must match ids [B, T] ...
if ids.shape[1] < 2:
raise ValueError("the causal LM loss needs T >= 2 (position t predicts token t + 1)")
# Position t predicts token t + 1: drop the last logit row and the first id.
targets = ids[:, 1:].copy()
if mask is not None:
m = np.asarray(mask, dtype=bool)
# ...
# The mask is applied to TARGETS (m[:, 1:]), not inputs.
targets[~m[:, 1:]] = -100 # a padded target is not a prediction
return cross_entropy(logits[:, :-1, :], targets, ignore_index=-100)

In the worked example, ids (0,1,1)(0, 1, 1) give targets (1,1)(1, 1) for logit rows 0 and 1. With the third token padded, targets becomes (1,−100)(1, -100) and only row 0 counts, giving 0.287682.

def _hf_key(sd, name):
# Accept both GPT2LMHeadModel ("transformer.h.0...") and bare GPT2Model ("h.0...").
for k in ("transformer." + name, name):
if k in sd:
return np.asarray(sd[k], dtype=np.float32)
raise KeyError(f"GPT-2 state dict has no {name!r} (with or without 'transformer.')")
def load_hf_gpt2(model, sd):
c = model.cfg
d = c.d_model
out = {
"wte.weight": _hf_key(sd, "wte.weight"),
"wpe.weight": _hf_key(sd, "wpe.weight"),
"ln_f.weight": _hf_key(sd, "ln_f.weight"),
"ln_f.bias": _hf_key(sd, "ln_f.bias"),
}
for i in range(c.n_layers):
p = f"h.{i}."
# HF's Conv1D stores weight as [in, out] (x @ W): transpose to Linear's [out, in].
w_attn = _hf_key(sd, p + "attn.c_attn.weight")
b_attn = _hf_key(sd, p + "attn.c_attn.bias")
# ... shape check: [d, 3d] ...
# The 3 d output columns are q | k | v, each d wide.
for j, n in enumerate(("q_proj", "k_proj", "v_proj")):
out[f"{p}attn.{n}.weight"] = w_attn[:, j * d : (j + 1) * d].T
out[f"{p}attn.{n}.bias"] = b_attn[j * d : (j + 1) * d]
out[p + "attn.out_proj.weight"] = _hf_key(sd, p + "attn.c_proj.weight").T
out[p + "attn.out_proj.bias"] = _hf_key(sd, p + "attn.c_proj.bias")
out[p + "mlp_fc.weight"] = _hf_key(sd, p + "mlp.c_fc.weight").T
out[p + "mlp_fc.bias"] = _hf_key(sd, p + "mlp.c_fc.bias")
out[p + "mlp_proj.weight"] = _hf_key(sd, p + "mlp.c_proj.weight").T
out[p + "mlp_proj.bias"] = _hf_key(sd, p + "mlp.c_proj.bias")
for ln in ("ln_1", "ln_2"):
out[f"{p}{ln}.weight"] = _hf_key(sd, f"{p}{ln}.weight")
out[f"{p}{ln}.bias"] = _hf_key(sd, f"{p}{ln}.bias")
if not c.tie:
out["lm_head.weight"] = np.asarray(sd["lm_head.weight"], dtype=np.float32)
# Strict load: every one of our keys must be filled, with the right shape.
model.load_state_dict(out)

The loader builds a dict of exactly the keys the model has and reads only what it needs. The old buffers attn.bias and attn.masked_bias are never touched, and lm_head.weight (the same tensor as wte in a tied Hugging Face model) is ignored. A missing key raises with its name; a model built with the wrong d fails the c_attn shape check or the shape check in load_state_dict.

fit_gpt is AdamW with GPT-style betas, a cosine schedule down to 0.1⋅0.1 \cdot lr, and gradient clipping:

opt = AdamW(list(model.parameters()), lr=lr, betas=(0.9, 0.95), weight_decay=weight_decay)
model.train()
def loss_fn(m, b):
return m(b["x"], b["y"])[1] # TokenStream already shifted y by one
for step in range(steps):
x, y = stream.next_batch()
opt.lr = cosine_with_warmup(step, warmup, steps, lr, 0.1 * lr)
losses.append(train_step(model, {"x": x, "y": y}, loss_fn, opt, clip)["loss"])

save_gpt writes Hugging Face-style config names (n_embd, n_layer, n_positions) plus tl_arch = "gpt" and the tokenizer name; load_gpt reverses it. The full solution is in gpt.py.

A forward pass costs O(L(T2d+Td dff)+TVd)O(L(T^2 d + T d\, d_{ff}) + TVd), and for small models with byte or BPE vocabularies the tied head is the single largest matmul. The parameter count is Vd+nctxd+L(4d2+2d dff)Vd + n_{ctx} d + L(4d^2 + 2 d\, d_{ff}) plus biases and norms, with no second VdVd for the head.

The solution keeps separate q, k, v projections rather than one fused [3d, d] Linear. That way the attention module from Part 5 is reused unchanged, and LoRA can target each projection by name. The price is the split in the loader. Putting the shift in one place, only in clm_loss and never in forward, keeps TokenStream’s pre-shifted batches and full-window evaluation consistent. Strict loading is preferred over “load what matches”, because a silently missing tensor would leave random weights and produce plausible but wrong text. The one tradeoff accepted is that learned positions cap the context at n_ctx; Part 7 replaces them with RoPE.

A correct solution has to survive the following checks.

CaseKind of checkChecksWhy it matters downstream
the hand-worked lossunit0.490415, the padded 0.287682, the gradienteveryone agrees on the shift
agreement with Hugging Face GPT-2goldena random tiny GPT2LMHeadModel: logits, loss, and the gradient of every HF tensor through the loaderreal GPT-2 checkpoints load (the Llama-family loader does the same for SmolLM2)
the key variants HF shipsboundarybare GPT2Model keys and old buffers load; a missing key is named; a wrong config is a shape errorcheckpoints from many sources
logits are causalpropertychanging tokens after tt leaves logits 0..t0..t bitwise equaltraining on all positions is honest
one block against the formuladifferentialone block in float64 numpy with large weightstanh GELU, pre-LN, both residuals
forward with targets equals clm_losspropertyforward(x[:, :-1], x[:, 1:]) equals clm_loss(x)the shift happens once
the head is tiedunitno lm_head; logits =hWte⊤= h W_{te}^\top; tie=False adds onehalf the output parameters
GPT-2 initstatisticalresidual projections at 0.02/2L0.02/\sqrt{2L}, others 0.02, seededtraining starts stable at depth
save and loadunitthe model directory loads back exactlythe model zoo
bad argumentsboundarywindows past n_ctx, one-token loss, float idserrors, not silent garbage
it learnslearning150 AdamW updates of a 2-layer byte GPT reach a reference held-out lossthe model learns language

The model tests are in test_gpt.py. Their key idea is worth borrowing: build a GPT-2 state dict in Hugging Face’s layout by hand, write the Hugging Face forward pass in float64 numpy from that dict, and compare it with the loaded model. The oracle never sees the loader’s transposes, so it cannot share their bugs.

Most bugs in a GPT are silent: the model still trains, or still loads, and only its numbers are wrong. The most common ones, and how the solution avoids each, are these.

PitfallSymptomHow the solution avoids it
a Conv1D weight loaded without the transposec_proj is square: no error, wrong logitsevery Conv1D weight is loaded with .T
c_attn split into interleaved columns instead of three blocksq, k, v mixedcontiguous slices [:, j*d:(j+1)*d] for j=0,1,2j = 0, 1, 2
no causal maskloss near zero in training, nonsense when generatinghidden always passes causal_mask(T) to every block
no shift (each position predicts its own token), or the padding mask applied to inputsthe model learns to copy; padded targets countedclm_loss pairs logits[:, :-1] with ids[:, 1:] and masks with m[:, 1:]
exact GELUoff by about 10−310^{-3}: HF parity failsF.gelu(..., approximate="tanh")
ln_f forgottenlogits too large; HF disagreeshidden returns self.ln_f(x)
residual projections at the plain 0.02the stream’s variance grows with depthout_proj and mlp_proj use scaled_residual_std(0.02, L)
position embeddings never addedthe model is a bag of tokensself.wpe(self.wte(ids)) adds the position rows
no gradient through the tied headwte learns only from the input sidethe head is a matmul on self.wte.weight itself, not a copy of its data

Warning: The missing transpose on attention’s c_proj is the classic GPT-2 loading bug. The matrix is square, so nothing complains; the model simply produces fluent-looking nonsense. Only a value-level comparison with a reference catches it.

The rest of this chapter builds on GPT.hidden. The fine-tuning heads put sequence, token, and reward heads on it; LoRA with PiSSA attaches adapters to its attention projections; and the evaluation harness and model zoo reports its gpt rows in bits per byte. Later, the KV cache and generate adds the first incremental decoder to this model.

The production equivalents add a good deal around the same core. nanoGPT and Hugging Face’s GPT2LMHeadModel add flash attention, weight-decay groups, and torch.compile. Hugging Face’s from_pretrained adds sharded safetensors, dtype casting, and key-renaming hooks. Llama-style models replace learned wpe with RoPE, which removes the length cap of the table (see RoPE). And training systems such as llm.c and Megatron add mixed precision, gradient accumulation, and data parallelism, which mixed precision and accumulation covers.

Further reading:

  • Radford et al., “Language Models are Unsupervised Multitask Learners” (GPT-2, 2019), section 2.3.
  • Karpathy, nanoGPT, model.py.
  • transformers/models/gpt2/modeling_gpt2.py and transformers/modeling_utils.py.
  • karpathy/llm.c.
  • Decoder-only model: a single causal self-attention stack in which every position predicts the next token.
  • Causal LM loss: mean cross-entropy of logits at position tt against the token at t+1t + 1, over real targets.
  • Tied head: an output layer that reuses the token embedding table, z=hWte⊤z = h W_{te}^\top.
  • Conv1D: Hugging Face’s GPT-2 projection, storing WW as [in, out] and computing xW+bxW + b.
  • Scaled residual init: starting the residual-writing projections at 0.02/2L0.02/\sqrt{2L} so the stream’s variance does not grow with depth.
  • A GPT is the Transformer decoder without cross-attention: one causal stack, so one forward pass trains on every position.
  • The causal LM loss shifts by one, exactly once; the last position has no target and padded targets count for nothing.
  • GPT-2’s block is pre-LN with a tanh-approximated GELU and a final ln_f, and the output layer is the token table.
  • Hugging Face stores GPT-2 projections as Conv1D with weight [in, out]: every weight is transposed on load, and c_attn is cut into three column blocks q, k, v.
  • The two projections that write into the residual stream start at 0.02/2L0.02/\sqrt{2L}.
  1. Why does a missing transpose on attn.c_proj give no error, while a missing transpose on mlp.c_fc does?
    Answer`c_proj` is $d \times d$, so its transpose has the same shape and `load_state_dict` accepts it; the logits are simply wrong. `c_fc` is $[d, 4d]$ against `mlp_fc.weight` of $[4d, d]$, so the shape check fails.
  2. Why are the residual-writing projections initialized at 0.02/2L0.02/\sqrt{2L} and not 0.02/L0.02/\sqrt L?
    AnswerEach block adds two terms to the stream (attention output and MLP output), so $L$ blocks add $2L$ terms. Scaling each by $1/\sqrt{2L}$ keeps the variance of their sum at about $0.02^2$ times a constant, independent of depth.
  3. forward(ids, targets) calls cross_entropy without shifting, while clm_loss shifts. Why is that not a contradiction?
    AnswerThey take different inputs. `forward` receives (inputs, targets) already offset by one (as `TokenStream` produces), so `forward(x[:, :-1], x[:, 1:])` equals `clm_loss` on the full window `x`. Shifting in both would predict two tokens ahead.
  4. In clm_loss, why is the mask sliced as m[:, 1:]?
    AnswerRow $t$ predicts token $t + 1$, so whether row $t$ counts depends on whether token $t + 1$ is real. Masking with `m[:, :-1]` (the input side) would count predictions of padding and drop real targets.
  5. How does the tied head make wte learn from the output side?
    AnswerThe logits are `matmul(x, transpose(wte.weight))`, a differentiable op on the same `Tensor` the embedding reads. Backward accumulates the output-side gradient into `wte.weight.grad` alongside the input-side gradient from the embedding gather.
  6. Why does the test oracle build the Hugging Face forward pass from the Hugging Face-layout dict rather than from the loaded model?
    AnswerSo that the oracle never passes through the loader's transposes and splits. If it reused the loaded weights, a loader bug would appear identically on both sides and the comparison would pass.