Skip to content

Commit c9ee711

Browse files
authored
check for nans in imatrix and quantize (#7807)
* imatrix : detect nan/inf values * quantize : check imatrix for nan/inf values
1 parent ee459f4 commit c9ee711

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

examples/imatrix/imatrix.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ bool IMatrixCollector::collect_imatrix(struct ggml_tensor * t, bool ask, void *
151151
for (int j = 0; j < (int)src1->ne[0]; ++j) {
152152
e.values[e_start + j] += x[j]*x[j];
153153
e.counts[e_start + j]++;
154+
if (!std::isfinite(e.values[e_start + j])) {
155+
fprintf(stderr, "%f detected in %s\n", e.values[e_start + j], wname.c_str());
156+
exit(1);
157+
}
154158
}
155159
}
156160
}
@@ -183,6 +187,10 @@ bool IMatrixCollector::collect_imatrix(struct ggml_tensor * t, bool ask, void *
183187
for (int j = 0; j < (int)src1->ne[0]; ++j) {
184188
e.values[j] += x[j]*x[j];
185189
e.counts[j]++;
190+
if (!std::isfinite(e.values[j])) {
191+
fprintf(stderr, "%f detected in %s\n", e.values[j], wname.c_str());
192+
exit(1);
193+
}
186194
}
187195
}
188196
if (e.ncall > m_last_call) {

llama.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15237,6 +15237,14 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const s
1523715237
if (imatrix_data) {
1523815238
LLAMA_LOG_INFO("================================ Have weights data with %d entries\n",int(imatrix_data->size()));
1523915239
qs.has_imatrix = true;
15240+
// check imatrix for nans or infs
15241+
for (const auto & kv : *imatrix_data) {
15242+
for (float f : kv.second) {
15243+
if (!std::isfinite(f)) {
15244+
throw std::runtime_error(format("imatrix contains non-finite value %f\n", f));
15245+
}
15246+
}
15247+
}
1524015248
}
1524115249
}
1524215250

0 commit comments

Comments
 (0)