// node tools/test.mjs : the JS world model must reproduce PyTorch's rollout (tools/fixtures.json) to < 1e-4 m. import { readFileSync } from "node:fs"; import { loadModel, rollout } from "../core/model.js"; const here = new URL(".", import.meta.url); const m = loadModel(JSON.parse(readFileSync(new URL("../core/model.json", here)))); const fx = JSON.parse(readFileSync(new URL("fixtures.json", here))); const S = fx.future.length, T = fx.future[0].length; const plans = Float32Array.from(fx.future.flat(2)); const t0 = performance.now(); const out = rollout(m, fx.mounts, fx.states, fx.cmds, plans, S, T); let maxErr = 0, worst = -1; const ref = fx.rollout.flat(2); for (let i = 0; i < ref.length; i++) { const e = Math.abs(out[i] - ref[i]); if (e > maxErr) { maxErr = e; worst = i; } } console.log(`rollout ${S}x${T}: max |js - torch| = ${maxErr.toExponential(2)} (index ${worst % 14} of the state)`); const big = new Float32Array(128 * T * 5).map((_, i) => plans[i % plans.length]); const t1 = performance.now(); for (let r = 0; r < 5; r++) rollout(m, fx.mounts, fx.states, fx.cmds, big, 128, T); console.log(`S=128, T=10: ${((performance.now() - t1) / 5).toFixed(1)} ms per rollout (node, one thread)`); if (!(maxErr < 1e-4)) { console.error("FAIL"); process.exit(1); } console.log("PASS");