Skip to content

feat: enforce add_assertion on the JAX likelihood path as a traced penalty (#1581) - #1583

Merged
Jammy2211 merged 2 commits into
mainfrom
feature/traced-assertions-on-jax-path
Sep 8, 2026
Merged

Jammy2211 merged 2 commits into
mainfrom
feature/traced-assertions-on-jax-path

Conversation

@Jammy2211

@Jammy2211 Jammy2211 commented Sep 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

model.add_assertion(...) was enforced only by check_assertions raising FitException, which Fitness.call catches on the NumPy branch alone. With a JAX analysis a violating sample let the exception escape (non-vmapped Fitness), and the vmapped Fitness Nautilus builds by default (use_jax_vmap=True) failed at trace time with TracerBoolConversionError for every vector, because check_assertions applied Python not to a tracer and CompoundAssertion used Python and. Measured 2026-09-08 on af 2026.8.17.1 with the Euclid vis_lp model (euclid_strong_lens_modeling_pipeline#54, docs/mge_label_degeneracy.md), where an ordering assertion between two MGE bases is needed to remove an exact label symmetry.

Assertions are now enforced on both backends with the same outcome. On the JAX branch Fitness.call builds the instance with assertions ignored, evaluates every assertion in the model tree as a traced boolean, and applies xp.where(satisfied, log_likelihood, resample_figure_of_merit). The gather and the exception_override read happen once in Fitness.__init__, so the branch is a static Python bool under jit / vmap. The NumPy branch is byte-for-byte unchanged. Cross-checked on the real Euclid model: the JAX and vmapped rows that raised now return the sentinel for a violating vector and the NumPy value for a satisfying one.

API Changes

Added, no removals or signature changes to existing public functions:

  • AbstractPriorModel.gathered_assertions() returns the assertions attached anywhere in the model tree.
  • AbstractPriorModel.assertions_satisfied_for_arguments(arguments, xp=np, assertions=None) and assertions_satisfied_from_vector(vector, xp=np, assertions=None) return a boolean array (traced under JAX) instead of raising.
  • check_assertions(arguments, xp=np) gained an optional xp.
    Changed behaviour: on a JAX analysis a violating sample now yields resample_figure_of_merit instead of an escaped FitException (non-vmapped) or a trace failure (jit/vmap). On NumPy CompoundAssertion keeps its original short-circuit and; logical_and is used only off the NumPy path.
    See full details below.

Test Plan

  • pytest test_autofit -q green in the task worktree (2533 passed, 2 skipped after the review follow-up) (numpy layer; the only JAX-importing test file starts with pytest.importorskip("jax"))
  • test_autofit/non_linear/test_fitness_assertions_jax.py: non-vmapped, jit and vmap Fitness return the sentinel for violating vectors and the NumPy value (1e-8) for satisfying ones, for top-level and child-attached assertions, no exception
  • Existing NumPy pin test_fitness_returns_resample_fom_on_assertion_failure unchanged and green
  • Workspace witness autofit_workspace_test/scripts/jax_assertions/assertions_traced.py (follow-up workspace PR)
Full API Changes (for automation & release notes)

Added

  • autofit.mapper.prior_model.abstract.AbstractPriorModel.gathered_assertions() — own _assertions plus those of every model in the tree
  • autofit.mapper.prior_model.abstract.AbstractPriorModel.assertions_satisfied_for_arguments(arguments, xp=np, assertions=None) — boolean array, True when every assertion holds
  • autofit.mapper.prior_model.abstract.AbstractPriorModel.assertions_satisfied_from_vector(vector, xp=np, assertions=None) — same, from a physical vector
  • autofit.non_linear.fitness.Fitness._apply_assertions_traced, Fitness._traced_assertions — static per-instance decision and gathered list (private), recomputed from the model on unpickle
  • AbstractPriorModel.gathered_assertions() also walks each assertion's operand graph, so assertions attached to a compound-prior operand are enforced on both backends

Changed Signature

  • AbstractPriorModel.check_assertions(arguments, xp=np) — optional xp, default preserves behaviour

Changed Behaviour

  • Fitness.call JAX branch: instance built with ignore_assertions=True; violating vectors map to resample_figure_of_merit via xp.where applied to the final figure of merit (after the log-prior and chi-squared conversions, matching the NumPy early return) instead of raising / failing to trace
  • CompoundAssertion._instance_for_arguments combines with xp.logical_and off the NumPy path; NumPy keeps the original short-circuit and
  • GreaterThanLessThanAssertion / GreaterThanLessThanEqualAssertion thread xp into their operands

Migration

  • None required. Assertions attached anywhere in the model tree are enforced on both backends.

Generated by the PyAutoLabs agent workflow.

🤖 Generated with Claude Code

https://claude.ai/code/session_015RALRY9yekWDTtbVfok64a

…nalty (#1581)

Assertions were enforced only by check_assertions raising FitException,
which Fitness.call caught on the NumPy branch alone. With a JAX analysis a
violating sample let the exception escape (non-vmapped Fitness) and the
vmapped Fitness Nautilus builds by default could not trace at all:
check_assertions applied Python `not` to a tracer and CompoundAssertion
used Python `and`, so every vector raised TracerBoolConversionError.

Now AbstractPriorModel.gathered_assertions() collects the assertions of the
whole model tree, assertions_satisfied_for_arguments / _from_vector evaluate
them as a boolean array (xp.logical_and, never a Python bool), the
comparison and compound assertion classes thread xp, and Fitness.call's JAX
branch builds the instance with ignore_assertions=True then applies
xp.where(satisfied, log_likelihood, resample_figure_of_merit). The gather
and the exception_override read happen once in Fitness.__init__ so the
branch is a static Python bool under jit/vmap. The NumPy branch is
unchanged (exception, then resample).

Value-only rejection, same gradient caveat as the NaN guards; the
constraint.py docstring now distinguishes it from __model_constraint__'s
gradient-carrying measure.

Motivation: the Euclid two-basis MGE ordering assertion
(euclid_strong_lens_modeling_pipeline#54).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015RALRY9yekWDTtbVfok64a
@Jammy2211

Copy link
Copy Markdown
Collaborator Author

Workspace PR: PyAutoLabs/autofit_workspace_test#100

…w-up, #1581)

Apply the assertion sentinel to the final figure of merit so chi-squared and
posterior conversions cannot turn a rejection into an optimum (NumPy returns
the raw sentinel early; JAX now does too). Recompute the gathered assertions
on unpickle instead of defaulting to none. Keep the NumPy compound-assertion
short-circuit exactly as before (xp is np) and use logical_and only under
JAX. Gather assertions attached to an assertion's own operands. Tests for
each, plus jit/vmap compound chaining, >= boundaries and exception_override.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015RALRY9yekWDTtbVfok64a
@Jammy2211

Copy link
Copy Markdown
Collaborator Author

Independent review: Codex CLI (gpt-6-astra, read-only sandbox) reviewed the branch against main. Four defects reported, all four reproduced with a failing test before any change, all fixed in aefea1d:

  • P1 Fitness.__setstate__ defaulted a pre-change pickle to "no assertions" on resume; now recomputed from the restored model.
  • P1 the JAX sentinel was applied before the log-prior / chi-squared conversions (convert_to_chi_squared=True returned +2e99 for a violating vector); now applied to the final figure of merit, matching the NumPy early return exactly.
  • P2 NumPy compound assertions had lost their short-circuit ((0 < p) < (1 / p) at p=0 raised ZeroDivisionError instead of FitException); the original and is kept on NumPy, logical_and only off the NumPy path.
  • P2 assertions attached to an assertion's own operands were not gathered; the gather now walks operand graphs too.

Coverage gaps it named are closed: compound chaining under jit and vmap, >= boundaries, exception_override, legacy-state restore, conversions. Full suite: 2533 passed, 2 skipped. A side finding (the -1e99 sentinel overflows to -inf under float32 JAX, pre-existing and hidden by the harness exporting JAX_ENABLE_X64=True) is filed in PyAutoMind ideas.md rather than fixed here.

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