memlnaut-nisps/scripts/bench-engines.sh

165 lines
6.3 KiB
Bash
Raw Normal View History

feat: curve truth, DriverConfig, real telemetry, engine benchmark 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.
2026-07-21 22:02:23 +02:00
#!/usr/bin/env bash
# scripts/bench-engines.sh — measure engine throughput on BOTH host targets.
#
# The repo's performance constraint ("super performance-sensitive", ALIGNMENT
# defect 5) was enforced entirely by static discipline: a no-heap lint, section
# attributes, and — since Phase 4 — a firmware flash/RAM report. Nothing
# measured time. This does.
#
# It builds tests/cpp/engine_bench.cpp twice from ONE source:
# native — CMake target nisps_engine_bench (Release/-O3)
# wasm — emcc, with the same flags scripts/build-wasm.sh uses for the
# shipped module, run under node
# then prints a side-by-side table of ns/sample, blocks/s and realtime factor
# per engine, plus a wasm/native ratio.
#
# NOTHING HERE FAILS. A wall-clock threshold on shared CI hardware is either
# slack enough to be meaningless or tight enough to fail on an unrelated noisy
# runner — the same call the firmware size job made. A regression is noticed by
# running this with --compare against a previous report, which prints per-engine
# Δ% (positive = slower). Reports are plain JSON; keep one around to diff.
#
# Usage:
# scripts/bench-engines.sh # native + wasm, full run
# scripts/bench-engines.sh --native-only # skip emcc
# scripts/bench-engines.sh --smoke # ~1 s, proves it still runs
# scripts/bench-engines.sh --engine verb_fx # one engine
# scripts/bench-engines.sh --compare old.json # diff vs a previous report
# scripts/bench-engines.sh --out bench-2026-07-21.json
#
# Env:
# NISPS_BUILD_DIR default nisps/build
# NISPS_BENCH_NO_BUILD 1 = never invoke a build; fail if artifacts missing
# EMCC emcc path (same convention as build-wasm.sh)
#
# Exit codes: 0 on a completed run, 2 on missing artifacts/args, 3 on build
# failure.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
BUILD_DIR="${NISPS_BUILD_DIR:-$ROOT/nisps/build}"
BENCH_DIR="$BUILD_DIR/bench"
NATIVE_BIN="$BUILD_DIR/nisps_engine_bench"
SRC="$ROOT/tests/cpp/engine_bench.cpp"
REPORT="$ROOT/tests/cpp/bench_report.mjs"
NO_BUILD="${NISPS_BENCH_NO_BUILD:-0}"
run_native=1
run_wasm=1
out_path=""
compare_path=""
bench_args=()
while [[ $# -gt 0 ]]; do
case "$1" in
--native-only) run_wasm=0; shift ;;
--wasm-only) run_native=0; shift ;;
--out) out_path="${2:?--out needs a path}"; shift 2 ;;
--compare) compare_path="${2:?--compare needs a path}"; shift 2 ;;
--smoke) bench_args+=("--smoke"); shift ;;
--engine|--repeats|--target-ms|--block-size|--sample-rate|--seed)
bench_args+=("$1" "${2:?$1 needs a value}"); shift 2 ;;
-h|--help)
sed -n '2,40p' "$0" | sed 's/^# \{0,1\}//'
exit 0 ;;
*)
echo "[bench-engines] unknown argument: $1" >&2
exit 2 ;;
esac
done
if [[ $run_native -eq 0 && $run_wasm -eq 0 ]]; then
echo "[bench-engines] --native-only and --wasm-only are mutually exclusive" >&2
exit 2
fi
mkdir -p "$BENCH_DIR"
runs=()
# ---------------------------------------------------------------------------
# Native
# ---------------------------------------------------------------------------
if [[ $run_native -eq 1 ]]; then
if [[ ! -x "$NATIVE_BIN" ]]; then
if [[ "$NO_BUILD" == "1" ]]; then
echo "[bench-engines] missing $NATIVE_BIN and NISPS_BENCH_NO_BUILD=1" >&2
exit 2
fi
echo "[bench-engines] native binary missing — running build-cpp-tests.sh"
NISPS_RUN_TESTS=0 "$ROOT/scripts/build-cpp-tests.sh" >/dev/null || {
echo "[bench-engines] C++ build failed" >&2
exit 3
}
fi
echo "[bench-engines] running native..."
"$NATIVE_BIN" --json --label native "${bench_args[@]}" > "$BENCH_DIR/native.json"
runs+=("$BENCH_DIR/native.json")
fi
# ---------------------------------------------------------------------------
# WASM — same source, same optimisation/exception/RTTI flags as the shipped
# module (scripts/build-wasm.sh), plus a node-shaped runtime. STACK_SIZE is
# raised for the same reason build-wasm.sh raises it: the DSP objects are big.
# ---------------------------------------------------------------------------
if [[ $run_wasm -eq 1 ]]; then
EMCC="${EMCC:-$(command -v emcc || echo /usr/lib/emscripten/emcc)}"
if [[ "$EMCC" != */* ]]; then EMCC="$(command -v "$EMCC" || echo "$EMCC")"; fi
if [[ ! -x "$EMCC" && ! -f "$EMCC" ]]; then
echo "[bench-engines] emcc not found at $EMCC — skipping the WASM leg" >&2
echo "[bench-engines] (set EMCC=/path/to/emcc, or pass --native-only)" >&2
run_wasm=0
elif ! command -v node >/dev/null 2>&1; then
echo "[bench-engines] node not on PATH — skipping the WASM leg" >&2
run_wasm=0
fi
fi
if [[ $run_wasm -eq 1 ]]; then
if [[ "$NO_BUILD" == "1" && ! -f "$BENCH_DIR/engine_bench.js" ]]; then
echo "[bench-engines] missing $BENCH_DIR/engine_bench.js and NISPS_BENCH_NO_BUILD=1" >&2
exit 2
fi
if [[ "$NO_BUILD" != "1" ]]; then
echo "[bench-engines] compiling WASM bench..."
"$EMCC" "$SRC" \
-std=c++20 -O3 \
-fno-exceptions \
-fno-rtti \
-s ENVIRONMENT=node \
-s ALLOW_MEMORY_GROWTH=1 \
-s INITIAL_MEMORY=16777216 \
-s STACK_SIZE=1048576 \
-s ASSERTIONS=0 \
-s EXIT_RUNTIME=1 \
-o "$BENCH_DIR/engine_bench.js" || {
echo "[bench-engines] WASM build failed" >&2
exit 3
}
fi
echo "[bench-engines] running wasm..."
node "$BENCH_DIR/engine_bench.js" --json --label wasm "${bench_args[@]}" > "$BENCH_DIR/wasm.json"
runs+=("$BENCH_DIR/wasm.json")
fi
# ---------------------------------------------------------------------------
# Report
# ---------------------------------------------------------------------------
if [[ ${#runs[@]} -eq 0 ]]; then
echo "[bench-engines] no target ran" >&2
exit 2
fi
report_args=("${runs[@]}")
report_args+=(--out "${out_path:-$BENCH_DIR/latest.json}")
if [[ -n "$compare_path" ]]; then
if [[ ! -f "$compare_path" ]]; then
echo "[bench-engines] --compare file not found: $compare_path" >&2
exit 2
fi
report_args+=(--compare "$compare_path")
fi
echo ""
node "$REPORT" "${report_args[@]}"