okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0c165b48b
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { readFileSync , writeFileSync , existsSync } from 'node:fs' ; 4import { join } from 'node:path' ; 5import { nativeFixture , finishJob } from './helpers/native-fixture.mjs' ; 6import { resultSentence } from '../server/native-autotune.mjs' ; 7 8const arg = ( args , name ) => { const i = args . indexOf ( name ); return i < 0 ?undefined :args [ i + 1 ]; }; 9 10/** A pretend `strata` command: records what it was asked, writes status-file steps, then prints its JSON. */ 11function handler ( log , { result, hold= false , delayMs= 60 } = {}) { 12return ( child , args ) => { 13log . push ( args . slice ( args . findIndex ( a => String ( a ). endsWith ( 'strata.py' )) + 1 )); 14const cmd = log . at ( - 1 ); 15const out = json => { child . stdout . emit ( 'data' , Buffer . from ( JSON . stringify ( json ))); child . exitCode = 0 ; child . emit ( 'exit' , 0 ); child . emit ( 'close' , 0 ); }; 16if ( cmd [ 0 ] === 'presets' && cmd [ 1 ] === 'status' ) return out ({ ok :true , active :null , suggested_name :'IQ2_XS 32K – Full speed' , presets :[], state :null , reasons :[] }); 17if ( cmd [ 0 ] === 'presets' && cmd [ 1 ] === 'list' ) return out ({ ok :true , presets :[{ id :'a' . repeat ( 32 ), name :'x' }] }); 18if ( cmd [ 0 ] === 'presets' && cmd [ 1 ] === 'effective' ) return out ({ ok :true , applied :true , preset :{ id :'a' . repeat ( 32 ), name :'IQ2_XS 32K – Work' }, args :[ '--kv-resident' , '65536' ], changed :[{ flag :'--kv-resident' , was :'32768' , now :'65536' }] }); 19if ( cmd [ 0 ] === 'presets' ) return out ({ ok :true , done :cmd [ 1 ] }); 20if ( cmd [ 0 ] === 'tune' ) { 21const status = arg ( cmd , '--status-file' ); 22writeFileSync ( status , JSON . stringify ({ state :'running' , stage :'rounds' , text :'Finding the fastest settings for your PC… about 9 min left' , minutes_left :9 , runs_done :3 , runs_planned :9 , elapsed_s :400 , message :'Trying: host core' })); 23if ( hold ) return ; // stays running until killed 24setTimeout (() => out ({ ok :true , result}), delayMs ); 25} 26}; 27} 28 29const improved = { outcome :'improved' , gain_pct :11.6 , seconds :900 , preset :{ id :'b' . repeat ( 32 ), name :'IQ2_XS 32K – Full speed' }, answers :{ class :'exact' , fields :[] }, settings :{ args :{ '--host-core' :'last' }, env :{}, per_request :{} } }; 30 31test ( 'the result sentence is plain words with the gain and the saved name' , () => { 32assert . equal ( resultSentence ( improved ), "+12% faster answers, saved as 'IQ2_XS 32K – Full speed'" ); 33assert . match ( resultSentence ({ outcome :'default_confirmed' , preset :{ name :'X' } }), / already the fastest / ); 34assert . equal ( resultSentence ({ outcome :'inconclusive' , message :'The PC was too noisy.' }), 'The PC was too noisy.' ); 35}); 36 37test ( 'a tune is started on the config a load would use, unloads nothing it did not have, follows progress and reports the saved name' , async () => { 38const log = []; 39const f = nativeFixture ({ tuneHandler :handler ( log , { result :improved , delayMs :2500 }) }); 40try { 41await f . control . refresh ( true ); 42const job = f . control . operation ( 'autotune' , { model :'iq2_xs' , mode :'quick' , use :'chat' , resourceProfile :'work' }); 43let seen = null ; 44for ( let n = 0 ; n < 200 && ! seen ; n ++ ) { await new Promise ( r => setTimeout ( r , 30 )); const j = f . control . state (). jobs . find ( x => x . id === job . id ); if ( j . progress ) seen = j . progress ; } 45assert . ok ( seen , 'progress from the status file reaches the job' ); 46assert . equal ( seen . minutesLeft , 9 ); 47const done = await finishJob ( f . control , job . id ); 48assert . equal ( done . state , 'done' , done . error ); 49assert . equal ( done . result . summary , "+12% faster answers, saved as 'IQ2_XS 32K – Full speed'" ); 50const cmd = log . find ( c => c [ 0 ] === 'tune' ); 51assert . equal ( cmd [ 1 ], 'auto' ); 52assert . equal ( arg ( cmd , '--mode' ), 'quick' ); 53assert . equal ( arg ( cmd , '--goal' ), 'room' ); // "Leave 20% for my work" 54assert . equal ( arg ( cmd , '--leave-ram' ), '20' ); 55assert . ok ( ! cmd . includes ( '--allow-small-differences' )); 56const cfg = JSON . parse ( readFileSync ( cmd [ 2 ], 'utf8' )); // the config the tuner measures is the one a load would start 57assert . equal ( cfg . ui_resource_profile , 'work' ); 58assert . ok ( Array . isArray ( cfg . args ) && cfg . args . includes ( '--max-context' )); 59assert . equal ( cfg . api_key , undefined ); 60assert . ok ( ! existsSync ( arg ( cmd , '--status-file' )), 'the status file is removed afterwards' ); 61} finally { await f . cleanup (); } 62}); 63 64test ( 'full performance asks for speed with no memory left over, and the tick for small differences is only passed when ticked' , async () => { 65const log = []; 66const f = nativeFixture ({ tuneHandler :handler ( log , { result :improved }) }); 67try { 68await f . control . refresh ( true ); 69const done = await finishJob ( f . control , f . control . operation ( 'autotune' , { model :'iq3_s' , mode :'thorough' , resourceProfile :'performance' , allowSmallDifferences :true }). id ); 70assert . equal ( done . state , 'done' , done . error ); 71const cmd = log . find ( c => c [ 0 ] === 'tune' ); 72assert . equal ( arg ( cmd , '--goal' ), 'speed' ); 73assert . equal ( cmd . includes ( '--leave-ram' ), false ); 74assert . equal ( arg ( cmd , '--mode' ), 'thorough' ); 75assert . ok ( cmd . includes ( '--allow-small-differences' )); 76} finally { await f . cleanup (); } 77}); 78 79test ( 'Stop ends the tune and keeps your settings; only one operation at a time' , async () => { 80const log = []; 81const f = nativeFixture ({ tuneHandler :handler ( log , { hold :true }) }); 82try { 83await f . control . refresh ( true ); 84const job = f . control . operation ( 'autotune' , { model :'iq2_xs' }); 85await new Promise ( r => setTimeout ( r , 80 )); 86assert . throws (() => f . control . operation ( 'autotune' , { model :'iq3_s' }), / current engine operation / ); 87await f . control . cancel ( job . id ); 88const j = f . control . state (). jobs . find ( x => x . id === job . id ); 89assert . equal ( j . state , 'cancelled' ); 90assert . equal ( j . result , null ); 91} finally { await f . cleanup (); } 92}); 93 94test ( 'a tune is refused while a model is generating, and loaded models are unloaded first' , async () => { 95const log = []; 96const f = nativeFixture ({ tuneHandler :handler ( log , { result :improved }) }); 97try { 98await f . control . refresh ( true ); 99const loaded = await finishJob ( f . control , f . control . operation ( 'load' , { model :'iq2_xs' }). id ); 100assert . equal ( loaded . state , 'done' , loaded . error ); 101f . requests . push ({ instanceId :loaded . result . instanceId , state :'generating' }); 102const refused = await finishJob ( f . control , f . control . operation ( 'autotune' , { model :'iq2_xs' }). id ); 103assert . equal ( refused . state , 'error' ); 104assert . match ( refused . error , / Finish requests / ); 105f . requests . length = 0 ; 106const done = await finishJob ( f . control , f . control . operation ( 'autotune' , { model :'iq2_xs' }). id ); 107assert . equal ( done . state , 'done' , done . error ); 108assert . equal ( f . control . state (). instances . length , 0 , 'the model was unloaded so the tuner has the graphics card' ); 109} finally { await f . cleanup (); } 110}); 111 112test ( 'saved tunings: status for the choices, list, and actions go to Strata Control by id' , async () => { 113const log = []; 114const f = nativeFixture ({ tuneHandler :handler ( log , {}) }); 115try { 116await f . control . refresh ( true ); 117const st = await f . control . tuneStatus ({ model :'iq2_xs' , resourceProfile :'performance' }); 118assert . equal ( st . suggested_name , 'IQ2_XS 32K – Full speed' ); 119const listed = await f . control . tunePresets . list (); 120assert . equal ( listed . presets . length , 1 ); 121await f . control . tunePresets . action ( 'rename' , 'a' . repeat ( 32 ), { name :'My fast one' }); 122assert . deepEqual ( log . at ( - 1 ). slice ( 0 , 4 ), [ 'presets' , 'rename' , 'a' . repeat ( 32 ), 'My fast one' ]); 123await assert . rejects ( f . control . tunePresets . action ( 'apply' , '../x' ), / Choose a saved tuning / ); 124await assert . rejects ( f . control . tunePresets . action ( 'format' , 'a' . repeat ( 32 )), / supported action / ); 125} finally { await f . cleanup (); } 126}); 127 128test ( 'a model that is not prepared cannot be tuned' , async () => { 129const f = nativeFixture ({ tuneHandler :handler ([], {}) }); 130try { 131await f . control . refresh ( true ); 132f . installed . models [ 0 ]. ready = false ; 133await f . control . refresh ( true ); 134const job = await finishJob ( f . control , f . control . operation ( 'autotune' , { model :'iq2_xs' }). id ); 135assert . equal ( job . state , 'error' ); 136assert . match ( job . error , / Prepare the model / ); 137} finally { await f . cleanup (); } 138}); 139 140test ( 'Expert mode can read the tuned values and the tuning can be switched off for these choices' , async () => { 141const log = []; 142const f = nativeFixture ({ tuneHandler :handler ( log , {}) }); 143try { 144await f . control . refresh ( true ); 145const eff = await f . control . tuneEffective ({ model :'iq2_xs' , resourceProfile :'work' }); 146assert . equal ( eff . applied , true ); 147assert . deepEqual ( eff . changed , [{ flag :'--kv-resident' , was :'32768' , now :'65536' }]); 148const cmd = log . find ( c => c [ 1 ] === 'effective' ); 149const cfg = JSON . parse ( readFileSync ( cmd [ 2 ], 'utf8' )); 150assert . equal ( cfg . ui_resource_profile , 'work' ); // the choices a load would make, so the right tuning is found 151await f . control . tuneOff ({ model :'iq2_xs' , resourceProfile :'work' }); 152assert . deepEqual ( log . at ( - 1 ). slice ( 0 , 2 ), [ 'presets' , 'off' ]); 153} finally { await f . cleanup (); } 154}); 155 156test ( 'a busy PC is a plain result, not an error' , () => { 157assert . match ( resultSentence ({ outcome :'inconclusive' , code :'busy_pc' , message :'x' }), / PC was busy / ); 158});