Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

feat(llamacpp): upgrade the engine to 0.4.0 (b10809)

Akarshan Biswasfbfff0d7b3cf
diff --git a/extensions/llamacpp-extension/settings.json b/extensions/llamacpp-extension/settings.json
index 8d0762455..4b7c07f3f 100644
--- a/extensions/llamacpp-extension/settings.json
+++ b/extensions/llamacpp-extension/settings.json
@@ -149,6 +149,18 @@
"textAlign": "right"
}
},
+ {
+ "key": "n_cpu_ffn",
+ "title": "Dense FFN Layers on CPU",
+ "description": "Keep the dense feed-forward weights of the first N layers in CPU memory. The counterpart of the MoE setting above, for dense models. 0 disables it.",
+ "controllerType": "input",
+ "controllerProps": {
+ "value": 0,
+ "placeholder": "0",
+ "type": "number",
+ "textAlign": "right"
+ }
+ },
{
"key": "no_kv_offload",
"title": "Disable KV Cache Offload",
@@ -282,6 +294,29 @@
"value": false
}
},
+ {
+ "key": "lazy_mode",
+ "title": "Lazy Tensor Reading",
+ "description": "Read the rows of large arch-marked tensors (e.g. per-layer embeddings) from disk on demand instead of keeping them in memory. Auto applies it above 4 GiB. Requires mmap.",
+ "controllerType": "dropdown",
+ "controllerProps": {
+ "value": "auto",
+ "options": [
+ {
+ "value": "auto",
+ "name": "Auto"
+ },
+ {
+ "value": "on",
+ "name": "On"
+ },
+ {
+ "value": "off",
+ "name": "Off"
+ }
+ ]
+ }
+ },
{
"key": "mlock",
"title": "MLock",
@@ -540,6 +575,28 @@
]
}
},
+ {
+ "key": "kv_unified_per_slot",
+ "title": "Context Per Slot",
+ "description": "Cap how much of the unified KV cache one slot may use, in tokens. 0 leaves a slot free to fill the whole cache. Only applies when the cache is unified.",
+ "controllerType": "input",
+ "controllerProps": {
+ "value": 0,
+ "placeholder": "0",
+ "type": "number",
+ "textAlign": "right",
+ "min": 0
+ }
+ },
+ {
+ "key": "reasoning_preserve",
+ "title": "Preserve Reasoning History",
+ "description": "Keep the reasoning of every earlier assistant turn in the prompt, not just the most recent one. Only affects models whose chat template supports it, and costs prompt tokens.",
+ "controllerType": "checkbox",
+ "controllerProps": {
+ "value": true
+ }
+ },
{
"key": "keep",
"title": "Keep First N Tokens",
diff --git a/extensions/llamacpp-extension/src/index.ts b/extensions/llamacpp-extension/src/index.ts
index e2b6312bb..fbc7b64e2 100644
--- a/extensions/llamacpp-extension/src/index.ts
+++ b/extensions/llamacpp-extension/src/index.ts
@@ -120,6 +120,7 @@ const PRESET_AFFECTING_KEYS = new Set<string>([
'batch_size',
'ubatch_size',
'n_cpu_moe',
+ 'n_cpu_ffn',
'no_kv_offload',
'device',
'split_mode',
@@ -144,6 +145,9 @@ const PRESET_AFFECTING_KEYS = new Set<string>([
'no_op_offload',
'ctx_checkpoints',
'checkpoint_min_step',
+ 'kv_unified_per_slot',
+ 'reasoning_preserve',
+ 'lazy_mode',
])
@@ -311,6 +315,14 @@ const MODEL_SETTINGS_YAML_MAPPING: Record<
return Number.isFinite(n) && n > 0 ? Math.floor(n) : null
},
},
+ n_cpu_ffn: {
+ yamlKey: 'n_cpu_ffn',
+ coerce: (v) => {
+ if (v === '' || v == null) return null
+ const n = typeof v === 'number' ? v : Number(v)
+ return Number.isFinite(n) && n > 0 ? Math.floor(n) : null
+ },
+ },
no_kv_offload: {
yamlKey: 'no_kv_offload',
coerce: (v) => (v === true ? true : null),
diff --git a/extensions/llamacpp-extension/src/preset.test.ts b/extensions/llamacpp-extension/src/preset.test.ts
index 3178b3d23..0864de047 100644
--- a/extensions/llamacpp-extension/src/preset.test.ts
+++ b/extensions/llamacpp-extension/src/preset.test.ts
@@ -216,6 +216,65 @@ describe('generatePreset parallel', () => {
})
})
+describe('generatePreset 0.4.0 keys', () => {
+ it('emits n-cpu-ffn when set, and omits it at 0', async () => {
+ setupModel('llama', {})
+ await generatePreset('/p', '/jan', { n_cpu_ffn: 12 } as any)
+ expect(writtenFiles['/p/router.preset.ini']).toContain('n-cpu-ffn = 12')
+
+ await generatePreset('/p', '/jan', { n_cpu_ffn: 0 } as any)
+ expect(writtenFiles['/p/router.preset.ini']).not.toContain('n-cpu-ffn')
+ })
+
+ it('emits a per-model n-cpu-ffn override', async () => {
+ setupModel('llama', { n_cpu_ffn: 4 })
+ await generatePreset('/p', '/jan', {} as any)
+ expect(writtenFiles['/p/router.preset.ini']).toContain('n-cpu-ffn = 4')
+ })
+
+ // auto is upstream's own default, so naming it would pin a value that is
+ // meant to track the engine.
+ it('emits lazy-mode only when it is not auto', async () => {
+ setupModel('llama', {})
+ await generatePreset('/p', '/jan', { lazy_mode: 'auto' } as any)
+ expect(writtenFiles['/p/router.preset.ini']).not.toContain('lazy-mode')
+
+ await generatePreset('/p', '/jan', { lazy_mode: 'off' } as any)
+ expect(writtenFiles['/p/router.preset.ini']).toContain('lazy-mode = off')
+
+ await generatePreset('/p', '/jan', { lazy_mode: 'on' } as any)
+ expect(writtenFiles['/p/router.preset.ini']).toContain('lazy-mode = on')
+ })
+
+ it('emits kv-unified-per-slot when set, and omits it at 0', async () => {
+ setupModel('llama', {})
+ await generatePreset('/p', '/jan', { kv_unified_per_slot: 4096 } as any)
+ expect(writtenFiles['/p/router.preset.ini']).toContain(
+ 'kv-unified-per-slot = 4096'
+ )
+
+ await generatePreset('/p', '/jan', { kv_unified_per_slot: 0 } as any)
+ expect(writtenFiles['/p/router.preset.ini']).not.toContain(
+ 'kv-unified-per-slot'
+ )
+ })
+
+ // Enabled is the engine's default since 0.4.0, so only the off case is
+ // written; emitting `true` would be a no-op line that outlives the default.
+ it('emits reasoning-preserve only to turn it off', async () => {
+ setupModel('llama', {})
+ await generatePreset('/p', '/jan', { reasoning_preserve: true } as any)
+ expect(writtenFiles['/p/router.preset.ini']).not.toContain(
+ 'reasoning-preserve'
+ )
+
+ await generatePreset('/p', '/jan', { reasoning_preserve: false } as any)
+ expect(writtenFiles['/p/router.preset.ini']).toContain(
+ 'reasoning-preserve = false'
+ )
+ })
+})
+
describe('generatePreset kv-unified', () => {
it('enables unified KV on auto when an explicit parallel is emitted', async () => {
setupModel('llama', {})
diff --git a/extensions/llamacpp-extension/src/preset.ts b/extensions/llamacpp-extension/src/preset.ts
index 7eecbb2a2..fbb5079db 100644
--- a/extensions/llamacpp-extension/src/preset.ts
+++ b/extensions/llamacpp-extension/src/preset.ts
@@ -49,6 +49,7 @@ type ModelYaml = ModelConfig & {
spec_draft_p_min?: number
cpu_moe?: boolean
n_cpu_moe?: number
+ n_cpu_ffn?: number
no_kv_offload?: boolean
override_tensor?: string
mmproj_offload?: boolean
@@ -325,6 +326,15 @@ export async function generatePreset(
) {
lines.push(`n-cpu-moe = ${Math.floor(config.n_cpu_moe)}`)
}
+ // n-cpu-ffn default = 0. The dense-model counterpart of n-cpu-moe, added in
+ // llama.cpp 0.4.0; the two are independent and a model can want either.
+ if (
+ typeof config.n_cpu_ffn === 'number' &&
+ Number.isFinite(config.n_cpu_ffn) &&
+ config.n_cpu_ffn > 0
+ ) {
+ lines.push(`n-cpu-ffn = ${Math.floor(config.n_cpu_ffn)}`)
+ }
// no-kv-offload default = false (the cache is offloaded). Spelled negatively
// to match llama.cpp's own flag and the existing no_mmap setting.
// common_preset's parse_bool_arg recognises `no-kv-offload` as the negated
@@ -435,6 +445,12 @@ export async function generatePreset(
) {
lines.push(`cache-ram = ${Math.floor(config.cache_ram)}`)
}
+ // lazy-mode default = auto (on for arch-marked tensors above 4 GiB). Needs
+ // mmap, so it is silently inert with `no_mmap` on; `off` is the pre-0.4.0
+ // behaviour of always keeping those tensors resident.
+ if (config.lazy_mode === 'on' || config.lazy_mode === 'off') {
+ lines.push(`lazy-mode = ${config.lazy_mode}`)
+ }
// slot-save-path has no default: naming it is what enables llama.cpp's slot
// save/restore routes at all. Emitted even with the feature off, so the worker
// knows which directory to keep clear and can still erase a deleted thread's
@@ -459,6 +475,24 @@ export async function generatePreset(
} else if (config.kv_unified === 'off') {
lines.push('kv-unified = false')
}
+ // kv-unified-per-slot default = unset. Caps one slot's share of the shared KV
+ // pool, which is what bounds a single conversation once the pool is unified
+ // and every surface shares a slot. Only sizes the pool itself when no
+ // ctx-size is pinned (server.cpp:160-170, mirrored in the shim).
+ if (
+ typeof config.kv_unified_per_slot === 'number' &&
+ Number.isFinite(config.kv_unified_per_slot) &&
+ config.kv_unified_per_slot > 0
+ ) {
+ lines.push(`kv-unified-per-slot = ${Math.floor(config.kv_unified_per_slot)}`)
+ }
+ // reasoning-preserve defaults to on since 0.4.0, for any template advertising
+ // `supports_preserve_reasoning`. Emitted only to turn it off, which restores
+ // the template's own default and keeps a per-request `chat_template_kwargs`
+ // (Jan's per-model `preserve_thinking`) the authoritative control.
+ if (config.reasoning_preserve === false) {
+ lines.push('reasoning-preserve = false')
+ }
// keep default = 0
if (
typeof config.keep === 'number' &&
@@ -603,6 +637,9 @@ export async function generatePreset(
if (typeof mc.n_cpu_moe === 'number' && mc.n_cpu_moe > 0) {
lines.push(`n-cpu-moe = ${Math.floor(mc.n_cpu_moe)}`)
}
+ if (typeof mc.n_cpu_ffn === 'number' && mc.n_cpu_ffn > 0) {
+ lines.push(`n-cpu-ffn = ${Math.floor(mc.n_cpu_ffn)}`)
+ }
if (mc.no_kv_offload === true) {
// INI key is the negated form; parse_bool_arg flips it server-side.
// Writing `no-kv-offload = true` => kv-offload disabled.
diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock
index da2530f60..706197d6e 100644
--- a/src-tauri/Cargo.lock
+++ b/src-tauri/Cargo.lock
@@ -7563,7 +7563,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-llamacpp"
-version = "0.3.0"
+version = "0.4.0"
dependencies = [
"base64 0.22.1",
"byteorder",
diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/Cargo.lock b/src-tauri/plugins/tauri-plugin-llamacpp/Cargo.lock
index 22307195d..ded74514f 100644
--- a/src-tauri/plugins/tauri-plugin-llamacpp/Cargo.lock
+++ b/src-tauri/plugins/tauri-plugin-llamacpp/Cargo.lock
@@ -3421,7 +3421,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-llamacpp"
-version = "0.3.0"
+version = "0.4.0"
dependencies = [
"base64 0.22.1",
"byteorder",
diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/Cargo.toml b/src-tauri/plugins/tauri-plugin-llamacpp/Cargo.toml
index b43dd44ed..3554d1902 100644
--- a/src-tauri/plugins/tauri-plugin-llamacpp/Cargo.toml
+++ b/src-tauri/plugins/tauri-plugin-llamacpp/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "tauri-plugin-llamacpp"
-version = "0.3.0"
+version = "0.4.0"
authors = ["Jan <[email protected]>"]
description = "Tauri plugin for managing Jan LlamaCpp server processes and model loading"
license = "MIT"
diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/build.rs b/src-tauri/plugins/tauri-plugin-llamacpp/build.rs
index 28f47870b..376c9a66e 100644
--- a/src-tauri/plugins/tauri-plugin-llamacpp/build.rs
+++ b/src-tauri/plugins/tauri-plugin-llamacpp/build.rs
@@ -13,10 +13,10 @@
// vendor/llama.cpp and with engine::PINNED_* on the Rust side; the shim
// re-exports llama_version()/llama_build_number() so a mismatch is caught at
// runtime instead of producing a silently wrong engine.
-pub const LLAMA_CPP_TAG: &str = "b10621";
-pub const LLAMA_CPP_BUILD_NUMBER: u32 = 10621;
-pub const LLAMA_CPP_COMMIT: &str = "c1d0e7a004015f23bc0233470b747b596f29b264";
-pub const LLAMA_CPP_VERSION: &str = "0.3.0";
+pub const LLAMA_CPP_TAG: &str = "b10809";
+pub const LLAMA_CPP_BUILD_NUMBER: u32 = 10809;
+pub const LLAMA_CPP_COMMIT: &str = "5266f24da75dc449bd56cbed7addb9c8e4a6a73e";
+pub const LLAMA_CPP_VERSION: &str = "0.4.0";
const COMMANDS: &[&str] = &[
// Cleanup command
diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/guest-js/types.ts b/src-tauri/plugins/tauri-plugin-llamacpp/guest-js/types.ts
index 423dd71f6..27fa5dc2b 100644
--- a/src-tauri/plugins/tauri-plugin-llamacpp/guest-js/types.ts
+++ b/src-tauri/plugins/tauri-plugin-llamacpp/guest-js/types.ts
@@ -70,6 +70,7 @@ export type LlamacppConfig = {
batch_size: number
ubatch_size: number
n_cpu_moe: number
+ n_cpu_ffn: number
no_kv_offload: boolean
device: string
split_mode: string
@@ -96,6 +97,12 @@ export type LlamacppConfig = {
checkpoint_min_step: number
swa_full: boolean
kv_unified: string
+ /** Per-slot cap on the shared KV pool, in tokens. 0 = unset. */
+ kv_unified_per_slot: number
+ /** Keep reasoning for the whole history, not just the last assistant turn. */
+ reasoning_preserve: boolean
+ /** On-demand reading of arch-marked tensors: `auto` | `on` | `off`. */
+ lazy_mode: string
keep: number
}
diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/package.json b/src-tauri/plugins/tauri-plugin-llamacpp/package.json
index 738082879..37998b83e 100644
--- a/src-tauri/plugins/tauri-plugin-llamacpp/package.json
+++ b/src-tauri/plugins/tauri-plugin-llamacpp/package.json
@@ -1,6 +1,6 @@
{
"name": "@janhq/tauri-plugin-llamacpp-api",
- "version": "0.3.0",
+ "version": "0.4.0",
"private": true,
"description": "",
"type": "module",
diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/shim/jan_llama_shim.cpp b/src-tauri/plugins/tauri-plugin-llamacpp/shim/jan_llama_shim.cpp
index 896e95f4b..240fed71b 100644
--- a/src-tauri/plugins/tauri-plugin-llamacpp/shim/jan_llama_shim.cpp
+++ b/src-tauri/plugins/tauri-plugin-llamacpp/shim/jan_llama_shim.cpp
@@ -13,6 +13,7 @@
#include "ggml-backend.h"
#include <atomic>
+#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
@@ -391,6 +392,15 @@ jan_llama_engine * jan_llama_engine_start_from_preset(const char * ini_pat
engine->params.kv_overrides.back().key[0] = 0;
}
+ // arg.cpp:963-966, at the end of common_params_parse_ex. Since 0.4.0 the
+ // kwarg defaults to on when the user did not name it, and a template
+ // with `supports_preserve_reasoning` reads it -- so omitting this here
+ // would have the preset path preserve less reasoning than the argv path
+ // and than llama-server, for the same ini.
+ if (!engine->params.default_template_kwargs.count("preserve_reasoning")) {
+ engine->params.default_template_kwargs["preserve_reasoning"] = "true";
+ }
+
return finish_start(std::move(engine), err, err_len);
} catch (const std::exception & e) {
set_err(err, err_len, e.what());
@@ -416,6 +426,16 @@ jan_llama_engine * finish_start(std::unique_ptr<jan_llama_engine> engine,
engine->params.kv_unified = true;
}
+ // server.cpp:160-170, immediately after the block above and dependent on
+ // it: the pool is n_parallel wide, so it can only be sized once the
+ // sentinel is resolved. Only `-c 0` (size to the model's trained
+ // context) leaves n_ctx at 0 for this to act on; any explicit ctx-size
+ // pins the pool and the cap then applies per slot inside it.
+ if (engine->params.kv_unified_per_slot > 0 && engine->params.n_ctx == 0 &&
+ (uint32_t) engine->params.fit_params_min_ctx != UINT32_MAX) {
+ engine->params.n_ctx = engine->params.n_parallel * engine->params.kv_unified_per_slot;
+ }
+
common_init();
// After common_init, which installs its own callback.
llama_log_set(capture_log_callback, nullptr);
diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/shim/jan_llama_shim.h b/src-tauri/plugins/tauri-plugin-llamacpp/shim/jan_llama_shim.h
index 8520be171..d40b8b811 100644
--- a/src-tauri/plugins/tauri-plugin-llamacpp/shim/jan_llama_shim.h
+++ b/src-tauri/plugins/tauri-plugin-llamacpp/shim/jan_llama_shim.h
@@ -96,8 +96,8 @@ void jan_llama_string_free(char * s);
// The llama.cpp the shim was compiled against, for the pin assertion in
// build.rs / engine::assert_pinned_version.
-const char * jan_llama_version(void); // e.g. "0.3.0"
-int jan_llama_build_number(void); // e.g. 10621
+const char * jan_llama_version(void); // e.g. "0.4.0"
+int jan_llama_build_number(void); // e.g. 10809
const char * jan_llama_commit(void);
#ifdef __cplusplus
diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/mod.rs b/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/mod.rs
index 4f2db584d..3a6cf91d1 100644
--- a/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/mod.rs
+++ b/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/mod.rs
@@ -212,9 +212,9 @@ mod tests {
#[test]
fn pin_constants_are_populated_from_build_rs() {
- assert_eq!(PINNED_TAG, "b10621");
- assert_eq!(PINNED_BUILD_NUMBER, "10621");
- assert_eq!(PINNED_VERSION, "0.3.0");
+ assert_eq!(PINNED_TAG, "b10809");
+ assert_eq!(PINNED_BUILD_NUMBER, "10809");
+ assert_eq!(PINNED_VERSION, "0.4.0");
assert_eq!(PINNED_COMMIT.len(), 40, "commit should be a full sha");
}
diff --git a/web-app/src/containers/ModelSetting.tsx b/web-app/src/containers/ModelSetting.tsx
index 9fc5257c4..f036dbad1 100644
--- a/web-app/src/containers/ModelSetting.tsx
+++ b/web-app/src/containers/ModelSetting.tsx
@@ -172,7 +172,8 @@ export function ModelSetting({
key === 'offload_mmproj' ||
key === 'batch_size' ||
key === 'cpu_moe' ||
- key === 'n_cpu_moe'
+ key === 'n_cpu_moe' ||
+ key === 'n_cpu_ffn'
) {
// Check if model is running before stopping it
serviceHub
diff --git a/web-app/src/hooks/useModelProvider.ts b/web-app/src/hooks/useModelProvider.ts
index 6354a309e..9bc50355f 100644
--- a/web-app/src/hooks/useModelProvider.ts
+++ b/web-app/src/hooks/useModelProvider.ts
@@ -775,9 +775,29 @@ export const useModelProvider = create<ModelProviderState>()(
})
}
+ if (version <= 18 && state?.providers) {
+ // llama.cpp 0.4.0 added --n-cpu-ffn, the dense counterpart of
+ // --n-cpu-moe; add the control to persisted llamacpp models so the
+ // sidebar renders it.
+ state.providers.forEach((provider) => {
+ if (provider.provider !== 'llamacpp' || !provider.models) return
+ provider.models.forEach((model) => {
+ if (!model.settings) model.settings = {}
+ if (!model.settings.n_cpu_ffn) {
+ model.settings.n_cpu_ffn = {
+ ...modelSettings.n_cpu_ffn,
+ controller_props: {
+ ...modelSettings.n_cpu_ffn.controller_props,
+ },
+ }
+ }
+ })
+ })
+ }
+
return state
},
- version: 18,
+ version: 19,
}
)
)
diff --git a/web-app/src/lib/predefined.ts b/web-app/src/lib/predefined.ts
index e760244c0..7aaa8e5f4 100644
--- a/web-app/src/lib/predefined.ts
+++ b/web-app/src/lib/predefined.ts
@@ -173,6 +173,18 @@ export const modelSettings = {
type: 'number',
},
},
+ n_cpu_ffn: {
+ key: 'n_cpu_ffn',
+ title: 'Number of dense FFN weights in the CPU',
+ description:
+ 'Keep the dense feed-forward weights of the first N layers in the CPU (if GPU is used). For dense models; MoE experts use the setting above.',
+ controller_type: 'input',
+ controller_props: {
+ value: '',
+ placeholder: '24',
+ type: 'number',
+ },
+ },
override_tensor_buffer_t: {
key: 'override_tensor_buffer_t',
title: 'Override Tensor Buffer Type',