|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html lang="en"> |
3 | 3 | <head> |
4 | | - <meta charset="UTF-8"> |
5 | | - <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
6 | | - <title>Oxideon in Browser — WebAssembly + WebGPU</title> |
7 | | - <style> |
8 | | - body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, monospace; background: #0b0f19; color: #f3f4f6; margin: 0; padding: 2rem; } |
9 | | - .container { max-width: 800px; margin: 0 auto; background: #111827; border: 1px solid #1f2937; border-radius: 12px; padding: 2rem; box-shadow: 0 10px 25px rgba(0,0,0,0.5); } |
10 | | - h1 { color: #60a5fa; margin-top: 0; } |
11 | | - .badge { display: inline-block; padding: 0.25rem 0.75rem; border-radius: 9999px; font-size: 0.85rem; font-weight: 600; background: #1e3a8a; color: #93c5fd; margin-bottom: 1.5rem; } |
12 | | - textarea { width: 100%; height: 120px; background: #1f2937; color: #f9fafb; border: 1px solid #374151; border-radius: 8px; padding: 1rem; font-family: monospace; font-size: 1rem; box-sizing: border-box; resize: vertical; } |
13 | | - button { background: #2563eb; color: white; border: none; padding: 0.75rem 1.5rem; font-size: 1rem; font-weight: 600; border-radius: 8px; cursor: pointer; transition: background 0.2s; margin-top: 1rem; } |
14 | | - button:hover { background: #1d4ed8; } |
15 | | - .output { margin-top: 1.5rem; padding: 1rem; background: #030712; border: 1px solid #1f2937; border-radius: 8px; font-family: monospace; min-height: 80px; white-space: pre-wrap; color: #34d399; } |
16 | | - .meta { margin-top: 0.5rem; font-size: 0.85rem; color: #9ca3af; } |
17 | | - </style> |
| 4 | +<meta charset="UTF-8"> |
| 5 | +<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | +<title>Oxideon in the browser</title> |
| 7 | +<style> |
| 8 | + :root { --bg:#0b0f19; --card:#111827; --line:#1f2937; --fg:#f3f4f6; --mute:#9ca3af; --acc:#60a5fa; --ok:#34d399; --bad:#f87171; } |
| 9 | + * { box-sizing: border-box; } |
| 10 | + body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--fg); margin: 0; padding: 1.25rem; } |
| 11 | + main { max-width: 860px; margin: 0 auto; } |
| 12 | + h1 { color: var(--acc); margin: 0 0 .25rem; } |
| 13 | + h2 { margin: 0 0 .25rem; font-size: 1.15rem; } |
| 14 | + p { color: var(--mute); line-height: 1.5; margin: .25rem 0 .75rem; } |
| 15 | + .status { font-size: .85rem; padding: .5rem .75rem; border-radius: 8px; background: #052e1a; color: var(--ok); border: 1px solid #14532d; margin: .75rem 0 1rem; } |
| 16 | + .status.err { background: #3b0d0d; color: var(--bad); border-color: #7f1d1d; } |
| 17 | + section { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 1.1rem 1.25rem; margin-bottom: 1rem; } |
| 18 | + .row { display: flex; flex-wrap: wrap; gap: .75rem; align-items: center; margin: .5rem 0; } |
| 19 | + label { color: var(--mute); font-size: .9rem; } |
| 20 | + select, input[type=range] { accent-color: var(--acc); } |
| 21 | + select { background: #1f2937; color: var(--fg); border: 1px solid #374151; border-radius: 6px; padding: .3rem .5rem; } |
| 22 | + button { background: #2563eb; color: #fff; border: 0; padding: .55rem 1.1rem; font-weight: 600; border-radius: 8px; cursor: pointer; } |
| 23 | + button:hover { background: #1d4ed8; } |
| 24 | + button:disabled { background: #374151; cursor: default; } |
| 25 | + pre { background: #030712; border: 1px solid var(--line); border-radius: 8px; padding: .75rem; margin: .5rem 0 0; color: var(--ok); font-size: .85rem; white-space: pre-wrap; overflow-x: auto; } |
| 26 | + canvas { max-width: 100%; background: #030712; border: 1px solid var(--line); border-radius: 8px; display: block; margin-top: .5rem; } |
| 27 | + .legend span { margin-right: 1rem; font-size: .85rem; } |
| 28 | + footer { color: var(--mute); font-size: .8rem; text-align: center; margin: 1.5rem 0; } |
| 29 | + a { color: var(--acc); } |
| 30 | +</style> |
18 | 31 | </head> |
19 | 32 | <body> |
20 | | - <div class="container"> |
21 | | - <span class="badge">Oxideon (simulated demo)</span> |
22 | | - <h1>In-Browser LLM & Neural Inference</h1> |
23 | | - <p>Layout mock-up of a client-side inference page for Rust compiled to WebAssembly with WebGPU acceleration.</p> |
24 | | - <p><strong>Simulated:</strong> this page does not load oxideon or any model. The answer is canned and the timing is a fixed delay, so the numbers below say nothing about real performance.</p> |
25 | | - |
26 | | - <label for="prompt">Prompt Input:</label> |
27 | | - <textarea id="prompt">Explain the difference between PagedAttention and standard multi-head attention in one sentence.</textarea> |
28 | | - |
29 | | - <button id="runBtn" onclick="runInference()">Show canned answer</button> |
30 | | - |
31 | | - <div class="output" id="output">Output will stream here...</div> |
32 | | - <div class="meta" id="metrics">Backend: none (simulated)</div> |
| 33 | +<main> |
| 34 | + <h1>Oxideon in your browser</h1> |
| 35 | + <p>Every result below is computed by the real Oxideon tensor library, compiled to WebAssembly, running in this page. Nothing is sent to a server, and the numbers are measured on your machine.</p> |
| 36 | + <div id="status" class="status">Loading the WebAssembly module...</div> |
| 37 | + |
| 38 | + <section> |
| 39 | + <h2>1. Matrix multiply: speed and accuracy</h2> |
| 40 | + <p>Multiplies two random <i>n</i>x<i>n</i> float32 matrices with Oxideon, then checks the result against a float64 reference. A plain JavaScript loop accumulating in float32 is shown next to it.</p> |
| 41 | + <div class="row"> |
| 42 | + <label>size <select id="mm-n"><option>64</option><option>128</option><option selected>256</option><option>384</option></select></label> |
| 43 | + <button id="mm-run" disabled>Run</button> |
| 44 | + </div> |
| 45 | + <pre id="mm-out">Press Run.</pre> |
| 46 | + </section> |
| 47 | + |
| 48 | + <section> |
| 49 | + <h2>2. Activation functions</h2> |
| 50 | + <p>Curves computed by Oxideon on 2000 points in [-6, 6] (solid) against an exact float64 reference (dashed). The table gives the largest absolute error.</p> |
| 51 | + <div class="row"><button id="act-run" disabled>Draw</button></div> |
| 52 | + <canvas id="act-cv" width="820" height="300"></canvas> |
| 53 | + <div class="legend" id="act-legend"></div> |
| 54 | + <pre id="act-out"></pre> |
| 55 | + </section> |
| 56 | + |
| 57 | + <section> |
| 58 | + <h2>3. Softmax does not overflow</h2> |
| 59 | + <p>Softmax of large logits. A textbook <code>exp(x) / sum(exp(x))</code> in JavaScript overflows to NaN; Oxideon subtracts the maximum first.</p> |
| 60 | + <div class="row"> |
| 61 | + <label>logit scale <input id="sm-scale" type="range" min="1" max="2000" value="900"> <b id="sm-scale-v">900</b></label> |
| 62 | + <button id="sm-run" disabled>Run</button> |
33 | 63 | </div> |
| 64 | + <pre id="sm-out">Press Run.</pre> |
| 65 | + </section> |
| 66 | + |
| 67 | + <section> |
| 68 | + <h2>4. A small neural network, forward pass</h2> |
| 69 | + <p>A two-layer ReLU network (matmul, bias add, relu, matmul, softmax, all Oxideon ops) whose hand-set weights compute XOR: class 1 where <i>x</i> and <i>y</i> have the same sign. The map shows the predicted probability on a 64x64 grid, run as one batch of 4096 points.</p> |
| 70 | + <div class="row"> |
| 71 | + <label>confidence <input id="nn-k" type="range" min="1" max="30" value="6"> <b id="nn-k-v">6</b></label> |
| 72 | + <button id="nn-run" disabled>Run</button> |
| 73 | + </div> |
| 74 | + <canvas id="nn-cv" width="256" height="256" style="width:256px;image-rendering:pixelated"></canvas> |
| 75 | + <pre id="nn-out"></pre> |
| 76 | + </section> |
| 77 | + |
| 78 | + <footer>Source: <a href="https://github.com/voidstackloop/oxideon">github.com/voidstackloop/oxideon</a> · <code>pip install oxideon</code></footer> |
| 79 | +</main> |
| 80 | + |
| 81 | +<script type="module"> |
| 82 | +const $ = id => document.getElementById(id); |
| 83 | +const status = $("status"); |
| 84 | +let ox; |
| 85 | +try { |
| 86 | + ox = await import("./pkg/oxideon.js"); |
| 87 | + const t0 = performance.now(); |
| 88 | + await ox.default(); |
| 89 | + const sess = new ox.WasmInferenceSession(); |
| 90 | + status.textContent = `Oxideon WebAssembly module loaded in ${(performance.now() - t0).toFixed(0)} ms (WebGPU backend in this build: ${sess.has_webgpu() ? "yes" : "no, CPU"}).`; |
| 91 | + for (const b of ["mm-run", "act-run", "sm-run", "nn-run"]) $(b).disabled = false; |
| 92 | +} catch (e) { |
| 93 | + status.className = "status err"; |
| 94 | + status.textContent = "Could not load the WebAssembly module (./pkg/oxideon.js): " + e + ". Build it first: see examples/browser_demo/README.md."; |
| 95 | +} |
| 96 | + |
| 97 | +const T = (data, shape) => ox.WasmTensor.from_f32_array(Float32Array.from(data), Uint32Array.from(shape)); |
| 98 | +const fmt = v => v === 0 ? "0" : Math.abs(v) < 1e-3 || Math.abs(v) >= 1e5 ? v.toExponential(2) : v.toPrecision(4); |
| 99 | +const time = fn => { const t = performance.now(); const r = fn(); return [r, performance.now() - t]; }; |
| 100 | + |
| 101 | +// ---- 1. matmul |
| 102 | +$("mm-run").onclick = () => { |
| 103 | + const n = +$("mm-n").value; |
| 104 | + const A = new Float32Array(n * n), B = new Float32Array(n * n); |
| 105 | + for (let i = 0; i < n * n; i++) { A[i] = Math.random() * 2 - 1; B[i] = Math.random() * 2 - 1; } |
| 106 | + const a = T(A, [n, n]), b = T(B, [n, n]); |
| 107 | + a.matmul(b).free(); // warm-up |
| 108 | + const [c, ms] = time(() => a.matmul(b)); |
| 109 | + const out = c.to_f32_vec(); c.free(); |
| 110 | + // float64 reference and a naive float32 loop (summation order k = 0..n) |
| 111 | + const ref = new Float64Array(n * n), naive = new Float32Array(n * n); |
| 112 | + const [, msNaive] = time(() => { |
| 113 | + for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) { |
| 114 | + let s = 0, f = 0; |
| 115 | + for (let k = 0; k < n; k++) { const p = A[i * n + k] * B[k * n + j]; s += p; f = Math.fround(f + Math.fround(p)); } |
| 116 | + ref[i * n + j] = s; naive[i * n + j] = f; |
| 117 | + } |
| 118 | + }); |
| 119 | + // error relative to the sum of the absolute terms (cancellation cannot hide it), in units of 2^-24 |
| 120 | + let eo = 0, en = 0; |
| 121 | + for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) { |
| 122 | + let sa = 0; for (let k = 0; k < n; k++) sa += Math.abs(A[i * n + k] * B[k * n + j]); |
| 123 | + const u = 16777216 / sa; |
| 124 | + eo = Math.max(eo, Math.abs(out[i * n + j] - ref[i * n + j]) * u); |
| 125 | + en = Math.max(en, Math.abs(naive[i * n + j] - ref[i * n + j]) * u); |
| 126 | + } |
| 127 | + const gf = 2 * n ** 3 / (ms / 1000) / 1e9; |
| 128 | + $("mm-out").textContent = |
| 129 | +`${n}x${n} @ ${n}x${n} |
| 130 | +Oxideon (WebAssembly): ${ms.toFixed(2)} ms (${gf.toFixed(2)} GFLOP/s) |
| 131 | +JavaScript loop: ${msNaive.toFixed(0)} ms (also computes the float64 reference; not a fair timing) |
| 132 | +
|
| 133 | +worst error vs float64, in units of 2^-24 relative to the sum of |terms|: |
| 134 | + Oxideon: ${fmt(eo)} |
| 135 | + naive float32 JS: ${fmt(en)}`; |
| 136 | +}; |
| 137 | + |
| 138 | +// ---- 2. activations (exact float64 references) |
| 139 | +function erf(x) { // series for small |x|, continued fraction for the tail; accurate to ~1e-15 |
| 140 | + const ax = Math.abs(x); |
| 141 | + let r; |
| 142 | + if (ax < 3) { let t = ax, s = ax, k = 0; do { k++; t *= -ax * ax / k; s += t / (2 * k + 1); } while (Math.abs(t) > 1e-18); r = 2 / Math.sqrt(Math.PI) * s; } |
| 143 | + else { let f = 0; for (let k = 60; k >= 1; k--) f = k / 2 / (ax + f); r = 1 - Math.exp(-ax * ax) / Math.sqrt(Math.PI) / (ax + f); } |
| 144 | + return x < 0 ? -r : r; |
| 145 | +} |
| 146 | +const ACT = { |
| 147 | + gelu: { ref: x => 0.5 * x * (1 + erf(x / Math.SQRT2)), color: "#f472b6" }, |
| 148 | + silu: { ref: x => x / (1 + Math.exp(-x)), color: "#60a5fa" }, |
| 149 | + tanh: { ref: Math.tanh, color: "#34d399" }, |
| 150 | + relu: { ref: x => Math.max(0, x), color: "#fbbf24" }, |
| 151 | +}; |
| 152 | +$("act-legend").innerHTML = Object.entries(ACT).map(([k, v]) => `<span style="color:${v.color}">■ ${k}</span>`).join(""); |
| 153 | +$("act-run").onclick = () => { |
| 154 | + const N = 2000, xs = new Float32Array(N); |
| 155 | + for (let i = 0; i < N; i++) xs[i] = -6 + 12 * i / (N - 1); |
| 156 | + const x = T(xs, [N]); |
| 157 | + const cv = $("act-cv"), g = cv.getContext("2d"), W = cv.width, H = cv.height; |
| 158 | + g.clearRect(0, 0, W, H); |
| 159 | + const X = v => (v + 6) / 12 * W, Y = v => H / 2 - v * (H / 2) / 6.2; // y range about [-6.2, 6.2] |
| 160 | + g.strokeStyle = "#1f2937"; g.beginPath(); g.moveTo(0, Y(0)); g.lineTo(W, Y(0)); g.moveTo(X(0), 0); g.lineTo(X(0), H); g.stroke(); |
| 161 | + let lines = "function max |Oxideon - float64 reference|\n"; |
| 162 | + for (const [name, a] of Object.entries(ACT)) { |
| 163 | + const y = x[name](), ys = y.to_f32_vec(); y.free(); |
| 164 | + let maxErr = 0; |
| 165 | + g.strokeStyle = a.color; g.lineWidth = 2; g.setLineDash([]); g.beginPath(); |
| 166 | + for (let i = 0; i < N; i++) { const px = X(xs[i]), py = Y(ys[i]); i ? g.lineTo(px, py) : g.moveTo(px, py); maxErr = Math.max(maxErr, Math.abs(ys[i] - a.ref(xs[i]))); } |
| 167 | + g.stroke(); |
| 168 | + g.lineWidth = 1; g.setLineDash([4, 4]); g.beginPath(); |
| 169 | + for (let i = 0; i < N; i++) { const px = X(xs[i]), py = Y(a.ref(xs[i])); i ? g.lineTo(px, py) : g.moveTo(px, py); } |
| 170 | + g.stroke(); |
| 171 | + lines += `${name.padEnd(10)} ${maxErr.toExponential(2)}\n`; |
| 172 | + } |
| 173 | + g.setLineDash([]); x.free(); |
| 174 | + $("act-out").textContent = lines.trimEnd(); |
| 175 | +}; |
| 176 | + |
| 177 | +// ---- 3. softmax |
| 178 | +$("sm-scale").oninput = e => $("sm-scale-v").textContent = e.target.value; |
| 179 | +$("sm-run").onclick = () => { |
| 180 | + const s = +$("sm-scale").value, v = [1, 0.5, 0.25, 0, -0.5].map(u => u * s); |
| 181 | + const naiveE = v.map(Math.exp), z = naiveE.reduce((p, q) => p + q, 0), naive = naiveE.map(e => e / z); |
| 182 | + const t = T(v, [1, v.length]), r = t.softmax(-1), p = r.to_f32_vec(); t.free(); r.free(); |
| 183 | + const sum = Array.from(p).reduce((a, b) => a + b, 0); |
| 184 | + $("sm-out").textContent = |
| 185 | +`logits: [${v.join(", ")}] |
| 186 | +Oxideon softmax: [${Array.from(p).map(fmt).join(", ")}] sum = ${sum.toFixed(6)} |
| 187 | +naive JS softmax: [${naive.map(fmt).join(", ")}]${naive.some(Number.isNaN) ? " <- NaN: exp overflowed" : ""}`; |
| 188 | +}; |
34 | 189 |
|
35 | | - <script type="module"> |
36 | | - window.runInference = async function() { |
37 | | - const out = document.getElementById("output"); |
38 | | - const metrics = document.getElementById("metrics"); |
39 | | - out.textContent = "Simulating..."; |
40 | | - |
41 | | - const start = performance.now(); |
42 | | - // Simulate Wasm generation step |
43 | | - setTimeout(() => { |
44 | | - const elapsed = (performance.now() - start).toFixed(2); |
45 | | - out.textContent = "PagedAttention partitions the KV cache into fixed-size physical blocks (like OS virtual memory pages), eliminating fragmentation and enabling zero-copy prefix sharing."; |
46 | | - metrics.textContent = `Simulated: ${elapsed} ms fixed delay | 28 canned tokens | WebGPU available in this browser: ${"gpu" in navigator ? "yes" : "no"}`; |
47 | | - }, 120); |
48 | | - }; |
49 | | - </script> |
| 190 | +// ---- 4. XOR network |
| 191 | +$("nn-k").oninput = e => $("nn-k-v").textContent = e.target.value; |
| 192 | +$("nn-run").onclick = () => { |
| 193 | + const k = +$("nn-k").value, G = 64, P = G * G, pts = new Float32Array(P * 2); |
| 194 | + for (let i = 0; i < G; i++) for (let j = 0; j < G; j++) { pts[(i * G + j) * 2] = -1 + 2 * (j + .5) / G; pts[(i * G + j) * 2 + 1] = 1 - 2 * (i + .5) / G; } |
| 195 | + // hidden = relu([x+y, -(x+y), x-y, y-x]); f = |x+y| - |x-y| has the sign of x*y |
| 196 | + const W1 = T([1, -1, 1, -1, 1, -1, -1, 1], [2, 4]), b1 = T([0, 0, 0, 0], [1, 4]); |
| 197 | + const W2 = T([k, -k, k, -k, -k, k, -k, k], [4, 2]), b2 = T([0, 0], [1, 2]); |
| 198 | + const X = T(pts, [P, 2]); |
| 199 | + const [probs, ms] = time(() => { |
| 200 | + const h = X.matmul(W1).add(b1).relu(); |
| 201 | + const o = h.matmul(W2).add(b2); |
| 202 | + return o.softmax(-1); |
| 203 | + }); |
| 204 | + const p = probs.to_f32_vec(); probs.free(); X.free(); |
| 205 | + const cv = $("nn-cv"), g = cv.getContext("2d"), img = g.createImageData(G, G); |
| 206 | + cv.width = G; cv.height = G; |
| 207 | + let right = 0; |
| 208 | + for (let i = 0; i < P; i++) { |
| 209 | + const p1 = p[i * 2], x = pts[i * 2], y = pts[i * 2 + 1]; |
| 210 | + img.data.set([Math.round(244 * p1 + 59 * (1 - p1)), Math.round(114 * p1 + 130 * (1 - p1)), Math.round(182 * p1 + 246 * (1 - p1)), 255], i * 4); |
| 211 | + if ((p1 > .5) === (x * y > 0)) right++; |
| 212 | + } |
| 213 | + g.putImageData(img, 0, 0); |
| 214 | + $("nn-out").textContent = `4096 points through the network in ${ms.toFixed(2)} ms\n${right} of 4096 points classified correctly (${(100 * right / P).toFixed(1)}%)\nclass 1 (x and y have the same sign) in pink, class 0 in blue`; |
| 215 | +}; |
| 216 | +</script> |
50 | 217 | </body> |
51 | 218 | </html> |
0 commit comments