Repository navigation
Description
Overview
model.add_assertion(...) is enforced only by check_assertions raising FitException, which Fitness.call catches and turns into resample_figure_of_merit on the NumPy branch alone. With a JAX analysis a violating sample lets the exception escape (non-vmapped Fitness), and the vmapped Fitness that Nautilus builds by default cannot even trace: check_assertions applies Python not to a tracer and CompoundAssertion uses Python and, so the run fails with TracerBoolConversionError for every vector. Measured 2026-09-08 on af 2026.8.17.1 (euclid_strong_lens_modeling_pipeline#54, docs/mge_label_degeneracy.md). This blocks applying a label-symmetry-breaking ordering to the Euclid two-basis MGE under use_jax=True.
Plan
- Add a traced evaluation of a model's assertions that returns a boolean array instead of raising:
assertions_satisfied_for_arguments/assertions_satisfied_from_vectoronAbstractPriorModel, withCompoundAssertioncombining viaxp.logical_andand the comparison assertions threadingxp. - On the JAX branch of
Fitness.call, build the instance with assertions ignored, then fold the traced result into the log likelihood withxp.where(satisfied, log_likelihood, resample_figure_of_merit). The NumPy branch keeps its exception-and-resample behaviour unchanged. - Pin it: numpy-only unit tests for the new evaluation and the unchanged NumPy path, an
importorskip("jax")unit test for the non-vmapped, jitted and vmappedFitness, and anautofit_workspace_test/scripts/jax_assertions/assertions_traced.pywitness script. - Update the
add_assertion,Fitness.callandconstraint.pydocstrings to the new contract.
Detailed implementation plan
Work Classification
Both (library first: PyAutoFit; workspace follow-up: autofit_workspace_test witness script).
Affected Repositories
- PyAutoFit (primary)
- autofit_workspace_test
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| ./PyAutoFit | main | clean |
| ./autofit_workspace_test | main | clean |
Conflict guard: clear (exit 0).
Suggested branch: feature/traced-assertions-on-jax-path
Worktree root: ~/Code/PyAutoLabs-wt/traced-assertions-on-jax-path/ (created by /start_library)
Implementation Steps
autofit/mapper/prior_model/abstract.py: addassertions_satisfied_for_arguments(arguments, xp=np)(True when_assertionsempty; literal True/False handled as incheck_assertions:193-226; reduce withxp.logical_and) andassertions_satisfied_from_vector(vector, xp=np)buildingargumentsexactly asinstance_from_vectordoes. Update theadd_assertiondocstring (:441).autofit/mapper/prior/arithmetic/assertion.py: threadxpintoleft_for_arguments/right_for_argumentsinGreaterThanLessThanAssertionandGreaterThanLessThanEqualAssertion;CompoundAssertion._instance_for_argumentsusesxp.logical_andinstead ofand.autofit/non_linear/fitness.py: in__init__decide once (static) whether traced assertion checks apply (model._assertionsnon-empty andconf.instance["general"]["test"]["exception_override"]false), likelog_likelihood_ceiling(:225-233). Incall's JAX branch (:380-388):instance_from_vector(..., ignore_assertions=True, xp=xp), evaluate, thenxp.where(model.assertions_satisfied_from_vector(parameters, xp=xp), log_likelihood, resample_figure_of_merit). NumPy branch untouched. Docstring paragraph updated.autofit/mapper/prior_model/constraint.py: module docstring sentence "cannot work under JAX" replaced with the new contract.- Tests:
test_autofit/mapper/prior/test_assertion.py(new-method cases:>,>=, compound assertion, compound-prior ordering(a0**2 + a1**2) > (b0**2 + b1**2), empty model);test_autofit/non_linear/test_fitness_assertions.py(NumPy path unchanged); newtest_autofit/non_linear/test_fitness_assertions_jax.pywithpytest.importorskip("jax")(precedenttest_fitness_ceiling_jax.py): non-vmapped,use_jax_jit=True,use_jax_vmap=Trueall return the sentinel for a violating vector and the NumPy value (1e-8) for a satisfying one, no exception. - Workspace:
autofit_workspace_test/scripts/jax_assertions/assertions_traced.pymirroringfitness_dispatch.py(ENV: jaxheader,assert_*functions,__main__runner), asserting the prompt's Witness against the branch PyAutoFit.
Key Files
autofit/non_linear/fitness.py—Fitness.callJAX branch (:380-394), sentinel setup (:225-233)autofit/mapper/prior_model/abstract.py—check_assertions(:193-226),instance_from_vector,instance_for_arguments(:1600-1628),add_assertion(:441)autofit/mapper/prior/arithmetic/assertion.py— comparison and compound assertion classesautofit/mapper/prior/arithmetic/compound.py—left_for_arguments/right_for_arguments(:148-215) already acceptxpautofit/mapper/prior_model/constraint.py— traced per-class sibling (docstring)test_autofit/non_linear/test_fitness_assertions.py,test_fitness_ceiling_jax.py— existing pins and the in-tree JAX test patternautofit_workspace_test/scripts/jax_assertions/fitness_dispatch.py— witness-script precedent
Original Prompt
Click to expand starting prompt
Make add_assertion enforceable on the JAX likelihood path
Type: feature
Target: autofit
Repos:
- PyAutoFit
- autofit_workspace_test
Difficulty: medium
Autonomy: supervised
Priority: high
Status: formalised
Consequence: review
Witness: anautofit_workspace_test/scripts/jax_assertions/script composes a model with an ordering assertion between compound priors ((a0**2 + a1**2) > (b0**2 + b1**2)), builds a JAX analysis and theFitnessNautilus builds (use_jax_vmap=True) plus the non-vmapped one; for a violating vector both return the resample sentinel with no exception, for a satisfying vector both return the same log likelihood as the NumPy path to 1e-8, andjax.jitof the fitness compiles; numpy-only unit tests pin the new traced assertion evaluation and the unchanged NumPy resample path.
Review-minutes: 10
Unattended: ready
Make model.add_assertion(...) work when the analysis runs on JAX, so that a label-symmetry-breaking ordering (the Euclid two-basis MGE |e_A|^2 > |e_B|^2, euclid_strong_lens_modeling_pipeline#54, docs/mge_label_degeneracy.md) can be applied to a Nautilus search with use_jax=True.
Today (af 2026.8.17.1) assertions are enforced only by check_assertions raising exc.FitException inside instance_for_arguments (autofit/mapper/prior_model/abstract.py:193-226, 1600-1628), and Fitness.call (autofit/non_linear/fitness.py:380-394) catches that exception and returns resample_figure_of_merit only on the NumPy branch. Measured on 2026-09-08 with a real model:
- non-vmapped JAX
Fitness:FitExceptionpropagates uncaught fromFitness.call, killing the run on the first violating sample; - vmapped
Fitness, which Nautilus builds by default (use_jax_vmap=True,autofit/non_linear/search/nest/nautilus/search.py:116, 242-256):jax.errors.TracerBoolConversionErrorat trace time for satisfying and violating vectors alike, becausecheck_assertionsdoesnot assertion.instance_for_arguments(...)on a tracer. The model cannot compile at all.
Required behaviour: on the JAX branch of Fitness.call, build the instance with assertions ignored, evaluate every assertion as a traced boolean with the array module (GreaterThanLessThanAssertion / GreaterThanLessThanEqualAssertion / CompoundAssertion in autofit/mapper/prior/arithmetic/assertion.py; their left_for_arguments / right_for_arguments already evaluate compound priors arithmetically), combine them, and fold the result into the log likelihood with xp.where(violated, resample_figure_of_merit, log_likelihood), the penalty-term shape autofit/mapper/prior_model/constraint.py anticipates. The NumPy branch keeps its exception-and-resample behaviour and its results byte-for-byte. exception_override and ignore_assertions semantics are unchanged. Gradient safety follows the existing contract in the Fitness.call docstring: the where is value-only, which is fine here because the likelihood is evaluated at a valid instance either way.
Constraints: no import jax in library unit tests (JAX path pinned by the workspace_test script); do not change resample_figure_of_merit defaults; do not touch __model_constraint__ / ball constraints (separate mechanism); take_attributes / assert_no_assertions unchanged.
Out of scope (separate follow-ups already in ideas.md): the mge_model_from ordering option in PyAutoGalaxy and the Euclid pipeline edit + witness rerun.
Activity
Jammy2211 commentedon Sep 8, 2026
Library PR Created: #1583
Next: /start_workspace — autofit_workspace_test gains scripts/jax_assertions/assertions_traced.py, the witness for this change (no other workspace impact: only additive API, no existing symbol changed).
API-change summary: added AbstractPriorModel.gathered_assertions(), assertions_satisfied_for_arguments(), assertions_satisfied_from_vector(); check_assertions gained optional xp; Fitness.call JAX branch maps violating samples to resample_figure_of_merit via xp.where instead of raising or failing to trace.
Jammy2211 commentedon Sep 8, 2026
Workspace PR: PyAutoLabs/autofit_workspace_test#100 (witness script; merge after the library PR)
fix: traced assertions match the NumPy contract exactly (review follo…
Merge pull request #1583 from PyAutoLabs/feature/traced-assertions-on…
Jammy2211 commentedon Sep 8, 2026
Shipped — 2026-09-08
PRs
Summary
model.add_assertion(...)is now enforced on the JAX likelihood path:AbstractPriorModel.gathered_assertions()collects assertions across the model tree,assertions_satisfied_from_vectorevaluates them as a traced boolean, andFitness.call's JAX branch appliesxp.whereto the final figure of merit (matching the NumPy early return). NumPy path unchanged. Independent Codex (gpt-6-astra) review: four defects reproduced and fixed before merge. Full suite 2533 passed. Witness:autofit_workspace_test/scripts/jax_assertions/assertions_traced.py.Merge pull request #1586 from PyAutoLabs/feature/order-lens-mge-bases…