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

Open
#103 0 comments 0 reactions 0 assignees View on GitHub

@jejjohnson is already working on this.

Since Oct 17, 2026.

  • #157 by @jejjohnson — open

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

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

bug

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 with EQX_ON_ERROR). Where the check only makes sense for Python scalars (e.g. order, power as exponents that must be static), validate with isinstance(x, (int, float)) and skip otherwise.
  • check_consistency / from_L_dx: use math.isclose(self.L, self.N * self.dx, rel_tol=rtol) and math.isclose(N_float, round(N_float), abs_tol=1e-8) on Python floats; this also removes the isclose(N_float % 1, 1) hack.
  • modify_rhs_1d: switch to Array | None like 2D/3D and test with is not None.
  • _validate_L: drop it (the solvers always pass a positive L), or guard with isinstance.
  • zero_mean as a traced bool (solvers.py, spherical) also fails; document it as static or use jnp.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.vmap over alpha for the Helmholtz solvers and over nu for hyperviscosity.
  • jax.jit(solve_helmholtz_dst, static_argnames="approximation")(f, dx, dy, lam, approximation="spectral") with traced dx works.
  • 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from jejjohnson/spectraldiffx

All issues in jejjohnson/spectraldiffx

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.