Download code/tests/sampling_eval.js from Redhanuman/pocket-storyteller: direct link, hf CLI and curl.
- Browser
- Download file 3.28 kB
-
https://huggingface.co/Redhanuman/pocket-storyteller/resolve/main/code/tests/sampling_eval.js
- Command line
-
hf download hf://Redhanuman/pocket-storyteller/code/tests/sampling_eval.js
-
curl -L -o sampling_eval.js https://huggingface.co/Redhanuman/pocket-storyteller/resolve/main/code/tests/sampling_eval.js
3.28 kB
| // How clean are the stories a model writes? | |
| // node tests/sampling_eval.js web/storyteller.bin default setting (imagination 0.8) | |
| // node tests/sampling_eval.js web/storyteller.bin 1.0 0.6 other imagination settings | |
| // Every run uses the same random numbers and the same 40 first lines, so two models can be compared. | |
| // Reports made-up words (words that never appear in 120 MB of the real stories), repetition and length. | |
| const fs = require("fs"), path = require("path"); | |
| const Storyteller = require(path.resolve(__dirname, "../web/storyteller.js")); | |
| const b = fs.readFileSync(path.resolve(process.argv[2] || path.resolve(__dirname, "../web/storyteller.bin"))); | |
| const st = new Storyteller(b.buffer.slice(b.byteOffset, b.byteOffset + b.length)); | |
| const WORD = /[A-Za-z]+/g, known = new Set(); | |
| for (const [file, bytes] of [["data/valid.txt", 22e6], ["data/train.txt", 100e6]]) { | |
| const fd = fs.openSync(path.resolve(__dirname, "..", file), "r"), buf = Buffer.alloc(bytes); | |
| const n = fs.readSync(fd, buf, 0, bytes, 0); fs.closeSync(fd); | |
| for (const w of buf.toString("latin1", 0, n).match(WORD) || []) known.add(w.toLowerCase()); | |
| } | |
| const STARTS = ["Once upon a time, there was a little fox named Pip", "Lily found a tiny door in the garden", "The big red ball rolled down the hill", | |
| "Tom wanted to fly like a bird", "One day, a dragon came to the village", "Once upon a time", "Mia and her dog went to the park", "The little robot was sad because"]; | |
| function mulberry(a) { return () => { a |= 0; a = a + 0x6D2B79F5 | 0; let t = Math.imul(a ^ a >>> 15, 1 | a); t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; return ((t ^ t >>> 14) >>> 0) / 4294967296; }; } | |
| const N = +(process.env.STORIES || 40), START = +(process.env.START || 0), LEN = 240; // START skips ahead, to split a big run | |
| for (const arg of process.argv.length > 3 ? process.argv.slice(3) : ["0.8"]) { | |
| const temperature = +arg, topK = 40; | |
| let words = 0, bad = 0, pieces = 0, ended = 0, rep = 0, grams = 0, logp = 0; const badList = {}, names = new Set(), types = new Set(); | |
| for (let i = START; i < START + N; i++) { | |
| Math.random = mulberry(1000 + i); | |
| const prompt = STARTS[i % STARTS.length]; | |
| st.reset(st.encode(prompt)); let text = "", n = 0; | |
| for (; n < LEN; n++) { const s = st.next({ temperature, topK }); if (s.done) { ended++; break; } text += s.text; logp += Math.log(s.prob); } | |
| pieces += n; | |
| const ws = (text.match(WORD) || []).map((w) => w.toLowerCase()); | |
| for (const w of ws) { words++; types.add(w); if (!known.has(w)) { bad++; badList[w] = (badList[w] || 0) + 1; } } | |
| const seen = new Set(); | |
| for (let k = 0; k + 4 <= ws.length; k++) { const g = ws.slice(k, k + 4).join(" "); grams++; if (seen.has(g)) rep++; seen.add(g); } | |
| for (const m of text.matchAll(/named ([A-Z][a-z]+)/g)) names.add(m[1]); | |
| } | |
| const ex = Object.entries(badList).sort((a, c) => c[1] - a[1]).slice(0, 8).map(([w]) => w).join(" "); | |
| console.log(`imagination ${temperature}: made-up words ${(1000 * bad / words).toFixed(2)} per 1000 (${bad} of ${words}) | repeated 4-word runs ${(100 * rep / grams).toFixed(1)}% (${rep} of ${grams}) | different words ${types.size} | names ${names.size} | finished ${ended}/${N} | avg log-prob ${(logp / pieces).toFixed(3)} | e.g. ${ex}`); | |
| } | |