Skip to content

Commit 3eb556e

Browse files
committed
Add weighted statistics per layer
1 parent a3ac66c commit 3eb556e

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tools/imatrix/imatrix.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <vector>
1414
#include <fstream>
1515
#include <unordered_map>
16+
#include <map>
1617
#include <algorithm>
1718
#include <regex>
1819
#include <numeric>
@@ -729,18 +730,73 @@ int main(int argc, char ** argv) {
729730
};
730731
std::sort(ts.begin(), ts.end(), tensor_comparer());
731732

733+
struct weighted_stats {
734+
float weighted_bias = 0.0f;
735+
float weighted_zd = 0.0f;
736+
float weighted_cossim = 0.0f;
737+
int total_elements = 0;
738+
};
739+
std::map<int, weighted_stats> ws;
740+
732741
LOG_INF("\nComputing statistics for %s (%d tensors)\n", params.in_files[0].c_str(), static_cast<int>(ts.size()));
733742
LOG_INF("\n%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
734743
" Layer", " Tensor", " Σ(Bias)", " Min", " Max", " μ", " σ", " % Active", "N", " Entropy", "E (norm)", "ZD", " CosSim");
735744
LOG_INF("=========================================================================================================================================================================\n");
736745
for (const auto & tstat : ts) {
737746
std::string layer, name;
738747
process_tensor_name(tstat.tensor, layer, name);
748+
749+
int blk;
750+
try {
751+
blk = std::stoi(layer);
752+
} catch (const std::exception & e) {
753+
blk = -1; // not a block layer
754+
}
755+
739756
LOG_INF("%5s\t%-20s\t%10.2f\t%8.4f\t%11.4f\t%6.2f\t%6.2f\t%8.2f%%\t%6d\t%10.4f\t%6.2f%%\t%10.2f%%\t%8.4f\n",
740757
layer.c_str(), name.c_str(), tstat.total_bias, tstat.min_bias, tstat.max_bias, tstat.mean_bias, tstat.stddev,
741758
tstat.active * 100.0f, tstat.elements, tstat.entropy, 100.0f * (tstat.entropy / std::log2(tstat.elements)),
742759
100.0f * tstat.zd, tstat.cossim);
760+
761+
const float weighted_bias = tstat.elements * tstat.total_bias;
762+
const float weighted_zd = tstat.elements * tstat.zd;
763+
const float weighted_cossim = tstat.elements * tstat.cossim;
764+
765+
if (ws.find(blk) != ws.end()) {
766+
ws[blk].weighted_bias += weighted_bias;
767+
ws[blk].weighted_zd += weighted_zd;
768+
ws[blk].weighted_cossim += weighted_cossim;
769+
ws[blk].total_elements += tstat.elements;
770+
} else {
771+
weighted_stats temp_ws;
772+
temp_ws.weighted_bias = weighted_bias;
773+
temp_ws.weighted_zd = weighted_zd;
774+
temp_ws.weighted_cossim = weighted_cossim;
775+
temp_ws.total_elements = tstat.elements;
776+
ws[blk] = temp_ws;
777+
}
743778
}
779+
780+
const int layers = std::count_if(ws.begin(), ws.end(), [](const auto & kv) { return kv.first >= 0; });
781+
LOG_INF("\nComputing weighted statistics per layer (%d layers)\n", layers);
782+
LOG_INF("\n%s\t%s\t%s\t%s\n", " Layer", " Σ(Bias)", " ZD", "CosSim");
783+
LOG_INF("===============================================\n");
784+
785+
for (const auto & [first, second] : ws) {
786+
const auto & layer = first;
787+
const auto & stats = second;
788+
789+
if (stats.total_elements == 0) continue;
790+
791+
if (layer >= 0) {
792+
const float bias = stats.weighted_bias / stats.total_elements;
793+
const float zd = stats.weighted_zd / stats.total_elements;
794+
const float cossim = stats.weighted_cossim / stats.total_elements;
795+
796+
LOG_INF("%5d\t%14.2f\t%10.4f%%\t%6.4f\n", layer, bias, 100.0f * zd, cossim);
797+
}
798+
}
799+
744800
LOG_INF("\n");
745801

746802
return 0;

0 commit comments

Comments
 (0)