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