Repository navigation
Description
Follow-up from #155 / PR #156, which added Fortran-parity LLt output (per-timepoint, per-model log-likelihood) to both backends.
Current design
Neither backend retains a full-dataset per-sample log-likelihood after the E-step: it is computed per block inside _get_block_updates and discarded once folded into sufficient statistics. So #156 recomputes it with a fresh full-dataset forward pass at write time. This was the right call for that PR (it is correct after a keep_best rollback, and it avoids retaining the sphered dataset), but it has two known costs.
Costs
- Repeated recompute on the NumPy backend.
_write_resultsruns at everywritestepcheckpoint during training, not only at the end (defaultwritestep=100; the bundled sample config uses 20). Each checkpoint now pays a full O(n_samples) forward pass it did not pay before. Bounded and small at the default, and NumPy is the legacy backend, which is why Phase 3: posterior-probability (LLt) output parity with Fortran #156 accepted it. - One extra full E-step per torch fit.
fit()computes LLt once at the end even ifwrite_amica_outputis never called.
Cheaper design
Stash the per-sample logV as it is computed during the training E-step and reuse it at write time, which is what Fortran does: it keeps modloglik(num_models, N) permanently allocated (amica15.f90:2601) and write_output just dumps it. Memory cost is (n_models+1) * N * 8 bytes, far smaller than the sphered data.
Two attractive side effects:
- It reproduces the
do_rejectzero sentinel for free. Fortran's E-step only writesmodloglikat good indices, so rejected samples naturally retain their zero-initialized value; Phase 3: posterior-probability (LLt) output parity with Fortran #156 has to arrange that explicitly by scattering into zero-filled arrays. - It matches Fortran's write cadence exactly.
Why it was not done in #156
It collides with the torch backend's keep_best rollback (#51): a stashed logV from the last training iteration would not correspond to the restored best iterate's parameters. Making it work means snapshotting the stashed logV alongside the parameters in _snapshot_params/_restore_params, which is a real design change to the best-iterate mechanism and was out of scope.
Note this would also move pyAMICA onto Fortran's one-M-step LLt staleness (Fortran fills modloglik during iteration i's E-step, the M-step then updates the parameters, and write_output writes both, so its on-disk LLt is stale by one M-step relative to the W/A written alongside it). #156 deliberately diverges here: recomputing post-update makes pyAMICA's LLt self-consistent with the written parameters. Any work on this issue should decide that tradeoff explicitly rather than inherit it by accident.
Acceptance
- No extra full-dataset forward pass at write time on either backend.
keep_bestrollback still yields an LLt consistent with the exported parameters.- The
do_rejectzero-sentinel behavior and all of Phase 3: posterior-probability (LLt) output parity with Fortran #156's LLt tests still pass. - Single-model Verify NG init-basin vs Newton bit-parity via Fortran load_* init-matching #24 parity stays bit-exact.
Activity
Phase 3: MLX rejection, LLt, export, MIR (#311)