Skip to content

Distribute and run LLMs with a single file.

fix(ggml): use `ggml_time_init()` in `ggml_time_ms` and `ggml_time_us` so that `timer_freq` is initialised (#1054)

Davide Eynard0c59e5263e3f
diff --git a/llama.cpp.patches/README.md b/llama.cpp.patches/README.md
index f7ee834f..67c7f7b5 100644
--- a/llama.cpp.patches/README.md
+++ b/llama.cpp.patches/README.md
@@ -268,6 +268,7 @@ can change on a llama.cpp bump — when it does, the asset list in
| `src_models_t5.cpp.patch` | Forward-declares the `graph<false>`/`graph<true>` explicit specializations before `build_arch_graph` so clang's `-std=gnu++23` doesn't reject them as specializations after implicit instantiation |
| `src_models_eagle3.cpp.patch` | Moves `build_arch_graph` to the end of the file, after the `graph<true>`/`graph<false>` constructor specializations, so clang's `-std=gnu++23` doesn't reject them as explicit specializations appearing after the `make_unique<graph<...>>` implicit instantiation point |
| `src_models_dflash.cpp.patch` | Same fix as `eagle3` for the DFlash model (new in b10052): moves `build_arch_graph` to the end of the file, after the `graph<true>`/`graph<false>`/`graph_dsv4` specializations, so clang's `-std=gnu++23` doesn't reject them as explicit specializations after the `make_unique<graph<...>>` implicit instantiation point |
+| `ggml_src_ggml.c.patch` | Makes the `ggml_time_ms()`/`ggml_time_us()` call `ggml_time_init()` lazily. The Windows dylibs link their own copy of `ggml.c`, so the DLL's `timer_freq` is never set by the executable's `ggml_init()` and any `ggml_time_*()` call made from inside the DLL divides by zero (`0xC0000094`, surfaced by Cosmopolitan as SIGFPE). First hit when #1051 enabled `-DGGML_CUDA_USE_GRAPHS`, calling `ggml_time_us()` in `ggml-cuda`. Linux/macOS are unaffected (`clock_gettime`, no static divisor). |
## Creating New Patches
diff --git a/llama.cpp.patches/patches/ggml_src_ggml.c.patch b/llama.cpp.patches/patches/ggml_src_ggml.c.patch
new file mode 100644
index 00000000..6b38ed86
--- /dev/null
+++ b/llama.cpp.patches/patches/ggml_src_ggml.c.patch
@@ -0,0 +1,17 @@
+diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c
+--- a/llama.cpp/ggml/src/ggml.c
++++ b/llama.cpp/ggml/src/ggml.c
+@@ -547,11 +547,13 @@ void ggml_time_init(void) {
+ InitOnceExecuteOnce(&once, ggml_time_init_once, NULL, NULL);
+ }
+ int64_t ggml_time_ms(void) {
++ ggml_time_init(); // llamafile: GPU DLLs carry their own ggml.c; init lazily so timer_freq is never 0
+ LARGE_INTEGER t;
+ QueryPerformanceCounter(&t);
+ return ((t.QuadPart-timer_start) * 1000) / timer_freq;
+ }
+ int64_t ggml_time_us(void) {
++ ggml_time_init(); // llamafile: see ggml_time_ms
+ LARGE_INTEGER t;
+ QueryPerformanceCounter(&t);
+ return ((t.QuadPart-timer_start) * 1000000) / timer_freq;