Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(llamacpp): stop halving per-slot context when parallel is set (#8534)
Akarshan Biswas7832e1904585
diff --git a/extensions/llamacpp-extension/src/preset.test.ts b/extensions/llamacpp-extension/src/preset.test.tsindex 2d7c74d4b..28790ee37 100644--- a/extensions/llamacpp-extension/src/preset.test.ts+++ b/extensions/llamacpp-extension/src/preset.test.ts@@ -210,6 +210,50 @@ describe('generatePreset parallel reservation', () => {})})+describe('generatePreset kv-unified', () => {+ it('enables unified KV on auto when an explicit parallel is emitted', async () => {+ setupModel('llama', {})+ await generatePreset('/p', '/jan', { parallel: 1 } as any, {+ supportsMtp: false,+ })+ const ini = writtenFiles['/p/router.preset.ini']+ expect(ini).toContain('parallel = 2')+ expect(ini).toContain('kv-unified = true')+ })++ it('enables unified KV on auto when only a per-model parallel is emitted', async () => {+ setupModel('llama', { parallel: 3 })+ await generatePreset('/p', '/jan', {} as any, { supportsMtp: false })+ const ini = writtenFiles['/p/router.preset.ini']+ expect(ini).toContain('kv-unified = true')+ })++ it('omits kv-unified on auto when no explicit parallel is emitted', async () => {+ setupModel('llama', {})+ await generatePreset('/p', '/jan', {} as any, { supportsMtp: false })+ const ini = writtenFiles['/p/router.preset.ini']+ expect(ini).not.toContain('kv-unified')+ })++ it('respects an explicit off even when parallel is emitted', async () => {+ setupModel('llama', {})+ await generatePreset('/p', '/jan', { parallel: 1, kv_unified: 'off' } as any, {+ supportsMtp: false,+ })+ const ini = writtenFiles['/p/router.preset.ini']+ expect(ini).toContain('kv-unified = false')+ })++ it('respects an explicit on when no parallel is emitted', async () => {+ setupModel('llama', {})+ await generatePreset('/p', '/jan', { kv_unified: 'on' } as any, {+ supportsMtp: false,+ })+ const ini = writtenFiles['/p/router.preset.ini']+ expect(ini).toContain('kv-unified = true')+ })+})+describe('generatePreset ctx-size default', () => {it('emits ctx-size = 8192 in [*] when fit is off and no ctx_size is set', async () => {setupModel('llama', {})diff --git a/extensions/llamacpp-extension/src/preset.ts b/extensions/llamacpp-extension/src/preset.tsindex e244a96ee..7fa0bf8ae 100644--- a/extensions/llamacpp-extension/src/preset.ts+++ b/extensions/llamacpp-extension/src/preset.ts@@ -127,6 +127,9 @@ export async function generatePreset(modelEntries.sort((a, b) => a.modelId.localeCompare(b.modelId))+ const kvUnifiedIsAuto =+ config.kv_unified !== 'on' && config.kv_unified !== 'off'+const lines: string[] = []// Emit only values that differ from llama.cpp's compiled defaults so the@@ -204,6 +207,11 @@ export async function generatePreset(// slot is added on top and never exposed in the setting's own value.if (typeof config.parallel === 'number' && config.parallel > 0) {lines.push(`parallel = ${config.parallel + reservedBackgroundSlots}`)+ // llama.cpp only turns on unified KV as part of resolving parallel = -1;+ // passing parallel explicitly leaves it off, which splits ctx-size into+ // ctx-size/parallel per slot. Restore the auto behaviour so the configured+ // context is what each slot actually gets.+ if (kvUnifiedIsAuto) lines.push('kv-unified = true')}// cont-batching default = true; emit only the explicit-off case.if (config.cont_batching === false) {@@ -326,7 +334,8 @@ export async function generatePreset(if (config.swa_full === true) {lines.push('swa-full = true')}- // auto = omit the flag, let llama.cpp decide based on slot count+ // auto is handled next to each `parallel` emission above; with no explicit+ // parallel the flag is omitted so llama.cpp's own auto resolution applies.if (config.kv_unified === 'on') {lines.push('kv-unified = true')} else if (config.kv_unified === 'off') {@@ -412,6 +421,7 @@ export async function generatePreset(}if (typeof mc.parallel === 'number' && mc.parallel > 0) {lines.push(`parallel = ${mc.parallel + reservedBackgroundSlots}`)+ if (kvUnifiedIsAuto) lines.push('kv-unified = true')}if (mc.cont_batching === false) {lines.push('cont-batching = false')