Archived
core/format: an imatrix entry is found under llama.cpp's spelling, .weight and all
llama.cpp names each imatrix entry by its tensor (blk.0.attn_q.weight.in_sum2); importance_for stripped .weight from the weight's name and looked up only the bare stem, so --imatrix matched no weight from a llama.cpp file: the codebook types that need it (iq1_s, iq2_xxs, iq2_xs) were refused and every other type was quantised without its importances. Both spellings are found now, per-expert rows included, and the row is read under the entry's own name. Tests: format_test a_converter_weight_name_finds_llama_cpps_spelling (a dense and a stacked expert entry spelled .weight.in_sum2, a missing expert, a wrong width) fails without the change and passes with it; a_converter_weight_name_finds_its_imatrix_row (the bare spelling) is unchanged.
This commit is contained in:
1 parent
b56139a96a
commit
fa8c746497
2 files changed
+36
-5
No files matched your search
+12
-5
@@ -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,
|
bool ImatrixFile::importance_for(const std::string& weight, int64_t cols,
|
||||||
std::vector<float>* out) const {
|
std::vector<float>* 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;
|
std::string base = weight;
|
||||||
if (base.size() > 7 && base.compare(base.size() - 7, 7, ".weight") == 0)
|
const bool suffixed = base.size() > 7 && base.compare(base.size() - 7, 7, ".weight") == 0;
|
||||||
base.resize(base.size() - 7);
|
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;
|
int64_t expert = 0;
|
||||||
if (!find(base)) {
|
const ImatrixEntry* e = look(base);
|
||||||
|
if (!e) {
|
||||||
/* `<stem>.<expert>`: the last component must be a plain decimal index. */
|
/* `<stem>.<expert>`: the last component must be a plain decimal index. */
|
||||||
const size_t dot = base.find_last_of('.');
|
const size_t dot = base.find_last_of('.');
|
||||||
if (dot == std::string::npos || dot + 1 == base.size()) return false;
|
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');
|
expert = expert * 10 + (base[i] - '0');
|
||||||
}
|
}
|
||||||
base.resize(dot);
|
base.resize(dot);
|
||||||
|
e = look(base);
|
||||||
}
|
}
|
||||||
const ImatrixEntry* e = find(base);
|
|
||||||
if (!e || e->cols != cols) return false;
|
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 {
|
double ImatrixFile::count(const std::string& name, int64_t expert) const {
|
||||||
|
|||||||
@@ -2015,6 +2015,30 @@ TEST(a_converter_weight_name_finds_its_imatrix_row) {
|
|||||||
CHECK(!im.importance_for("blk.0.attn_k.weight", 4, &w));
|
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<float> 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
|
/* 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. */
|
* read by memcpy, so its values are those the file holds. */
|
||||||
TEST(an_importance_at_an_odd_offset_reads_the_values_the_file_holds) {
|
TEST(an_importance_at_an_odd_offset_reads_the_values_the_file_holds) {
|
||||||
|
|||||||
Reference in new issue
Block a user