Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0183b3a
Control vectors: read a GLP add vector and refuse what it cannot apply
BlivionIaG Nov 10, 2026
ad1789e
Control vectors: add the direction inside the residual norm on Qwen3.…
BlivionIaG Nov 10, 2026
04575f3
Control vectors: --glp FILE and --glp-alpha serve one on Qwen3.8-27B
BlivionIaG Nov 10, 2026
5d50702
steering: isolate saved states and reject malformed strengths
BlivionIaG Nov 10, 2026
3d58707
contract: the steering launch declares its partials' threadgroup memo…
BlivionIaG Nov 13, 2026
38910c3
server: serve flags share cli_flag's one Flag type
BlivionIaG Nov 23, 2026
3ff4a1e
qwen27: keep the served host under the lean ceiling
BlivionIaG Nov 23, 2026
1e6fc11
glp: bound container reads and report only the parsed digest
BlivionIaG Nov 23, 2026
11eb21c
kernel contract: keep the steer suite alive and compile the norm as s…
BlivionIaG Nov 23, 2026
63fad25
glp: guard the checker, refuse Gemma first, and escape the served report
BlivionIaG Nov 23, 2026
dd0755b
glp: supply the layers field in the digest load test
BlivionIaG Nov 23, 2026
884a03b
tests: the capability audit keeps the platform bit and the forced bui…
BlivionIaG Nov 24, 2026
09eb735
glp: the MTP head's block reads rows the target already steered
BlivionIaG Nov 24, 2026
ac7aa93
Round the Qwen steering product on its own before the add
BlivionIaG Nov 25, 2026
8a4cc7e
changelog: GLP entries under Unreleased once
BlivionIaG Nov 25, 2026
7f26fd8
glp: a present spec_version that is not an integer is refused, not re…
BlivionIaG Nov 27, 2026
1984d79
contract: the qwen27 launch source keeps main's dispatch, one branch …
BlivionIaG Nov 27, 2026
c5cacc6
glp: pin the served lane's source after the merge
BlivionIaG Nov 27, 2026
a422aec
glp: enforce Qwen revision check, refuse v1 scales, preserve -0.0
BlivionIaG Nov 28, 2026
d354db2
qwen27 steer contract: add deterministic zero-step device rows and sk…
BlivionIaG Nov 28, 2026
b3d6711
glp: refuse wrong-type base identity metadata
BlivionIaG Nov 29, 2026
1cfcf68
glp: refuse non-string hook_point, derived_at, and layer_ids_zero_based
BlivionIaG Nov 29, 2026
a73ff68
runtime sources: register the Qwen steering norm
ashhart Nov 25, 2026
1a8a2cb
changelog: #1835 under Unreleased
ashhart Nov 29, 2026
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- Metal: opt-in GLP additive control vectors for Qwen3.8-27B through `--glp FILE` and `--glp-alpha`, in the GLP format of the `weightless` project; startup reports the escaped filename, the file's SHA-256 and that replies are steered, other modes and malformed files are refused, saved conversation states carry the file hash and strength in their identity, and output is unchanged without `--glp` (#1835, fixes #1834). Thanks @BlivionIaG.

- Metal contracts, Qwen3.5 CUDA drafts and Qwen27 lanes: four more busy 600-line sources split into their own files, leaving room under the cap; no call site changes (#5595).

- Inkling 2 Small EXL3 on Metal: the non-looped 48-layer forward, its Metal ops with kernel-contract gates and launch-limit records, and disk-pack loader and oracle tests; Inkling 2 Mini is unchanged (#5105). Thanks to @trozzelle.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ See [the chat command](docs/chat.md) for its flags and the serve flags it passes
Cortheon mode, every serve flag, the Mac app and TensorFold Starlight.
- [Supported models](docs/supported-models.md): which checkpoints run on which machines, models on disk and model discovery.
- [Living Weights (experimental)](docs/living-weights.md): teach a running model new facts in its own weights.
- [Control vectors](docs/control-vectors.md): steer Qwen3.8-27B on Metal with `--glp FILE` and `--glp-alpha ALPHA`, the vector file's format and how one is brought.
- [Installation runbook](RUNBOOK.md): macOS and CUDA installation, paired Metal serving and troubleshooting.
- [Windows](docs/WINDOWS.md), [TensorFold Web](docs/web.md) and [the Mac app](app/README.md).
- [API](docs/api.md), [Cortheon mode](docs/cortheon.md) and [known issues](docs/known-issues.md).
Expand Down
4 changes: 4 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ pub fn build(b: *std.Build) void {
@import("zig/build/quant_geometry_host.zig").steps(b, test_step);
@import("zig/build/glm_tfq3_host.zig").steps(b, test_step);
@import("zig/build/stage.zig").steps(b, test_step);
@import("zig/build/glp.zig").tests(b, test_step);
@import("zig/build/glp_steer.zig").tests(b, test_step);
@import("zig/build/glp_serve.zig").tests(b, test_step);
@import("zig/build/steer.zig").tests(b, test_step);
@import("zig/build/dsv41_metal.zig").hostSteps(b);
if (b.graph.host.result.os.tag == .macos) {
const step5_mods = metalEngineModules(b, b.graph.host, .Debug, draft_ids);
Expand Down
213 changes: 213 additions & 0 deletions docs/control-vectors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
# Control vectors

A control vector steers a model's residual stream at inference time. It is a
direction per layer: at the output of a block, the direction, scaled by the
strength, is added to the residual stream as a bias. Nothing is written to the
checkpoint. Steering is disabled unless the server starts with `--glp`.

TensorFold reads these vectors on Metal for Qwen3.8-27B. The container, the
kernel and the serve surface are all native Zig. The engine ships the mechanism
only: a vector file is supplied by the operator, and the repository carries no
derived direction files.

## Serving one

```
tensorfold serve TensorFold/Qwen3.8-27B-MLX-4bit --glp vector.gguf --glp-alpha 1.0
```

`--glp` names the file and `--glp-alpha` the strength. Without `--glp-alpha`
the file's own `glp.alpha_default` applies. The applied fp32 strength must be
finite and above 0.

At startup the server names the file it loaded, the SHA-256 of that file's
exact bytes, and says that replies are steered.

The file name is written escaped, so a name with a newline or a quote cannot
add a line of its own. The file hash is taken over the bytes the server read
once, before the model loads; it is the hash to compare against the file you
meant to load. The tensor digest is the separate SHA-256 over the direction
tensors that a file may claim in `glp.content_sha256`. The engine repeats that
replies are steered, with the file hash, when the vector is in force.

Saved conversation states also include that file hash and the applied strength
in their identity. Changing either starts those states cold; without `--glp`
the existing policy key is unchanged.

A file the server cannot apply is refused and the server does not start.

## Bringing a vector

The repository carries none, and this page names none to download. A vector is
derived by the operator, for the style or topic they want a model to lean
toward, and the prerequisites are theirs to meet:

- Two sets of prompts that differ in the one thing the vector should carry,
and activations collected from Qwen3.8-27B (the 5120-wide model) at the
output of its blocks on both sets.
- One direction per layer from those activations, for example the difference
of the two sets' mean activations, in fp32. llama.cpp's `cvector-generator`
writes this container; any tool that writes the format below works.
- Layer ids of 1 to 63 for the 64-block model, zero-based (see below), in a
file whose `glp.base_model`, if present, names the checkpoint served.

The strength is the operator's to find on their own prompts: a direction added
too strongly degrades the text, and one added too weakly changes nothing. The
reader does not judge either.

## Add only

A vector is applied by adding the direction to the residual stream. A file
whose mode is missing is an `add` file, as the format specifies, so a llama.cpp
control vector works as it stands. A file that names another mode is refused,
`project` included, and is never read as an `add` file.

## What the server refuses

The reader fails closed rather than guessing.

| Refused | Why |
| --- | --- |
| `glp.mode` (or `dspark.mode`) is anything but `add` | Only add vectors are applied. A file that names `project` is refused by name, not coerced to `add`. |
| `glp.hook_point` is not `residual_stream_post_layer` | The vector was derived at another site. |
| `glp.rank` is not the integer 1 | The reader implements rank 1. |
| `glp.spec_version` is not 1 | The reader implements version 1. |
| `glp.alpha_default` is not a floating-point value | A malformed strength is not silently replaced with 1. |
| A direction holds a NaN or an infinity | The direction is added as it stands, so every value has to be finite. |
| The digest does not match the bytes | The file says one thing and holds another. |
| `glp.content_sha256` is not text | A digest that cannot be read is not reported as verified, so the file is refused instead. |
| A tensor's offset or byte count runs past the file | The bounds are checked before the bytes are read, and a value that would wrap is refused, not wrapped. |
| A metadata array nests deeper than four | The reader bounds its own recursion; no control vector names an array inside an array this deep. |
| One layer is named twice | `direction.5` and `direction.05` both parse as layer 5; a file has one direction per layer. |
| A layer id is past the checkpoint's depth | The checkpoint's `num_hidden_layers` is read at startup, so the file is refused before the model loads. |
| The width does not match the checkpoint | The vector is for another model. |
| The base model does not match | The vector was derived from another checkpoint. The names are compared without their owners, because a checkpoint is often a quantization of another repo's model under a new name. |
| A Gemma checkpoint | A Gemma is never steered. |
| A CUDA or ROCm backend | Only the Metal engine steers. |
| Any other model family | Only Qwen3.8-27B takes a control vector. |

A file whose `glp.derived_at` differs from its `glp.hook_point` is a warning,
not a refusal: the vector is applied at the residual stream either way.

## The layer a direction names

`direction.N` is the layer index `N`, and the layers are zero-based: `direction.N`
is the output of the N-th block, the residual stream as that block leaves it.
Layer 0 is not steerable, so a file for a 64-block model names layers 1 to 63.

This is worth stating because the alternative is silently wrong. The container
follows llama.cpp, and llama.cpp's own generator writes `direction.{il+1}` while
its applier reads `direction.il`: the two cancel, so the applier's numbering is
the one a file means. An off-by-one here does not fail loudly either, because
adjacent layers' directions are highly correlated and a stack shifted by one
layer still produces plausible output.

The file says so itself. A vector for this model can carry its layer list in
its metadata:

```
glp.layer_ids_zero_based = 10,11,12,...,58
```

and this reader refuses a file whose tensor names and that list disagree.

The direction lands on the residual stream after a block, and the block that
follows reads the result: the direction is added before the next block's
normalisation sees the stream.

## How the bias is applied

A steered layer takes one dispatch, the same one an unsteered layer takes: the
norm that adds the block's output to the residual stream also adds the
direction, scaled by the strength, to the row, and the normalisation that
follows reads the result. There is no separate pass over the residual row.

The arithmetic is pinned by the kernel contract
(`zig/src/core/contract/steer.zig`): a float64 reference and the bound the
kernel's own fp32 arithmetic is checked against. The GPU half of that contract
(`zig/tests/contract/metal_qwen27_steer.zig`) needs a Mac and has to be run
there; this page carries no timing or output measurement for it.

## The file

The container is GGUF v3, read by `zig/src/core/glp_container.zig`, the GLP
reader for the subset a vector needs. The metadata follows the GLP standard:

| Key | Meaning |
| --- | --- |
| `glp.spec_version` | 1. |
| `glp.mode` | `add`, or absent. |
| `glp.rank` | 1. |
| `glp.hook_point` | `residual_stream_post_layer`. |
| `glp.derived_at` | The site the direction was captured at. It is a site, not a time: the format's own validator refuses a value that is not a hook name, and a file whose `derived_at` differs from its `hook_point` is a transferred vector, which is warned about. |
| `glp.created` | The day the file was written. |
| `glp.alpha_default` | The strength the file applies with by default. |
| `glp.method` | How the numbers were made, for example `difference_of_means_last_token_per_layer`. |
| `glp.contrast` | What the two prompt sets contrast. |
| `glp.base_model`, `glp.base_revision` | The checkpoint the vector came from. |
| `glp.layer_ids_zero_based` | The layers the file holds. |
| `glp.content_sha256` | SHA-256 over the tensor bytes, which the reader checks. |

The tensors are `direction.N`, fp32, one per layer, each the width of the
residual stream.

The file also says what it is, for a consumer that reads the header before it
reads the directions: `general.name`, `general.description`, `general.author`,
`general.license`, `general.base_model.*` (the repo it came from), and
`controlvector.model_hint` and `controlvector.layer_count`. The reader keeps
these when the file carries them; `tf-glp-check rewrite` writes a file back out
with the fields it read.

### The checkpoint the file names

`glp.base_model` is the checkpoint the vector came from, and the server refuses
a vector whose checkpoint is not the one it is serving. The check compares
names: the served checkpoint's model name has to be the vector's model name or
start with it, so a conversion published under a longer name takes the vector
and a different model does not.

The same weights under another name are refused with it. A file can be
re-stamped for that name:

```
tf-glp-check rewrite vector.gguf vector-mlx.gguf \
--repo mlx-community/Qwen3.8-27B-4bit --revision REV
```

The directions stay the same bytes and the digest over them does not move; only
the name the file carries changes. That is a claim the operator makes, not one
the reader verifies: the check is a name comparison, so only re-stamp a file
for a checkpoint that holds the same weights.

## The integration test

A file that one implementation accepts has to mean the same thing to another.
The GLP format is defined by the `weightless` project (@msuiche); its
`docs/spec/GLP.md` at commit `b181a07d` is the specification this reader
implements, and `plugins/vllm/weightless_steer/container.py` at the same commit
is the container reader the parity test compares against. This repository does
not vendor either: the test takes a checkout of that commit and reads the
reader as a package.

```
zig build tf-glp-check
python3 tools/zig/glp_parity.py --reference PATH/TO/weightless \
--vector vector.gguf --vector PATH/TO/another-vector.gguf
```

Four legs, over the same files:

| Leg | What it checks |
| --- | --- |
| read | The reader in Zig and the reference reader on one file: the layer ids, the metadata and every direction's bytes are equal. |
| rewrite | The file read in Zig and written out again, read by the reference: the numbers, the hook point and the strength survive the round trip. |
| digest | The file's own `glp.content_sha256` over the tensor payloads, hashed the same way on both sides. |
| file hash | The SHA-256 the reader reports for the file's bytes is Python's SHA-256 of the same file. |

The reader that does the Zig half is `tf-glp-check`, which prints the reader's
view of a file and writes its directions out as bytes, so the two sides can be
compared exactly rather than approximately.

The test needs `numpy` for the reference reader, and a `weightless` checkout at
commit `b181a07d`. Nothing of the reference is committed here, and no vector is
either: the file is supplied when the test runs.
9 changes: 9 additions & 0 deletions docs/run-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ The read-only [`/memory` diagnostics](memory-diagnostics.md) report process foot
available backend allocation counters, and the applied retained-prefix plan without changing memory policy.

The binary's `capabilities --json` response lists its supported flags and platform-specific values.
The GLP flags are left out where the build cannot serve them (everything but Metal on macOS), though they still parse there so their refusal names the engine.
`serve MODEL --help` prints usage without loading a model.

| Flag | Meaning |
Expand Down Expand Up @@ -154,6 +155,8 @@ The binary's `capabilities --json` response lists its supported flags and platfo
| `--drafter DRAFTER` | An external drafter, as a directory or a repo id in the Hugging Face cache, for models that take one (Gemma 4 31B's recipe names its drafter); `none` serves without one. Without the flag, Qwen3.8-27B pairs DFlash2 when the cache holds it (never downloaded, not on HIP): one line names it, and serve runs without it if it does not open. |
| `--drafter-bits 0\|4` | Drafter weight precision where the model offers a choice: 4 bits (the default), or 0 to keep bf16. Qwen3.8-27B on CUDA takes 4 only. |
| `--record DIR` | Write each stream's committed tokens, positions and tapped rows under DIR, for drafter training. CUDA Nemotron only; a single stream runs through the lane rounds, and recording stops below 25 GiB free disk. |
| `--glp FILE` | A GLP control vector whose directions are added to the residual stream, one per layer; off unless given. Qwen3.8-27B on Metal takes one; the file's width and base model must match the checkpoint, and a Gemma checkpoint, CUDA and ROCm are refused. Only add vectors are applied. |
| `--glp-alpha ALPHA` | The strength the vector applies with, above 0; the file's own `glp.alpha_default` by default. |
| `--keep-warm SECONDS` | Keep the Metal GPU active while idle for this long after a request, default 900; zero disables it. |
| `--cfg-gamma GAMMA` | Drama speech on Metal and CUDA: semantic guidance strength, finite 1–5; 0/default disables it. Only reference-conditioned requests are guided. Enabled replies report `X-Cfg-Gamma`; codec heads stay conditional. |
| `--cfg-allow-request-gamma` | Drama speech on Metal and CUDA: allow a finite 1–5 `X-Cfg-Gamma` request header; invalid values return 400 before engine submission. Without this opt-in, the server's gamma is fixed. |
Expand Down Expand Up @@ -206,6 +209,12 @@ a long conversation. Where the cache cannot keep a whole context, the server als
at each prompt's history end, so a turn whose template drops the last reply's reasoning resumes there. Replies are
the same tokens as a fresh prompt pass; `TF_QWEN27_SLOT_RESUME=0` turns both off.

`--glp` reads its file once at startup, and the startup line names the file (escaped), the SHA-256 of its exact bytes and
that replies are steered, with the strength in force, the layers the file names, the checkpoint the vector was derived from
and whether the file's own tensor digest matched; a vector derived at another site is reported as a warning, not refused.
The [control vector guide](control-vectors.md) covers the format, the layer a direction names, the files the server
refuses and how a vector is brought.

Sampling defaults come from the checkpoint's `generation_config.json`, then serve flags and request fields override them.
`--context 0` is family-specific; use a positive limit for GLM and inspect the capacity reported at startup.
The supported environment variables are listed in `capabilities --json`, including API keys, request logging and Hugging Face cache paths.
Expand Down
Loading
Loading