fix: Python `if` on possibly-traced values breaks jit with traced `alpha`/`nu`/`dx`/`bc_values` in ~10 places; use `eqx.error_if` or guard on Python scalars
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 65/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- jupyter-notebook, python
- Domain
- backend, performance, tooling
Research direction
The issue lists specific files and lines where Python if statements cause JAX tracer errors. Start by examining fourier/operators.py, fourier/eigenvalues.py, fourier/grid.py, fourier/solvers.py, spherical/solvers.py, spherical/filters.py, and chebyshev/solvers.py and chebyshev/filters.py. Replace problematic if checks with eqx.error_if or guard on Python scalars. Run the provided example to reproduce the error, then write parametrised tests to verify fixes under jax.jit and jax.vmap.
Written by the indexing model from the issue text.
Description
Part of #82.
Summary
Several validation checks compare a value that may be a JAX tracer with a Python if, raising TracerBoolConversionError under jax.jit/vmap when the parameter is traced. This breaks parameter sweeps (vmap over alpha), gradient-based tuning of nu, jitted solvers with traced dx, and any pytree of parameters that puts these scalars in arrays. Fourier solvers already handle lambda_ correctly with jnp.where (solvers.py:1211-1216), so this is about consistency.
Sites (all reproduced)
| File:line | Check | Fails on |
|---|---|---|
fourier/operators.py:13-20 (_validate_hyperviscosity, called at :143, 280, 490) |
if nu < 0, if order < 1 |
jax.jit with traced nu/order; nu=jnp.asarray(1e-3) inside jit |
fourier/eigenvalues.py:346-349 (_validate_L) |
if L <= 0 |
every *_eigenvalues_ps under jit/vmap with traced L; any solver with approximation="spectral" and traced dx since solvers pass L = N*dx (solvers.py:861, 930, 993, 1059, 1129) |
fourier/eigenvalues.py:196, 427 |
if N < 2 |
traced N (rare) |
fourier/grid.py:102, 158, 299, 303, 377, 379, 526-534, 642-646 (check_consistency, from_L_dx) |
if jnp.isclose(...) on Python floats |
any trace, even with the grid static (also a device round trip for scalars in eager mode) |
fourier/solvers.py:240 (modify_rhs_1d) |
if left_val == 0.0 and right_val == 0.0 (!= at :195) |
traced bc_values (2D/3D use is not None and are fine) |
spherical/solvers.py:188 |
if alpha < 0 |
traced alpha |
spherical/filters.py:498-501, 543-546, 601-603, 641-643 |
if alpha < 0, if power <= 0, if nu_hyper < 0 |
traced filter parameters |
chebyshev/solvers.py:137, 310, chebyshev/filters.py:93, 204 (after #79) |
if alpha < 0, if bc_type == "neumann" and alpha == 0.0 (:162) |
traced alpha |
Example:
import jax, jax.numpy as jnp
from spectraldiffx import FourierGrid2D, SpectralDerivative2D
g = FourierGrid2D.from_N_L(Nx=16, Ny=16, Lx=1.0, Ly=1.0)
d = SpectralDerivative2D(g); u = jnp.zeros((16, 16))
jax.jit(lambda nu: d.hyperviscosity(u, nu, 2))(1e-3) # TracerBoolConversionError
Proposed fix
- Numeric range checks:
value = eqx.error_if(value, value < 0, "nu must be >= 0"), which works eagerly and under jit (and can be disabled withEQX_ON_ERROR). Where the check only makes sense for Python scalars (e.g.order,poweras exponents that must be static), validate withisinstance(x, (int, float))and skip otherwise. check_consistency/from_L_dx: usemath.isclose(self.L, self.N * self.dx, rel_tol=rtol)andmath.isclose(N_float, round(N_float), abs_tol=1e-8)on Python floats; this also removes theisclose(N_float % 1, 1)hack.modify_rhs_1d: switch toArray | Nonelike 2D/3D and test withis not None._validate_L: drop it (the solvers always pass a positiveL), or guard withisinstance.zero_meanas a traced bool (solvers.py, spherical) also fails; document it as static or usejnp.where.
Acceptance criteria
- A parametrised test jits each affected function with the relevant parameter traced and checks the result against eager to 1e-12.
-
jax.vmapoveralphafor the Helmholtz solvers and overnuforhyperviscosity. -
jax.jit(solve_helmholtz_dst, static_argnames="approximation")(f, dx, dy, lam, approximation="spectral")with traceddxworks. - The invalid-value tests still raise (now
EquinoxRuntimeError/ValueError).
- Dominant language
- Python
- Stars
- 2
- Forks
- 0
- Avg merge
- 8h 13m
- Merged PRs (30d)
- 8
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from jejjohnson/spectraldiffx
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
jejjohnson/spectraldiffx#172 ·
-
epic
Difficulty 1/5 Under an hour Newbie friendliness 62/100
jejjohnson/spectraldiffx#167 · 1 comment ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
jejjohnson/spectraldiffx#113 ·
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
jejjohnson/spectraldiffx#105 ·
-
dependencies
Difficulty 4/5 3-5 days Newbie friendliness 45/100
jejjohnson/spectraldiffx#181 ·
All issues in jejjohnson/spectraldiffx
Similar issues
-
bug(operator): VolterraOperator built inside jax.jit with a concrete decay raises TracerBoolConversionErrorPossibly taken @jejjohnson claimed this today. Opentype:bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
jejjohnson/integrix#144 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
finos/ai-governance-framework#459 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
autowarefoundation/autoware_carla_scenario#2963 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
freelawproject/litigant-portal#1307 ·
Maintainers usually reply within 2 days
-
type/bug
Difficulty 1/5 Under an hour Newbie friendliness 86/100
h2oai/h2o-llmstudio#1163 ·
Maintainers usually reply within 1 day