okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
081edf2d9
main
1/** 2* Several requests at once in the installed app: the default, how it is sized, what a person's own number does, and the 3* retry with one when a load with several slots fails. Nothing here starts an engine; Control is a fake that records what 4* the host saved and asked it to start. 5*/ 6import './helpers/isolate-profile.mjs' ; // a test run never writes the owner's real Strata profile 7import test from 'node:test' ; 8import assert from 'node:assert/strict' ; 9import { mkdtempSync , mkdirSync , readFileSync , rmSync , writeFileSync } from 'node:fs' ; 10import { join , basename } from 'node:path' ; 11import { tmpdir } from 'node:os' ; 12import { createNativeControl } from '../server/native-control.mjs' ; 13import { chooseParallel , applyParallel , withoutParallel , slotGb , DEFAULT_SLOTS , PARALLEL_POLICY , BATCH_MTP_POLICY , FALLBACK_NOTE } from '../server/native-parallel.mjs' ; 14import { buildProfile } from '../server/agent/profile.mjs' ; 15 16const hw = ( gb , n = 1 ) => ({ gpus :Array . from ({ length :n }, ( _ , i ) => ({ index :i , vram_gb :gb })) }); 17const cfg = ( context , extra = {}) => ({ args :[ '--max-context' , String ( context ), '--mtp' , '/m' , '--spec' , '4' ], gpu :0 , ...extra }); 18 19test ( 'the default aims for 4 request slots; the policy is one switch; with no hardware read it is one at a time' , () => { 20assert . equal ( DEFAULT_SLOTS , 4 ); 21assert . ok ([ 'fit' , 'fixed' ]. includes ( PARALLEL_POLICY )); 22const unknown = chooseParallel ({ config :cfg ( 32768 ) }); 23assert . equal ( unknown . slots , PARALLEL_POLICY === 'fixed' ?4 :1 , 'nothing to size against: the safe default' ); 24}); 25 26test ( 'sizing: the slots may take at most a fifth of the expert cache the card would have; said in plain words' , () => { 27// measured on the 0.1.45 engine: 0.92 GiB per slot at 32K; a 12 GB card ends with 5.1 GiB of expert cache 28assert . ok ( Math . abs ( slotGb ({ context :32768 }) * 0.931 - 0.92 ) < 0.03 ); 29if ( PARALLEL_POLICY !== 'fit' ) return ; 30const slots = ( gb , context , extra = {}, cards = 1 ) => chooseParallel ({ config :cfg ( context , extra ), hardware :hw ( gb , cards ) }); 31assert . deepEqual ([ 12 , 16 , 20 , 24 , 32 ]. map ( gb => slots ( gb , 32768 ). slots ), [ 1 , 1 , 2 , 3 , 4 ]); 32assert . deepEqual ([ 12 , 24 , 32 , 48 ]. map ( gb => slots ( gb , 65536 ). slots ), [ 1 , 1 , 2 , 4 ], 'the context is per slot: doubling it halves the slots' ); 33assert . equal ( slots ( 24 , 65536 , { gpu :[ 0 , 1 ] }, 2 ). slots , 4 , 'two cards share the load' ); 34const small = slots ( 12 , 32768 ); 35assert . equal ( small . slots , 1 ); assert . match ( small . note , / one request at a time / ); assert . match ( small . note , / Helper agents and a second chat take their turn / ); 36const some = slots ( 20 , 32768 ); 37assert . match ( some . note , / room for 2 requests at once / ); assert . match ( some . note , / smaller context size leaves room for more / ); 38assert . equal ( slots ( 32 , 32768 ). note , null , 'no note when the default fits' ); 39assert . doesNotMatch ( small . note + some . note , / -- | batch | VRAM | GiB | tok | cache / i , 'no engine words' ); 40}); 41 42test ( 'batch-mtp is automatic on big single cards only: 24 GB and up with room for the drafters, never on small cards or a layer split; Expert mode overrides either way' , () => { 43assert . equal ( BATCH_MTP_POLICY , 'auto' ); 44const mtp = ( gb , extra = {}, cards = 1 , values = {}, context = 32768 ) => chooseParallel ({ config :cfg ( context , extra ), hardware :hw ( gb , cards ), values}); 45assert . deepEqual ([ 12 , 16 , 20 ]. map ( gb => mtp ( gb ). batchMtp ), [ false , false , false ], 'small cards: it did not pay (3060: -6% for one chat, 400 MiB less cache)' ); 46assert . deepEqual ([ 24 , 32 , 48 , 72 ]. map ( gb => mtp ( gb ). batchMtp ), [ true , true , true , true ]); 47assert . equal ( mtp ( 72 ). slots , 4 ); 48assert . equal ( mtp ( 24 , {}, 1 , {}, 131072 ). batchMtp , false , 'one slot only at this context: nothing to batch' ); 49assert . equal ( mtp ( 72 , { gpu :[ 0 , 1 ] }, 2 ). batchMtp , false , 'a layer split: -22% with all experts in VRAM' ); 50assert . equal ( chooseParallel ({ config :{ args :[ '--max-context' , '32768' ], gpu :0 }, hardware :hw ( 72 ) }). batchMtp , false , 'no draft layer, nothing to batch' ); 51assert . equal ( chooseParallel ({ config :cfg ( 32768 , { args :[ '--max-context' , '32768' , '--mtp' , '/m' , '--batch-mtp' ] }), hardware :hw ( 72 ) }). batchMtp , false , 'already there: not added twice' ); 52const applied = applyParallel ( cfg ( 32768 ), mtp ( 72 )); 53assert . ok ( applied . cfg . args . includes ( '--batch-mtp' )); assert . equal ( applied . cfg . parallel , 4 ); 54// Expert mode: off wins on a big card; on wins on a small one-card setup; an explicit slot count can still ask for it 55assert . equal ( mtp ( 72 , {}, 1 , { batchMtp :false }). batchMtp , false ); 56assert . equal ( mtp ( 72 , { batchMtp :false }). batchMtp , false ); 57assert . equal ( mtp ( 20 , {}, 1 , { batchMtp :true }). batchMtp , true , '20 GB: 2 slots, asked for by hand' ); 58assert . equal ( mtp ( 72 , { gpu :[ 0 , 1 ] }, 2 , { batchMtp :true }). batchMtp , false , 'never forced on a layer split' ); 59const manual = chooseParallel ({ config :cfg ( 32768 ), values :{ parallel :2 , batchMtp :true }, hardware :hw ( 12 ) }); 60assert . deepEqual ([ manual . slots , manual . explicit , manual . batchMtp ], [ 2 , true , true ]); 61assert . ok ( applyParallel ( cfg ( 32768 ), manual ). cfg . args . includes ( '--batch-mtp' )); 62assert . ok ( ! applyParallel ( cfg ( 32768 , { parallel :2 }), chooseParallel ({ config :cfg ( 32768 , { parallel :2 }), hardware :hw ( 72 ) })). cfg . args . includes ( '--batch-mtp' ), 'a number the person chose alone does not add it' ); 63// the retry with one request at a time takes it away again 64assert . ok ( ! withoutParallel ( applied . cfg ). args . includes ( '--batch-mtp' )); 65}); 66 67test ( "a number the person chose is kept exactly: Expert mode's value, the saved settings, an explicit --batch" , () => { 68assert . deepEqual ( applyParallel ( cfg ( 32768 , { parallel :1 }), chooseParallel ({ config :cfg ( 32768 , { parallel :1 }), hardware :hw ( 24 ) })). cfg . parallel , 1 ); 69const six = chooseParallel ({ config :cfg ( 32768 ), values :{ parallel :6 }, hardware :hw ( 8 ) }); 70assert . deepEqual ([ six . slots , six . explicit , six . batchMtp ], [ 6 , true , false ], 'recommend, never force: 6 on an 8 GB card is allowed' ); 71const one = chooseParallel ({ config :cfg ( 32768 ), values :{ parallel :1 }, hardware :hw ( 24 ) }); 72assert . deepEqual ([ one . slots , one . explicit ], [ 1 , true ]); 73assert . equal ( chooseParallel ({ config :cfg ( 32768 , { args :[ '--max-context' , '32768' , '--batch' , '3' ] }), hardware :hw ( 24 ) }). explicit , true ); 74const saved = cfg ( 32768 , { parallel :3 }); 75assert . equal ( applyParallel ( saved , chooseParallel ({ config :saved })). cfg , saved , 'untouched' ); 76}); 77 78test ( 'the retry configuration has no slots and no batch-mtp' , () => { 79const next = withoutParallel ({ ...cfg ( 32768 ), parallel :4 , args :[ ...cfg ( 32768 ). args , '--batch-mtp' ] }); 80assert . equal ( next . parallel , undefined ); assert . ok ( ! next . args . includes ( '--batch-mtp' )); 81}); 82 83test ( 'helper agents: at least as many as the engine slots, and extra ones wait for their turn instead of being refused' , () => { 84const profile = slots => buildProfile ({ local :[{ id :'a' , baseUrl :'http://127.0.0.1:1/v1' , modelId :'m' , slots}] }); 85const cap = slots => JSON . parse ( profile ( slots ). patch . split ( '\n' ). slice ( 1 ). join ( '\n' )). find ( e => e . id === 'subagent' ). config . maxActiveSubagents ; 86assert . deepEqual ([ cap ( 1 ), cap ( 2 ), cap ( 4 ), cap ( 6 )], [ 4 , 4 , 4 , 6 ]); 87assert . notEqual ( profile ( 6 ). signature , profile ( 2 ). signature , 'a changed helper limit restarts the agent when idle' ); 88}); 89 90// ---------------------------------------------------------------------------------------- the installed app's load 91 92function fixture ({ failStarts= 0 , config= {} } = {}) { 93const root = mkdtempSync ( join ( tmpdir (), 'strata-par-test-' )); 94const install = join ( root , 'install' ), data = join ( root , 'data' ); 95mkdirSync ( install , { recursive :true }); 96process . env . STRATA_INSTALL_DIR = install ; // the app's own install folder: the host always names it to Control 97const path = join ( install , 'strata-iq3_s.json' ); 98writeFileSync ( path , JSON . stringify ({ exe :'component:strata-engine' , tokenizer :join ( install , 'tok' ), args :[ '--max-context' , '32768' , '--mtp' , '/m' , '--spec' , '4' ], model_name :'Qwen iq3_s' , port :8080 , ...config })); 99const saved = []; const calls = []; 100let starts = 0 ; 101const control = { 102control :{ dir :join ( root , 'control' ) }, version :'0.1.45' , 103async installedModels () { return { install_dir :install , models :[{ id :'iq3_s' , name :'Qwen iq3_s' , source_config :path , component_status :'present' , context_tokens :32768 }] }; }, 104async engine () { return { usable :true , backend :'cuda' , components_dir :'E:\\c' , engine :{ version :'0.1.45' , platform :'windows-x86_64' , backend :'cuda' , exe :'E:\\e\\strata.exe' , root :'E:\\e' } }; }, 105async saveProfile ( name , file ) { saved . push ( JSON . parse ( readFileSync ( file , 'utf8' ))); calls . push ([ 'saveProfile' , name , basename ( file )]); return {}; }, 106async start ( name ) { calls . push ([ 'start' , name ]); starts ++ ; if ( starts <= failStarts ) throw new Error ( 'server_exited: out of memory' ); return { ok :true , state :'ready' }; }, 107async stop ( name ) { calls . push ([ 'stop' , name ]); return { ok :true }; }, 108}; 109const fetcher = async () => Response . json ({ status :'ok' }); 110const native = createNativeControl ({ control, hardwareProbe :() => [{ index :0 , vram_gb :32 }], directory :data , spawnProcess :() => { throw new Error ( 'the host starts no process' ); }, fetcher, requests :() => [] }); 111return { native, saved, calls, cleanup :() => rmSync ( root , { recursive :true , force :true }) }; 112} 113const finish = async ( native , id ) => { for ( let i = 0 ; i < 500 ; i ++ ) { const job = native . state (). jobs . find ( j => j . id === id ); if ( job . state !== 'running' ) return job ; await new Promise ( r => setTimeout ( r , 10 )); } throw new Error ( 'job did not finish' ); }; 114 115test ( 'the installed app loads with 4 request slots by default' , async () => { 116const f = fixture (); 117try { 118await f . native . refresh ( true ); 119const job = await finish ( f . native , f . native . operation ( 'load' , { model :'iq3_s' }). id ); 120assert . equal ( job . state , 'done' , job . error ); 121assert . equal ( f . saved . length , 1 ); 122assert . equal ( f . saved [ 0 ]. parallel , 4 ); 123assert . ok ( f . saved [ 0 ]. args . includes ( '--batch-mtp' ), 'a 32 GB card: MTP per request is on automatically' ); 124assert . equal ( f . native . state (). instances [ 0 ]. parallel , 4 , 'the agent reads the slot count from here' ); 125} finally { await f . native . close (); f . cleanup (); } 126}); 127 128test ( "the person's own number wins: saved settings and Expert mode's value" , async () => { 129for ( const [ config , values , expected ] of [[{ parallel :1 }, {}, 1 ], [{}, { parallel :3 }, 3 ], [{ parallel :2 }, { parallel :6 }, 6 ]]) { 130const f = fixture ({ config}); 131try { 132await f . native . refresh ( true ); 133const job = await finish ( f . native , f . native . operation ( 'load' , { model :'iq3_s' , values}). id ); 134assert . equal ( job . state , 'done' , job . error ); 135assert . equal ( f . saved [ 0 ]. parallel , expected ); 136assert . ok ( ! f . saved [ 0 ]. args . includes ( '--batch-mtp' )); 137} finally { await f . native . close (); f . cleanup (); } 138} 139}); 140 141test ( 'a load that fails with several slots is tried again with one, and the person is told in plain words' , async () => { 142const f = fixture ({ failStarts :1 }); 143try { 144await f . native . refresh ( true ); 145const job = await finish ( f . native , f . native . operation ( 'load' , { model :'iq3_s' }). id ); 146assert . equal ( job . state , 'done' , job . error ); 147assert . equal ( f . saved . length , 2 ); 148assert . equal ( f . saved [ 0 ]. parallel , 4 ); assert . equal ( f . saved [ 1 ]. parallel , undefined ); 149assert . ok ( ! f . saved [ 1 ]. args . includes ( '--batch-mtp' )); 150assert . match ( job . result . summary , / one request at a time / ); 151assert . equal ( job . result . summary . includes ( FALLBACK_NOTE ), true ); 152assert . equal ( f . native . state (). instances [ 0 ]. parallel , 1 ); 153} finally { await f . native . close (); f . cleanup (); } 154}); 155 156test ( 'a load that fails even with one slot reports the failure, and a chosen number is never silently changed' , async () => { 157const both = fixture ({ failStarts :2 }); 158try { 159await both . native . refresh ( true ); 160const job = await finish ( both . native , both . native . operation ( 'load' , { model :'iq3_s' }). id ); 161assert . equal ( job . state , 'error' ); 162assert . equal ( both . saved . length , 2 ); 163} finally { await both . native . close (); both . cleanup (); } 164const chosen = fixture ({ failStarts :1 , config :{ parallel :3 } }); 165try { 166await chosen . native . refresh ( true ); 167const job = await finish ( chosen . native , chosen . native . operation ( 'load' , { model :'iq3_s' }). id ); 168assert . equal ( job . state , 'error' , 'the person asked for 3: no quiet fallback' ); 169assert . equal ( chosen . saved . length , 1 ); 170} finally { await chosen . native . close (); chosen . cleanup (); } 171}); 172 173test ( 'the draft layer (MTP) is part of what the card is sized for; switched off, its 1 GB goes to the slots' , () => { 174if ( PARALLEL_POLICY !== 'fit' ) return ; 175const slots = ( gb , extra = {}, args ) => chooseParallel ({ config :{ ...cfg ( 32768 , extra ), ...( args ?{ args} :{}) }, hardware :hw ( gb ) }). slots ; 176assert . equal ( slots ( 26.5 ), 3 , 'MTP on (the default): the measured sizing stands' ); 177assert . equal ( slots ( 26.5 , { mtp :false }), 4 , 'a card with the draft layer off has 1 GB more cache' ); 178assert . equal ( slots ( 26.5 , {}, [ '--max-context' , '32768' , '--spec' , '4' ]), 4 , 'no draft layer in the arguments' ); 179assert . equal ( slots ( 12 , { mtp :false }), 1 , 'a 12 GB card still gets one request at a time' ); 180}); 181 182test ( 'DFlash2 chosen on purpose means one request at a time unless the person picked a number (a number they picked is kept and DFlash2 serves that many)' , () => { 183const big = hw ( 72 ); 184const auto = chooseParallel ({ config :cfg ( 32768 ), hardware :big }); 185assert . ok ( auto . slots > 1 , 'a card with room gets slots by default' ); 186const chosen = chooseParallel ({ config :cfg ( 32768 ), values :{ drafting :'dflash2' }, hardware :big }); 187assert . equal ( chosen . slots , 1 ); 188assert . equal ( chosen . explicit , false ); 189assert . match ( chosen . note , / DFlash2 answers one request at a time / ); 190assert . equal ( chooseParallel ({ config :cfg ( 32768 , { drafting :'dflash2' }), hardware :big }). slots , 1 , 'a saved choice counts' ); 191assert . equal ( chooseParallel ({ config :cfg ( 32768 ), values :{ drafting :'mtp' }, hardware :big }). slots , auto . slots , 'MTP and Auto keep the slots' ); 192assert . equal ( chooseParallel ({ config :cfg ( 32768 ), values :{ drafting :'dflash2' , parallel :3 }, hardware :big }). slots , 3 , 'a number the person chose is theirs' ); 193assert . equal ( applyParallel ( cfg ( 32768 , { parallel :4 }), chosen ). cfg . parallel , undefined , 'and the saved config carries no slots' ); 194}); 195 196test ( 'DFlash2 named with a number of requests the person chose: that many, no note, the number stays in the run config (owner 2026-11-03)' , () => { 197const big = hw ( 72 ); 198for ( const n of [ 2 , 4 , 8 ]) { 199const viaValues = chooseParallel ({ config :cfg ( 32768 ), values :{ drafting :'dflash2' , parallel :n }, hardware :big }); 200assert . deepEqual ([ viaValues . slots , viaValues . explicit , viaValues . note ], [ n , true , null ], `values ${ n } ` ); 201const viaConfig = chooseParallel ({ config :cfg ( 32768 , { drafting :'dflash2' , parallel :n }), hardware :big }); 202assert . deepEqual ([ viaConfig . slots , viaConfig . explicit , viaConfig . note ], [ n , true , null ], `saved ${ n } ` ); 203const applied = applyParallel ( cfg ( 32768 , { drafting :'dflash2' , parallel :n }), viaConfig ); 204assert . equal ( applied . cfg . parallel , n , 'the number stays' ); 205assert . equal ( applied . note , null ); 206assert . ok ( ! applied . cfg . args . includes ( '--batch-mtp' ), 'no wish, no --batch-mtp' ); 207} 208// the same through the resolved drafter the load passes, and a --batch in the engine arguments 209assert . equal ( chooseParallel ({ config :cfg ( 32768 , { drafting :'dflash2' , parallel :4 }), drafter :'dflash2' , hardware :big }). slots , 4 ); 210const withBatch = chooseParallel ({ config :{ ...cfg ( 32768 , { drafting :'dflash2' }), args :[ '--max-context' , '32768' , '--mtp' , '/m' , '--batch' , '3' ] }, drafter :'dflash2' , hardware :big }); 211assert . deepEqual ([ withBatch . explicit , withBatch . note ], [ true , null ]); 212// Expert mode's batch-mtp wish is passed on exactly as in 0.2.10 for every drafter: the server drops --batch-mtp when DFlash2 runs and keeps it when the MTP runs (Metal) 213const wish = ( extra , drafter ) => chooseParallel ({ config :cfg ( 32768 ), values :{ parallel :4 , batchMtp :true , ...extra }, drafter, hardware :big }). batchMtp ; 214assert . equal ( wish ({ drafting :'dflash2' }, 'dflash2' ), true ); 215assert . equal ( wish ({ drafting :'mtp' }, 'mtp' ), true ); 216assert . equal ( wish ({}, 'dflash2' ), true , 'Auto that resolved to DFlash2 is not named: the MTP serves the several requests' ); 217assert . equal ( wish ({ drafting :'dflash2' }, 'mtp' ), true , 'named, but the drafter is not here yet: the MTP runs and keeps its slot switch' ); 218// one request chosen is one request, and nothing chosen under DFlash2 is still one with the note 219assert . equal ( chooseParallel ({ config :cfg ( 32768 ), values :{ drafting :'dflash2' , parallel :1 }, hardware :big }). slots , 1 ); 220assert . match ( chooseParallel ({ config :cfg ( 32768 ), values :{ drafting :'dflash2' }, hardware :big }). note , / DFlash2 answers one request at a time / ); 221});