Spaces:
Running
Running
mfn commited on
Commit ·
4cd411d
1
Parent(s): ecd8cf0
WebGPU demo
Browse filesFiles changed (4)
- README.md+14-5
- index.html+151-17
- style.cssDeleted-28
- worker.jsAdded+126
12345678910---title: Chitra Tiny Webgpuemoji: 🐨colorFrom: pinkcolorTo: graysdk: staticpinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference12345678910111213141516171819---title: Chitra Tiny WebGPUemoji: 🖼️colorFrom: yellowcolorTo: redsdk: staticpinned: falselicense: apache-2.0short_description: 212M text-to-image model running in your browser (WebGPU)models:- udgamlabs/Chitra-Tiny- mrfakename/Chitra-Tiny-ONNX---Browser port of [udgamlabs/Chitra-Tiny](https://huggingface.co/udgamlabs/Chitra-Tiny), a 212M text-to-image model (256×256).- ONNX graphs from [mrfakename/Chitra-Tiny-ONNX](https://huggingface.co/mrfakename/Chitra-Tiny-ONNX): flan-t5-base text encoder, the Chitra Tiny DiT and the sd-vae-ft-mse decoder. Weights are stored in fp16 and computed in fp32.- Inference runs in a Web Worker with onnxruntime-web on WebGPU; the T5 tokenizer comes from transformers.js. About 745 MB, cached in the browser after the first visit.- Same sampler as the official `chitra_tiny.py` (Euler rectified flow, classifier-free guidance). The noise comes from a seeded JS generator, so a seed gives a different picture than the same seed on the server demo.
12345678910111213141516171819<!doctype html><html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width" /><title>My static Space</title><link rel="stylesheet" href="style.css" /></head><body><div class="card"><h1>Welcome to your static Space!</h1><p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p><p>Also don't forget to check the<a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.</p></div></body></html>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Chitra Tiny WebGPU</title><link rel="icon" href="data:image/svg+xml,%3Csvg xmlns=%27http://www.w3.org/2000/svg%27 viewBox=%270 0 16 16%27%3E%3Crect x=%271.5%27 y=%272.5%27 width=%2713%27 height=%2711%27 rx=%271%27 fill=%27none%27 stroke=%27%23b4432b%27 stroke-width=%271.6%27/%3E%3Cpath d=%27M3 12l3.5-4 2.5 2.5L11 8l2 4z%27 fill=%27%23b4432b%27/%3E%3C/svg%3E"><style>:root{--bg:#f4f0e8;--panel:#fbf9f4;--line:#e3ddd0;--text:#1c1b18;--muted:#6b675e;--accent:#b4432b;--accent-text:#fff;--chip:#ece6da;--tile:#e9e3d6}@media (prefers-color-scheme:dark){:root{--bg:#141311;--panel:#1d1b18;--line:#34302a;--text:#ece8e1;--muted:#a39d92;--accent:#d2593e;--accent-text:#fff;--chip:#27241f;--tile:#24211d}}*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--text);font:15px/1.5 system-ui,-apple-system,"Segoe UI",sans-serif}main{max-width:860px;margin:0 auto;padding:28px 16px 40px}h1{font-size:26px;margin:0 0 6px;letter-spacing:-.01em}p{margin:0 0 10px}a{color:var(--accent)}.muted{color:var(--muted);font-size:14px}.panel{background:var(--panel);border:1px solid var(--line);border-radius:6px;padding:14px;margin-top:16px}label{display:block;font-size:13px;color:var(--muted);margin-bottom:6px}textarea,input[type=text]{width:100%;border:1px solid var(--line);border-radius:4px;background:var(--bg);color:var(--text);font:inherit;padding:8px 10px}textarea{min-height:64px;resize:vertical}input[type=text]{height:36px}textarea:focus,input:focus{outline:2px solid var(--accent);outline-offset:-1px}details{margin-top:12px;border-top:1px solid var(--line);padding-top:10px}summary{cursor:pointer;font-size:14px}.grid{display:grid;grid-template-columns:1fr 1fr;gap:12px;margin-top:10px}.span2{grid-column:1/-1}input[type=number]{width:100%;height:36px;border:1px solid var(--line);border-radius:4px;background:var(--bg);color:var(--text);font:inherit;padding:0 10px}input[type=range]{width:100%;accent-color:var(--accent);height:36px;margin:0}.seedrow{display:flex;gap:10px;align-items:center}.seedrow label{margin:0;display:flex;gap:6px;align-items:center;white-space:nowrap;color:var(--text);font-size:14px}.row{display:flex;gap:10px;align-items:center;margin-top:12px;flex-wrap:wrap}button{height:40px;border:0;border-radius:4px;background:var(--accent);color:var(--accent-text);font:600 15px system-ui,sans-serif;padding:0 22px;cursor:pointer}button:disabled{opacity:.5;cursor:default}.chips{display:flex;flex-wrap:wrap;gap:8px;margin-top:8px}.chip{background:var(--chip);border:1px solid var(--line);color:var(--text);height:auto;min-height:32px;padding:5px 10px;font-weight:400;font-size:13px;text-align:left;max-width:100%}.bar{height:4px;background:var(--line);border-radius:2px;overflow:hidden;margin-top:10px;display:none}.bar>div{height:100%;width:0;background:var(--accent)}#out{display:grid;grid-template-columns:repeat(auto-fill,minmax(190px,1fr));gap:10px}#out a{display:block;aspect-ratio:1;background:var(--tile);border-radius:4px;overflow:hidden}#out img{width:100%;height:100%;display:block;image-rendering:auto}#out .empty{grid-column:1/-1;color:var(--muted);font-size:14px;padding:30px 0;text-align:center}footer{margin-top:28px;font-size:14px}@media (max-width:520px){.grid{grid-template-columns:1fr}}</style></head><body><main><h1>Chitra Tiny</h1><p>Type what you want to see and get small 256×256 pictures, made entirely in your browser. Nothing leaves your device.Best at photos, paintings, landscapes, buildings, vehicles and food; weak at people, text and counting.</p><p class="muted">Model: <a href="https://huggingface.co/udgamlabs/Chitra-Tiny" target="_blank" rel="noopener">udgamlabs/Chitra-Tiny</a> · Server demo: <a href="https://huggingface.co/spaces/mrfakename/chitra-tiny" target="_blank" rel="noopener">mrfakename/chitra-tiny</a></p><div class="panel"><label for="prompt">Prompt</label><textarea id="prompt" maxlength="500">a lighthouse on a rocky coast at sunset</textarea><details><summary>Settings</summary><div class="grid"><div class="span2"><label for="neg">Avoid (optional)</label><input id="neg" type="text" placeholder="e.g. blurry, dark"></div><div><label for="n">Number of pictures: <span id="nv">2</span></label><input id="n" type="range" min="1" max="4" step="1" value="2"></div><div><label for="steps">Steps (fewer is faster): <span id="stepsv">30</span></label><input id="steps" type="range" min="10" max="100" step="5" value="30"></div><div><label for="guid">Prompt strength: <span id="guidv">7</span></label><input id="guid" type="range" min="1" max="12" step="0.5" value="7"></div><div><label for="seed">Seed</label><div class="seedrow"><input id="seed" type="number" min="0" step="1" value="0"><label><input id="rand" type="checkbox" checked> Random</label></div></div></div></details><div class="row"><button id="go" disabled>Loading…</button><span id="status" class="muted"></span></div><div class="bar" id="bar"><div></div></div></div><div class="panel"><label>Pictures</label><div id="out"><div class="empty">Your pictures will show up here.</div></div></div><div class="panel"><label>Try one</label><div class="chips" id="chips"></div></div><footer class="muted">The first visit downloads about 745 MB, then it is saved in your browser. Needs a browser with WebGPU (recent Chrome, Edge or Safari).<br><a href="https://x.com/realmrfakename" target="_blank" rel="noopener">x.com/realmrfakename</a></footer></main><script>const EXAMPLES = ["a lighthouse on a rocky coast at sunset","an oil painting of a vase of flowers","a mountain landscape with snow and pine trees","a watercolor painting of a bird on a branch","a red car parked in front of a house","a bridge over a river in autumn","a black and white photo of a city street","a bowl of soup next to a spoon",];const $ = id => document.getElementById(id);const go = $("go"), status = $("status"), bar = $("bar"), fill = bar.firstElementChild, out = $("out");let state = "probe", urls = [];const worker = new Worker("worker.js", { type: "module" });for (const t of EXAMPLES) {const b = document.createElement("button"); b.className = "chip"; b.textContent = t;b.onclick = () => { $("prompt").value = t; if (state === "ready") generate(); }; $("chips").appendChild(b);}for (const [id, fmt] of [["n", v => v], ["steps", v => v], ["guid", v => Number(v).toString()]])$(id).oninput = e => $(id + "v").textContent = fmt(e.target.value);const progress = f => { bar.style.display = "block"; fill.style.width = (100 * f).toFixed(1) + "%"; };const done = () => { bar.style.display = "none"; fill.style.width = "0"; };function setButton(text, enabled) { go.textContent = text; go.disabled = !enabled; }function load() { state = "loading"; setButton("Loading…", false); status.textContent = "Downloading…"; worker.postMessage({ type: "load" }); }function generate() {if (state !== "ready") return;const prompt = $("prompt").value.trim();if (!prompt) { status.textContent = "Type a prompt first."; return; }if ($("rand").checked) $("seed").value = Math.floor(Math.random() * 2147483647);state = "busy"; setButton("Generating…", false); status.textContent = "Starting…"; progress(0);worker.postMessage({ type: "generate", prompt, negative: $("neg").value.trim(), n: Number($("n").value),steps: Number($("steps").value), guidance: Number($("guid").value), seed: Math.max(0, parseInt($("seed").value) || 0) });}go.onclick = () => state === "idle" ? load() : generate();$("prompt").addEventListener("keydown", e => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); generate(); } });const mb = b => (b / 1e6).toFixed(0);worker.onmessage = ({ data }) => {if (data.type === "probe") {if (!data.webgpu) { state = "none"; setButton("Not supported", false); status.textContent = "This browser has no WebGPU. Try recent Chrome, Edge or Safari, or the server demo."; return; }if (data.cached) load(); else { state = "idle"; setButton("Download model (745 MB)", true); status.textContent = "One-time download, then saved in your browser."; }} else if (data.type === "download") {progress(data.got / data.total); status.textContent = `Downloading… ${mb(data.got)} / ${mb(data.total)} MB`;} else if (data.type === "status") {status.textContent = data.text;} else if (data.type === "ready") {state = "ready"; done(); setButton("Generate", true); status.textContent = "Ready";} else if (data.type === "step") {progress(data.done / data.total); status.textContent = `Step ${data.done} of ${data.total}`;} else if (data.type === "images") {state = "ready"; done(); setButton("Generate", true);urls.forEach(u => URL.revokeObjectURL(u)); urls = data.images.map(b => URL.createObjectURL(b));out.replaceChildren(...urls.map((u, i) => {const a = document.createElement("a"); a.href = u; a.download = `chitra-${data.seed}-${i}.png`; a.title = "Click to save";const img = document.createElement("img"); img.src = u; img.alt = $("prompt").value; a.appendChild(img); return a;}));status.textContent = `Took ${data.took.toFixed(1)} s`;document.body.dataset.done = "1";} else if (data.type === "error") {done();if (data.message === "NO_WEBGPU") { state = "none"; setButton("Not supported", false); status.textContent = "WebGPU is not available here. Try recent Chrome, Edge or Safari, or the server demo."; return; }const wasReady = state === "busy";state = wasReady ? "ready" : "idle"; setButton(wasReady ? "Generate" : "Try again", true);status.textContent = "Something went wrong: " + data.message;document.body.dataset.error = data.message;}};worker.postMessage({ type: "probe" });</script></body></html>
12345678910111213141516171819202122232425262728body { padding: 2rem; font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;}
h1 { font-size: 16px; margin-top: 0;}
p { color: rgb(107, 114, 128); font-size: 15px; margin-bottom: 10px; margin-top: 5px;}
.card { max-width: 620px; margin: 0 auto; padding: 16px; border: 1px solid lightgray; border-radius: 16px;}
.card p:last-child { margin-bottom: 0;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126// Chitra Tiny in the browser: flan-t5-base encoder + DiT + VAE decoder, three ONNX graphs on WebGPU.import * as ort from "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.30.0/dist/ort.webgpu.min.mjs";import { AutoTokenizer, env } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@4.3.1/dist/transformers.min.js";
ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.30.0/dist/";ort.env.logLevel = "error";env.allowLocalModels = false;
const REPO = "mrfakename/Chitra-Tiny-ONNX";const BASE = `https://huggingface.co/${REPO}/resolve/main/onnx/`;const FILES = [["text", "text_encoder.onnx", 219487197], ["dit", "transformer.onnx", 425085665], ["vae", "vae_decoder.onnx", 99180284]];const CACHE = "chitra-tiny-onnx-v1";const TOKENS = 77, G = 32, C = 4;let S = null, tok = null;
async function cached() { if (!self.caches) return false; const c = await caches.open(CACHE); for (const [, f] of FILES) if (!(await c.match(BASE + f))) return false; return true;}
async function fetchModel(file, onBytes) { const url = BASE + file; const c = self.caches ? await caches.open(CACHE) : null; const hit = c && await c.match(url); if (hit) { const b = await hit.arrayBuffer(); onBytes(b.byteLength); return new Uint8Array(b); } const r = await fetch(url); if (!r.ok) throw new Error(`Download failed (${r.status})`); const size = Number(r.headers.get("content-length")) || 0, reader = r.body.getReader(), parts = []; let got = 0; for (;;) { const { done, value } = await reader.read(); if (done) break; parts.push(value); got += value.length; onBytes(value.length); } const buf = new Uint8Array(got); let o = 0; for (const p of parts) { buf.set(p, o); o += p.length; } if (size && got !== size) throw new Error("Download was cut short, try again"); if (c) await c.put(url, new Response(buf, { headers: { "content-type": "application/octet-stream" } })).catch(() => {}); return buf;}
async function load() { if (!navigator.gpu) throw new Error("NO_WEBGPU"); const adapter = await navigator.gpu.requestAdapter().catch(() => null); if (!adapter) throw new Error("NO_WEBGPU"); tok = await AutoTokenizer.from_pretrained(REPO); const total = FILES.reduce((a, f) => a + f[2], 0); let got = 0, last = 0; const onBytes = n => { got += n; const now = performance.now(); if (now - last > 150) { last = now; self.postMessage({ type: "download", got, total }); } }; const bufs = []; for (const [, f] of FILES) bufs.push(await fetchModel(f, onBytes)); self.postMessage({ type: "download", got: total, total }); self.postMessage({ type: "status", text: "Getting ready…" }); S = {}; for (let i = 0; i < FILES.length; i++) { // one at a time: the WebGPU EP dislikes concurrent session creation S[FILES[i][0]] = await ort.InferenceSession.create(bufs[i], { executionProviders: ["webgpu"], graphOptimizationLevel: "all" }); bufs[i] = null; }}
function rng(seed) { // mulberry32 + Box-Muller let a = seed >>> 0; const u = () => { a = (a + 0x6d2b79f5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return (((t ^ (t >>> 14)) >>> 0) + 1) / 4294967297; }; return () => Math.sqrt(-2 * Math.log(u())) * Math.cos(2 * Math.PI * u());}
async function encode(text) { const e = tok(text, { padding: "max_length", max_length: TOKENS, truncation: true }); const ids = BigInt64Array.from(e.input_ids.data, BigInt), am = BigInt64Array.from(e.attention_mask.data, BigInt); const out = await S.text.run({ input_ids: new ort.Tensor("int64", ids, [1, TOKENS]), attention_mask: new ort.Tensor("int64", am, [1, TOKENS]), }); const h = out.last_hidden_state.data.slice(), mask = Uint8Array.from(e.attention_mask.data, Number); out.last_hidden_state.dispose?.(); return { h, mask };}
async function generate({ prompt, negative, n, steps, guidance, seed }) { const t0 = performance.now(); const cond = await encode(prompt), unc = await encode(negative || ""); const D = 768, L = TOKENS * D, B = 2 * n, P = C * G * G; // Batch layout matches chitra_tiny.py: [cond x n, uncond x n]. const txt = new Float32Array(B * L), mask = new Uint8Array(B * TOKENS); for (let i = 0; i < n; i++) { txt.set(cond.h, i * L); txt.set(unc.h, (n + i) * L); mask.set(cond.mask, i * TOKENS); mask.set(unc.mask, (n + i) * TOKENS); } const txtT = new ort.Tensor("float32", txt, [B, TOKENS, D]), maskT = new ort.Tensor("bool", mask, [B, TOKENS]); const g = rng(seed), x = new Float32Array(n * P); for (let i = 0; i < x.length; i++) x[i] = g(); const xx = new Float32Array(B * P), tt = new Float32Array(B); for (let s = 0; s < steps; s++) { const ti = 1 - s / steps, tn = 1 - (s + 1) / steps; xx.set(x, 0); xx.set(x, n * P); tt.fill(ti); const out = await S.dit.run({ x: new ort.Tensor("float32", xx, [B, C, G, G]), t: new ort.Tensor("float32", tt, [B]), txt: txtT, txt_mask: maskT }); const v = out.v.data, dt = tn - ti; for (let i = 0; i < n * P; i++) { const vc = v[i], vu = v[n * P + i]; x[i] += dt * (vu + guidance * (vc - vu)); } out.v.dispose?.(); self.postMessage({ type: "step", done: s + 1, total: steps }); } const images = []; for (let i = 0; i < n; i++) { const out = await S.vae.run({ latent: new ort.Tensor("float32", x.slice(i * P, (i + 1) * P), [1, C, G, G]) }); const im = out.image.data, HW = 256 * 256, rgba = new Uint8ClampedArray(HW * 4); for (let p = 0; p < HW; p++) { rgba[p * 4] = Math.round(im[p] * 127.5 + 127.5); rgba[p * 4 + 1] = Math.round(im[HW + p] * 127.5 + 127.5); rgba[p * 4 + 2] = Math.round(im[2 * HW + p] * 127.5 + 127.5); rgba[p * 4 + 3] = 255; } out.image.dispose?.(); const cv = new OffscreenCanvas(256, 256); cv.getContext("2d").putImageData(new ImageData(rgba, 256, 256), 0, 0); images.push(await cv.convertToBlob({ type: "image/png" })); } return { images, took: (performance.now() - t0) / 1000 };}
self.onmessage = async ({ data }) => { try { if (data.type === "probe") { self.postMessage({ type: "probe", cached: await cached(), webgpu: !!navigator.gpu }); return; } if (data.type === "load") { await load(); self.postMessage({ type: "ready" }); return; } if (data.type === "generate") { const r = await generate(data); self.postMessage({ type: "images", ...r, seed: data.seed }); } } catch (e) { self.postMessage({ type: "error", message: String(e?.message || e) }); }};README.md+14-5
index.html+151-17
style.cssDeleted-28
worker.jsAdded+126