kv-mean-center: add make-calib-corpus.sh self-generated corpus helper#52
Merged
Conversation
Generates a calibration corpus from the model itself via a temporary llama-server, removing the need for an external calibration text file. Includes a degenerate-output guard based on gzip compression ratio. Validated end to end: the resulting bias agrees with one calibrated on a standard multi-domain calibration set to within sampling noise.
khosravipasha
approved these changes
Jul 10, 2026
khosravipasha
left a comment
Collaborator
There was a problem hiding this comment.
LGTM good example, people can easily customize for their prompts use-case so should be good.
There was a problem hiding this comment.
Pull request overview
Adds a helper script to generate a self-produced text corpus for llama-kv-mean-center calibration (removing the need for users to supply external calibration text), and documents this workflow in the kv-mean-center README.
Changes:
- Add
tools/kv-mean-center/make-calib-corpus.shto spin up a temporaryllama-server, generate responses for built-in prompts, and write/validate a calibration corpus. - Document the new corpus-generation option and rationale in
tools/kv-mean-center/README.md.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| tools/kv-mean-center/README.md | Documents how to generate a calibration corpus via the new helper script and explains why it is acceptable for mean-centering calibration. |
| tools/kv-mean-center/make-calib-corpus.sh | Implements self-generated calibration corpus creation by running llama-server, calling /v1/chat/completions, and validating output size/degeneracy. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+20
to
+29
| while getopts "s:m:o:n:p:h" opt; do | ||
| case $opt in | ||
| s) SERVER_BIN=$OPTARG ;; | ||
| m) MODEL=$OPTARG ;; | ||
| o) OUT=$OPTARG ;; | ||
| n) NTOK=$OPTARG ;; | ||
| p) PORT=$OPTARG ;; | ||
| h|*) grep '^#' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; | ||
| esac | ||
| done |
Comment on lines
+75
to
+77
| echo "starting llama-server on port $PORT ..." >&2 | ||
| "$SERVER_BIN" -m "$MODEL" -ngl 99 --port "$PORT" >/dev/null 2>&1 & | ||
| SERVER_PID=$! |
Comment on lines
+9
to
+11
| # Requires: curl, jq. Starts a temporary llama-server on the given port, generates | ||
| # one response per built-in seed prompt with thinking disabled, concatenates the | ||
| # responses, and checks the result is not degenerate (gzip compression ratio). |
Print only the header comment as help text instead of grepping every comment line, exit nonzero on unknown options, add gzip to the dependency preflight and the Requires line, and stop forcing -ngl 99: the server's own --n-gpu-layers default now applies unless -g is given.
khosravipasha
approved these changes
Jul 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds
tools/kv-mean-center/make-calib-corpus.sh, a helper that generates a calibration corpus forllama-kv-mean-centerfrom the model itself, plus a README section documenting it.Why
The calibration tool requires a plain-text corpus (
-f), which meant every user had to bring their own data file. Since the per-channel K mean is dominated by model-intrinsic channel structure rather than corpus content, self-generated text works just as well: in an A/B test, a self-generated corpus and a standard multi-domain calibration set produced biases agreeing to within calibration sampling noise (cosine similarity above 0.95 on every layer, with a domain-matched split-half control at the same noise level). This removes the external-data dependency entirely.How
The script starts a temporary
llama-serveron a configurable port, generates one response per built-in seed prompt (24 prompts mixing expository, narrative, technical, code and dialogue styles) at temperature 0.9 with thinking disabled viachat_template_kwargs, concatenates the responses, and refuses to write a corpus that is too small or looks degenerate (gzip compression ratio guard, since repetitive output under-excites K channels and skews the mean estimate). The server is cleaned up on exit via trap.Validated end to end: script output fed into
llama-kv-mean-centerreproduces a previous self-generated calibration at cosine 0.991, i.e. within sampling noise.