Skip to content

Commit 88854c9

Browse files
committed
Refactor legacy mode
1 parent 4c3fea8 commit 88854c9

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

tools/imatrix/imatrix.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static std::vector<float> compute_tensor_averages(const Stats & tstats) {
160160
return vec;
161161
}
162162

163-
static int compute_vector_statistics(std::vector<tensor_statistics> & tstats, const std::string & name, const Stats & e) {
163+
static bool compute_vector_statistics(std::vector<tensor_statistics> & tstats, const std::string & name, const Stats & e) {
164164
if (e.values.size() % e.counts.size() != 0) {
165165
LOG_ERR("%s: activation size mismatch for tensor %s (%zu vs %zu)\n", __func__, name.c_str(), e.counts.size(), e.values.size());
166166
return -1;;
@@ -172,7 +172,6 @@ static int compute_vector_statistics(std::vector<tensor_statistics> & tstats, co
172172

173173
const int n_mat = e.counts.size();
174174
const int row_size = e.values.size() / n_mat;
175-
const int calc_mode = e.activations.empty() ? 2 : 1;
176175

177176
std::vector<float> activations;
178177

@@ -203,7 +202,15 @@ static int compute_vector_statistics(std::vector<tensor_statistics> & tstats, co
203202
const float std_deviation = std::sqrt(std::max(0.0f, variance));
204203
float entropy = 0;
205204

206-
if (calc_mode == 1) {
205+
if (e.activations.empty()) {
206+
if (sum > 0) {
207+
for (const auto act : activations) {
208+
if (const float p = act / sum; p > 0) {
209+
entropy -= p * std::log2(p);
210+
}
211+
}
212+
}
213+
} else {
207214
float div = 0.0;
208215
std::vector<float> weights(activations.size());
209216
for (size_t i = 0; i < activations.size(); ++i) {
@@ -218,14 +225,6 @@ static int compute_vector_statistics(std::vector<tensor_statistics> & tstats, co
218225
if (p > 0.0) entropy -= p * std::log2(p);
219226
}
220227
}
221-
} else {
222-
if (sum > 0) {
223-
for (const auto act : activations) {
224-
if (const float p = act / sum; p > 0) {
225-
entropy -= p * std::log2(p);
226-
}
227-
}
228-
}
229228
}
230229

231230
int z_score = 0;
@@ -247,7 +246,7 @@ static int compute_vector_statistics(std::vector<tensor_statistics> & tstats, co
247246
ts.entropy = entropy;
248247
ts.zd_score = static_cast<float>(z_score) / ts.elements;
249248

250-
return calc_mode;
249+
return e.activations.empty();
251250
}
252251

253252
static void compute_tensor_statistics(std::vector<tensor_statistics> & tstats) {
@@ -1257,15 +1256,15 @@ static bool compute_imatrix(llama_context * ctx, const common_params & params, c
12571256

12581257
static bool show_statistics(const common_params & params) {
12591258
std::vector<tensor_statistics> ts;
1260-
int tensor_calc_mode = 0;
1259+
bool legacy_mode = false;
12611260

12621261
if (params.in_files.empty() || params.in_files.size() > 1) {
12631262
LOG_ERR("\nError: a single imatrix file is required to compute tensor statistics\n\n");
12641263
return false;
12651264
}
12661265
if (g_collector.load_imatrix(params.in_files[0].c_str())) {
12671266
for (const auto & [name, stats] :g_collector.get_mstats()) {
1268-
tensor_calc_mode =compute_vector_statistics(ts, name, stats);
1267+
legacy_mode = compute_vector_statistics(ts, name, stats);
12691268
}
12701269
} else {
12711270
LOG_ERR("\nError: %s is not a valid imatrix file\n\n", params.in_files[0].c_str());
@@ -1300,7 +1299,7 @@ static bool show_statistics(const common_params & params) {
13001299
LOG_INF("\n%6s\t%18s\t%13s\t%8s\t%8s\t%7s\t%15s\t%13s\t%12s\t%s\t%5s\t%10s\n",
13011300
"Layer",
13021301
"Tensor",
1303-
tensor_calc_mode == 1 ? "L₂ Norm" : "Σ(Act²)",
1302+
legacy_mode ? "Σ(Act²)" : "L₂ Norm",
13041303
"Min",
13051304
"Max",
13061305
"μ",
@@ -1327,7 +1326,7 @@ static bool show_statistics(const common_params & params) {
13271326
LOG_INF("%5s\t%-20s\t%11.2f\t%10.4f\t%10.4f\t%8.2f\t%8.2f\t%7d\t%12.4f\t%7.2f%%\t%6.2f%%\t%10.4f\n",
13281327
layer.c_str(),
13291328
name.c_str(),
1330-
tensor_calc_mode == 1 ? tstat.l2_norm : tstat.sum_values,
1329+
legacy_mode == 1 ? tstat.sum_values : tstat.l2_norm,
13311330
tstat.min_values,
13321331
tstat.max_values,
13331332
tstat.mean_values,
@@ -1361,7 +1360,7 @@ static bool show_statistics(const common_params & params) {
13611360
LOG_INF("\nComputing aggregated statistics per layer (%ld layers)\n", layers);
13621361
LOG_INF("\n%6s\t%13s\t%5s\t%10s\n",
13631362
"Layer",
1364-
tensor_calc_mode == 1 ? "L₂ Norm" : "Σ(Act²)",
1363+
legacy_mode ? "Σ(Act²)" : "L₂ Norm",
13651364
"ZD",
13661365
"CosSim");
13671366
LOG_INF("============================================\n");
@@ -1375,7 +1374,7 @@ static bool show_statistics(const common_params & params) {
13751374
const float l2_norm = (ll2n != lyr_l2_norm.end()) ? ll2n->second : 0.0f;
13761375
LOG_INF("%5d\t%11.2f\t%6.2f%%\t%10.4f\n",
13771376
layer,
1378-
tensor_calc_mode == 1 ? l2_norm: lyr_sum,
1377+
legacy_mode ? lyr_sum : l2_norm,
13791378
100.0f * lyr_zd,
13801379
lyr_cs);
13811380
}

0 commit comments

Comments
 (0)