Download index.html from StrangerInAStrangeLand/qwen3-embedding-benchmark: direct link, hf CLI and curl.
- Browser
- Download file 22.8 kB
-
https://huggingface.co/spaces/StrangerInAStrangeLand/qwen3-embedding-benchmark/resolve/main/index.html
- Command line
-
hf download hf://spaces/StrangerInAStrangeLand/qwen3-embedding-benchmark/index.html
-
curl -L -o index.html https://huggingface.co/spaces/StrangerInAStrangeLand/qwen3-embedding-benchmark/resolve/main/index.html
22.8 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | |
| <title>Qwen3 Embedding — Speed Test</title> | |
| <style> | |
| :root{ | |
| --bg:#0b0d14; --panel:#141824; --panel2:#1b2030; --line:#2a3042; | |
| --fg:#e8ebf2; --muted:#96a0b5; --accent:#8b7cf6; --accent2:#5cc8ff; | |
| --ok:#3ddc97; --warn:#ffb454; --err:#ff6b6b; | |
| } | |
| *{box-sizing:border-box} | |
| body{ | |
| margin:0; background:radial-gradient(1200px 600px at 70% -10%,#1d2140 0%,var(--bg) 60%); | |
| color:var(--fg); font:15px/1.5 ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,sans-serif; | |
| min-height:100vh; | |
| } | |
| .wrap{max-width:1180px;margin:0 auto;padding:22px 18px 60px} | |
| header h1{margin:0 0 4px;font-size:24px;letter-spacing:.2px} | |
| header p{margin:0;color:var(--muted);font-size:13.5px} | |
| header a{color:var(--accent2);text-decoration:none} | |
| header a:hover{text-decoration:underline} | |
| .statusbar{ | |
| display:flex;flex-wrap:wrap;gap:10px;align-items:center; | |
| margin:16px 0 14px;padding:10px 14px;background:var(--panel); | |
| border:1px solid var(--line);border-radius:12px;font-size:13px; | |
| } | |
| .dot{width:9px;height:9px;border-radius:50%;background:var(--warn);flex:0 0 auto} | |
| .dot.ok{background:var(--ok)} .dot.err{background:var(--err)} | |
| #status{color:var(--muted)} | |
| .badge{ | |
| margin-left:auto;padding:3px 10px;border-radius:999px;font-size:12px; | |
| background:var(--panel2);border:1px solid var(--line);color:var(--muted) | |
| } | |
| .badge b{color:var(--fg);font-weight:600} | |
| .progress{height:4px;background:var(--panel2);border-radius:99px;overflow:hidden;margin-top:10px;display:none} | |
| .progress.on{display:block} | |
| .progress i{display:block;height:100%;width:0;background:linear-gradient(90deg,var(--accent),var(--accent2));transition:width .25s} | |
| .tabs{display:flex;gap:6px;flex-wrap:wrap;margin-bottom:14px} | |
| .tab{ | |
| padding:8px 16px;border-radius:10px;border:1px solid var(--line);background:var(--panel); | |
| color:var(--muted);cursor:pointer;font-size:14px;font-weight:500; | |
| } | |
| .tab:hover{color:var(--fg);border-color:#3a4258} | |
| .tab.active{background:linear-gradient(180deg,#2b2f52,#222642);color:#fff;border-color:#414a6e} | |
| .panel{display:none} | |
| .panel.active{display:block} | |
| .card{background:var(--panel);border:1px solid var(--line);border-radius:14px;padding:18px;margin-bottom:14px} | |
| .card h3{margin:0 0 12px;font-size:15px;font-weight:600} | |
| .card h3 small{color:var(--muted);font-weight:400} | |
| label{display:block;font-size:12.5px;color:var(--muted);margin:10px 0 5px} | |
| textarea,input[type=text],input[type=number],select{ | |
| width:100%;background:var(--panel2);color:var(--fg);border:1px solid var(--line); | |
| border-radius:9px;padding:9px 11px;font:14px/1.45 ui-monospace,SFMono-Regular,Menlo,monospace; | |
| resize:vertical;outline:none; | |
| } | |
| textarea:focus,input:focus,select:focus{border-color:var(--accent)} | |
| textarea{min-height:96px} | |
| .row{display:flex;gap:12px;flex-wrap:wrap;align-items:flex-end} | |
| .row>div{flex:1;min-width:150px} | |
| button.go{ | |
| margin-top:12px;padding:10px 20px;border:0;border-radius:10px;cursor:pointer; | |
| background:linear-gradient(180deg,#8b7cf6,#6f5fe0);color:#fff;font-size:14.5px;font-weight:600; | |
| } | |
| button.go:hover{filter:brightness(1.08)} | |
| button.go:disabled{background:#333a4d;color:#7b8499;cursor:not-allowed;filter:none} | |
| button.ghost{ | |
| padding:8px 14px;border-radius:9px;border:1px solid var(--line);background:var(--panel2); | |
| color:var(--muted);cursor:pointer;font-size:13px;margin-top:12px | |
| } | |
| button.ghost:hover{color:var(--fg)} | |
| .out{margin-top:14px;font:13px/1.55 ui-monospace,SFMono-Regular,Menlo,monospace;color:var(--fg)} | |
| .out .k{color:var(--muted)} | |
| .kpis{display:flex;gap:10px;flex-wrap:wrap;margin-top:12px} | |
| .kpi{flex:1;min-width:130px;background:var(--panel2);border:1px solid var(--line);border-radius:11px;padding:11px 13px} | |
| .kpi .v{font-size:20px;font-weight:700;letter-spacing:.3px} | |
| .kpi .l{font-size:11.5px;color:var(--muted);margin-top:2px} | |
| .kpi.good .v{color:var(--ok)} | |
| .kpi.hot .v{color:var(--accent2)} | |
| .verdict{margin-top:14px;padding:12px 14px;border-radius:11px;border:1px solid var(--line);background:var(--panel2);font-size:13.5px} | |
| .verdict b{color:var(--ok)} | |
| .chips{display:flex;flex-wrap:wrap;gap:8px;margin-top:8px} | |
| .chip{padding:5px 11px;border-radius:999px;background:var(--panel2);border:1px solid var(--line);font-size:12.5px;color:var(--muted);cursor:pointer} | |
| .chip:hover{color:var(--fg);border-color:var(--accent)} | |
| .rank{margin-top:12px;border:1px solid var(--line);border-radius:11px;overflow:hidden} | |
| .rank .it{display:flex;gap:12px;align-items:center;padding:9px 12px;border-bottom:1px solid var(--line);font-size:13.5px} | |
| .rank .it:last-child{border-bottom:0} | |
| .rank .bar{flex:0 0 74px;height:8px;border-radius:99px;background:var(--panel2);overflow:hidden} | |
| .rank .bar i{display:block;height:100%;background:linear-gradient(90deg,var(--accent),var(--accent2))} | |
| .rank .sc{flex:0 0 60px;text-align:right;font-variant-numeric:tabular-nums;color:var(--accent2);font-weight:600} | |
| .rank .tx{flex:1;color:var(--fg);overflow:hidden;text-overflow:ellipsis;white-space:nowrap} | |
| .errbox{color:var(--err)} | |
| footer{margin-top:26px;color:var(--muted);font-size:12.5px;text-align:center} | |
| </style> | |
| </head> | |
| <body> | |
| <div class="wrap"> | |
| <header> | |
| <h1>⚡ Qwen3 Embedding — Speed Test</h1> | |
| <p> | |
| <b>Qwen/Qwen3-Embedding-0.6B</b> running fully in your browser via transformers.js + ONNX WebGPU. | |
| No server, no GPU quota — inference happens on your device. | |
| · <a href="https://huggingface.co/Qwen/Qwen3-Embedding-0.6B" target="_blank" rel="noopener">model card</a> | |
| </p> | |
| </header> | |
| <div class="statusbar"> | |
| <span class="dot" id="dot"></span> | |
| <span id="status">Starting…</span> | |
| <span class="badge" id="deviceBadge">device <b>—</b></span> | |
| </div> | |
| <div class="progress" id="progWrap"><i id="progBar"></i></div> | |
| <div class="tabs"> | |
| <div class="tab active" data-tab="bench">Benchmark</div> | |
| <div class="tab" data-tab="encode">Encode</div> | |
| <div class="tab" data-tab="compare">Compare vs CPU</div> | |
| <div class="tab" data-tab="sim">Similarity</div> | |
| </div> | |
| <!-- ============ BENCHMARK ============ --> | |
| <div class="panel active" id="p-bench"> | |
| <div class="card"> | |
| <h3>Measure this device <small>— real numbers, measured live</small></h3> | |
| <div class="row"> | |
| <div> | |
| <label for="bCount">Texts per batch (latency sweep)</label> | |
| <input type="number" id="bCount" value="16" min="1" max="256" /> | |
| </div> | |
| <div> | |
| <label for="bReps">Repeat each measurement</label> | |
| <input type="number" id="bReps" value="3" min="1" max="20" /> | |
| </div> | |
| </div> | |
| <button class="go" id="benchBtn" disabled>Run benchmark</button> | |
| <button class="ghost" id="lenBtn" disabled>Measure latency vs text length</button> | |
| </div> | |
| <div class="card" id="benchResultCard" style="display:none"> | |
| <h3>Results</h3> | |
| <div class="kpis" id="benchKpis"></div> | |
| <div class="out" id="benchOut"></div> | |
| </div> | |
| </div> | |
| <!-- ============ ENCODE ============ --> | |
| <div class="panel" id="p-encode"> | |
| <div class="card"> | |
| <h3>Encode a single text</h3> | |
| <label for="eText">Text</label> | |
| <textarea id="eText">The quick brown fox jumps over the lazy dog.</textarea> | |
| <label for="eMode">Mode</label> | |
| <select id="eMode"> | |
| <option value="query">Query (applies the Instruct/Query prefix)</option> | |
| <option value="doc">Document (plain text, no prefix)</option> | |
| </select> | |
| <button class="go" id="encodeBtn" disabled>Embed</button> | |
| <div class="out" id="encodeOut"></div> | |
| </div> | |
| </div> | |
| <!-- ============ COMPARE ============ --> | |
| <div class="panel" id="p-compare"> | |
| <div class="card"> | |
| <h3>This device <small>vs.</small> the CPU server baseline</h3> | |
| <p style="color:var(--muted);font-size:13px;margin:0 0 6px"> | |
| The same model weights (Qwen3-Embedding-0.6B, 1024-dim) served from a 2-vCPU server on CPU. | |
| Enter that baseline to compute the speedup. Defaults are the measured figures from | |
| <code>psychoguineapig</code> on 2026-10-09. | |
| </p> | |
| <div class="row"> | |
| <div> | |
| <label for="cWarm">CPU baseline — warm latency (ms / embedding)</label> | |
| <input type="number" id="cWarm" value="43054" min="1" /> | |
| </div> | |
| <div> | |
| <label for="cCold">CPU baseline — cold latency (ms / embedding)</label> | |
| <input type="number" id="cCold" value="122822" min="1" /> | |
| </div> | |
| </div> | |
| <button class="go" id="compareBtn" disabled>Measure this device & compare</button> | |
| <div class="kpis" id="cmpKpis"></div> | |
| <div class="verdict" id="cmpVerdict" style="display:none"></div> | |
| </div> | |
| </div> | |
| <!-- ============ SIMILARITY ============ --> | |
| <div class="panel" id="p-sim"> | |
| <div class="card"> | |
| <h3>Rank candidates against a query</h3> | |
| <label for="sQuery">Query</label> | |
| <textarea id="sQuery" style="min-height:56px">What is the capital of China?</textarea> | |
| <label for="sDocs">Candidates — one per line</label> | |
| <textarea id="sDocs" style="min-height:120px">The capital of China is Beijing. | |
| Gravity is a force that attracts two bodies towards each other. | |
| Beijing is the political and cultural centre of the country. | |
| Paris is the capital of France.</textarea> | |
| <button class="go" id="simBtn" disabled>Rank by similarity</button> | |
| <div class="rank" id="simRank" style="display:none"></div> | |
| </div> | |
| </div> | |
| <footer> | |
| Weights: <a href="https://huggingface.co/onnx-community/Qwen3-Embedding-0.6B-ONNX" target="_blank" rel="noopener">onnx-community/Qwen3-Embedding-0.6B-ONNX</a> | |
| · quantised q4f16 · embeddings never leave your machine. | |
| </footer> | |
| </div> | |
| <script type="module"> | |
| import { pipeline, env } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@4.3.1"; | |
| const TFJS_URL = "https://cdn.jsdelivr.net/npm/@huggingface/transformers@4.3.1"; | |
| const MODEL_ID = "onnx-community/Qwen3-Embedding-0.6B-ONNX"; | |
| const DIM = 1024; | |
| const INSTRUCT = "Given a web search query, retrieve relevant passages that answer the query"; | |
| env.allowLocalModels = false; // always fetch from the Hub / browser cache | |
| env.useBrowserCache = true; | |
| const $ = (id) => document.getElementById(id); | |
| const statusEl = $("status"), dotEl = $("dot"), progWrap = $("progWrap"), progBar = $("progBar"); | |
| const deviceBadge = $("deviceBadge"); | |
| let extractor = null; | |
| const buttons = () => ["benchBtn","lenBtn","encodeBtn","compareBtn","simBtn"]; | |
| function setStatus(msg, kind="warn"){ | |
| statusEl.textContent = msg; | |
| dotEl.className = "dot" + (kind === "ok" ? " ok" : kind === "err" ? " err" : ""); | |
| } | |
| function setReady(on){ buttons().forEach(id => { const b = $(id); if (b) b.disabled = !on; }); } | |
| function fmt(n, d=1){ return Number(n).toLocaleString(undefined,{minimumFractionDigits:d,maximumFractionDigits:d}); } | |
| function pct(x){ return (x*100).toFixed(0) + "%"; } | |
| function esc(s){ return s.replace(/[&<>"]/g, c => ({'&':'&','<':'<','>':'>','"':'"'}[c])); } | |
| /* ---------- model load ---------- */ | |
| async function load(){ | |
| progWrap.classList.add("on"); | |
| const onProgress = (p) => { | |
| if (p.status === "progress" && p.total){ | |
| const pctv = (p.loaded / p.total) * 100; | |
| progBar.style.width = pctv.toFixed(1) + "%"; | |
| setStatus(`Downloading ${p.file ?? "weights"} — ${fmt(pctv,0)}%`); | |
| } else if (p.status === "ready"){ | |
| setStatus("Initialising model…"); | |
| } | |
| }; | |
| // --- Probe WebGPU FIRST. ONNX Runtime registers its webgpu execution provider | |
| // eagerly, and if it is present but cannot get an adapter, that state poisons | |
| // the module for every later pipeline() call — even with device:"wasm". | |
| // So we never let transformers.js touch WebGPU unless an adapter is provable. | |
| let webgpuOK = false; | |
| if (typeof navigator !== "undefined" && "gpu" in navigator){ | |
| try{ | |
| setStatus("Checking for a WebGPU adapter…"); | |
| const adapter = await navigator.gpu.requestAdapter(); | |
| webgpuOK = !!adapter; | |
| }catch(e){ webgpuOK = false; } | |
| } | |
| const plan = webgpuOK | |
| ? { device:"webgpu", dtype:"q4f16", label:"WebGPU · q4f16" } | |
| : { device:"wasm", dtype:"q8", label:"WASM (CPU) · q8" }; | |
| const progressArg = { progress_callback: onProgress }; | |
| try{ | |
| setStatus(`Loading ${plan.label}…`); | |
| extractor = await pipeline("feature-extraction", MODEL_ID, { ...plan, ...progressArg }); | |
| }catch(e){ | |
| // If WebGPU was chosen but failed anyway, retry on a *fresh* module instance so | |
| // the poisoned webgpu provider state is not inherited. | |
| if (plan.device === "webgpu"){ | |
| console.warn("webgpu attempt failed, retrying on wasm via fresh module:", e); | |
| try{ | |
| setStatus("WebGPU failed — retrying on CPU (WASM)…"); | |
| const fresh = await import(TFJS_URL + "?fallback=" + Date.now()); | |
| extractor = await fresh.pipeline("feature-extraction", MODEL_ID, | |
| { device:"wasm", dtype:"q8", ...progressArg }); | |
| webgpuOK = false; | |
| }catch(e2){ fail(e2); return; } | |
| } else { fail(e); return; } | |
| } | |
| progBar.style.width = "100%"; | |
| deviceBadge.innerHTML = `device <b>${webgpuOK ? "WebGPU · q4f16" : "WASM (CPU) · q8"}</b>`; | |
| setStatus(webgpuOK ? "Model ready (GPU)." : "Model ready — no WebGPU adapter, using CPU (WASM).", "ok"); | |
| setReady(true); | |
| setTimeout(() => progWrap.classList.remove("on"), 800); | |
| function fail(err){ | |
| setReady(false); | |
| progWrap.classList.remove("on"); | |
| deviceBadge.innerHTML = `device <b>unavailable</b>`; | |
| setStatus("Could not load the model: " + (err?.message || err), "err"); | |
| console.error(err); | |
| } | |
| } | |
| /* ---------- embedding ---------- */ | |
| /* | |
| * Qwen3-Embedding uses LAST-TOKEN pooling + L2 normalisation. | |
| * Queries are prefixed with "Instruct: <task>\nQuery: <q>"; documents are embedded plain. | |
| */ | |
| async function embed(texts, { isQuery = false, task = INSTRUCT } = {}){ | |
| const payload = isQuery ? texts.map(t => `Instruct: ${task}\nQuery: ${t}`) : texts; | |
| const t0 = performance.now(); | |
| const out = await extractor(payload, { pooling: "last_token", normalize: true }); | |
| const ms = performance.now() - t0; | |
| const vectors = out.tolist(); | |
| return { vectors, ms }; | |
| } | |
| function dot(a, b){ let s = 0; for (let i = 0; i < a.length; i++) s += a[i] * b[i]; return s; } | |
| function norm(a){ return Math.sqrt(dot(a, a)); } | |
| /* ---------- tabs ---------- */ | |
| document.querySelectorAll(".tab").forEach(t => { | |
| t.addEventListener("click", () => { | |
| document.querySelectorAll(".tab").forEach(x => x.classList.remove("active")); | |
| document.querySelectorAll(".panel").forEach(x => x.classList.remove("active")); | |
| t.classList.add("active"); | |
| $("p-" + t.dataset.tab).classList.add("active"); | |
| }); | |
| }); | |
| /* ---------- benchmark ---------- */ | |
| function kpi(v, l, cls=""){ return `<div class="kpi ${cls}"><div class="v">${v}</div><div class="l">${l}</div></div>`; } | |
| async function runBenchmark(){ | |
| const n = Math.max(1, Math.min(256, parseInt($("bCount").value) || 16)); | |
| const reps = Math.max(1, Math.min(20, parseInt($("bReps").value) || 3)); | |
| $("benchBtn").disabled = true; | |
| const base = "Retrieval-augmented generation grounds a language model in retrieved documents so its answers stay factual and current."; | |
| const batch = Array.from({ length: n }, (_, i) => `${base} (sample ${i + 1})`); | |
| try{ | |
| // warm-up: the first call after load pays one-off graph/allocator setup | |
| setStatus("Warming up…"); | |
| await embed(["warm up"], { isQuery: false }); | |
| setStatus(`Measuring single-embed latency (${reps}×)…`); | |
| const single = []; | |
| for (let i = 0; i < reps; i++){ | |
| const { ms } = await embed(["A single sentence about retrieval quality."], { isQuery: false }); | |
| single.push(ms); | |
| } | |
| setStatus(`Measuring batch of ${n} (${reps}×)…`); | |
| const batches = []; | |
| for (let i = 0; i < reps; i++){ | |
| const { ms } = await embed(batch, { isQuery: false }); | |
| batches.push(ms); | |
| } | |
| const srt = [...single].sort((a,b) => a-b); | |
| const med = srt[Math.floor(srt.length / 2)]; | |
| const mean = single.reduce((a,b) => a+b, 0) / single.length; | |
| const bMed = [...batches].sort((a,b) => a-b)[Math.floor(batches.length / 2)]; | |
| const perItem = bMed / n; | |
| const thr = (n / bMed) * 1000; | |
| $("benchKpis").innerHTML = | |
| kpi(fmt(med) + " ms", "warm single-embed (median)", "hot") + | |
| kpi(fmt(mean) + " ms", "warm single-embed (mean)") + | |
| kpi(fmt(perItem, 2) + " ms", `per item, batch of ${n}`) + | |
| kpi(fmt(thr, 1), "embeddings / second", "good"); | |
| $("benchOut").innerHTML = | |
| `<div><span class="k">single-embed samples:</span> ${single.map(x => fmt(x)).join(" · ")} ms</div>` + | |
| `<div><span class="k">batch samples (${n}):</span> ${batches.map(x => fmt(x)).join(" · ")} ms</div>` + | |
| `<div><span class="k">dimensions:</span> ${DIM} · <span class="k">device:</span> ${deviceBadge.querySelector("b").textContent}</div>`; | |
| $("benchResultCard").style.display = "block"; | |
| setStatus("Benchmark complete.", "ok"); | |
| }catch(e){ | |
| console.error(e); | |
| setStatus("Benchmark failed: " + e.message, "err"); | |
| $("benchOut").innerHTML = `<span class="errbox">${esc(String(e))}</span>`; | |
| $("benchResultCard").style.display = "block"; | |
| }finally{ | |
| $("benchBtn").disabled = false; | |
| } | |
| } | |
| async function runLengthSweep(){ | |
| const btn = $("lenBtn"); btn.disabled = true; | |
| try{ | |
| const lens = [8, 32, 128, 512]; | |
| const rows = []; | |
| for (const words of lens){ | |
| const text = Array.from({ length: words }, (_, i) => `token${i}`).join(" "); | |
| await embed([text], { isQuery: false }); // warm-up for this shape | |
| const t0 = performance.now(); | |
| await embed([text], { isQuery: false }); | |
| const ms = performance.now() - t0; | |
| rows.push({ words, chars: text.length, ms }); | |
| setStatus(`Measured ${words} words…`); | |
| } | |
| $("benchKpis").innerHTML = rows.map(r => | |
| kpi(fmt(r.ms) + " ms", `${r.words} words · ${r.chars} chars`)).join(""); | |
| $("benchOut").innerHTML = | |
| `<div><span class="k">Latency scaling with input length:</span></div>` + | |
| rows.map(r => `<div>${String(r.words).padStart(4)} words → ${fmt(r.ms)} ms</div>`).join(""); | |
| $("benchResultCard").style.display = "block"; | |
| setStatus("Length sweep complete.", "ok"); | |
| }catch(e){ | |
| setStatus("Length sweep failed: " + e.message, "err"); | |
| }finally{ btn.disabled = false; } | |
| } | |
| /* ---------- encode ---------- */ | |
| async function runEncode(){ | |
| const btn = $("encodeBtn"); btn.disabled = true; | |
| try{ | |
| const text = $("eText").value.trim(); | |
| if (!text){ $("encodeOut").textContent = "Nothing to embed."; return; } | |
| const isQuery = $("eMode").value === "query"; | |
| const { vectors, ms } = await embed([text], { isQuery }); | |
| const v = vectors[0]; | |
| $("encodeOut").innerHTML = | |
| `<div><span class="k">latency:</span> ${fmt(ms, 2)} ms</div>` + | |
| `<div><span class="k">mode:</span> ${isQuery ? "query (Instruct prefix applied)" : "document (plain)"}</div>` + | |
| `<div><span class="k">dimensions:</span> ${v.length}</div>` + | |
| `<div><span class="k">L2 norm:</span> ${norm(v).toFixed(6)} <span class="k">(1.0 = normalised)</span></div>` + | |
| `<div><span class="k">first 8 values:</span> [${v.slice(0, 8).map(x => x.toFixed(5)).join(", ")}]</div>`; | |
| setStatus("Encoded.", "ok"); | |
| }catch(e){ setStatus("Encode failed: " + e.message, "err"); } | |
| finally{ btn.disabled = false; } | |
| } | |
| /* ---------- compare ---------- */ | |
| async function runCompare(){ | |
| const btn = $("compareBtn"); btn.disabled = true; | |
| try{ | |
| const warmCPU = parseFloat($("cWarm").value) || 43054; | |
| const coldCPU = parseFloat($("cCold").value) || 122822; | |
| await embed(["warm up"], { isQuery: false }); | |
| const samples = []; | |
| for (let i = 0; i < 3; i++){ | |
| const { ms } = await embed(["The capital of China is Beijing."], { isQuery: false }); | |
| samples.push(ms); | |
| } | |
| const here = samples.reduce((a,b) => a+b, 0) / samples.length; | |
| const coldHere = samples[0]; | |
| const warmSpeed = warmCPU / here; | |
| const coldSpeed = coldCPU / coldHere; | |
| $("cmpKpis").innerHTML = | |
| kpi(fmt(here, 1) + " ms", "this device — warm", "hot") + | |
| kpi(fmt(warmCPU, 0) + " ms", "CPU baseline — warm") + | |
| kpi("×" + fmt(warmSpeed, 0), "warm speedup", "good") + | |
| kpi("×" + fmt(coldSpeed, 0), "vs CPU cold start", "good"); | |
| $("cmpVerdict").style.display = "block"; | |
| $("cmpVerdict").innerHTML = | |
| `The same model runs <b>${fmt(warmSpeed, 0)}× faster</b> here than warm on the CPU server, ` + | |
| `and <b>${fmt(coldSpeed, 0)}× faster</b> than the CPU server's cold start — ` + | |
| `with <b>zero server load</b>. Device: ${deviceBadge.querySelector("b").textContent}. ` + | |
| `Baseline measured on a 2-vCPU host, ${fmt(warmCPU)} ms warm / ${fmt(coldCPU)} ms cold per embedding.`; | |
| setStatus("Comparison complete.", "ok"); | |
| }catch(e){ setStatus("Comparison failed: " + e.message, "err"); } | |
| finally{ btn.disabled = false; } | |
| } | |
| /* ---------- similarity ---------- */ | |
| async function runSim(){ | |
| const btn = $("simBtn"); btn.disabled = true; | |
| try{ | |
| const q = $("sQuery").value.trim(); | |
| const docs = $("sDocs").value.split("\n").map(s => s.trim()).filter(Boolean); | |
| if (!q || !docs.length){ setStatus("Need a query and at least one candidate.", "err"); return; } | |
| const qv = (await embed([q], { isQuery: true })).vectors[0]; | |
| const dv = (await embed(docs, { isQuery: false })).vectors; | |
| const scored = docs.map((d, i) => ({ d, s: dot(qv, dv[i]) })) | |
| .sort((a, b) => b.s - a.s); | |
| const lo = Math.min(...scored.map(x => x.s)); | |
| const hi = Math.max(...scored.map(x => x.s)); | |
| const span = (hi - lo) || 1; | |
| $("simRank").style.display = "block"; | |
| $("simRank").innerHTML = scored.map(x => { | |
| const w = ((x.s - lo) / span) * 100; | |
| return `<div class="it"><div class="bar"><i style="width:${w.toFixed(1)}%"></i></div>` + | |
| `<div class="sc">${x.s.toFixed(4)}</div>` + | |
| `<div class="tx" title="${esc(x.d)}">${esc(x.d)}</div></div>`; | |
| }).join(""); | |
| setStatus("Ranked.", "ok"); | |
| }catch(e){ setStatus("Ranking failed: " + e.message, "err"); } | |
| finally{ btn.disabled = false; } | |
| } | |
| /* ---------- wire up ---------- */ | |
| $("benchBtn").addEventListener("click", runBenchmark); | |
| $("lenBtn").addEventListener("click", runLengthSweep); | |
| $("encodeBtn").addEventListener("click", runEncode); | |
| $("compareBtn").addEventListener("click", runCompare); | |
| $("simBtn").addEventListener("click", runSim); | |
| load(); | |
| </script> | |
| </body> | |
| </html> | |