]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
ggml : fix internal overflow in ggml_time_us on Windows (#1702)
authorgrahameth <redacted>
Mon, 5 Jun 2023 20:11:49 +0000 (22:11 +0200)
committerGitHub <redacted>
Mon, 5 Jun 2023 20:11:49 +0000 (23:11 +0300)
Co-authored-by: grahameth <->
ggml.c

diff --git a/ggml.c b/ggml.c
index 7f9bff995719fb9e951b5d9b776bbf1ccd07c8a4..24f0d2fad19d4356d68c98a34b0e8feb2fa31070 100644 (file)
--- a/ggml.c
+++ b/ggml.c
@@ -404,21 +404,27 @@ void ggml_fp32_to_fp16_row(const float * x, ggml_fp16_t * y, size_t n) {
 //
 
 #if defined(_MSC_VER) || defined(__MINGW32__)
-static int64_t timer_freq;
+static int64_t timer_freq, timer_start;
 void ggml_time_init(void) {
-    LARGE_INTEGER frequency;
-    QueryPerformanceFrequency(&frequency);
-    timer_freq = frequency.QuadPart;
+    LARGE_INTEGER t;
+    QueryPerformanceFrequency(&t);
+    timer_freq = t.QuadPart;
+
+    // The multiplication by 1000 or 1000000 below can cause an overflow if timer_freq
+    // and the uptime is high enough.
+    // We subtract the program start time to reduce the likelihood of that happening.
+    QueryPerformanceCounter(&t);
+    timer_start = t.QuadPart;
 }
 int64_t ggml_time_ms(void) {
     LARGE_INTEGER t;
     QueryPerformanceCounter(&t);
-    return (t.QuadPart * 1000) / timer_freq;
+    return ((t.QuadPart-timer_start) * 1000) / timer_freq;
 }
 int64_t ggml_time_us(void) {
     LARGE_INTEGER t;
     QueryPerformanceCounter(&t);
-    return (t.QuadPart * 1000000) / timer_freq;
+    return ((t.QuadPart-timer_start) * 1000000) / timer_freq;
 }
 #else
 void ggml_time_init(void) {}