okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
2.9 KiB60 linesraw
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(() => {
5  const box = document.createElement("label");
6  box.className = "model-switch";
7  box.hidden = true;
8  box.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).";
9  box.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>';
11  const sel = box.querySelector("select"), status = box.querySelector(".model-switch__s");
12  document.querySelector(".st-header__spacer").after(box);
13  let switching = null;
14
15  async function load() {
16    let sw;
17    try { sw = await (await fetch("switcher", {headers: headers()})).json(); } catch (e) { return; }
18    const keys = Object.keys(sw.choices || {});
19    if (!sw.enabled || sw.error || keys.length < 2) { box.hidden = true; return; }
20    sel.replaceChildren(...keys.map((k) => {
21      const o = document.createElement("option");
22      o.value = k;
23      o.textContent = sw.choices[k].replace(/ \(Strata SYCL engine\)$/, "");
24      o.selected = k === (sw.starting || sw.mode);
25      return o;
26    }));
27    sel.disabled = !!sw.starting || !!switching;
28    status.textContent = sw.starting && !switching ? `loading ${sw.choices[sw.starting] || sw.starting}…` : "";
29    box.hidden = false;
30  }
31
32  sel.onchange = async () => {
33    const mode = sel.value, name = sel.selectedOptions[0].textContent;
34    if (!confirm(`Load ${name}? The current model is unloaded first; it takes about 2 minutes and stops any answer in progress.`)) {
35      load();
36      return;
37    }
38    let from = null;
39    try { from = (await (await fetch("health", {cache: "no-store"})).json()).model; } catch (e) { /* going down */ }
40    switching = {from, t0: Date.now()};
41    sel.disabled = true;
42    try {
43      await fetch("switcher", {method: "POST", headers: headers(true), body: JSON.stringify({mode})});
44    } catch (e) { /* this server may already be going down */ }
45    const wait = async () => {
46      try {
47        const h = await (await fetch("health", {cache: "no-store"})).json();
48        if (h.model && h.model !== switching.from) { location.reload(); return; }
49      } catch (e) { /* down while the models swap */ }
50      const s = Math.round((Date.now() - switching.t0) / 1000);
51      if (s > 360) { status.textContent = `${name} did not come up in 6 minutes`; switching = null; sel.disabled = false; return; }
52      status.textContent = `switching to ${name}… ${s} s`;
53      setTimeout(wait, 3000);
54    };
55    setTimeout(wait, 5000);
56  };
57
58  load();
59  setInterval(() => { if (!switching) load(); }, 15000);   // a switch started elsewhere shows here too
60})();