memlnaut-nisps/tests/cpp/parity_diff.mjs
w1n5t0n 3138701100 Stream 11: verification infrastructure (golden vectors + parity + lint + CI + Playwright migration)
Agent ran out of API credits before committing. Files staged + committed by orchestrator. Coverage:
- tests/cpp/ml_golden_vectors.cpp: fixed-seed regression tests for MLP determinism
- tests/cpp/engine_impulse.cpp: white-noise impulse responses with binary baseline
- tests/cpp/parity_check.cpp + parity_wasm.mjs + parity_diff.mjs: native vs WASM bit-equivalence
- scripts/build-cpp-tests.sh, lint-cpp.sh, parity-check.sh, run-all-tests.sh
- playground/tests/e2e/{ml-engine,modes,persistence,ui-interactions}.spec.ts (+helpers)
- .github/workflows/ci.yml

(meml-x06)
2026-04-29 19:50:55 +03:00

117 lines
3.7 KiB
JavaScript

#!/usr/bin/env node
/**
* tests/cpp/parity_diff.mjs — float-tolerant blob comparison.
*
* Reads two binary blobs in the format produced by parity_check.cpp and
* parity_wasm.mjs, and reports any pair of floats that differs by more than
* the allowed tolerance.
*
* Usage:
* node parity_diff.mjs <native.bin> <wasm.bin> [tolerance]
*
* Default tolerance is 1e-5. Returns exit code 0 on match, 1 on mismatch,
* 2 on file/format error.
*/
import { readFile } from 'node:fs/promises';
const MAGIC = 0x5450524e;
const VERSION = 1;
const DEFAULT_TOL = 1e-5;
// Layout for context in error messages — must match parity_check.cpp / parity_wasm.mjs.
const SECTIONS = [
{ name: 'stage1_outputs', count: 126 },
{ name: 'weight_probe', count: 12 },
{ name: 'stage2_outputs', count: 126 },
{ name: 'final_loss', count: 1 },
{ name: 'paf_synth_means', count: 2 },
{ name: 'channel_strip_means', count: 2 },
];
async function readBlob(path) {
const buf = await readFile(path);
if (buf.length < 12) throw new Error(`${path}: file too short`);
const magic = buf.readUInt32LE(0);
if (magic !== MAGIC) {
throw new Error(`${path}: bad magic 0x${magic.toString(16)}, want 0x${MAGIC.toString(16)}`);
}
const version = buf.readUInt32LE(4);
if (version !== VERSION) {
throw new Error(`${path}: unsupported version ${version}`);
}
const n = buf.readUInt32LE(8);
const expected = 12 + n * 4;
if (buf.length < expected) {
throw new Error(`${path}: truncated, header says ${n} floats but file has ${(buf.length - 12) / 4}`);
}
const arr = new Float32Array(n);
for (let i = 0; i < n; ++i) arr[i] = buf.readFloatLE(12 + i * 4);
return arr;
}
function locate(idx) {
let off = 0;
for (const s of SECTIONS) {
if (idx < off + s.count) {
return `${s.name}[${idx - off}]`;
}
off += s.count;
}
return `payload[${idx}]`;
}
async function main() {
const [, , nativePath, wasmPath, tolArg] = process.argv;
if (!nativePath || !wasmPath) {
console.error('usage: parity_diff.mjs <native.bin> <wasm.bin> [tolerance]');
process.exit(2);
}
const tol = tolArg ? Number(tolArg) : DEFAULT_TOL;
if (!(tol > 0)) {
console.error(`bad tolerance: ${tolArg}`);
process.exit(2);
}
let native, wasm;
try {
[native, wasm] = await Promise.all([readBlob(nativePath), readBlob(wasmPath)]);
} catch (err) {
console.error('[parity_diff]', err.message);
process.exit(2);
}
if (native.length !== wasm.length) {
console.error(`[parity_diff] length mismatch: native=${native.length}, wasm=${wasm.length}`);
process.exit(1);
}
const mismatches = [];
let maxDelta = 0;
let maxDeltaIdx = -1;
for (let i = 0; i < native.length; ++i) {
const d = Math.abs(native[i] - wasm[i]);
if (d > maxDelta) { maxDelta = d; maxDeltaIdx = i; }
if (d > tol) {
mismatches.push({ idx: i, native: native[i], wasm: wasm[i], delta: d });
}
}
if (mismatches.length === 0) {
console.log(`[parity_diff] OK: ${native.length} floats match within ${tol.toExponential()} (max delta ${maxDelta.toExponential()} at ${locate(maxDeltaIdx)})`);
process.exit(0);
}
console.error(`[parity_diff] FAIL: ${mismatches.length}/${native.length} floats differ by more than ${tol.toExponential()}`);
console.error(` max delta: ${maxDelta.toExponential()} at ${locate(maxDeltaIdx)}`);
const head = mismatches.slice(0, 8);
for (const m of head) {
console.error(` ${locate(m.idx).padEnd(28)} native=${m.native.toFixed(8)} wasm=${m.wasm.toFixed(8)} delta=${m.delta.toExponential(3)}`);
}
if (mismatches.length > head.length) {
console.error(` ... and ${mismatches.length - head.length} more`);
}
process.exit(1);
}
main();