calib-corpora / METHOD.md
worthant
docs: refactor markdown documentation files
60e42e6
|
Raw History Blame Contribute Delete
5.73 kB
# Calibration methodology - what we measured
Findings from building `nemotron-3.5-lightning`. They are about calibration in
general, not about one model, and every number here is reproducible with the
tools in this repo.
## A recipe is per-model, and the chat markup is the reason
The pool is model-agnostic: `pool/**/**.jsonl` holds dialogues as structured
records, not as text. Rendering into a model's chat template happens at build
time, so one pool serves every target.
This matters more than it sounds. Reusing a build made for another model puts
that model's special tokens in the corpus. For `muse-glimmer-30b` those are
`<|start|>`, `<|message|>`, `<|eot|>`; for `nemotron-3.5-lightning` they are
`<|im_start|>`, `<|im_end|>`, `<think>`, `<tool_call>`. **The two sets do not
overlap at all.** Run `llama-imatrix --parse-special` with the wrong build and
the markup is tokenized as literal punctuation, while the tokens the model will
really see appear zero times — for these recipes that is the agentic plus
reasoning slices, about 40% of the corpus, calibrating nothing.
It also shows up in convergence. Same tool, same model, same chunk budget:
| corpus | step 1504 → 2000 | tensors still moving |
| ----------------------------------- | ---------------: | -------------------: |
| rendered in another model's markup | mean cos 0.9928 | 22 |
| rendered in the target's own markup | mean cos 0.9985 | 10 |
Correct markup converges about twice as cleanly, because the wrong tokens were
injecting noise into the estimate.
Add a model with a recipe plus a renderer module; `chat.format` selects it. The
vocabulary sweep is per-tokenizer and auto-selected by recipe name, so adding a
model never requires editing recipes that already exist.
## Content matters ~100× more than volume
Cosine similarity between imatrix files, per tensor:
| comparison | mean cos |
| ----------------------------------------------- | -------: |
| same corpus, 823 vs 3000 chunks (3.6× the data) | 0.9819 |
| different corpora, both at 823 chunks | 0.8553 |
Tripling the data moves the importance vector by 0.018. Changing what is _in_
the corpus moves it by 0.145. Beyond the saturation point, more of the same text
is close to free of effect; a different mix is not.
The two corpora also do not converge toward each other as data grows (0.855 →
0.860 from 823 to 3000 chunks), so the difference is systematic, not sampling
noise.
## When to stop: a criterion, not a feeling
The imatrix estimates a mean, and means converge. The question "have I collected
enough" has a definite answer.
**Necessary, cheap, no model needed.** Build at N and 2N chunks, take the
per-tensor cosine between them. Done when no tensor is still moving.
`tools/converge.py` does this against `--save-frequency` checkpoints, so one run
produces the whole curve for free.
For `nemotron-3.5-lightning` that point is ~8,500 chunks (4.3M tokens): past it,
zero of 186 tensors change measurably, min cosine 0.9963.
**Sufficient, expensive, final.** Quantize the same recipe twice, with the
imatrix at N and at 2N, and measure KL divergence of both against the
unquantized model on held-out text. Done when the difference between them is
below the reported KLD error — at that point more data provably cannot change
the product.
Note what this criterion does _not_ answer: whether the corpus has the right
content. That question is only well-posed once you fix the evaluation set,
because "importance" is defined relative to the text the model will actually
see. A corpus tuned for agentic work should win on agentic held-out and may lose
on generic prose — that is the intended outcome, not a bug. This is why `eval/`
carries three independent sets rather than one.
## `% Active` is not a quality metric
`llama-imatrix --show-statistics` reports a `% Active` column. It is the
fraction of columns whose mean squared activation exceeds a hard threshold of
`1e-5` (`tools/imatrix/imatrix.cpp`, `compute_statistics`). It measures
activation magnitude, not calibration coverage.
Early layers score low on it because their activations are genuinely small —
layer 1's `ffn_down_shexp` has Σ(Act²) = 0.13. No corpus raises that; it is a
property of the model. Chasing the number optimizes for nothing.
Two checks that do mean something:
- **Dead experts** — the `.counts` array in the imatrix says how many times each
expert was routed to. `counts[i] == 0` is a genuine hole. Measured here: 0
dead of 5,888 (46 expert tensors × 128) at every corpus size tested, including
823 chunks. For this model, expert coverage is not what a larger corpus buys.
- **Per-tensor cosine between imatrix files** — the convergence criterion above.
The per-layer Σ(Act²) curve is a model property, not a corpus property: two
corpora with cosine 0.855 between their imatrices produce per-layer curves
within 8% of each other (median 2.6%). Useful as a sanity check for a broken
build, useless as a quality claim.
## Practical notes
- `llama-imatrix` defaults to `parse_special = false`. Without `--parse-special`
all chat markup is literal text.
- `-b 8192` instead of the default 512: 1h22m instead of 2h12m for 9.3k chunks
on 2×RTX 5090. It plateaus there; `-b 16384` saves another minute.
- MTP / NextN blocks are never executed in a normal forward pass, so they
collect no imatrix data at any corpus size. Pin them explicitly at quantize
time.
- `max_doc_fraction` is not optional. Uncapped, one amalgamated header took
59.8% of a slice — the imatrix then describes that file rather than that
category.