diff --git a/core/cvec.cpp b/core/cvec.cpp index a26c3546..262b40ae 100644 --- a/core/cvec.cpp +++ b/core/cvec.cpp @@ -19,7 +19,6 @@ #include #include -#include #include #include @@ -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. 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; diff --git a/core/cvec.h b/core/cvec.h index 52c896db..2378b9f4 100644 --- a/core/cvec.h +++ b/core/cvec.h @@ -48,6 +48,7 @@ struct ControlVector { std::vector 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; diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index 521f98fe..d286b011 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -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; " diff --git a/tests/cvec_test.cpp b/tests/cvec_test.cpp index 8e703ae0..1988db7e 100644 --- a/tests/cvec_test.cpp +++ b/tests/cvec_test.cpp @@ -15,7 +15,6 @@ #include "cvec.h" #include -#include #include #include #include @@ -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()