Repository navigation
feat: enforce add_assertion on the JAX likelihood path as a traced penalty (#1581) - #1583
Merged
Merged
Conversation
…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
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
Collaborator
Author
|
Independent review: Codex CLI (
Coverage gaps it named are closed: compound chaining under jit and vmap, |
This was referenced Sep 8, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
model.add_assertion(...)was enforced only bycheck_assertionsraisingFitException, whichFitness.callcatches on the NumPy branch alone. With a JAX analysis a violating sample let the exception escape (non-vmappedFitness), and the vmappedFitnessNautilus builds by default (use_jax_vmap=True) failed at trace time withTracerBoolConversionErrorfor every vector, becausecheck_assertionsapplied Pythonnotto a tracer andCompoundAssertionused Pythonand. 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.callbuilds the instance with assertions ignored, evaluates every assertion in the model tree as a traced boolean, and appliesxp.where(satisfied, log_likelihood, resample_figure_of_merit). The gather and theexception_overrideread happen once inFitness.__init__, so the branch is a static Python bool underjit/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)andassertions_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 optionalxp.Changed behaviour: on a JAX analysis a violating sample now yields
resample_figure_of_meritinstead of an escapedFitException(non-vmapped) or a trace failure (jit/vmap). On NumPyCompoundAssertionkeeps its original short-circuitand;logical_andis used only off the NumPy path.See full details below.
Test Plan
pytest test_autofit -qgreen in the task worktree (2533 passed, 2 skipped after the review follow-up) (numpy layer; the only JAX-importing test file starts withpytest.importorskip("jax"))test_autofit/non_linear/test_fitness_assertions_jax.py: non-vmapped, jit and vmapFitnessreturn the sentinel for violating vectors and the NumPy value (1e-8) for satisfying ones, for top-level and child-attached assertions, no exceptiontest_fitness_returns_resample_fom_on_assertion_failureunchanged and greenautofit_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_assertionsplus those of every model in the treeautofit.mapper.prior_model.abstract.AbstractPriorModel.assertions_satisfied_for_arguments(arguments, xp=np, assertions=None)— boolean array,Truewhen every assertion holdsautofit.mapper.prior_model.abstract.AbstractPriorModel.assertions_satisfied_from_vector(vector, xp=np, assertions=None)— same, from a physical vectorautofit.non_linear.fitness.Fitness._apply_assertions_traced,Fitness._traced_assertions— static per-instance decision and gathered list (private), recomputed from the model on unpickleAbstractPriorModel.gathered_assertions()also walks each assertion's operand graph, so assertions attached to a compound-prior operand are enforced on both backendsChanged Signature
AbstractPriorModel.check_assertions(arguments, xp=np)— optionalxp, default preserves behaviourChanged Behaviour
Fitness.callJAX branch: instance built withignore_assertions=True; violating vectors map toresample_figure_of_meritviaxp.whereapplied to the final figure of merit (after the log-prior and chi-squared conversions, matching the NumPy early return) instead of raising / failing to traceCompoundAssertion._instance_for_argumentscombines withxp.logical_andoff the NumPy path; NumPy keeps the original short-circuitandGreaterThanLessThanAssertion/GreaterThanLessThanEqualAssertionthreadxpinto their operandsMigration
Generated by the PyAutoLabs agent workflow.
🤖 Generated with Claude Code
https://claude.ai/code/session_015RALRY9yekWDTtbVfok64a