|
13 | 13 | #include <vector> |
14 | 14 | #include <fstream> |
15 | 15 | #include <unordered_map> |
| 16 | +#include <map> |
16 | 17 | #include <algorithm> |
17 | 18 | #include <regex> |
18 | 19 | #include <numeric> |
@@ -729,18 +730,73 @@ int main(int argc, char ** argv) { |
729 | 730 | }; |
730 | 731 | std::sort(ts.begin(), ts.end(), tensor_comparer()); |
731 | 732 |
|
| 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 | + |
732 | 741 | LOG_INF("\nComputing statistics for %s (%d tensors)\n", params.in_files[0].c_str(), static_cast<int>(ts.size())); |
733 | 742 | 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", |
734 | 743 | " Layer", " Tensor", " Σ(Bias)", " Min", " Max", " μ", " σ", " % Active", "N", " Entropy", "E (norm)", "ZD", " CosSim"); |
735 | 744 | LOG_INF("=========================================================================================================================================================================\n"); |
736 | 745 | for (const auto & tstat : ts) { |
737 | 746 | std::string layer, name; |
738 | 747 | 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 | + |
739 | 756 | 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", |
740 | 757 | layer.c_str(), name.c_str(), tstat.total_bias, tstat.min_bias, tstat.max_bias, tstat.mean_bias, tstat.stddev, |
741 | 758 | tstat.active * 100.0f, tstat.elements, tstat.entropy, 100.0f * (tstat.entropy / std::log2(tstat.elements)), |
742 | 759 | 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 | + } |
743 | 778 | } |
| 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 | + |
744 | 800 | LOG_INF("\n"); |
745 | 801 |
|
746 | 802 | return 0; |
|
0 commit comments