Skip to content

kv-mean-center: add make-calib-corpus.sh self-generated corpus helper#52

Merged
bri-prism merged 2 commits into
prismfrom
feat/kv-mean-center-selfgen-corpus
Jul 10, 2026
Merged

kv-mean-center: add make-calib-corpus.sh self-generated corpus helper#52
bri-prism merged 2 commits into
prismfrom
feat/kv-mean-center-selfgen-corpus

Conversation

@bri-prism

Copy link
Copy Markdown

What

Adds tools/kv-mean-center/make-calib-corpus.sh, a helper that generates a calibration corpus for llama-kv-mean-center from 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-server on 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 via chat_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-center reproduces a previous self-generated calibration at cosine 0.991, i.e. within sampling noise.

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.
@github-actions github-actions Bot added documentation Improvements or additions to documentation examples labels Jul 10, 2026

@khosravipasha khosravipasha left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM good example, people can easily customize for their prompts use-case so should be good.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.sh to spin up a temporary llama-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 thread tools/kv-mean-center/make-calib-corpus.sh
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.
@bri-prism bri-prism merged commit 3277aaa into prism Jul 10, 2026
6 checks passed
@khosravipasha khosravipasha deleted the feat/kv-mean-center-selfgen-corpus branch July 10, 2026 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation examples

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants