Skip to content

Commit bc3bd57

Browse files
committed
Refactor variable names
1 parent f5fd2b7 commit bc3bd57

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

tools/imatrix/imatrix.cpp

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ struct Stats {
4040
struct tensor_statistics {
4141
std::string tensor;
4242
Stats stats;
43-
float total_bias = 0;
44-
float mean_bias = 0;
45-
float max_bias = 0;
46-
float min_bias = 0;
47-
int elements = 0;
48-
float stddev = 0;
49-
float active = 0;
50-
float entropy = 0;
51-
float zd = 0;
52-
float cossim = 0;
43+
float total_sqract = 0.0f;
44+
float mean_sqract = 0.0f;
45+
float max_sqract = 0.0f;
46+
float min_sqract = 0.0f;
47+
int elements = 0;
48+
float stddev = 0.0f;
49+
float active = 0.0f;
50+
float entropy = 0.0f;
51+
float zd = 0.0f;
52+
float cossim = 0.0f;
5353
};
5454

5555
class IMatrixCollector {
@@ -127,31 +127,38 @@ static void compute_statistics(std::vector<tensor_statistics> & tstats, const st
127127
return;
128128
}
129129

130-
const float total = std::accumulate(activations.begin(), activations.end(), 0.0f);
131-
const float max = *std::max_element(activations.begin(), activations.end());
132-
const float min = *std::min_element(activations.begin(), activations.end());
133-
const float mean = total / activations.size();
134-
const float sq_total = std::inner_product(activations.begin(), activations.end(), activations.begin(), 0.0f);
135-
const float variance = (sq_total / activations.size()) - (mean * mean);
136-
const float dev = std::sqrt(std::max(0.0f, variance));
137-
float threshold = 1e-6f;
138-
const int inactive_count = std::count_if(activations.begin(), activations.end(),
130+
const int size = e.counts.size();
131+
std::vector<float> activations;
132+
activations.reserve(size);
133+
for (int i = 0; i < size; i++) {
134+
activations.push_back(e.values[i] / e.counts[i]);
135+
}
136+
137+
const float act_total = std::accumulate(activations.begin(), activations.end(), 0.0f);
138+
const float act_max = *std::max_element(activations.begin(), activations.end());
139+
const float act_min = *std::min_element(activations.begin(), activations.end());
140+
const float act_mean = act_total / activations.size();
141+
const float act_sqr_total = std::inner_product(activations.begin(), activations.end(), activations.begin(), 0.0f);
142+
const float act_var = (act_sqr_total / activations.size()) - (act_mean * act_mean);
143+
const float act_dev = std::sqrt(std::max(0.0f, act_var));
144+
float threshold = 1e-5f;
145+
const int inactive_count = std::count_if(activations.begin(), activations.end(),
139146
[threshold](const float v) { return fabs(v) <= threshold; });
140-
const float active_ratio = 1 - static_cast<float>(inactive_count) / activations.size();
147+
const float active_ratio = 1 - static_cast<float>(inactive_count) / activations.size();
141148

142149
float entropy = 0;
143-
if (total > 0) {
150+
if (act_total > 0) {
144151
for (const auto act : activations) {
145-
if (const float p = act / total; p > 0) {
152+
if (const float p = act / act_total; p > 0) {
146153
entropy -= p * std::log2(p);
147154
}
148155
}
149156
}
150157

151158
int z_score = 0;
152-
if (dev > 0.0f) {
159+
if (act_dev > 0.0f) {
153160
for (const auto act : activations) {
154-
if (const float p = (act - mean) / dev; p > 1) {
161+
if (const float p = (act - act_mean) / act_dev; p > 1) {
155162
z_score++;
156163
}
157164
}
@@ -160,12 +167,12 @@ static void compute_statistics(std::vector<tensor_statistics> & tstats, const st
160167
auto & ts = tstats.emplace_back();
161168
ts.tensor = name;
162169
ts.stats = e;
163-
ts.total_bias = total;
164-
ts.mean_bias = mean;
165-
ts.max_bias = max;
166-
ts.min_bias = min;
170+
ts.total_sqract = act_total;
171+
ts.mean_sqract = act_mean;
172+
ts.max_sqract = act_max;
173+
ts.min_sqract = act_min;
167174
ts.elements = static_cast<int>(activations.size());
168-
ts.stddev = dev;
175+
ts.stddev = act_dev;
169176
ts.active = active_ratio;
170177
ts.entropy = entropy;
171178
ts.zd = static_cast<float>(z_score) / ts.elements;
@@ -733,7 +740,7 @@ static bool show_statistics(const common_params & params) {
733740
;
734741
process_tensor_name(a.tensor, layer, name_a);
735742
process_tensor_name(b.tensor, layer, name_b);
736-
return name_a < name_b || (name_a == name_b && a.total_bias > b.total_bias);
743+
return name_a < name_b || (name_a == name_b && a.total_sqract > b.total_sqract);
737744
}
738745
};
739746
std::sort(ts.begin(), ts.end(), tensor_comparer());
@@ -765,11 +772,11 @@ static bool show_statistics(const common_params & params) {
765772
}
766773

767774
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",
768-
layer.c_str(), name.c_str(), tstat.total_bias, tstat.min_bias, tstat.max_bias, tstat.mean_bias,
775+
layer.c_str(), name.c_str(), tstat.total_sqract, tstat.min_sqract, tstat.max_sqract, tstat.mean_sqract,
769776
tstat.stddev, tstat.active * 100.0f, tstat.elements, tstat.entropy,
770777
100.0f * (tstat.entropy / std::log2(tstat.elements)), 100.0f * tstat.zd, tstat.cossim);
771778

772-
const float weighted_bias = tstat.elements * tstat.total_bias;
779+
const float weighted_bias = tstat.elements * tstat.total_sqract;
773780
const float weighted_zd = tstat.elements * tstat.zd;
774781
const float weighted_cossim = tstat.elements * tstat.cossim;
775782

0 commit comments

Comments
 (0)