fix(spherical): GFD solvers rebuild `SphericalDerivative2D` (and scipy tables) on every call and cannot be jitted
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 55/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- jupyter-notebook, python
- Domain
- backend-api-design, performance
Research direction
The issue points to specific lines in spectraldiffx/_src/spherical/solvers.py and operators.py. Start by reading the constructors of SphericalVorticityInversionSolver, SphericalDivergenceInversionSolver, and SphericalHelmholtzDecomposition. The fix involves moving operator construction from solve/decompose methods to __init__ and modifying SphericalDerivative1D.__init__ to compute nodes directly. Run the provided reproduction script to see the error, then write a test to verify JIT compatibility and performance.
Written by the indexing model from the issue text.
Description
Part of #81.
Summary
SphericalVorticityInversionSolver.solve, SphericalDivergenceInversionSolver.solve and SphericalHelmholtzDecomposition.decompose construct a SphericalPoissonSolver and a SphericalDerivative2D inside the method body. SphericalDerivative2D.__init__ builds a fresh SphericalGrid1D (scipy lpmv + Gauss–Legendre nodes), and SphericalDerivative1D.__init__ does np.array(grid._nodes) (operators.py:130). Two consequences:
- Impossible to
jax.jit. Under any trace,grid._nodesis a tracer andnp.array(...)raisesTracerArrayConversionError, even when the solver is closed over and even undereqx.filter_jit. - Eager cost dominated by table construction. 39–250 ms per call at
Nx = 32, Ny = 16for ≤ 0.3 ms of arithmetic.
vmap and grad happen to work because they do not stage the construction.
Reproduction
import jax; jax.config.update("jax_enable_x64", True)
import jax.numpy as jnp, numpy as np
from spectraldiffx import SphericalGrid2D, SphericalVorticityInversionSolver
g = SphericalGrid2D.from_N_L(Nx=32, Ny=16, Lx=2*np.pi, Ly=np.pi)
s = SphericalVorticityInversionSolver(g)
zeta = jnp.sin(g.y)[:, None] * jnp.cos(g.x)[None, :]
jax.jit(lambda z: s.solve(z))(zeta) # TracerArrayConversionError at operators.py:130
Same for SphericalHelmholtzDecomposition.decompose, eqx.filter_jit(lambda s, z: s.solve(z))(s, zeta), and jax.jit(lambda x: SphericalDerivative2D(g).gradient(x)).
Root cause
spectraldiffx/_src/spherical/solvers.py:274-277, 320-322, 410-415: operators built per call.spectraldiffx/_src/spherical/operators.py:127-134, 296-305:SphericalGrid1D/ numpy conversion inside__init__.
Proposed fix
- Build the derivative and Poisson operators once in each solver's
__init__and store them as fields (the same wayderiv_thetais already a field onSphericalDerivative2D). - In
SphericalDerivative1D.__init__, compute nodes from_gauss_legendre_nodes_weights(grid.N)in numpy rather than convertinggrid._nodesback from JAX. - Longer term (with the stable-recurrence issue in this epic): build the tables in JAX with
lax.scan, so construction itself is traceable andeqx.filter_jitover a freshly constructed operator works.
Once the spectral θ-derivative issue lands, SphericalDerivative1D is no longer needed on the 2D path at all, which removes the scipy dependency from SphericalDerivative2D entirely.
Acceptance criteria
-
jax.jit(lambda z: solver.solve(z))works for all three GFD solvers; a test asserts this and compares to the eager result to 1e-12. - A test counts
SphericalDerivative2Dconstructions (monkeypatch) across threesolvecalls: exactly zero after__init__. - Eager
solveatNx = 32, Ny = 16under 2 ms.
- 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