Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can you explain how equality constraints are treated as rewrite rules in JAX


Can you explain how equality constraints are treated as rewrite rules in JAX


In JAX, equality constraints are treated as rewrite rules. This means that whenever a symbolic expression on the left-hand side of an equality constraint is encountered, it is replaced with the expression on the right-hand side. For example, if you have a constraint like `floordiv(a, b) == c`, all occurrences of `floordiv(a, b)` will be replaced with `c`. This process helps simplify symbolic expressions and can aid in working around limitations in JAX's reasoning mechanisms.

Equality constraints in JAX must adhere to certain rules. They cannot contain addition or subtraction at the top level on the left-hand side. Valid examples of left-hand sides include `a * b`, `4 * a`, or `floordiv(a + c, b)`. These constraints are used to normalize expressions and facilitate more efficient computation by reducing the complexity of symbolic manipulations.

Here is an example of how equality constraints are used in JAX:

python
from jax.experimental import export

# Define symbolic shape variables with constraints
a, b, c, d = export.symbolic_shape("a, b, c, d", constraints=("a * b == c + d",))

# Use the constraints to simplify expressions
expression = 2 * b * a
simplified_expression = 2 * d + 2 * c

In this example, the constraint `a * b == c + d` is used to rewrite expressions involving `a * b` to `c + d`, thus simplifying the computation. This approach is particularly useful when dealing with complex symbolic expressions where direct manipulation might be challenging.

Citations:
[1] https://docs.jax.dev/en/latest/export/shape_poly.html
[2] https://or.stackexchange.com/questions/3166/does-it-make-sense-to-use-strict-equality-constraints-in-optimization
[3] https://github.com/google/jax/blob/main/jax/experimental/jax2tf/README.md
[4] https://ask.cvxr.com/t/how-to-write-equality-constraints-with-tolerance/12922
[5] https://jesper.sikanda.be/posts/rewriting-type-theory.html
[6] https://jax.readthedocs.io/en/latest/faq.html
[7] https://jaxopt.github.io/stable/quadratic_programming.html
[8] https://stackoverflow.com/questions/42317551/generic-method-equality-constraint