File size: 3,955 Bytes
96fc193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Forward pass of the KEP1 network of the NEB chain (spec/nebhash-v2.md, sections 3.1 and 4.1), integer only.
// A port of neb2_model_predict in pow/ref/nebhash2.c: same layers, same rounding, same first-maximum rules.
// Works in a browser and in Node. Input: 784 values in 0..7 (global view 583, then local view 201). Output: 0 or 1.

const KEP1 = [ // kind, cin, cout, k, pad, pool, shift, in_len, in_off, sources, w_off
  { conv: true, cin: 1, cout: 8, k: 5, pad: 2, pool: 4, s: 2, n: 583, off: 0, src: [], w: 0 },
  { conv: true, cin: 8, cout: 16, k: 5, pad: 2, pool: 4, s: 4, n: 145, src: [0], w: 40 },
  { conv: true, cin: 1, cout: 8, k: 5, pad: 2, pool: 4, s: 3, n: 201, off: 583, src: [], w: 680 },
  { conv: true, cin: 8, cout: 16, k: 5, pad: 2, pool: 4, s: 4, n: 50, src: [2], w: 720 },
  { conv: false, cin: 768, cout: 32, s: 5, src: [1, 3], w: 1360 },
  { conv: false, cin: 32, cout: 2, s: 4, src: [4], w: 25936 },
];

// Packed weights (13,000 bytes): two signed 4-bit weights per byte, low nibble first.
export function unpackWeights(packed) {
  const w = new Int8Array(packed.length * 2);
  for (let j = 0; j < packed.length; j++) {
    const lo = packed[j] & 15, hi = packed[j] >> 4;
    w[2 * j] = lo >= 8 ? lo - 16 : lo;
    w[2 * j + 1] = hi >= 8 ? hi - 16 : hi;
  }
  return w;
}

const rshift = (x, n) => (n === 0 ? x : (x + (1 << (n - 1))) >> n);
const clip = (v) => (v < -8 ? -8 : v > 7 ? 7 : v);

// Returns { label, sums }: the class (first maximum of the raw output sums) and those two sums.
export function predict(w, x) {
  const outs = [];
  let lastIn = null;
  for (let i = 0; i < KEP1.length; i++) {
    const L = KEP1[i];
    let input;
    if (L.src.length === 0) input = Int8Array.from(x.slice(L.off, L.off + L.cin * L.n));
    else {
      input = new Int8Array(L.src.reduce((t, s) => t + outs[s].length, 0));
      let at = 0;
      for (const s of L.src) { input.set(outs[s], at); at += outs[s].length; }
    }
    lastIn = input;
    if (!L.conv) {
      const out = new Int8Array(L.cout);
      for (let o = 0; o < L.cout; o++) {
        let z = 0;
        const base = L.w + o * L.cin;
        for (let j = 0; j < L.cin; j++) z += w[base + j] * input[j];
        out[o] = clip(rshift(z, L.s));
      }
      outs.push(out);
      continue;
    }
    const n = L.n, cl = n, a = new Int8Array(L.cout * cl); // stride 1 and padding 2 with kernel 5 keep the length
    for (let o = 0; o < L.cout; o++)
      for (let p = 0; p < cl; p++) {
        let z = 0;
        for (let c = 0; c < L.cin; c++) {
          const wb = L.w + (o * L.cin + c) * L.k, rb = c * n;
          for (let t = 0; t < L.k; t++) {
            const q = p + t - L.pad;
            if (q >= 0 && q < n) z += w[wb + t] * input[rb + q];
          }
        }
        a[o * cl + p] = clip(rshift(z, L.s));
      }
    const ol = Math.floor(cl / L.pool), out = new Int8Array(L.cout * ol);
    for (let o = 0; o < L.cout; o++)
      for (let j = 0; j < ol; j++) {
        let best = o * cl + j * L.pool;
        for (let t = 1; t < L.pool; t++) if (a[o * cl + j * L.pool + t] > a[best]) best = o * cl + j * L.pool + t;
        out[o * ol + j] = a[best];
      }
    outs.push(out);
  }
  const L = KEP1[KEP1.length - 1], sums = [];
  for (let o = 0; o < L.cout; o++) {
    let z = 0;
    for (let j = 0; j < L.cin; j++) z += w[L.w + o * L.cin + j] * lastIn[j];
    sums.push(z);
  }
  return { label: sums[1] > sums[0] ? 1 : 0, sums };
}

// A dataset file NMD1: "NMD1", count (u32 little-endian), then records of 784 values and a label.
export function readDataset(bytes) {
  const head = String.fromCharCode(...bytes.slice(0, 4));
  if (head !== "NMD1") throw new Error("not an NMD1 dataset");
  const n = bytes[4] | (bytes[5] << 8) | (bytes[6] << 16) | (bytes[7] << 24);
  const rows = [];
  for (let i = 0; i < n; i++) {
    const at = 8 + i * 785;
    rows.push({ x: bytes.subarray(at, at + 784), label: bytes[at + 784] });
  }
  return rows;
}