okami.mom/strata

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

Niko1221Merge f-022-gpuroles: Main / Helper / Off per graphics card in the app, TUI, launch, setup, server and Dockere28889d12

main
2.9 KiB52 linesraw
1/**
2 * The host's copy of the roles rules it needs to WRITE a model's settings (src/lib/gpu-roles.ts has the screen's, strata_control/gpu_roles.py the
3 * Terminal's and the server's; tests/data/gpu_roles_vectors.json is run against all three).
4 * Roles are { [card number]: 'main' | 'helper' | 'off' }. The engine takes the cards in stage order: Helpers, then the Main card LAST.
5 */
6export const ROLES = ['main', 'helper', 'off'];
7const mem = c => Number(c.vram_gb) || 0;
8const speed = c => (typeof c.speed === 'number' && c.speed > 0 ? c.speed : 0);
9const nums = roles => Object.keys(roles).map(Number).sort((a, b) => a - b);
10const onList = roles => nums(roles).filter(i => roles[i] !== 'off');
11const shortName = c => { let n = String(c.name || `GPU ${c.index + 1}`); for (const j of ['NVIDIA GeForce ', 'NVIDIA ', 'GeForce ', 'AMD Radeon ', 'AMD ', 'Intel(R) ', 'Intel ']) if (n.startsWith(j)) { n = n.slice(j.length); break; } return n.trim(); };
12
13export function parseSaved(value) {
14  if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
15  const out = {};
16  for (const [k, v] of Object.entries(value)) {
17    if (!/^\d+$/.test(k.trim()) || !ROLES.includes(String(v).toLowerCase())) return null;
18    out[Number(k)] = String(v).toLowerCase();
19  }
20  if (!Object.keys(out).length || Object.values(out).filter(r => r === 'main').length > 1) return null;
21  return out;
22}
23
24export function normalize(roles, cards) {
25  const cs = new Map(cards.map(c => [c.index, c]));
26  const out = {};
27  for (const i of nums(roles)) if (cs.has(i)) out[i] = ROLES.includes(roles[i]) ? roles[i] : 'off';
28  const list = onList(out), mains = list.filter(i => out[i] === 'main');
29  if (list.length && !mains.length) {
30    const best = [...list].sort((a, b) => speed(cs.get(b)) - speed(cs.get(a)) || Math.round(mem(cs.get(b))) - Math.round(mem(cs.get(a))) || a - b)[0];
31    out[best] = 'main';
32  }
33  for (const extra of mains.slice(1)) out[extra] = 'helper';
34  if (list.length === 1) out[list[0]] = 'main';
35  return out;
36}
37
38export function order(roles, cards) {
39  const cs = new Map(cards.map(c => [c.index, c]));
40  const r = normalize(roles, cards);
41  const helpers = nums(r).filter(i => r[i] === 'helper').sort((a, b) => speed(cs.get(a)) - speed(cs.get(b)) || a - b);
42  return [...helpers, ...nums(r).filter(i => r[i] === 'main')];
43}
44
45/** "gpu_roles" plus the legacy mirror ("gpu" Main last, "gpu_order": "as_given"); nothing on: "gpu": [] and "gpu_none": true. */
46export function mirror(roles, cards) {
47  const r = normalize(roles, cards), lst = order(r, cards);
48  const out = { gpu_roles: Object.fromEntries(nums(r).map(i => [String(i), r[i]])), gpu: [], gpu_cards: {} };
49  if (lst.length) { out.gpu = lst.length === 1 ? lst[0] : lst; out.gpu_order = 'as_given'; } else out.gpu_none = true;
50  for (const c of cards) if (c.index in r) out.gpu_cards[String(c.index)] = [shortName(c), c.bus ?? ''].filter(Boolean).join('|');
51  return out;
52}