]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
check for nans in imatrix and quantize (#7807)
authorslaren <redacted>
Fri, 7 Jun 2024 06:01:29 +0000 (08:01 +0200)
committerGitHub <redacted>
Fri, 7 Jun 2024 06:01:29 +0000 (09:01 +0300)
* imatrix : detect nan/inf values

* quantize : check imatrix for nan/inf values

examples/imatrix/imatrix.cpp
llama.cpp

index 38420041c47817e1418579ce228bed7bff85d709..e18f495630616470947203082e9c476ddb8aaa52 100644 (file)
@@ -151,6 +151,10 @@ bool IMatrixCollector::collect_imatrix(struct ggml_tensor * t, bool ask, void *
                     for (int j = 0; j < (int)src1->ne[0]; ++j) {
                         e.values[e_start + j] += x[j]*x[j];
                         e.counts[e_start + j]++;
+                        if (!std::isfinite(e.values[e_start + j])) {
+                            fprintf(stderr, "%f detected in %s\n", e.values[e_start + j], wname.c_str());
+                            exit(1);
+                        }
                     }
                 }
             }
@@ -183,6 +187,10 @@ bool IMatrixCollector::collect_imatrix(struct ggml_tensor * t, bool ask, void *
             for (int j = 0; j < (int)src1->ne[0]; ++j) {
                 e.values[j] += x[j]*x[j];
                 e.counts[j]++;
+                if (!std::isfinite(e.values[j])) {
+                    fprintf(stderr, "%f detected in %s\n", e.values[j], wname.c_str());
+                    exit(1);
+                }
             }
         }
         if (e.ncall > m_last_call) {
index 32264a0082a907c5b13c0db378737e9706b07406..8b675ea993a38cddfd0c6919af443193b3ed2a44 100644 (file)
--- a/llama.cpp
+++ b/llama.cpp
@@ -15237,6 +15237,14 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const s
         if (imatrix_data) {
             LLAMA_LOG_INFO("================================ Have weights data with %d entries\n",int(imatrix_data->size()));
             qs.has_imatrix = true;
+            // check imatrix for nans or infs
+            for (const auto & kv : *imatrix_data) {
+                for (float f : kv.second) {
+                    if (!std::isfinite(f)) {
+                        throw std::runtime_error(format("imatrix contains non-finite value %f\n", f));
+                    }
+                }
+            }
         }
     }