Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions autofit/mapper/prior/arithmetic/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ def _instance_for_arguments(self, arguments, ignore_assertions=False, xp=np):
lower = self.left_for_arguments(
arguments,
ignore_assertions=ignore_assertions,
xp=xp,
)
greater = self.right_for_arguments(
arguments,
ignore_assertions=ignore_assertions,
xp=xp,
)
return lower < greater

Expand Down Expand Up @@ -75,9 +77,11 @@ def _instance_for_arguments(
return self.left_for_arguments(
arguments,
ignore_assertions=ignore_assertions,
xp=xp,
) <= self.right_for_arguments(
arguments,
ignore_assertions=ignore_assertions,
xp=xp,
)


Expand All @@ -94,12 +98,43 @@ def _instance_for_arguments(
ignore_assertions=False,
xp=np,
):
return self.assertion_1.instance_for_arguments(
arguments,
ignore_assertions,
) and self.assertion_2.instance_for_arguments(
arguments,
ignore_assertions,
"""
Both sub-assertions must hold.

The numpy path keeps the Python ``and``, which **short-circuits**: a false first half
means the second is never realised. That is load-bearing rather than incidental — a
chained assertion's second half can be undefined exactly where the first one fails
(``(0 < p) < (1 / p)`` at ``p = 0``), and the user's contract there is a ``FitException``
(resample), not a ``ZeroDivisionError`` out of the likelihood.

The JAX path cannot use ``and``: each half is a tracer, and ``and`` coerces its operands
to Python bools, raising ``TracerBoolConversionError``. ``xp.logical_and`` evaluates both
halves, which is safe there because the arithmetic that raises on numpy is ``inf`` or
``nan`` under JAX rather than an exception.

The branch is on the *module identity* of ``xp``, which is a static Python value even
inside a trace, so it is legal under ``jit`` and ``vmap``.
"""
if xp is np:
return self.assertion_1.instance_for_arguments(
arguments,
ignore_assertions,
) and self.assertion_2.instance_for_arguments(
arguments,
ignore_assertions,
)

return xp.logical_and(
self.assertion_1.instance_for_arguments(
arguments,
ignore_assertions,
xp=xp,
),
self.assertion_2.instance_for_arguments(
arguments,
ignore_assertions,
xp=xp,
),
)

def dict(self) -> dict:
Expand Down
192 changes: 188 additions & 4 deletions autofit/mapper/prior_model/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,21 @@ def assertions(self):
def assertions(self, assertions):
self._assertions = assertions

def check_assertions(self, arguments: Dict[Prior, float]):
def check_assertions(self, arguments: Dict[Prior, float], xp=np):
"""
Check that all assertions are satisfied by the given arguments.

This is the **exception** form of the assertion check, and therefore the numpy form: it
applies a Python ``not`` to each assertion's value, which is illegal on a traced value.
The traced form is `assertions_satisfied_for_arguments`.

Parameters
----------
arguments
A dictionary mapping priors to values
xp
The array module (``numpy`` or ``jax.numpy``) used to realise the values the
assertions compare.

Raises
------
Expand All @@ -211,6 +218,7 @@ def check_assertions(self, arguments: Dict[Prior, float]):
or assertion is not True
and not assertion.instance_for_arguments(
arguments,
xp=xp,
)
]
number_of_failed_assertions = len(failed_assertions)
Expand All @@ -226,6 +234,166 @@ def check_assertions(self, arguments: Dict[Prior, float]):
f"{number_of_failed_assertions} assertions failed!\n{name_string}"
)

def gathered_assertions(self) -> list:
"""
Every assertion attached anywhere in this model's tree.

Assertions are attached to whichever model object the user happened to hold -- a child
`Model` as often as the top-level `Collection` -- and on the numpy path that does not
matter, because each model checks its own assertions as its own instance is built. The
traced path has no such recursion to hook into: it is handed one model and one parameter
vector, so it must find the assertions itself or silently enforce fewer of them than
numpy does.

Returns
-------
This model's own assertions first, then those of every `AbstractPriorModel` reachable
beneath it in walk order, then those attached to the assertions' own operands. A component
reachable by more than one path (a shared component) contributes its assertions once.
"""
assertions = list(self._assertions)

# `_assertions` itself is never walked into by the tree walk below:
# `path_instances_of_class` skips attributes whose name starts with an underscore, so the
# assertion objects -- which are themselves `AbstractPriorModel`s -- are not mistaken for
# components of the model. Their operands are picked up separately, afterwards.
seen = {id(self)}

for _, model in self.attribute_tuples_with_type(
AbstractPriorModel,
ignore_children=False,
):
if id(model) in seen:
continue
seen.add(id(model))
assertions.extend(getattr(model, "_assertions", []))

# An assertion's operand can itself be a model carrying assertions -- `derived = p + 1` is
# a `CompoundPrior`, and `derived.add_assertion(...)` attaches to it. On numpy those fire
# when the operand is realised, so they are part of the model's contract; the tree walk
# cannot see them, because the operand hangs off the assertion rather than off the model.
collected = {id(assertion) for assertion in assertions}
visited = set()
queue = list(assertions)

while queue:
node = queue.pop(0)

if id(node) in visited:
continue
visited.add(id(node))

if isinstance(node, AbstractPriorModel) and id(node) not in seen:
for assertion in node._assertions:
if id(assertion) not in collected:
collected.add(id(assertion))
assertions.append(assertion)
queue.append(assertion)

for attribute in ("_left", "_right", "assertion_1", "assertion_2"):
operand = getattr(node, attribute, None)
if operand is not None:
queue.append(operand)

return assertions

def assertions_satisfied_for_arguments(
self,
arguments: Dict[Prior, float],
xp=np,
assertions=None,
):
"""
Whether every assertion attached to this model is satisfied, returned as a **value**
rather than signalled by an exception.

This is the traced sibling of `check_assertions`. It never applies a Python ``not``,
``and`` or ``if`` to an assertion's value, so under ``jax.jit`` / ``jax.vmap`` -- where
each value is a tracer -- it returns a traced boolean instead of raising
``TracerBoolConversionError``. `Fitness` applies that boolean with ``xp.where``,
mapping a violating model to the resample figure of merit.

Parameters
----------
arguments
A dictionary mapping priors to physical values.
xp
The array module (``numpy`` or ``jax.numpy``) used to realise and combine the
assertion values.
assertions
The assertions to evaluate. Defaults to `gathered_assertions`, i.e. every assertion
in the model tree; a caller that gathers them once (as `Fitness` does, in its
constructor) passes them in so the walk is not repeated per likelihood evaluation.

Returns
-------
A boolean array which is ``True`` when every assertion holds. A model with no assertions
returns ``True``.
"""
if assertions is None:
assertions = self.gathered_assertions()

satisfied = xp.asarray(True)

for assertion in assertions:
if assertion is True:
continue
if assertion is False:
value = xp.asarray(False)
else:
value = assertion.instance_for_arguments(
arguments,
ignore_assertions=True,
xp=xp,
)
satisfied = xp.logical_and(satisfied, value)

return satisfied

def assertions_satisfied_from_vector(self, vector, xp=np, assertions=None):
"""
Whether every assertion attached to this model is satisfied by a physical parameter
vector, returned as a **value** rather than signalled by an exception.

The vector is mapped to arguments exactly as `instance_from_vector` maps it, so the
assertions are evaluated against the same physical values the instance would be built
from.

Parameters
----------
vector: [float]
A vector of physical parameter values, ordered by prior id.
xp
The array module (``numpy`` or ``jax.numpy``) used to realise and combine the
assertion values.
assertions
The assertions to evaluate. Defaults to `gathered_assertions` (see
`assertions_satisfied_for_arguments`).

Returns
-------
A boolean array which is ``True`` when every assertion holds. A model with no assertions
returns ``True``.
"""
if len(vector) != self.prior_count:
raise AssertionError(
f"Vector length {len(vector)} != prior count {self.prior_count}"
)

arguments = dict(
map(
lambda prior_tuple, physical_unit: (prior_tuple.prior, physical_unit),
self.prior_tuples_ordered_by_id,
vector,
)
)

return self.assertions_satisfied_for_arguments(
arguments,
xp=xp,
assertions=assertions,
)

def set_item_at_path(self, path: Tuple[str, ...], value):
"""
Set an item at a path in the model.
Expand Down Expand Up @@ -441,8 +609,24 @@ def from_json(cls, file: str):
def add_assertion(self, assertion, name=""):
"""
Assert that some relationship holds between physical values associated with
priors at the point an instance is created. If this fails a FitException is
raised causing the model to be re-sampled.
priors at the point an instance is created.

The assertion is enforced differently on the two array backends, but with the same
outcome -- the non-linear search never selects a violating model:

- **numpy**: `check_assertions` raises a ``FitException`` when the assertion fails, which
:class:`~autofit.non_linear.fitness.Fitness` catches and turns into the resample figure
of merit.
- **JAX**: raising is impossible inside a trace, so the assertion is instead evaluated as
a traced boolean by `assertions_satisfied_from_vector` and applied by
:class:`~autofit.non_linear.fitness.Fitness` with ``xp.where``, mapping a violating
model to the resample figure of merit. This is a **value-only** penalty, carrying the
same gradient caveat as the NaN guards documented on `Fitness.call`.

Both paths enforce assertions attached anywhere in the model tree: numpy because each
model checks its own as its instance is built, JAX because `gathered_assertions` collects
them from the whole tree.

Parameters
----------
assertion
Expand Down Expand Up @@ -1620,7 +1804,7 @@ def instance_for_arguments(
if not (
conf.instance["general"]["test"]["exception_override"] or ignore_assertions
):
self.check_assertions(arguments)
self.check_assertions(arguments, xp=xp)

return self._instance_for_arguments(
arguments,
Expand Down
26 changes: 16 additions & 10 deletions autofit/mapper/prior_model/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@
Class-declared model constraints, evaluated as traced values.

This is the differentiable sibling of :meth:`AbstractPriorModel.add_assertion`.
Assertions are attached to a *model instance* by the user and signal failure by
raising :class:`FitException`; that works on the NumPy path, where
:class:`Fitness` catches the exception and returns the resample sentinel, but it
cannot work under JAX. A ``raise`` needs a concrete boolean, and inside a trace
the condition is a tracer — attempting it gives ``TracerBoolConversionError``.
That is why guards such as ``autogalaxy.profiles.validate.validate_ell_comps``
return early for non-concrete scalars rather than raising: the escape hatch is
load-bearing, or every jitted likelihood would crash instead of sampling.
Assertions are attached to a *model instance* by the user. On the NumPy path they
signal failure by raising :class:`FitException`, which :class:`Fitness` catches,
returning the resample sentinel. A ``raise`` cannot happen inside a JAX trace — it
needs a concrete boolean, and the condition there is a tracer, which gives
``TracerBoolConversionError`` — so under JAX the assertion is instead evaluated as
a **traced boolean** and applied by :class:`Fitness` as an ``xp.where`` penalty
that maps a violating model to the resample sentinel. Assertions therefore survive
``jit`` and ``vmap``, but as a *value-only* rejection: reverse-mode
differentiates both branches of that ``where``, so the penalty carries no usable
gradient back into the valid region. This is also why guards such as
``autogalaxy.profiles.validate.validate_ell_comps`` return early for non-concrete
scalars rather than raising: the escape hatch is load-bearing, or every jitted
likelihood would crash instead of sampling.

A model constraint differs in exactly two ways:

- it is declared **on the class**, so every model built from that class carries
it without the user remembering to attach anything;
- it is evaluated as a **traced non-negative violation measure** rather than
raising, so it survives ``jit``, ``vmap`` and ``grad``.
- it is evaluated as a **traced non-negative violation measure** rather than a
boolean, so it survives ``jit``, ``vmap`` and ``grad`` — a magnitude carries a
gradient back into the valid region where the assertion ``where`` cannot.

Declaring one
-------------
Expand Down
Loading
Loading