Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ With flags:
```sh
M=path/to/model.gguf

rustingstrata calibrate --model $M # once per model and machine: best chunk size, MTP drafts
rustingstrata calibrate --model $M --dry-run # preview the workload matrix and knob space
rustingstrata calibrate --model $M # once per model and machine: best chunk, MTP drafts, short threshold
rustingstrata chat --model $M --gpu # --think for thinking mode, --temp 0.7 to sample
rustingstrata generate --model $M --gpu --prompt "Write a haiku about rivers." -n 128
rustingstrata serve --model $M --gpu --addr 127.0.0.1:8080
Expand All @@ -80,6 +81,7 @@ Inkling 2 open the GPU by themselves.

- [USAGE.md](docs/USAGE.md): flags, the chat page and MCP tools, the OpenAI and Anthropic APIs, prompt caching,
several GPUs.
- [CALIBRATION.md](docs/CALIBRATION.md): the budgeted search, task corpora, the `spec` sweep and the JSON file.
- [MODELS.md](docs/MODELS.md): supported architectures and GGUF types, notes and caveats for each model.
- [BENCHMARKS.md](docs/BENCHMARKS.md): every model against llama.cpp; older runs are in the
[benchmark history](docs/README.md#benchmark-history).
Expand Down
190 changes: 190 additions & 0 deletions docs/CALIBRATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
# Calibration

`rustingstrata calibrate` measures the settings that make a model fastest on **this** machine and writes them to
`~/.cache/rusting_strata/strata-<model>[-backend][-dev…].json`. Later runs read that file unless a flag overrides the
value. One calibration per model, backend and device combination is enough; re-run after changing GPU, driver, quant,
KV format or a draft file.

Every architecture the binary supports is measured, including Inkling 2, Hy4, GLM, DeepSeek and Gemma 4, which the
old command skipped because they run on their own code path. Measurement is delegated to the existing `bench`
command (as a child process, reading its `strata.bench.v1` JSON), so there is a single timing implementation to trust.

## Quick start

```sh
M=path/to/model.gguf

# See the derived knob space and workload matrix, without loading weights:
rustingstrata calibrate --model $M --dry-run

# A normal calibration: 24 samples, prompt depths 128/512/2048:
rustingstrata calibrate --model $M

# A smaller, explicit search with two real corpora:
rustingstrata calibrate --model $M --chunk-values 512,1024,2048,4096 \
--task prose --task coding --pp 128,512,2048 --tg 128 --samples 32 \
--out ./calib-reports

# Every model in a folder, one after another:
rustingstrata calibrate --model ~/models
```

The command prints the workload matrix and knob space, then the baseline and the winner, then the path of the JSON
file. `--out DIR` additionally writes a full trace of every sample as `strata-<model>-calib-report.json`.

## What is optimized

Two signals are measured per workload: **prefill throughput** (prompt tokens / prefill time) and **decode throughput**
(generated tokens / greedy decode time). Both are divided by their own baseline and combined with a geometric mean:

@aletheore aletheore Bot Nov 23, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No longer detected as of df37bb56787d.


🟡 Medium · Correctness · 4 of 7

The score formula covers one workload, but the matrix has several prompt depths and tasks, and the doc never says how they are combined. A user cannot tell whether the score is a mean over all workloads, a minimum, or per-workload. A candidate that wins on prose but regresses on coding may still be reported as the winner, and the user cannot check this from the doc.

Prompt for your AI agent
File: docs/CALIBRATION.md
Line: 39
Issue: The score formula covers one workload, but the matrix has several prompt depths and tasks, and the doc never says how they are combined. A user cannot tell whether the score is a mean over all workloads, a minimum, or per-workload. A candidate that wins on prose but regresses on coding may still be reported as the winner, and the user cannot check this from the doc.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

```
score = sqrt((prefill / baseline_prefill) · (decode / baseline_decode))
```

An arithmetic mean would let a huge gain in one signal hide a regression in the other. Ratios are clamped to
`[1e-3, 1e3]` so a failed measurement cannot produce infinities. Several workloads are combined with a geometric mean,
so every prompt depth counts equally. Time to first token is *not* a separate signal: for greedy decoding the first
token's logits come out of the prefill, so TTFT would just reweight prefill.

The baseline is what a user gets today **without** a calibration file: the engine's own chunk — 4096 for the main
engine, 256 (Gemma 4), 512 (GLM), 1024 (Hy4) and 256 (Inkling 2, which clamps its GPU prefill to that size) for the
architectures with their own forward, and no chunk at all for DeepSeek, which prefills one token at a time. Where the
knobs apply, the baseline also uses short-prompt threshold 32, the physical core count and no drafts. The reported
score is relative to that, not to the first point of the search space.

Context length and KV format are deliberately not searched. They are memory choices the user makes; a noisy speed
win must not silently double someone's KV cache. `--ctx` and `--kv` only set the conditions of the measurement.

## Workloads and tasks

`--pp` lists prompt depths (default `128,512,2048`); `--tg` sets decoded tokens (default 128). All depths of one task
are measured in a single child process.

- `prose` (default): the built-in repeated paragraph, a stable machine-level baseline.
- `coding`: a built-in code snippet; real punctuation and identifiers change routing and draft acceptance.
- `user:PATH`: your own prompt file, for RAG documents, long system prompts, etc.

Repeat `--task` to mix corpora:

```sh
rustingstrata calibrate --model $M --task prose --task user:prompts.txt --pp 256,2048
```

## Search strategy

The search is deterministic for a given `--seed` and bounded by `--samples`:

1. **Baseline** — current defaults, measured on the full workload matrix (not counted in the budget). It is also a
trial in the search, so a candidate that scores below it cannot win. If no candidate beats it, no calibration is
written and the defaults stay in force. A failed baseline warns and scores against a neutral `1x` reference; such a
run writes a file only when a point beats that reference.
2. **Coarse sweep** — up to `min(samples/3, 8)` points from a low-discrepancy (radical-inverse) sequence over the
joint knob space. Their scores only pick starting points; they are never compared with full-matrix scores and
cannot win.

@aletheore aletheore Bot Nov 23, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No longer detected as of df37bb56787d.


🔵 Low · 5 of 7

Final polish is described as one more pass, but the doc does not say whether its samples count against the --samples budget. A user with --samples 16 finds the run used more samples than the cap because polish is outside the budget. The stated hard cap is then not accurate.

Prompt for your AI agent
File: docs/CALIBRATION.md
Line: 76
Issue: Final polish is described as one more pass, but the doc does not say whether its samples count against the --samples budget. A user with --samples 16 finds the run used more samples than the cap because polish is outside the budget. The stated hard cap is then not accurate.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

3. **Multi-start coordinate descent** — each start is re-measured on the full matrix, then each knob is tried at
every other value and the best move kept. The walk stops after two rounds without improvement. `stop_reason` is
reset for every start, so all of them get a full descent.
4. **Final polish** — one more pass over the cheap knobs from the overall winner.

A candidate that fails (OOM, timeout, missing JSON fields) is recorded with its error and never wins; it still counts
against the budget. Every stage, including the final polish, draws from `--samples`; the baseline alone is outside the
budget. `--reps N` repetitions happen inside one sample and do not consume extra samples. `--timeout SECS` caps each
sample. `--warmup N` adds an extra discarded run per sample; it is off by default because it loads the model a second
time.

### Knobs

| knob | values | applies to |
| --- | --- | --- |
| `chunk` | `--chunk-values`, default `256,512,1024,2048,4096` | main engine and Gemma 4, GLM, Hy4, Inkling 2 (capped at 256); DeepSeek has no chunk knob |
| `mtp` | `0,1,2,4` | main-engine models with a draft head, otherwise `0` only |
| `short` | 32 plus one threshold just above each `--pp` depth (default `32,129,513,2049`) | main engine only |

Each `short` threshold flips exactly one prompt depth into the short-prompt path: a prompt uses token-at-a-time
prefill when it is shorter than the threshold. The old fixed `16/32/64/128` list was below every default workload
depth, so no candidate changed anything and the value written was noise.

Inkling 2, Hy4, GLM and Gemma 4 read their chunk from the environment at prefill time, so `bench_sample` sets
`STRATA_DS_CHUNK`, `STRATA_GLM_CHUNK` and `STRATA_G4_CHUNK` to the candidate value. Their engines apply a calibrated
chunk the same way at load time. In `serve`, those variables are restored before a different model is loaded, so a
model without a calibration never inherits the previous model's chunk. DeepSeek prefills one token at a time and
reads no chunk: its calibration records no chunk, MTP count or short threshold. The main engine reads `chunk`, `mtp`
and `short` from the file in the usual way.

Multi-GPU runs (pass `--device 0,1`) keep the engine's free-VRAM layer split; calibration does not measure or write a
layer split, and `--split` is ignored with a note.

## Flags

@aletheore aletheore Bot Nov 23, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No longer detected as of df37bb56787d.


🟠 High · Correctness · 2 of 7

--reps multiplies measurements, but the docs say --samples is a hard cap on samples without saying whether reps count toward it. A user sets --samples 24 with the default --reps 3 and expects 24 measurements. If reps are not counted, the run takes three times the expected time. If they are counted, the coarse sweep and descent get only a third of the budget. Either way the budget is misread.

Prompt for your AI agent
File: docs/CALIBRATION.md
Line: 103
Issue: --reps multiplies measurements, but the docs say --samples is a hard cap on samples without saying whether reps count toward it. A user sets --samples 24 with the default --reps 3 and expects 24 measurements. If reps are not counted, the run takes three times the expected time. If they are counted, the coarse sweep and descent get only a third of the budget. Either way the budget is misread.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

| flag | default | meaning |
| --- | --- | --- |
| `--samples N` | 24 | hard cap on benchmark samples |
| `--reps N` | 3 | repetitions per point (their mean is used) |
| `--warmup N` | 0 | extra discarded run before each sample |
| `--pp A,B,C` | `128,512,2048` | prompt depths |
| `--tg N` | 128 | decoded tokens |
| `--chunk-values A,B` | `256,…,4096` | prefill chunks to search |
| `--task LABEL` | `prose` | corpus (repeatable) |
| `--seed N` | 1 | deterministic search seed |
| `--timeout SECS` | 0 (none) | per-sample time limit |
| `--dry-run` | off | print the plan, take no measurements |
| `--out DIR` | — | also write the full sample trace |
| `--mtp-model` / `--draft-model` | — | measure with that draft file loaded |
| `--ctx`, `--kv` | 4096, f16 | conditions of the measurement, not searched |

`--model` also accepts a folder: every `.gguf` in it and its subfolders is calibrated with the same flags; a model
that fails is reported and the rest go on. `--mtp-model` and `--draft-model` need a single model file.

## The `spec` subcommand

`calibrate spec` sweeps speculative settings **per task**: which spec type and how many drafts are best for coding
versus prose on this model.

```sh
rustingstrata calibrate spec \
--model ~/Models/Inkling2/Inkling-2-Mini-UD-Q3_K_XL.gguf \
--draft-model ~/Models/Inkling2/dspark-Inkling-2-Mini-Q8_0.gguf \
--tasks prose,coding --spec-types ngram-simple,ngram-mod,ngram-cache \
--draft-max 4,8,16 --ngram-n 3,6,12 --samples 16
```

Knobs: `spec_type` (`none`, `suffix`, `ngram-simple`, `ngram-map-k`, `ngram-map-k4v`, `ngram-mod`, `ngram-cache`),
`draft_max` (drafts verified per step), `ngram_n` (lookup key length), `mtp` (MTP count, main engine only). The search
stores indices into those lists; the JSON report and the child `bench` command both receive the actual values. The
baseline is today's spec defaults (`none`, 16 drafts, n-gram 12, mtp 0).

The result is written under `spec` in the same calibration file, keyed by task, with decode rate and draft acceptance
percentage of the winner. A spec sweep **only adds that section**: engine knobs measured by a previous `calibrate`
run are left untouched.

## The calibration file

The file keeps the v1 keys the engines read (`chunk`, `mtp`, `short`, `ctx`, `gpu`, `bytes`, `measured`, `aux`), so
older binaries still read it. A stale v1 `split` is deliberately dropped: v2 does not measure a layer split and
nothing writes that key. v2 adds:

```json
{
"schema": 2,
"arch": "qwen35moe",
"chunk": 2048, "mtp": 2, "short": 32,
"ctx": 4096,
"measured": {"prefill_tok_s": 1057.0, "decode_tok_s": 26.7},
"best": {"chunk": 2048, "mtp": 2, "short": 32},
"search": {"samples_used": 24, "score": 1.234, "stop_reason": "budget",
"elapsed_ms": 123456, "seed": 1},
"workloads": [{"pp": 128, "tg": 128, "task": "prose"}],
"spec": {"prose": {"spec_type": "ngram-mod", "draft_max": 8, "ngram_n": 6,
"acceptance_pct": 72.5}}
}
```

`ctx` records the context the measurement ran at; it is never a recommendation applied to later runs. There is no
`kv` or `threads` key from v2 — those remain the user's choice. `--out DIR` writes a separate
`strata-<model>-calib-report.json` with all trials, their knob points, measurements, errors and timings, useful for
auditing a surprising result or comparing two seeds.

The filename encodes the backend that **actually opened** and the device order (`-vulkan`, `-dev0-1`), so a CUDA
to Vulkan fallback writes the file the runs will look for, and calibrations for different setups do not overwrite
each other. As before, `XDG_CACHE_HOME` moves the folder, and a file measured on another GPU or another file of the
same name (different size) is ignored with a warning.
5 changes: 3 additions & 2 deletions docs/MODELS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ The engine picks the code path from `general.architecture` in the file.
| [Hy4-flash](#hy4-flash-iq2_siq2_xxs-85-gb) | `hy_v4_flash` | own forward, matvecs and expert cache on CUDA or Vulkan, greedy only |
| [Inkling 2 Mini](#inkling-2-mini-iq4_nl-176-gb) | `inkling2_loop`, `inkling2` | own forward, matvecs and expert cache on CUDA or Vulkan |

The models with their own forward open the GPU by themselves (no `--gpu` flag), have nothing to calibrate and load
from the chat page of one server like the others. Image input is in [VISION.md](VISION.md).
The models with their own forward open the GPU by themselves (no `--gpu` flag) and load from the chat page of one
server like the others. `rustingstrata calibrate` measures their prefill chunk too (DeepSeek has no chunk to tune);
they read it from the same calibration file at load time. Image input is in [VISION.md](VISION.md).

## Qwen models

Expand Down
32 changes: 14 additions & 18 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ other MCP clients use, and pass it with `--mcp <file>` (or `STRATA_MCP`; the def
differently from one-token decoding, so greedy output can differ at a near tie.
- `--chunk`, `--threads` prefill chunk size and CPU expert threads. On a GPU `--threads` defaults to the physical
core count (SMT siblings only add contention; on the 6-core/12-thread reference machine 6 beats 12 by ~20%).
`rustingstrata calibrate` measures the best chunk and later runs read it from
`~/.cache/rusting_strata/strata-<model>.json` unless flags override.
`rustingstrata calibrate` measures the best chunk, draft count and short-prompt threshold and later runs read them
from `~/.cache/rusting_strata/strata-<model>[-backend][-dev…].json` unless flags override. The suffix uses the
backend that actually opened (so a CUDA to Vulkan fallback writes the Vulkan file). The models with their own
forward are calibrated too; see [CALIBRATION.md](CALIBRATION.md) for the budgeted search and the `calibrate spec`
sweep.
`XDG_CACHE_HOME` moves that folder to `$XDG_CACHE_HOME/rusting_strata` (on Windows it is
`%LOCALAPPDATA%\rusting_strata`). In a container, set it to a mounted path: `docker run --rm` otherwise discards
the file on exit, and the next container runs uncalibrated. The file names the GPU it was measured on, and a file
Expand All @@ -136,9 +139,9 @@ other MCP clients use, and pass it with `--mcp <file>` (or `STRATA_MCP`; the def
expert placement see the VRAM it takes, and picks the draft count. Runs with the same `--mtp-model` or
`--draft-model` file use that count unless `--mtp` is given; runs with another draft file or none do not.
- `calibrate --model ~/models` calibrates every model in a folder (and its subfolders, as the chat page lists them),
one after another, with the other flags applied to each. A model that fails is reported and the rest go on; Inkling
2, Hy4, GLM, DeepSeek and Gemma 4 models are skipped: `calibrate` measures the main engine only, and these models
run on their own code path, which reads no calibration file. `--mtp-model` and `--draft-model` need a single file.
one after another, with the other flags applied to each. A model that fails is reported and the rest go on. All
architectures are included: Inkling 2, Hy4, GLM, DeepSeek and Gemma 4 are measured through their own `bench` path.
`--mtp-model` and `--draft-model` need a single model file.
- `--thinking-budget N` (`chat` and `serve`) ends the thinking block after `N` tokens by inserting `</think>`,
so the model goes on to the answer (0, the default, is no limit). A request overrides it with
`thinking_budget` or `chat_template_kwargs.thinking_budget` (Anthropic: `thinking.budget_tokens`).
Expand Down Expand Up @@ -239,19 +242,12 @@ first, which is not the order of `nvidia-smi`), on Vulkan too; the other Vulkan
integrated ones. The load prints the card of every index (`cuda device 1: ...` or `vulkan device 1: ...`).

The split by free VRAM does not know which card is faster or which sits on a slow PCIe link (x1 riser, x4 slot).
`rustingstrata calibrate --model M.gguf --device 0,1` measures it: it times prefill and decode with the split by free
VRAM, then with layers moved from one card to the other in steps of 15% of the layers, and follows the faster
direction for up to three rounds. A split that is more than 3% faster (prefill tok/s times decode tok/s; each split line prints this product as
a share of the best one so far) is written to the calibration file as layers per card, and later runs with the same
cards in the same `--device` order load with it.
A card with fewer layers keeps more of their experts in VRAM, so fewer are uploaded to it during prefill, and the other
card then keeps fewer of its own: the timings decide. The order of the cards is not searched: to compare, calibrate
`--device 1,0` too. Each order has its own file (`strata-<model>-dev0-1.json`, `-dev1-0.json`), and a run uses the
file of the order it is given.

`--split 30,70` (or `STRATA_SPLIT=30,70`) sets the share of the layers per card by hand, in `--device` order, and
takes precedence over the calibrated split; `calibrate --split 30,70` measures chunk and drafts with that split and
stores it. The load prints each card's layers and its measured host-to-VRAM copy rate. AMD cards clock down in the gaps while the other card works; `echo high >
`--split 30,70` (or `STRATA_SPLIT=30,70`) sets the share of the layers per card by hand, in `--device` order; the load
prints each card's layers and its measured host-to-VRAM copy rate. `rustingstrata calibrate` does not measure or write
a split: it times the chunk, drafts and short-prompt threshold with the split the run will use, and `--split` is
accepted for symmetry but ignored with a note. To compare card orders, calibrate `--device 1,0` too: each order has
its own file (`strata-<model>-dev0-1.json`, `-dev1-0.json`), and a run uses the file of the order it is given.
AMD cards clock down in the gaps while the other card works; `echo high >
/sys/class/drm/cardN/device/power_dpm_force_performance_level` (as root, per card) took a two-card split from 44 to 57
tok/s in one report.

Expand Down
Loading
Loading