Spaces:
Running
Running
Live mode: regenerate as you type, latest request wins, per-seed cache
Browse files- index.html +25 -7
- jeveration.js +31 -12
- worker.js +22 -3
index.html
CHANGED
|
@@ -40,6 +40,8 @@
|
|
| 40 |
td:last-child { text-align: right; }
|
| 41 |
.note { font-size: .8rem; color: var(--muted); }
|
| 42 |
a { color: var(--accent); }
|
|
|
|
|
|
|
| 43 |
</style>
|
| 44 |
</head>
|
| 45 |
<body>
|
|
@@ -61,6 +63,8 @@
|
|
| 61 |
<button class="secondary" id="rand" type="button">Random</button>
|
| 62 |
<button id="go" disabled>Generate</button>
|
| 63 |
</div>
|
|
|
|
|
|
|
| 64 |
<table id="timings"></table>
|
| 65 |
<p class="note">512 x 512. The first generation includes shader compilation; time the second one.</p>
|
| 66 |
</section>
|
|
@@ -73,7 +77,12 @@
|
|
| 73 |
const files = new Map();
|
| 74 |
const setStatus = (t, err = false) => { $("status").textContent = t; $("status").className = "status" + (err ? " err" : ""); };
|
| 75 |
const fmt = (b) => (b / 1e9).toFixed(2) + " GB";
|
| 76 |
-
let t0 = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
worker.onmessage = ({ data: m }) => {
|
| 78 |
if (m.status === "checked") {
|
| 79 |
$("gpu").textContent = `WebGPU ready: ${m.data.vendor || "GPU"} ${m.data.architecture || ""}`.trim();
|
|
@@ -88,7 +97,8 @@
|
|
| 88 |
} else if (m.status === "ready") {
|
| 89 |
$("bar").style.width = "100%";
|
| 90 |
setStatus(`Ready (loaded in ${m.data.seconds.toFixed(1)} s).`);
|
| 91 |
-
$("go").disabled = false; $("load").disabled = true;
|
|
|
|
| 92 |
} else if (m.status === "image") {
|
| 93 |
const { rgb, timings } = m.data, img = new ImageData(512, 512);
|
| 94 |
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; }
|
|
@@ -96,7 +106,13 @@
|
|
| 96 |
const rows = [["Backbone (Qwen3.5-4B, int8)", timings.backbone], ["Image head", timings.head], ["Sampling", timings.sample],
|
| 97 |
["CLIP + decoder", timings.decode], ["Total", timings.total]];
|
| 98 |
$("timings").innerHTML = rows.map(([k, v]) => `<tr><td>${k}</td><td>${(v / 1000).toFixed(2)} s</td></tr>`).join("");
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
$("go").disabled = false;
|
| 101 |
} else if (m.status === "error") {
|
| 102 |
setStatus("Error: " + m.data, true); $("go").disabled = false;
|
|
@@ -104,10 +120,12 @@
|
|
| 104 |
};
|
| 105 |
$("load").onclick = () => { $("load").disabled = true; setStatus("Starting..."); worker.postMessage({ type: "load" }); };
|
| 106 |
$("rand").onclick = () => { $("seed").value = Math.floor(Math.random() * 1e6); };
|
| 107 |
-
$("go").onclick = () => {
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
| 111 |
worker.postMessage({ type: "check" });
|
| 112 |
worker.onerror = (e) => setStatus("Worker error: " + (e.message || e), true);
|
| 113 |
</script>
|
|
|
|
| 40 |
td:last-child { text-align: right; }
|
| 41 |
.note { font-size: .8rem; color: var(--muted); }
|
| 42 |
a { color: var(--accent); }
|
| 43 |
+
.toggle { display: flex; gap: 8px; align-items: flex-start; font-weight: 500; overflow-wrap: anywhere; }
|
| 44 |
+
.toggle input { flex: 0 0 auto; margin-top: 4px; }
|
| 45 |
</style>
|
| 46 |
</head>
|
| 47 |
<body>
|
|
|
|
| 63 |
<button class="secondary" id="rand" type="button">Random</button>
|
| 64 |
<button id="go" disabled>Generate</button>
|
| 65 |
</div>
|
| 66 |
+
<label class="toggle"><input id="live" type="checkbox" checked> Live: regenerate as you type (same seed, so the image changes smoothly)</label>
|
| 67 |
+
<div class="status" id="live-stats"></div>
|
| 68 |
<table id="timings"></table>
|
| 69 |
<p class="note">512 x 512. The first generation includes shader compilation; time the second one.</p>
|
| 70 |
</section>
|
|
|
|
| 77 |
const files = new Map();
|
| 78 |
const setStatus = (t, err = false) => { $("status").textContent = t; $("status").className = "status" + (err ? " err" : ""); };
|
| 79 |
const fmt = (b) => (b / 1e9).toFixed(2) + " GB";
|
| 80 |
+
let t0 = 0, ready = false, reqId = 0;
|
| 81 |
+
const sent = new Map(), frames = [];
|
| 82 |
+
const request = () => {
|
| 83 |
+
const id = ++reqId; sent.set(id, performance.now()); t0 = performance.now();
|
| 84 |
+
worker.postMessage({ type: "generate", data: { prompt: $("prompt").value, seed: Number($("seed").value) || 0, id } });
|
| 85 |
+
};
|
| 86 |
worker.onmessage = ({ data: m }) => {
|
| 87 |
if (m.status === "checked") {
|
| 88 |
$("gpu").textContent = `WebGPU ready: ${m.data.vendor || "GPU"} ${m.data.architecture || ""}`.trim();
|
|
|
|
| 97 |
} else if (m.status === "ready") {
|
| 98 |
$("bar").style.width = "100%";
|
| 99 |
setStatus(`Ready (loaded in ${m.data.seconds.toFixed(1)} s).`);
|
| 100 |
+
$("go").disabled = false; $("load").disabled = true; ready = true;
|
| 101 |
+
if ($("live").checked) request();
|
| 102 |
} else if (m.status === "image") {
|
| 103 |
const { rgb, timings } = m.data, img = new ImageData(512, 512);
|
| 104 |
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; }
|
|
|
|
| 106 |
const rows = [["Backbone (Qwen3.5-4B, int8)", timings.backbone], ["Image head", timings.head], ["Sampling", timings.sample],
|
| 107 |
["CLIP + decoder", timings.decode], ["Total", timings.total]];
|
| 108 |
$("timings").innerHTML = rows.map(([k, v]) => `<tr><td>${k}</td><td>${(v / 1000).toFixed(2)} s</td></tr>`).join("");
|
| 109 |
+
const now = performance.now(), latency = now - (sent.get(m.data.id) ?? now);
|
| 110 |
+
sent.forEach((_, k) => { if (k <= m.data.id) sent.delete(k); });
|
| 111 |
+
frames.push(now); while (frames.length && now - frames[0] > 5000) frames.shift();
|
| 112 |
+
const fps = frames.length > 1 ? (frames.length - 1) / ((now - frames[0]) / 1000) : 0;
|
| 113 |
+
setStatus(`Done in ${(timings.total / 1000).toFixed(2)} s.`);
|
| 114 |
+
$("live-stats").textContent = `Latency from keystroke to image: ${(latency / 1000).toFixed(2)} s` +
|
| 115 |
+
(fps ? ` · ${fps.toFixed(1)} images/s while typing` : "") + (m.data.skipped ? ` · ${m.data.skipped} stale requests skipped` : "");
|
| 116 |
$("go").disabled = false;
|
| 117 |
} else if (m.status === "error") {
|
| 118 |
setStatus("Error: " + m.data, true); $("go").disabled = false;
|
|
|
|
| 120 |
};
|
| 121 |
$("load").onclick = () => { $("load").disabled = true; setStatus("Starting..."); worker.postMessage({ type: "load" }); };
|
| 122 |
$("rand").onclick = () => { $("seed").value = Math.floor(Math.random() * 1e6); };
|
| 123 |
+
$("go").onclick = () => { $("go").disabled = true; setStatus("Generating..."); request(); };
|
| 124 |
+
const live = () => { if (ready && $("live").checked) request(); };
|
| 125 |
+
$("prompt").addEventListener("input", live);
|
| 126 |
+
$("seed").addEventListener("input", live);
|
| 127 |
+
$("live").addEventListener("change", live);
|
| 128 |
+
$("rand").addEventListener("click", live);
|
| 129 |
worker.postMessage({ type: "check" });
|
| 130 |
worker.onerror = (e) => setStatus("Worker error: " + (e.message || e), true);
|
| 131 |
</script>
|
jeveration.js
CHANGED
|
@@ -89,13 +89,20 @@ export class JeverationPipeline {
|
|
| 89 |
return { state, t: new Float32Array([masked / N_CODES]) };
|
| 90 |
}
|
| 91 |
|
| 92 |
-
/**
|
| 93 |
-
static
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
const codes = new BigInt64Array(N_CODES);
|
| 95 |
for (let i = 0; i < N_CODES; i++) {
|
| 96 |
let best = -Infinity, arg = 0;
|
| 97 |
-
for (let k = 0; k < K; k++) {
|
| 98 |
-
const v = logits[
|
| 99 |
if (v > best) { best = v; arg = k; }
|
| 100 |
}
|
| 101 |
codes[i] = BigInt(arg);
|
|
@@ -103,14 +110,24 @@ export class JeverationPipeline {
|
|
| 103 |
return codes;
|
| 104 |
}
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
/** the backbone pass and the image head: (ids, state, t) -> logits Float32Array [128*8192] */
|
| 107 |
-
async logits(ids, state, t, timings = {}) {
|
| 108 |
const { ort, s } = this, T = (n, d, dims) => new ort.Tensor(n, d, dims);
|
| 109 |
let t0 = performance.now();
|
| 110 |
const emb = (await s.embed.run({ input_ids: T("int64", BigInt64Array.from(ids.map(BigInt)), [1, ids.length]) })).inputs_embeds;
|
| 111 |
-
const tail = (await s.decisionEmbed.run({ state: T("float32", state, [1, N_CODES, CODE_DIM]), t: T("float32", t, [1]) })).tail;
|
| 112 |
const L = ids.length + N_CODES, x = new Float32Array(L * HIDDEN);
|
| 113 |
-
x.set(toF32(emb.data), 0); x.set(tail
|
| 114 |
const pos = new BigInt64Array(3 * L);
|
| 115 |
for (let a = 0; a < 3; a++) for (let i = 0; i < L; i++) pos[a * L + i] = BigInt(i);
|
| 116 |
timings.inputs = performance.now() - t0; t0 = performance.now();
|
|
@@ -141,14 +158,16 @@ export class JeverationPipeline {
|
|
| 141 |
return rgb;
|
| 142 |
}
|
| 143 |
|
| 144 |
-
/** prompt -> {rgb, codes, timings}; one backbone pass
|
|
|
|
| 145 |
async generate(prompt, { seed = 0 } = {}) {
|
| 146 |
-
const
|
| 147 |
const t0 = performance.now();
|
| 148 |
-
const
|
| 149 |
-
|
|
|
|
| 150 |
let t1 = performance.now();
|
| 151 |
-
const codes = JeverationPipeline.sampleCodes(logits,
|
| 152 |
timings.sample = performance.now() - t1;
|
| 153 |
const rgb = await this.decode(codes, prompt, timings);
|
| 154 |
timings.total = performance.now() - t0;
|
|
|
|
| 89 |
return { state, t: new Float32Array([masked / N_CODES]) };
|
| 90 |
}
|
| 91 |
|
| 92 |
+
/** Gumbel noise [128*8192] for sampling all codes: one categorical draw per position is argmax(logits + noise) */
|
| 93 |
+
static gumbel(rng) {
|
| 94 |
+
const g = new Float32Array(N_CODES * K);
|
| 95 |
+
for (let i = 0; i < g.length; i++) g[i] = -Math.log(-Math.log(rng.uniform()));
|
| 96 |
+
return g;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
/** logits [128, 8192] + Gumbel noise -> codes. With the noise held fixed, a small change of the logits keeps most codes. */
|
| 100 |
+
static sampleCodes(logits, noise) {
|
| 101 |
const codes = new BigInt64Array(N_CODES);
|
| 102 |
for (let i = 0; i < N_CODES; i++) {
|
| 103 |
let best = -Infinity, arg = 0;
|
| 104 |
+
for (let k = 0, o = i * K; k < K; k++) {
|
| 105 |
+
const v = logits[o + k] + noise[o + k];
|
| 106 |
if (v > best) { best = v; arg = k; }
|
| 107 |
}
|
| 108 |
codes[i] = BigInt(arg);
|
|
|
|
| 110 |
return codes;
|
| 111 |
}
|
| 112 |
|
| 113 |
+
/** Everything a seed decides, computed once and reused while only the prompt changes: the random state, the
|
| 114 |
+
* decision tokens' input embeddings and the sampling noise. */
|
| 115 |
+
async seedCache(seed) {
|
| 116 |
+
if (this._seed?.seed === seed) return this._seed;
|
| 117 |
+
const rng = new Rng(seed), { state, t } = this.randomState(rng), noise = JeverationPipeline.gumbel(rng);
|
| 118 |
+
const T = (n, d, dims) => new this.ort.Tensor(n, d, dims);
|
| 119 |
+
const tail = toF32((await this.s.decisionEmbed.run({ state: T("float32", state, [1, N_CODES, CODE_DIM]), t: T("float32", t, [1]) })).tail.data);
|
| 120 |
+
return (this._seed = { seed, state, t, noise, tail });
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
/** the backbone pass and the image head: (ids, state, t) -> logits Float32Array [128*8192] */
|
| 124 |
+
async logits(ids, state, t, timings = {}, tailCached = null) {
|
| 125 |
const { ort, s } = this, T = (n, d, dims) => new ort.Tensor(n, d, dims);
|
| 126 |
let t0 = performance.now();
|
| 127 |
const emb = (await s.embed.run({ input_ids: T("int64", BigInt64Array.from(ids.map(BigInt)), [1, ids.length]) })).inputs_embeds;
|
| 128 |
+
const tail = tailCached ?? toF32((await s.decisionEmbed.run({ state: T("float32", state, [1, N_CODES, CODE_DIM]), t: T("float32", t, [1]) })).tail.data);
|
| 129 |
const L = ids.length + N_CODES, x = new Float32Array(L * HIDDEN);
|
| 130 |
+
x.set(toF32(emb.data), 0); x.set(tail, ids.length * HIDDEN);
|
| 131 |
const pos = new BigInt64Array(3 * L);
|
| 132 |
for (let a = 0; a < 3; a++) for (let i = 0; i < L; i++) pos[a * L + i] = BigInt(i);
|
| 133 |
timings.inputs = performance.now() - t0; t0 = performance.now();
|
|
|
|
| 158 |
return rgb;
|
| 159 |
}
|
| 160 |
|
| 161 |
+
/** prompt -> {rgb, codes, timings}; one backbone pass. Same seed = same random state and sampling noise, so editing
|
| 162 |
+
* the prompt changes the image smoothly instead of re-rolling it. */
|
| 163 |
async generate(prompt, { seed = 0 } = {}) {
|
| 164 |
+
const timings = {};
|
| 165 |
const t0 = performance.now();
|
| 166 |
+
const c = await this.seedCache(seed);
|
| 167 |
+
timings.seed = performance.now() - t0;
|
| 168 |
+
const logits = await this.logits(this.promptIds(prompt), c.state, c.t, timings, c.tail);
|
| 169 |
let t1 = performance.now();
|
| 170 |
+
const codes = JeverationPipeline.sampleCodes(logits, c.noise);
|
| 171 |
timings.sample = performance.now() - t1;
|
| 172 |
const rgb = await this.decode(codes, prompt, timings);
|
| 173 |
timings.total = performance.now() - t0;
|
worker.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
// Runs the whole pipeline off the main thread. Protocol: check -> load -> generate, with status/progress messages.
|
|
|
|
|
|
|
| 2 |
import * as ort from "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.31.1/dist/ort.webgpu.bundle.min.mjs";
|
| 3 |
import { HubFiles } from "./hub.js";
|
| 4 |
import { JeverationPipeline } from "./jeveration.js";
|
|
@@ -6,7 +8,7 @@ import { JeverationPipeline } from "./jeveration.js";
|
|
| 6 |
const MODEL_ID = "linoyts/Jeveration-int8-ONNX";
|
| 7 |
ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.31.1/dist/";
|
| 8 |
ort.env.webgpu.powerPreference = "high-performance";
|
| 9 |
-
let pipe = null;
|
| 10 |
|
| 11 |
const post = (m) => self.postMessage(m);
|
| 12 |
|
|
@@ -19,6 +21,22 @@ async function check() {
|
|
| 19 |
return { vendor: info.vendor, architecture: info.architecture, maxBufferSize: adapter.limits.maxBufferSize };
|
| 20 |
}
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
self.onmessage = async ({ data: { type, data } }) => {
|
| 23 |
try {
|
| 24 |
if (type === "check") post({ status: "checked", data: await check() });
|
|
@@ -30,8 +48,9 @@ self.onmessage = async ({ data: { type, data } }) => {
|
|
| 30 |
post({ status: "ready", data: { seconds: (performance.now() - t0) / 1000 } });
|
| 31 |
}
|
| 32 |
if (type === "generate") {
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
}
|
| 36 |
} catch (e) {
|
| 37 |
post({ status: "error", data: String(e?.message ?? e) });
|
|
|
|
| 1 |
// Runs the whole pipeline off the main thread. Protocol: check -> load -> generate, with status/progress messages.
|
| 2 |
+
// Generate requests are "latest wins": while one runs, newer requests replace each other, so live typing never builds
|
| 3 |
+
// a backlog and the image always catches up with the newest prompt.
|
| 4 |
import * as ort from "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.31.1/dist/ort.webgpu.bundle.min.mjs";
|
| 5 |
import { HubFiles } from "./hub.js";
|
| 6 |
import { JeverationPipeline } from "./jeveration.js";
|
|
|
|
| 8 |
const MODEL_ID = "linoyts/Jeveration-int8-ONNX";
|
| 9 |
ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.31.1/dist/";
|
| 10 |
ort.env.webgpu.powerPreference = "high-performance";
|
| 11 |
+
let pipe = null, busy = false, pending = null;
|
| 12 |
|
| 13 |
const post = (m) => self.postMessage(m);
|
| 14 |
|
|
|
|
| 21 |
return { vendor: info.vendor, architecture: info.architecture, maxBufferSize: adapter.limits.maxBufferSize };
|
| 22 |
}
|
| 23 |
|
| 24 |
+
async function drain() {
|
| 25 |
+
if (busy) return;
|
| 26 |
+
busy = true;
|
| 27 |
+
while (pending) {
|
| 28 |
+
const req = pending; pending = null;
|
| 29 |
+
try {
|
| 30 |
+
const out = await pipe.generate(req.prompt, { seed: req.seed });
|
| 31 |
+
post({ status: "image", data: { rgb: out.rgb, timings: out.timings, seed: req.seed, prompt: req.prompt, id: req.id, skipped: req.skipped } });
|
| 32 |
+
} catch (e) {
|
| 33 |
+
post({ status: "error", data: String(e?.message ?? e) });
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
busy = false;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
let skipped = 0;
|
| 40 |
self.onmessage = async ({ data: { type, data } }) => {
|
| 41 |
try {
|
| 42 |
if (type === "check") post({ status: "checked", data: await check() });
|
|
|
|
| 48 |
post({ status: "ready", data: { seconds: (performance.now() - t0) / 1000 } });
|
| 49 |
}
|
| 50 |
if (type === "generate") {
|
| 51 |
+
if (pending) skipped++;
|
| 52 |
+
pending = { ...data, skipped };
|
| 53 |
+
drain();
|
| 54 |
}
|
| 55 |
} catch (e) {
|
| 56 |
post({ status: "error", data: String(e?.message ?? e) });
|