Spaces:
Running
Running
File size: 8,374 Bytes
298a39f | 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | /* nlrt.js: browser and Node twin of nlrt.py. Batch size 1, float64 math. */
(function (root, factory) {
if (typeof module === "object" && module.exports) module.exports = factory();
else root.NLRT = factory();
})(typeof self !== "undefined" ? self : this, function () {
"use strict";
function b64d(s) {
let bin;
if (typeof atob === "function") bin = atob(s);
else bin = Buffer.from(s, "base64").toString("binary");
const bytes = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
return new Float32Array(bytes.buffer);
}
function decode(spec) {
for (const name in spec.nets || {}) {
for (const L of spec.nets[name]) {
if (typeof L.w === "string") L.w = b64d(L.w);
if (typeof L.b === "string") L.b = b64d(L.b);
}
}
for (const name in spec.tensors || {}) {
const t = spec.tensors[name];
if (typeof t.data === "string") t.data = b64d(t.data);
}
return spec;
}
async function loadModel(url) {
const r = await fetch(url);
if (!r.ok) throw new Error("cannot load " + url);
return decode(await r.json());
}
function pair(v) { return Array.isArray(v) ? v : [v, v]; }
// Tensor: {shape: [D]} or {shape: [C, H, W]} or {shape: [C, L]}, data: Float64Array
function T(shape, data) {
const n = shape.reduce((a, b) => a * b, 1);
return { shape: shape.slice(), data: data || new Float64Array(n) };
}
function as3(x) {
if (x.shape.length === 2) return { t: T([x.shape[0], 1, x.shape[1]], x.data), sq: true };
return { t: x, sq: false };
}
function conv2d(x0, L) {
const { t: x, sq } = as3(x0);
const [C, H, W] = x.shape;
const [O, I, KH, KW] = L.w_shape;
const [sh, sw] = pair(L.stride), [ph, pw] = pair(L.pad);
const OH = Math.floor((H + 2 * ph - KH) / sh) + 1;
const OW = Math.floor((W + 2 * pw - KW) / sw) + 1;
const out = new Float64Array(O * OH * OW);
const w = L.w, b = L.b, xd = x.data;
for (let o = 0; o < O; o++) {
for (let oy = 0; oy < OH; oy++) {
for (let ox = 0; ox < OW; ox++) {
let s = b[o];
for (let c = 0; c < I; c++) {
for (let ky = 0; ky < KH; ky++) {
const iy = oy * sh + ky - ph;
if (iy < 0 || iy >= H) continue;
const wb = ((o * I + c) * KH + ky) * KW, xb = (c * H + iy) * W;
for (let kx = 0; kx < KW; kx++) {
const ix = ox * sw + kx - pw;
if (ix < 0 || ix >= W) continue;
s += w[wb + kx] * xd[xb + ix];
}
}
}
out[(o * OH + oy) * OW + ox] = s;
}
}
}
return sq ? T([O, OW], out) : T([O, OH, OW], out);
}
function maxpool(x0, L) {
const { t: x, sq } = as3(x0);
const [C, H, W] = x.shape, [kh, kw] = pair(L.k);
const OH = Math.floor(H / kh), OW = Math.floor(W / kw);
const out = new Float64Array(C * OH * OW);
for (let c = 0; c < C; c++)
for (let oy = 0; oy < OH; oy++)
for (let ox = 0; ox < OW; ox++) {
let m = -Infinity;
for (let i = 0; i < kh; i++)
for (let j = 0; j < kw; j++) {
const v = x.data[(c * H + oy * kh + i) * W + ox * kw + j];
if (v > m) m = v;
}
out[(c * OH + oy) * OW + ox] = m;
}
return sq ? T([C, OW], out) : T([C, OH, OW], out);
}
function linear(x, L) {
const [O, I] = L.w_shape;
const out = new Float64Array(O);
for (let o = 0; o < O; o++) {
let s = L.b[o];
const base = o * I;
for (let i = 0; i < I; i++) s += L.w[base + i] * x.data[i];
out[o] = s;
}
return T([O], out);
}
function map(x, f) {
const d = new Float64Array(x.data.length);
for (let i = 0; i < d.length; i++) d[i] = f(x.data[i]);
return T(x.shape, d);
}
function forward(layers, x) {
if (!(x.data instanceof Float64Array)) x = T(x.shape, Float64Array.from(x.data));
for (const L of layers) {
switch (L.type) {
case "linear": x = linear(x, L); break;
case "conv2d": x = conv2d(x, L); break;
case "relu": x = map(x, v => (v > 0 ? v : 0)); break;
case "tanh": x = map(x, Math.tanh); break;
case "sigmoid": x = map(x, v => 1 / (1 + Math.exp(-v))); break;
case "maxpool": x = maxpool(x, L); break;
case "gap": {
const C = x.shape[0], n = x.data.length / C, d = new Float64Array(C);
for (let c = 0; c < C; c++) { let s = 0; for (let i = 0; i < n; i++) s += x.data[c * n + i]; d[c] = s / n; }
x = T([C], d); break;
}
case "flatten": x = T([x.data.length], x.data); break;
case "reshape": x = T(L.shape, x.data); break;
case "upsample": {
const s = L.s;
if (x.shape.length === 3) {
const [C, H, W] = x.shape, d = new Float64Array(C * H * s * W * s);
for (let c = 0; c < C; c++)
for (let y = 0; y < H * s; y++)
for (let xx = 0; xx < W * s; xx++)
d[(c * H * s + y) * W * s + xx] = x.data[(c * H + Math.floor(y / s)) * W + Math.floor(xx / s)];
x = T([C, H * s, W * s], d);
} else {
const [C, Ln] = x.shape, d = new Float64Array(C * Ln * s);
for (let c = 0; c < C; c++) for (let i = 0; i < Ln * s; i++) d[c * Ln * s + i] = x.data[c * Ln + Math.floor(i / s)];
x = T([C, Ln * s], d);
}
break;
}
case "l2norm": {
let s = 0; for (const v of x.data) s += v * v;
const n = Math.max(Math.sqrt(s), 1e-12);
x = map(x, v => v / n); break;
}
default: throw new Error("unknown layer " + L.type);
}
}
return x;
}
function mulberry32(seed) {
let a = seed >>> 0;
const random = function () {
a = (a + 0x6D2B79F5) >>> 0;
let t = a;
t = Math.imul(t ^ (t >>> 15), t | 1) >>> 0;
t = (t ^ ((t + (Math.imul(t ^ (t >>> 7), t | 61) >>> 0)) >>> 0)) >>> 0;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
return {
random,
uniform: (lo, hi) => lo + (hi - lo) * random(),
randint: n => Math.floor(random() * n),
normal: () => { const u = Math.max(random(), 1e-12), v = random(); return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v); },
};
}
function softmax(arr) {
let m = -Infinity; for (const v of arr) if (v > m) m = v;
const e = Array.from(arr, v => Math.exp(v - m)); const s = e.reduce((a, b) => a + b, 0);
return e.map(v => v / s);
}
function argmax(arr) { let k = 0; for (let i = 1; i < arr.length; i++) if (arr[i] > arr[k]) k = i; return k; }
// Deep comparison used by parity tests: numbers within tol, everything else exact.
function compare(got, want, tol, path, acc) {
acc = acc || { maxDiff: 0, mismatches: [] };
path = path || "$";
if (typeof want === "number") {
const d = Math.abs(Number(got) - want);
if (!(d <= tol * Math.max(1, Math.abs(want)))) acc.mismatches.push(path + ": got " + got + " want " + want);
if (isFinite(d)) acc.maxDiff = Math.max(acc.maxDiff, d);
} else if (Array.isArray(want)) {
if (!got || got.length !== want.length) acc.mismatches.push(path + ": length " + (got && got.length) + " vs " + want.length);
else for (let i = 0; i < want.length; i++) compare(got[i], want[i], tol, path + "[" + i + "]", acc);
} else if (want && typeof want === "object") {
for (const k in want) compare(got ? got[k] : undefined, want[k], tol, path + "." + k, acc);
} else if (got !== want) acc.mismatches.push(path + ": got " + got + " want " + want);
return acc;
}
function runParity(core, ctx, parity) {
const results = [];
let maxDiff = 0, failed = 0;
for (const c of parity.cases) {
const out = core.runCase(ctx, c.input);
const r = compare(out, c.expected, c.tol != null ? c.tol : parity.tol);
maxDiff = Math.max(maxDiff, r.maxDiff);
if (r.mismatches.length) failed++;
results.push({ name: c.name, maxDiff: r.maxDiff, mismatches: r.mismatches.slice(0, 5) });
}
return { cases: parity.cases.length, failed, maxDiff, results };
}
return { b64d, decode, loadModel, T, forward, mulberry32, softmax, argmax, compare, runParity };
});
|