Add opt-in full-block test: times the speech-to-motion transformer and estimates a whole 400 ms block
Browse filesFiles changed (3)
- app.js+33-4
- index.html+1
- style.css+1-1
9 unmodified lines1011121314151617 unmodified lines34353637383940104 unmodified lines1451461471481491501511521531541551561571585 unmodified lines1641651661671681699 unmodified linesconst MODELS = local(q.get('models')) || 'https://huggingface.co/WeReCooking/AvatarForcing-ONNX-WebGPU/resolve/main/';const $ = (id) => document.getElementById(id);const ui = { status: $('status'), bar: $('bar').firstElementChild, canvas: $('view'), fps: $('fps'), ms: $('ms'), verdict: $('verdict'), gpu: $('gpu'),start: $('start'), pause: $('pause'), bench: $('bench'), amp: $('amp'), spd: $('spd'), file: $('file'), avatars: $('avatars'), out: $('benchout') };const ctx2d = ui.canvas.getContext('2d');const AVATARS = [['rumi', 'Rumi'], ['vicky', 'Vicky'], ['ken', 'Ken'], ['amber', 'Amber']];const SIZE = 512, FEAT_SHAPES = [[1, 512, 8, 8], [1, 512, 16, 16], [1, 512, 32, 32], [1, 256, 64, 64], [1, 128, 128, 128], [1, 64, 256, 256], [1, 32, 512, 512]];17 unmodified linesfor (let i = 0; i < 65536; i++) { const s = i & 0x8000 ? -1 : 1, e = (i >> 10) & 31, f = i & 1023; HALF[i] = e === 0 ? s * f * 2 ** -24 : e === 31 ? (f ? NaN : s * Infinity) : s * (1 + f / 1024) * 2 ** (e - 15); }// ------------------------------------------------------------ model loadinglet sess = null, F16 = false, basis = null, feats = null, xr = null, running = false, token = 0;async function fetchBytes(url, label, base, span) { // download with progress; the browser cache keeps it for next timeconst r = await fetch(url); if (!r.ok) throw new Error(`${label}: HTTP ${r.status}`);104 unmodified linesconst med = (a) => a.slice().sort((x, y) => x - y)[a.length >> 1], p95 = (a) => a.slice().sort((x, y) => x - y)[Math.floor(a.length * 0.95)];const m = med(tot), msg = `${ui.gpu.textContent}\nframe (render + read back + paint): median ${m.toFixed(1)} ms, p95 ${p95(tot).toFixed(1)} ms, render+read back median ${med(gpu).toFixed(1)} ms\n` +`= ${(1000 / m).toFixed(1)} fps; a 10-frame block takes about ${(10 * m).toFixed(0)} ms of the 400 ms budget (the speech-to-motion step still has to fit in the rest)`;ui.out.textContent = msg; ui.out.hidden = false; status('Benchmark done.', 1);if (was) { running = true; loop(token); }}// ------------------------------------------------------------ UIasync function useAvatar(src) {try { token++; running = false; ui.pause.textContent = 'Pause'; status('Reading the portrait…', 0.7); await loadModels(); const ms = await loadPortrait(src);status(`Portrait encoded in ${ms} ms. Press Start.`, 1); ui.start.disabled = false; ui.bench.disabled = false; ui.pause.disabled = true; }catch (e) { console.error(e); status(e.message || String(e), null, true); }}for (const [id, name] of AVATARS) {5 unmodified linesui.start.addEventListener('click', () => { if (!feats) return; status('Rendering live. Motion is synthetic (not speech).', 1); running = true; token++; ui.start.disabled = true; ui.pause.disabled = false; loop(token); });ui.pause.addEventListener('click', () => { running = !running; ui.pause.textContent = running ? 'Pause' : 'Resume'; if (running) { token++; loop(token); } });ui.bench.addEventListener('click', benchmark);document.addEventListener('visibilitychange', () => { if (document.hidden && running) { running = false; ui.pause.textContent = 'Resume'; } }); // do not burn the GPU in a background tabwindow.__av = { useAvatar, benchmark, frame, paint, pixel: (x, y) => [...imgData.data.slice((y * SIZE + x) * 4, (y * SIZE + x) * 4 + 4)], state: () => ({ F16, ema }) }; // test hookadapterInfo().then((i) => { // check the GPU up front so nobody clicks a portrait and waits just to be told no9 unmodified lines1011121314151617 unmodified lines34353637383940104 unmodified lines1451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851865 unmodified lines1921931941951961971989 unmodified linesconst MODELS = local(q.get('models')) || 'https://huggingface.co/WeReCooking/AvatarForcing-ONNX-WebGPU/resolve/main/';const $ = (id) => document.getElementById(id);const ui = { status: $('status'), bar: $('bar').firstElementChild, canvas: $('view'), fps: $('fps'), ms: $('ms'), verdict: $('verdict'), gpu: $('gpu'),start: $('start'), pause: $('pause'), bench: $('bench'), bflow: $('bflow'), amp: $('amp'), spd: $('spd'), file: $('file'), avatars: $('avatars'), out: $('benchout') };const ctx2d = ui.canvas.getContext('2d');const AVATARS = [['rumi', 'Rumi'], ['vicky', 'Vicky'], ['ken', 'Ken'], ['amber', 'Amber']];const SIZE = 512, FEAT_SHAPES = [[1, 512, 8, 8], [1, 512, 16, 16], [1, 512, 32, 32], [1, 256, 64, 64], [1, 128, 128, 128], [1, 64, 256, 256], [1, 32, 512, 512]];17 unmodified linesfor (let i = 0; i < 65536; i++) { const s = i & 0x8000 ? -1 : 1, e = (i >> 10) & 31, f = i & 1023; HALF[i] = e === 0 ? s * f * 2 ** -24 : e === 31 ? (f ? NaN : s * Infinity) : s * (1 + f / 1024) * 2 ** (e - 15); }// ------------------------------------------------------------ model loadinglet flowSess = null, frameMedian = 0, sess = null, F16 = false, basis = null, feats = null, xr = null, running = false, token = 0;async function fetchBytes(url, label, base, span) { // download with progress; the browser cache keeps it for next timeconst r = await fetch(url); if (!r.ok) throw new Error(`${label}: HTTP ${r.status}`);104 unmodified linesconst med = (a) => a.slice().sort((x, y) => x - y)[a.length >> 1], p95 = (a) => a.slice().sort((x, y) => x - y)[Math.floor(a.length * 0.95)];const m = med(tot), msg = `${ui.gpu.textContent}\nframe (render + read back + paint): median ${m.toFixed(1)} ms, p95 ${p95(tot).toFixed(1)} ms, render+read back median ${med(gpu).toFixed(1)} ms\n` +`= ${(1000 / m).toFixed(1)} fps; a 10-frame block takes about ${(10 * m).toFixed(0)} ms of the 400 ms budget (the speech-to-motion step still has to fit in the rest)`;frameMedian = m; ui.out.textContent = msg; ui.out.hidden = false; status('Benchmark done.', 1);if (was) { running = true; loop(token); }}// ------------------------------------------------------------ speech-to-motion step: timing only (random inputs)// One block of 10 frames needs ~9 passes of this 8-layer transformer (2 guidance branches, 12 tokens, KV cache) plus 10 renderer frames.async function flowBenchmark() {if (!sess) return;const was = running; running = false; token++; ui.bflow.disabled = true;try {const dt = q.get('flow') === '32' ? 'fp32' : (F16 ? 'fp16' : 'fp32'), half = dt === 'fp16';if (!flowSess || flowSess.dt !== dt) {const b = await fetchBytes(`${MODELS}flow_step_${dt}.onnx`, `speech-to-motion step (${dt})`, 0, 0.8);status('Preparing the GPU programs…', 0.85); const t = performance.now();flowSess = { dt, s: await ort.InferenceSession.create(b, { executionProviders: ['webgpu'], graphOptimizationLevel: 'all', logSeverityLevel: 3 }), ms: Math.round(performance.now() - t) };}const shapes = { x: [2, 12, 512], c: [2, 12, 1024], wr: [2, 12, 512], freqs: [12, 128], ada: [2, 12, 6144], fada: [2, 12, 2048] };for (let i = 0; i < 8; i++) { shapes['sk' + i] = [2, 8, 38, 128]; shapes['sv' + i] = [2, 8, 38, 128]; shapes['ck' + i] = [2, 8, 2, 128]; shapes['cv' + i] = [2, 8, 2, 128]; }const gauss = () => Math.sqrt(-2 * Math.log(Math.random() + 1e-12)) * Math.cos(2 * Math.PI * Math.random());const feed = {}; for (const [k, sh] of Object.entries(shapes)) { const n = sh.reduce((a, b) => a * b, 1), a = new Float32Array(n), sc = k === 'ada' || k === 'fada' ? 0.1 : 1; for (let i = 0; i < n; i++) a[i] = gauss() * sc; feed[k] = half ? new ort.Tensor('float16', Uint16Array.from(a, toHalf), sh) : new ort.Tensor('float32', a, sh); }const W = q.has('fw') ? +q.get('fw') : 3, N = +q.get('fn') || 15, times = [];status(`Timing the speech-to-motion step: ${W} warm-up, ${N} timed passes…`, 0.9);for (let i = 0; i < W + N; i++) { const t = performance.now(); const o = await exclusive(async () => { const r = await flowSess.s.run(feed); await r.y.getData(); return r; }); if (i >= W) times.push(performance.now() - t); }const med = times.slice().sort((a, b) => a - b)[times.length >> 1], fm = frameMedian || 0;let msg = `speech-to-motion step (${dt}): median ${med.toFixed(1)} ms per pass; a block needs about 9 passes = ${(9 * med).toFixed(0)} ms (GPU programs built in ${flowSess.ms} ms)\n`;msg += fm ? `renderer: ${(10 * fm).toFixed(0)} ms for 10 frames (from the last Benchmark)\nESTIMATED BLOCK: ${(9 * med + 10 * fm).toFixed(0)} ms of the 400 ms budget (${(400 / (9 * med + 10 * fm)).toFixed(2)}× real time; above 1.0 is fast enough)\nNote: random inputs, passes run one after another (a single GPU queue does not overlap them with rendering), no speech recognition, language model or voice counted.`: 'Run Benchmark first to add the renderer time and get the full-block estimate.';ui.out.textContent = (ui.out.hidden ? '' : ui.out.textContent + '\n\n') + msg; ui.out.hidden = false; status('Full-block test done.', 1);} catch (e) { console.error(e); status(`Full-block test failed: ${e.message || e}`, null, true); }ui.bflow.disabled = false; if (was) { running = true; loop(token); }}// ------------------------------------------------------------ UIasync function useAvatar(src) {try { token++; running = false; ui.pause.textContent = 'Pause'; status('Reading the portrait…', 0.7); await loadModels(); const ms = await loadPortrait(src);status(`Portrait encoded in ${ms} ms. Press Start.`, 1); ui.start.disabled = false; ui.bench.disabled = false; ui.bflow.disabled = false; ui.pause.disabled = true; }catch (e) { console.error(e); status(e.message || String(e), null, true); }}for (const [id, name] of AVATARS) {5 unmodified linesui.start.addEventListener('click', () => { if (!feats) return; status('Rendering live. Motion is synthetic (not speech).', 1); running = true; token++; ui.start.disabled = true; ui.pause.disabled = false; loop(token); });ui.pause.addEventListener('click', () => { running = !running; ui.pause.textContent = running ? 'Pause' : 'Resume'; if (running) { token++; loop(token); } });ui.bench.addEventListener('click', benchmark);ui.bflow.addEventListener('click', flowBenchmark);document.addEventListener('visibilitychange', () => { if (document.hidden && running) { running = false; ui.pause.textContent = 'Resume'; } }); // do not burn the GPU in a background tabwindow.__av = { useAvatar, benchmark, frame, paint, pixel: (x, y) => [...imgData.data.slice((y * SIZE + x) * 4, (y * SIZE + x) * 4 + 4)], state: () => ({ F16, ema }) }; // test hookadapterInfo().then((i) => { // check the GPU up front so nobody clicks a portrait and waits just to be told no
18 unmodified lines19202122232418 unmodified lines<div id="avatars" role="group" aria-label="Portraits"></div><label class="up">or your own face-centred portrait <input id="file" type="file" accept="image/*"></label><div id="row"><button id="start" type="button" disabled>Start</button><button id="pause" type="button" disabled>Pause</button><button id="bench" type="button" disabled>Benchmark</button></div><label>Motion strength <input id="amp" type="range" min="0" max="1.2" step="0.05" value="0.5"></label><label>Motion speed <input id="spd" type="range" min="0.2" max="3" step="0.1" value="1"></label><p id="fps" aria-live="off">– fps</p>18 unmodified lines1920212223242518 unmodified lines<div id="avatars" role="group" aria-label="Portraits"></div><label class="up">or your own face-centred portrait <input id="file" type="file" accept="image/*"></label><div id="row"><button id="start" type="button" disabled>Start</button><button id="pause" type="button" disabled>Pause</button><button id="bench" type="button" disabled>Benchmark</button></div><div id="row2"><button id="bflow" type="button" disabled title="Downloads the speech-to-motion transformer (about 270 MB) and times it with your renderer speed">Full-block test (+270 MB)</button></div><label>Motion strength <input id="amp" type="range" min="0" max="1.2" step="0.05" value="0.5"></label><label>Motion speed <input id="spd" type="range" min="0.2" max="3" step="0.1" value="1"></label><p id="fps" aria-live="off">– fps</p>
14 unmodified lines1516171819202114 unmodified lines.up { font-size: 13px; color: var(--muted); display: grid; gap: 4px; } label { font-size: 13px; color: var(--muted); display: grid; gap: 2px; }#row { display: flex; gap: 8px; flex-wrap: wrap; }button { font: inherit; font-weight: 600; min-height: 38px; padding: 0 16px; cursor: pointer; color: var(--accent-ink); background: var(--accent); border: 0; border-radius: 5px; }button:disabled { opacity: .5; cursor: default; } #pause, #bench { background: transparent; color: var(--ink); border: 1px solid var(--line); font-weight: 500; }#fps { margin: 4px 0 0; font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; } #ms, #verdict { margin: 0; font-size: 13px; color: var(--muted); }#verdict.ok { color: var(--ok); } #verdict.slow { color: var(--bad); }#status { margin: 0; min-height: 1.4em; font-size: 13px; color: var(--muted); } #status.bad { color: var(--bad); }14 unmodified lines1516171819202114 unmodified lines.up { font-size: 13px; color: var(--muted); display: grid; gap: 4px; } label { font-size: 13px; color: var(--muted); display: grid; gap: 2px; }#row { display: flex; gap: 8px; flex-wrap: wrap; }button { font: inherit; font-weight: 600; min-height: 38px; padding: 0 16px; cursor: pointer; color: var(--accent-ink); background: var(--accent); border: 0; border-radius: 5px; }button:disabled { opacity: .5; cursor: default; } #pause, #bench, #bflow { background: transparent; color: var(--ink); border: 1px solid var(--line); font-weight: 500; }#fps { margin: 4px 0 0; font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; } #ms, #verdict { margin: 0; font-size: 13px; color: var(--muted); }#verdict.ok { color: var(--ok); } #verdict.slow { color: var(--bad); }#status { margin: 0; min-height: 1.4em; font-size: 13px; color: var(--muted); } #status.bad { color: var(--bad); }
app.js+33-4
index.html+1
style.css+1-1