From ad7c962375338ac8151b079a2ae1d349bfe3a6c5 Mon Sep 17 00:00:00 2001 From: C_Tamas Date: Tue, 20 Oct 2026 14:39:20 +0700 Subject: [PATCH] core: only the package's layer range steers, and the table stays out of the budget The loader stored a norm for every direction in the file, and the plugin declares an op wherever a layer's scale is nonzero, so `layer_range` (4..44, the package's own selection) reached only the range checks and the log line: the engine steered all 47 directions -- 94 hc_project instances in the declared graph where the sources and the docs say 41. The scale is zeroed outside the range now, which is what RadControlVector documents ("a zero scale means this layer is not steered") and what llama.cpp's patch does (its tensor is absent outside the range). cvec_test asserts it for the default and for a JSON-stated range; --debug-graph shows 41 a rank. The direction table is also taken back out of the VRAM budget (VramFacts::held_out). It is allocated in declare(), before vram_budget_resolve reads free VRAM, so without the term the pools are planned around bytes the program never sees. On this box the split came out identical either way (claimable 12.92 GiB in both), so the term is a guarantee by construction rather than a fix for an observed miss -- and it keeps a deployment that merely offers a vector on the same plan as one that does not. --- core/cvec.cpp | 5 ++++- core/engine_bringup.cpp | 5 +++++ core/mem/vram_budget.cpp | 9 +++++++-- core/mem/vram_budget.h | 6 ++++++ tests/cvec_test.cpp | 9 ++++++--- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/core/cvec.cpp b/core/cvec.cpp index 262b40ae..b2c2840e 100644 --- a/core/cvec.cpp +++ b/core/cvec.cpp @@ -254,7 +254,10 @@ int control_vector_load(const std::string& path, const std::string& arch, int64_ float* dst = &out->dir[(size_t) (l * n_embd)]; const float inv = (float) (1.0 / norm); for (int64_t i = 0; i < n_embd; ++i) dst[i] = d[i] * inv; - out->scales[(size_t) l] = (float) norm * scale; + /* Zero outside the range: a layer is steered exactly when its scale is nonzero + * (abi/rad_builder.h, RadControlVector), and a file carries directions for layers the + * package's JSON leaves out. The row is kept so a range change needs no new upload. */ + out->scales[(size_t) l] = (l >= first && l <= last) ? (float) norm * scale : 0.0f; /* A layer inside the range with no direction would silently be unsteered. */ if (l >= first && l <= last) worst = std::max(worst, std::fabs(norm - 1.0)); diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index d286b011..77517438 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -1510,6 +1510,11 @@ int Engine::plan() { const int64_t ck_cap = INT64_MAX / 8; const int64_t ck_store = !cfg_.prefix_cache || ck_slot <= 0 ? 0 : ck_n > ck_cap / ck_slot ? ck_cap : ck_slot * ck_n; + /* The direction table was allocated in declare(); it is not part of the program, and + * claiming its bytes back keeps a deployment that offers a vector planning against the + * same card as one that does not (VramFacts::held_out). */ + const int64_t cvec_bytes = cvec_.loaded() ? (int64_t) cvec_.dir.size() * 4 : 0; + for (VramFacts& f : facts) f.held_out = cvec_bytes; RAD_TRY(resolve_table_placement(facts, emb_bytes, wfs, kv_floor, ngram_bytes, ngram_device, ngram_host, ck_store + host_arenas)); /* The n-gram table was counted off the card; on it, every rank holds the whole table. */ diff --git a/core/mem/vram_budget.cpp b/core/mem/vram_budget.cpp index 844cca7e..75932168 100644 --- a/core/mem/vram_budget.cpp +++ b/core/mem/vram_budget.cpp @@ -93,8 +93,13 @@ static int resolve(Config& cfg, const std::vector& per_rank, VramBudg /* The only measured term: the driver, loaded code objects and anything else on the card (e.g. * a desktop compositor), so it is reported on its own line rather than folded into the - * headroom. Read after declare(), once every kernel plugin's code object is resident. */ - b.already_held = std::max(b.capacity - freeb, 0); + * headroom. Read after declare(), once every kernel plugin's code object is resident. + * + * `held_out` is taken back out: it is the engine's own allocation that the program does not + * see (core/cvec.h's direction table), which the headroom already covers. */ + int64_t held_out = 0; + for (const VramFacts& f : per_rank) held_out = std::max(held_out, f.held_out); + b.already_held = std::max(b.capacity - freeb - held_out, 0); /* What is left after the fixed terms. */ b.drift = kDriverDrift; diff --git a/core/mem/vram_budget.h b/core/mem/vram_budget.h index 21f80092..19b9aca5 100644 --- a/core/mem/vram_budget.h +++ b/core/mem/vram_budget.h @@ -80,6 +80,12 @@ struct VramFacts { * or auto), not --deterministic, on a device with virtual memory management. A lending cache * can be sized for what live requests could need; a non-lending one would hold it empty. */ bool lends = false; + /* A loaded control vector's direction table: device bytes the engine already holds, which are + * not part of the program or the pools and must not shrink what the plan may claim. It comes + * out of the headroom, as every deployment keeps that unclaimed. Without this, a deployment + * that merely *offers* a vector would plan its expert cache against a smaller card than one + * that does not, which is exactly the difference "off == stock" cannot allow. */ + int64_t held_out = 0; /* Integrated graphics: host memory the card must leave to the host, since device memory is * host memory there. 0 on a discrete card. */ int64_t host_reserve = 0; diff --git a/tests/cvec_test.cpp b/tests/cvec_test.cpp index c65cb433..de039d3d 100644 --- a/tests/cvec_test.cpp +++ b/tests/cvec_test.cpp @@ -146,10 +146,11 @@ TEST(the_published_shape_loads_and_is_normalised) { ss += (double) f.cv.dir[(size_t) (l * 2560 + i)] * (double) f.cv.dir[(size_t) (l * 2560 + i)]; CHECK_NEAR(std::sqrt(ss), 1.0, 1e-5); - /* Unit rows, so the stored norm -- what llama.cpp keeps as `scales[il]` -- is 1. */ - CHECK_NEAR(f.cv.scales[(size_t) l], 1.0f, 1e-5); + /* Unit rows, so the stored norm -- what llama.cpp keeps as `scales[il]` -- is 1 where the + * range steers, and 0 outside it: the plugin declares an op exactly where it is nonzero. */ + CHECK_NEAR(f.cv.scales[(size_t) l], (l >= 4 && l <= 44) ? 1.0f : 0.0f, 1e-5); } - /* Outside the range nothing is steered: the plugin declares no op there. */ + /* Outside the range nothing is steered: no op, and the scale says so too. */ CHECK_EQ(f.cv.covers(3), false); CHECK_EQ(f.cv.covers(4), true); CHECK_EQ(f.cv.covers(44), true); @@ -211,6 +212,8 @@ TEST(the_packages_json_sets_the_range_and_scale) { CHECK_NEAR(a.cv.scales[(size_t) 8], 0.5f, 1e-5); CHECK_EQ(a.cv.covers(7), false); CHECK_EQ(a.cv.covers(21), false); + CHECK_EQ(a.cv.scales[(size_t) 7], 0.0f); + CHECK_EQ(a.cv.scales[(size_t) 21], 0.0f); /* Another mode is not implemented: the file's directions still load, with the defaults. */ const Loaded b = load(make_vector(2560, 47), 2560, 48, "json-add.gguf",