Download code/tests/window_test.js from Redhanuman/pocket-storyteller: direct link, hf CLI and curl.
- Browser
- Download file 1.79 kB
-
https://huggingface.co/Redhanuman/pocket-storyteller/resolve/main/code/tests/window_test.js
- Command line
-
hf download hf://Redhanuman/pocket-storyteller/code/tests/window_test.js
-
curl -L -o window_test.js https://huggingface.co/Redhanuman/pocket-storyteller/resolve/main/code/tests/window_test.js
1.79 kB
| // Which way of forgetting is better once a story is longer than the model's memory? | |
| // "slide": each new piece overwrites the oldest one (no pause) | |
| // "reread": throw away the oldest half and re-read the rest (a pause of ~1 second) | |
| // Measures how well the model predicts real story text beyond its memory size. Lower is better. | |
| // node tests/window_test.js web/storyteller.bin data/val.bin | |
| const fs = require("fs"), path = require("path"); | |
| const Storyteller = require(path.resolve(__dirname, "../web/storyteller.js")); | |
| const b = fs.readFileSync(process.argv[2]), v = fs.readFileSync(process.argv[3]); | |
| const val = new Uint16Array(v.buffer, v.byteOffset, v.length / 2); | |
| const st = new Storyteller(b.buffer.slice(b.byteOffset, b.byteOffset + b.length)); | |
| const C = st.cfg.ctx, SEG = +process.argv[4] || 10, LEN = 640; | |
| const nll = () => { let m = -Infinity; for (const x of st.logits) if (x > m) m = x; let z = 0; for (const x of st.logits) z += Math.exp(x - m); return (t) => -(st.logits[t] - m - Math.log(z)); }; | |
| const out = {}; | |
| for (const mode of ["slide", "reread"]) { | |
| st.mode = mode; let inside = 0, nIn = 0, beyond = 0, nBe = 0; const t0 = Date.now(); | |
| for (let s = 0; s < SEG; s++) { | |
| const off = 1000 + s * 250000; | |
| st.reset([]); st.pos = 0; st.tokens = []; // start with no start marker, like a training window | |
| for (let i = 0; i < LEN; i++) { | |
| st.push(val[off + i]); | |
| const l = nll()(val[off + i + 1]); | |
| if (i < C - 1) { inside += l; nIn++; } else { beyond += l; nBe++; } | |
| } | |
| } | |
| out[mode] = { inside: inside / nIn, beyond: beyond / nBe }; | |
| console.log(`${mode.padEnd(7)} loss inside memory ${out[mode].inside.toFixed(4)} | beyond memory ${out[mode].beyond.toFixed(4)} (${nBe} pieces, ${((Date.now() - t0) / 1000).toFixed(0)} s)`); | |
| } | |