okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
039d2d460
main
1#!/usr/bin/env node 2/** 3* The Graphics cards choice of the app, end to end on a box with two cards, through the installed app layout (packaged host + bundled Control + a real 4* engine component): the model list carries the options (one rule, strata_control/gpu_fit.py), a model that fits one card recommends that card, then 5* load with ONE card -> answers, only that card holds the model; load with BOTH cards (the layer split) -> answers, both cards hold the model. 6* 7* node cards-e2e.mjs --install <folder with node/ host/ control/> --components <folder holding engine/<package>> --work <scratch> \ 8* --family qwen --model IQ3_XXS --data <data folder that has the model files> [--smi "nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits"] 9*/ 10import { spawn , spawnSync } from 'node:child_process' ; 11import { existsSync , mkdirSync , readdirSync } from 'node:fs' ; 12import { createServer } from 'node:net' ; 13import { join , resolve } from 'node:path' ; 14 15const args = Object . fromEntries ( process . argv . slice ( 2 ). reduce (( acc , a , i , all ) => ( a . startsWith ( '--' ) ?[ ...acc , [ a . slice ( 2 ), all [ i + 1 ]?. startsWith ( '--' ) || all [ i + 1 ] === undefined ?true :all [ i + 1 ]]] :acc ), [])); 16for ( const need of [ 'install' , 'components' , 'work' , 'model' ]) if ( ! args [ need ]) { console . error ( 'missing --' + need ); process . exit ( 2 ); } 17const install = resolve ( args . install ), work = resolve ( args . work ), components = resolve ( args . components ), family = String ( args . family ?? 'qwen' ), modelId = String ( args . model ); 18const node = join ( install , 'node' , 'node' ), hostEntry = join ( install , 'host' , 'server' , 'index.mjs' ); 19const dirs = { models :join ( work , 'models' ), profiles :join ( work , 'profiles' ), runtime :join ( work , 'runtime' ), home :join ( work , 'home' ), xdg :join ( work , 'xdg' ), appdata :join ( work , 'appdata' ) }; 20for ( const d of [ dirs . models , dirs . home , dirs . xdg , dirs . appdata ]) mkdirSync ( d , { recursive :true }); 21const delay = ms => new Promise ( r => setTimeout ( r , ms )); 22let failed = false ; 23const record = ( name , ok , detail = '' ) => { if ( ! ok ) failed = true ; console . log ( ` ${ ok ? 'ok ' : 'FAIL' } ${ name } ${ detail ? ' ' + String ( detail ). slice ( 0 , 500 ) : '' } ` ); }; 24const freePort = async () => { const s = createServer (); await new Promise ( r => s . listen ( 0 , '127.0.0.1' , r )); const p = s . address (). port ; await new Promise ( r => s . close ( r )); return p ; }; 25const env = { ...process . env , STRATA_COMPONENTS_DIR :components , STRATA_PROFILE_DIR :dirs . profiles , STRATA_RUNTIME_DIR :dirs . runtime , STRATA_INSTALL_DIR :dirs . models , 26STRATA_HOME :dirs . home , STRATA_MODELS_DIR :join ( work , 'models-unused' ), APPDATA :dirs . appdata , XDG_CONFIG_HOME :dirs . xdg , PYTHONDONTWRITEBYTECODE :'1' }; 27delete env . STRATA_API_KEY ; 28const port = await freePort (); 29const base = `http://127.0.0.1: ${ port } ` ; 30const host = spawn ( node , [ hostEntry ], { cwd :join ( install , 'host' ), stdio :[ 'ignore' , 'pipe' , 'pipe' ], 31env :{ ...env , STRATA_PORT :String ( port ), STRATA_DIST :join ( install , 'host' , 'dist' ), NODE_ENV :'production' , STRATA_PACKAGED :'1' , STRATA_REAL_ENGINE :'1' , STRATA_NO_ACQUIRE :'1' , 32STRATA_SHARE_PORT :String ( await freePort ()), STRATA_TUNNEL_PORT :String ( await freePort ()) } }); 33let hostLog = '' ; 34host . stdout . on ( 'data' , c => { hostLog += c ; }); host . stderr . on ( 'data' , c => { hostLog += c ; }); 35const call = async ( path , method = 'GET' , body ) => { 36const response = await fetch ( base + path , { method, headers :{ 'content-type' :'application/json' }, body :body === undefined ?undefined :JSON . stringify ( body ) }); 37return { status :response . status , body :await response . json (). catch (() => null ) }; 38}; 39async function waitJob ( id , seconds ) { 40let last = '' ; 41for ( let i = 0 ; i < seconds * 2 ; i ++ ) { 42const state = ( await call ( '/api/native' )). body ; 43const job = state . jobs . find ( j => j . id === id ); 44if ( job && job . summary !== last ) { last = job . summary ; console . log ( ' ... ' + String ( last ). slice ( 0 , 120 )); } 45if ( job && job . state !== 'running' ) return { job, state}; 46await delay ( 500 ); 47} 48throw new Error ( 'operation did not finish' ); 49} 50const smi = () => { 51const cmd = String ( args . smi ?? 'nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits' ). split ( ' ' ); 52const r = spawnSync ( cmd [ 0 ], cmd . slice ( 1 ), { encoding :'utf8' }); 53return ( r . stdout ?? '' ). split ( '\n' ). map ( s => Number ( s . trim ())). filter ( Number . isFinite ); 54}; 55async function chatPong ( instance ) { 56const chat = await call ( '/v1/chat/completions' , 'POST' , { model :instance ?. apiModelId ?? modelId . toLowerCase (), max_tokens :400 , stream :false , temperature :0 , messages :[{ role :'user' , content :'Reply with exactly one word: pong' }] }); 57const m = chat . body ?. choices ?.[ 0 ]?. message ?? {}; 58return { ok :chat . status === 200 && / pong / i . test ( ` ${ m . content ?? '' } ` ), text :` ${ m . content ?? '' } ` . slice ( 0 , 60 ), status :chat . status }; 59} 60async function main () { 61let up = false ; 62for ( let i = 0 ; i < 200 && ! up ; i ++ ) { try { up = ( await fetch ( base + '/api/health' )). ok ; } catch { /* not yet */ } if ( ! up ) await delay ( 150 ); } 63record ( 'host healthy' , up , up ?base :hostLog . slice ( - 500 )); 64if ( ! up ) return ; 65const packages = existsSync ( join ( components , 'engine' )) ?readdirSync ( join ( components , 'engine' )) :[]; 66record ( 'an engine component is installed' , packages . length > 0 , packages . join ( ', ' )); 67 68// ---- the model list carries the choice 69const native = ( await call ( '/api/native?refresh=1' )). body ; 70const gpus = native . hardware ?. gpus ?? []; 71record ( 'the host sees two or more graphics cards' , gpus . filter ( g => g . usable !== false ). length >= 2 , JSON . stringify ( gpus . map ( g => ({ i :g . index , n :g . name , gb :g . vram_gb })))); 72const size = native . catalog ?. families ?. find ( f => f . family === family )?. sizes . find ( s => s . model === modelId ); 73const opt = size ?. gpu_options ; 74record ( 'the model list offers the cards choice for this model' , !! opt && opt . options . length >= 2 , JSON . stringify ( opt ?. options ?. map ( o => ({ key :o . key , cards :o . cards , verdict :o . verdict , rec :o . recommended })))); 75record ( 'a model that fits one card recommends the single largest card (the split is offered, not forced)' , opt ?. recommended === 'single' && opt . options . find ( o => o . key === 'split' ), `recommended= ${ opt ?. recommended } ` ); 76const single = opt ?. options . find ( o => o . key === 'single' )?. cards ?? [ 0 ]; 77const both = opt ?. options . find ( o => o . key === 'split' )?. cards ?? [ 0 , 1 ]; 78 79// ---- Download and prepare with the recommended card 80const values = { family, model :modelId , context :32768 , vision :'no' , gpu :single [ 0 ], ...( args . data ?{ data_dir :resolve ( args . data ) } :{}) }; 81const started = await call ( '/api/native/operations' , 'POST' , { kind :'install' , args :values }); 82record ( 'the install operation starts' , started . status === 200 || started . status === 202 , JSON . stringify ( started . body ). slice ( 0 , 160 )); 83const done = await waitJob ( started . body . id , 3600 ); 84record ( 'Download and prepare finishes' , done . job . state === 'done' , ` ${ done . job . state } ${ done . job . error ?? done . job . summary } ` ); 85const installed = ( await call ( '/api/native?refresh=1' )). body . installed ?. models ?. find ( m => String ( m . model ). toLowerCase () === modelId . toLowerCase ()); 86const model = installed ?. model ?? modelId . toLowerCase (); 87 88for ( const [ label , cards ] of [[ 'ONE card' , single ], [ 'BOTH cards (layer split)' , both ]]) { 89const before = smi (); 90const load = await call ( '/api/native/operations' , 'POST' , { kind :'load' , args :{ model, gpus :cards } }); 91const loaded = await waitJob ( load . body . id , 900 ); 92const instance = loaded . state . instances [ 0 ]; 93record ( `load with ${ label } starts the engine` , loaded . job . state === 'done' && instance ?. status === 'ready' , ` ${ loaded . job . state } ${ loaded . job . error ?? '' } ; instance= ${ instance ?. status } ; gpus= ${ JSON . stringify ( instance ?. gpus )} ` ); 94record ( `the instance runs on exactly the chosen card(s)` , JSON . stringify ([ ...( instance ?. gpus ?? [])]. sort ()) === JSON . stringify ([ ...cards ]. sort ()), `want ${ JSON . stringify ( cards )} got ${ JSON . stringify ( instance ?. gpus )} ` ); 95await delay ( 3000 ); 96const used = smi (); 97const holding = used . map (( mb , i ) => ( mb - ( before [ i ] ?? 0 )) > 1000 ?i :- 1 ). filter ( i => i >= 0 ); 98record ( `only the chosen card(s) hold the model (graphics memory in use: ${ used . join ( ', ' )} MiB)` , JSON . stringify ( holding ) === JSON . stringify ([ ...cards ]. sort ()), `holding ${ JSON . stringify ( holding )} ` ); 99const pong = await chatPong ( instance ); 100record ( `it answers ( ${ label } )` , pong . ok , ` ${ pong . status } ${ JSON . stringify ( pong . text )} ` ); 101const unloaded = await call ( `/api/native/instances/ ${ instance ?. id } /unload` , 'POST' , {}); 102record ( `unload ( ${ label } )` , unloaded . status === 200 , unloaded . status ); 103await delay ( 4000 ); 104} 105} 106try { await main (); } catch ( e ) { record ( 'run' , false , e . stack ?? e ); } 107host . kill (); 108await delay ( 1500 ); 109console . log ( failed ?'CARDS-E2E FAIL' :'CARDS-E2E PASS' ); 110process . exit ( failed ?1 :0 );