Skip to content

Commit 63aa3f3

Browse files
committed
Add imatrix mapping logic
1 parent 5a805d1 commit 63aa3f3

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/llama-quant.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <unordered_map>
1515

1616
//static std::vector prune_map = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
17-
static std::vector<int> prune_map = {7};
17+
static std::vector<int> prune_map = {3};
1818

1919
static void zeros(std::ofstream & file, size_t n) {
2020
char zero = 0;
@@ -52,6 +52,28 @@ static std::string remap_layer(const std::string & orig_name, const std::vector<
5252
return orig_name;
5353
}
5454

55+
static std::string remap_imatrix (const std::string & orig_name, const std::map<int, std::string>& mapped) {
56+
if (mapped.empty()) {
57+
return orig_name;
58+
}
59+
60+
static const std::regex pattern(R"(blk\.(\d+)\.)");
61+
if (std::smatch match; std::regex_search(orig_name, match, pattern)) {
62+
const std::string blk(match[1]);
63+
std::string new_name = orig_name;
64+
65+
for (const auto & p : mapped) {
66+
if (p.second == blk) {
67+
//LLAMA_LOG_DEBUG("(imatrix -> %d) ", p.first);
68+
return new_name.replace(match.position(1), match.length(1), std::to_string(p.first));
69+
}
70+
}
71+
GGML_ABORT("\n%s: imatrix mapping error for %s\n", __func__, orig_name.c_str());
72+
}
73+
74+
return orig_name;
75+
}
76+
5577
struct quantize_state_impl {
5678
const llama_model & model;
5779
const llama_model_quantize_params * params;
@@ -882,7 +904,7 @@ static void llama_model_quantize_impl(const std::string & fname_inp, const std::
882904

883905
const float * imatrix = nullptr;
884906
if (imatrix_data) {
885-
auto it = imatrix_data->find(tensor->name);
907+
auto it = imatrix_data->find(remap_imatrix(tensor->name, mapped));
886908
if (it == imatrix_data->end()) {
887909
LLAMA_LOG_INFO("\n====== %s: did not find weights for %s\n", __func__, tensor->name);
888910
} else {

0 commit comments

Comments
 (0)