File size: 1,629 Bytes
6986878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/** Browser parity harness. Serve /models/<size>/ 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()
  }
}