/** Browser inference for the frozen v17a transformer. No response rules or training. * All linear maps come from the original, hash-checked sparse weight archive. * Sparse dot products omit mathematical zero terms; JS accumulation differs from * PyTorch, so this backend has its own measured token-parity report. */ export const WEIGHT_SHA = '4739a3511f5c4b9170f5d09524bd8105f5853a7999cbeefdea7a8cbae01d737d'; export const CONFIG_SHA = '56df33d5842a0d3a1351dae78732c01589bde1ce55a33868f9d13dec171bee91'; const decoder = new TextDecoder(); export async function sha256(bytes) { return Array.from(new Uint8Array(await crypto.subtle.digest('SHA-256', bytes)), b => b.toString(16).padStart(2, '0')).join(''); } async function inflate(bytes) { const stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream('deflate-raw')); return new Uint8Array(await new Response(stream).arrayBuffer()); } export async function unpackZip(bytes, decompress = inflate) { const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); let end = bytes.length - 22; while (end >= Math.max(0, bytes.length - 65557) && view.getUint32(end, true) !== 0x06054b50) end--; if (end < 0) throw new Error('Invalid weight archive'); const count = view.getUint16(end + 10, true); let pos = view.getUint32(end + 16, true); const members = new Map(); for (let n = 0; n < count; n++) { if (view.getUint32(pos, true) !== 0x02014b50) throw new Error('Invalid ZIP directory'); const method = view.getUint16(pos + 10, true), size = view.getUint32(pos + 20, true); const original = view.getUint32(pos + 24, true), len = view.getUint16(pos + 28, true); const extra = view.getUint16(pos + 30, true), comment = view.getUint16(pos + 32, true); const local = view.getUint32(pos + 42, true), name = decoder.decode(bytes.subarray(pos + 46, pos + 46 + len)); const start = local + 30 + view.getUint16(local + 26, true) + view.getUint16(local + 28, true); const compressed = bytes.subarray(start, start + size); const raw = method === 0 ? compressed.slice() : method === 8 ? await decompress(compressed) : null; if (!raw || raw.length !== original || members.has(name)) throw new Error('Invalid ZIP member'); members.set(name, raw); pos += 46 + len + extra + comment; } return members; } export class Tokenizer { constructor(config) { this.config = config; this.labels = config.vocabulary; this.ids = new Map(this.labels.map((x, i) => [x.toLowerCase(), i])); this.pointers = new Map(); this.reverse = new Map(); this.used = [0, 0]; this.maxPhrase = Math.max(...this.labels.map(x => x.trim().split(/\s+/).length)); } encode(text) { // Python's original regex uses Unicode-aware \w in the punctuation branch. const words = text.match(/[A-Za-z]+(?:'[A-Za-z]+)?|[0-9]+|[^\p{L}\p{N}_\s]/gu) || []; const out = []; for (let i = 0; i < words.length;) { let found = false; for (let len = Math.min(this.maxPhrase, words.length - i); len > 0; len--) { const key = words.slice(i, i + len).join(' ').toLowerCase(); if (this.ids.has(key) && !key.startsWith('<')) { out.push(this.ids.get(key)); i += len; found = true; break; } } if (found) continue; const original = words[i++], key = original.toLowerCase(); if (!this.pointers.has(key)) { const section = 'aeiou'.includes(key[0]) ? 0 : 1; if (this.used[section] >= 64) throw new Error('The fixed pointer vocabulary is full; start a new conversation.'); const tid = this.config.dynamic_start + 64 * section + this.used[section]++; this.pointers.set(key, tid); this.reverse.set(tid, original); } out.push(this.pointers.get(key)); } return out; } tokenText(id) { return this.reverse.get(id) ?? this.labels[id]; } decode(ids) { return ids.filter(x => x !== this.config.special.eos).map(x => this.tokenText(x)).join(' ') .replace(/\s+([.,?!:;])/g, '$1').replace(/(? a + b.toUpperCase()); } } class Tensor { constructor(spec, members) { this.shape = spec.shape; const ib = members.get(spec.prefix + '.indices.u32'), vb = members.get(spec.prefix + '.values.f32'); if (!ib || !vb || ib.length !== spec.entries * 4 || vb.length !== ib.length) throw new Error('Truncated tensor'); this.indices = new Uint32Array(Uint8Array.from(ib).buffer); this.values = new Float32Array(Uint8Array.from(vb).buffer); const total = this.shape.reduce((a,b) => a*b, 1); for (let i=0; i=total || (i && this.indices[i]<=this.indices[i-1])) throw new Error('Invalid sparse indices'); if (this.shape.length === 1) { this.dense = new Float32Array(this.shape[0]); for (let i=0;i{if(v!==0)active.add(i);}); const headDim=this.width/config.heads, heads=[]; for(let head=0;head{}) { const c=this.config,w=this.width,n=ids.length,offset=cache?cache[0].length:0,total=offset+n; if(total>c.context_length) throw new Error('Context is full; start a new conversation.'); let h=new Float32Array(n*w); for(let t=0;tmax) max=scores[j]; } let sum=0; for(let j=0;j{}) { const started=performance.now(),c=this.config; const previous={pointers:new Map(this.tokenizer.pointers),reverse:new Map(this.tokenizer.reverse),used:[...this.tokenizer.used]}; let prefix; try { prefix=[...this.history,c.special.user,...this.tokenizer.encode(prompt),c.special.assistant]; if(prefix.length+maxNewTokens>(this.contextLimit??c.context_length)) throw new Error('Insufficient context space; start a new conversation.'); } catch(error) { Object.assign(this.tokenizer,previous);throw error; } let state=this.forward(prefix,null,(layer,total)=>progress({phase:'prefill',layer,total,prefix_tokens:prefix.length})); const output=[]; let stopped=false; for(let step=0;stepstate.logits[token]) token=j; output.push(token); progress({phase:'generate',tokens:output.length}); if(token===c.special.eos) { stopped=true; break; } state=this.forward([token],state.cache); } this.history=prefix.concat(output); return {prompt,text:this.tokenizer.decode(output),token_ids:output,stopped_on_eos:stopped, generated_tokens:output.length,seconds:(performance.now()-started)/1000,device:'browser-cpu',prefix_tokens:prefix.length}; } }