Spaces:
Running
Running
Download core.js from thelamapi/Next-Signal: direct link, hf CLI and curl.
- Browser
- Download file 2.68 kB
-
https://huggingface.co/spaces/thelamapi/Next-Signal/resolve/main/core.js
- Command line
-
hf download hf://spaces/thelamapi/Next-Signal/core.js
-
curl -L -o core.js https://huggingface.co/spaces/thelamapi/Next-Signal/resolve/main/core.js
2.68 kB
| /* Next Signal compute core: port of signal_core.py (encoder via nlrt, stats features, probes, perturbation). */ | |
| (function (root, factory) { | |
| if (typeof module === "object" && module.exports) module.exports = factory(); | |
| else root.Core = factory(); | |
| })(typeof self !== "undefined" ? self : this, function () { | |
| "use strict"; | |
| const ASSETS = ["model.json", "windows.json"]; | |
| const C = 9, L = 128; | |
| function init(assets) { | |
| const m = NLRT.decode(assets["model.json"]); | |
| return { m, meta: m.meta, w: assets["windows.json"] }; | |
| } | |
| function perturb(x, noise, scale, shift, seed) { | |
| const rng = NLRT.mulberry32(seed >>> 0), out = []; | |
| for (let c = 0; c < C; c++) { | |
| let mu = 0; for (let t = 0; t < L; t++) mu += x[c][t]; mu /= L; | |
| let v = 0; for (let t = 0; t < L; t++) v += (x[c][t] - mu) * (x[c][t] - mu); const sd = Math.sqrt(v / L); | |
| const row = new Array(L); | |
| for (let t = 0; t < L; t++) row[t] = x[c][(((t - shift) % L) + L) % L] * scale + noise * sd * rng.normal(); | |
| out.push(row); | |
| } | |
| return out; | |
| } | |
| function embed(ctx, net, x) { | |
| const d = new Float64Array(C * L); | |
| for (let c = 0; c < C; c++) for (let t = 0; t < L; t++) d[c * L + t] = (x[c][t] - ctx.meta.mean[c]) / ctx.meta.std[c]; | |
| return NLRT.forward(ctx.m.nets[net], NLRT.T([C, L], d)).data; | |
| } | |
| function stats(x) { | |
| const f = []; | |
| for (let c = 0; c < C; c++) { | |
| let mu = 0, mn = Infinity, mx = -Infinity, ad = 0, sq = 0; | |
| for (let t = 0; t < L; t++) { const v = x[c][t]; mu += v; if (v < mn) mn = v; if (v > mx) mx = v; sq += v * v; if (t) ad += Math.abs(v - x[c][t - 1]); } | |
| mu /= L; let vv = 0; for (let t = 0; t < L; t++) vv += (x[c][t] - mu) * (x[c][t] - mu); | |
| f.push(mu, Math.sqrt(vv / L), mn, mx, ad / (L - 1), sq / L); | |
| } | |
| return f; | |
| } | |
| function probe(p, z) { | |
| const K = p.W.length, out = new Array(K); | |
| for (let k = 0; k < K; k++) { let s = p.b[k]; for (let j = 0; j < z.length; j++) s += p.W[k][j] * (z[j] - p.mu[j]) / p.sd[j]; out[k] = s; } | |
| return NLRT.softmax(out); | |
| } | |
| function predictAll(ctx, x, budget) { | |
| const P = ctx.meta.probes, b = String(budget); | |
| return { ssl: probe(P.ssl[b], embed(ctx, "encoder", x)), random: probe(P.random[b], embed(ctx, "random_encoder", x)), stats: probe(P.stats[b], stats(x)) }; | |
| } | |
| function runCase(ctx, input) { | |
| const base = ctx.w.windows[input.window]; | |
| const x = perturb(base, input.noise, input.scale, input.shift, input.seed); | |
| const p = predictAll(ctx, x, input.budget); | |
| return { ssl: p.ssl, random: p.random, stats: p.stats, x_c0_t5: x[0][5] }; | |
| } | |
| return { ASSETS, init, perturb, embed, stats, probe, predictAll, runCase }; | |
| }); | |