diff --git a/core/format/imatrix.cpp b/core/format/imatrix.cpp index a4805dd5..4b9568a9 100644 --- a/core/format/imatrix.cpp +++ b/core/format/imatrix.cpp @@ -384,11 +384,18 @@ bool ImatrixFile::importance(const std::string& name, int64_t expert, bool ImatrixFile::importance_for(const std::string& weight, int64_t cols, std::vector* out) const { + /* llama.cpp's imatrix names each entry by its tensor, `.weight` and all + * (blk.0.attn_q.weight.in_sum2); some writers drop the suffix. Either spelling is found. */ std::string base = weight; - if (base.size() > 7 && base.compare(base.size() - 7, 7, ".weight") == 0) - base.resize(base.size() - 7); + const bool suffixed = base.size() > 7 && base.compare(base.size() - 7, 7, ".weight") == 0; + if (suffixed) base.resize(base.size() - 7); + auto look = [&](const std::string& b) -> const ImatrixEntry* { + if (const ImatrixEntry* e = find(b)) return e; + return suffixed ? find(b + ".weight") : nullptr; + }; int64_t expert = 0; - if (!find(base)) { + const ImatrixEntry* e = look(base); + if (!e) { /* `.`: the last component must be a plain decimal index. */ const size_t dot = base.find_last_of('.'); if (dot == std::string::npos || dot + 1 == base.size()) return false; @@ -397,10 +404,10 @@ bool ImatrixFile::importance_for(const std::string& weight, int64_t cols, expert = expert * 10 + (base[i] - '0'); } base.resize(dot); + e = look(base); } - const ImatrixEntry* e = find(base); if (!e || e->cols != cols) return false; - return importance(base, expert, out); + return importance(e->name, expert, out); } double ImatrixFile::count(const std::string& name, int64_t expert) const { diff --git a/tests/format_test.cpp b/tests/format_test.cpp index 25013c1a..ce62c3bc 100644 --- a/tests/format_test.cpp +++ b/tests/format_test.cpp @@ -2015,6 +2015,30 @@ TEST(a_converter_weight_name_finds_its_imatrix_row) { CHECK(!im.importance_for("blk.0.attn_k.weight", 4, &w)); } +/* llama.cpp's imatrix (tools/imatrix, "%s.in_sum2" of the tensor's name) keeps `.weight` in every + * entry's name; a converter weight finds that spelling too, dense or an expert of a stack. */ +TEST(a_converter_weight_name_finds_llama_cpps_spelling) { + const std::string path = write_tmp("names-llama.imatrix", gguf_bytes( + { + { "blk.0.attn_q.weight.in_sum2", { 4 }, { 1.0f, 2.0f, 3.0f, 6.0f } }, + { "blk.5.ffn_down_exps.weight.in_sum2", { 4, 2 }, + { 1.0f, 1.0f, 1.0f, 1.0f, 8.0f, 4.0f, 2.0f, 2.0f } }, + { "blk.5.ffn_down_exps.weight.counts", { 1, 2 }, { 10.0f, 3.0f } }, + }, + {})); + ImatrixFile im; + REQUIRE(im.open(path) == RAD_OK); + std::vector w; + REQUIRE(im.importance_for("blk.0.attn_q.weight", 4, &w)); + CHECK_NEAR(w[0], 1.0 / 3.0, 1e-6); + CHECK_NEAR(w[3], 2.0, 1e-6); + REQUIRE(im.importance_for("blk.5.ffn_down_exps.1.weight", 4, &w)); + CHECK_NEAR(w[0], 2.0, 1e-6); /* expert 1's own row, mean 4 */ + CHECK_NEAR(w[2], 0.5, 1e-6); + CHECK(!im.importance_for("blk.5.ffn_down_exps.2.weight", 4, &w)); /* no expert 2 */ + CHECK(!im.importance_for("blk.0.attn_q.weight", 5, &w)); +} + /* With general.alignment 1 every float of a payload may sit at an odd address; the importance is * read by memcpy, so its values are those the file holds. */ TEST(an_importance_at_an_odd_offset_reads_the_values_the_file_holds) {