@@ -116,14 +116,20 @@ static void process_tensor_name(const std::string & input, std::string & layer,
116116 }
117117}
118118
119- static void compute_statistics (std::vector<tensor_statistics> & tstats, const int & i, const std::string & name, const Stats & e, const std::vector<float > & activations) {
119+ static void compute_statistics (std::vector<tensor_statistics> & tstats, const std::string & name, const Stats & e, const std::vector<float > & activations) {
120+ if (activations.empty ()) {
121+ LOG_ERR (" %s: there are no activations for tensor %s. The imatrix may be suboptimal\n " , __func__, name.c_str ());
122+ return ;
123+ }
124+
120125 const float total = std::accumulate (activations.begin (), activations.end (), 0 .0f );
121126 const float max = *std::max_element (activations.begin (), activations.end ());
122127 const float min = *std::min_element (activations.begin (), activations.end ());
123128 const float mean = total / activations.size ();
124129 const float sq_total = std::inner_product (activations.begin (), activations.end (), activations.begin (), 0 .0f );
125- const float dev = std::sqrt ((sq_total / activations.size ()) - (mean * mean));
126- float threshold = min + min * 0 .5f ;
130+ const float variance = (sq_total / activations.size ()) - (mean * mean);
131+ const float dev = std::sqrt (std::max (0 .0f , variance));
132+ float threshold = 1e-6f ;
127133 const int inactive_count = std::count_if (activations.begin (), activations.end (),
128134 [threshold](const float v) { return fabs (v) <= threshold; });
129135 const float active_ratio = 1 - static_cast <float >(inactive_count) / activations.size ();
@@ -138,14 +144,15 @@ static void compute_statistics(std::vector<tensor_statistics> & tstats, const in
138144 }
139145
140146 int z_score = 0 ;
141- for (const auto act : activations) {
142- if (const float p = (act - mean) / dev; p > 1 ) {
143- z_score++;
147+ if (dev > 0 .0f ) {
148+ for (const auto act : activations) {
149+ if (const float p = (act - mean) / dev; p > 1 ) {
150+ z_score++;
151+ }
144152 }
145153 }
146154
147- tstats.emplace_back ();
148- auto & ts = tstats[i];
155+ auto & ts = tstats.emplace_back ();
149156 ts.tensor = name;
150157 ts.stats = e;
151158 ts.total_bias = total;
@@ -464,7 +471,7 @@ bool IMatrixCollector::load_imatrix(const char * fname, std::vector<tensor_stati
464471 e.ncall += ncall;
465472
466473 if (tstats) {
467- compute_statistics (*tstats, i, name_as_vec.data (), e, activations);
474+ compute_statistics (*tstats, name_as_vec.data (), e, activations);
468475 }
469476 }
470477
0 commit comments