| 1082 | } |
| 1083 | |
| 1084 | static bool show_statistics(const common_params & params) { |
| 1085 | std::vector<tensor_statistics> ts; |
| 1086 | if (params.in_files.empty() || params.in_files.size() > 1) { |
| 1087 | LOG_ERR("\nError: a single imatrix file is required to compute tensor statistics\n\n"); |
| 1088 | return false; |
| 1089 | } |
| 1090 | if (g_collector.load_imatrix(params.in_files[0].c_str())) { |
| 1091 | for (const auto & [name, stats] :g_collector.get_mstats()) { |
| 1092 | compute_statistics(ts, name, stats); |
| 1093 | } |
| 1094 | } else { |
| 1095 | LOG_ERR("\nError: %s is not a valid imatrix file\n\n", params.in_files[0].c_str()); |
| 1096 | return false; |
| 1097 | } |
| 1098 | if (!ts.empty()) { |
| 1099 | compute_cossim(ts); |
| 1100 | } else { |
| 1101 | LOG_ERR("Error: cannot compute statistics for %s\n\n", params.in_files[0].c_str()); |
| 1102 | return false; |
| 1103 | } |
| 1104 | |
| 1105 | struct tensor_comparer { |
| 1106 | bool operator()(const tensor_statistics & a, const tensor_statistics & b) const { |
| 1107 | std::string layer, name_a, name_b; |
| 1108 | ; |
| 1109 | process_tensor_name(a.tensor, layer, name_a); |
| 1110 | process_tensor_name(b.tensor, layer, name_b); |
| 1111 | return name_a < name_b || (name_a == name_b && a.total_sqract > b.total_sqract); |
| 1112 | } |
| 1113 | }; |
| 1114 | std::sort(ts.begin(), ts.end(), tensor_comparer()); |
| 1115 | |
| 1116 | struct weighted_stats { |
| 1117 | float weighted_bias = 0.0f; |
| 1118 | float weighted_zd = 0.0f; |
| 1119 | float weighted_cossim = 0.0f; |
| 1120 | int total_elements = 0; |
| 1121 | }; |
| 1122 | std::map<int, weighted_stats> ws; |
| 1123 | |
| 1124 | LOG_INF("\nComputing statistics for %s (%d tensors)\n", params.in_files[0].c_str(), static_cast<int>(ts.size())); |
| 1125 | 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", " Layer", " Tensor", " Σ(Act²)", |
| 1126 | " Min", " Max", " μ", " σ", " % Active", "N", " Entropy", "E (norm)", "ZD", |
| 1127 | " CosSim"); |
| 1128 | LOG_INF( |
| 1129 | "==============================================================================================================" |
| 1130 | "===========================================================\n"); |
| 1131 | for (const auto & tstat : ts) { |
| 1132 | std::string layer, name; |
| 1133 | process_tensor_name(tstat.tensor, layer, name); |
| 1134 | |
| 1135 | int blk; |
| 1136 | try { |
| 1137 | blk = std::stoi(layer); |
| 1138 | } catch (const std::exception & e) { |
| 1139 | blk = -1; // not a block layer |
| 1140 | } |
| 1141 |
no test coverage detected