core: a control vector loaded from a controlvector GGUF, discovered per model family

A control vector is one unit direction per layer, applied to the residual stream while the model
runs (llama.cpp's --cvec-mode project). This is the loader and its ABI, with no configuration
surface: a vector is a property of the model family, so the engine looks for one beside the
container, in its directory or in $RADIANCE_HOME/vectors, and uses it when it fits the container.
The operation's parameters (layer range, scale, mode) come from the package's own JSON.

Validation is against the container, never the file's provenance: architecture hint, direction
width against n_embd, every layer in the range present, F32 rows. A discovered file that does not
fit is reported and skipped; the switch stays off until a request asks for it.

The ABI grows by one trailing, zero-defaulted field (RadBuildCtx::cvec), so an architecture plugin
that ignores it is unchanged, and the cache gains a salt (0 without a vector) that keeps blocks and
linear-state snapshots of one mode from being reused by the other.
This commit is contained in:
C_Tamas authored and StillDeadcode committed 2026-10-21 09:26:17 +02:00
1 parent aa12f79579
commit 64adee3ece
10 files changed
+859 -2

No files matched your search

+33
View File
@@ -19,6 +19,7 @@
*/
#pragma once
#include "rad_core.h"
#include "cvec.h"
#include <map>
#include <set>
#include "build/startup.h"
@@ -88,6 +89,24 @@ public:
std::string diagnostics_json() const;
bool startup_report_printed() const { return report_printed_; }
/* ---- the control vector's public surface (core/cvec.h).
*
* `cvec_loaded` is whether a vector was found and validated for this container; a request
* asking for the feature on a deployment without one is refused by name rather than silently
* served unprojected. `cvec_default_on` is the runtime default for requests that name no
* choice, and the setter is what POST /admin/control-vector calls: it flips a flag, builds
* nothing, reloads nothing, and leaves every running request alone. */
bool cvec_loaded() const { return cvec_.loaded(); }
bool cvec_default_on() const { return cvec_default_.load(); }
void set_cvec_default(bool on) { cvec_default_.store(on); }
/* {available, default, mode, first, last, n_embd, n_layers, scale, file} for /stats, the
* admin route and the dashboard. */
std::string cvec_json() const;
/* The value the block cache is keyed by for a step with this mode: the vector's identity when
* on, 0 when off. Zero with no vector loaded, so a deployment without the feature hashes
* exactly as before. */
uint64_t cvec_salt_for(bool on) const { return on ? cvec_salt_ : 0; }
private:
/* One thread per tensor-parallel rank, each owning one device. Ranks share the address
* space, so there is no IPC or serialisation and the step batch is shared by pointer rather
@@ -463,6 +482,20 @@ private:
std::string p2p_blocked_;
bool has_ngram_ = false; /* the model carries an n-gram table */
std::string prefix_off_why_; /* why the engine itself turned prefix caching off, or "" */
/* ---- the experimental speed projection (core/cvec.h). `cvec_` is the host table, loaded
* once at bringup and empty when the feature is off; `cvec_views_` is, per rank, that table
* uploaded to the rank's card and the ABI view build_ctx hands to a declaring plugin (the
* view's `dir` is the allocation, so one vector holds both). `cvec_salt_` partitions the
* prefix cache between the mode on and off, so a conversation's KV and linear state are never
* reused by a step that would compute different hidden states from the same tokens. */
ControlVector cvec_;
std::vector<RadControlVector> cvec_views_;
uint64_t cvec_salt_ = 0;
/* Runtime default for requests that name no choice (flippable through
* POST /admin/control-vector). Atomic: the server's admin thread writes it while
* request threads read it; nothing about the graph changes with it. */
std::atomic<bool> cvec_default_{false};
std::vector<std::string> env_set_; /* the RADIANCE_* names set when the engine was made */
bool report_printed_ = false;
/* Runtime device identity (PCI address, name, integrated flag, runtime version), read once in