core: bound the loader's layer index to the table it fills, and log the file's own count

The direction table is indexed by layer (row 0 unused) and sized n_layers, so a
file carrying direction.<n_layers> passed the range check and then wrote one
row past `dir`, and a package JSON whose range ended at n_layers read one past
`scales`. Both bounds are exclusive now, and cvec_test covers each: direction.48
of a 48-layer model, and a JSON range 4..48 on the same model.

The startup line printed `n_layers - 1` as the direction count -- the published
file's count, but wrong for a file carrying fewer directions; the loader records
what it read and the line prints that. Also drops an unused <cstring>.
This commit is contained in:
C_Tamas authored and StillDeadcode committed 2026-10-21 09:26:17 +02:00
1 parent f3979aa538
commit d09d733b07
4 files changed
+23 -9

No files matched your search

+5 -3
View File
@@ -19,7 +19,6 @@
#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <nlohmann/json.hpp>
@@ -188,7 +187,9 @@ int control_vector_load(const std::string& path, const std::string& arch, int64_
return fail("control vector " + path + ": tensor name '" + t.name +
"' does not end in a layer number");
}
if (layer < 1 || layer > n_model_layers)
/* 1 .. n_layers-1: the table is indexed by layer (row 0 is unused, the format carrying no
* direction for it), so direction.<n_layers> would write one row past it. */
if (layer < 1 || layer >= n_model_layers)
return fail("control vector " + path + ": direction." + std::to_string(layer) +
" is outside this model's " + std::to_string(n_model_layers) + " layers");
if (t.n_dims != 1 || t.ne[0] != n_embd)
@@ -218,7 +219,7 @@ int control_vector_load(const std::string& path, const std::string& arch, int64_
if (first < 1 || last < first)
return fail("control vector " + path + ": the layer range " + std::to_string(first) + ":" +
std::to_string(last) + " is empty");
if (last > n_model_layers)
if (last >= n_model_layers)
return fail("control vector " + path + ": the layer range " + std::to_string(first) + ":" +
std::to_string(last) + " runs past this model's " +
std::to_string(n_model_layers) + " layers");
@@ -229,6 +230,7 @@ int control_vector_load(const std::string& path, const std::string& arch, int64_
/* Indexed by layer: rows 0..n_layers-1. Row 0 stays zero, the format carrying no direction
* for layer 0, and every row a model can name exists. */
out->n_layers = n_model_layers;
out->n_dirs = n_dir;
out->first = first;
out->last = last;
out->user_scale = scale;
+1
View File
@@ -48,6 +48,7 @@ struct ControlVector {
std::vector<float> scales;
int64_t n_embd = 0;
int64_t n_layers = 0;
int64_t n_dirs = 0; /* directions the file carried (47 for the published one) */
int first = -1, last = -1; /* inclusive layer range */
float user_scale = 1.0f;
std::string path;
+1 -1
View File
@@ -1106,7 +1106,7 @@ int Engine::declare() {
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),
cvec_.path.c_str(), (long long) cvec_.n_dirs,
(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; "
+16 -5
View File
@@ -15,7 +15,6 @@
#include "cvec.h"
#include <cmath>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <string>
@@ -76,8 +75,6 @@ struct GgufWriter {
h += dir;
while (h.size() % alignment) h.push_back('\0');
for (const T& t : tensors) {
const size_t at = h.size();
(void) at;
h.append((const char*) t.data.data(), t.data.size() * 4);
while (h.size() % alignment) h.push_back('\0');
}
@@ -132,8 +129,8 @@ Loaded load(const GgufWriter& w, int64_t n_embd, int64_t model_layers, const std
} // namespace
TEST(the_published_shape_loads_and_is_normalised) {
/* 47 directions of 2560 for a 48-layer model, the range the package's own JSON states. */
/* Nothing states a range, so the loader takes the published selection, 4..44. */
/* 47 directions of 2560 for a 48-layer model, and no JSON beside it: the loader takes the
* published selection, 4..44. */
const Loaded f = load(make_vector(2560, 47), 2560, 48, "flash-next.gguf");
CHECK_OK(f.rc);
CHECK_EQ(f.cv.n_embd, (int64_t) 2560);
@@ -289,4 +286,18 @@ TEST(a_layer_range_past_the_model_is_refused) {
CHECK(l.err.find("runs past") != std::string::npos);
}
/* A layer index the table cannot hold: a 48-layer model's table is rows 0..47 and the format has
* no direction for the last row, so direction.48 and a JSON range ending at 48 are both refused
* rather than indexing one row past it. */
TEST(a_layer_at_the_models_own_count_is_refused) {
const Loaded a = load(make_vector(2560, 48), 2560, 48, "layer-48.gguf");
CHECK(a.rc < 0);
CHECK(a.err.find("outside") != std::string::npos);
const Loaded b = load(make_vector(2560, 47), 2560, 48, "range-to-48.gguf",
R"({"mode":"project","scale":1.0,"layer_range":[4,48]})");
CHECK(b.rc < 0);
CHECK(b.err.find("runs past") != std::string::npos);
}
RAD_TEST_MAIN()