Four items from one workflow, committed together because their build and CI
wiring genuinely interleaves — nisps/CMakeLists.txt, run-all-tests.sh and
ci.yml each carry hunks from two of them, and the stage renumbering (1/5 ->
1/6) touches every line. Splitting would produce commits that do not build,
which is worse than a commit that does four things and says so.
S26 part 2 — the curve declaration now matches reality. params[].curve stays
the mode-wide DEFAULT; a voice_spaces entry may now be {name, curve_overrides}
declaring only the slots where THAT voice space deviates. The 6 modes with one
voice space are byte-identical. The values were derived MECHANICALLY by a new
codegen/curve-audit.ts that models the four idioms a p[N]*p[N] regex misses
(alias form, memlcelium's implicit-counter sq() lambda, loop-generated indices,
smooth_params_), inlines helpers, and RAISES rather than guessing when it
cannot reduce an expression. A drift gate cross-checks 1179 (voice space x
param) slots against engine source on every run and was proved to fail loudly
on three drift classes. Application stays in the engine: nisps/engines,
nisps/pipeline and nisps/core are untouched, generated output is pure insertion
(755 insertions, 0 deletions), and the rebuilt nisps.wasm was byte-identical.
S4 / 7.2 — firmware reads the active mode's driver config at mode start, and
mic/line is real. My brief assumed the engine owns this; the code disagreed and
the code was right. sound_analysis_midi's EngineT is NoOpEngine — the mic lives
on a separately-composed AnalysisEngine member — so engine-level wiring would
have compiled, passed every gate, and left the one mic mode on line input.
Hence a mode-level seam defaulting to engine().driver_config(). Separately,
DriverConfig's defaults (line_level 0, output_volume 1.0) had drifted from
memllib's actual 3/0.8 because nothing had ever read them; wiring them as-is
would have made every silent mode louder and its line input maximally
insensitive — a behaviour change disguised as plumbing. Now pinned by a test.
Also: GetSysClockSpeed() panic()s on unsupported sample rates and runs on the
first line of setup(), so sample_rate needed a fallback ahead of clock setup.
CI's firmware env list gains soundanalysismidi — it is the only mic variant and
nothing else compiles that path.
Plan 5e — telemetry is real. A loss_history C-API entry across the full 5-layer
chain lets the browser read the per-iteration loss the core already records.
The audit named one fabrication site; there were two — wasm-iml.ts's
synchronous train() published lossHistory: [loss] as well. A third, ctx.loss,
was not merely dead but actively synthetic (fallbacks of prev * 0.82 and a
literal 0.5, rendered by nothing) and is deleted. The firmware buffer stays
untouched, per the L25 call. EngineApi.lossHistory() reads spine state rather
than the MLP handle, because trainAsync() fits on the worker's mirror net and
the handle would give a subtly-wrong second answer.
Plan 5f — engine throughput is measurable. One source compiled twice (CMake
natively, emcc for WASM) so the targets compare directly and no WASM export is
added. Sequencers are driven into a working state, and every row prints its own
working-state evidence so a number produced by an idle engine is visible rather
than plausible. Reports, never asserts: a wall-clock threshold on shared
hardware is meaningless or flaky, same call as the firmware size job.
ALIGNMENT: the telemetry defect is deleted (built, not deferred); the
performance defect is rewritten to what is actually left — these are HOST
numbers, and nothing measures the RP2350 at 150 MHz, which is the target the
mission's constraint is about. Q4 (memllib ownership) and Q5 (legacy feedback
modes) are closed.
Corrections to my own earlier claims, both found by agents contradicting the
brief: manifold/ONBOARDING.md was NOT "now accurate" — its primitives list
still named five deleted primitives and cited a seededGradient() that does not
exist. And the parity harness misses the sequencer engines because it runs 128
frames while their sequencers evaluate every 400-500 samples, NOT because
all-params-0.5 fails to trigger them (it does trigger: 0.5 maps to ratio 2,
firing three times per bar). The fix is a longer window, not different params.
Gates: run-all-tests.sh ALL GREEN — 4/4 ctest, parity PASS, lint clean, curve
drift 1179 slots ok, 39 e2e (was 33). Firmware: 5 envs built including the mic
variant.
134 lines
5.4 KiB
TypeScript
134 lines
5.4 KiB
TypeScript
/**
|
|
* Loss-history C-ABI contract (`bun test`).
|
|
*
|
|
* `nisps_ml_loss_history` is the browser's only honest answer to "is the
|
|
* network learning?" — it hands back the per-iteration curve the C++ core
|
|
* already records (`nisps::ml::MLPCore::loss_history`). Before §6.5e the
|
|
* worker fabricated a ONE-element "history" from the final loss, so the first
|
|
* assertion here is deliberately that the curve is longer than one entry.
|
|
*
|
|
* This test drives the committed `manifold/public/nisps.{js,wasm}` directly,
|
|
* which matters: `scripts/parity-check.sh` only exercises PAFSynth and
|
|
* ChannelStrip from an all-params-0.5 baseline and never touches the training
|
|
* path, so a parity PASS is no evidence for anything asserted below.
|
|
*/
|
|
import { readFileSync } from 'node:fs';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { dirname, join } from 'node:path';
|
|
import { beforeAll, expect, test } from 'bun:test';
|
|
|
|
interface EmModule {
|
|
HEAPF32: Float32Array;
|
|
_malloc(bytes: number): number;
|
|
_free(ptr: number): void;
|
|
cwrap(name: string, ret: string | null, args: string[]): (...a: number[]) => number;
|
|
}
|
|
type Factory = (opts: { wasmBinary: Uint8Array }) => Promise<EmModule>;
|
|
|
|
let M: EmModule;
|
|
let mlCreate: (i: number, o: number, h: number, nh: number, seed: number) => number;
|
|
let mlDestroy: (ml: number) => number;
|
|
let mlAddExample: (ml: number, f: number, l: number) => number;
|
|
let mlTrain: (ml: number, lr: number, maxIter: number, minErr: number, sw: number) => number;
|
|
let mlLossHistory: (ml: number, out: number, max: number) => number;
|
|
|
|
beforeAll(async () => {
|
|
// Same glue-loading dance as tests/wasm-load.ts: MODULARIZE output with no ES
|
|
// exports, in a `type:module` sub-package.
|
|
const dir = dirname(fileURLToPath(import.meta.url));
|
|
const source = readFileSync(join(dir, '..', 'public', 'nisps.js'), 'utf8');
|
|
const factory = new Function(
|
|
'module', 'exports',
|
|
`${source}\n;return typeof createNispsModule === 'function' ? createNispsModule : null;`,
|
|
)({ exports: {} }, {}) as Factory | null;
|
|
if (typeof factory !== 'function') throw new Error('createNispsModule not found in glue');
|
|
M = await factory({ wasmBinary: readFileSync(join(dir, '..', 'public', 'nisps.wasm')) });
|
|
|
|
mlCreate = M.cwrap('nisps_ml_create', 'number', ['number', 'number', 'number', 'number', 'number']) as typeof mlCreate;
|
|
mlDestroy = M.cwrap('nisps_ml_destroy', null, ['number']) as typeof mlDestroy;
|
|
mlAddExample = M.cwrap('nisps_ml_add_example', null, ['number', 'number', 'number']) as typeof mlAddExample;
|
|
mlTrain = M.cwrap('nisps_ml_train', 'number', ['number', 'number', 'number', 'number', 'number']) as typeof mlTrain;
|
|
mlLossHistory = M.cwrap('nisps_ml_loss_history', 'number', ['number', 'number', 'number']) as typeof mlLossHistory;
|
|
});
|
|
|
|
/** A 2→1 net fed the XOR table; returns the handle (caller destroys). */
|
|
function trainedNet(maxIter: number, minErr = 0): { ml: number; loss: number } {
|
|
const ml = mlCreate(2, 1, 0, 0, 7);
|
|
const f = M._malloc(2 * 4);
|
|
const l = M._malloc(1 * 4);
|
|
for (const [x, y, t] of [[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0]]) {
|
|
new Float32Array(M.HEAPF32.buffer, f, 2).set([x, y]);
|
|
new Float32Array(M.HEAPF32.buffer, l, 1).set([t]);
|
|
mlAddExample(ml, f, l);
|
|
}
|
|
M._free(f);
|
|
M._free(l);
|
|
return { ml, loss: mlTrain(ml, 0.5, maxIter, minErr, 0) };
|
|
}
|
|
|
|
function readHistory(ml: number, cap?: number): number[] {
|
|
const total = mlLossHistory(ml, 0, 0);
|
|
const n = cap ?? total;
|
|
if (n <= 0) return [];
|
|
const ptr = M._malloc(n * 4);
|
|
mlLossHistory(ml, ptr, n);
|
|
const out = Array.from(new Float32Array(M.HEAPF32.buffer, ptr, n));
|
|
M._free(ptr);
|
|
return out;
|
|
}
|
|
|
|
test('an untrained handle reports an empty history', () => {
|
|
const ml = mlCreate(2, 1, 0, 0, 7);
|
|
expect(mlLossHistory(ml, 0, 0)).toBe(0);
|
|
mlDestroy(ml);
|
|
});
|
|
|
|
test('a training run records ONE entry per iteration, not a 1-element fake', () => {
|
|
const { ml, loss } = trainedNet(40);
|
|
const count = mlLossHistory(ml, 0, 0);
|
|
expect(count).toBe(40);
|
|
// The pre-§6.5e worker synthesised `new Float32Array([loss])`.
|
|
expect(count).toBeGreaterThan(1);
|
|
|
|
const hist = readHistory(ml);
|
|
expect(hist).toHaveLength(40);
|
|
for (const v of hist) expect(Number.isFinite(v)).toBe(true);
|
|
// The last recorded epoch loss IS what train() returned.
|
|
expect(Math.abs(hist[39]! - loss)).toBeLessThan(1e-6);
|
|
// A real fit descends.
|
|
expect(hist[39]!).toBeLessThan(hist[0]!);
|
|
mlDestroy(ml);
|
|
});
|
|
|
|
test('a truncated read returns the TOTAL count and fills the prefix', () => {
|
|
const { ml } = trainedNet(40);
|
|
const full = readHistory(ml);
|
|
const ptr = M._malloc(5 * 4);
|
|
new Float32Array(M.HEAPF32.buffer, ptr, 5).fill(-1);
|
|
const total = mlLossHistory(ml, ptr, 5);
|
|
const partial = Array.from(new Float32Array(M.HEAPF32.buffer, ptr, 5));
|
|
M._free(ptr);
|
|
|
|
expect(total).toBe(40); // total available, not the number written
|
|
expect(partial).toEqual(full.slice(0, 5));
|
|
mlDestroy(ml);
|
|
});
|
|
|
|
test('early convergence truncates the curve to the iterations actually run', () => {
|
|
// An absurd min_err makes train() break after the first iteration.
|
|
const { ml } = trainedNet(50, 1e9);
|
|
expect(mlLossHistory(ml, 0, 0)).toBe(1);
|
|
mlDestroy(ml);
|
|
});
|
|
|
|
test('a fresh run REPLACES the curve rather than appending to it', () => {
|
|
const { ml } = trainedNet(40);
|
|
expect(mlLossHistory(ml, 0, 0)).toBe(40);
|
|
mlTrain(ml, 0.5, 3, 0, 0);
|
|
expect(mlLossHistory(ml, 0, 0)).toBe(3);
|
|
mlDestroy(ml);
|
|
});
|
|
|
|
test('a null handle is safe and reports nothing', () => {
|
|
expect(mlLossHistory(0, 0, 0)).toBe(0);
|
|
});
|