diff --git a/apps/host/src/components/NativeModels.tsx b/apps/host/src/components/NativeModels.tsx index 9523827c..4bf16382 100644 --- a/apps/host/src/components/NativeModels.tsx +++ b/apps/host/src/components/NativeModels.tsx @@ -204,7 +204,7 @@ export function NativeModels({ onOtherModels, onReady, onImport, onConnectEngine {tr(expert && IMAGE_GENERATION_VISIBLE ? <>
{tr("Image generation ")}{tr("Optional")} { }}/>
: null)} {tr(expert ?

{tr("Expert settings")}

{tr(tunedNow ?

{tr("Showing the values of your tuning '{0}'.", [tunedNow.name])}

: null)}
: null)} - + !pending && setPlan(null)} actions={<> setPlan(null)}>{tr("Cancel")} void download()}>{tr(installed?.ready ? "Apply & load" : "Download & load")}}>

{tr(plan?.plan?.title ?? selectedFamily?.title)}{tr(installed?.ready ? null : <>{tr(" \u00B7 approximately ")}{tr(plan?.plan?.download_gb ?? selectedSize?.download_gb)}{tr(" GB")})}

{tr(plan?.drafting?.needed ?

{tr("Choosing DFlash2 also downloads its drafter: about {0} GB, once. If it does not fit in graphics memory, the model uses the MTP instead.", [plan.drafting.gb])}

: null)}{tr(installed?.ready ?

{tr("The model files are already on this computer. Nothing will be downloaded; only the settings you chose are applied.")}

: null)}{tr(plan?.plan?.disk_short ? : null)}{tr(installed?.ready ?

{tr("Any currently loaded native models will be unloaded first.")}

: <>

{tr("Existing files are checked and reused. Any currently loaded native models will be unloaded before preparing files.")}

{tr("Your computer may feel slower while weights load. You can stop the download and resume later.")}

)}{tr(selectedSize?.images ?

{tr("Vision encoder files are included. Picture reading loads only when enabled.")}

: null)}
diff --git a/apps/host/src/lib/native-fit.ts b/apps/host/src/lib/native-fit.ts index 23dad9e6..b34d2784 100644 --- a/apps/host/src/lib/native-fit.ts +++ b/apps/host/src/lib/native-fit.ts @@ -30,16 +30,27 @@ export function memoryNeeded(v: FitInput): number { return round1((v.experts_gb ?? v.ram_needed_gb ?? 0) + DENSE_EXTRA_GB); } -export function fitOn(v: FitInput, hw: FitHardware | null | undefined, mtp = true): Fit { +/** The conversation cache of Flash-Next: 13 attention layers, bytes per token per layer by cache format, 5.4% over the bare cache + * (strata_control/vram_fit.py KV_BYTES, KV_MEASURED_FACTOR; setup.py UD_FIT_KV_BYTES). */ +const KV_BYTES: Record = { fp16: 2112, int8: 1056, q4_0: 576, k8v4: 816 }; +const KV_LAYERS = 13, KV_FACTOR = 1.054, BASE_CONTEXT = 32768; +export function cacheGb(context: number, kv = 'int8'): number { + return context * KV_LAYERS * (KV_BYTES[String(kv).toLowerCase()] ?? KV_BYTES.int8) * KV_FACTOR / 2 ** 30; +} +export interface CacheChoice { context: number; kv?: string } + +export function fitOn(v: FitInput, hw: FitHardware | null | undefined, mtp = true, cache?: CacheChoice | null): Fit { const memoryGb = memoryNeeded(v); + // DENSE_GB already holds a 32K cache in the default format; the person's context and format replace it (#3832). + const denseGb = cache && cache.context > 0 ? Math.max(DENSE_GB - cacheGb(BASE_CONTEXT), 0) + cacheGb(cache.context, cache.kv) : DENSE_GB; const ram = Number(hw?.ram_gb), vram = Number(hw?.vram_gb) || 0; if (!Number.isFinite(ram) || ram <= 0) return { memoryGb, vramUseGb: 0, ramUseGb: 0, spillGb: 0, known: false }; const experts = Math.max(0, memoryNeeded(v) - DENSE_EXTRA_GB); - const onGpu = Math.max(0, Math.min(experts, vram - DENSE_GB - (mtp && vram > 0 ? MTP_VRAM_GB : 0))); + const onGpu = Math.max(0, Math.min(experts, vram - denseGb - (mtp && vram > 0 ? MTP_VRAM_GB : 0))); const ramRoom = Math.max(0, ram - SYSTEM_ROOM_GB); const spillGb = Math.max(0, experts - onGpu - ramRoom); const inRam = Math.max(0, experts - onGpu - spillGb); - const vramUseGb = vram > 0 ? Math.min(vram, DENSE_GB + (mtp ? MTP_VRAM_GB : 0) + onGpu) : 0; + const vramUseGb = vram > 0 ? Math.min(vram, denseGb + (mtp ? MTP_VRAM_GB : 0) + onGpu) : 0; return { memoryGb, vramUseGb: round1(vramUseGb), ramUseGb: round1(inRam + DENSE_EXTRA_GB * (vram > 0 ? 0 : 1)), spillGb: round1(spillGb), known: true }; } diff --git a/apps/host/tests/native-fit.test.mjs b/apps/host/tests/native-fit.test.mjs index b09e2b2d..b1fb57fd 100644 --- a/apps/host/tests/native-fit.test.mjs +++ b/apps/host/tests/native-fit.test.mjs @@ -5,7 +5,7 @@ import { readFile } from 'node:fs/promises'; import { transform } from 'esbuild'; const { code } = await transform(await readFile(new URL('../src/lib/native-fit.ts', import.meta.url), 'utf8'), { loader: 'ts', format: 'esm' }); -const { fitOn, memoryNeeded, RECOMMENDED, VERSION_GROUPS, groupOpen, recommendedVersions, PICK_NOTE, OTHER_NOTE, MTP_VRAM_GB } = await import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`); +const { fitOn, cacheGb, memoryNeeded, RECOMMENDED, VERSION_GROUPS, groupOpen, recommendedVersions, PICK_NOTE, OTHER_NOTE, MTP_VRAM_GB } = await import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`); const swift = { ram_needed_gb: 48, experts_gb: 35.5 }; const gyroS = { ram_needed_gb: 27.6, experts_gb: 25.8, mem_total: true }; @@ -83,3 +83,15 @@ test('the tiers of the engine are 40 GB Swift IQ2_XS, 48 GB IQ3_XXS, 64 GB IQ3_S assert.deepEqual([...m[1].matchAll(/\(([\d.]+),\s*"(\w+)",\s*"(\w+)"\)/g)].map(x => [Number(x[1]), x[2], x[3]]), [[62, 'qwen', 'IQ3_S'], [46, 'qwen', 'IQ3_XXS'], [38, 'swift', 'IQ2_XS']], file); } }); + +test('the conversation context and cache format move the estimate (#3832)', () => { + const pc = { ram_gb: 64, vram_gb: 16 }; + const base = fitOn(swift, pc); + assert.deepEqual(fitOn(swift, pc, true, { context: 32768, kv: 'int8' }), base, 'the default context is what the fixed figure already held'); + const big = fitOn(swift, pc, true, { context: 524288, kv: 'int8' }); + assert.ok(big.vramUseGb >= base.vramUseGb && big.ramUseGb > base.ramUseGb, 'a bigger cache takes graphics memory from the experts, which then sit in RAM'); + assert.ok(fitOn(swift, pc, true, { context: 524288, kv: 'q4_0' }).ramUseGb < big.ramUseGb, 'a smaller cache format leaves more room'); + assert.ok(Math.abs(cacheGb(524288, 'int8') * 1024 - 7361) < 25, 'the 512K Int8 cache is the 7361 MiB the engine reports'); + const tight = fitOn(q4, pc, true, { context: 524288, kv: 'int8' }); + assert.ok(tight.spillGb > fitOn(q4, pc).spillGb, 'what no longer fits is read from the SSD and the card says so'); +});