From f3979aa5381ef392b12260cdf2cfd8dd91405720 Mon Sep 17 00:00:00 2001 From: C_Tamas Date: Tue, 20 Oct 2026 14:39:20 +0700 Subject: [PATCH] docs, tests: say what the branch does, and test what the docs say Review found four places where the branch's prose had outrun its code: - docs/GUIDE.md 4.10 promised that an incompatible sidecar "stops startup" two sentences after promising it is "reported and skipped" (the code warns and serves without it), and claimed deployment-level bit-identity for a request without the vector, which the PR explicitly withdraws. Both now say what the engine does; the wording is core/cvec.h's, which already had it right. - cvec_test claimed discovery order and a range past the model in the PR, but neither case existed. Both are there now, reusing the file writer and load() the file already had, so discovery -- the branch's policy choice -- has coverage at all. - the libr4d row descriptions still said "experimental speed projection" after the naming commit, and pointed at the unit that no longer exists; ref_hc.cpp's comment had a duplicated clause. - /stats' `n_layers` reported the direction count (47 of 48 layers); it is the model's layer count now, with first/last carrying the range. The alias is read before the field's own name, so a request that sends both is read as `control_vector`, and engine.cpp's /server_info comment is back above engine_info. --- core/engine.cpp | 8 +++---- core/server/oai.cpp | 4 +++- docs/GUIDE.md | 10 +++++---- libr4d/r4d_rows.cpp | 14 ++++++------- libref/ref_hc.cpp | 8 +++---- tests/cvec_test.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 20 deletions(-) diff --git a/core/engine.cpp b/core/engine.cpp index bf6cd2c6..92cfc965 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -1630,9 +1630,6 @@ bool Engine::state_slots_of(uint64_t seq, std::vector* out) const { } /* ------------------------------------------------------------------ the HTTP frontend */ -/* The engine's part of /server_info: model, cards, KV, drafting and wire, for a running process. - * The server holds only interfaces, so the engine renders this object and the server splices it in - * verbatim. */ /* The control vector, for /stats and /server_info. `available` is * what a client should test before sending the field: without a loaded vector a request that asks * for it is refused, so a UI can hide the control rather than offer a switch that fails. */ @@ -1644,7 +1641,7 @@ std::string Engine::cvec_json() const { o["first"] = cvec_.first; o["last"] = cvec_.last; o["n_embd"] = cvec_.n_embd; - o["n_layers"] = cvec_.n_layers > 0 ? cvec_.n_layers - 1 : 0; + o["n_layers"] = cvec_.n_layers; o["scale"] = cvec_.user_scale; o["file"] = cvec_.path; o["mode"] = "project"; @@ -1652,6 +1649,9 @@ std::string Engine::cvec_json() const { return o.dump(); } +/* The engine's part of /server_info: model, cards, KV, drafting and wire, for a running process. + * The server holds only interfaces, so the engine renders this object and the server splices it in + * verbatim. */ std::string Engine::engine_info() const { nlohmann::ordered_json e; e["arch"] = meta_.arch_id ? meta_.arch_id : ""; diff --git a/core/server/oai.cpp b/core/server/oai.cpp index 06b539c9..20cbd947 100644 --- a/core/server/oai.cpp +++ b/core/server/oai.cpp @@ -847,7 +847,9 @@ static int parse_sampling(const json& b, const OaiLimits& lim, OaiRequest* r, Ap * turns it on when the deployment's default is off. `control_vector` is the field's name; * `experimental_speed_projection` is Strata's spelling of the same switch, kept as an alias so * a client written against that reports what it means. */ - for (const char* key : { "control_vector", "experimental_speed_projection" }) { + /* The field's own name is read last, so a request that sends both spellings is read as + * `control_vector`; the alias is Strata's name for the same switch. */ + for (const char* key : { "experimental_speed_projection", "control_vector" }) { if (!b.contains(key)) continue; bool on = false; if (!get_bool(b, key, &on, err)) return RAD_E_INVAL; diff --git a/docs/GUIDE.md b/docs/GUIDE.md index c7ba3a56..27e4a55f 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -1461,14 +1461,16 @@ default for *requests* stays off in all cases. - **Only a compatible container accepts it.** The file must be a GGUF `controlvector` whose `controlvector.model_hint` (when present) is this model's architecture (`qwen4exp`), whose directions are exactly the model's `--n-embd` wide, and whose range lies inside its layers. - Anything else stops startup with the reason: a direction from another model's residual basis - would move hidden states along a meaningless axis. + Anything else is reported and skipped, never served: a direction from another model's residual + basis would move hidden states along a meaningless axis. - **It is off until a request asks for it.** The dashboard's Model view flips the default for requests that name no choice, and a request's own `control_vector` field always wins. **No switch reloads the model**: the directions are uploaded once at startup and each step carries a per-request row mask. -- **A request without it computes exactly what it computes with no vector loaded** — the op skips - a disabled row without reading or writing it. +- **The op is a no-op when off** — it skips a disabled row without reading or writing it, so an + unprojected request's stream is the one the stock model computes. What a deployment that merely + *offers* a vector does not promise is bit-identity with one that has none: the table's VRAM can + move an expert or a KV block, the tie-level variance radiance documents elsewhere. - **Conversations are partitioned.** Blocks and linear-state snapshots computed with the vector on are keyed differently from those computed without it, so switching modes in one conversation recomputes instead of reusing hidden states that belonged to the other mode. A runtime LoRA diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 59f47cbc..822d32ff 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -480,8 +480,8 @@ static const RadOperandSpec oGdnConvRecurrentUpdate[] = { /* The gated residual. `hc_enter` and `hc_write` share a parameter list; `hc_read` adds the two * switches its operand list depends on: `inject` 0 is the final mixer and `group` 0 a read whose * consumer wants bf16. Both default on, as every trunk connection uses both. */ -/* The experimental speed projection (libr4d/r4d_hc_project.hip): a bf16 stream, one unit f32 - * direction per layer, an int32 switch per row. */ +/* The control vector's projection (r4d_hc_bf16.hip, with the other gated-residual kernels): a bf16 + * stream, one unit f32 direction for every stream, an int32 switch per row. */ static const RadParamSpec pHcProject[] = { P_INT("M"), P_INT("n"), P_INT("hc"), P_F64("scale"), P_STR("dtype") }; static const RadOperandSpec oHcProject[] = { @@ -2945,11 +2945,11 @@ static const RadOpSchema kSchemas[] = { "the mean of the gated streams -- plus the per-branch write gains and, optionally, " "the block-scaled fp8 form of the block input and its Hadamard-rotated twin."), SCHEMA("hc_project", pHcProject, oHcProject, - "the experimental speed projection (llama.cpp's `--cvec-mode project`): for each of the " + "the control vector's projection (llama.cpp's `--cvec-mode project`): for each of the " "hc streams of a row whose `on` flag is 1, h[s] -= scale * (h[s] . v) * v, v that " - "layer's unit direction and scale its stored norm. A row whose flag is 0 is neither read " - "nor written, so an unprojected request is byte-identical to a deployment with no " - "vector loaded."), + "layer's unit direction -- the same v for every stream -- and scale its stored norm. A " + "row whose flag is 0 is neither read nor written, so an unprojected request's stream is " + "the stock model's."), SCHEMA("hc_write", pHcPlain, oHcWrite, "the write half: h[s] += inj[s] * y, into the UNNORMED stream."), SCHEMA("mtp_enter", pMtpEnter, oMtpEnter, @@ -6527,7 +6527,7 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, r4d_shape_hc_write }, { "hc_project_bf16", "hc_project", "norm", - "speed projection: h[s] -= scale * (h[s] . v) * v where the row's flag is on", + "control vector: h[s] -= scale * (h[s] . v) * v where the row's flag is on", "any M, any n, hc 2..8", "bf16 stream, f32 direction, i32 switch", RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, diff --git a/libref/ref_hc.cpp b/libref/ref_hc.cpp index 1dee79f2..f7780318 100644 --- a/libref/ref_hc.cpp +++ b/libref/ref_hc.cpp @@ -262,10 +262,10 @@ int ref_hc_write(const RadArgs* a, RadStream) { * patch): the dot is taken per hyper-connection stream, as `mul_mat` over a [n, hc, tokens] view * gives it, and not on the stream mean. * - * A row whose flag is 0 is not touched: no read, no write, so a request that did not ask for the - * vector's activations are the stock model's: no read, no write. (A deployment that merely *has* the - * table loaded is not bit-identical to one without it -- the table's VRAM moves the expert/KV plan -- - * but that is placement, not this op.) + * A row whose flag is 0 is not touched: no read, no write, so an unprojected request's activations + * are the stock model's. (A deployment that merely *has* the table loaded is not bit-identical to + * one without it -- the table's VRAM moves the expert/KV plan -- but that is placement, not this + * op.) * * `on` is an int32 per row (core derive.cpp's `projection_on_rows`); the direction is f32. */ int ref_hc_project(const RadArgs* a, RadStream) { diff --git a/tests/cvec_test.cpp b/tests/cvec_test.cpp index c2a422f6..8e703ae0 100644 --- a/tests/cvec_test.cpp +++ b/tests/cvec_test.cpp @@ -238,4 +238,55 @@ TEST(a_unit_vector_gives_scale_one_at_every_steered_layer) { CHECK_NEAR(cv.scales[(size_t) 44], 1.0f, 1e-5); } +/* Discovery is the branch's policy -- a file beside the model makes the capability available -- so + * the order it searches in is part of the contract: the container's sidecar, then its directory, + * then each `$RADIANCE_HOME/vectors`, with an empty file skipped rather than reported as found. */ +TEST(discovery_finds_the_sidecar_then_the_directory_then_the_home) { + const std::string dir = radtest::tmpdir(); + const std::string model = dir + "/model.gguf"; + const std::string home = dir + "/home"; + const std::string hvec = home + "/vectors/qwen4exp-control-vector.gguf"; + std::string found, tried; + + /* Nothing anywhere: no answer, and the candidates are named for the log line. */ + CHECK_OK(rad::control_vector_discover(model, "qwen4exp", home, &found, &tried)); + CHECK(found.empty()); + CHECK(tried.find(dir + "/model.control-vector.gguf") != std::string::npos); + + /* A home's vectors/ entry is the last candidate, and an empty file there is not a vector (the + * loader would refuse it), so discovery keeps looking rather than reporting it. */ + std::filesystem::create_directories(home + "/vectors"); + std::ofstream(hvec, std::ios::binary).close(); + CHECK_OK(rad::control_vector_discover(model, "qwen4exp", home, &found, &tried)); + CHECK(found.empty()); + make_vector(2560, 47).write(hvec); + CHECK_OK(rad::control_vector_discover(model, "qwen4exp", home, &found, &tried)); + CHECK_EQ(found, hvec); + + /* The container's directory beats every home; the sidecar beats the directory. */ + make_vector(2560, 47).write(dir + "/control-vector.gguf"); + CHECK_OK(rad::control_vector_discover(model, "qwen4exp", home, &found, &tried)); + CHECK_EQ(found, dir + "/control-vector.gguf"); + make_vector(2560, 47).write(dir + "/model.control-vector.gguf"); + CHECK_OK(rad::control_vector_discover(model, "qwen4exp", home, &found, &tried)); + CHECK_EQ(found, dir + "/model.control-vector.gguf"); + + /* Homes separated by `:` are searched in order: one without a vector defers to the next. */ + const std::string dir2 = dir + "/two"; + std::filesystem::create_directories(dir2 + "/vectors"); + make_vector(2560, 47).write(dir2 + "/vectors/qwen4exp-control-vector.gguf"); + CHECK_OK(rad::control_vector_discover(dir2 + "/m.gguf", "qwen4exp", dir + "/none:" + dir2, + &found, &tried)); + CHECK_EQ(found, dir2 + "/vectors/qwen4exp-control-vector.gguf"); +} + +/* The range is the package's, and it must lie inside the container. Twenty directions fit a + * 32-layer model; a JSON naming 4..40 does not. */ +TEST(a_layer_range_past_the_model_is_refused) { + const Loaded l = load(make_vector(2560, 20), 2560, 32, "range-past.gguf", + R"({"mode":"project","scale":1.0,"layer_range":[4,40]})"); + CHECK(l.rc < 0); + CHECK(l.err.find("runs past") != std::string::npos); +} + RAD_TEST_MAIN()