Skip to content

Add matched-stopping precision experiment prep - #291

Merged
neuromechanist merged 4 commits into
devfrom
feature/issue-209-precision-prep
Aug 23, 2026
Merged

neuromechanist merged 4 commits into
devfrom
feature/issue-209-precision-prep

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Aug 23, 2026 •

Copy link
Copy Markdown
Member

Summary

This is the prep half of #209: a self-contained experiment script plus a README for the CUDA run, no results and no docs/guides/validation.md changes yet (those land once the CUDA run's numbers exist).
.context/issue-209/precision_experiment.py runs the f32-vs-f64 precision contrast and the Fortran-vs-torch implementation contrast under matched stopping, per the issue's design-decision comment: every arm runs to an identical fixed iteration budget (default 2000 and 6000) with every early-stop path disabled, so precision is the only variable in the precision contrast.
It also runs an unconstrained diagnostic (default stopping, capped at the largest budget) that records whether/when/why each precision would have stopped early on its own, per the issue's second comment, which asks for that behavior to be reported as a candidate float32 finding rather than engineered away.
.context/issue-209/README.md gives the one command for the CUDA box, a runtime estimate, and an analysis plan mapping the future results onto the specific confounded rows they replace (docs/guides/validation.md:278-284, the k=152 table) and the rows that are not confounded (docs/guides/validation.md:200-204, the k=30 claim) plus the JOSS paper's LL claim (paper.md:119).

Design notes

Suppressing every AMICATorchNG early-stop path needs no monkeypatching, which the issue's second comment asked to check: use_min_dll=False/use_grad_norm=False disable the two standalone per-iteration checks.
Of the two unconditional decrease-branch checks, min_nd=-1.0 is genuinely unreachable (a vector norm is never negative); minlrate=-1.0 (not 0.0, see "Review round 1 fixes" below) closes the other, since lrate is provably always >= 0.
The Fortran binary takes the mirror-image four param-file overrides; amica15.f90:1058 has the identical unconditional decrease-branch check, confirmed by reading the reference source directly.
Component correlation reuses the repo's established Hungarian-matched row-normalized-cosine convention (validate_implementations.py/benchmarks/benchmark_decompose.py), not a fresh metric.

A finding from the smoke run

The initial CPU smoke test at a too-small k produced a silent all-NaN Fortran fit (LL stuck at exactly 0.0, exit code 0, no error message): amica15's fixed-block-size path splits frames across max_threads OpenMP segments before chunking each segment by block_size, and if block_size exceeds one segment's length the run silently degenerates.
Bisected directly (frames=5120, threads=4: block_size=1280 gives a real fit, block_size=1281 gives all-NaN).
The script clamps block_size to at most frames // threads for the Fortran call only (torch has no such per-thread split and is unaffected).
This is a real amica15 behavior worth knowing for anyone else driving the reference binary at small data/high thread counts, not a bug in this script, and is documented in the script's comments.

Review round 1 fixes

Two correctness gaps in the matched-stopping guarantee, found by review before the unattended CUDA run:

CRITICAL -- Fortran NaN detection had a blind spot. amica15.f90:1054 ("Got NaN! Exiting") and :1060 ("minimum change threshold met") print to stdout only, with no write(20,...) twin, while the min_dll/grad_norm messages at :1083-1085/:1094-1095 are written to both.
The script previously scanned only run_out/out.txt, so a NaN'd Fortran arm parsed as a clean max_iter stop with final_ll=nan, which passed the old degenerate check (nan == 0.0 is False) and could reach _match_correlation with a NaN W.
Fixed by also scanning the already-captured res.stdout for _STOP_MESSAGES, and by broadening degenerate to include math.isnan(final_ll) as an independent second check.

IMPORTANT -- minlrate=0.0 was not actually unreachable. float32 lrate underflows to an exact 0.0 after ~147 consecutive LL-decrease halvings (lratefact=0.5 applied on every decrease), which is plausible well within a 2000-6000 iteration budget; at that point lrate <= 0.0 fires lrate_floor mid-"matched" run, reproducing the exact confound #209 exists to kill.
Fixed by using minlrate=-1.0 on the torch side (lrate is provably always >= 0) and "minlrate -1.000e+00" in _write_fortran_param (the E15.3 field reads scientific notation cleanly; min_grad_norm stays at its F15.12-field 0, which was already genuinely unreachable).
Also gated precision_correlation/implementation_correlation row inclusion on stop_reason == "max_iter" in addition to not-degenerate, so any early stop that somehow survives the suppression is excluded from the aggregates rather than silently averaged in.

IMPORTANT -- implementation-contrast block-size confound. The Fortran arm's per-thread block-size clamp (above) can land below the validated 512-8192 range on a many-core box while torch keeps the full requested block_size, so a low implementation-contrast correlation could be a block-size artifact rather than a genuine Fortran-vs-torch difference.
threads is now recorded in _config_header, and fortran_block_size/threads are recorded on every implementation_correlation row and printed in the run's summary output (not only the raw JSON), with an explicit "BELOW FLOOR" flag when the clamp lands under 512.
README item 3 now states this caveat explicitly.

Also added two one-line README caveats: CUDA kernel non-determinism is uncontrolled (no torch.use_deterministic_algorithms), so an anomalous single-seed dip should be checked against a re-run before being attributed to precision; and preprocessing (mean removal, sphering) always runs in float64 on CPU regardless of --device/precision, so the precision contrast is strictly about the EM iterations.

Smoke-run evidence (CPU, small budget, post-fix)

uv run python .context/issue-209/precision_experiment.py --device cpu --budgets 20 --seeds 0 --k-values 5
data: bundled:pamica/sample_data/eeglab_data.fdt  channels=32  available_frames=30504
device=cpu  block_size=8192  n_mix=3  do_newton=False
fortran binary: pamica/sample_data/amica15mac  available
  k~5.0: block_size clamped 8192 -> 5120 (frames=5120)
  k~5.0: fortran block_size further clamped 5120 -> 365 (frames=5120, threads=14; amica15 splits frames across threads before blocking)
  [unconstrained] k~  5.0 seed=0 f32 -> stop=max_iter               iter=   20/20  LL=-3.26687  (0.1s)
  [unconstrained] k~  5.0 seed=0 f64 -> stop=max_iter               iter=   20/20  LL=-3.26686  (0.1s)
  [matched k~  5.0 seed=0 budget=20] torch-f32 stop=max_iter   LL=-3.26687  (0.1s)
  [matched k~  5.0 seed=0 budget=20] torch-f64 stop=max_iter   LL=-3.26686  (0.1s)
  [matched k~  5.0 seed=0 budget=20] fortran-f64 stop=max_iter   LL=-3.27647  (0.2s)

=== summary ===
matched runs: 3  stopped via max_iter (expected): 3/3  degenerate: 0
unconstrained runs: 2  stopped early: 0
precision contrast (f32 vs f64): mean|corr| over 1 (k,seed,budget) points = 1.0000 (min 1.0000)
implementation contrast (fortran-f64 vs torch-f64): mean|corr| over 1 (k,seed,budget) points = 0.9966 (min 0.9966)
  fortran ran at threads=14, block_size in [365] (validated range: 512-8192) -- BELOW FLOOR at [365]: an implementation gap at that k could be a block-size artifact, not precision -- see README

A second, wider smoke pass (--budgets 15,25 --seeds 0,1 --k-values 5,10, 24 matched runs across both k values, both budgets, both seeds) also completed clean: all 24 matched runs stopped via max_iter, zero degenerate, precision mean|corr|=1.0000, implementation mean|corr|=0.9969 (min 0.9954), threads=14, fortran_block_size in [365, 731] (365 flagged below floor, 731 not).

Test plan

  • uv run ruff check .context/issue-209/precision_experiment.py and ruff format --check clean.
  • uv run ty check . clean (.context/** is excluded from ty project-wide, per pyproject.toml, but confirmed anyway).
  • CPU smoke run above completes end-to-end with every matched arm stopping via max_iter (confirms stop suppression actually holds, including after the minlrate=-1.0 fix) and non-degenerate correlations.
  • git status after the run is clean (no stray output files land in the repo tree; --out defaults next to the script for the real CUDA run).
  • The real CUDA run and the docs/guides/validation.md rewrite are follow-up work per the approved plan.

Partially addresses #209.

@neuromechanist
neuromechanist merged commit a9af99b into dev Aug 23, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-209-precision-prep branch August 23, 2026 22:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant