Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
d8e902e
Add --show-statistics option
EAddario Apr 1, 2025
f46693b
Add --show-statistics logic
EAddario Apr 1, 2025
b3ac78b
Merge branch 'master' into imatrix
EAddario Apr 1, 2025
dc3373e
Add tensor name parsing
EAddario Apr 2, 2025
0589c3e
Tidy output format
EAddario Apr 2, 2025
e1fd1af
Fix typo in title
EAddario Apr 2, 2025
490a8fe
Merge branch 'master' into imatrix
EAddario Apr 7, 2025
62ac268
Improve tensor influence ranking
EAddario Apr 8, 2025
73d8ecb
Add better statistics
EAddario Apr 13, 2025
200d88c
Merge branch 'master' into imatrix
EAddario Apr 13, 2025
0b7f9c4
Change statistics' sort order
EAddario Apr 15, 2025
52e86e2
Merge branch 'master' into imatrix
EAddario Apr 15, 2025
91d48da
Merge branch 'master' into imatrix
EAddario Apr 19, 2025
755c1ef
Add Cosine Similarity
EAddario Apr 22, 2025
72a5ec1
Merge branch 'master' into imatrix
EAddario May 3, 2025
5cd20e4
Add header search path
EAddario May 3, 2025
1dbe6c3
Change header search path to private
EAddario May 3, 2025
bb47f0d
Merge branch 'master' into imatrix
EAddario May 11, 2025
a3ac66c
Merge branch 'master' into imatrix
EAddario May 25, 2025
3eb556e
Add weighted statistics per layer
EAddario May 25, 2025
0276d71
Merge branch 'master' into imatrix
EAddario Jun 3, 2025
1f8dc23
Merge branch 'master' into imatrix
EAddario Jun 13, 2025
8ecd5fa
Merge branch 'master' into imatrix
EAddario Jun 14, 2025
8302a8a
Merge branch 'master' into imatrix
EAddario Jun 15, 2025
bfc0dfc
Merge branch 'master' into imatrix
EAddario Jun 21, 2025
5cfc443
Update report title
EAddario Jun 21, 2025
280dfdd
Merge branch 'master' into imatrix
EAddario Jun 22, 2025
235442a
Refactor compute_statistics out of main
EAddario Jun 22, 2025
c823d16
Refactor compute_cossim out of load_imatrix
EAddario Jun 22, 2025
a5c4640
Refactor compute_statistics out of load_imatrix
EAddario Jun 22, 2025
655be19
Move imatrix statistics calculation into its own functions
EAddario Jun 22, 2025
23ecca8
Add checks and validations
EAddario Jun 22, 2025
a4166a8
Remove unnecessary include directory
EAddario Jun 22, 2025
ed4ba31
Merge branch 'master' into imatrix
EAddario Jun 23, 2025
19f8e15
Rename labels
EAddario Jun 24, 2025
f5fd2b7
Add m_stats getter and refactor compute_statistics out of load_imatrix
EAddario Jun 24, 2025
bc3bd57
Refactor variable names
EAddario Jun 24, 2025
c3ede42
Merge branch 'master' into imatrix
EAddario Jun 24, 2025
1389753
Merge branch 'master' into imatrix
EAddario Jun 29, 2025
fde3089
Minor cosmetic change
EAddario Jun 29, 2025
c5a3d0a
Retrigger checks (empty commit)
EAddario Jul 1, 2025
688d0c2
Merge branch 'master' into imatrix
EAddario Jul 5, 2025
b1c481a
Rerun checks (empty commit)
EAddario Jul 5, 2025
dd13175
Fix unnecessary type promotion
EAddario Jul 7, 2025
0cd8e67
Reverting change to improve code readability
EAddario Jul 7, 2025
6c72d8e
Merge branch 'master' into imatrix
EAddario Jul 7, 2025
6826341
Rerun checks (empty commit)
EAddario Jul 7, 2025
432650b
Rerun checks (empty commit)
EAddario Jul 8, 2025
61a21a4
Rerun checks - third time's the Charm 🤞 (empty commit)
EAddario Jul 9, 2025
1a43247
Merge branch 'master' into imatrix
EAddario Jul 11, 2025
a3fdb2b
Minor cosmetic change
EAddario Jul 12, 2025
f9391bd
Update README
EAddario Jul 12, 2025
98bcd3e
Fix typo
EAddario Jul 12, 2025
71d8492
Merge branch 'master' into imatrix
EAddario Jul 13, 2025
69a0b17
Update README
EAddario Jul 13, 2025
9f2c558
Rerun checks (empty commit)
EAddario Jul 13, 2025
2b45dca
Merge branch 'master' into imatrix
EAddario Jul 17, 2025
3c1e250
Merge branch 'master' into imatrix
EAddario Jul 19, 2025
f7b1ab2
Re-implement changes on top of #9400
EAddario Jul 19, 2025
fa7c3da
Update README.md
EAddario Jul 19, 2025
e9ab48f
Update README
EAddario Jul 19, 2025
c02952a
Update README.md
EAddario Jul 19, 2025
75cd6e0
Update README.md
EAddario Jul 19, 2025
50e2ecf
Update README.md
EAddario Jul 19, 2025
139ff4b
Remove duplicate option in print_usage()
EAddario Jul 19, 2025
ac0922f
Update README.md
EAddario Jul 19, 2025
693dfb2
Update README.md
EAddario Jul 19, 2025
aef8914
Update README.md
EAddario Jul 19, 2025
fc77a54
Remove input check
EAddario Jul 20, 2025
360c8a5
Remove commented out code
EAddario Jul 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add m_stats getter and refactor compute_statistics out of load_imatrix
  • Loading branch information
EAddario committed Jun 24, 2025
commit f5fd2b70f08aa9d75a4f1110f49e3c2ddfbd7043
27 changes: 14 additions & 13 deletions tools/imatrix/imatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class IMatrixCollector {
void set_params(common_params params) { m_params = std::move(params); }
bool collect_imatrix(struct ggml_tensor * t, bool ask, void * user_data);
void save_imatrix(int ncall = -1) const;
bool load_imatrix(const char * fname, std::vector<tensor_statistics> * tstats = nullptr);
bool load_imatrix(const char * fname);
const std::unordered_map<std::string, Stats> & get_mstats() const { return m_stats; }
private:
std::unordered_map<std::string, Stats> m_stats;
common_params m_params;
Expand Down Expand Up @@ -116,8 +117,12 @@ static void process_tensor_name(const std::string & input, std::string & layer,
}
}

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

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

// Recreate the state as expected by save_imatrix(), and correct for weighted sum.
std::vector<float> activations;
activations.reserve(nval);
for (int i = 0; i < nval; i++) {
e.values[i] += tmp[i];
e.counts[i] += ncall;
activations.push_back(e.values[i] / e.counts[i]);
}
e.ncall += ncall;

if (tstats) {
compute_statistics(*tstats, name_as_vec.data(), e, activations);
}
}

return true;
Expand Down Expand Up @@ -714,11 +712,14 @@ static bool show_statistics(const common_params & params) {
LOG_ERR("\nError: a single imatrix file is required to compute tensor statistics\n\n");
return false;
}
if (!g_collector.load_imatrix(params.in_files[0].c_str(), &ts)) {
if (g_collector.load_imatrix(params.in_files[0].c_str())) {
for (const auto & [name, stats] :g_collector.get_mstats()) {
compute_statistics(ts, name, stats);
}
} else {
LOG_ERR("\nError: %s is not a valid imatrix file\n\n", params.in_files[0].c_str());
return false;
}

if (!ts.empty()) {
compute_cossim(ts);
} else {
Expand Down