Skip to content

The first capstone of AI Engineering trains for hours, and the release workflow in ModelRelease: gates, canary, burn check, and rollback gates on eval numbers. Until now, a model was right when its logits matched a fixture to 10−510^{-5}, as in the transformer, modern-block, and inference chapters. That oracle is gone the moment you train a new model: there is no reference to match, and two correct runs differ. Yet the bugs are real and quiet. A RoPE base read from the wrong config key, a layer skipped by an off-by-one, a tokenizer that lost one merge: every one of them still trains, still produces plausible text, and costs a few tenths of a bit per byte. Without a regression test you find out from users.

Concepts from other sections (14)

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 test: measured numbers, recorded from a good model, compared with statistics that know the difference between noise and a defect. A model eval is a test: a held-out metric and a regression rule, run on every change, that fails the build, and it is judged like any test, by the defects it catches. The section builds the statistics from first principles, works four candidates by hand, then walks through the solution function by function and the three-test suite. Try to compute each p-value before reading it.

It builds on judging tests by planted faults (TDD, unit tests, and mutation testing) and reading survivors (Mutation testing in depth). From Mathematics it uses permutation tests (Hypothesis tests) and bits per byte (Perplexity, bits per byte, NLL accumulator); from AI Engineering, the The evaluation harness and the model zoo and RoPE.

The solution is evals.py, a kata with held-out bits per byte heldout_bpb, a seeded sample-quality score quality_score, the paired sign-flip test sign_flip_p, and the regression rule regressed; test_model_evals.py, the eval suite; and baseline.json, the good model, measured. The model under test is tinymodel.py. The contract is the rules in the kata’s docstring. The solution is used like this (the fixtures, tinylm.npz, val.txt, and words.txt, live under $TINYLLM_FIXTURES/craft.22/):

Terminal window
uv run --no-project --with numpy python course/solutions/primers/craft.22/evals.py --record # writes baseline.json
(cd course/solutions/primers/craft.22 && uv run --no-project --with pytest --with numpy python -m pytest -q test_model_evals.py)

Learning objectives

  • Measure held-out bits per byte and a seeded generation score as paired, per-seed metrics.
  • Compute an exact one-sided sign-flip p-value by hand and explain why five seeds is the minimum.
  • Decide a regression with a margin and a significance test together, and explain why neither alone works.
  • Explain why a suite needs both a loss metric and a generation metric to catch model mutants.

The notation for this section:

SymbolMeaningType
s=1,…,ns = 1, \dots, na seed: it picks the held-out windows and drives the samplerint
bsb_s, csc_sthe baseline’s and the candidate’s metric at seed ssfloat
dsd_show much worse the candidate is at seed ss: cs−bsc_s - b_s for bpb (lower is better), bs−csb_s - c_s for qualityfloat
T=∑sdsT = \sum_s d_sthe observed statisticfloat
ϵ∈{+1,−1}n\epsilon \in \{+1, -1\}^na sign vector
p=12n#{ϵ:∑sϵsds≥T}p = \frac{1}{2^n} \#\{\epsilon : \sum_s \epsilon_s d_s \ge T\}the exact one-sided sign-flip p-valuefloat
δ\deltathe margin: the smallest regression worth failing a build forfloat
α\alphathe significance level, 0.05float

Measure the baseline and the candidate on the same windows and with the same seeds, so that each seed gives one difference dsd_s. The windows’ own difficulty (one held-out story is harder than another) then cancels in dsd_s instead of swamping it. Held-out bits per byte pools the bits of every window over their bytes, which makes it independent of the tokenizer: a tokenizer change that splits text into more tokens cannot hide behind a per-token loss.

If the candidate is as good as the baseline, each dsd_s is as likely to be positive as negative: flipping its sign gives an equally likely world. Enumerate all 2n2^n flips and ask how often the flipped sum is at least the one you saw. That fraction is an exact p-value with no distribution assumed. When all nn differences are positive, only the unflipped vector reaches TT, so p=1/2np = 1/2^n: 1/32=0.0311/32 = 0.031 for five seeds, 1/16=0.06251/16 = 0.0625 for four. A four-seed suite can never reject at 0.05, which is why five seeds is the minimum.

A model is never bit-identical to its baseline: float64 arithmetic, another BLAS, or a new sampling stream all move the numbers. So flag a regression only when both of two conditions hold: the mean difference exceeds δ\delta (it matters) and p<αp < \alpha (it is not noise). Significance alone fails on tiny, real, harmless shifts: float64 arithmetic moves every seed by 10−710^{-7}, and all in one direction gives p=1/32p = 1/32. The margin alone fails on noise: another sampling stream can make quality 0.03 worse on average with signs going both ways.

Held-out bpb tests what the model computes under teacher forcing. A seeded generation score tests what it does when it runs: sample from fixed prompts with a seeded generator and score the text, here as the fraction of words that exist in the training vocabulary, times one minus the rate of repeated 4-grams. Decoding bugs live only in the second metric, and some forward bugs show up only there too. One metric is not enough: held-out loss is blind to decoding bugs, and a broken final norm can make bpb better while samples get worse.

Take five seeds, a baseline bpb of 1.00 at each, δ=0.02\delta = 0.02, and α=0.05\alpha = 0.05, and judge four candidates.

Candidate A scores 1.03 at every seed. Then d=(0.03,0.03,0.03,0.03,0.03)d = (0.03, 0.03, 0.03, 0.03, 0.03), with mean 0.03>0.020.03 > 0.02 and T=0.15T = 0.15. Every flip of a positive term lowers the sum, so only ϵ=(+,+,+,+,+)\epsilon = (+,+,+,+,+) reaches 0.150.15: p=1/32=0.03125<0.05p = 1/32 = 0.03125 < 0.05. Regressed.

Candidate B scores 1.03, 1.03, 1.03, 1.03, 0.99. Then d=(0.03,0.03,0.03,0.03,−0.01)d = (0.03, 0.03, 0.03, 0.03, -0.01), with mean 0.022>0.020.022 > 0.02 and T=0.11T = 0.11. The identity gives 0.11; flipping the last sign gives 0.13≥0.110.13 \ge 0.11; any flip of a 0.030.03 lowers the sum below 0.11. So p=2/32=0.0625p = 2/32 = 0.0625. Not a regression: one seed disagrees.

Candidate C scores 1.01 at every seed. Then p=1/32p = 1/32, but the mean 0.01<0.020.01 < 0.02. Not a regression: real, but too small to fail a build for.

Four seeds, all worse, give p=1/16=0.0625p = 1/16 = 0.0625. Nothing can ever be flagged.

A correct sign_flip_p and regressed must reproduce these four verdicts exactly.

The kata, primers/craft.22/evals.py, has its spec in its docstring:

SEEDS = (0, 1, 2, 3, 4); PROMPTS = ("Once upon a time", "One day", "Mia and the", "At night")
def heldout_bpb(model, text, seed, windows=16, chars=80) -> float # default_rng(seed) picks the windows
def words(text) -> list[str]
def repeat_ngram_rate(ws, n=4) -> float
def valid_word_rate(text, vocabulary) -> float
def quality_score(model, vocabulary, seed, tokens=48) -> float # one rng for the four prompts, in order
def measure(model, text, vocabulary, seeds=SEEDS) -> dict # {"seeds", "bpb", "quality"}
def sign_flip_p(diffs) -> float
def regressed(base, cand, margin, alpha=0.05, higher_is_better=False) -> bool
# python evals.py --record writes baseline.json

The model, primers/craft.22/tinymodel.py, is given and is not part of the solution’s design: load(), encode, decode, log_probs(ids), sample(prompt, tokens, rng, temperature=0.8). It is a two-layer Llama-style decoder (RMSNorm, RoPE, SwiGLU, tied embeddings) over a character BPE of 96 merges, trained on synthetic stories; the held-out stories and the training vocabulary are fixtures.

The suite, primers/craft.22/test_model_evals.py, has at least two tests, imports only evals, tinymodel, numpy, pytest, and the standard library, reads the fixtures through TINYLLM_FIXTURES, and fails when held-out bpb or sample quality regressed against baseline.json (margins 0.02 each).

A correct solution handles six cases:

CaseKind of checkWhy it matters downstream
the four hand-worked candidates on sign_flip_p and regressedunitthe decision rule every release gate uses
bpb, word, repeat, and quality functions follow the docstring exactly (window choice, pooling, the one rng)goldena baseline recorded with one definition and compared with another is meaningless
baseline.json has 5 or more seeds and equals the good model measured nowgoldena stale or typed-in baseline gates against a model that does not exist
the suite passes on the good modelconformancea gate that fails the model it was recorded from is switched off
the suite passes on two harmless variants: float64 arithmetic, another sampling streamconformanceno false alarms on changes that do not matter
the suite fails on at least 80% of seven model mutants, always on the wrong RoPE base, a dropped layer, and a tokenizer one merge shortfaultthe defects that train and generate but are wrong

Seven observations shape the kata.

  1. There is no exact oracle for a trained model, so a test must compare a candidate with a recorded good model and decide whether the difference is a defect. That needs a measurement, a baseline, and a rule.
  2. Differences between windows are larger than differences between models. So measure both models on the same windows with the same seeds, and test the per-seed differences: pairing cancels window difficulty. This forces the seed to choose the windows (default_rng(seed)), so the baseline and the candidate see the same text.
  3. The metric must not depend on the tokenizer, or a tokenizer bug hides in it. Bits per byte, pooled as total bits over total bytes, has that property; a mean of per-window ratios would overweight short windows, and a per-token loss rewards a tokenizer that makes more, easier tokens.
  4. The first token of a window is never predicted, so its bytes must not be in the denominator; otherwise bpb is biased low by a tokenizer-dependent amount.
  5. Generation bugs are invisible to teacher-forced loss. So a second metric must run the sampler with a seeded generator and score the text. The score must be cheap and catch the two ways a broken model fails: non-words and loops.
  6. The rule needs a margin and a p-value, and an exact test needs no distributional assumption. With n=5n = 5 the sign-flip test enumerates 32 vectors, which is trivially cheap and exact.
  7. Two metrics, two directions. bpb is lower-better, quality higher-better; one regressed with a flag keeps the “worse is positive” convention in one place.
def heldout_bpb(model, text: str, seed: int, windows: int = 16, chars: int = 80) -> float:
rng = np.random.default_rng(seed) # the seed picks the windows: paired across models
starts = rng.integers(0, len(text) - chars, size=windows)
bits = 0.0
nbytes = 0
for s in starts:
w = text[int(s) : int(s) + chars]
ids = model.encode(w)
lp = model.log_probs(ids) # lp[t] predicts ids[t + 1]
bits += -sum(float(lp[t, ids[t + 1]]) for t in range(len(ids) - 1)) / math.log(2) # nats to bits
nbytes += len(w.encode()) - len(model.decode(ids[:1]).encode()) # the first token is never predicted
return bits / nbytes # pooled: total bits over total bytes

Windows are 80 characters at random offsets, so they cut words; that is fine because both models see the same cuts. Dividing once at the end pools the windows (step 3).

The generation score combines two failure detectors:

def repeat_ngram_rate(ws: Sequence[str], n: int = 4) -> float:
grams = [tuple(ws[i : i + n]) for i in range(len(ws) - n + 1)]
if not grams:
return 0.0
seen: set[tuple[str, ...]] = set()
repeats = 0
for g in grams:
repeats += g in seen # a repeat of an n-gram that started earlier
seen.add(g)
return repeats / len(grams)
def quality_score(model, vocabulary: set[str], seed: int, tokens: int = 48) -> float:
rng = np.random.default_rng(seed) # one stream for all four prompts
text = " ".join(model.sample(p, tokens, rng) for p in PROMPTS) # in order: the stream is reproducible
return valid_word_rate(text, vocabulary) * (1.0 - repeat_ngram_rate(words(text), 4))

The product is 1 only when every word is real and no 4-gram repeats; a model that loops scores low on the second factor, one that emits fragments on the first. Using one generator for the prompts in a fixed order means the score at seed ss is a deterministic function of the model, so it can be paired.

def sign_flip_p(diffs: Sequence[float]) -> float:
d = np.asarray(diffs, dtype=np.float64)
observed = d.sum()
hits = sum(
# every sign vector; ties count, with a 1e-12 tolerance so the identity always counts
float(np.dot(s, d)) >= observed - 1e-12 for s in itertools.product((1.0, -1.0), repeat=len(d))
)
return hits / 2 ** len(d) # >= 1/2^n: the identity is always a hit
def regressed(base, cand, margin, alpha=0.05, higher_is_better=False) -> bool:
b, c = np.asarray(base, dtype=np.float64), np.asarray(cand, dtype=np.float64)
worse = b - c if higher_is_better else c - b # positive means worse, for either metric
return bool(worse.mean() > margin and sign_flip_p(worse) < alpha) # it matters AND it is not noise

Check it against the worked example. Candidate A gives worse = [0.03]*5, mean 0.03 > 0.02, and only the identity reaches the sum, so p=1/32p = 1/32: regressed. Candidate B’s mean is 0.022, but two vectors reach 0.11, so p=2/32p = 2/32: not regressed. The 1e-12 tolerance exists because a sum of floats in a different order can differ in the last bit. Without it, a sign vector whose sum equals the observed one mathematically (a tie, which the definition counts) could be dropped by rounding, making pp too small. The full kata is evals.py.

FIX = Path(os.environ["TINYLLM_FIXTURES"]) / "craft.22" # the model and texts are fixtures, not code
@pytest.fixture(scope="module")
def current(): # measured once per run: the expensive part
model = tinymodel.load(FIX / "tinylm.npz")
text = (FIX / "val.txt").read_text()
vocab = set((FIX / "words.txt").read_text().split())
return evals.measure(model, text, vocab)
@pytest.fixture(scope="module")
def baseline():
return json.loads((HERE / "baseline.json").read_text())
def test_seeds_match_the_baseline(current, baseline):
# Pairing is only valid on the same seeds, and five is the minimum for p < 0.05.
assert current["seeds"] == baseline["seeds"] and len(baseline["seeds"]) >= 5
def test_heldout_bpb_did_not_regress(current, baseline):
assert not evals.regressed(baseline["bpb"], current["bpb"], evals.BPB_MARGIN), (
f"bpb {current['bpb']} vs baseline {baseline['bpb']}"
)
def test_sample_quality_did_not_regress(current, baseline):
assert not evals.regressed(
baseline["quality"], current["quality"], evals.QUALITY_MARGIN, higher_is_better=True
), f"quality {current['quality']} vs baseline {baseline['quality']}"

Each metric is its own test, so a failure names which property regressed. The baseline is a file written by evals.py --record from the good model, never typed in. The recorded bpb ranges from 0.97 to 1.20 across seeds, which is exactly the window-difficulty spread that pairing removes.

The suite is judged against seven model mutants, and each must be caught by at least one metric:

MutantWhich metric must catch it
RoPE base 500 instead of 10000either: the forward pass is wrong, so teacher-forced bpb moves
the last layer never runseither, as above
the tokenizer is one merge shortbpb, measured per byte so the extra tokens cannot hide it
decoding samples from the previous position’s logitsquality only: teacher forcing never calls sample, so bpb is unchanged
attention scores not scaled by 1/d1/\sqrt{d}either: the forward pass is wrong
the causal mask shows the next tokeneither: the forward pass sees what it must predict
the final norm loses its gainquality only in the direction that matters: bpb improves by 0.16 while samples get worse

Two of the seven are invisible to a bpb-only suite, which is why a single-metric suite cannot reach 80%. The full suite is test_model_evals.py.

The suite is cheap: 5 seeds×(16 forward passes+4 samples of 48 tokens)5 \text{ seeds} \times (16 \text{ forward passes} + 4 \text{ samples of } 48 \text{ tokens}) on a two-layer model takes seconds. The sign-flip test is O(2n⋅n)O(2^n \cdot n), 160 multiply-adds at n=5n = 5; beyond about 20 seeds one would sample sign vectors instead of enumerating them.

The sign-flip test beats a paired t-test here. The t-test assumes normal differences, which five seeds cannot check; the sign-flip test is exact under the symmetric null. Its price is coarse p-values (k/32k/32).

It also beats a bootstrap interval at this size. A bootstrap over five values is unstable, and with small nn an exact test is the honest choice. The bootstrap fits better when there are hundreds of items, as in Agent evals as tests.

More seeds would give more power and finer p-values at linear cost; five is the minimum that can ever reject at 0.05, chosen to keep the gate fast.

The margins, 0.02 bits per byte and 0.02 quality, are set above the harmless variants’ shifts and below the mutants’ effects. A fixed margin is a decision someone signs, like the perf budget in Benchmarks and perf gates.

Eight pitfalls account for the ways a model eval suite fails to protect anything:

PitfallSymptomHow the solution avoids it
1. checking only that the model runsa wrong RoPE base, a dropped layer, a tokenizer one merge short, an unscaled attention, a mask that shows the next token: all train and all generatea measured bpb and quality compared with a recorded baseline
2. held-out loss as the only metrica decoder that samples from the previous position’s logits has a perfect bpba second, generation-based metric, quality_score
3. “lower bpb is better, so it passed”dropping the final norm’s gain improves bpb by 0.16 and ruins the samplesthe quality test fails independently of bpb
4. exact equality with the baselinefloat64 arithmetic or another machine fails the buildregressed requires the mean to exceed a margin
5. a margin without a significance testanother sampling stream, 0.03 worse on average with mixed signs, fails the buildregressed also requires sign_flip_p < 0.05
6. four seeds, or unpaired windowsnothing can ever reach p<0.05p < 0.05; window difficulty drowns the differencefive seeds, asserted by test_seeds_match_the_baseline; the seed picks the windows
7. a stale or typed-in baselinethe suite compares with a model that no longer existsbaseline.json is written only by evals.py --record
8. bpb averaged per window, or per tokena tokenizer change hides behind more, easier tokensbits and bytes are pooled before one division

Warning: A lower loss is not a pass. Dropping the final norm’s gain improves bpb by 0.16 while the samples fall apart; only the generation metric sees it.

This section reused judging by planted faults from TDD, unit tests, and mutation testing, the permutation tests of Hypothesis tests (the sign flip is their paired, one-sided, exact form), and bits per byte from Perplexity, bits per byte, NLL accumulator. Going forward:

Each piece has a production equivalent:

  • heldout_bpb corresponds to EleutherAI’s lm-evaluation-harness, which adds rolling-window perplexity, bits per byte and per word, and hundreds of tasks (lm_eval/api/task.py, loglikelihood_rolling).
  • regressed corresponds to Dror et al.’s significance testing for NLP, which adds test selection by metric, multiple comparisons (Holm), and effect sizes.
  • quality_score corresponds to HELM and OpenAI evals, which add many scenarios and metrics per model and judges for open-ended outputs (crfm-helm).
  • The model mutants correspond to mutation testing of ML systems (DeepMutation), with operators on weights and training data and killing by statistical tests (Ma et al., ISSRE 2018).
  • Model eval: a held-out metric plus a regression rule, run on every change as a test.
  • Bits per byte (bpb): total predicted bits divided by total predicted bytes; independent of the tokenizer.
  • Paired measurement: measuring baseline and candidate on the same windows and seeds, so per-seed differences cancel window difficulty.
  • Sign-flip test: an exact permutation test that counts sign vectors whose flipped sum reaches the observed sum.
  • Margin: the smallest regression worth failing a build for.
  • Model mutant: a deliberately broken variant of a model (wrong RoPE base, dropped layer) used to judge the eval suite.
  • A model eval is a test: a held-out metric and a regression rule, run on every change, that fails the build, judged by the defects it catches.
  • A model is never bit-identical to its baseline, so a margin and a significance test together keep the suite quiet on harmless changes.
  • Five seeds is the minimum for p<0.05p < 0.05 with an exact paired sign-flip test, because the smallest possible p-value is 1/2n1/2^n and 1/16>0.051/16 > 0.05.
  • One metric is not enough: held-out loss is blind to decoding bugs, and a broken final norm can make bpb better while samples get worse.
  1. Why can a four-seed suite never flag a regression at α=0.05\alpha = 0.05?

    AnswerThe identity sign vector always reaches the observed sum, so $p \ge 1/2^n$. For $n = 4$ that is $1/16 = 0.0625 > 0.05$, so `sign_flip_p(worse) < alpha` is never true.
  2. Why is the first token’s byte length subtracted from the denominator in heldout_bpb?

    AnswerThe model never predicts the first token of a window (there is no context), so its bits are not in the numerator. Keeping its bytes in the denominator would lower bpb by an amount that depends on how long the tokenizer's first token is, which a tokenizer change would shift.
  3. Why does float64 arithmetic, which shifts every seed’s bpb by about 10−710^{-7} in the same direction, not fail the suite?

    AnswerAll differences positive gives $p = 1/32 < 0.05$, so significance alone would flag it. The mean, $10^{-7}$, is far below the 0.02 margin, so `regressed` is false. This is why both conditions are required.
  4. Which mutant does held-out bpb miss, and why does the quality score catch it?

    AnswerSampling from the previous position's logits: teacher-forced `log_probs` never calls `sample`, so bpb is identical. The generated text is shifted by one step and turns into fragments and repeats, so the valid-word rate and the repeat factor drop.
  5. Why must quality_score use one generator for the four prompts in a fixed order?

    AnswerSo the score at seed $s$ is a deterministic function of the model and the seed. Then the baseline's and the candidate's scores at the same seed are paired measurements, and a difference is due to the model, not to which random numbers each prompt happened to draw.