Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(llamacpp): run MTP models on one slot when Parallel Sequences is auto (#9186)
James Dam0386e814f206
diff --git a/extensions/llamacpp-extension/src/preset.test.ts b/extensions/llamacpp-extension/src/preset.test.tsindex 2189744b0..04b7311d5 100644--- a/extensions/llamacpp-extension/src/preset.test.ts+++ b/extensions/llamacpp-extension/src/preset.test.ts@@ -275,6 +275,95 @@ describe('generatePreset 0.4.0 keys', () => {})})+// A slot of a hybrid model with speculative decoding holds (1 + n_max) rows+// of recurrent state, so auto's 4 slots overflowed a 12 GB card in 0.8.5.+describe('generatePreset parallel with speculative decoding', () => {+ const section = (ini: string, id: string) =>+ ini.split(`[${id}]`)[1].split('load-on-startup')[0]++ it('pins an embedded-MTP model to one slot when parallel is auto', async () => {+ setupModel('qwen', { mtp: true, mtp_layers: 1 })+ await generatePreset('/p', '/jan', { parallel: 0 } as any)+ const ini = writtenFiles['/p/router.preset.ini']+ expect(section(ini, 'qwen')).toContain('parallel = 1')+ expect(section(ini, 'qwen')).toContain('kv-unified = true')+ expect(ini.split('[qwen]')[0]).not.toContain('parallel =')+ })++ it('pins a draft-companion model to one slot when parallel is unset', async () => {+ setupModel('qwen', {+ mtp: true,+ mtp_layers: 0,+ mtp_model_path: 'models/qwen/mtp.gguf',+ })+ await generatePreset('/p', '/jan', {} as any)+ expect(section(writtenFiles['/p/router.preset.ini'], 'qwen')).toContain(+ 'parallel = 1'+ )+ })++ it('leaves models without speculative decoding on auto', async () => {+ setupModel('off', { mtp: false, mtp_layers: 1 })+ setupModel('plain', {})+ await generatePreset('/p', '/jan', { parallel: 0 } as any)+ const ini = writtenFiles['/p/router.preset.ini']+ expect(ini).not.toContain('parallel =')+ expect(ini).not.toContain('kv-unified')+ })++ it('keeps an explicit global parallel for an MTP model', async () => {+ setupModel('qwen', { mtp: true, mtp_layers: 1 })+ await generatePreset('/p', '/jan', { parallel: 2 } as any)+ const ini = writtenFiles['/p/router.preset.ini']+ expect(ini.split('[qwen]')[0]).toContain('parallel = 2')+ expect(section(ini, 'qwen')).not.toContain('parallel =')+ })++ // The gate and the [*] emission must read the string the same way: if only+ // the gate accepted it, nothing would be emitted and auto's 4 slots return.+ it('emits a legacy string parallel and does not pin the MTP model', async () => {+ setupModel('qwen', { mtp: true, mtp_layers: 1 })+ await generatePreset('/p', '/jan', { parallel: '2' } as any)+ const ini = writtenFiles['/p/router.preset.ini']+ expect(ini.split('[qwen]')[0]).toContain('parallel = 2')+ expect(section(ini, 'qwen')).not.toContain('parallel =')+ })++ it('treats a zero or non-numeric string parallel as auto and pins', async () => {+ for (const parallel of ['0', '', 'auto']) {+ setupModel('qwen', { mtp: true, mtp_layers: 1 })+ await generatePreset('/p', '/jan', { parallel } as any)+ const ini = writtenFiles['/p/router.preset.ini']+ expect(ini.split('[qwen]')[0]).not.toContain('parallel =')+ expect(section(ini, 'qwen')).toContain('parallel = 1')+ }+ })++ it('keeps a legacy string per-model parallel for an MTP model', async () => {+ setupModel('qwen', { mtp: true, mtp_layers: 1, parallel: '2' })+ await generatePreset('/p', '/jan', {} as any)+ const qwen = section(writtenFiles['/p/router.preset.ini'], 'qwen')+ expect(qwen).toContain('parallel = 2')+ expect(qwen).not.toContain('parallel = 1')+ })++ it('keeps an explicit per-model parallel for an MTP model', async () => {+ setupModel('qwen', { mtp: true, mtp_layers: 1, parallel: 2 })+ await generatePreset('/p', '/jan', {} as any)+ const qwen = section(writtenFiles['/p/router.preset.ini'], 'qwen')+ expect(qwen).toContain('parallel = 2')+ expect(qwen).not.toContain('parallel = 1')+ })++ it('respects an explicit kv-unified off', async () => {+ setupModel('qwen', { mtp: true, mtp_layers: 1 })+ await generatePreset('/p', '/jan', { kv_unified: 'off' } as any)+ const ini = writtenFiles['/p/router.preset.ini']+ expect(section(ini, 'qwen')).toContain('parallel = 1')+ expect(section(ini, 'qwen')).not.toContain('kv-unified')+ })+})+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.tsindex 0c4b946de..579eb702d 100644--- a/extensions/llamacpp-extension/src/preset.ts+++ b/extensions/llamacpp-extension/src/preset.ts@@ -137,6 +137,20 @@ async function existingFilePath(value: string): Promise<string | null> {}}+/**+ * A user's Parallel Sequences value as a slot count, or undefined for auto.+ * Settings saved by older builds can hold the number as a string, so both the+ * emission and the speculative-decoding pin read it through here: if they+ * disagreed, a string '2' would suppress the pin without ever being emitted,+ * and llama.cpp would resolve auto to 4 slots anyway.+ */+function explicitParallel(v: unknown): number | undefined {+ const n = typeof v === 'string' && v.trim() !== '' ? Number(v) : v+ if (typeof n !== 'number' || !Number.isFinite(n)) return undefined+ const slots = Math.floor(n)+ return slots > 0 ? slots : undefined+}+function escapeIniValue(v: string): string {// INI values for llama-server are read as strings; trim surrounding whitespace// and strip stray newlines that would break parsing.@@ -263,8 +277,9 @@ export async function generatePreset(// value is intent and is emitted verbatim. Jan adds no hidden slot of its// own: every pinned surface shares slot 0 and is told apart by `thread_id`// (web-app/src/constants/models.ts), so no slot has to be reserved for one.- if (typeof config.parallel === 'number' && config.parallel > 0) {- lines.push(`parallel = ${config.parallel}`)+ const globalParallel = explicitParallel(config.parallel)+ if (globalParallel !== undefined) {+ lines.push(`parallel = ${globalParallel}`)// 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@@ -610,8 +625,34 @@ export async function generatePreset() {lines.push(`cache-type-v = ${escapeIniValue(mc.cache_type_v)}`)}- if (typeof mc.parallel === 'number' && mc.parallel > 0) {- lines.push(`parallel = ${mc.parallel}`)+ // MTP either lives in the main gguf (mtp_layers > 0) or ships as a separate+ // draft gguf (mtp_model_path), which is passed to the engine as the draft.+ const hasMtpModel =+ typeof mc.mtp_model_path === 'string' && mc.mtp_model_path.length > 0+ const hasMtpLayers =+ typeof mc.mtp_layers === 'number' && mc.mtp_layers > 0+ const specDecoding = mc.mtp === true && (hasMtpLayers || hasMtpModel)++ const modelParallel = explicitParallel(mc.parallel)+ if (modelParallel !== undefined) {+ lines.push(`parallel = ${modelParallel}`)+ if (kvUnifiedIsAuto) lines.push('kv-unified = true')+ } else if (+ specDecoding &&+ explicitParallel(config.parallel) === undefined+ ) {+ // A model with speculative decoding gets one slot when parallel is auto.+ // Every slot of a hybrid model (qwen35) carries (1 + spec-draft-n-max)+ // rows of recurrent state for draft rollback -- about 600 MiB each on a+ // 27B -- so llama.cpp's auto resolution to 4 slots costs ~1.2 GB more+ // VRAM than the 2 Jan used before 0.8.5. On Windows the NVIDIA driver+ // then spills into shared system RAM instead of failing, and decode+ // drops 4x (59 -> 14 tok/s on a 12 GB card). Jan's own surfaces share+ // slot 0 anyway. Only a positive value, global or per-model, counts as+ // the user's: auto (0) is also what the 0.8.5 migration wrote, so the+ // two cannot be told apart. A legacy numeric string still counts, and is+ // emitted in [*] above (see explicitParallel).+ lines.push('parallel = 1')if (kvUnifiedIsAuto) lines.push('kv-unified = true')}if (mc.cont_batching === false) {@@ -653,13 +694,7 @@ export async function generatePreset(lines.push('mmproj-offload = false')}- // MTP either lives in the main gguf (mtp_layers > 0) or ships as a separate- // draft gguf (mtp_model_path), which is passed to the engine as the draft.- const hasMtpModel =- typeof mc.mtp_model_path === 'string' && mc.mtp_model_path.length > 0- const hasMtpLayers =- typeof mc.mtp_layers === 'number' && mc.mtp_layers > 0- if (mc.mtp === true && (hasMtpLayers || hasMtpModel)) {+ if (specDecoding) {const specType =typeof mc.spec_type === 'string' && SPEC_TYPES.has(mc.spec_type)? mc.spec_type