Repository navigation
Description
Overview
TruncatedGaussianPrior.value_for is the #1 cProfile hotspot identified
by the graphical-ep-scale-up scoping pass (PR #17 on
autofit_workspace_developer): it accounts for 33% of total wall time
in a graphical-model joint Dynesty fit at N=10, and ~16% of an EP
toy run at N=10. The current code dispatches on xp (numpy vs JAX) but
on both paths goes through scipy.stats.norm.cdf / ppf, whose
_distn_infrastructure wrapper overhead dwarfs the underlying erf
computation. This task replaces both paths with direct erf / erfinv
calls — mathematically identical to within ULPs.
Expected wall-time reduction: ~30% for graphical, ~17% for EP at
N=10. Numerics must match the pre-fix baseline to 1e-6 relative
tolerance — that's the merge gate.
Plan
- Add a small helper (in
autofit/mapper/prior/) that exposes a tail-safetruncated_normal_value_for(unit, mean, sigma, lower, upper, xp)built onxp.special.erf/erfinv/erfc/erfcinv. - Replace the body of
TruncatedGaussianPrior.value_for(and the equivalent code inTruncatedNormalMessageif it routes throughscipy.stats.normtoo). - Add a numerical-equivalence test that asserts the new path matches
scipy.stats.truncnorm.ppfto1e-12relative error over a parameter grid that includes deep tails (a=10/b=20, narrow brackets). - Re-run the
autofit_workspace_developer/graphical/fit.pyandep/fit.pybaselines at N=3/10/30. Posterior means, max log L, and sanity checks must match the pre-fixprofiles/baseline.jsonto1e-6relative tolerance. Wall-time reduction is the speed gate (≥20% graphical, ≥10% EP at N=10). - All existing
test_autofit/mapper/prior/andtest_autofit/non_linear/tests pass unchanged.
Detailed implementation plan
Affected Repositories
PyAutoFit(primary)autofit_workspace_developer(post-merge baseline refresh — separate PR)
Work Classification
Library (PyAutoFit). Workspace baseline refresh happens after the library lands.
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
./PyAutoFit |
main | clean |
Suggested branch: feature/truncated-gaussian-fast-path
Worktree root: ~/Code/PyAutoLabs-wt/truncated-gaussian-fast-path/ (created later by /start_library)
Implementation Steps
- Read
autofit/mapper/prior/truncated_gaussian.py:136(value_for)andautofit/messages/truncated_normal.py. Decide whether the helper lives in a new private module (autofit/mapper/prior/_erf_helpers.py) or as a static method onTruncatedGaussianPrior. Lean private module so the EP scoping follow-up (paths.ep_modeflag) can also reuse it. - Implement
truncated_normal_value_for(unit, mean, sigma, lower, upper, xp):a = (lower - mean) / sigma; b = (upper - mean) / sigmaPhi_a = 0.5 * (1 + erf(a / sqrt(2))), switching to0.5 * erfc(-a / sqrt(2))whena < -1(or equivalently the tail-safe form)- Same for
Phi_b p = Phi_a + unit * (Phi_b - Phi_a)x = sqrt(2) * erfinv(2*p - 1), switching tosqrt(2) * erfcinv(2*(1 - p))whenp > 0.9(and the symmetric form whenp < 0.1)return mean + sigma * x
- Replace
value_forbody intruncated_gaussian.pywith a call to the helper. Keep the docstring; update it to reflect the new implementation reference. - Audit
autofit/messages/truncated_normal.pyfor anyscipy.stats.norm.cdf/ppfcalls and route them through the helper too. - Add
test_autofit/mapper/prior/test_truncated_gaussian_erf.py(or extend the existing test) with a numerical-equivalence test againstscipy.stats.truncnorm.ppf. Parameter grid:unit ∈ {1e-9, 1e-6, 1e-3, 0.1, 0.3, 0.5, 0.7, 0.9, 1-1e-3, 1-1e-6, 1-1e-9}(mean, sigma, lower, upper) ∈ {(0,1,-3,3), (0,1,-10,10), (0,1,-20,-10), (0,1,10,20), (5,2,0,inf), (0,1,-0.001,0.001)}- tolerance:
1e-12relative for moderate cases;1e-9fora=10/b=20deep tails.
- Run
pytest test_autofit/mapper/prior/andpytest test_autofit/messages/— must pass unchanged. - From the
autofit_workspace_developercheckout (attached to the same task worktree), regenerate the graphical/ep baselines at N=3/10/30 and diff against the committedprofiles/baseline.json. Posterior values + max log L within1e-6rel; wall time meets the speed gate. - Ship the PyAutoFit PR. The workspace baseline refresh is a follow-up PR in autofit_workspace_developer.
Key Files
PyAutoFit/autofit/mapper/prior/truncated_gaussian.py:136—value_forreplaced.PyAutoFit/autofit/mapper/prior/_erf_helpers.py(new) — shared helper.PyAutoFit/autofit/messages/truncated_normal.py— verify + update if applicable.PyAutoFit/test_autofit/mapper/prior/test_truncated_gaussian.py— extended with the equivalence test.
Original Prompt
Click to expand starting prompt
TruncatedGaussianPrior.value_for — direct-erf fast path
Replace the scipy.stats.norm.cdf / jax.scipy.stats.norm.cdf calls inside
@PyAutoFit/autofit/mapper/prior/truncated_gaussian.py:136(value_for) with
direct erf / erfinv calls from scipy.special (numpy) and
jax.scipy.special (JAX). The current implementation routes through
scipy.stats._distn_infrastructure.cdf, which has substantial Python-side
wrapper overhead per call (arg validation, broadcasting setup, dispatch
chain) that dwarfs the actual erf computation.
This is the #1 cProfile hotspot identified by the
graphical-ep-scale-up scoping pass — see
PyAutoPrompt/graphical_ep/graphical_scoping.md and
PyAutoPrompt/graphical_ep/ep_scoping.md.
Motivation
cProfile attribution at N=10 from
autofit_workspace_developer/{graphical,ep}/profiles/N10_hotspots.txt:
| Package | Total | value_for cumtime |
scipy.stats..cdf cumtime |
% of total |
|---|---|---|---|---|
| graphical | 60 s | 22.7 s | 19.5 s (184 200 calls) | 33% |
| ep | 724 s | not isolated | 116.4 s (1 599 610 calls) | 16% |
Almost all of value_for's cumtime is the scipy.stats wrapper — the actual
erf math is fast. GaussianPrior.value_for (the non-truncated variant)
already uses the direct-erfinv approach on its JAX path
(gaussian.py:117); this prompt extends that pattern to the truncated
variant on both numpy and JAX.
Numerics-preserving identity
norm.cdf(z) ≡ 0.5 * (1 + erf(z / sqrt(2)))
≡ 0.5 * erfc(-z / sqrt(2)) (preferred when z << 0)
norm.ppf(p) ≡ sqrt(2) * erfinv(2*p - 1)
≡ -sqrt(2) * erfcinv(2*p) (preferred when p close to 1)
≡ sqrt(2) * erfcinv(2*(1-p)) (preferred when p close to 0)
Both forms are mathematically equivalent to scipy.stats.norm.cdf/ppf
and produce the same float64 values to within ULPs. The erfc/erfcinv
forms are used when the argument is in the tail to avoid catastrophic
cancellation.
For TruncatedGaussianPrior:
a = (lower_limit - mean) / sigma
b = (upper_limit - mean) / sigma
Phi_a = 0.5 * (1 + erf(a / sqrt(2))) # or erfc form if a << 0
Phi_b = 0.5 * (1 + erf(b / sqrt(2))) # or erfc form if b >> 0
p = Phi_a + unit * (Phi_b - Phi_a)
x = sqrt(2) * erfinv(2*p - 1) # or erfcinv forms if p near 0/1
return mean + sigma * x
Plan
- Add a new helper module (or inline functions in the same file)
that exposestruncated_normal_value_for(unit, mean, sigma, lower, upper, xp)
built onxp.special.erf/erfinv(importingscipy.specialfor numpy
andjax.scipy.specialfor jax). Centralise the tail-safeerfc/
erfcinvbranching so both the prior and the message classes can call
into it. - Replace the bodies of:
autofit/mapper/prior/truncated_gaussian.py:136(value_for)- The corresponding code path in
autofit/messages/truncated_normal.py(ifTruncatedNormalMessage
also usesscipy.stats.norm.cdf— verify; reuse the helper if so).
- Numerical equivalence test in
test_autofit/mapper/prior/test_truncated_gaussian.py: compare the
newvalue_foragainstscipy.stats.truncnorm.ppf(the
library-agnostic ground truth, not the old code path) on a grid of
(unit, mean, sigma, lower, upper)including extreme truncations
(a=10/b=20, a=-20/b=-10, narrow [0.499, 0.501] bracket). Tolerance:
1e-12relative error for moderate cases,1e-9in the deep tails. - Benchmark gate. Run
autofit_workspace_developer/graphical/fit.py --total_datasets={3,10,30}
andep/fit.py --total_datasets={3,10,30}from a clean checkout
and compareprofiles/baseline.jsonagainst the pre-fix baseline
committed by issue model.results mislabelled parameters #16. Expected wall-time reduction:- Graphical: ~30% (prior transforms drop from 38% of total to <5%)
- EP: ~17% (same fix, smaller share of total runtime)
- Sanity-check max log L and recovered posteriors must match the
pre-fix baseline to within1e-6relative tolerance — this is the
correctness gate, the speed gain is only meaningful if numerics
don't drift.
- Run the full PyAutoFit prior test suite:
pytest test_autofit/mapper/prior/
and any tests undertest_autofit/non_linear/that exercise the
prior transform. They must all pass unchanged.
Affected files
@PyAutoFit/autofit/mapper/prior/truncated_gaussian.py—value_forbody.@PyAutoFit/autofit/messages/truncated_normal.py— verify and update if it
also usesscipy.stats.norm.cdf/ppf.@PyAutoFit/autofit/mapper/prior/abstract.pyor a new file in
autofit/mapper/prior/_erf_helpers.py— the shared helper.@PyAutoFit/test_autofit/mapper/prior/test_truncated_gaussian.py— numerical
equivalence + extreme-truncation tests.- (Verification only, no changes expected)
@PyAutoFit/autofit/mapper/prior/gaussian.py
— already uses the direct-erfinv pattern on JAX; cross-check the new
helper is consistent.
Out of scope
- Replacing
scipy.special.erfwith a hand-rolled rational approximation —
scipy already calls into the systemlibmerf, which is what we want. - Other priors (
LogGaussianPrior,LogUniformPrior): they don't show
up in the cProfile data and are not on the scaling critical path. If
the helper extracts cleanly they can re-use it later — out of scope
for this issue. - JAX vs numpy parity audit beyond
TruncatedGaussianPrior: separate
scope.
Success criteria
- All existing PyAutoFit prior tests pass.
- New numerical equivalence test passes at
1e-12rel tolerance. autofit_workspace_developer/graphical/fit.py --total_datasets={3,10,30}
posterior means and max log L match the pre-fixbaseline.jsonto
1e-6rel tolerance.- Graphical wall time at N=10 drops by ≥20% (target ~30%); EP wall time
at N=10 drops by ≥10% (target ~17%). - Updated baseline.json files are committed in a follow-up
autofit_workspace_developer PR after the library change merges.
Activity
Jammy2211 commentedon May 20, 2026
Shipped — 2026-05-20
PR
Summary
Replaced
scipy.stats.norm.cdf/ppfinsideTruncatedGaussianPrior.value_forandTruncatedNormalMessage.value_forwith directscipy.special.ndtr/ndtri(and thejax.scipy.specialequivalents on the JAX branch) via a new shared helperautofit.mapper.prior._erf_helpers.truncated_normal_value_for. Bit-exact equivalent at the algorithm level — same Cephes routines, just without thescipy.stats._distn_infrastructurewrapper.Measured speedup (toy 1D Gaussian baseline)
The measured speedups are 2–4× larger than the cProfile-projected ~30% (graphical) and ~17% (EP) because cProfile itself adds wrapper overhead that masked the true
scipy.statscost. The Python-side dispatch in_distn_infrastructurewas a much larger share of real-fit runtime than its in-cProfile share suggested.Numerics gate (all PASS)
scipy.stats.norm.cdf/ppfcomposition on a (mean, sigma, lower, upper) × unit grid.autofit_workspace_developer/graphical/fit.py --total_datasets=3post-fix: sanity PASS, max log L = -125.24 vs pre-fix -125.14 (rel diff 7.8e-4, within Dynesty stochastic noise).autofit_workspace_developer/ep/fit.py --total_datasets=3post-fix: sanity PASS.test_truncated_gaussian.py).TruncatedNormalMessage.value_forbit-exact toTruncatedGaussianPrior.value_forfor matching parameters (both route through the same helper).Session notes
erf/erfinv(the pattern inGaussianPrior.value_for's JAX branch). It passed at moderate units but lost 1e-11 rel precision atunit ∈ {1e-6, 1-1e-6}for half-bounded truncations. Switched tondtr/ndtridirectly — these are the actual Cephes routinesscipy.stats.norm.cdf/ppfwrap, so the result is bit-exact identical to the pre-fix path. ~3 minutes of investigation.scipy.stats.truncnorm.ppfand got 1e-11 disagreement at extreme units.truncnormhas its own tail-safe branching that neither the old nor the new prior-side code implements — so this comparison was apples-to-oranges. The right oracle is the pre-fixscipy.stats.norm.cdf/ppfcomposition (which is bit-exact equivalent tondtr/ndtri). Test rewritten; documented thetruncnorm.ppfprecision regime in a separate, narrower test.Future work
This is the first sub-task from the
graphical-ep-scale-upscoping pass. Next prompts queued for/start_dev(fromPyAutoPrompt/graphical_ep/ep_scoping.md):paths.ep_mode = Trueflag — suppress per-factor matplotlib visualisation + internal-folder cleanup (~50% EP runtime cut; biggest remaining EP win).A follow-up
autofit_workspace_developerPR will refresh the committedprofiles/baseline.jsonat N=3/10/30 with the post-fix numbers, replacing the pre-fix baselines committed in PR #17.