Download code/tests/check_engine.js from Redhanuman/pocket-storyteller: direct link, hf CLI and curl.
- Browser
- Download file 4.08 kB
-
https://huggingface.co/Redhanuman/pocket-storyteller/resolve/main/code/tests/check_engine.js
- Command line
-
hf download hf://Redhanuman/pocket-storyteller/code/tests/check_engine.js
-
curl -L -o check_engine.js https://huggingface.co/Redhanuman/pocket-storyteller/resolve/main/code/tests/check_engine.js
4.08 kB
| // Check the browser engine against PyTorch's answers (run tests/check_export.py first). | |
| // node tests/check_engine.js web/storyteller.bin | |
| const fs = require("fs"), path = require("path"); | |
| const Storyteller = require(path.resolve(__dirname, "../web/storyteller.js")); | |
| const file = process.argv[2] || path.resolve(__dirname, "../web/storyteller.bin"); | |
| const toAB = (b) => b.buffer.slice(b.byteOffset, b.byteOffset + b.length); | |
| const exp = JSON.parse(fs.readFileSync(path.resolve(__dirname, "expected.json"))); | |
| const st = new Storyteller(toAB(fs.readFileSync(file))); | |
| let bad = 0; | |
| for (const t of exp.tok) { | |
| const ids = st.encode(t.text); | |
| if (JSON.stringify(ids) !== JSON.stringify(t.ids)) { bad++; console.log("TOKENIZER MISMATCH:", JSON.stringify(t.text.slice(0, 60))); } | |
| } | |
| console.log(`tokenizer: ${exp.tok.length - bad}/${exp.tok.length} texts split identically`); | |
| let worst = 0; | |
| for (const c of exp.cases) { | |
| st.reset(c.ids); | |
| let d = 0, arg = 0, argRef = 0; | |
| for (let i = 0; i < c.logits.length; i++) { | |
| d = Math.max(d, Math.abs(st.logits[i] - c.logits[i])); | |
| if (st.logits[i] > st.logits[arg]) arg = i; | |
| if (c.logits[i] > c.logits[argRef]) argRef = i; | |
| } | |
| worst = Math.max(worst, d); | |
| if (arg !== argRef) { bad++; console.log("TOP CHOICE MISMATCH"); } | |
| } | |
| console.log(`scores: largest difference from PyTorch ${worst.toExponential(2)} over ${exp.cases.length} texts`); | |
| // the three container forms must hold the same model | |
| const wasm = file.replace(/\.bin$/, ".wasm"); | |
| if (fs.existsSync(wasm)) { | |
| const a = new Storyteller(toAB(fs.readFileSync(wasm))); | |
| const b = Storyteller.fromBase64(fs.readFileSync(file).toString("base64")); | |
| st.reset(exp.cases[0].ids); | |
| for (const o of [a, b]) { o.reset(exp.cases[0].ids); for (let i = 0; i < st.logits.length; i++) if (o.logits[i] !== st.logits[i]) { bad++; break; } } | |
| console.log("containers: .wasm and base64 forms checked"); | |
| } | |
| // going back in time and steering must give exactly the same scores as the first time round | |
| { | |
| const ids = exp.cases[0].ids; st.reset(ids); | |
| const snaps = [], picks = []; | |
| for (let i = 0; i < 20; i++) { snaps.push(Float32Array.from(st.logits)); const s = st.next({ temperature: 0.8 }); if (s.done) break; picks.push(s.id); } | |
| const k = Math.min(8, picks.length - 1); | |
| const same = (a, b) => a.every((v, i) => v === b[i]); | |
| if (!st.rewind(1 + ids.length + k) || !same(snaps[k], st.logits)) { bad++; console.log("REWIND MISMATCH"); } | |
| st.push(picks[k]); | |
| if (!same(snaps[k + 1], st.logits)) { bad++; console.log("PUSH MISMATCH"); } | |
| st.reset(ids.concat(picks.slice(0, k + 1))); | |
| if (!same(snaps[k + 1], st.logits)) { bad++; console.log("RESET MISMATCH"); } | |
| console.log("steering: rewind, push and reset agree"); | |
| } | |
| // long stories: once the memory is full, each new piece overwrites the oldest one | |
| { | |
| const ids = exp.cases[0].ids; st.reset(ids); let n = 0, ok = true; | |
| while (n < 700) { const s = st.next({ temperature: 1.0 }); if (s.done) st.push(st.encode("\nOnce")[0]); n++; if (!Number.isFinite(s.prob) || s.attn.length > st.cfg.ctx || Math.abs(s.attn.reduce((a, b) => a + b, 0) - 1) > 1e-3) ok = false; } | |
| const total = st.tokens.length; | |
| if (st.rewind(10)) ok = false; // far too long ago: must refuse | |
| if (!st.rewind(total - 50)) ok = false; // recent: must work without re-reading | |
| for (let i = 0; i < 300; i++) { const s = st.next({ temperature: 1.0 }); if (s.done) st.push(st.encode("\nOnce")[0]); if (!Number.isFinite(s.prob)) ok = false; } | |
| if (!ok) { bad++; console.log("LONG STORY PROBLEM"); } | |
| console.log(`long story: ${n} pieces and a rewind, memory window ${st.span} of ${st.cfg.ctx}`); | |
| } | |
| // speed | |
| st.reset(st.encode("Once upon a time")); | |
| const t0 = process.hrtime.bigint(); let n = 0; | |
| for (let i = 0; i < 300; i++) { if (st.next({ temperature: 0.8 }).done) st.reset(st.encode("Once upon a time")); n++; } | |
| console.log(`speed: ${(n / (Number(process.hrtime.bigint() - t0) / 1e9)).toFixed(0)} word-pieces per second`); | |
| if (bad || worst > 2e-2) { console.log("FAILED"); process.exit(1); } | |
| console.log("OK"); | |