okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { readFileSync , realpathSync } from 'node:fs' ; 4import { join } from 'node:path' ; 5import { resourceSettings } from '../server/native-resources.mjs' ; 6import { markPictures , runtimeVision } from '../server/native-vision.mjs' ; 7import { nativeFixture , finishJob } from './helpers/native-fixture.mjs' ; 8 9test ( 'new native downloads use the selected model root without adding a legacy subfolder' , async () => { 10const directory = '/fixture/Documents/Strata/Models' , f = nativeFixture ({ modelsDirectory :() => directory }); try { 11await f . control . refresh (); f . installed . models . length = 0 ; await f . control . refresh ( true ); 12assert . equal ( f . control . state (). downloadDirectory , directory ); 13await f . control . plan ({ family :'qwen' , model :'IQ2_XS' }); 14assert . equal ( f . rpcCalls . find ( c => c . name === 'strata_install' ). arguments . data_dir , directory ); 15} finally { await f . cleanup ();} 16}); 17 18test ( 'work profile reserves GPU memory and CPU workers while preserving model arguments' ,() => { 19const hardware = { cpu :{ cores :10 }, gpus :[{ index :0 , vram_gb :24 }]}; 20const original = { gpu :0 , args :[ '--max-context' , '32768' , '--pool-workers' , '10' , '--vram-reserve-mib' , '700' ], vision :{ gpu :true }}; 21const work = resourceSettings ( original , hardware , 'work' ); 22assert . deepEqual ( work . args . slice ( 0 , 2 ),[ '--max-context' , '32768' ]); 23assert . equal ( work . args [ work . args . indexOf ( '--pool-workers' ) + 1 ], '8' ); 24assert . equal ( work . vram_reserve_mib , 4915 ); 25assert . equal ( original . args [ 3 ], '10' ); 26const performance = resourceSettings ({ ...original , ...work , ui_resource_profile :'work' }, hardware , 'performance' ); 27assert . equal ( performance . args [ performance . args . indexOf ( '--pool-workers' ) + 1 ], '10' ); 28assert . equal ( performance . vram_reserve_mib , 2048 ); // The vision encoder still needs its safety reserve. 29assert . throws (() => resourceSettings ( original , hardware , 'invalid' ), / preference / ); 30}); 31 32test ( 'guided tuning targets the prepared model, persists headroom and opens one measured instance' , async () => { 33const f = nativeFixture (); try { 34f . setHardware ({ cpu :{ cores :10 }, ram_gb :128 , gpus :[{ index :0 , name :'NVIDIA RTX 4090' , vram_gb :24 }]}); 35await f . control . refresh ( true ); 36await finishJob ( f . control , f . control . operation ( 'load' ,{ model :'iq3_s' }). id ); 37const tuned = await finishJob ( f . control , f . control . operation ( 'tune' ,{ model :'iq2_xs' , profile :'work' , calibrate :true }). id ); 38assert . equal ( tuned . state , 'done' , tuned . error ); 39assert . ok ( f . calls . find ( call => call . args [ 0 ] === '-c' && call . args [ 2 ]. endsWith ( 'strata-iq2_xs.json' ))); 40assert . equal ( f . control . state (). instances . length , 1 ); 41assert . equal ( f . control . state (). instances [ 0 ]. model , 'iq2_xs' ); 42assert . equal ( tuned . result . outputTps , 41 ); 43const config = JSON . parse ( readFileSync ( join ( f . repo , 'strata-iq2_xs.json' ))); 44assert . equal ( config . ui_resource_profile , 'work' ); assert . equal ( config . vram_reserve_mib , 4915 ); 45assert . deepEqual ( config . unknown_native_key ,{ preserve :true }); 46assert . equal ( config . args [ config . args . indexOf ( '--pool-workers' ) + 1 ], '8' ); 47assert . ok ( readFileSync ( join ( f . repo , 'strata-iq2_xs.json.bak' ))); 48} finally { await f . cleanup ();} 49}); 50 51test ( 'quick setup applies resource settings without invoking long calibration' , async () => { 52const f = nativeFixture (); try { 53await f . control . refresh (); 54const tuned = await finishJob ( f . control , f . control . operation ( 'tune' ,{ model :'iq2_xs' , profile :'performance' , calibrate :false }). id ); 55assert . equal ( tuned . state , 'done' , tuned . error ); 56assert . equal ( f . calls . some ( c => c . args [ 0 ] === '-c' ), false ); 57assert . equal ( f . control . benchmarkReport (). source , 'engine' ); 58} finally { await f . cleanup ();} 59}); 60 61test ( 'an existing native model folder is persisted and used by the installer without moving files' , async () => { 62const f = nativeFixture (); try { 63await f . control . refresh (); f . installed . models . length = 0 ; await f . control . refresh ( true ); 64f . control . setDownloadDirectory ( f . directory ); 65assert . equal ( f . control . state (). downloadDirectory , realpathSync ( f . directory )); 66await f . control . plan ({ family :'qwen' , model :'IQ2_XS' }); 67assert . equal ( f . rpcCalls . find ( c => c . name === 'strata_install' ). arguments . data_dir , realpathSync ( f . directory )); 68assert . throws (() => f . control . setDownloadDirectory ( join ( f . repo , 'setup.py' )), / folder / ); 69} finally { await f . cleanup ();} 70}); 71 72test ( 'preparation completes without loading until the user chooses a resource preference' , async () => { 73const f = nativeFixture (); try { 74await f . control . refresh (); 75const prepared = await finishJob ( f . control , f . control . operation ( 'install' ,{ family :'qwen' , model :'IQ2_XS' , loadAfter :false }). id ); 76assert . equal ( prepared . state , 'done' , prepared . error ); assert . equal ( f . control . state (). instances . length , 0 ); 77assert . equal ( f . calls . some ( c => c . args [ 0 ]?. replaceAll ( '\\' , '/' ). endsWith ( 'serve/server.py' )), false ); 78} finally { await f . cleanup ();} 79}); 80 81test ( 'stopping long tuning retains files and does not continue into loading' , async () => { 82const f = nativeFixture (); try { 83await f . control . refresh (); f . setProgramWait ( true ); 84const job = f . control . operation ( 'tune' ,{ model :'iq2_xs' , profile :'work' }); 85while ( ! f . calls . some ( c => c . args [ 0 ] === '-c' )) await new Promise ( r => setTimeout ( r , 5 )); 86await f . control . cancel ( job . id ); await new Promise ( r => setTimeout ( r , 10 )); 87assert . equal ( f . control . state (). jobs . find ( j => j . id === job . id ). state , 'cancelled' ); 88assert . equal ( f . control . state (). instances . length , 0 ); 89assert . ok ( readFileSync ( join ( f . repo , 'strata-iq2_xs.json' ))); 90assert . equal ( f . calls . some ( c => c . args [ 0 ]?. replaceAll ( '\\' , '/' ). endsWith ( 'serve/server.py' )), false ); 91} finally { await f . cleanup ();} 92}); 93 94test ( 'the consumer can change resource preference later and restore full performance' , async () => { 95const f = nativeFixture (); try { 96f . setHardware ({ cpu :{ cores :10 }, gpus :[{ index :0 , name :'NVIDIA RTX 4090' , vram_gb :24 }]}); await f . control . refresh (); 97const work = await finishJob ( f . control , f . control . operation ( 'load' ,{ model :'iq2_xs' , resourceProfile :'work' }). id ); 98assert . equal ( work . state , 'done' , work . error ); 99assert . equal ( f . control . state (). installed . models [ 0 ]. resourceProfile , 'work' ); 100const performance = await finishJob ( f . control , f . control . operation ( 'load' ,{ model :'iq2_xs' , resourceProfile :'performance' }). id ); 101assert . equal ( performance . state , 'done' , performance . error ); assert . equal ( f . control . state (). instances . length , 1 ); 102const config = JSON . parse ( readFileSync ( join ( f . repo , 'strata-iq2_xs.json' ))); 103assert . equal ( config . ui_resource_profile , 'performance' ); assert . equal ( config . vram_reserve_mib , 1024 ); // the fixture's picture reader is the CPU one (gpu:0): no VRAM, so the 24 GB card's 1024 floor 104assert . equal ( config . args . includes ( '--pool-workers' ), false ); 105} finally { await f . cleanup ();} 106}); 107 108test ( 'stopping during the health check cannot leave a cancelled model loaded' , async () => { 109const f = nativeFixture (); try { 110await f . control . refresh (); f . setHealthWait ( true ); 111const job = f . control . operation ( 'load' ,{ model :'iq2_xs' , resourceProfile :'work' }); 112while ( ! f . hasPendingHealth ()) await new Promise ( r => setTimeout ( r , 5 )); 113await f . control . cancel ( job . id ); f . releaseHealth (); await new Promise ( r => setTimeout ( r , 20 )); 114assert . equal ( f . control . state (). instances . length , 0 ); 115assert . equal ( f . control . state (). jobs . find ( j => j . id === job . id ). state , 'cancelled' ); 116} finally { f . releaseHealth (); await f . cleanup ();} 117}); 118 119test ( 'vision can be disabled at load without losing the prepared encoder config or triggering install' , async () => { 120const f = nativeFixture (); try { 121await f . control . refresh (); 122const off = await finishJob ( f . control , f . control . operation ( 'tune' ,{ model :'iq2_xs' , profile :'work' , calibrate :false , vision :'no' }). id ); 123assert . equal ( off . state , 'done' , off . error ); assert . equal ( f . control . state (). instances [ 0 ]. vision , false ); 124const source = JSON . parse ( readFileSync ( join ( f . repo , 'strata-iq2_xs.json' ))); 125assert . deepEqual ( source . vision ,{ gpu :0 }); assert . equal ( source . ui_vision_enabled , false ); 126assert . equal ( f . control . state (). installed . models [ 0 ]. visionEnabled , false ); 127const proc = f . calls . findLast ( c => c . args [ 0 ]?. replaceAll ( '\\' , '/' ). endsWith ( 'serve/server.py' )); 128assert . equal ( JSON . parse ( readFileSync ( proc . args [ proc . args . indexOf ( '--config' ) + 1 ])). vision , undefined ); 129const again = await finishJob ( f . control , f . control . operation ( 'load' ,{ model :'iq2_xs' , vision :'no' }). id ); 130assert . equal ( again . state , 'done' , again . error ); assert . equal ( f . calls . filter ( c => c . args [ 0 ]?. replaceAll ( '\\' , '/' ). endsWith ( 'serve/server.py' )). length , 1 ); 131const on = await finishJob ( f . control , f . control . operation ( 'load' ,{ model :'iq2_xs' , vision :'cpu' }). id ); 132assert . equal ( on . state , 'done' , on . error ); assert . equal ( f . control . state (). instances [ 0 ]. vision , true ); 133const runtime = f . calls . findLast ( c => c . args [ 0 ]?. replaceAll ( '\\' , '/' ). endsWith ( 'serve/server.py' )); 134assert . equal ( JSON . parse ( readFileSync ( runtime . args [ runtime . args . indexOf ( '--config' ) + 1 ])). vision . gpu , false ); 135assert . equal ( f . control . state (). installed . models [ 0 ]. visionEnabled , true ); 136assert . equal ( f . control . state (). installed . models [ 0 ]. visionDevice , 'cpu' ); 137assert . equal ( f . rpcCalls . some ( c => c . name === 'strata_install' ), false ); 138} finally { await f . cleanup ();} 139}); 140 141test ( 'a load while the engine has no picture reader runs without pictures and saves nothing (it used to save "off" for good)' , async () => { 142const f = nativeFixture (); try { 143await f . control . refresh (); 144const first = await finishJob ( f . control , f . control . operation ( 'load' ,{ model :'iq2_xs' , vision :'no' , visionUnavailable :true }). id ); 145assert . equal ( first . state , 'done' , first . error ); 146assert . equal ( f . control . state (). instances [ 0 ]. vision , false , 'no picture reading for this run' ); 147const source = JSON . parse ( readFileSync ( join ( f . repo , 'strata-iq2_xs.json' ))); 148assert . equal ( source . ui_vision_enabled , undefined , 'no off was saved' ); 149assert . deepEqual ( source . vision ,{ gpu :0 }, 'the prepared picture files stay' ); 150assert . equal ( f . control . state (). installed . models [ 0 ]. visionEnabled , false , 'while it runs without pictures the state says so' ); 151} finally { await f . cleanup ();} 152}); 153 154test ( 'a context the person picked is saved with a marker and read back (32K included); nothing is marked when none was picked' , async () => { 155const plainFixture = nativeFixture (); try { 156await plainFixture . control . refresh (); 157const plain = await finishJob ( plainFixture . control , plainFixture . control . operation ( 'load' ,{ model :'iq2_xs' }). id ); 158assert . equal ( plain . state , 'done' , plain . error ); 159assert . equal ( JSON . parse ( readFileSync ( join ( plainFixture . repo , 'strata-iq2_xs.json' ))). ui_context_chosen , undefined ); 160assert . equal ( plainFixture . control . state (). installed . models [ 0 ]. contextChosen , false ); 161} finally { await plainFixture . cleanup ();} 162const f = nativeFixture (); try { 163await f . control . refresh (); 164const picked = await finishJob ( f . control , f . control . operation ( 'load' ,{ model :'iq2_xs' , contextChosen :true }). id ); 165assert . equal ( picked . state , 'done' , picked . error ); 166assert . equal ( JSON . parse ( readFileSync ( join ( f . repo , 'strata-iq2_xs.json' ))). ui_context_chosen , true ); 167assert . equal ( f . control . state (). installed . models [ 0 ]. contextChosen , true ); 168} finally { await f . cleanup ();} 169}); 170 171test ( 'runtime vision checks prepared files, validates mode, and leaves the source config intact' ,() => { 172const config = { vision :{ mmproj :'/models/mmproj.gguf' , gpu :true , max_tokens :8192 }, ui_vision_enabled :false }; 173const before = structuredClone ( config ); 174assert . equal ( runtimeVision ( config ), undefined ); 175assert . equal ( runtimeVision ( config , 'cpu' ). gpu , false ); 176assert . equal ( runtimeVision ( config , 'yes' ). mmproj , config . vision . mmproj ); 177assert . deepEqual ( config , before ); 178const amd = { vision :{ exe :'/e/strata-vision-hip' , cpu_exe :'/e/strata-vision' , mmproj :'/m.gguf' , gpu :true }}; 179assert . deepEqual ( runtimeVision ( amd , 'cpu' ),{ exe :'/e/strata-vision' , mmproj :'/m.gguf' , gpu :false }); 180assert . equal ( runtimeVision ( amd , 'gpu' ). exe , '/e/strata-vision-hip' ); assert . equal ( runtimeVision ( amd , 'gpu' ). cpu_exe , '/e/strata-vision' ); 181assert . equal ( runtimeVision ( amd , 'yes' ). gpu , true ); 182assert . throws (() => runtimeVision ({}, 'yes' ), / Prepare / ); 183assert . throws (() => runtimeVision ( config , 'invalid' ), / picture reading / ); 184}); 185 186test ( 'download-and-load prepares vision automatically while loading text only when requested' , async () => { 187const f = nativeFixture (); try { 188await f . control . refresh (); 189const job = await finishJob ( f . control , f . control . operation ( 'install' ,{ family :'qwen' , model :'IQ2_XS' , vision :'yes' , loadVision :'no' , loadAfter :true }). id ); 190assert . equal ( job . state , 'done' , job . error ); 191const install = f . rpcCalls . find ( c => c . name === 'strata_install' && c . arguments . confirm ); 192assert . equal ( install . arguments . vision , 'yes' ); assert . equal ( install . arguments . loadVision , undefined ); 193assert . equal ( f . control . state (). instances [ 0 ]. vision , false ); 194assert . deepEqual ( JSON . parse ( readFileSync ( join ( f . repo , 'strata-iq2_xs.json' ))). vision ,{ gpu :0 }); 195} finally { await f . cleanup ();} 196}); 197 198test ( 'a card that shares the PC memory does not hold back a fifth of the shared pool as graphics memory (#3510)' ,() => { 199const original = { gpu :0 , args :[ '--max-context' , '131072' , '--pool-workers' , '10' ], vision :{ gpu :true }}; 200const shared = { cpu :{ cores :16 }, ram_gb :128 , gpus :[{ index :0 , vram_gb :82 , uma :true , name :'AMD Radeon 8060S' }]}; 201const apu = resourceSettings ( original , shared , 'work' ); 202assert . equal ( apu . vram_reserve_mib , 2048 , 'the display floor with pictures, not 20% of 82 GB (16 GB)' ); 203const discrete = resourceSettings ( original ,{ cpu :{ cores :16 }, ram_gb :128 , gpus :[{ index :0 , vram_gb :82 , name :'a card' }]}, 'work' ); 204assert . equal ( discrete . vram_reserve_mib , Math . floor ( 82 * 1024 * .2 ), 'a card with its own memory keeps its fifth' ); 205}); 206 207test ( 'picture readiness: Qwen needs strata-vision in the engine folder' ,() => { 208const catalog = { families :[ 209{ family :'flash-next' , images :true , sizes :[{ id :'q' , images :true }]}, 210{ family :'text' , images :false , sizes :[{ id :'t' , images :false }]}]}; 211const without = markPictures ( catalog , false ). families , withReader = markPictures ( catalog , true ). families ; 212assert . deepEqual ( without . map ( f => f . sizes [ 0 ]. images ),[ false , false ]); 213assert . deepEqual ( without . map ( f => f . sizes [ 0 ]. imagesCapable ),[ true , false ]); 214assert . deepEqual ( without . map ( f => f . images ),[ false , false ]); 215assert . deepEqual ( withReader . map ( f => f . sizes [ 0 ]. images ),[ true , false ]); 216assert . equal ( markPictures ( null , true ), null ); 217});