File size: 1,453 Bytes
a2bd61d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
import assert from "node:assert/strict";
import fs from "node:fs";
import {
  parseModel,
  predict,
  features,
  fnv,
  normalized,
} from "./inference.mjs";
const raw = fs.readFileSync(
    new URL("./run-001/model-int8.json", import.meta.url),
    "utf8",
  ),
  model = parseModel(raw);
assert.equal(fnv("hello"), 0x4f9f2cab);
assert.equal(normalized(" Cafe\u0301\u0085 NOTE "), "café note");
assert.ok(features("😀").ids.length > 0);
for (const text of [
  "Where is my card?",
  "中文😀",
  "<script>alert(1)</script>",
  "x".repeat(2048),
]) {
  const a = predict(model, text, true),
    b = predict(model, text, false);
  assert.equal(a.probabilities.length, 77);
  assert.ok(Math.abs(a.probabilities.reduce((a, b) => a + b, 0) - 1) < 1e-12);
  assert.equal(a.predicted_label_id, b.predicted_label_id);
}
for (const input of ["", " ", "x".repeat(2049), "\ud800"])
  assert.throws(() => predict(model, input));
for (const edit of [
  (m) => (m.dimensions.classes = 76),
  (m) => (m.labels[1] = m.labels[0]),
  (m) => (m.embedding_scales[0] = -1),
  (m) => (m.output_bias[0] = "bad"),
  (m) => (m.embedding_q_base64 = "a"),
  (m) => (m.extra = true),
]) {
  const m = JSON.parse(raw);
  edit(m);
  assert.throws(() => parseModel(JSON.stringify(m)));
}
console.log(
  JSON.stringify({
    status: "PASS",
    known_classes: 77,
    input_limit_utf16: 2048,
    calibration_preserves_argmax: true,
    malformed_models_rejected: 6,
  }),
);