Spaces:
Running
Running
Live mode: regenerate as you type, latest request wins, per-seed cache
Browse filesFiles changed (3)
- index.html+25-7
- jeveration.js+31-12
- worker.js+22-3
39 unmodified lines404142434445464715 unmodified lines63646566676869706 unmodified lines777879768081828384858687888 unmodified lines979899911001011021031041 unmodified line106107108109110111112991131141151161171181 unmodified line12012112210710810911012312412512612712812913013139 unmodified lines td:last-child { text-align: right; } .note { font-size: .8rem; color: var(--muted); } a { color: var(--accent); } .toggle { display: flex; gap: 8px; align-items: flex-start; font-weight: 500; overflow-wrap: anywhere; } .toggle input { flex: 0 0 auto; margin-top: 4px; }</style></head><body>15 unmodified lines <button class="secondary" id="rand" type="button">Random</button> <button id="go" disabled>Generate</button> </div> <label class="toggle"><input id="live" type="checkbox" checked> Live: regenerate as you type (same seed, so the image changes smoothly)</label> <div class="status" id="live-stats"></div> <table id="timings"></table> <p class="note">512 x 512. The first generation includes shader compilation; time the second one.</p> </section>6 unmodified lines const files = new Map(); const setStatus = (t, err = false) => { $("status").textContent = t; $("status").className = "status" + (err ? " err" : ""); }; const fmt = (b) => (b / 1e9).toFixed(2) + " GB"; let t0 = 0; let t0 = 0, ready = false, reqId = 0; const sent = new Map(), frames = []; const request = () => { const id = ++reqId; sent.set(id, performance.now()); t0 = performance.now(); worker.postMessage({ type: "generate", data: { prompt: $("prompt").value, seed: Number($("seed").value) || 0, id } }); }; worker.onmessage = ({ data: m }) => { if (m.status === "checked") { $("gpu").textContent = `WebGPU ready: ${m.data.vendor || "GPU"} ${m.data.architecture || ""}`.trim();8 unmodified lines } else if (m.status === "ready") { $("bar").style.width = "100%"; setStatus(`Ready (loaded in ${m.data.seconds.toFixed(1)} s).`); $("go").disabled = false; $("load").disabled = true; $("go").disabled = false; $("load").disabled = true; ready = true; if ($("live").checked) request(); } else if (m.status === "image") { const { rgb, timings } = m.data, img = new ImageData(512, 512); for (let i = 0; i < 512 * 512; i++) { img.data[4 * i] = rgb[3 * i]; img.data[4 * i + 1] = rgb[3 * i + 1]; img.data[4 * i + 2] = rgb[3 * i + 2]; img.data[4 * i + 3] = 255; }1 unmodified line const rows = [["Backbone (Qwen3.5-4B, int8)", timings.backbone], ["Image head", timings.head], ["Sampling", timings.sample], ["CLIP + decoder", timings.decode], ["Total", timings.total]]; $("timings").innerHTML = rows.map(([k, v]) => `<tr><td>${k}</td><td>${(v / 1000).toFixed(2)} s</td></tr>`).join(""); const now = performance.now(), latency = now - (sent.get(m.data.id) ?? now); sent.forEach((_, k) => { if (k <= m.data.id) sent.delete(k); }); frames.push(now); while (frames.length && now - frames[0] > 5000) frames.shift(); const fps = frames.length > 1 ? (frames.length - 1) / ((now - frames[0]) / 1000) : 0; setStatus(`Done in ${(timings.total / 1000).toFixed(2)} s (wall ${((performance.now() - t0) / 1000).toFixed(2)} s).`); setStatus(`Done in ${(timings.total / 1000).toFixed(2)} s.`); $("live-stats").textContent = `Latency from keystroke to image: ${(latency / 1000).toFixed(2)} s` + (fps ? ` · ${fps.toFixed(1)} images/s while typing` : "") + (m.data.skipped ? ` · ${m.data.skipped} stale requests skipped` : ""); $("go").disabled = false; } else if (m.status === "error") { setStatus("Error: " + m.data, true); $("go").disabled = false;1 unmodified line }; $("load").onclick = () => { $("load").disabled = true; setStatus("Starting..."); worker.postMessage({ type: "load" }); }; $("rand").onclick = () => { $("seed").value = Math.floor(Math.random() * 1e6); }; $("go").onclick = () => { $("go").disabled = true; t0 = performance.now(); setStatus("Generating..."); worker.postMessage({ type: "generate", data: { prompt: $("prompt").value, seed: Number($("seed").value) || 0 } }); }; $("go").onclick = () => { $("go").disabled = true; setStatus("Generating..."); request(); }; const live = () => { if (ready && $("live").checked) request(); }; $("prompt").addEventListener("input", live); $("seed").addEventListener("input", live); $("live").addEventListener("change", live); $("rand").addEventListener("click", live); worker.postMessage({ type: "check" }); worker.onerror = (e) => setStatus("Worker error: " + (e.message || e), true);</script>88 unmodified lines8990919293929394959697989910010110210397981041051061071081 unmodified line11011111211311411511611711811912012112212310712412512612711112812911313013113213324 unmodified lines15815916014416116216314616416514814916616716816915117017117217388 unmodified lines return { state, t: new Float32Array([masked / N_CODES]) }; }
/** logits [128, 8192] -> codes, one categorical draw per position (Gumbel-max) */ static sampleCodes(logits, rng) { /** Gumbel noise [128*8192] for sampling all codes: one categorical draw per position is argmax(logits + noise) */ static gumbel(rng) { const g = new Float32Array(N_CODES * K); for (let i = 0; i < g.length; i++) g[i] = -Math.log(-Math.log(rng.uniform())); return g; }
/** logits [128, 8192] + Gumbel noise -> codes. With the noise held fixed, a small change of the logits keeps most codes. */ static sampleCodes(logits, noise) { const codes = new BigInt64Array(N_CODES); for (let i = 0; i < N_CODES; i++) { let best = -Infinity, arg = 0; for (let k = 0; k < K; k++) { const v = logits[i * K + k] - Math.log(-Math.log(rng.uniform())); for (let k = 0, o = i * K; k < K; k++) { const v = logits[o + k] + noise[o + k]; if (v > best) { best = v; arg = k; } } codes[i] = BigInt(arg);1 unmodified line return codes; }
/** Everything a seed decides, computed once and reused while only the prompt changes: the random state, the * decision tokens' input embeddings and the sampling noise. */ async seedCache(seed) { if (this._seed?.seed === seed) return this._seed; const rng = new Rng(seed), { state, t } = this.randomState(rng), noise = JeverationPipeline.gumbel(rng); const T = (n, d, dims) => new this.ort.Tensor(n, d, dims); const tail = toF32((await this.s.decisionEmbed.run({ state: T("float32", state, [1, N_CODES, CODE_DIM]), t: T("float32", t, [1]) })).tail.data); return (this._seed = { seed, state, t, noise, tail }); }
/** the backbone pass and the image head: (ids, state, t) -> logits Float32Array [128*8192] */ async logits(ids, state, t, timings = {}) { async logits(ids, state, t, timings = {}, tailCached = null) { const { ort, s } = this, T = (n, d, dims) => new ort.Tensor(n, d, dims); let t0 = performance.now(); const emb = (await s.embed.run({ input_ids: T("int64", BigInt64Array.from(ids.map(BigInt)), [1, ids.length]) })).inputs_embeds; const tail = (await s.decisionEmbed.run({ state: T("float32", state, [1, N_CODES, CODE_DIM]), t: T("float32", t, [1]) })).tail; const tail = tailCached ?? toF32((await s.decisionEmbed.run({ state: T("float32", state, [1, N_CODES, CODE_DIM]), t: T("float32", t, [1]) })).tail.data); const L = ids.length + N_CODES, x = new Float32Array(L * HIDDEN); x.set(toF32(emb.data), 0); x.set(tail.data, ids.length * HIDDEN); x.set(toF32(emb.data), 0); x.set(tail, ids.length * HIDDEN); const pos = new BigInt64Array(3 * L); for (let a = 0; a < 3; a++) for (let i = 0; i < L; i++) pos[a * L + i] = BigInt(i); timings.inputs = performance.now() - t0; t0 = performance.now();24 unmodified lines return rgb; }
/** prompt -> {rgb, codes, timings}; one backbone pass */ /** prompt -> {rgb, codes, timings}; one backbone pass. Same seed = same random state and sampling noise, so editing * the prompt changes the image smoothly instead of re-rolling it. */ async generate(prompt, { seed = 0 } = {}) { const rng = new Rng(seed), timings = {}; const timings = {}; const t0 = performance.now(); const { state, t } = this.randomState(rng); const logits = await this.logits(this.promptIds(prompt), state, t, timings); const c = await this.seedCache(seed); timings.seed = performance.now() - t0; const logits = await this.logits(this.promptIds(prompt), c.state, c.t, timings, c.tail); let t1 = performance.now(); const codes = JeverationPipeline.sampleCodes(logits, rng); const codes = JeverationPipeline.sampleCodes(logits, c.noise); timings.sample = performance.now() - t1; const rgb = await this.decode(codes, prompt, timings); timings.total = performance.now() - t0;1234561 unmodified line89109111213146 unmodified lines212223242526272829303132333435363738394041425 unmodified lines4849503334515253545556// Runs the whole pipeline off the main thread. Protocol: check -> load -> generate, with status/progress messages.// Generate requests are "latest wins": while one runs, newer requests replace each other, so live typing never builds// a backlog and the image always catches up with the newest prompt.import * as ort from "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.31.1/dist/ort.webgpu.bundle.min.mjs";import { HubFiles } from "./hub.js";import { JeverationPipeline } from "./jeveration.js";1 unmodified lineconst MODEL_ID = "linoyts/Jeveration-int8-ONNX";ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.31.1/dist/";ort.env.webgpu.powerPreference = "high-performance";let pipe = null;let pipe = null, busy = false, pending = null;
const post = (m) => self.postMessage(m);
6 unmodified lines return { vendor: info.vendor, architecture: info.architecture, maxBufferSize: adapter.limits.maxBufferSize };}
async function drain() { if (busy) return; busy = true; while (pending) { const req = pending; pending = null; try { const out = await pipe.generate(req.prompt, { seed: req.seed }); post({ status: "image", data: { rgb: out.rgb, timings: out.timings, seed: req.seed, prompt: req.prompt, id: req.id, skipped: req.skipped } }); } catch (e) { post({ status: "error", data: String(e?.message ?? e) }); } } busy = false;}
let skipped = 0;self.onmessage = async ({ data: { type, data } }) => { try { if (type === "check") post({ status: "checked", data: await check() });5 unmodified lines post({ status: "ready", data: { seconds: (performance.now() - t0) / 1000 } }); } if (type === "generate") { const out = await pipe.generate(data.prompt, { seed: data.seed }); post({ status: "image", data: { rgb: out.rgb, timings: out.timings, seed: data.seed } }, [out.rgb.buffer]); if (pending) skipped++; pending = { ...data, skipped }; drain(); } } catch (e) { post({ status: "error", data: String(e?.message ?? e) });index.html+25-7
jeveration.js+31-12
worker.js+22-3