Skip to content

Phase 13: use the reference's single-precision constants - #349

Merged
neuromechanist merged 13 commits into
feature/issue-324-epic-mlx-first-classfrom
feature/issue-344-phase13-float32-consts
Sep 23, 2026
Merged

neuromechanist merged 13 commits into
feature/issue-324-epic-mlx-first-classfrom
feature/issue-344-phase13-float32-consts

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Summary

The reference writes several density normalizers as default-kind (single-precision) Fortran literals widened with dble, for example log(dble(2.506628274)), so the binary uses the float32 rounding of each decimal, not the decimal. pamica used the decimals' double values. All three backends (PyTorch, NumPy, MLX) now read the reference's values from one new shared module, pamica/reference_constants.py: the generalized Gaussian's rho == 2 normalizer (log(dble(1.772453851)), amica15.f90:1313), the Gaussian and the two cosh families (pdftype 2, 4 and 1, :1333, :1359, :1371), and the rho-update guard epsdble (1.0e-16, amica15_header.f90:73, hard-coded, used at :1558). The dyadic log(dble(2.0)) and log(dble(4.0)) move into the same module at their exact values. MLX casts the values to float32; at rho == 2 its host-side lgamma table now holds LOG_SQRT_PI - LOG2, so LOG2 + table is the reference's normalizer.

The default maxrho = 2 clamp makes the rho == 2 branch common (a two-model fit on 4096 samples of the bundled recording reaches it on its fifth iteration), so default GG trajectories move by a tiny amount toward the reference. The merged-state oracle of Phase 8 no longer needs its maxrho = 1.99 workaround: seeded at rho == 2, PyTorch and NumPy match the binary to float64 round-off, where the code before this change is off by 2.8e-9 in log-likelihood after one iteration and 1.2e-8 after three (and 8.4e-3 in the mu of a low-mass mixture). New seeded oracles for pdftype 2, 4 and 1 show the pre-change log-likelihood off by exactly each literal's rounding (3.66e-10, 2.02e-8, -2.06e-8), now 2.7e-15 or less; this also confirms against the binary the three offsets Phase 8 had only computed.

Parameters the reference reads from input.param are unchanged (lead's decision): the binary's compiled-in defaults are single-precision too (lrate prints as 0.1000000015 when the key is missing, 0.10000000000000001 when given), but pamica's native engine and the seeded oracles write every such key. Checking that also showed a reference quirk: without an lrate key its ceiling lrate0 is never set (amica15.f90:3617 is its only assignment), so the binary's lrate drops to 0 from the second iteration (min(lrate0, ...), :1812); row 16 says so. Row 16 of the differences guide is rewritten to record exactly that remaining difference; the fix itself gets its own section. The initialization scales (0.05, 0.1, 0.01) are single-precision literals too but are init code (Phase 12's) drawn from a different generator, so they are recorded and not adopted.

The three tests that compare a live backend bit for bit with code from before this change (Phase 8's layout comparison, Phase 7's doscaling=False comparison, and the MLX pre-Phase-3 no-op check) failed because their two-model and non-GG fits reach the changed normalizers. They now give the live backend its pre-#344 constants first through a new helper, pamica.tests.pre_change.use_pre_344_constants (monkeypatches the five module constants; every code path still runs), so each still isolates the change it was written for, at full byte-identity strength.

Review follow-up: the NumPy plotting helper numpy_impl.pdf.compute_pdf (drawn by viz.plot_pdf_fits) now takes its normalizers from pamica.reference_constants, including the single-precision sqrt(pi) at rho == 2, so it draws the density the fit uses; its module docstring now says it is a plotting helper with the legacy pdftype numbering, and its unused compute_log_pdf is removed. The no-copy test scans every .py file of torch_impl, numpy_impl and mlx_impl (comments and strings blanked) and flags linear forms (sqrt(pi), sqrt(2 * np.pi), ...) as well as log forms and literals; a companion test checks it flags every definition this change removed, and restoring the old pdf.py line makes it fail (checked by hand). A control test checks that use_pre_344_constants changes every constant it patches, covers every changed constant each backend reads, and restores the pre-change MLX rho == 2 table entry. The family oracle asserts that every source kept its family and no kurtosis switch ran (n_kurt_done == 0).

Part of epic #324; resolves #344 (closes when the epic merges to dev).

The literal sweep

Every real literal in amica15.f90's code is dble(<default-kind literal>); there are no bare or d0 literals (pinned by test_every_inexact_single_precision_literal_in_the_reference_is_accounted_for). funmod2.f90 (gamln, psifun) uses D-exponent double literals throughout, plus exact 1.0.

Line Literal float32 rounding (as float64) pamica before Exact in float32? Matters? Action
amica15.f90:1313 log(dble(1.772453851)), GG at rho == 2 1.7724539041519165 0.5*log(pi) (MLX: log 2 + lgamma(1.5)) no; log +3.0e-8 yes, default maxrho clamp; adopted (LOG_SQRT_PI)
amica15.f90:1333 log(dble(2.506628274)), pdftype 2 2.5066282749176025 log(2.506628274) no; log +3.7e-10 yes; adopted (LOG_SQRT_2PI)
amica15.f90:1359 log(dble(4.132731354)), pdftype 4 4.1327314376831055 log(4.132731354) no; log +2.0e-8 yes; adopted (LOG_NORM_COSH_SUB)
amica15.f90:1371 log(dble(1.858073988)), pdftype 1 1.8580739498138428 log(1.858073988) no; log -2.1e-8 yes; adopted (LOG_NORM_COSH_SUP)
amica15_header.f90:73, used amica15.f90:1558 epsdble = 1.0e-16 (never read from input.param) 1.0000000168623835e-16 1e-16 no; +1.7e-24 threshold on `
amica15.f90:1308, :1324 log(dble(2.0)) 2.0 log(2.0) yes shared as LOG2, same value
amica15.f90:1346 log(dble(4.0)), logistic 4.0 log(4.0) yes shared as LOG4, same value
amica15.f90:756 dble(0.05), mu init jitter 0.05000000074505806 0.05 no init scale; pamica's RNG differs, so the reference init is not reproducible either way; init code is Phase 12's; not adopted
amica15.f90:771 dble(0.1), sbeta init 0.10000000149011612 0.1 no same; not adopted
amica15.f90:814, :1035 dble(0.01), A init and NaN-restart redraw 0.009999999776482582 0.01 no same; not adopted
amica15.f90:2210-2211 dble(-999999999.0), dble(999999999.0) -/+1.0e9 none no starting values of the rejection pass's running max/min; never enter a result
amica15.f90:2171 dble(1.5) 1.5 n/a yes block-size timing probe only
everywhere else dble(0.0), 1.0, 2.0, 0.5, -1.0, 0.25, 4.0 exact exact yes none
amica15_header.f90:66 mineig = 1.0e-15 1.0000000036274937e-15 1e-15 no input.param key; unchanged (row 16)
amica15_header.f90:68 lrate = 0.1, minlrate = 1.0e-12, rholrate = rholrate0 = 0.05 0.10000000149, 9.99999996e-13, 0.0500000007 decimals no input.param keys; unchanged (row 16)
amica15_header.f90:70 rholratefact = 0.1 0.10000000149 0.1 no input.param key; unchanged
amica15_header.f90:72 invsigmin = 0.0001 9.9999997e-05 1e-4 no input.param key; unchanged
amica15_header.f90:74 comp_thresh = 0.99, min_dll = 1.0e-9, min_nd = 1.0e-7 (key min_grad_norm) 0.99000000954, 9.9999997e-10, 1.00000001e-07 decimals no input.param keys; unchanged
amica15_header.f90:66, :73 mincond = 1.0e-15, maxdble = 1.0e32, mineigv = 1.0e-15, minhess = 1.0e-5 inexact none no declared, never read
amica15_header.f90:66-74 minrho = 1.0, maxrho = 2.0, minlog = -1500.0, rejsig = 3.0, lratefact = 0.5, rho0 = 1.5, invsigmax = 1000.0, newtrate = 0.5 exact exact yes none

Oracle numbers (native binary v0.3.3, float64)

rho == 2, merged two-model warm state (test_component_rows.py::test_updates_from_a_merged_state_match_the_seeded_reference, maxrho = 2.0, maxima over PyTorch/NumPy and doscaling on/off; bounds unchanged):

A mu sbeta LL
before (#344 code replayed), 1 iteration 2.6e-10 1.8e-8 5.0e-9 2.8e-9
after, 1 iteration 1.3e-15 1.7e-12 1.3e-14 2.2e-15
before, 3 iterations 3.4e-8 8.4e-3 2.6e-6 1.2e-8
after, 3 iterations 3.8e-13 5.7e-9 2.9e-11 1.6e-13
bound, 1 / 3 iterations 1e-13 / 1e-10 1e-9 / 1e-6 1e-12 / 1e-8 1e-13 / 1e-11

Same state with no merge (noise floor): 1 iteration A 1.1e-15, mu 1.7e-12, sbeta 1.2e-14, LL 0; 3 iterations A 5.1e-14, mu 3.3e-9, sbeta 1.9e-12, LL 1.3e-14. The pre-#344 replay is now an in-test control (every quantity over its bound, LL over 10x).

Fixed families, seeded from pamica's initialization (test_reference_constants.py::test_family_matches_the_seeded_reference, PyTorch):

pdftype LL before (pre-#344, 1 and 3 its) LL after, 1 it LL after, 3 its A / mu / sbeta after (max over 1 and 3 its)
2 +3.66070e-10 0.0 4.4e-16 1.6e-15 / 1.8e-14 / 1.5e-14
4 +2.02489e-8 8.9e-16 1.3e-15 1.3e-15 / 6.8e-15 / 1.6e-14
1 (held at code 1) -2.05515e-8 2.7e-15 2.7e-15 1.9e-15 / 1.4e-15 / 1.1e-14

Before the change the A/mu/sbeta errors were the same (the normalizer shifts every mixture of a family alike); only the log-likelihood was off, by exactly log(float32(lit)) - log(lit). The test asserts that for the replayed pre-#344 code, to 1e-12.

The other gated oracles (test_doscaling_rows, test_iteration_order_native_oracle, test_schedule_native_oracle, test_llt_stash, test_fortran_param_forwarding, test_ng_pdf_families converged LL) pass with their bounds unchanged.

Existing tests changed

  • torch_tests/test_ng_pdf_families.py, test_mlx_pdf_families_cross_backend.py: the "literal Fortran" normalizers are now the float32 roundings of the literals (the binary's values); tolerances unchanged (1e-12 and 1e-6).
  • torch_tests/test_ng_backend.py: its independent rho == 2 log-density uses the reference's normalizer.
  • test_component_rows.py: merged oracle at maxrho = 2.0; the seed guard flipped from rho.max() < 2 to "some mixture sits at rho == 2" (so the branch is exercised); pre-Use the reference's single-precision density constants #344 control added; measured-numbers comment updated; bounds unchanged. The byte-identity test against 0930c0e calls use_pre_344_constants.
  • test_doscaling_rows.py::test_doscaling_off_is_byte_identical_to_the_pre_fix_code and mlx_tests/test_mlx_fit_noop.py (both tests): call use_pre_344_constants.
  • benchmarks/reproduce_table1.py: its literal-formula copy uses the reference's values.
  • test_pamica.py::test_pdf_computation and test_amica.py::test_pdf_computation: the rho == 2 expectation of compute_pdf is the reference's normalizer (tolerance unchanged).

No MLX float32 pin moved, so no new perturbation-derived tolerance was needed.

Test plan

  • uv run ruff check, ruff format --check, ty check, typos
  • mkdocs build --strict (isolated env with the docs extra)
  • New pamica/tests/test_reference_constants.py: 55 passed with AMICA_RUN_FORTRAN=1 on Apple Silicon (60 passed, 5 skipped with the pdf tests in an env without MLX)
  • All gated oracles with AMICA_RUN_FORTRAN=1, plus the byte-identity, pdf and viz tests: 350 passed
  • Full non-slow suite with CI=1: 1910 passed, 34 skipped, 1 xfailed (after the review follow-up)
  • -m slow with CI=1: 10 passed, 2 xfailed (the two existing test_sample_data.py xfails)
  • The retuned byte-identity files and the new test file in an environment without MLX (uv run --isolated, as on the Linux runner): 114 passed, 58 skipped
  • git rev-parse --is-shallow-repository: false

The reference writes its density normalizers as default-kind literals
widened with dble, so the binary uses their float32 roundings. All three
backends now read those values, and epsdble's, from one shared module,
pamica/reference_constants.py; MLX holds the rho == 2 normalizer in its
lgamma table. The literal-formula tests and the benchmark's copy now use
the reference's values. Tested: pdf-family and backend test files pass.
The byte-identity tests against code from before issue #344 (component
rows, doscaling off, the MLX pre-Phase-3 tip) now give the live backend
its old density constants through pre_change.use_pre_344_constants, so
they still isolate the change each was written for: their two-model and
non-GG fits reach the changed normalizers. Tested: the three files pass.
test_reference_constants.py checks each shared constant against the
float32 rounding of its literal on the cited reference line (exact
rational arithmetic), pins the sweep of amica15.f90 and its header,
checks that no backend keeps a copy, checks every backend's rho == 2
log-density, and (opt-in) seeds the native binary for pdftype 2, 4 and
1. Tested: 23 passed with AMICA_RUN_FORTRAN=1.
The merged-state oracle no longer holds maxrho at 1.99: its warm seed
now has mixtures clamped at 2, it asserts that, and the code before
issue #344 is replayed from the same state as a control (off by 2.8e-9
in log-likelihood after 1 iteration, against 2.2e-15 now). Tolerances
unchanged. Tested with AMICA_RUN_FORTRAN=1.
Changelog entry with the oracle numbers; the differences guide gains a
section on the constants, and row 16 now records the one kind of
single-precision literal pamica keeps at its decimal value (defaults of
input.param keys); the validation guide notes the new family oracle.
Tested: mkdocs build --strict, typos.
Without an lrate key the reference never sets its ceiling lrate0, so
its lrate drops to 0 after the first iteration (checked against the
pinned binary). Row 16 now says so and uses comp_thresh as the example.
Tested: mkdocs build --strict, typos.
On a host without MLX the byte-identity tests imported the MLX module
to patch it before the check that skips them. Resolve the classes
first. Tested: the two files pass with MLX and skip its cases without.
numpy_impl.pdf.compute_log_pdf had no callers in the package, tests or
docs. Tested: the pdf and viz tests pass.
compute_pdf, which viz.plot_pdf_fits draws, now takes its normalizers
from pamica.reference_constants, including the reference's
single-precision sqrt(pi) at rho == 2, so it draws the density the fit
uses. Its docstring says it is a plotting helper with the legacy
pdftype numbering. Tested: the pdf, viz and constants tests pass.
The no-copy test now scans every .py file of torch_impl, numpy_impl and
mlx_impl with comments and strings blanked, and flags the linear forms
(sqrt(pi), sqrt(2*pi), ...) as well as the log forms and the literals.
A new test checks that it flags every definition this change removed.
Tested: passes; fails on the old pdf.py sqrt(pi) line (checked by hand).
The family oracle now asserts that every source kept its family and no
kurtosis switch ran (n_kurt_done == 0), for pdftype=1 with num_kurt=0
too, in the live and the pre-change runs. Tested with
AMICA_RUN_FORTRAN=1.
A control test: every constant use_pre_344_constants patches differs
from the live value, the patch takes effect, it covers every changed
constant each backend module reads, and on MLX it restores the
pre-change rho == 2 table entry. Tested: passes on all three backends.
compute_pdf now draws the fit's density and compute_log_pdf is gone.
Tested: mkdocs build --strict, typos.
@neuromechanist
neuromechanist merged commit 0138a71 into feature/issue-324-epic-mlx-first-class Sep 23, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-344-phase13-float32-consts branch September 23, 2026 18:10
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