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",