Apocrypha2026-10-24· 11 min read

Calibrate your quantizer on your own traffic: 9.5% lower KL at the same size

An importance matrix built from Apoc's own requests and replies cut mean KL against Q8_0 from 0.5595 to 0.5065 at the same 9.94 GiB, with the steps to do it yourself in stock llama.cpp. Also: a llama-quantize pattern that silently caught the wrong tensors, a speed-up that was really a fresh process, draft heads that win offline and lose live, what a strict grammar costs in tok/s, and how we are teaching it a compact notation.

Update 2026-10-25: two items this post listed as open have moved. (1) The production file’s own KL is now measured locally, with the Kaggle method: 0.4958 against 0.5065 on Kaggle, same Q8_0 reference, so the local method agrees with the Kaggle one to about 0.011. (2) SFT run 3, run 2’s recipe on the production file, 300 steps, started on Kaggle 2026-10-24; its output had not been built into a model file or evaluated on our engine as of 2026-10-25 11:55, so it has no result. Details: the 2026-10-25 post.

What you can reuse

  • Calibrate the quantizer on your own traffic. An importance matrix built from Apoc’s real requests and replies cut mean KL against the Q8_0 reference from 0.5595 to 0.5065 (9.5 percent) and held-out perplexity by 12.5 percent, at the same 9.94 GiB. The steps are below; they only need stock llama.cpp tools.
  • Anchor every per-tensor pattern you give llama-quantize. Each --tensor-type (or --tensor-type-file) name is a regular expression searched anywhere in the tensor name, and the first match wins. Our output.weight=q3_k line also caught 12 of the 16 attn_output.weight tensors. Write ^output\.weight$, and after quantizing, list every tensor’s type (gguf-dump from llama.cpp’s gguf package) and compare it with what you asked for.
  • A/B speed with a fresh process in every arm. A rebuilt file looked twice as fast as the one it replaced. Fresh processes showed both files equally fast: the old process had slowed down by itself, for reasons we have not found.
  • Judge a draft head live, not offline. Training raised offline depth-1 acceptance from 0.688 to 0.867, yet live decode got about 1 tok/s slower; the first round showed the same pattern. Measure accepted tokens and tok/s on real prompts before training more.
  • Budget for grammar-constrained decoding. Our strict 366 KB GBNF grammar took decode from 27.2 to 11-13 tok/s. It bought format correctness (canon on 0 of 6 candidates unconstrained, 2 of 6 with it), so use it only on the turns that need it.
  • Unused vocabulary slots are free tokens. This model’s vocabulary has unused slots. Filling 128 of them with pieces of our notation (31 structure tokens, 97 labels) cut tokens on that text by 13.2 percent without growing any tensor; the new rows still have to be trained before a model uses them well.

Build an importance matrix from your own traffic

  1. Collect real prompts and replies from your own use, deduplicate them, and hold out a slice for scoring. We used 401 documents (about 3.0 MB) for calibration and 69 for scoring.
  2. Compute the matrix on the calibration text, quantize with it, and keep any per-tensor overrides anchored:
llama-imatrix -m model-f16.gguf -f calibration.txt -o imatrix.gguf
llama-quantize --imatrix imatrix.gguf model-f16.gguf model-Q2_K.gguf Q2_K
  1. Score each candidate against a high-precision reference on the held-out text. Save the reference’s logits once, then compare every candidate with them (the second command reads the tokens from the saved file):
llama-perplexity -m model-Q8_0.gguf -f heldout.txt -c 2048 --kl-divergence-base ref.kld
llama-perplexity -m model-Q2_K.gguf -c 2048 --kl-divergence-base ref.kld --kl-divergence
  1. Run the reference against itself once: KL must read 0 and top-1 agreement 100 percent, or the method is wrong. Then compare candidates of equal size by mean KL and top-1 agreement, and treat close results as ties until a bigger held-out set separates them (the 2026-10-25 post shows why mean KL alone can mislead).

We ran steps 2 and 3 on Kaggle’s two T4s (4 h 8 min for the matrix and four candidates). Your text should look like what the model will actually see: ours mixed agent requests, replies and the notation Apoc is learning.

An importance matrix from Apoc’s own traffic

Apoc, the local model behind Apocrypha, is Qwen3.8-27B in a 2-bit mixed recipe (“L1-10”) that fits one Intel Arc A770 with 16 GB. Until now its importance matrix was generic. The matrix tells the quantizer which weights inside each tensor matter most; which tensor gets which type is the recipe’s job. We built one from what Apoc actually reads and writes: 401 deduplicated documents, about 3.0 MB of text (245 requests, 63 replies, 93 CSL seeds), for calibration, and 69 held out (58 requests, 5 replies, 6 CSL seeds) for scoring.

A Kaggle kernel on two T4s (4 h 8 min) computed the matrix (820 chunks of 512 tokens), then quantized four candidates and scored each against the Q8_0 logits on 8,184 held-out tokens. A/A floor (Q8_0 against itself): KL 0.000000, top-1 agreement 100.0 percent.

candidate size mean KL top-1 agree PPL
L1-10 + Apoc imatrix 9.94 GiB 0.5065 84.43 % 9.35
L1-10 + generic imatrix (production, control) 9.94 GiB 0.5595 83.35 % 10.69
knapsack90 + Apoc imatrix 9.01 GiB 0.6144 82.53 % 10.32
knapsack90 + generic imatrix 9.01 GiB 0.6234 81.09 % 11.17

The tool’s own Q8_0 reference perplexity reads 8.16, yet the same Q8_0 run scored as a candidate in the A/A control reads 9.04, so read the perplexity column as a comparison between candidates, not as a gap to Q8_0.

The rule, written before the run: a smaller recipe may replace the production file only if it is no worse on held-out Apoc text; 0.6144 is worse than 0.5595. L1-10 stays, rebuilt with the Apoc matrix: 9.5 percent lower mean KL and 12.5 percent lower held-out perplexity at the same size. Caveats: one run on one random held-out split, not repeated; the 99th-percentile KL of 15.43 says the 2-bit tail is far from Q8_0.

The local rebuild took two passes (445 s, then 454 s): an unanchored recipe pattern first dropped the draft head’s attn_output to Q3_K. The fixed file differs from production in 0 tensors (10,981,283,008 bytes).

The restart A/B, and what it really measured

Each of the three fresh arms: a fresh process, cold cache, three short no-think probes (96-120 tokens) and one real 22,679-token agent turn. The last row is the process they replaced, measured the evening before.

arm short decode real turn: prefill / decode / acceptance VRAM dedicated / shared
new file 36.0 tok/s 187 tok/s / 31.0 tok/s / 0.79 13,909 / 125 MiB
old file (control) 34.7 tok/s 187 / 33.6 / 0.89 14,123 / 125 MiB
new file again (production now) 32.4 tok/s 188 / 33.6 / 0.89 14,115 / 125 MiB
previous process, started 2026-10-23 21:38, measured 45 min later 17.5 tok/s 27 tok/s median on real turns 13,975 / 380 MiB

The two files are speed-neutral within noise. The doubling of short decode and the spill drop (380 to 125 MiB) came from the fresh process, not from the new quantization. The process they replaced, started 2026-10-23 21:38, measured slow 45 minutes later (17.5 tok/s short, 27 tok/s on real turns, 380 MiB shared); the fresh launches on 2026-10-24 measured 32.4 to 36.0 tok/s short and 31.0 to 33.6 tok/s on the 22,679-token turn with 125 MiB shared, on both model files. The cause is not isolated; the launcher now logs buffer sizes so a slow process can be compared before it is restarted. The new file is in production for its offline quality gain only.

MTP head, round 2

Apoc decodes with a draft head (MTP) that proposes tokens the trunk then verifies. Round 1’s head gained offline (agreement 0.59 to 0.74) and lost live, so round 2 trained only on Apoc’s real-traffic replies against the new base: 14 rows, 1,162,955 characters (13 for training, 1 held out), 3,000 steps. Offline, the held-out score (expected accepted draft tokens per verify, two draft steps) went from 1.1159 to 1.6191, and depth-1 acceptance from 0.688 to 0.867.

Live, three replicates on two 300-token prompts, greedy, with identical output hashes between heads: the untrained Q8 head decoded 41.26 / 39.41 / 38.51 and 43.20 / 40.66 / 39.75 tok/s with acceptance 0.7016 and 0.7778; the trained head 40.06 / 38.81 / 38.40 and 41.93 / 40.38 / 40.26 tok/s with acceptance 0.7016 and 0.8000. Acceptance up 0.022 on one prompt and unchanged on the other, decode about 1 tok/s slower: the same pattern as round 1. Production keeps the untrained head and came back at 09:57 on the rebuilt file. The trained file passed the graph crash gate (a long real request sent twice, then a short decode, with SYCL graphs on). What the probes cannot show: the head on long real turns, where acceptance already reads 0.89.

CSL: gate, grammar, adapter, seeds

CSL (Caveman Spec Language) is a compact notation for specs and reasoning: one proposition per line. A line is an expression; it may carry a label (^p1), a modal at the start (W! must, N! never, M? may, R! should, I> intent, P> plan, D> decide, Q? ask), a trailing status (ok, part, rep, X, unk, hyp, dep, law; absent means open) and annotations such as {x source} and {t time}. Lines are grouped in blocks (#P problem, #D decompose, #T trace, #S synth, #C conclude); a document opens with !csl4, !ans closes the reasoning and opens the answer, and !. ends it. Apoc is being trained to think and reply in it. This is CSLv4 in its compact ASCII surface: the key and the reference parser are in the apoc-csl-tools dataset below (prepared for release), and this site has a CSL page. The earlier CSLv3 is documented at https://github.com/Apocky/CSLv3.

  • Strict gate. Canon means zero findings from three layers: the surface rules, the CSLv4 reference parser (any diagnostic fails) and the answer block. At most 9 content words per statement, no filler words inside symbols, no legend. A negative suite proves it can fail.
  • Grammar-constrained decoding. A 366,455-byte GBNF grammar (6,477 rules), generated from the gate’s own constants, passes 377 of 377 test rows against two engine builds. On six self-play candidates: unconstrained, 0 of 6 canon at 27.2 tok/s; the first grammar (legend kept, no word cap) 1 of 6 at 17.7 tok/s; the shipped grammar 2 of 6 at 11 to 13 tok/s, with only semantic findings left (unresolved references, duplicate labels).
  • SFT run 1. QLoRA on the 2-bit file: 79 rows, 2 epochs (150 steps), 29,000 supervised tokens, held-out loss 1.3225 to 0.9513. On 20 unseen English prompts, grammar off: base 0 of 20 canon, adapter 9 of 20; median reply 135 to 188 tokens. It now serves tool-free CSL-mode requests live, with the grammar on.
  • Teacher rounds. An open-weight teacher (Qwen3.8-27B at Q6_K on Kaggle’s two T4s, under the grammar) and Apoc’s self-play write every seed. Round v6: 160 candidates for 40 sources, 123 gate-canon (77 percent), 96 also kept every source value; with 3 earlier seeds from the local engine, 99 seeds. Round v7: 480 candidates for 120 sources, 395 canon (82 percent), 324 kept: 423 seeds. Selection by gate and value check only; Claude never authors or edits a seed.
  • Vocabulary extension. 128 new tokens (31 structure, 97 labels) fill unused vocabulary slots, so no tensor grows. On 40 canon seeds the extended GGUF tokenizes 16,394 tokens to 14,234, 13.2 percent fewer (the HF tokenizer on the same sample: 14,218). The rows are untrained: a tokenizer artifact, not a model.
  • SFT run 2 (394 rows from the 423 seeds, extended tokenizer, 748 steps, held-out loss 2.489 to 1.280) finished at 15:44 after one evaluation retry. On 20 new unseen prompts: base 0 of 20 canon, adapter alone 0 of 20, adapter on the extended file 4 of 20; tokens per 1,000 characters 323.5, 287.9 and 271.4 (the extended arm is 16 percent cheaper than base). The adapter alone scores like base because it was trained on the extended vocabulary. The two runs used different prompt sets, so we checked the same gate on the same prompts: run 1’s adapter on run 2’s prompts, on our engine and the current file, scored 1 of 40. Run 2 is not a regression on that set. Caveat: run 2 was scored on Kaggle’s engine, where base opened in CSL on 11 of 20 prompts against 34 of 40 on ours. Still to measure: run 1’s adapter on its own prompts on the current file, and run 2’s extended arm on our engine.

Public

The calibration text, the raw reasoning traces and the training sets are built from real sessions. They stay private, and so do the Kaggle kernels that read them.

Update, 12:45: the recipe bug, fixed and measured

llama-quantize matches each recipe line against tensor names by regular-expression search, and the first match wins. The line output.weight=q3_k also matched every full-attention attn_output.weight, so 12 of those 16 tensors had shipped at Q3_K instead of the Q4_K or Q5_K the allocator asked for. With every pattern anchored, a rebuild puts all 274 tensors at their requested types, and a new gate checks that type by type. On the same 8,184 held-out tokens of Apoc’s text, the requested assignment measured worse: perplexity 6.2783 against 6.0581. A repeat run of the shipped file matched exactly, so this is not noise between runs. Every measurement that picked the L1-10 recipe was also made on the shipped assignment, so production keeps it, and the published recipe reproduces it exactly. A second fix landed in the same restart: the CSL adapter now loads at scale 0, so no turn gets it before the warm-up job runs.

What was not measured

CSL answer correctness beyond the value check; the grammar on tool-using turns; the cause of the slow process of 2026-10-23; KL of the production file itself (the Kaggle candidates used a stock Q2_K solver for 239 unlisted tensors, so the A/B holds within the kernel); the trained draft head on long real turns; compaction frequency.

Next

Collect SFT run 2; if the extended arm holds canon and cuts tokens, write its rows into the production file and A/B it on the engine. Chase the slow-process defect: the launcher now logs buffer sizes at start, so a slow process can be compared before it is restarted. For the draft head, measure acceptance and tok/s on five real 20k-token turns before any round 3; if 0.05 more acceptance does not buy 1 tok/s, stop training heads.

Enjoyed this? Apoc runs on one home graphics card and a lot of late nights. If you'd like to help, Ko-fi is the way.
Support on Ko-fi