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

+63
View File
@@ -895,6 +895,12 @@ RadBuildCtx Engine::build_ctx(int rank, int64_t max_tok) const {
/* Under --prefill-span every step but a lone prefill is planned at the shared budget
* (Scheduler::step), so a wider step is one sequence; 0 without a span. */
bc.max_mixed_tok = cfg_.prefill_span > 1 ? std::min(max_tok, prefill_shared_tok(cfg_)) : 0;
/* The control vector, when one is loaded: this rank's view, or null. A plugin reads it at
* declare only (the direction table never changes); the per-request switch rides the derived
* mask, not this struct. */
if (cvec_.loaded() && rank >= 0 && rank < (int) cvec_views_.size() &&
cvec_views_[(size_t) rank].dir)
bc.cvec = &cvec_views_[(size_t) rank];
return bc;
}
@@ -1063,6 +1069,52 @@ int Engine::declare() {
* a serial loop times out. Registry::select() and init_instance() share a mutex for this. Each
* thread binds its device first, because the current device is thread-local and the init
* allocates on it. */
/* Control vectors are resolved here, before any rank declares: the plugin reads the direction
* table from RadBuildCtx, so a table that failed validation must stop the server before a graph
* is built around it.
*
* There is nothing to configure. A vector is a property of the model family, so the engine
* looks for one beside the container (or in the directory, or in $RADIANCE_HOME/vectors) and
* uses it if it is compatible with this container; its operation parameters come from the
* package's own JSON. A file that does not fit is reported and skipped -- a sidecar must never
* stop a server nobody pointed at it -- while the switch itself stays off until a request (or
* the dashboard) asks for it. */
const std::string arch = meta_.arch_id ? meta_.arch_id : "";
if (!arch.empty()) {
std::string proj_path, tried;
(void) control_vector_discover(cfg_.model, arch, cfg_.radiance_home, &proj_path, &tried);
if (proj_path.empty()) {
RAD_INFO("control vectors: none found (%s)", tried.c_str());
} else if (arch != "qwen4exp") {
RAD_WARN("control vectors: %s is for this family only (qwen4exp, whose gated residual "
"carries the projection op); this container is '%s' -- ignoring it",
proj_path.c_str(), arch.c_str());
} else {
std::string why;
const int rc = control_vector_load(proj_path, arch, meta_.n_embd, meta_.n_layers,
&cvec_, &why);
if (rc != RAD_OK) {
RAD_WARN("%s -- serving without it", why.c_str());
cvec_ = ControlVector{};
}
}
}
if (cvec_.loaded()) {
cvec_views_.assign(ranks_.size(), RadControlVector{});
cvec_salt_ = control_vector_salt(cvec_);
cvec_default_.store(false);
RAD_INFO("control vectors: %s: %lld directions of %lld, layers %d..%d, scale %.3f -- "
"off until a request asks for it (the dashboard's switch, or a request's `control_vector` "
"field)",
cvec_.path.c_str(), (long long) (cvec_.n_layers > 0 ? cvec_.n_layers - 1 : 0),
(long long) cvec_.n_embd, cvec_.first, cvec_.last, (double) cvec_.user_scale);
decide("control vectors", cvec_.path,
"found beside the container, in its directory, or in $RADIANCE_HOME/vectors; "
"requests opt in");
} else {
cvec_default_.store(false);
}
std::vector<int> st(ranks_.size(), RAD_OK);
std::vector<std::thread> th;
th.reserve(ranks_.size());
@@ -1076,6 +1128,17 @@ int Engine::declare() {
st[(size_t)ri] = RAD_E_DEVICE;
return;
}
/* This rank's copy of the control vector's directions, on the device the thread has
* just bound. Uploaded outside the program's arena: it is not a weight (it is not in
* the container) and not an activation (it lives for the deployment). */
if (cvec_.loaded()) {
const float* d = control_vector_upload(cvec_, ri);
if (!d) {
st[(size_t)ri] = RAD_E_DEVICE;
return;
}
cvec_views_[(size_t)ri] = control_vector_view(cvec_, d);
}
const RadBuildCtx bc = build_ctx(ri, cfg_.max_tok);
LoraDeclare ld;
ld.cfg = &cfg_.lora;