okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1// The model menu (sycl/serve/server_intel.py): a host that swaps models on one GPU (config model_switcher) offers 2// the ones served on this port. Switching stops THIS server and starts the other on the same port, so the page 3// waits for /health to name the new model, then reloads. Uses app.js's headers(). 4(() => { 5const box = document . createElement ( "label" ); 6box . className = "model-switch" ; 7box . hidden = true ; 8box . title = "Which model is loaded. Only one fits on the GPU at a time: switching unloads this one and loads the other (about 2 minutes)." ; 9box . innerHTML = '<span class="model-switch__k">Model</span><select aria-label="Model on the GPU"></select>' + 10'<span class="model-switch__s" role="status"></span>' ; 11const sel = box . querySelector ( "select" ), status = box . querySelector ( ".model-switch__s" ); 12document . querySelector ( ".st-header__spacer" ). after ( box ); 13let switching = null ; 14 15async function load () { 16let sw ; 17try { sw = await ( await fetch ( "switcher" , { headers :headers ()})). json (); } catch ( e ) { return ; } 18const keys = Object . keys ( sw . choices || {}); 19if ( ! sw . enabled || sw . error || keys . length < 2 ) { box . hidden = true ; return ; } 20sel . replaceChildren ( ...keys . map (( k ) => { 21const o = document . createElement ( "option" ); 22o . value = k ; 23o . textContent = sw . choices [ k ]. replace ( / \(Strata SYCL engine\)$ / , "" ); 24o . selected = k === ( sw . starting || sw . mode ); 25return o ; 26})); 27sel . disabled = !! sw . starting || !! switching ; 28status . textContent = sw . starting && ! switching ?`loading ${ sw . choices [ sw . starting ] || sw . starting } …` :"" ; 29box . hidden = false ; 30} 31 32sel . onchange = async () => { 33const mode = sel . value , name = sel . selectedOptions [ 0 ]. textContent ; 34if ( ! confirm ( `Load ${ name } ? The current model is unloaded first; it takes about 2 minutes and stops any answer in progress.` )) { 35load (); 36return ; 37} 38let from = null ; 39try { from = ( await ( await fetch ( "health" , { cache :"no-store" })). json ()). model ; } catch ( e ) { /* going down */ } 40switching = { from, t0 :Date . now ()}; 41sel . disabled = true ; 42try { 43await fetch ( "switcher" , { method :"POST" , headers :headers ( true ), body :JSON . stringify ({ mode})}); 44} catch ( e ) { /* this server may already be going down */ } 45const wait = async () => { 46try { 47const h = await ( await fetch ( "health" , { cache :"no-store" })). json (); 48if ( h . model && h . model !== switching . from ) { location . reload (); return ; } 49} catch ( e ) { /* down while the models swap */ } 50const s = Math . round (( Date . now () - switching . t0 ) / 1000 ); 51if ( s > 360 ) { status . textContent = ` ${ name } did not come up in 6 minutes` ; switching = null ; sel . disabled = false ; return ; } 52status . textContent = `switching to ${ name } … ${ s } s` ; 53setTimeout ( wait , 3000 ); 54}; 55setTimeout ( wait , 5000 ); 56}; 57 58load (); 59setInterval (() => { if ( ! switching ) load (); }, 15000 ); // a switch started elsewhere shows here too 60})();