Skip to content

Stash per-sample logV during the E-step instead of recomputing for LLt #157

Closed

Description

@neuromechanist

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

  1. Repeated recompute on the NumPy backend. _write_results runs at every writestep checkpoint during training, not only at the end (default writestep=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.
  2. One extra full E-step per torch fit. fit() computes LLt once at the end even if write_amica_output is 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_reject zero sentinel for free. Fortran's E-step only writes modloglik at 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions