Skip to content

Commit f5fd2b7

Browse files
committed
Add m_stats getter and refactor compute_statistics out of load_imatrix
1 parent 19f8e15 commit f5fd2b7

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

tools/imatrix/imatrix.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class IMatrixCollector {
5858
void set_params(common_params params) { m_params = std::move(params); }
5959
bool collect_imatrix(struct ggml_tensor * t, bool ask, void * user_data);
6060
void save_imatrix(int ncall = -1) const;
61-
bool load_imatrix(const char * fname, std::vector<tensor_statistics> * tstats = nullptr);
61+
bool load_imatrix(const char * fname);
62+
const std::unordered_map<std::string, Stats> & get_mstats() const { return m_stats; }
6263
private:
6364
std::unordered_map<std::string, Stats> m_stats;
6465
common_params m_params;
@@ -116,8 +117,12 @@ static void process_tensor_name(const std::string & input, std::string & layer,
116117
}
117118
}
118119

119-
static void compute_statistics(std::vector<tensor_statistics> & tstats, const std::string & name, const Stats & e, const std::vector<float> & activations) {
120-
if (activations.empty()) {
120+
static void compute_statistics(std::vector<tensor_statistics> & tstats, const std::string & name, const Stats & e) {
121+
if (e.values.size() != e.counts.size()) {
122+
LOG_ERR("%s: activation size mismatch for tensor %s (%zu vs %zu)\n", __func__, name.c_str(), e.counts.size(), e.values.size());
123+
return;
124+
}
125+
if (e.counts.empty()) {
121126
LOG_ERR("%s: there are no activations for tensor %s. The imatrix may be suboptimal\n", __func__, name.c_str());
122127
return;
123128
}
@@ -413,7 +418,7 @@ void IMatrixCollector::save_imatrix(int ncall) const {
413418
LOG_DBGV(1, "%s: stored collected data after %d chunks in %s\n", __func__, m_last_call, fname.c_str());
414419
}
415420

416-
bool IMatrixCollector::load_imatrix(const char * fname, std::vector<tensor_statistics> * tstats) {
421+
bool IMatrixCollector::load_imatrix(const char * fname) {
417422
std::ifstream in(fname, std::ios::binary);
418423
if (!in) {
419424
LOG_ERR("%s: failed to open %s\n",__func__, fname);
@@ -461,18 +466,11 @@ bool IMatrixCollector::load_imatrix(const char * fname, std::vector<tensor_stati
461466
}
462467

463468
// Recreate the state as expected by save_imatrix(), and correct for weighted sum.
464-
std::vector<float> activations;
465-
activations.reserve(nval);
466469
for (int i = 0; i < nval; i++) {
467470
e.values[i] += tmp[i];
468471
e.counts[i] += ncall;
469-
activations.push_back(e.values[i] / e.counts[i]);
470472
}
471473
e.ncall += ncall;
472-
473-
if (tstats) {
474-
compute_statistics(*tstats, name_as_vec.data(), e, activations);
475-
}
476474
}
477475

478476
return true;
@@ -714,11 +712,14 @@ static bool show_statistics(const common_params & params) {
714712
LOG_ERR("\nError: a single imatrix file is required to compute tensor statistics\n\n");
715713
return false;
716714
}
717-
if (!g_collector.load_imatrix(params.in_files[0].c_str(), &ts)) {
715+
if (g_collector.load_imatrix(params.in_files[0].c_str())) {
716+
for (const auto & [name, stats] :g_collector.get_mstats()) {
717+
compute_statistics(ts, name, stats);
718+
}
719+
} else {
718720
LOG_ERR("\nError: %s is not a valid imatrix file\n\n", params.in_files[0].c_str());
719721
return false;
720722
}
721-
722723
if (!ts.empty()) {
723724
compute_cossim(ts);
724725
} else {

0 commit comments

Comments
 (0)