okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
061a28583
main
1/** 2* The person's own engine settings survive a load (#3215 #3270 #3216): a --vram-reserve-mib of 0, the Expert setting "GPU memory for other 3* apps", and engine arguments the app does not know (the control vector options). Nothing here starts an engine; Control is a fake that 4* records the run file 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 } from 'node:path' ; 11import { tmpdir } from 'node:os' ; 12import { createNativeControl } from '../server/native-control.mjs' ; 13import { resourceSettings , explicitReserve } from '../server/native-resources.mjs' ; 14import { capture , merge , groups } from '../server/native-user-args.mjs' ; 15 16const ARGS = [ '--pack' , '/p' , '--expert-cache' , 'auto' , '--spec' , '4' , '--max-context' , '40000' , '--kv' , 'int8' ]; 17const CVEC = [ '--control-vector-scaled' , 'C:/vectors/mine.gguf:1.0' , '--control-vector-layer-range' , '4' , '44' , '--cvec-mode' , 'project' , '--cvec-dir' , 'per-layer' ]; 18 19function fixture ( config = {}, { gpus= [{ index :0 , vram_gb :12 }] } = {}) { 20const root = mkdtempSync ( join ( tmpdir (), 'strata-userargs-test-' )); 21const install = join ( root , 'install' ), data = join ( root , 'data' ); 22mkdirSync ( install , { recursive :true }); 23process . env . STRATA_INSTALL_DIR = install ; 24const path = join ( install , 'strata-q2_0.json' ); 25writeFileSync ( path , JSON . stringify ({ exe :'component:strata-engine' , tokenizer :join ( install , 'tok' ), args :ARGS , model_name :'Qwen q2_0' , port :8080 , ...config })); 26const saved = []; 27const control = { 28control :{ dir :join ( root , 'control' ) }, version :'0.2.0' , 29async installedModels () { return { install_dir :install , models :[{ id :'q2_0' , name :'Qwen q2_0' , source_config :path , component_status :'present' , context_tokens :40000 }] }; }, 30async engine () { return { usable :true , backend :'cuda' , components_dir :'E:\\c' , engine :{ version :'0.2.0' , platform :'windows-x86_64' , backend :'cuda' , exe :'E:\\e\\strata.exe' , root :'E:\\e' } }; }, 31async saveProfile ( name , file ) { saved . push ( JSON . parse ( readFileSync ( file , 'utf8' ))); return {}; }, 32async start () { return { ok :true , state :'ready' }; }, 33async stop () { return { ok :true }; }, 34}; 35const native = createNativeControl ({ control, hardwareProbe :() => gpus , directory :data , spawnProcess :() => { throw new Error ( 'no process' ); }, fetcher :async () => Response . json ({ status :'ok' }), requests :() => [] }); 36return { native, saved, path, read :() => JSON . parse ( readFileSync ( path , 'utf8' )), write :c => writeFileSync ( path , JSON . stringify ( c )), cleanup :() => rmSync ( root , { recursive :true , force :true }) }; 37} 38const 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' ); }; 39const reserveOf = args => args [ args . indexOf ( '--vram-reserve-mib' ) + 1 ]; 40const load = async ( f , args = {}) => { const job = await finish ( f . native , f . native . operation ( 'load' , { model :'q2_0' , vision :'no' , ...args }). id ); assert . equal ( job . state , 'done' , job . error ); return f . saved . at ( - 1 ); }; 41 42test ( 'the rule: groups, merge and capture' , () => { 43assert . deepEqual ( groups ([ '--a' , '1' , '--b' , '--c' , 'x' , 'y' ]), [[ '--a' , '1' ], [ '--b' ], [ '--c' , 'x' , 'y' ]]); 44assert . deepEqual ( merge ([ '--a' , '1' , '--b' , '2' ], [ '--a' , '9' ]), [ '--b' , '2' , '--a' , '9' ]); 45const written = [ ...ARGS , '--vram-reserve-mib' , '1536' ]; 46assert . deepEqual ( capture ({ args :[ ...written , ...CVEC ], args_written :written }), CVEC ); 47assert . deepEqual ( capture ({ args :written , args_written :written }), []); 48assert . deepEqual ( capture ({ args :written . map ( a => ( a === '1536' ?'0' :a )), args_written :written }), [ '--vram-reserve-mib' , '0' ]); 49assert . deepEqual ( capture ({ args :[ '--max-context' , '1' ], args_written :[ '--max-context' , '2' ] }), [], 'the context is the app\'s own choice' ); 50}); 51 52test ( 'explicitReserve: any number the person set counts (0 too); the engine default, the app\'s own value and beta leftovers do not' , () => { 53assert . equal ( explicitReserve ({ args :[ '--vram-reserve-mib' , '0' ] }), 0 ); 54assert . equal ( explicitReserve ({ args :[], vram_reserve_mib :0 }), 0 ); 55assert . equal ( explicitReserve ({ args :[], user_args :[ '--vram-reserve-mib' , '256' ], vram_reserve_mib :3000 }), 256 ); 56assert . equal ( explicitReserve ({ args :[], vram_reserve_mib :3000 }, { vram_reserve_mib :128 }), 128 , 'the Expert setting of this load' ); 57assert . equal ( explicitReserve ({ args :[ '--vram-reserve-mib' , '700' ] }), null , 'setup writes 700 for pictures: not a choice' ); 58assert . equal ( explicitReserve ({ args :[] }), null ); 59// what the app wrote last is not the person's: the value and the note that it was the app's 60assert . equal ( explicitReserve ({ args :[ '--vram-reserve-mib' , '3276' ], vram_reserve_mib :3276 , ui_reserve_applied :3276 }), null ); 61assert . equal ( explicitReserve ({ args :[ '--vram-reserve-mib' , '1024' ], vram_reserve_mib :1024 , ui_reserve_applied :3276 }), 1024 , 'changed since: the person\'s' ); 62// a 0.2.0-beta file: the app wrote the same number in both places, without a note 63const beta = { args :[ '--vram-reserve-mib' , '1536' ], vram_reserve_mib :1536 , ui_resource_baseline :{ workers :0 , reserve :700 } }; 64assert . equal ( explicitReserve ( beta ), null ); 65assert . equal ( explicitReserve ({ ...beta , args :[ '--vram-reserve-mib' , '0' ] }), 0 , 'a different number in args is the person\'s' ); 66}); 67 68test ( '3215: a reserve of 0 is kept on both resource profiles; the safety default stays where nothing was asked' , () => { 69const display = { cpu :{ cores :12 }, ram_gb :64 , gpus :[{ index :0 , vram_gb :12 , drives_display :true }] }; 70const headless = { cpu :{ cores :12 }, ram_gb :64 , gpus :[{ index :0 , vram_gb :12 , drives_display :false }] }; 71const unknown = { cpu :{ cores :12 }, ram_gb :64 , gpus :[{ index :0 , vram_gb :12 }] }; 72const zero = { gpu :0 , args :[ '--vram-reserve-mib' , '0' ] }; 73for ( const profile of [ 'work' , 'performance' ]) for ( const hw of [ display , headless , unknown ]) { 74const r = resourceSettings ( zero , hw , profile ); 75assert . equal ( reserveOf ( r . args ), '0' , ` ${ profile } ` ); 76assert . equal ( r . vram_reserve_mib , 0 ); 77assert . equal ( r . ui_reserve_applied , undefined , 'the app did not choose it' ); 78} 79assert . equal ( resourceSettings ({ gpu :0 , args :[], vision :{ gpu :true } }, display , 'performance' ). vram_reserve_mib , 2048 , 'a display card with pictures: the safety default' ); 80assert . equal ( resourceSettings ({ gpu :0 , args :[] }, display , 'performance' ). vram_reserve_mib , 1536 ); 81assert . equal ( resourceSettings ({ gpu :0 , args :[] }, unknown , 'performance' ). vram_reserve_mib , 1536 , 'not known to be headless: the safety default' ); 82assert . equal ( resourceSettings ({ gpu :0 , args :[] }, display , 'work' ). vram_reserve_mib , 1536 , 'a fifth of 12 GB (2457) is capped at 1536 on a card of 16 GB or less (0.2.3 tuning)' ); 83// a card that drives no display: 512 MiB (700 with pictures), and "Leave 20% for my work" has no desktop to keep room for 84for ( const profile of [ 'work' , 'performance' ]) { 85assert . equal ( resourceSettings ({ gpu :0 , args :[] }, headless , profile ). vram_reserve_mib , 512 , profile ); 86assert . equal ( resourceSettings ({ gpu :0 , args :[], vision :{ gpu :true } }, headless , profile ). vram_reserve_mib , 700 , ` ${ profile } with pictures` ); 87} 88// two cards, one of them on the desktop 89const mixed = { ...display , gpus :[{ index :0 , vram_gb :12 , drives_display :false }, { index :1 , vram_gb :12 , drives_display :true }] }; 90assert . equal ( resourceSettings ({ gpu :[ 0 , 1 ], args :[] }, mixed , 'performance' ). vram_reserve_mib , 1536 ); 91}); 92 93test ( '3215: the app load keeps --vram-reserve-mib 0 typed into the file, on every load' , async () => { 94const f = fixture ({ args :[ ...ARGS , '--vram-reserve-mib' , '0' ] }); 95try { 96await f . native . refresh ( true ); 97for ( let i = 0 ; i < 3 ; i ++ ) { 98const run = await load ( f , { resourceProfile :i === 1 ?'performance' :'work' }); 99assert . equal ( reserveOf ( run . args ), '0' , `load ${ i + 1 } ` ); 100assert . equal ( run . args . filter ( a => a === '--vram-reserve-mib' ). length , 1 ); 101const file = f . read (); 102assert . equal ( reserveOf ( file . args ), '0' , 'the file keeps it too' ); 103assert . deepEqual ( file . user_args , [ '--vram-reserve-mib' , '0' ]); 104} 105} finally { await f . native . close (); f . cleanup (); } 106}); 107 108test ( '3270: the Expert setting "GPU memory for other apps" is kept across loads, a lowered value is not reset, and clearing it brings the default back' , async () => { 109const f = fixture ({ args :[ ...ARGS , '--vram-reserve-mib' , '2048' ], args_written :[ ...ARGS , '--vram-reserve-mib' , '2048' ], vram_reserve_mib :2048 }); 110try { 111await f . native . refresh ( true ); 112await load ( f , { resourceProfile :'work' }); // the app's own load first: its numbers are in the file now 113f . native . save ( 'q2_0' , { values :{ vram_reserve_mib :1024 } }); 114for ( let i = 0 ; i < 2 ; i ++ ) { 115const run = await load ( f , { resourceProfile :'work' }); 116assert . equal ( reserveOf ( run . args ), '1024' , `load ${ i + 1 } ` ); 117assert . equal ( f . read (). vram_reserve_mib , 1024 ); 118assert . equal ( reserveOf ( f . read (). args ), '1024' ); 119} 120f . native . save ( 'q2_0' , { values :{ vram_reserve_mib :300 } }); 121assert . equal ( reserveOf (( await load ( f , { resourceProfile :'performance' })). args ), '300' ); 122// edited by hand in both places of the file 123const hand = f . read (); hand . vram_reserve_mib = 900 ; hand . args = hand . args . map ( a => ( a === '300' ?'900' :a )); f . write ( hand ); 124assert . equal ( reserveOf (( await load ( f , { resourceProfile :'work' })). args ), '900' ); 125// the value of this load alone 126assert . equal ( reserveOf (( await load ( f , { resourceProfile :'work' , values :{ vram_reserve_mib :128 } })). args ), '128' ); 127f . native . save ( 'q2_0' , { values :{ vram_reserve_mib :null } }); 128const cleared = f . read (); 129assert . equal ( 'vram_reserve_mib' in cleared , false ); 130assert . equal ( cleared . args . includes ( '--vram-reserve-mib' ), false ); 131assert . equal ( cleared . user_args , undefined ); 132assert . equal ( reserveOf (( await load ( f , { resourceProfile :'performance' })). args ), '1536' , 'nothing asked: the safety default' ); 133} finally { await f . native . close (); f . cleanup (); } 134}); 135 136test ( '3270: a 0.2.0-beta file (the app wrote 1536 itself) is not taken for the person\'s choice, but a number they changed is' , async () => { 137const f = fixture ({ args :[ ...ARGS , '--vram-reserve-mib' , '1536' ], vram_reserve_mib :1536 , ui_resource_profile :'work' , ui_resource_baseline :{ workers :0 , reserve :700 , ramBudget :false , ramHeadroom :false } }); 138try { 139await f . native . refresh ( true ); 140assert . equal ( reserveOf (( await load ( f , { resourceProfile :'performance' })). args ), '1536' ); 141const edited = f . read (); edited . args = edited . args . map ( a => ( a === '1536' ?'1024' :a )); f . write ( edited ); 142assert . equal ( reserveOf (( await load ( f , { resourceProfile :'performance' })). args ), '1024' ); 143} finally { await f . native . close (); f . cleanup (); } 144}); 145 146test ( '3216: engine arguments the app does not know (control vectors) survive a load and a setup run that rewrites "args"' , async () => { 147// setup wrote ARGS (and a copy of them); the person typed the control vector options into the JSON editor 148const f = fixture ({ args :[ ...ARGS , ...CVEC ], args_written :ARGS }); 149try { 150await f . native . refresh ( true ); 151const run = await load ( f , { resourceProfile :'work' }); 152assert . deepEqual ( run . args . slice ( run . args . indexOf ( '--control-vector-scaled' ), run . args . indexOf ( '--control-vector-scaled' ) + 9 ), CVEC ); 153const file = f . read (); 154assert . deepEqual ( file . user_args , CVEC ); 155assert . ok ( file . args . join ( ' ' ). includes ( CVEC . join ( ' ' )), 'the file keeps them in "args" too' ); 156// a setup run rewrites "args" from its own choices and carries user_args along (setup.py carry_user_args) 157f . write ({ ...file , args :[ ...ARGS , '--max-context' , '65536' ], args_written :undefined }); 158const again = await load ( f , { resourceProfile :'work' }); 159assert . ok ( again . args . join ( ' ' ). includes ( CVEC . join ( ' ' )), 'still there after the rewrite' ); 160assert . equal ( again . args . filter ( a => a === '--cvec-mode' ). length , 1 ); 161assert . deepEqual ( f . read (). user_args , CVEC ); 162// removing them from "user_args" and "args" by hand takes them off for good 163const bare = f . read (); bare . user_args = undefined ; bare . args = [ ...ARGS ]; bare . args_written = [ ...ARGS ]; f . write ( bare ); 164assert . ok ( ! ( await load ( f , { resourceProfile :'work' })). args . includes ( '--cvec-mode' )); 165} finally { await f . native . close (); f . cleanup (); } 166}); 167 168test ( '3216: without any resource preference the load keeps them too, and the person\'s value beats a generated one for the same option' , async () => { 169const f = fixture ({ args :ARGS . map ( a => ( a === '4' ?'2' :a )), args_written :ARGS }); 170try { 171await f . native . refresh ( true ); 172const run = await load ( f ); // no resourceProfile: the plain path 173assert . equal ( run . args . filter ( a => a === '--spec' ). length , 1 ); 174assert . equal ( run . args [ run . args . indexOf ( '--spec' ) + 1 ], '2' , 'a hand-changed --spec is the person\'s' ); 175assert . deepEqual ( f . read (). user_args , [ '--spec' , '2' ]); 176} finally { await f . native . close (); f . cleanup (); } 177});