Skip to content

feat: traced assertions on the JAX likelihood path (add_assertion under jit/vmap) #1581

Closed

Description

@Jammy2211

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_vector on AbstractPriorModel, with CompoundAssertion combining via xp.logical_and and the comparison assertions threading xp.
  • On the JAX branch of Fitness.call, build the instance with assertions ignored, then fold the traced result into the log likelihood with xp.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 vmapped Fitness, and an autofit_workspace_test/scripts/jax_assertions/assertions_traced.py witness script.
  • Update the add_assertion, Fitness.call and constraint.py docstrings 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

  1. autofit/mapper/prior_model/abstract.py: add assertions_satisfied_for_arguments(arguments, xp=np) (True when _assertions empty; literal True/False handled as in check_assertions :193-226; reduce with xp.logical_and) and assertions_satisfied_from_vector(vector, xp=np) building arguments exactly as instance_from_vector does. Update the add_assertion docstring (:441).
  2. autofit/mapper/prior/arithmetic/assertion.py: thread xp into left_for_arguments / right_for_arguments in GreaterThanLessThanAssertion and GreaterThanLessThanEqualAssertion; CompoundAssertion._instance_for_arguments uses xp.logical_and instead of and.
  3. autofit/non_linear/fitness.py: in __init__ decide once (static) whether traced assertion checks apply (model._assertions non-empty and conf.instance["general"]["test"]["exception_override"] false), like log_likelihood_ceiling (:225-233). In call's JAX branch (:380-388): instance_from_vector(..., ignore_assertions=True, xp=xp), evaluate, then xp.where(model.assertions_satisfied_from_vector(parameters, xp=xp), log_likelihood, resample_figure_of_merit). NumPy branch untouched. Docstring paragraph updated.
  4. autofit/mapper/prior_model/constraint.py: module docstring sentence "cannot work under JAX" replaced with the new contract.
  5. 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); new test_autofit/non_linear/test_fitness_assertions_jax.py with pytest.importorskip("jax") (precedent test_fitness_ceiling_jax.py): non-vmapped, use_jax_jit=True, use_jax_vmap=True all return the sentinel for a violating vector and the NumPy value (1e-8) for a satisfying one, no exception.
  6. Workspace: autofit_workspace_test/scripts/jax_assertions/assertions_traced.py mirroring fitness_dispatch.py (ENV: jax header, assert_* functions, __main__ runner), asserting the prompt's Witness against the branch PyAutoFit.

Key Files

  • autofit/non_linear/fitness.py — Fitness.call JAX 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 classes
  • autofit/mapper/prior/arithmetic/compound.py — left_for_arguments / right_for_arguments (:148-215) already accept xp
  • autofit/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 pattern
  • autofit_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: an autofit_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 the Fitness Nautilus 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, and jax.jit of 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: FitException propagates uncaught from Fitness.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.TracerBoolConversionError at trace time for satisfying and violating vectors alike, because check_assertions does not 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

  1. Jammy2211 commented on Sep 8, 2026

    @Jammy2211
    CollaboratorAuthor

    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.

  2. Jammy2211 commented on Sep 8, 2026

    @Jammy2211
    CollaboratorAuthor

    Workspace PR: PyAutoLabs/autofit_workspace_test#100 (witness script; merge after the library PR)

  3. added 2 commits that reference this issue on Sep 8, 2026
  4. Jammy2211 commented on Sep 8, 2026

    @Jammy2211
    CollaboratorAuthor

    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_vector evaluates them as a traced boolean, and Fitness.call's JAX branch applies xp.where to 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.

  5. added a commit that references this issue on Sep 8, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions