Fix black frame on fp16: read Float16Array output as numbers (not raw bits); report NaN frames instead of failing silently
Browse filesCurrent Chrome returns float16 tensor data as Float16Array of real numbers; the page indexed a bit-pattern table with them, giving NaN and a black canvas. Both representations are now handled; frame errors surface in the status line.
83 unmodified lines848586878889902 unmodified lines9394959697989910010116 unmodified lines11811912012112212312439 unmodified lines16416516616716816917083 unmodified linesconst t = performance.now();const out = await exclusive(() => sess.enc.run({ img: tensorFrom(x, [1, 3, SIZE, SIZE]) }));const encMs = Math.round(performance.now() - t);xr = await out.x_r.getData(); xr = F16 ? Float32Array.from(xr, (v) => HALF[v]) : Float32Array.from(xr);feats = FEAT_SHAPES.map((_, i) => out[`f${i}`]); // stay as tensors: they are fed to the renderer every framectx2d.drawImage(c, 0, 0);return encMs;2 unmodified lines// ------------------------------------------------------------ renderingconst imgData = ctx2d.createImageData(SIZE, SIZE);function paint(u) { // decoder output [1,3,512,512] in [-1,1] -> canvasconst n = SIZE * SIZE, d = imgData.data;for (let i = 0; i < n; i++) {const r = F16 ? HALF[u[i]] : u[i], gg = F16 ? HALF[u[n + i]] : u[n + i], b = F16 ? HALF[u[2 * n + i]] : u[2 * n + i];const o = i * 4; d[o] = (r * 0.5 + 0.5) * 255; d[o + 1] = (gg * 0.5 + 0.5) * 255; d[o + 2] = (b * 0.5 + 0.5) * 255; d[o + 3] = 255;}ctx2d.putImageData(imgData, 0, 0);16 unmodified linesasync function loop(my) {let last = performance.now(), t0 = last, n = 0;while (running && my === token) {const r = await frame((performance.now() - t0) / 1000); const now = performance.now(), dt = now - last; last = now; n++;const k = n < 3 ? 1 : 0.1; ema.gpu += k * (r.gpu - ema.gpu); ema.draw += k * (r.draw - ema.draw); ema.tot += k * (dt - ema.tot);if (n === 1 || n % 3 === 0) show();await yieldNow();39 unmodified linesui.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, state: () => ({ F16, ema }) }; // test hookadapterInfo().then((i) => { // check the GPU up front so nobody clicks a portrait and waits just to be told noui.gpu.textContent = `${i.name} 路 shader-f16: ${i.f16 ? 'yes' : 'no'}${i.soft ? ' 路 SOFTWARE adapter, timings are meaningless' : ''}`;status(`Pick a portrait to begin (about ${i.f16 && q.get('fp') !== '32' ? 100 : 185} MB of models, downloaded once).`, 0);83 unmodified lines848586878889902 unmodified lines9394959697989910010110216 unmodified lines11912012112212312412539 unmodified lines16516616716816917017183 unmodified linesconst t = performance.now();const out = await exclusive(() => sess.enc.run({ img: tensorFrom(x, [1, 3, SIZE, SIZE]) }));const encMs = Math.round(performance.now() - t);const xd = await out.x_r.getData(); xr = xd instanceof Uint16Array ? Float32Array.from(xd, (v) => HALF[v]) : Float32Array.from(xd); // fp16 comes back as raw bits (Uint16Array) or, in current Chrome, as a Float16Array of real numbersfeats = FEAT_SHAPES.map((_, i) => out[`f${i}`]); // stay as tensors: they are fed to the renderer every framectx2d.drawImage(c, 0, 0);return encMs;2 unmodified lines// ------------------------------------------------------------ renderingconst imgData = ctx2d.createImageData(SIZE, SIZE);function paint(u) { // decoder output [1,3,512,512] in [-1,1] -> canvasconst n = SIZE * SIZE, d = imgData.data, bits = u instanceof Uint16Array; // raw fp16 bits need the lookup table; Float16Array / Float32Array already hold numbersif (!(u[n >> 1] === u[n >> 1]) && !bits) throw new Error('The GPU returned invalid numbers (NaN). Reload the page with ?fp=32 to use the fp32 models.');for (let i = 0; i < n; i++) {const r = bits ? HALF[u[i]] : u[i], gg = bits ? HALF[u[n + i]] : u[n + i], b = bits ? HALF[u[2 * n + i]] : u[2 * n + i];const o = i * 4; d[o] = (r * 0.5 + 0.5) * 255; d[o + 1] = (gg * 0.5 + 0.5) * 255; d[o + 2] = (b * 0.5 + 0.5) * 255; d[o + 3] = 255;}ctx2d.putImageData(imgData, 0, 0);16 unmodified linesasync function loop(my) {let last = performance.now(), t0 = last, n = 0;while (running && my === token) {let r; try { r = await frame((performance.now() - t0) / 1000); } catch (e) { console.error(e); running = false; ui.pause.textContent = 'Resume'; status(e.message || String(e), null, true); return; } const now = performance.now(), dt = now - last; last = now; n++;const k = n < 3 ? 1 : 0.1; ema.gpu += k * (r.gpu - ema.gpu); ema.draw += k * (r.draw - ema.draw); ema.tot += k * (dt - ema.tot);if (n === 1 || n % 3 === 0) show();await yieldNow();39 unmodified linesui.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 noui.gpu.textContent = `${i.name} 路 shader-f16: ${i.f16 ? 'yes' : 'no'}${i.soft ? ' 路 SOFTWARE adapter, timings are meaningless' : ''}`;status(`Pick a portrait to begin (about ${i.f16 && q.get('fp') !== '32' ? 100 : 185} MB of models, downloaded once).`, 0);
app.js+6-5