okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
d3cb2f58f
main
1/** 0.2.1, group 2: choosing graphics cards (#3203 #3195 #3256 #3221 #3211). */ 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { readFile } from 'node:fs/promises' ; 5import { fileURLToPath } from 'node:url' ; 6import { build , transform } from 'esbuild' ; 7import { nativeFixture , finishJob } from './helpers/native-fixture.mjs' ; 8 9const fit = await transform ( await readFile ( new URL ( '../src/lib/native-fit.ts' , import . meta. url ), 'utf8' ), { loader :'ts' , format :'esm' }); 10const { runCards} = await import ( `data:text/javascript;base64, ${ Buffer . from ( fit . code ). toString ( 'base64' )} ` ); 11const native = await build ({ entryPoints :[ 'src/lib/native.ts' ], bundle :true , platform :'node' , format :'esm' , write :false , alias :{ react :fileURLToPath ( new URL ( './helpers/react-stub.mjs' , import . meta. url )) }, logLevel :'silent' }); 12 13const card = ( index , name , vram , extra = {}) => ({ index, name, vram_gb :vram , usable :true , ...extra }); 14 15test ( '#3195: the cards to choose from are the ones the engine drives; the other make and the iGPU are not listed' , () => { 16// two RTX 3090 and an AMD chip that the report numbers 0 again (the same number as the first 3090) 17const gpus = [ card ( 0 , 'NVIDIA GeForce RTX 3090' , 24 , { vendor :'nvidia' }), card ( 1 , 'NVIDIA GeForce RTX 3090' , 24 , { vendor :'nvidia' }), 18card ( 0 , 'AMD Radeon (gfx1036)' , 2 , { vendor :'amd' , usable :false })]; 19const shown = runCards ( gpus , 'nvidia' ); 20assert . deepEqual ( shown . map ( g => g . index ), [ 0 , 1 ], 'no second card 0' ); 21assert . equal ( new Set ( shown . map ( g => g . index )). size , shown . length , 'every number once, so one tick never moves another' ); 22// even when the chip is flagged usable, the engine's make decides 23assert . deepEqual ( runCards ([ ...gpus . slice ( 0 , 2 ), card ( 0 , 'AMD Radeon 760M' , 8 , { vendor :'amd' })], 'nvidia' ). map ( g => g . name ), [ 'NVIDIA GeForce RTX 3090' , 'NVIDIA GeForce RTX 3090' ]); 24}); 25 26test ( '#3195: a chip that shares the system memory is left out while a card of its own exists, and is the only choice when it is alone' , () => { 27const igpu = card ( 0 , 'AMD Radeon 8060S' , 64 , { vendor :'amd' , uma :true }); 28const real = card ( 1 , 'AMD Radeon RX 9070 XT' , 16 , { vendor :'amd' }); 29assert . deepEqual ( runCards ([ igpu , real ], 'amd' ). map ( g => g . index ), [ 1 ]); 30assert . deepEqual ( runCards ([ igpu ], 'amd' ). map ( g => g . index ), [ 0 ]); 31}); 32 33test ( '#3203: a card the engine cannot use (usable false) is never offered' , () => { 34const gpus = [ card ( 0 , 'AMD Radeon 760M' , 8 , { vendor :'amd' , usable :false }), card ( 1 , 'AMD Radeon RX 6800 XT' , 16 , { vendor :'amd' , usable :false })]; 35assert . deepEqual ( runCards ( gpus , 'amd' ), []); 36assert . deepEqual ( runCards ( null , 'amd' ), []); 37}); 38 39test ( '#3256: the model settings file keeps its GPU order when the app only suggests cards; a card picked in the screen wins' , async () => { 40const f = nativeFixture (); 41try { 42await f . control . refresh ( true ); 43const load = async args => { const done = await finishJob ( f . control , f . control . operation ( 'load' , args ). id ); assert . equal ( done . state , 'done' , done . error ); return f . control . resolveInstance ( done . result . instanceId ); }; 44// the fixture's file says "gpu": 0; the screen suggests card 1 45const suggested = await load ({ model :'iq2_xs' , gpus :[ 1 ], gpusFromRecommendation :true }); 46assert . deepEqual ( suggested . gpus , [ 0 ], 'the file names its card, so the suggestion does not replace it' ); 47await f . control . unload ( suggested . id ); 48const picked = await load ({ model :'iq2_xs' , gpus :[ 1 ] }); 49assert . deepEqual ( picked . gpus , [ 1 ], 'a pick in the screen is the person\'s word' ); 50} finally { await f . cleanup (); } 51}); 52 53test ( '#3256: with no GPU in the file the suggested cards apply, in the order given' , async () => { 54const f = nativeFixture (); 55try { 56await f . control . refresh ( true ); 57const { readFileSync, writeFileSync} = await import ( 'node:fs' ); 58const { join} = await import ( 'node:path' ); 59const path = join ( f . repo , 'strata-iq2_xs.json' ); 60const config = JSON . parse ( readFileSync ( path , 'utf8' )); delete config . gpu ; writeFileSync ( path , JSON . stringify ( config )); 61await f . control . refresh ( true ); 62const done = await finishJob ( f . control , f . control . operation ( 'load' , { model :'iq2_xs' , gpus :[ 1 , 0 ], gpusFromRecommendation :true }). id ); 63assert . equal ( done . state , 'done' , done . error ); 64assert . deepEqual ( f . control . resolveInstance ( done . result . instanceId ). gpus , [ 1 , 0 ]); 65} finally { await f . cleanup (); } 66}); 67 68test ( '#3203: one chosen card is sent to the installer as "gpu", never as "gpus" (it asks for two or more there)' , async () => { 69const f = nativeFixture (); 70try { 71await f . control . refresh ( true ); 72await f . control . plan ({ family :'qwen' , model :'IQ2_XS' , context :131072 , gpus :'0' }); 73const call = f . rpcCalls . filter ( c => c . name === 'strata_install' ). pop (); 74assert . equal ( call . arguments . gpu , 0 ); 75assert . equal ( 'gpus' in call . arguments && call . arguments . gpus !== undefined , false ); 76await f . control . plan ({ family :'qwen' , model :'IQ2_XS' , context :131072 , gpus :'0,1' }); 77const two = f . rpcCalls . filter ( c => c . name === 'strata_install' ). pop (); 78assert . equal ( two . arguments . gpus , '0,1' ); 79} finally { await f . cleanup (); } 80}); 81 82test ( '#3211: automatic placement names no card in the config; the cards the engine reports are the model\'s assigned cards' , async () => { 83const mod = await import ( `data:text/javascript;base64, ${ Buffer . from ( native . outputFiles [ 0 ]. text ). toString ( 'base64' )} ` ); 84const hardware = { gpus :[{ id :'nvidia:0' , index :0 , vendor :'nvidia' }, { id :'nvidia:1' , index :1 , vendor :'nvidia' }] }; 85const instance = ( gpus , reported ) => ({ id :'a' , apiModelId :'a' , model :'m' , label :'M' , status :'ready' , loadedAt :'x' , phase :'idle' , gpus, metrics :reported ?{ hardware :{ gpus :reported } } :null }); 86const routed = ( inst ) => mod . nativeRouting ({ instances :[ inst ], defaultId :'a' }, hardware ). instances [ 0 ]. gpuIds ; 87assert . deepEqual ( routed ( instance ([ 1 ], null )), [ 'nvidia:1' ], 'a named card stays as it was' ); 88assert . deepEqual ( routed ( instance ([], [{ index :0 }])), [ 'nvidia:0' ], 'Automatic: the card the engine reports' ); 89assert . deepEqual ( routed ( instance ([], null )), [], 'nothing known: no assigned cards, so the Monitor shows all' ); 90}); 91 92test ( '#3211 #3221: the Monitor opens on the model\'s assigned cards and the dropdown rows grow with their text' , async () => { 93const monitor = await readFile ( new URL ( '../src/screens/Monitor.tsx' , import . meta. url ), 'utf8' ); 94assert . match ( monitor , / useKept<string \| null>\('monitor\.gpu', null\) / , 'unchosen until the person picks, and the pick is kept' ); 95assert . match ( monitor , / gpuSelection === 'all' \|\| !assignedDevices\.length \? 'all' : 'assigned' / , 'assigned by default when the model has cards' ); 96const css = await readFile ( new URL ( '../src/styles/annotation-fixes.css' , import . meta. url ), 'utf8' ); 97assert . match ( css , / \.select__opt \{ height: auto; min-height: 36px; / , 'a wrapped name no longer runs over the next row' ); 98}); 99 100test ( 'a card picked in the screen replaces an old AMD config\'s "hip_ordinal" (it would send the engine to the old card)' , async () => { 101const f = nativeFixture (); 102try { 103await f . control . refresh ( true ); 104const { readFileSync, writeFileSync} = await import ( 'node:fs' ); 105const { join} = await import ( 'node:path' ); 106const path = join ( f . repo , 'strata-iq2_xs.json' ); 107const config = JSON . parse ( readFileSync ( path , 'utf8' )); config . hip_ordinal = 0 ; writeFileSync ( path , JSON . stringify ( config )); 108await f . control . refresh ( true ); 109const done = await finishJob ( f . control , f . control . operation ( 'load' , { model :'iq2_xs' , gpus :[ 1 ] }). id ); 110assert . equal ( done . state , 'done' , done . error ); 111const instance = f . control . resolveInstance ( done . result . instanceId ); 112assert . deepEqual ( instance . gpus , [ 1 ]); 113const written = JSON . parse ( readFileSync ( instance . configPath , 'utf8' )); 114assert . equal ( written . gpu , 1 ); 115assert . equal ( 'hip_ordinal' in written , false ); 116} finally { await f . cleanup (); } 117});