/* GPU Lab: run a small GPU kernel on the GPU unit wired into the weights of Meta Muse Glimmer 30B (option 3 backend).
The result is checked against the reference executor and plain Python. Honors the sleep/wake of the GPU backend. */
(() => {
'use strict';
const K = {
vadd: {label:'Vector add', make:n => ({a:rnd(n, 50), b:rnd(n, 50)}), py:d => ({c:d.a.map((v, i) => v + d.b[i])})},
dot: {label:'Dot product', make:n => ({a:rnd(n, 12), b:rnd(n, 12)}), py:d => ({out:[d.a.reduce((s, v, i) => s + v * d.b[i], 0)]})},
reduce: {label:'Reduce (sum)', make:n => ({x:rnd(n, 99)}), py:d => ({out:[d.x.reduce((s, v) => s + v, 0)]})},
histogram: {label:'Histogram (16 bins)', make:n => ({x:rnd(n, 15)}), py:d => ({bins:Array.from({length:16}, (_, i) => d.x.filter(v => v === i).length)})},
};
function rnd(n, m) { return Array.from({length:n}, () => Math.floor(Math.random() * (m + 1))); }
async function gpu(path, body, ms) {
const ctl = new AbortController(); const to = setTimeout(() => ctl.abort(), ms || 300000);
try {
const r = await fetch('api/agx/' + path, body === undefined ? {cache:'no-store', signal:ctl.signal, headers:{'X-VK-Session':window.VK_SID}} : {method:'POST', headers:{'Content-Type':'application/json', 'X-VK-Session':window.VK_SID}, body:JSON.stringify(body), signal:ctl.signal});
if (r.status === 429) throw new Error('rate');
if (!r.ok) throw new Error('http ' + r.status);
return await r.json();
} finally { clearTimeout(to); }
}
const css = document.createElement('style');
css.textContent = `.gl{padding:14px;font-family:system-ui,'Segoe UI',sans-serif;display:flex;flex-direction:column;gap:12px;height:100%;overflow:auto}
.gl h3{margin:0;font-size:18px}.gl .mut{color:var(--muted);font-size:13px}.gl .row{display:flex;gap:8px;flex-wrap:wrap;align-items:center}
.gl .card{border:1px solid var(--border);border-radius:12px;padding:12px;background:var(--card)}.gl pre{margin:6px 0 0;white-space:pre-wrap;word-break:break-word;font:12px ui-monospace,Consolas,monospace;background:var(--bg);border-radius:8px;padding:8px}
.gl .ok{color:#16A34A;font-weight:800}.gl .bad{color:#E11D48;font-weight:800}.gl .tag{font:800 11px system-ui;padding:3px 8px;border-radius:999px;background:#EEF2FF;color:#4338CA}
.gl select,.gl input[type=range]{height:32px}.gl .grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));gap:8px}.gl .kv b{display:block;font-size:20px;white-space:nowrap}.gl .note{font-size:12px;color:var(--muted);border-left:3px solid #F59E0B;padding:4px 8px;background:var(--bg);border-radius:6px}`;
document.head.append(css);
window.VK_EXTRA_APPS = window.VK_EXTRA_APPS || [];
window.VK_EXTRA_APPS.push(h => ({id:'gpulab', name:() => 'GPU Lab', color:'#0F766E',
icon:'',
w:820, h:640, render(body) {
body.style.padding = '0';
const root = document.createElement('div'); root.className = 'gl'; body.append(root);
root.innerHTML = `
GPU Lab
A GPU unit wired into the weights of Meta Muse Glimmer 30B runs a small kernel inside the model's own forward pass. The result is checked token by token against the reference executor and against plain Python.
Exact and auditable, not a GPU replacement: a real GPU is roughly a trillion times faster in raw throughput.
checking backend...
Inputs
Pick a kernel and press Run. A run takes from a few seconds to about a minute, because every GPU step is computed by the model.
`;
const q = k => root.querySelector(`[data-k="${k}"]`);
let data = null, waking = false;
const fresh = () => { data = K[q('k').value].make(+q('n').value); q('in').textContent = JSON.stringify(data); };
q('k').onchange = fresh; q('n').oninput = () => { q('nv').textContent = q('n').value; fresh(); }; q('rand').onclick = fresh; fresh();
const warm = window.VK_WARM.bar(); root.insertBefore(warm, root.children[1]);
let isReady = false;
async function health() { const h = await window.VK_WARM.health(); isReady = window.VK_WARM.ready(h, true); if (isReady) { warm.set({pct:100}); q('state').textContent = 'GPU backend ready'; } return isReady; }
async function ensureAwake() {
if (await health()) return true;
q('state').textContent = 'loading the model...';
if (waking) return false; waking = true;
const ok = await window.VK_WARM.wait({needGpu:true, alive:() => root.isConnected, onProgress:i => { warm.set(i); q('state').textContent = i.pct >= 100 ? 'GPU backend ready' : 'loading the model ' + i.pct + '%'; }});
waking = false; isReady = ok;
if (ok) h.toast('GPU Lab: the in-weights model is ready'); else q('state').textContent = 'still loading; try again shortly';
return ok;
}
ensureAwake();
// run all 34: every kernel x 2 random inputs, checked in the weights, shown live as each launch finishes
const all = document.createElement('div'); all.className = 'card';
all.innerHTML = `
Acceptance set, live
Every kernel in the unit, twice, with fresh random inputs. Each launch is checked inside the weights against the reference executor, and against plain Python where one exists. Nothing is replayed.
`;
root.insertBefore(all, q('out'));
const esc = v => String(v).replace(/[&<>]/g, c => ({'&':'&', '<':'<', '>':'>'})[c]);
function drawJob(j, wall) {
const done = j.done || 0, tot = j.total || 34, pct = Math.round(100 * done / tot);
q('allsum').innerHTML = `
exact in the weights${j.exact}/${tot}
equal to plain Python${j.python_equal}/${j.python_checked}
launches done${done}/${tot}
time${(j.elapsed == null ? wall : j.elapsed).toFixed(1)} s
';
}
async function runAll() {
const btnAll = q('all');
if (!isReady && !(await health())) { q('allsum').innerHTML = 'The in-weights model is loading; see the progress bar above. The run starts once it is ready.'; btnAll.disabled = true; const ok = await ensureAwake(); btnAll.disabled = false; if (!ok) return; }
btnAll.disabled = true; const t0 = performance.now();
try {
let j = await gpu('gpu/all', {}, 30000);
if (j.error) throw new Error(j.error);
if (!j.started) q('allsum').innerHTML = 'Another visitor started a run a moment ago; showing that run live.';
while (root.isConnected) {
await new Promise(r => setTimeout(r, 700));
j = await gpu('gpu/job', undefined, 15000); drawJob(j, (performance.now() - t0) / 1000);
if (!j.running) break;
}
if (j.exact === j.total) h.toast(`GPU Lab: ${j.exact}/${j.total} exact in ${j.elapsed} s`);
return j;
} catch (e) { q('allsum').innerHTML = `${e.message === 'rate' ? 'Too many requests from your network; try again in a few minutes.' : 'The in-weights model is busy or loading. Please try again in a minute.'}`; }
finally { btnAll.disabled = false; }
}
q('all').onclick = runAll;
root.vkRunAll = runAll;
q('run').onclick = async () => {
const out = q('out'), k = q('k').value, n = +q('n').value;
if (!isReady && !(await health())) { out.innerHTML = 'The in-weights model is loading (progress above). Agent Guard option 1 stays live meanwhile.'; ensureAwake(); return; }
q('run').disabled = true; out.innerHTML = 'Running inside the model...';
const t0 = performance.now();
try {
const mode = q('mode').value;
const r = await gpu('gpu', {kernel:k, data, n, mode});
if (r.error) throw new Error(r.error);
const py = K[k].py(data), wall = (performance.now() - t0) / 1000;
const okPy = JSON.stringify(r.outputs) === JSON.stringify(py);
const fms = v => v == null ? 'n/a' : (+v).toFixed(1) + ' ms';
const parity = r.parity == null ? `
Parity: not checked in this mode. Reference executor only, ${fms(r.ms_reference)}.
`
: `
Parity: in-weights ${r.parity ? '✓ equals' : '✗ differs from'} reference. In the weights ${fms(r.ms_verify)}, reference ${fms(r.ms_reference)}.
`;
out.innerHTML = `${parity}
exact vs reference${r.equals_reference ? '✓ exact' : '✗ differs'}
model tokens${(r.tokens || 0).toLocaleString('en-US')}
GPU time in the model${r.sec == null ? 'n/a' : r.sec + ' s'}
round trip${wall.toFixed(1)} s
Output
${JSON.stringify(r.outputs)}
Plain Python
${JSON.stringify(py)}
`;
} catch (e) {
out.innerHTML = `${e.message === 'rate' ? 'Too many requests from your network; try again in a few minutes.' : 'The in-weights model is busy or waking. Please try again in a minute.'}`;
ensureAwake();
} finally { q('run').disabled = false; }
};
}}));
})();