Skip to content

armgit / ente

Public Imported from GitHub
0

💚 End-to-end encrypted cloud for everything.

[ensu] Fix inference templates and platform integration

setalpe7af210e74d9
diff --git a/.github/workflows/android-lint.yml b/.github/workflows/android-lint.yml
index 08aeebc356..415839be70 100644
--- a/.github/workflows/android-lint.yml
+++ b/.github/workflows/android-lint.yml
@@ -30,7 +30,7 @@ jobs:
- name: Install Vulkan build dependencies
run: |
sudo apt-get update
- sudo apt-get install -y libvulkan-dev glslc
+ sudo apt-get install -y libvulkan-dev glslc spirv-headers
- name: Restore Gradle cache
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
diff --git a/.github/workflows/ensu-build.yml b/.github/workflows/ensu-build.yml
index 7595e13d7c..013660b8bb 100644
--- a/.github/workflows/ensu-build.yml
+++ b/.github/workflows/ensu-build.yml
@@ -332,7 +332,7 @@ jobs:
- name: Install Vulkan build dependencies
run: |
sudo apt-get update
- sudo apt-get install -y libvulkan-dev glslc
+ sudo apt-get install -y libvulkan-dev glslc spirv-headers
- run: cargo codegen native
working-directory: rust
diff --git a/.github/workflows/rust-lint.yml b/.github/workflows/rust-lint.yml
index 7566b7c0aa..2eecf185d9 100644
--- a/.github/workflows/rust-lint.yml
+++ b/.github/workflows/rust-lint.yml
@@ -25,7 +25,7 @@ jobs:
- name: Install Linux dependencies
run: |
sudo apt-get update
- sudo apt-get install -y libwebkit2gtk-4.1-dev libssl-dev libayatana-appindicator3-dev librsvg2-dev libvulkan-dev glslc libdbus-1-dev
+ sudo apt-get install -y libwebkit2gtk-4.1-dev libssl-dev libayatana-appindicator3-dev librsvg2-dev libvulkan-dev glslc spirv-headers libdbus-1-dev
# Restore-only; primed on main by warm-caches.yml.
- name: Restore Rust cache
diff --git a/.github/workflows/rust-test.yml b/.github/workflows/rust-test.yml
index 693059c756..658f099f64 100644
--- a/.github/workflows/rust-test.yml
+++ b/.github/workflows/rust-test.yml
@@ -30,7 +30,7 @@ jobs:
- name: Install Linux dependencies
run: |
sudo apt-get update
- sudo apt-get install -y libwebkit2gtk-4.1-dev libssl-dev libayatana-appindicator3-dev librsvg2-dev libvulkan-dev glslc libdbus-1-dev dbus gnome-keyring libglib2.0-bin
+ sudo apt-get install -y libwebkit2gtk-4.1-dev libssl-dev libayatana-appindicator3-dev librsvg2-dev libvulkan-dev glslc spirv-headers libdbus-1-dev dbus gnome-keyring libglib2.0-bin
- name: Setup go
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
diff --git a/.github/workflows/warm-caches.yml b/.github/workflows/warm-caches.yml
index 927940d91b..f29f697dd3 100644
--- a/.github/workflows/warm-caches.yml
+++ b/.github/workflows/warm-caches.yml
@@ -37,7 +37,7 @@ jobs:
- name: Install Linux dependencies
run: |
sudo apt-get update
- sudo apt-get install -y libwebkit2gtk-4.1-dev libssl-dev libayatana-appindicator3-dev librsvg2-dev libvulkan-dev glslc libdbus-1-dev
+ sudo apt-get install -y libwebkit2gtk-4.1-dev libssl-dev libayatana-appindicator3-dev librsvg2-dev libvulkan-dev glslc spirv-headers libdbus-1-dev
- name: Setup Rust cache
uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
@@ -286,7 +286,7 @@ jobs:
- name: Install Vulkan build dependencies
run: |
sudo apt-get update
- sudo apt-get install -y libvulkan-dev glslc
+ sudo apt-get install -y libvulkan-dev glslc spirv-headers
- run: cargo codegen native
working-directory: rust
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt
index b3a6024ce6..f5e9add0a4 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt
@@ -395,7 +395,8 @@ class LlmProvider(
model
.newTitleContext(
chatContextSize = checkNotNull(currentContextLength).toUInt(),
- nThreads = generationThreads(),
+ nThreads = max(1, Runtime.getRuntime().availableProcessors() - 1),
+ nThreadsGeneration = generationThreads(),
)
.use { context ->
coroutine.ensureActive()
@@ -569,7 +570,8 @@ class LlmProvider(
val contextParams =
LlmContextParams(
contextSize = currentContextLength,
- nThreads = generationThreads(),
+ nThreads = null,
+ nThreadsGeneration = generationThreads(),
nBatch = LlmModelSelection(key.id, key.requestedContextLength).batchSize,
)
loadedContext?.destroy()
@@ -684,7 +686,7 @@ class LlmProvider(
private fun loadWithFallbacks(selection: LlmModelSelection, modelFile: File) {
val contexts = selection.contextCandidates()
- val threads = generationThreads()
+ val threads = max(1, Runtime.getRuntime().availableProcessors() - 1)
val batch = selection.batchSize
val modelParams =
@@ -705,6 +707,7 @@ class LlmProvider(
LlmContextParams(
contextSize = ctx,
nThreads = threads,
+ nThreadsGeneration = generationThreads(),
nBatch = batch,
)
loadedContext = model.newContext(contextParams)
diff --git a/apple/apps/ensu/Ensu/Services/LlmProvider.swift b/apple/apps/ensu/Ensu/Services/LlmProvider.swift
index 7bf73697f6..15f6695506 100644
--- a/apple/apps/ensu/Ensu/Services/LlmProvider.swift
+++ b/apple/apps/ensu/Ensu/Services/LlmProvider.swift
@@ -473,7 +473,8 @@ actor LlmProvider {
defer { generationControl.withLock { $0 = nil } }
let context = try model.newTitleContext(
chatContextSize: loadedContext.contextSize(),
- nThreads: Int32(max(1, ProcessInfo.processInfo.activeProcessorCount - 1))
+ nThreads: Int32(max(1, ProcessInfo.processInfo.activeProcessorCount - 1)),
+ nThreadsGeneration: nil
)
let outputTokens = 48
let inputTokens = max(0, min(2000, Int(context.contextSize()) - outputTokens))
@@ -706,7 +707,7 @@ actor LlmProvider {
let selection = LlmModelSelection(id: key.id, contextLength: key.requestedContextLength)
let contextParams = LlmContextParams(
contextSize: currentContextLength.map(Int32.init), nThreads: nil,
- nBatch: Int32(selection.batchSize))
+ nThreadsGeneration: nil, nBatch: Int32(selection.batchSize))
loadedContext = nil
loadedContext = try? model.newContext(params: contextParams)
}
@@ -755,6 +756,7 @@ actor LlmProvider {
do {
let contextParams = LlmContextParams(
contextSize: Int32(contextSize), nThreads: Int32(threadCount),
+ nThreadsGeneration: nil,
nBatch: Int32(selection.batchSize))
let context = try model.newContext(params: contextParams)
loadedContext = context
diff --git a/apple/apps/ensu/EnsuTests/LlmIntegrationTests.swift b/apple/apps/ensu/EnsuTests/LlmIntegrationTests.swift
index e81c9f0a90..d70ad5e6ae 100644
--- a/apple/apps/ensu/EnsuTests/LlmIntegrationTests.swift
+++ b/apple/apps/ensu/EnsuTests/LlmIntegrationTests.swift
@@ -48,7 +48,8 @@ final class LlmIntegrationTests: XCTestCase {
)
let context = try model.newContext(
- params: LlmContextParams(contextSize: 2048, nThreads: nil, nBatch: nil)
+ params: LlmContextParams(
+ contextSize: 2048, nThreads: nil, nThreadsGeneration: nil, nBatch: nil)
)
let request = LlmChatRequest(
@@ -98,7 +99,8 @@ final class LlmIntegrationTests: XCTestCase {
)
let context = try model.newContext(
- params: LlmContextParams(contextSize: 2048, nThreads: nil, nBatch: nil)
+ params: LlmContextParams(
+ contextSize: 2048, nThreads: nil, nThreadsGeneration: nil, nBatch: nil)
)
let request = LlmChatRequest(
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index c4a8f5a20c..053ded9dc0 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2620,13 +2620,11 @@ dependencies = [
name = "ente-ensu"
version = "0.0.0"
dependencies = [
- "cc",
"ente-assets",
"ente-core",
"ente-ensu-crypto",
"ente-image",
"llama-cpp-2",
- "llama-cpp-sys-2",
"ort",
"ort-sys",
"regex",
diff --git a/rust/bindings/uniffi/ensu/src/llm.rs b/rust/bindings/uniffi/ensu/src/llm.rs
index 64ad6a116a..7002cd95dc 100644
--- a/rust/bindings/uniffi/ensu/src/llm.rs
+++ b/rust/bindings/uniffi/ensu/src/llm.rs
@@ -43,6 +43,7 @@ pub struct LlmModelLoadParams {
pub struct LlmContextParams {
pub context_size: Option<i32>,
pub n_threads: Option<i32>,
+ pub n_threads_generation: Option<i32>,
pub n_batch: Option<i32>,
}
@@ -114,9 +115,15 @@ impl LlmModel {
&self,
chat_context_size: u32,
n_threads: Option<i32>,
+ n_threads_generation: Option<i32>,
) -> Result<Arc<LlmContext>, LlmError> {
- let handle = llm::Context::new_title(&self.handle, chat_context_size, n_threads)
- .map_err(LlmError::from)?;
+ let handle = llm::Context::new_title(
+ &self.handle,
+ chat_context_size,
+ n_threads,
+ n_threads_generation,
+ )
+ .map_err(LlmError::from)?;
Ok(Arc::new(LlmContext { handle }))
}
@@ -255,6 +262,7 @@ impl From<LlmContextParams> for llm::ContextParams {
Self {
context_size: value.context_size,
n_threads: value.n_threads,
+ n_threads_generation: value.n_threads_generation,
n_batch: value.n_batch,
}
}
diff --git a/rust/checks/lint-exceptions/suppressions.json b/rust/checks/lint-exceptions/suppressions.json
index 7c169d4f0a..97f5c94067 100644
--- a/rust/checks/lint-exceptions/suppressions.json
+++ b/rust/checks/lint-exceptions/suppressions.json
@@ -30,7 +30,6 @@
"rust/crates/core/src/crypto/types.rs": ["clippy::expect_used"],
"rust/crates/ensu/src/config.rs": ["clippy::expect_used"],
"rust/crates/ensu/src/config/knowledge.rs": ["clippy::expect_used"],
- "rust/crates/ensu/src/llm/chat_template_bridge.rs": ["unsafe_code"],
"rust/crates/ensu/src/llm/generate.rs": ["clippy::expect_used"],
"rust/crates/ensu/src/model/mod.rs": ["clippy::expect_used"],
"rust/crates/ensu/src/platform.rs": ["unsafe_code"],
diff --git a/rust/crates/ensu/Cargo.toml b/rust/crates/ensu/Cargo.toml
index 4a3831a9c4..f2d16c6332 100644
--- a/rust/crates/ensu/Cargo.toml
+++ b/rust/crates/ensu/Cargo.toml
@@ -7,7 +7,6 @@ ente-assets.workspace = true
ente-core.workspace = true
ente-ensu-crypto.workspace = true
ente-image.workspace = true
-llama-cpp-sys-2 = { version = "=0.1.158", default-features = false, features = ["common"] }
regex.workspace = true
rubato = { version = "5.0.0", optional = true }
rusqlite = { workspace = true, default-features = true, optional = true }
@@ -25,16 +24,13 @@ zstd = "0.14.0"
[dev-dependencies]
tempfile.workspace = true
-[build-dependencies]
-cc = "1.2.61"
-
# Keep `mtmd` on every platform for multimodal support.
# macOS disables OpenMP because Apple's clang lacks libomp. llama.cpp uses its
# own thread pool, Accelerate, and Metal on arm64 instead.
[target.'cfg(target_os = "macos")'.dependencies]
-llama-cpp-2 = { version = "=0.1.158", default-features = false, features = ["common", "mtmd"] }
+llama-cpp-2 = { version = "=0.1.158", default-features = false, features = ["mtmd"] }
-# Linux Vulkan needs a system Vulkan loader and glslc.
+# Linux Vulkan needs a system Vulkan loader, glslc, and SPIRV-Headers.
[target.'cfg(target_os = "linux")'.dependencies]
llama-cpp-2 = { version = "=0.1.158", features = ["mtmd", "vulkan"] }
diff --git a/rust/crates/ensu/build.rs b/rust/crates/ensu/build.rs
deleted file mode 100644
index 5df3142f9c..0000000000
--- a/rust/crates/ensu/build.rs
+++ /dev/null
@@ -1,36 +0,0 @@
-use std::{env, error::Error, fs, path::PathBuf};
-
-fn main() -> Result<(), Box<dyn Error>> {
- println!("cargo:rerun-if-changed=native/chat.cpp");
- println!("cargo:rerun-if-env-changed=DEP_LLAMA_GGML_CMAKE_DIR");
-
- let cmake_dir = PathBuf::from(env::var("DEP_LLAMA_GGML_CMAKE_DIR")?);
- let output = cmake_dir
- .parent()
- .and_then(|path| path.parent())
- .ok_or("Invalid llama.cpp CMake directory")?;
- let cache_path = output.join("build/CMakeCache.txt");
- println!("cargo:rerun-if-changed={}", cache_path.display());
- let cache = fs::read_to_string(cache_path)?;
- let source = cache
- .lines()
- .find_map(|line| line.strip_prefix("CMAKE_HOME_DIRECTORY:INTERNAL="))
- .ok_or("llama.cpp source directory missing from CMake cache")?;
- let source = PathBuf::from(source);
-
- let mut build = cc::Build::new();
- build
- .cpp(true)
- .std("c++17")
- .file("native/chat.cpp")
- .include(source.join("common"))
- .include(source.join("include"))
- .include(source.join("ggml/include"))
- .include(source.join("vendor"))
- .cpp_link_stdlib(None);
- if build.get_compiler().is_like_msvc() {
- build.flag("/EHsc");
- }
- build.compile("ensu_chat");
- Ok(())
-}
diff --git a/rust/crates/ensu/native/chat.cpp b/rust/crates/ensu/native/chat.cpp
deleted file mode 100644
index 08e4f68ec8..0000000000
--- a/rust/crates/ensu/native/chat.cpp
+++ /dev/null
@@ -1,78 +0,0 @@
-#include "chat.h"
-
-#include <cstdlib>
-#include <cstring>
-#include <exception>
-#include <stdexcept>
-#include <string>
-#include <utility>
-#include <vector>
-
-extern "C" {
-struct ensu_chat_result {
- char * data;
- size_t len;
- int status;
-};
-
-static ensu_chat_result copy_result(const char * text, size_t len, int status) noexcept {
- auto * data = static_cast<char *>(std::malloc(len + 1));
- if (!data) {
- return {nullptr, 0, 2};
- }
- std::memcpy(data, text, len);
- data[len] = '\0';
- return {data, len, status};
-}
-
-ensu_chat_result ensu_chat_render(const char * source, const char * bos, const char * eos,
- const char * messages_json, bool add_assistant) noexcept {
- try {
- auto messages = common_chat_msgs_parse_oaicompat(common_json::parse(messages_json));
- std::vector<const char *> builtins(llama_chat_builtin_templates(nullptr, 0));
- llama_chat_builtin_templates(builtins.data(), builtins.size());
- for (const auto * builtin : builtins) {
- if (std::strcmp(source, builtin) != 0 || std::strcmp(source, "chatml") == 0) {
- continue;
- }
- std::vector<llama_chat_message> chat;
- for (const auto & message : messages) {
- if (message.role.find('\0') != std::string::npos || message.content.find('\0') != std::string::npos) {
- throw std::runtime_error("Invalid built-in chat message: embedded NUL");
- }
- chat.push_back({message.role.c_str(), message.content.c_str()});
- }
- const auto length = llama_chat_apply_template(source, chat.data(), chat.size(), add_assistant, nullptr, 0);
- if (length < 0) {
- throw std::runtime_error("Failed to apply built-in chat template");
- }
- std::string prompt(length, '\0');
- const auto written = llama_chat_apply_template(source, chat.data(), chat.size(), add_assistant, prompt.data(), length);
- if (written != length) {
- throw std::runtime_error("Invalid built-in chat template output length");
- }
- return copy_result(prompt.data(), prompt.size(), 0);
- }
- auto templates = common_chat_templates_init(nullptr, source[0] ? source : "chatml", bos, eos);
- common_chat_templates_inputs inputs;
- inputs.messages = std::move(messages);
- inputs.add_generation_prompt = add_assistant;
- inputs.use_jinja = true;
- inputs.enable_thinking = false;
- inputs.chat_template_kwargs["enable_thinking"] = "false";
- inputs.add_bos = false;
- inputs.add_eos = false;
- const auto result = common_chat_templates_apply(templates.get(), inputs);
- return copy_result(result.prompt.data(), result.prompt.size(), 0);
- } catch (const std::exception & error) {
- return copy_result(error.what(), std::strlen(error.what()), 1);
- } catch (...) {
- constexpr char error[] = "Unknown chat template error";
- return copy_result(error, sizeof(error) - 1, 1);
- }
-}
-
-void ensu_chat_free(char * data) noexcept {
- std::free(data);
-}
-}
diff --git a/rust/crates/ensu/src/llm/chat_template_bridge.rs b/rust/crates/ensu/src/llm/chat_template_bridge.rs
deleted file mode 100644
index a6e844b76a..0000000000
--- a/rust/crates/ensu/src/llm/chat_template_bridge.rs
+++ /dev/null
@@ -1,80 +0,0 @@
-use std::ffi::{CString, c_char};
-
-use super::{Error, format_error};
-
-#[repr(C)]
-struct RenderResult {
- data: *mut c_char,
- len: usize,
- status: i32,
-}
-
-#[expect(
- unsafe_code,
- reason = "Declarations for the private, synchronous C++ chat bridge"
-)]
-unsafe extern "C" {
- fn ensu_chat_render(
- source: *const c_char,
- bos: *const c_char,
- eos: *const c_char,
- messages: *const c_char,
- add_assistant: bool,
- ) -> RenderResult;
- fn ensu_chat_free(data: *mut c_char);
-}
-
-impl Drop for RenderResult {
- #[expect(
- unsafe_code,
- reason = "The bridge allocated this buffer and frees it with the same allocator; null is allowed"
- )]
- fn drop(&mut self) {
- unsafe { ensu_chat_free(self.data) };
- }
-}
-
-#[expect(
- unsafe_code,
- reason = "CString inputs live for the synchronous call; the result owns len initialized bytes until Drop frees them"
-)]
-pub(super) fn render(
- source: &str,
- bos: &str,
- eos: &str,
- messages: &str,
- add_assistant: bool,
-) -> Result<String, Error> {
- let cstring = |value: &str| {
- CString::new(value)
- .map_err(|err| Error::InvalidInput(format_error("Invalid template input", err)))
- };
- let source = cstring(source)?;
- let bos = cstring(bos)?;
- let eos = cstring(eos)?;
- let messages = cstring(messages)?;
- let result = unsafe {
- ensu_chat_render(
- source.as_ptr(),
- bos.as_ptr(),
- eos.as_ptr(),
- messages.as_ptr(),
- add_assistant,
- )
- };
- let failure = |message| Error::Llama {
- op: "Failed to apply chat template",
- message,
- };
- if result.data.is_null() {
- return Err(failure("Could not allocate template output".to_owned()));
- }
- let bytes = unsafe { std::slice::from_raw_parts(result.data.cast::<u8>(), result.len) };
- let text = std::str::from_utf8(bytes)
- .map_err(|err| failure(err.to_string()))?
- .to_owned();
- if result.status != 0 {
- return Err(failure(text));
- }
- Ok(text)
-}
diff --git a/rust/crates/ensu/src/llm/context.rs b/rust/crates/ensu/src/llm/context.rs
index 468a33a941..e312672cf0 100644
--- a/rust/crates/ensu/src/llm/context.rs
+++ b/rust/crates/ensu/src/llm/context.rs
@@ -49,10 +49,11 @@ impl Context {
model: &ModelRef,
chat_context_size: u32,
n_threads: Option<i32>,
+ n_threads_generation: Option<i32>,
) -> Result<ContextRef, Error> {
Self::new(
model,
- super::memory::title_context_params(chat_context_size, n_threads),
+ super::memory::title_context_params(chat_context_size, n_threads, n_threads_generation),
)
}
@@ -176,6 +177,9 @@ impl Context {
pub struct ContextParams {
pub context_size: Option<i32>,
pub n_threads: Option<i32>,
+ /// Override generation threads without changing prompt-processing threads.
+ #[serde(default)]
+ pub n_threads_generation: Option<i32>,
pub n_batch: Option<i32>,
}
@@ -339,6 +343,28 @@ fn mtmd_cache_key_and_params(
Ok((key, params))
}
+fn with_thread_counts(
+ mut params: LlamaContextParams,
+ threads: Option<i32>,
+ generation: Option<i32>,
+) -> Result<LlamaContextParams, Error> {
+ if let Some(threads) = threads {
+ if threads <= 0 {
+ return Err(Error::InvalidInput("n_threads must be > 0".to_owned()));
+ }
+ params = params.with_n_threads(threads).with_n_threads_batch(threads);
+ }
+ if let Some(threads) = generation {
+ if threads <= 0 {
+ return Err(Error::InvalidInput(
+ "n_threads_generation must be > 0".to_owned(),
+ ));
+ }
+ params = params.with_n_threads(threads);
+ }
+ Ok(params)
+}
+
impl LocalContext {
pub fn new(model: &ModelRef, params: ContextParams) -> Result<Self, Error> {
let mut context_params = LlamaContextParams::default();
@@ -351,14 +377,11 @@ impl LocalContext {
context_params = context_params.with_n_ctx(Some(context_size));
}
- if let Some(n_threads) = params.n_threads {
- if n_threads <= 0 {
- return Err(Error::InvalidInput("n_threads must be > 0".to_string()));
- }
- context_params = context_params
- .with_n_threads(n_threads)
- .with_n_threads_batch(n_threads);
- }
+ context_params = with_thread_counts(
+ context_params,
+ params.n_threads,
+ params.n_threads_generation,
+ )?;
if let Some(n_batch) = params.n_batch {
let n_batch = u32::try_from(n_batch)
@@ -462,3 +485,37 @@ impl LocalContext {
})
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn generation_override_preserves_prompt_threads_and_existing_callers() {
+ let defaults = LlamaContextParams::default();
+ for (threads, generation, expected_generation, expected_prompt) in [
+ (Some(7), Some(3), 3, 7),
+ (Some(4), None, 4, 4),
+ (None, Some(3), 3, defaults.n_threads_batch()),
+ (None, None, defaults.n_threads(), defaults.n_threads_batch()),
+ ] {
+ let params =
+ with_thread_counts(LlamaContextParams::default(), threads, generation).unwrap();
+ assert_eq!(params.n_threads(), expected_generation);
+ assert_eq!(params.n_threads_batch(), expected_prompt);
+ }
+ for (threads, generation) in [
+ (Some(0), None),
+ (Some(-1), Some(7)),
+ (Some(3), Some(0)),
+ (None, Some(-1)),
+ ] {
+ assert!(
+ with_thread_counts(LlamaContextParams::default(), threads, generation).is_err()
+ );
+ }
+ let old_request: ContextParams =
+ serde_json::from_str(r#"{"context_size":512,"n_threads":4,"n_batch":128}"#).unwrap();
+ assert_eq!(old_request.n_threads_generation, None);
+ }
+}
diff --git a/rust/crates/ensu/src/llm/generate.rs b/rust/crates/ensu/src/llm/generate.rs
index 54ed1ac82c..d36cc5220c 100644
--- a/rust/crates/ensu/src/llm/generate.rs
+++ b/rust/crates/ensu/src/llm/generate.rs
@@ -250,8 +250,8 @@ impl GenerationJob<'_> {
for sample_index in 0..self.max_tokens {
check_cancelled(self.cancel_flag)?;
+ // Sampling also accepts the token into the sampler's history.
let token = sampler.sample(ctx, logits_index);
- sampler.accept(token);
check_cancelled(self.cancel_flag)?;
if ctx.model.vocab().is_eog(token) {
diff --git a/rust/crates/ensu/src/llm/memory.rs b/rust/crates/ensu/src/llm/memory.rs
index 8a2d06606c..9aa2788813 100644
--- a/rust/crates/ensu/src/llm/memory.rs
+++ b/rust/crates/ensu/src/llm/memory.rs
@@ -77,10 +77,12 @@ pub fn memory_budget(
pub(super) fn title_context_params(
chat_context_size: u32,
n_threads: Option<i32>,
+ n_threads_generation: Option<i32>,
) -> ContextParams {
ContextParams {
context_size: Some(chat_context_size.min(1024) as i32),
n_threads,
+ n_threads_generation,
n_batch: Some(128),
}
}
@@ -101,9 +103,11 @@ mod tests {
#[test]
fn title_context_caps_memory_without_raising_the_chat_limit() {
for (chat_context_size, expected) in [(512, 512), (4096, 1024)] {
- let params = title_context_params(chat_context_size, Some(2));
+ let params = title_context_params(chat_context_size, Some(7), Some(3));
assert_eq!(params.context_size, Some(expected));
assert_eq!(params.n_batch, Some(128));
+ assert_eq!(params.n_threads, Some(7));
+ assert_eq!(params.n_threads_generation, Some(3));
}
}
diff --git a/rust/crates/ensu/src/llm/mod.rs b/rust/crates/ensu/src/llm/mod.rs
index 0d65518b0b..3896090d15 100644
--- a/rust/crates/ensu/src/llm/mod.rs
+++ b/rust/crates/ensu/src/llm/mod.rs
@@ -1,4 +1,3 @@
-mod chat_template_bridge;
mod context;
mod embed;
mod event;
diff --git a/rust/crates/ensu/src/llm/template.rs b/rust/crates/ensu/src/llm/template.rs
index 48bfcd0559..d4219a7e1e 100644
--- a/rust/crates/ensu/src/llm/template.rs
+++ b/rust/crates/ensu/src/llm/template.rs
@@ -1,6 +1,29 @@
-use llama_cpp_2::model::LlamaModel;
+use llama_cpp_2::model::{LlamaChatMessage, LlamaChatTemplate, LlamaModel};
-use super::{ChatMessage, Error, chat_template_bridge, format_error};
+use super::{ChatMessage, Error, format_error};
+
+// Exact tokenizer.chat_template fingerprints from the GGUF revisions in config.rs.
+// A new or modified template requires a formatter review; never guess from token strings.
+// Source: Hugging Face ente-ai/LFM2.5-VL-1.6B-GGUF @ b2995f54e17fd7ec31e9cb399ade8fedfd51624d
+const LFM_SHA256: [u8; 32] = [
+ 0x01, 0x3e, 0xed, 0x60, 0x54, 0x64, 0x34, 0xb6, 0x96, 0x7e, 0x34, 0x83, 0x15, 0x3d, 0x8c, 0x5c,
+ 0x37, 0xab, 0xcb, 0x1d, 0x66, 0x7f, 0x8b, 0x1f, 0x91, 0x46, 0x83, 0xf2, 0xa9, 0x41, 0x15, 0x31,
+];
+// Sources: Hugging Face ente-ai GGUFs:
+// gemma-4-E4B-it-GGUF @ f0089e04ac8494e513619d18b44c829c6b815440
+// gemma-4-E2B-it-GGUF @ d9f70b02c9a2193b7263daee865dfa93276fd99a
+const GEMMA_SHA256: [u8; 32] = [
+ 0x24, 0x1c, 0x50, 0xd8, 0x6b, 0xdf, 0xe5, 0xe4, 0x33, 0x07, 0xda, 0x87, 0xf5, 0x59, 0xcd, 0x24,
+ 0x16, 0xaa, 0xcd, 0x67, 0xa8, 0xde, 0x46, 0xc1, 0x5a, 0xcc, 0x01, 0x05, 0xef, 0x22, 0x00, 0xb7,
+];
+// Sources: Hugging Face ente-ai GGUFs:
+// Qwen3.5-0.8B-GGUF @ f44fc9bf306e407078288aee9ff7a83b457d260a
+// Qwen3.5-2B-GGUF @ 4cd5d68754a443dc390533792bf345ad219c0b41
+// Qwen3.5-4B-GGUF @ 9b67f8db9bedc8c10f524ac08193b58fa9b20ac7
+const QWEN_SHA256: [u8; 32] = [
+ 0x7f, 0x0e, 0x52, 0x90, 0x32, 0xc2, 0x51, 0x83, 0xbc, 0xd6, 0x6c, 0x7f, 0x23, 0x8d, 0xa2, 0xd3,
+ 0x77, 0xf4, 0x3b, 0xe7, 0x54, 0xa9, 0x4e, 0x27, 0x25, 0xa5, 0x8c, 0x4e, 0x16, 0xd2, 0xed, 0x67,
+];
pub(super) fn build_chat_prompt(
model: &LlamaModel,
@@ -16,6 +39,25 @@ pub(super) fn build_chat_prompt(
.and_then(|template| template.to_string().ok())
.unwrap_or_else(|| "chatml".to_owned()),
};
+ // Preserve llama.cpp's named legacy templates through its existing safe Rust
+ // API. Known model templates are rendered below in Rust without a Jinja engine.
+ if !source.is_empty()
+ && source != "chatml"
+ && source
+ .bytes()
+ .all(|byte| byte.is_ascii_alphanumeric() || b"-._".contains(&byte))
+ {
+ let template = LlamaChatTemplate::new(&source)
+ .map_err(|err| Error::InvalidInput(format_error("Invalid template", err)))?;
+ let chat = messages
+ .iter()
+ .map(|message| LlamaChatMessage::new(message.role.clone(), message.content.clone()))
+ .collect::<Result<Vec<_>, _>>()
+ .map_err(|err| Error::InvalidInput(format_error("Invalid chat message", err)))?;
+ if let Ok(prompt) = model.apply_chat_template(&template, &chat, add_assistant) {
+ return Ok(prompt);
+ }
+ }
let vocab = model.vocab();
let special_token = |token: llama_cpp_2::token::LlamaToken| {
if token.0 < 0 {
@@ -33,6 +75,34 @@ pub(super) fn build_chat_prompt(
)
}
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum Template {
+ ChatMl,
+ Lfm,
+ Gemma,
+ Qwen,
+}
+
+impl Template {
+ fn identify(source: &str) -> Result<Self, Error> {
+ if source.is_empty() || source == "chatml" {
+ return Ok(Self::ChatMl);
+ }
+ match ente_ensu_crypto::sha256(source.as_bytes()) {
+ LFM_SHA256 => Ok(Self::Lfm),
+ GEMMA_SHA256 => Ok(Self::Gemma),
+ QWEN_SHA256 => Ok(Self::Qwen),
+ _ => Err(Error::Unsupported(
+ "This model's chat template is not supported by this version of Ensu",
+ )),
+ }
+ }
+}
+
+// ChatMessage currently contains only a role and string content. These formatters
+// reproduce that input surface with thinking disabled, including media markers
+// already inserted into content. Structured tool calls/content parts would need
+// their own implementation and reference cases before extending ChatMessage.
fn render(
source: &str,
bos: &str,
@@ -40,221 +110,421 @@ fn render(
messages: &[ChatMessage],
add_assistant: bool,
) -> Result<String, Error> {
- let messages = serde_json::to_string(messages)
- .map_err(|err| Error::InvalidInput(format_error("Invalid chat messages", err)))?;
- chat_template_bridge::render(source, bos, eos, &messages, add_assistant)
+ if [source, bos, eos].iter().any(|value| value.contains('\0')) {
+ return Err(Error::InvalidInput(
+ "Invalid template input: embedded NUL".to_owned(),
+ ));
+ }
+ match Template::identify(source)? {
+ Template::ChatMl => {
+ let mut prompt = String::new();
+ for message in messages {
+ chatml_turn(&mut prompt, role(message), &message.content);
+ }
+ if add_assistant {
+ prompt.push_str("<|im_start|>assistant\n");
+ }
+ Ok(prompt)
+ }
+ Template::Lfm => Ok(render_lfm(bos, messages, add_assistant)),
+ Template::Gemma => Ok(render_gemma(bos, messages, add_assistant)),
+ Template::Qwen => render_qwen(messages, add_assistant),
+ }
}
-#[cfg(test)]
-mod tests {
- use super::*;
+fn role(message: &ChatMessage) -> &str {
+ // The original llama.cpp common renderer normalizes developer to system for
+ // all three supported templates.
+ if message.role == "developer" {
+ "system"
+ } else {
+ &message.role
+ }
+}
- struct Fixture {
- model_id: &'static str,
- template: &'static str,
- bos: &'static str,
- eos: &'static str,
+fn trim(text: &str) -> &str {
+ // Match the pinned llama.cpp Jinja runtime's ASCII isspace, including VT.
+ text.trim_matches([' ', '\t', '\n', '\r', '\u{b}', '\u{c}'])
+}
+
+fn chatml_turn(prompt: &mut String, role: &str, content: &str) {
+ prompt.push_str("<|im_start|>");
+ prompt.push_str(role);
+ prompt.push('\n');
+ prompt.push_str(content);
+ prompt.push_str("<|im_end|>\n");
+}
+
+fn template_error(message: &str) -> Error {
+ Error::Llama {
+ op: "Failed to apply chat template",
+ message: message.to_owned(),
}
+}
- fn message(role: &str, content: &str) -> ChatMessage {
- ChatMessage {
- role: role.to_owned(),
- content: content.to_owned(),
+fn render_lfm(bos: &str, mut messages: &[ChatMessage], add_assistant: bool) -> String {
+ let mut prompt = bos.to_owned();
+ if let Some(first) = messages.first()
+ && role(first) == "system"
+ {
+ if !first.content.is_empty() {
+ chatml_turn(&mut prompt, "system", &first.content);
}
+ messages = &messages[1..];
+ }
+ let last_assistant = messages
+ .iter()
+ .rposition(|message| role(message) == "assistant");
+ for (index, message) in messages.iter().enumerate() {
+ let content = if role(message) == "assistant" && Some(index) != last_assistant {
+ message
+ .content
+ .rsplit_once("</think>")
+ .map_or(message.content.as_str(), |(_, tail)| trim(tail))
+ } else {
+ &message.content
+ };
+ chatml_turn(&mut prompt, role(message), content);
}
+ if add_assistant {
+ prompt.push_str("<|im_start|>assistant\n");
+ }
+ prompt
+}
- #[test]
- fn default_templates_preserve_turns_unicode_images_and_assistant_prefixes() {
- for fixture in DEFAULT_TEMPLATES {
- let messages = [
- message("system", "Be concise."),
- message("user", "नमस्ते 🐈\n<__media__>"),
- message("assistant", "A cat."),
- message("user", "What colour?"),
- ];
- let gemma = fixture.model_id.starts_with("gemma");
- let (start, end, assistant) = if gemma {
- ("<|turn>", "<turn|>", "model")
- } else {
- ("<|im_start|>", "<|im_end|>", "assistant")
- };
- let mut expected = fixture.bos.to_owned();
- for msg in &messages {
- let role = if msg.role == "assistant" {
- assistant
+fn render_gemma(bos: &str, mut messages: &[ChatMessage], add_assistant: bool) -> String {
+ let mut prompt = bos.to_owned();
+ if let Some(first) = messages.first()
+ && role(first) == "system"
+ {
+ prompt.push_str("<|turn>system\n");
+ prompt.push_str(trim(&first.content));
+ prompt.push_str("<turn|>\n");
+ messages = &messages[1..];
+ }
+ let mut previous_role = None;
+ let mut turns = messages
+ .iter()
+ .filter(|message| role(message) != "tool")
+ .peekable();
+ while let Some(message) = turns.next() {
+ let current_role = role(message);
+ let rendered_role = if current_role == "assistant" {
+ "model"
+ } else {
+ current_role
+ };
+ if rendered_role != "model" || previous_role != Some("assistant") {
+ prompt.push_str("<|turn>");
+ prompt.push_str(rendered_role);
+ prompt.push('\n');
+ }
+ if rendered_role == "model" {
+ // Gemma removes every inline thinking channel, including an unclosed
+ // final one. Consecutive assistant messages share a single turn.
+ let content: String = message
+ .content
+ .split("<channel|>")
+ .map(|part| part.split_once("<|channel>").map_or(part, |(text, _)| text))
+ .collect();
+ prompt.push_str(trim(&content));
+ } else {
+ prompt.push_str(trim(&message.content));
+ }
+ if rendered_role != "model" || turns.peek().is_none_or(|next| role(next) != "assistant") {
+ prompt.push_str("<turn|>\n");
+ }
+ previous_role = Some(current_role);
+ }
+ if add_assistant {
+ prompt.push_str("<|turn>model\n");
+ }
+ prompt
+}
+
+fn render_qwen(messages: &[ChatMessage], add_assistant: bool) -> Result<String, Error> {
+ if messages.is_empty() {
+ return Err(template_error("No messages provided."));
+ }
+ let last_query = messages
+ .iter()
+ .rposition(|message| {
+ let content = trim(&message.content);
+ role(message) == "user"
+ && !(content.starts_with("<tool_response>")
+ && content.ends_with("</tool_response>"))
+ })
+ .ok_or_else(|| template_error("No user query found in messages."))?;
+ let mut prompt = String::new();
+ for (index, message) in messages.iter().enumerate() {
+ let content = trim(&message.content);
+ match role(message) {
+ "system" if index == 0 => chatml_turn(&mut prompt, "system", content),
+ "system" => return Err(template_error("System message must be at the beginning.")),
+ "user" => chatml_turn(&mut prompt, "user", content),
+ "assistant" => {
+ let (thinking, answer) = if let Some((before, _)) = content.split_once("</think>") {
+ let before = before.trim_end_matches('\n');
+ let thinking = before
+ .rsplit_once("<think>")
+ .map_or(before, |(_, text)| text)
+ .trim_start_matches('\n');
+ let answer = content
+ .rsplit_once("</think>")
+ .map_or(content, |(_, text)| text)
+ .trim_start_matches('\n');
+ (trim(thinking), answer)
} else {
- &msg.role
+ ("", content)
};
- expected.push_str(&format!("{start}{role}\n{}{end}\n", msg.content));
+ prompt.push_str("<|im_start|>assistant\n");
+ if index > last_query {
+ prompt.push_str("<think>\n");
+ prompt.push_str(thinking);
+ prompt.push_str("\n</think>\n\n");
+ }
+ prompt.push_str(answer);
+ prompt.push_str("<|im_end|>\n");
}
- for add_assistant in [false, true] {
- let prompt = render(
- fixture.template,
- fixture.bos,
- fixture.eos,
- &messages,
- add_assistant,
- )
- .unwrap();
- let expected = if add_assistant {
- format!("{expected}{start}{assistant}\n")
- } else {
- expected.clone()
- };
- assert_eq!(
- prompt, expected,
- "{}: add_assistant={add_assistant}",
- fixture.model_id
- );
+ "tool" => {
+ if index > 0 && role(&messages[index - 1]) != "tool" {
+ prompt.push_str("<|im_start|>user");
+ }
+ prompt.push_str("\n<tool_response>\n");
+ prompt.push_str(content);
+ prompt.push_str("\n</tool_response>");
+ if messages
+ .get(index + 1)
+ .is_none_or(|next| role(next) != "tool")
+ {
+ prompt.push_str("<|im_end|>\n");
+ }
}
+ _ => return Err(template_error("Unexpected message role.")),
+ }
+ }
+ if add_assistant {
+ prompt.push_str("<|im_start|>assistant\n<think>\n\n</think>\n\n");
+ }
+ Ok(prompt)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn messages(turns: &[(&str, &str)]) -> Vec<ChatMessage> {
+ turns
+ .iter()
+ .map(|(role, content)| ChatMessage {
+ role: (*role).to_owned(),
+ content: (*content).to_owned(),
+ })
+ .collect()
+ }
+
+ // Selected compatibility expectations recorded from the original native renderer.
+ #[test]
+ fn message_boundaries_and_assistant_prefixes() {
+ let messages = messages(&[
+ ("system", "Be concise."),
+ ("user", "नमस्ते 🐈\n<__media__>"),
+ ("assistant", "A cat."),
+ ("user", "What colour?"),
+ ]);
+ let chatml = concat!(
+ "<|im_start|>system\nBe concise.<|im_end|>\n",
+ "<|im_start|>user\nनमस्ते 🐈\n<__media__><|im_end|>\n",
+ "<|im_start|>assistant\nA cat.<|im_end|>\n",
+ "<|im_start|>user\nWhat colour?<|im_end|>\n",
+ );
+ let gemma = concat!(
+ "<bos><|turn>system\nBe concise.<turn|>\n",
+ "<|turn>user\nनमस्ते 🐈\n<__media__><turn|>\n",
+ "<|turn>model\nA cat.<turn|>\n",
+ "<|turn>user\nWhat colour?<turn|>\n",
+ );
+ for add_assistant in [false, true] {
+ let suffix = if add_assistant {
+ "<|im_start|>assistant\n"
+ } else {
+ ""
+ };
+ assert_eq!(
+ render("chatml", "<bos>", "<eos>", &messages, add_assistant).unwrap(),
+ format!("{chatml}{suffix}")
+ );
+ assert_eq!(
+ render_lfm("<|startoftext|>", &messages, add_assistant),
+ format!("<|startoftext|>{chatml}{suffix}")
+ );
+ let suffix = if add_assistant { "<|turn>model\n" } else { "" };
+ assert_eq!(
+ render_gemma("<bos>", &messages, add_assistant),
+ format!("{gemma}{suffix}")
+ );
+ let suffix = if add_assistant {
+ "<|im_start|>assistant\n<think>\n\n</think>\n\n"
+ } else {
+ ""
+ };
+ assert_eq!(
+ render_qwen(&messages, add_assistant).unwrap(),
+ format!("{chatml}{suffix}")
+ );
}
}
#[test]
- fn overrides_receive_a_boolean_false_and_special_tokens() {
+ fn lfm_and_qwen_preserve_their_thinking_history_rules() {
+ let messages = messages(&[
+ ("user", " First \n"),
+ ("assistant", "<think>private</think>\n Answer "),
+ ("user", "Next"),
+ ("assistant", "<think>recent</think>Visible"),
+ ("user", "Last"),
+ ]);
assert_eq!(
- render("llama2", "", "", &[message("user", "Hi")], true).unwrap(),
- "[INST] Hi [/INST]"
+ render_lfm("<|startoftext|>", &messages, false),
+ concat!(
+ "<|startoftext|><|im_start|>user\n First \n<|im_end|>\n",
+ "<|im_start|>assistant\nAnswer<|im_end|>\n",
+ "<|im_start|>user\nNext<|im_end|>\n",
+ "<|im_start|>assistant\n<think>recent</think>Visible<|im_end|>\n",
+ "<|im_start|>user\nLast<|im_end|>\n",
+ )
+ );
+ assert_eq!(
+ render_qwen(&messages, false).unwrap(),
+ concat!(
+ "<|im_start|>user\nFirst<|im_end|>\n",
+ "<|im_start|>assistant\n Answer<|im_end|>\n",
+ "<|im_start|>user\nNext<|im_end|>\n",
+ "<|im_start|>assistant\nVisible<|im_end|>\n",
+ "<|im_start|>user\nLast<|im_end|>\n",
+ )
);
- let template = "{{ bos_token }}{% if enable_thinking is not defined or enable_thinking %}THINKING{% else %}DIRECT{% endif %}:{{ messages[0].content }}{{ eos_token }}{% if add_generation_prompt %}ASSISTANT{% endif %}";
- let prompt = render(
- template,
- "<bos>",
- "<eos>",
- &[message("user", "你好\n<__media__>")],
- true,
- )
- .unwrap();
- assert_eq!(prompt, "<bos>DIRECT:你好\n<__media__><eos>ASSISTANT");
}
#[test]
- fn template_errors_are_returned_and_subsequent_renders_recover() {
- assert!(render("{% if %}", "", "", &[message("user", "Hi")], true).is_err());
+ fn qwen_preserves_thinking_after_the_last_query() {
+ let messages = messages(&[
+ ("user", "Hi"),
+ ("assistant", "<think> private </think>\n\n Answer "),
+ ]);
+ assert_eq!(
+ render_qwen(&messages, false).unwrap(),
+ concat!(
+ "<|im_start|>user\nHi<|im_end|>\n",
+ "<|im_start|>assistant\n<think>\nprivate\n</think>\n\n Answer<|im_end|>\n",
+ )
+ );
+ }
+
+ #[test]
+ fn gemma_removes_thinking_channels_and_merges_assistant_turns() {
+ let channels = messages(&[
+ ("user", "Hi"),
+ (
+ "assistant",
+ "before<|channel>thought\nhidden<channel|>after<|channel>thought\nsecret<channel|>end",
+ ),
+ ("user", "Next"),
+ ("assistant", "<|channel>thought\nhidden"),
+ ]);
+ assert_eq!(
+ render_gemma("<bos>", &channels, false),
+ concat!(
+ "<bos><|turn>user\nHi<turn|>\n",
+ "<|turn>model\nbeforeafterend<turn|>\n",
+ "<|turn>user\nNext<turn|>\n",
+ "<|turn>model\n<turn|>\n",
+ )
+ );
+ let consecutive = messages(&[
+ ("user", "Hi"),
+ ("assistant", " First "),
+ ("assistant", " Second "),
+ ("assistant", " Third "),
+ ]);
+ assert_eq!(
+ render_gemma("<bos>", &consecutive, false),
+ "<bos><|turn>user\nHi<turn|>\n<|turn>model\nFirstSecondThird<turn|>\n"
+ );
+ }
+
+ #[test]
+ fn qwen_rejects_invalid_histories() {
+ for turns in [
+ vec![],
+ vec![("system", "Be concise.")],
+ vec![("user", "Hello"), ("system", "Late")],
+ vec![("user", "Hello"), ("unknown", "Invalid")],
+ ] {
+ assert!(render_qwen(&messages(&turns), true).is_err(), "{turns:?}");
+ }
+ }
+
+ #[test]
+ fn unknown_templates_fail_explicitly() {
+ for source in [
+ "{{ messages[0].content }}",
+ "qwen-new",
+ "{% if %}",
+ "chatml\n",
+ ] {
+ assert!(matches!(
+ Template::identify(source),
+ Err(Error::Unsupported(_))
+ ));
+ }
assert!(render("bad\0template", "", "", &[], true).is_err());
- assert!(render("llama2", "", "", &[message("user", "Hi\0there")], true).is_err());
- let messages = [message("user", "Hello")];
- for template in ["", "chatml"] {
+ assert!(render("chatml", "bad\0bos", "", &[], true).is_err());
+ assert!(render("chatml", "", "bad\0eos", &[], true).is_err());
+ for source in ["", "chatml"] {
assert_eq!(
- render(template, "", "", &messages, true).unwrap(),
- "<|im_start|>user\nHello<|im_end|>\n<|im_start|>assistant\n"
+ render(source, "", "", &[], true).unwrap(),
+ "<|im_start|>assistant\n"
);
}
}
- const DEFAULT_TEMPLATES: &[Fixture] = &[
- Fixture {
- model_id: "lfm-vl-1.6b",
- template: concat!(
- "{{- bos_token -}}\n{%- set keep_past_thinking = keep_past_thinking | default(false) -%}\n{%- set ns = namespace(system_prompt=\"\") -%}\n{%- if messages[0][\"role\"] == \"system\" -%}\n {%- set sys_content = ",
- "messages[0][\"content\"] -%}\n {%- if sys_content is not string -%}\n {%- for item in sys_content -%}\n {%- if item[\"type\"] == \"text\" -%}\n {%- set ns.system_prompt = ns.system_prompt + ",
- "item[\"text\"] -%}\n {%- endif -%}\n {%- endfor -%}\n {%- else -%}\n {%- set ns.system_prompt = sys_content -%}\n {%- endif -%}\n {%- set messages = messages[1:] -%}\n{%- endif -%}\n{%- if tools ",
- "-%}\n {%- set ns.system_prompt = ns.system_prompt + (\"\\n\" if ns.system_prompt else \"\") + \"List of tools: [\" -%}\n {%- for tool in tools -%}\n {%- if tool is not string -%}\n {%- set tool = tool | ",
- "tojson -%}\n {%- endif -%}\n {%- set ns.system_prompt = ns.system_prompt + tool -%}\n {%- if not loop.last -%}\n {%- set ns.system_prompt = ns.system_prompt + \", \" -%}\n {%- endif -%}\n ",
- " {%- endfor -%}\n {%- set ns.system_prompt = ns.system_prompt + \"]\" -%}\n{%- endif -%}\n{%- if ns.system_prompt -%}\n {{- \"<|im_start|>system\\n\" + ns.system_prompt + \"<|im_end|>\\n\" -}}\n{%- endif -%}\n{%- set ",
- "ns.last_assistant_index = -1 -%}\n{%- for message in messages -%}\n {%- if message[\"role\"] == \"assistant\" -%}\n {%- set ns.last_assistant_index = loop.index0 -%}\n {%- endif -%}\n{%- endfor -%}\n{%- for message ",
- "in messages -%}\n {{- \"<|im_start|>\" + message[\"role\"] + \"\\n\" -}}\n {%- set content = message[\"content\"] -%}\n {%- if content is not string -%}\n {%- set ns.content = \"\" -%}\n {%- for item in content ",
- "-%}\n {%- if item[\"type\"] == \"image\" -%}\n {%- set ns.content = ns.content + \"<image>\" -%}\n {%- elif item[\"type\"] == \"text\" -%}\n {%- set ns.content = ns.content + ",
- "item[\"text\"] -%}\n {%- else -%}\n {%- set ns.content = ns.content + item | tojson -%}\n {%- endif -%}\n {%- endfor -%}\n {%- set content = ns.content -%}\n {%- endif -%}\n ",
- " {%- if message[\"role\"] == \"assistant\" and not keep_past_thinking and loop.index0 != ns.last_assistant_index -%}\n {%- if \"</think>\" in content -%}\n {%- set content = content.split(\"</think>\")[-1] | ",
- "trim -%}\n {%- endif -%}\n {%- endif -%}\n {{- content + \"<|im_end|>\\n\" -}}\n{%- endfor -%}\n{%- if add_generation_prompt -%}\n {{- \"<|im_start|>assistant\\n\" -}}\n{%- endif -%}",
- ),
- bos: "<|startoftext|>",
- eos: "<|im_end|>",
- },
- Fixture {
- model_id: "gemma-4-e2b-q4km",
- template: concat!(
- "{%- macro format_parameters(properties, required, filter_keys=false) -%}\n {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}\n {%- set ns = namespace(found_first=false) -%}\n ",
- " {%- for key, value in properties | dictsort -%}\n {%- set add_comma = false -%}\n {%- if not filter_keys or key not in standard_keys -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ",
- "ns.found_first = true -%}\n {{ key }}:{\n {%- if value['description'] -%}\n description:<|\"|>{{ value['description'] }}<|\"|>\n {%- set add_comma = true -%}\n {%- ",
- "endif -%}\n {%- if value['type'] | upper == 'STRING' -%}\n {%- if value['enum'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n ",
- " enum:{{ format_argument(value['enum']) }}\n {%- endif -%}\n {%- elif value['type'] | upper == 'ARRAY' -%}\n {%- if value['items'] is mapping and value['items'] -%}\n ",
- " {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n items:{\n {%- set ns_items = namespace(found_first=false) -%}\n {%- for item_key, ",
- "item_value in value['items'] | dictsort -%}\n {%- if item_value is not none -%}\n {%- if ns_items.found_first %},{% endif -%}\n {%- set ",
- "ns_items.found_first = true -%}\n {%- if item_key == 'properties' -%}\n properties:{\n {%- if item_value is mapping -%}\n ",
- " {{- format_parameters(item_value, value['items']['required'] | default([])) -}}\n {%- endif -%}\n }\n {%- elif ",
- "item_key == 'required' -%}\n required:[\n {%- for req_item in item_value -%}\n <|\"|>{{- req_item -}}<|\"|>\n ",
- " {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n {%- elif item_key == 'type' -%}\n ",
- " {%- if item_value is string -%}\n type:{{ format_argument(item_value | upper) }}\n {%- else -%}\n type:{{ ",
- "format_argument(item_value | map('upper') | list) }}\n {%- endif -%}\n {%- else -%}\n {{ item_key }}:{{ format_argument(item_value) ",
- "}}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n }\n {%- endif -%}\n {%- endif -%}\n {%- if ",
- "value['nullable'] %}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n nullable:true\n {%- endif -%}\n {%- if value['type'] | upper == ",
- "'OBJECT' -%}\n {%- if value['properties'] is defined and value['properties'] is mapping -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n ",
- " properties:{\n {{- format_parameters(value['properties'], value['required'] | default([])) -}}\n }\n {%- elif value is mapping -%}\n {%- if add_comma ",
- "%},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n properties:{\n {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}\n }\n ",
- " {%- endif -%}\n {%- if value['required'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n required:[\n ",
- "{%- for item in value['required'] | default([]) -%}\n <|\"|>{{- item -}}<|\"|>\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n ",
- " {%- endif -%}\n {%- endif -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n type:<|\"|>{{ value['type'] | upper }}<|\"|>}\n {%- endif -%}\n ",
- " {%- endfor -%}\n{%- endmacro -%}\n{%- macro format_function_declaration(tool_data) -%}\n declaration:{{- tool_data['function']['name'] -}}{description:<|\"|>{{- tool_data['function']['description'] -}}<|\"|>\n {%- set ",
- "params = tool_data['function']['parameters'] -%}\n {%- if params -%}\n ,parameters:{\n {%- if params['properties'] -%}\n properties:{ {{- format_parameters(params['properties'], ",
- "params['required']) -}} },\n {%- endif -%}\n {%- if params['required'] -%}\n required:[\n {%- for item in params['required'] -%}\n <|\"|>{{- item -}}<|\"|>\n {{- ",
- "',' if not loop.last -}}\n {%- endfor -%}\n ],\n {%- endif -%}\n {%- if params['type'] -%}\n type:<|\"|>{{- params['type'] | upper -}}<|\"|>}\n {%- endif -%}\n {%- endif ",
- "-%}\n {%- if 'response' in tool_data['function'] -%}\n {%- set response_declaration = tool_data['function']['response'] -%}\n ,response:{\n {%- if response_declaration['description'] -%}\n ",
- "description:<|\"|>{{- response_declaration['description'] -}}<|\"|>,\n {%- endif -%}\n {%- if response_declaration['type'] | upper == 'OBJECT' -%}\n type:<|\"|>{{- response_declaration['type'] | upper ",
- "-}}<|\"|>}\n {%- endif -%}\n {%- endif -%}\n }\n{%- endmacro -%}\n{%- macro format_argument(argument, escape_keys=True) -%}\n {%- if argument is none -%}\n {{- 'null' -}}\n {%- elif argument is string ",
- "-%}\n {{- '<|\"|>' + argument + '<|\"|>' -}}\n {%- elif argument is boolean -%}\n {{- 'true' if argument else 'false' -}}\n {%- elif argument is mapping -%}\n {{- '{' -}}\n {%- set ns = ",
- "namespace(found_first=false) -%}\n {%- for key, value in argument | dictsort -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {%- if escape_keys -%}\n ",
- " {{- '<|\"|>' + key + '<|\"|>' -}}\n {%- else -%}\n {{- key -}}\n {%- endif -%}\n :{{- format_argument(value, escape_keys=escape_keys) -}}\n {%- endfor -%}\n ",
- " {{- '}' -}}\n {%- elif argument is sequence -%}\n {{- '[' -}}\n {%- for item in argument -%}\n {{- format_argument(item, escape_keys=escape_keys) -}}\n {%- if not loop.last %},{% ",
- "endif -%}\n {%- endfor -%}\n {{- ']' -}}\n {%- else -%}\n {{- argument -}}\n {%- endif -%}\n{%- endmacro -%}\n{%- macro strip_thinking(text) -%}\n {%- set ns = namespace(result='') -%}\n {%- for ",
- "part in text.split('<channel|>') -%}\n {%- if '<|channel>' in part -%}\n {%- set ns.result = ns.result + part.split('<|channel>')[0] -%}\n {%- else -%}\n {%- set ns.result = ns.result + ",
- "part -%}\n {%- endif -%}\n {%- endfor -%}\n {{- ns.result | trim -}}\n{%- endmacro -%}\n\n{%- macro format_tool_response_block(tool_name, response) -%}\n {{- '<|tool_response>' -}}\n {%- if response is ",
- "mapping -%}\n {{- 'response:' + tool_name + '{' -}}\n {%- for key, value in response | dictsort -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- if not loop.last ",
- "%},{% endif -%}\n {%- endfor -%}\n {{- '}' -}}\n {%- else -%}\n {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}\n {%- endif -%}\n {{- ",
- "'<tool_response|>' -}}\n{%- endmacro -%}\n\n{#- ===== SETUP ===== -#}\n{%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None) -%}\n{%- set loop_messages = messages -%}\n{%- set enable_thinking = ",
- "enable_thinking | default(false) -%}\n{%- set preserve_thinking = preserve_thinking | default(false) -%}\n{{- bos_token -}}\n{#- Handle System/Tool Definitions Block -#}\n{%- if enable_thinking or tools or (messages and ",
- "messages[0]['role'] in ['system', 'developer']) -%}\n {{- '<|turn>system\\n' -}}\n {#- Inject Thinking token at the very top of the FIRST system turn -#}\n {%- if enable_thinking -%}\n {{- '<|think|>\\n' -}}\n ",
- " {%- set ns.prev_message_type = 'think' -%}\n {%- endif -%}\n {%- if messages and messages[0]['role'] in ['system', 'developer'] -%}\n {%- if messages[0]['content'] is string -%}\n {{- ",
- "messages[0]['content'] | trim -}}\n {%- elif messages[0]['content'] is sequence -%}\n {%- for item in messages[0]['content'] -%}\n {{- item['text'] | trim + ' '-}}\n {%- endfor ",
- "-%}\n {%- endif -%}\n {%- set loop_messages = messages[1:] -%}\n {%- endif -%}\n {%- if tools -%}\n {%- for tool in tools %}\n {{- '<|tool>' -}}\n {{- ",
- "format_function_declaration(tool) | trim -}}\n {{- '<tool|>' -}}\n {%- endfor %}\n {%- set ns.prev_message_type = 'tool' -%}\n {%- endif -%}\n {{- '<turn|>\\n' -}}\n{%- endif %}\n\n{#- Pre-scan: ",
- "find last user message index for reasoning guard -#}\n{%- set ns_turn = namespace(last_user_idx=-1) -%}\n{%- for i in range(loop_messages | length) -%}\n {%- if loop_messages[i]['role'] == 'user' -%}\n {%- set ",
- "ns_turn.last_user_idx = i -%}\n {%- endif -%}\n{%- endfor -%}\n\n{#- Loop through messages -#}\n{%- for message in loop_messages -%}\n {%- if message['role'] != 'tool' -%}\n {%- set ns.prev_message_type = None -%}\n ",
- "{%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}\n {#- Detect continuation using tracked state — O(1) instead of O(n) backward scan -#}\n {%- set continue_same_model_turn = (role == ",
- "'model' and ns.prev_non_tool_role == 'assistant') -%}\n {%- if not continue_same_model_turn -%}\n {{- '<|turn>' + role + '\\n' }}\n {%- endif -%}\n\n {#- Render reasoning/reasoning_content as thinking channel ",
- "-#}\n {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}\n {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or (preserve_thinking and message.get('tool_calls')) -%}\n",
- " {%- if thinking_text and thinking_gate -%}\n {{- '<|channel>thought\\n' + thinking_text + '\\n<channel|>' -}}\n {%- endif -%}\n\n {%- if message.get('tool_calls') -%}\n {%- for tool_call ",
- "in message.get('tool_calls') -%}\n {%- set function = tool_call['function'] -%}\n {{- '<|tool_call>call:' + function['name'] + '{' -}}\n {%- if function['arguments'] ",
- "is mapping -%}\n {%- set ns_args = namespace(found_first=false) -%}\n {%- for key, value in function['arguments'] | dictsort -%}\n {%- if ",
- "ns_args.found_first %},{% endif -%}\n {%- set ns_args.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n ",
- "{%- endfor -%}\n {%- elif function['arguments'] is none -%}\n {%- elif function['arguments'] is string -%}\n {#- Pre-serialized args (e.g. an OpenAI JSON ",
- "string). We cannot JSON-parse\n portably in-template, so render non-fatally instead of erroring. Strip an\n outer {...} so it composes with the DSL braces rather than ",
- "double-wrapping.\n Prefer passing arguments as a mapping for exact Gemma DSL. -#}\n {%- set argstr = function['arguments'] | trim -%}\n {%- if ",
- "argstr[:1] == '{' and argstr[-1:] == '}' -%}\n {{- argstr[1:-1] -}}\n {%- else -%}\n {{- function['arguments'] -}}\n {%- ",
- "endif -%}\n {%- endif -%}\n {{- '}<tool_call|>' -}}\n {%- endfor -%}\n {%- set ns.prev_message_type = 'tool_call' -%}\n {%- endif -%}\n\n ",
- " {%- set ns_tr_out = namespace(flag=false) -%}\n {%- if message.get('tool_responses') -%}\n {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}\n ",
- "{%- for tool_response in message.get('tool_responses') -%}\n {{- format_tool_response_block(tool_response['name'] | default('unknown', true), tool_response['response']) -}}\n {%- set ",
- "ns_tr_out.flag = true -%}\n {%- set ns.prev_message_type = 'tool_response' -%}\n {%- endfor -%}\n {%- elif message.get('tool_calls') -%}\n {#- OpenAI Chat ",
- "Completions: forward-scan consecutive role:tool messages -#}\n {%- set ns_tool_scan = namespace(stopped=false) -%}\n {%- for k in range(loop.index0 + 1, loop_messages | length) -%}\n ",
- " {%- if ns_tool_scan.stopped -%}\n {%- elif loop_messages[k]['role'] != 'tool' -%}\n {%- set ns_tool_scan.stopped = true -%}\n {%- else -%}\n ",
- " {%- set follow = loop_messages[k] -%}\n {#- Resolve tool_call_id to function name -#}\n {%- set ns_tname = namespace(name=follow.get('name') or 'unknown') -%}\n ",
- " {%- for tc in message.get('tool_calls') -%}\n {%- if tc.get('id') == follow.get('tool_call_id') -%}\n {%- set ns_tname.name = ",
- "tc['function']['name'] -%}\n {%- endif -%}\n {%- endfor -%}\n {#- Handle content as string or content-parts array -#}\n {%- set ",
- "tool_body = follow.get('content') -%}\n {%- if tool_body is string -%}\n {{- format_tool_response_block(ns_tname.name, tool_body) -}}\n {%- elif ",
- "tool_body is sequence and tool_body is not string -%}\n {%- set ns_txt = namespace(s='') -%}\n {%- for part in tool_body -%}\n {%- if ",
- "part.get('type') == 'text' -%}\n {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}\n {%- endif -%}\n {%- endfor ",
- "-%}\n {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}\n {%- for part in tool_body -%}\n {%- if part.get('type') in ['image', ",
- "'image_url'] -%}\n {{- '<|image|>' -}}\n {%- elif part.get('type') in ['audio', 'input_audio'] -%}\n {{- '<|audio|>' -}}\n ",
- " {%- elif part.get('type') == 'video' -%}\n {{- '<|video|>' -}}\n {%- endif -%}\n {%- endfor -%}\n ",
- " {%- else -%}\n {{- format_tool_response_block(ns_tname.name, tool_body) -}}\n {%- endif -%}\n {%- set ns_tr_out.flag = true -%}\n ",
- " {%- set ns.prev_message_type = 'tool_response' -%}\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n\n {%- set captured_content -%}\n ",
- "{%- if message.get('content') is string -%}\n {%- if role == 'model' -%}\n {{- strip_thinking(message['content']) -}}\n {%- else -%}\n {{- ",
- "message['content'] | trim -}}\n {%- endif -%}\n {%- elif message.get('content') is sequence -%}\n {%- for item in message['content'] -%}\n {%- if item.get('type') ",
- "== 'text' -%}\n {%- if role == 'model' -%}\n {{- strip_thinking(item['text']) -}}\n {%- else -%}\n {{- item['text'] | trim ",
- "-}}\n {%- endif -%}\n {%- elif item.get('type') in ['image', 'image_url'] -%}\n {{- '<|image|>' -}}\n {%- elif item.get('type') in ",
- "['audio', 'input_audio'] -%}\n {{- '<|audio|>' -}}\n {%- elif item.get('type') == 'video' -%}\n {{- '<|video|>' -}}\n {%- endif -%}\n ",
- " {%- endfor -%}\n {%- endif -%}\n {%- endset -%}\n\n {{- captured_content -}}\n {%- set has_content = captured_content | trim | length > 0 -%}\n\n {#- Forward-scan: ",
- "find next non-tool message role for continuation detection -#}\n {%- set next_nt = namespace(role=None, found=false) -%}\n {%- for j in range(loop.index0 + 1, loop_messages | length) -%}\n {%- if ",
- "not next_nt.found -%}\n {%- if loop_messages[j]['role'] != 'tool' -%}\n {%- set next_nt.role = loop_messages[j]['role'] -%}\n {%- set next_nt.found = true -%}\n ",
- " {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n\n {%- set continues_into_next = (\n role == 'model'\n and next_nt.role == 'assistant'\n and (not ",
- "message.get('tool_calls') or ns_tr_out.flag)\n ) -%}\n\n {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}\n {{- '<|tool_response>' -}}\n {%- elif continues_into_next -%}\n ",
- " {%- elif not (ns_tr_out.flag and not has_content and not next_nt.found) -%}\n {{- '<turn|>\\n' -}}\n {%- endif -%}\n\n {#- Track previous non-tool role for next iteration (avoids O(n) backward ",
- "scan) -#}\n {%- set ns.prev_non_tool_role = message['role'] -%}\n {%- endif -%}\n{%- endfor -%}\n\n{%- if add_generation_prompt -%}\n {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != ",
- "'tool_call' -%}\n {{- '<|turn>model\\n' -}}\n {%- elif ns.prev_message_type == 'tool_response' and enable_thinking -%}\n {{- '<|channel>thought\\n' -}}\n {%- endif -%}\n{%- endif -%}\n",
- ),
- bos: "<bos>",
- eos: "<turn|>",
- },
- ];
+ #[test]
+ #[ignore = "requires ENSU_TEST_MODEL"]
+ fn named_templates_still_use_the_safe_llama_api() {
+ let model = LlamaModel::load_from_file(
+ crate::llm::backend().unwrap(),
+ std::env::var("ENSU_TEST_MODEL").unwrap(),
+ &llama_cpp_2::model::params::LlamaModelParams::default().with_n_gpu_layers(0),
+ )
+ .unwrap();
+ let messages = |content: &str| {
+ vec![ChatMessage {
+ role: "user".to_owned(),
+ content: content.to_owned(),
+ }]
+ };
+ assert_eq!(
+ build_chat_prompt(&model, messages("Hi"), Some("llama2".to_owned()), true).unwrap(),
+ "[INST] Hi [/INST]"
+ );
+ assert!(
+ build_chat_prompt(
+ &model,
+ messages("Hi\0there"),
+ Some("llama2".to_owned()),
+ true
+ )
+ .is_err()
+ );
+ assert!(
+ build_chat_prompt(
+ &model,
+ messages("Hi"),
+ Some("new-template".to_owned()),
+ true
+ )
+ .is_err()
+ );
+ }
}
diff --git a/rust/crates/ensu/tests/llm_worker.rs b/rust/crates/ensu/tests/llm_worker.rs
index a1d3b0727a..24a86107f5 100644
--- a/rust/crates/ensu/tests/llm_worker.rs
+++ b/rust/crates/ensu/tests/llm_worker.rs
@@ -31,6 +31,7 @@ fn context() -> llm::ContextRef {
ContextParams {
context_size: Some(512),
n_threads: Some(4),
+ n_threads_generation: None,
n_batch: Some(256),
},
)
@@ -130,6 +131,7 @@ fn concurrent_generations_preserve_output_and_can_drop_caller_model() {
ContextParams {
context_size: Some(512),
n_threads: Some(4),
+ n_threads_generation: None,
n_batch: Some(256),
},
)
@@ -238,6 +240,7 @@ fn default_model_reuses_multi_turn_cache_and_reloads() {
let params = ContextParams {
context_size: Some(512),
n_threads: Some(4),
+ n_threads_generation: None,
n_batch: Some(256),
};
let warm = Context::new(&owner, params.clone()).unwrap();
@@ -271,6 +274,7 @@ fn default_model_handles_wide_and_tall_images_and_projector_reloads() {
ContextParams {
context_size: Some(4096),
n_threads: Some(4),
+ n_threads_generation: None,
n_batch: Some(256),
},
)