Spaces:
Running
Running
Download index.html from Ternarycore/ternarycore-verify: direct link, hf CLI and curl.
- Browser
- Download file 11.4 kB
-
https://huggingface.co/spaces/Ternarycore/ternarycore-verify/resolve/main/index.html
- Command line
-
hf download hf://spaces/Ternarycore/ternarycore-verify/index.html
-
curl -L -o index.html https://huggingface.co/spaces/Ternarycore/ternarycore-verify/resolve/main/index.html
11.4 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <title>TernaryCore — verify the weights</title> | |
| <style> | |
| :root { | |
| --bg: #0b0d12; --panel: #141821; --line: #232936; | |
| --fg: #e6e9ef; --dim: #8b93a5; | |
| --pos: #5aa0ff; --neg: #ff6e5a; --zero: #1a1a20; | |
| --ok: #3ddc97; --bad: #ff6e5a; | |
| } | |
| * { box-sizing: border-box; } | |
| body { | |
| margin: 0; padding: 32px 20px 64px; | |
| background: var(--bg); color: var(--fg); | |
| font: 15px/1.6 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif; | |
| } | |
| .wrap { max-width: 920px; margin: 0 auto; } | |
| h1 { font-size: 26px; margin: 0 0 4px; letter-spacing: -0.3px; } | |
| h2 { font-size: 15px; margin: 32px 0 10px; color: var(--dim); | |
| text-transform: uppercase; letter-spacing: 0.08em; font-weight: 600; } | |
| p { margin: 0 0 14px; } | |
| .sub { color: var(--dim); margin-bottom: 24px; } | |
| a { color: var(--pos); } | |
| code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; } | |
| .panel { background: var(--panel); border: 1px solid var(--line); | |
| border-radius: 10px; padding: 18px; } | |
| .controls { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; } | |
| label { display: block; font-size: 12px; color: var(--dim); margin-bottom: 5px; } | |
| select, button { | |
| background: #1c2230; color: var(--fg); border: 1px solid var(--line); | |
| border-radius: 7px; padding: 9px 12px; font-size: 14px; font-family: inherit; | |
| } | |
| button { background: var(--pos); color: #06101f; border: none; font-weight: 600; cursor: pointer; } | |
| button:disabled { opacity: .5; cursor: default; } | |
| .result { display: grid; grid-template-columns: 1fr auto; gap: 24px; margin-top: 20px; } | |
| @media (max-width: 720px) { .result { grid-template-columns: 1fr; } } | |
| .verdict { font-size: 19px; font-weight: 700; margin-bottom: 14px; } | |
| .verdict.ok { color: var(--ok); } .verdict.bad { color: var(--bad); } | |
| table { border-collapse: collapse; width: 100%; } | |
| td { padding: 5px 0; vertical-align: top; } | |
| td:first-child { color: var(--dim); width: 150px; white-space: nowrap; } | |
| canvas { image-rendering: pixelated; border: 1px solid var(--line); border-radius: 6px; | |
| width: 288px; height: 288px; background: var(--zero); } | |
| .legend { display: flex; gap: 14px; margin-top: 10px; font-size: 12px; color: var(--dim); } | |
| .sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; | |
| margin-right: 5px; vertical-align: -1px; } | |
| .status { color: var(--dim); font-size: 13px; min-height: 20px; margin-top: 12px; } | |
| .foot { margin-top: 36px; padding-top: 20px; border-top: 1px solid var(--line); | |
| color: var(--dim); font-size: 13px; } | |
| .hl { color: var(--fg); } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="wrap"> | |
| <h1>TernaryCore — verify the weights</h1> | |
| <p class="sub"> | |
| Reads the published checkpoint straight from the Hugging Face CDN, in your browser, | |
| and checks that a projection really does hold only <span class="mono">{−s, 0, +s}</span>. | |
| No server, no inference — just the bytes. | |
| </p> | |
| <div class="panel"> | |
| <div class="controls"> | |
| <div> | |
| <label for="blk">Block</label> | |
| <select id="blk"></select> | |
| </div> | |
| <div> | |
| <label for="prj">Projection</label> | |
| <select id="prj"></select> | |
| </div> | |
| <button id="go" disabled>Verify</button> | |
| </div> | |
| <div class="status" id="status">Loading tensor index…</div> | |
| <div class="result" id="result" style="display:none"> | |
| <div> | |
| <div class="verdict" id="verdict"></div> | |
| <table> | |
| <tr><td>tensor</td><td class="mono" id="t-name"></td></tr> | |
| <tr><td>shape</td><td class="mono" id="t-shape"></td></tr> | |
| <tr><td>dtype</td><td class="mono" id="t-dtype"></td></tr> | |
| <tr><td>bytes read</td><td class="mono" id="t-bytes"></td></tr> | |
| <tr><td>distinct values</td><td class="mono" id="t-uniq"></td></tr> | |
| <tr><td>absmean scale</td><td class="mono" id="t-scale"></td></tr> | |
| <tr><td>exact zeros</td><td class="mono" id="t-zero"></td></tr> | |
| <tr><td>+1 / −1</td><td class="mono" id="t-pn"></td></tr> | |
| </table> | |
| </div> | |
| <div> | |
| <canvas id="tile" width="96" height="96"></canvas> | |
| <div class="legend"> | |
| <span><i class="sw" style="background:var(--pos)"></i>+1 add</span> | |
| <span><i class="sw" style="background:var(--neg)"></i>−1 subtract</span> | |
| <span><i class="sw" style="background:#2a2a33"></i>0 skip</span> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <h2>What you just did</h2> | |
| <p> | |
| A <span class="mono">safetensors</span> file begins with a JSON header listing every tensor, | |
| its dtype, its shape and its byte offsets. This page fetched that header with an HTTP range | |
| request, then fetched <em>only</em> the few megabytes belonging to the projection you picked — | |
| never the full 1.2 GB — decoded the bf16 values, and counted them. | |
| </p> | |
| <p> | |
| Every linear projection in this model is balanced ternary: one scale <span class="mono">s</span> | |
| per layer, and every weight is <span class="mono">−s</span>, <span class="mono">0</span> or | |
| <span class="mono">+s</span>. Against 8-bit activations that makes each product | |
| <span class="mono">−x</span>, <span class="mono">0</span> or <span class="mono">+x</span>, so | |
| the matrix multiplies collapse into subtract, skip and add — <span class="hl">no multiplier, | |
| zero DSP slices</span>. Roughly a third of the weights are exact zeros the array simply | |
| skips. The 2-bit packed form the FPGA actually consumes is under | |
| <span class="mono">hardware/</span> in the model repo, alongside a NumPy reference for | |
| element-by-element checking. | |
| </p> | |
| <div class="foot"> | |
| <strong>Model</strong> | |
| <a href="https://huggingface.co/Ternarycore/ternarycore-sst2-student">Ternarycore/ternarycore-sst2-student</a> | |
| · DOI <a href="https://doi.org/10.57967/hf/10741">10.57967/hf/10741</a><br> | |
| <strong>Hardware</strong> | |
| <a href="https://github.com/Ternarycore/ternarycore">github.com/Ternarycore/ternarycore</a> | |
| — CERN-OHL-S v2 · DOI <a href="https://doi.org/10.5281/zenodo.22837568">10.5281/zenodo.22837568</a> | |
| </div> | |
| </div> | |
| <script> | |
| const REPO = "Ternarycore/ternarycore-sst2-student"; | |
| const URL_ = `https://huggingface.co/${REPO}/resolve/main/model.safetensors`; | |
| const $ = id => document.getElementById(id); | |
| const status_ = m => { $("status").textContent = m; }; | |
| let INDEX = null; // name -> {dtype, shape, data_offsets} | |
| let DATA_START = 0; // byte offset where the tensor blob begins | |
| let TREE = {}; // block -> { projName -> tensorName } | |
| const PROJ_ORDER = ["q_proj","k_proj","v_proj","o_proj","gate_proj","up_proj","down_proj"]; | |
| async function range(start, endInclusive) { | |
| const r = await fetch(URL_, { headers: { Range: `bytes=${start}-${endInclusive}` } }); | |
| if (!r.ok && r.status !== 206) throw new Error(`HTTP ${r.status}`); | |
| return await r.arrayBuffer(); | |
| } | |
| async function loadIndex() { | |
| // first 8 bytes: little-endian u64 header length | |
| const head = await range(0, 7); | |
| const dv = new DataView(head); | |
| const lo = dv.getUint32(0, true), hi = dv.getUint32(4, true); | |
| const hlen = hi * 2 ** 32 + lo; | |
| DATA_START = 8 + hlen; | |
| const json = await range(8, 8 + hlen - 1); | |
| INDEX = JSON.parse(new TextDecoder().decode(json)); | |
| delete INDEX.__metadata__; | |
| // Discover the 2-D projection tensors. SubLN wraps o_proj / down_proj as | |
| // Sequential(norm, linear), so those arrive as "...o_proj.1.weight". | |
| // Filtering on rank 2 drops the 1-D norms without hardcoding any name. | |
| for (const [name, meta] of Object.entries(INDEX)) { | |
| if (!Array.isArray(meta.shape) || meta.shape.length !== 2) continue; | |
| const m = name.match(/^model\.layers\.(\d+)\.(?:self_attn|mlp)\.(\w+_proj)(?:\.\d+)?\.weight$/); | |
| if (!m) continue; | |
| const [, blk, proj] = m; | |
| (TREE[blk] ||= {})[proj] = name; | |
| } | |
| const blocks = Object.keys(TREE).map(Number).sort((a, b) => a - b); | |
| if (!blocks.length) throw new Error("no projection tensors found"); | |
| $("blk").innerHTML = blocks.map(b => `<option value="${b}">${b}</option>`).join(""); | |
| const projs = PROJ_ORDER.filter(p => TREE[blocks[0]][p]); | |
| $("prj").innerHTML = projs.map(p => `<option value="${p}">${p}</option>`).join(""); | |
| $("go").disabled = false; | |
| const nT = Object.keys(INDEX).length; | |
| status_(`${nT} tensors indexed · ${blocks.length} blocks × ${projs.length} projections = ${blocks.length * projs.length} ternary GEMMs · header read in ${(DATA_START / 1024).toFixed(1)} KB`); | |
| } | |
| function bf16(buf) { | |
| const u16 = new Uint16Array(buf); | |
| const ab = new ArrayBuffer(u16.length * 4); | |
| const u32 = new Uint32Array(ab), f32 = new Float32Array(ab); | |
| for (let i = 0; i < u16.length; i++) u32[i] = u16[i] << 16; | |
| return f32; | |
| } | |
| function draw(w, cols) { | |
| const c = $("tile"), g = c.getContext("2d"), N = 96; | |
| const img = g.createImageData(N, N); | |
| for (let r = 0; r < N; r++) for (let k = 0; k < N; k++) { | |
| const v = w[r * cols + k], o = (r * N + k) * 4; | |
| const col = v > 0 ? [90,160,255] : v < 0 ? [255,110,90] : [42,42,51]; | |
| img.data[o] = col[0]; img.data[o+1] = col[1]; img.data[o+2] = col[2]; img.data[o+3] = 255; | |
| } | |
| g.putImageData(img, 0, 0); | |
| } | |
| async function verify() { | |
| $("go").disabled = true; | |
| $("result").style.display = "none"; | |
| const blk = $("blk").value, prj = $("prj").value; | |
| const name = TREE[blk][prj], meta = INDEX[name]; | |
| const [s, e] = meta.data_offsets; | |
| const nbytes = e - s; | |
| status_(`fetching ${(nbytes / 1048576).toFixed(2)} MB …`); | |
| try { | |
| const buf = await range(DATA_START + s, DATA_START + e - 1); | |
| const w = meta.dtype === "BF16" ? bf16(buf) : new Float32Array(buf); | |
| let zero = 0, pos = 0, neg = 0, scale = 0; | |
| const uniq = new Set(); | |
| for (let i = 0; i < w.length; i++) { | |
| const v = w[i]; | |
| if (v === 0) zero++; else if (v > 0) { pos++; if (v > scale) scale = v; } else neg++; | |
| if (uniq.size <= 8) uniq.add(v); | |
| } | |
| const n = w.length, ok = uniq.size <= 3; | |
| $("verdict").textContent = ok | |
| ? `PASS — ${uniq.size} distinct values across ${n.toLocaleString()} weights` | |
| : `FAIL — ${uniq.size > 8 ? "more than 8" : uniq.size} distinct values`; | |
| $("verdict").className = "verdict " + (ok ? "ok" : "bad"); | |
| const vals = [...uniq].sort((a, b) => a - b).map(v => v.toExponential(4)).join(", "); | |
| $("t-name").textContent = name; | |
| $("t-shape").textContent = meta.shape.join(" × "); | |
| $("t-dtype").textContent = meta.dtype; | |
| $("t-bytes").textContent = `${nbytes.toLocaleString()} of 1,250,000,000`; | |
| $("t-uniq").textContent = uniq.size <= 8 ? `{ ${vals} }` : "> 8"; | |
| $("t-scale").textContent = scale.toExponential(6); | |
| $("t-zero").textContent = `${(100 * zero / n).toFixed(2)} % (skipped)`; | |
| $("t-pn").textContent = `${(100 * pos / n).toFixed(2)} % / ${(100 * neg / n).toFixed(2)} %`; | |
| draw(w, meta.shape[1]); | |
| $("result").style.display = "grid"; | |
| status_(`read ${(nbytes / 1048576).toFixed(2)} MB of a 1.2 GB checkpoint`); | |
| } catch (err) { | |
| status_(`Could not read the checkpoint: ${err.message}`); | |
| } | |
| $("go").disabled = false; | |
| } | |
| $("go").addEventListener("click", verify); | |
| loadIndex() | |
| .then(verify) | |
| .catch(err => status_(`Could not load the tensor index: ${err.message}`)); | |
| </script> | |
| </body> | |
| </html> | |