perf: scan-based matrix-vector products for Chebyshev time-stepping
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 45/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- python
- Domain
- performance
Research direction
The issue names no files or tests, so first locate the existing Chebyshev time-stepping and derivative entry points. Compare the proposed solve_ivp_scan utility and scan-compatible Euler, RK2, and RK4 steps against the current loop. Done means the Burgers example, benchmark, jax.grad compatibility, and both matrix- and FFT-based derivatives meet the listed criteria.
Written by the indexing model from the issue text.
Description
Summary
When time-stepping a PDE with Chebyshev spatial discretization, the inner loop applies D @ u (or D2 @ u) hundreds to thousands of times. The current implementation uses a Python loop or manual unrolling, which prevents XLA from fusing the operations across time steps. Using jax.lax.scan enables the compiler to optimize the entire trajectory computation as a single fused kernel.
Current Pattern (Unoptimized)
# Typical time-stepping loop
def rhs(u, t):
du_dx = D @ u # O(N^2) matmul
d2u_dx2 = D2 @ u # O(N^2) matmul
return nu * d2u_dx2 - u * du_dx # Burgers' equation
# Python loop — each iteration is a separate XLA dispatch
u = u0
for _ in range(n_steps):
k1 = rhs(u, t)
k2 = rhs(u + 0.5*dt*k1, t + 0.5*dt)
u = u + dt * k2
Problems:
- Each
D @ uis a separate XLA kernel launch - No cross-step fusion (e.g., the IFFT of step k and FFT of step k+1 could fuse)
- Python loop overhead (~10-100 us per iteration)
- Cannot efficiently differentiate through the loop (for adjoints/gradients)
Proposed: scan-based time-stepping
def step(u, _):
"""Single RK2 step — compiled as part of the scan body."""
k1 = rhs(u, t)
k2 = rhs(u + 0.5*dt*k1, t + 0.5*dt)
u_new = u + dt * k2
return u_new, u_new # (carry, output)
# Single XLA compilation for the entire trajectory
u_final, u_trajectory = jax.lax.scan(step, u0, xs=None, length=n_steps)
Benefits:
- Single XLA compilation: the entire n_steps trajectory is one fused kernel
- Cross-step fusion: XLA can fuse memory-bound operations across steps
- Reverse-mode AD:
jax.gradthroughscanuses O(1) memory (checkpointing) - Eliminates Python dispatch overhead: no per-step host-device sync
scan for the matrix-vector product itself
For the D @ u operation specifically, when D is applied repeatedly in a recurrence, scan can express this:
def apply_D_k_times(u, k):
"""Compute D^k @ u via scan (k successive applications)."""
def body(carry, _):
return D @ carry, None
result, _ = jax.lax.scan(body, u, xs=None, length=k)
return result
This is useful for higher-order derivatives without precomputing D^k:
- Avoids O(N^3) cost of computing D^k
- Each step is O(N^2), total O(k*N^2)
- XLA fuses the k matmuls into one kernel
Combining with FFT-based derivatives (#64)
When FFT-based derivatives are available, the scan body becomes:
def step(u, _):
du_dx = cheb_diff_fft(u) # O(N log N) via DCT + recurrence
d2u_dx2 = cheb_diff2_fft(u) # O(N log N) via DCT + recurrence
return u + dt * (nu * d2u_dx2 - u * du_dx), u
The scan compiles DCT → recurrence → IDCT → nonlinear term → DCT → ... as a single fused pipeline. This is where the real performance gain comes from: the intermediate arrays never leave GPU registers.
Back-of-Envelope: Overhead Reduction
| Component | Python loop | scan-based | Savings |
|---|---|---|---|
| Kernel launch per step | ~10 us | 0 (fused) | 10 us/step |
| Host-device sync | ~5 us | 0 (fused) | 5 us/step |
| Memory allocation | Per-step | Preallocated | Variable |
| Total overhead (1000 steps) | ~15 ms | ~0 ms | 15 ms |
For N=64, the actual matmul takes ~0.5 us, so the 15 us overhead per step is 30x larger than the computation. Scan eliminates this entirely.
Implementation Plan
- Add
solve_ivp_scanutility that wraps a Chebyshev RHS function injax.lax.scan - Provide example time-steppers: Euler, RK2, RK4 as scan-compatible step functions
- Demonstrate on Burgers' equation (1D Chebyshev + nonlinear advection)
- Benchmark: Python loop vs scan vs diffrax
JAX-Specific Notes
jax.lax.scansupports reverse-mode AD via implicit checkpointing- For very long trajectories, use
jax.checkpointto trade compute for memory scanwithunroll=kcan partially unroll for better instruction scheduling- The scan body must be a pure function (no side effects) — fits the JAX model
Acceptance Criteria
- scan-based time-stepping utility for Chebyshev PDEs
- Benchmark showing reduced overhead vs Python loop
- Example: Burgers' equation with Chebyshev + scan
- Compatible with
jax.gradfor adjoint-based optimization - Works with both matrix-based and FFT-based derivatives
Part of
- Epic #61
- Complements #64 (FFT-based derivatives)
- 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