From e16b2106e4549c133ecda8752cfe8b2a7ddbbb0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C5=82gorzata=20Zagajewska?= Date: Mon, 28 Sep 2026 19:30:54 +0200 Subject: [PATCH] Initialize the ggml timer when building sampler chains so they work without a backend on Windows --- Cargo.lock | 1 + llama-cpp-bindings/Cargo.toml | 1 + llama-cpp-bindings/src/sampling.rs | 2 ++ .../tests/sampler_chain_without_backend.rs | 24 +++++++++++++++++++ 4 files changed, 28 insertions(+) create mode 100644 llama-cpp-bindings/tests/sampler_chain_without_backend.rs diff --git a/Cargo.lock b/Cargo.lock index 01a64b40..33eeeca7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1165,6 +1165,7 @@ checksum = "11d3d7f243d5c5a8b9bb5d6dd2b1602c0cb0b9db1621bafc7ed66e35ff9fe092" name = "llama-cpp-bindings" version = "0.15.0" dependencies = [ + "anyhow", "encoding_rs", "enumflags2", "llama-cpp-bindings-sys", diff --git a/llama-cpp-bindings/Cargo.toml b/llama-cpp-bindings/Cargo.toml index 0d49b00c..a00b03f5 100644 --- a/llama-cpp-bindings/Cargo.toml +++ b/llama-cpp-bindings/Cargo.toml @@ -24,6 +24,7 @@ thiserror = { workspace = true } toktrie = { workspace = true } [dev-dependencies] +anyhow = { workspace = true } llama-cpp-wrapper-error-fixture = { workspace = true } serial_test = { workspace = true } diff --git a/llama-cpp-bindings/src/sampling.rs b/llama-cpp-bindings/src/sampling.rs index d8758173..cd9d3188 100644 --- a/llama-cpp-bindings/src/sampling.rs +++ b/llama-cpp-bindings/src/sampling.rs @@ -364,6 +364,8 @@ impl LlamaSampler { no_perf: bool, ) -> Result { unsafe { + llama_cpp_bindings_sys::ggml_time_init(); + let chain = llama_cpp_bindings_sys::llama_sampler_chain_init( llama_cpp_bindings_sys::llama_sampler_chain_params { no_perf }, ); diff --git a/llama-cpp-bindings/tests/sampler_chain_without_backend.rs b/llama-cpp-bindings/tests/sampler_chain_without_backend.rs new file mode 100644 index 00000000..f8ff4ee0 --- /dev/null +++ b/llama-cpp-bindings/tests/sampler_chain_without_backend.rs @@ -0,0 +1,24 @@ +use anyhow::Result; +use llama_cpp_bindings::sampling::LlamaSampler; +use llama_cpp_bindings::token::LlamaToken; +use llama_cpp_bindings::token::data::LlamaTokenData; +use llama_cpp_bindings::token::data_array::LlamaTokenDataArray; + +#[test] +fn sampler_chain_applies_without_initialized_backend() -> Result<()> { + let sampler_chain = LlamaSampler::chain_simple([LlamaSampler::greedy()?])?; + let mut candidates = LlamaTokenDataArray::new( + vec![ + LlamaTokenData::new(LlamaToken::new(0), 1.0, 0.0), + LlamaTokenData::new(LlamaToken::new(1), 5.0, 0.0), + LlamaTokenData::new(LlamaToken::new(2), 3.0, 0.0), + ], + false, + ); + + candidates.apply_sampler(&sampler_chain)?; + + assert_eq!(candidates.selected_token(), Some(LlamaToken::new(1))); + + Ok(()) +}