/** Browser parity harness. Serve /models// from .cache/variants with CORS. */ import { env } from '@huggingface/transformers' import { Crivo, type Question } from '../../src/decisions/adapters/crivo.js' env.allowRemoteModels = false env.allowLocalModels = true env.localModelPath = `${self.location.origin}/models/` self.onmessage = async ({ data }) => { const { size, dtype, device } = data let model: Crivo | undefined try { const fixture = await (await fetch(`/models/${size}/parity.json`)).json() model = await Crivo.load({ modelId: size, dtype, device }) let maxError = 0 for (const sample of fixture.cases) { const answers = await model.answerMany( sample.state, sample.questions.map((question: Question) => ({ question })) ) for (const [i, answer] of answers.entries()) { const expected: number[] = sample[dtype === 'q8' ? 'int8' : 'fp32'][i] const actual = answer.type === 'noul' ? [1 - answer.noul, answer.noul] : Object.values(answer.probabilities) for (const [j, value] of actual.entries()) { const delta = Math.abs(value - (expected[j] ?? Number.NaN)) if (!Number.isFinite(delta)) throw new Error('Nonfinite output') maxError = Math.max(maxError, delta) } } } if (dtype === 'fp32' && maxError > 1e-4) throw new Error(`FP32 parity failed: ${maxError}`) self.postMessage({ size, dtype, device, maxError }) } catch (error) { self.postMessage({ size, dtype, device, error: String(error) }) } finally { await model?.dispose() } }