Epic: `spectraldiffx.pipekit` — a pipekit operator layer with a spectral-space carrier

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

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
backend, devtools

Research direction

Start by reading the child PR breakdown and the cited prerequisites #108, #103, and #150, then inspect the existing Tier A spectral APIs and pipekit’s JaxOperator contract. The epic spans a carrier, multiple operator families, transform-budget tests, and a rollout gradient check, so implementation should follow the listed child PR order. Done means the RHS and RK4 transform counts meet the stated budgets, every operator passes check_operator, and the ν gradient matches finite differences.

Written by the indexing model from the issue text.

Description

enhancement epic

Why

spectraldiffx is the spectral member of the GeoStack's geonumerics. The geotoolz two-tier pattern (pure Tier-A primitives → thin pipekit Tier-B operators) worked for finitevolX with zero runtime cost. For spectral operators that only holds if the pipeline knows which space each edge lives in.

A prototype measured this on a barotropic vorticity model, RK4, 128², doubly periodic:

∂ₜq = −J(ψ, q) − ν(−∇²)²q,      ∇²ψ = q
implementation transforms per RK4 step (compiled HLO) time per step (CPU)
A — hand-written spectral RHS on q̂ 20 1.5–1.7 ms
P — pipekit Graph over today's public API (physical carrier) 40 2.7 ms
S — pipekit Graph with a Spectral(q̂) carrier 20 1.6–1.7 ms

Per RHS evaluation, P pays 10 transforms (3 forward + 7 inverse) against the minimum of 5 (4 inverse + 1 forward). XLA does CSE the three identical forward transforms of q, which is why the count is 10 rather than 12. It does not cancel inverse→forward pairs. The extra 5 come from:

  • the ψ̂ → ψ → ψ̂ round trip between inverse_laplacian and jacobian: 1 ifft + 1 fft;
  • jacobian's final apply_dealias: 1 fft + 1 ifft;
  • hyperviscosity returning physical output so the terms can be summed: 1 ifft.

The tendencies agree: P vs A max rel. error 1.5e-6, S vs A bit-identical. After 400 steps the P and S trajectories differ by 5e-6 (|q|max = 3.8). Fusion counts are 43 (P) vs 27 (S) vs 22 (A).

Two more prototype findings:

  • Coefficients cannot be learned through Tier A today. jax.grad w.r.t. ν through SpectralDerivative2D.hyperviscosity raises TracerBoolConversionError (Python nu < 0 check; this is #103). Through the S carrier, ∂L/∂ν = −1.0073e8 vs FD −1.0063e8. ∇ w.r.t. q₀ matches FD (6.2574 vs 6.2574).
  • Leading axes already broadcast (fft2 acts on the last two axes): an [8, N, N] ensemble runs through the P graph unchanged. Unlike finitevolX, no over_leading helper is needed.

The S variant had to re-implement the Fourier multipliers because Tier A cannot return spectral output. #108 (spectral-in/spectral-out + rfft + exposed multipliers) is therefore the real prerequisite: with it, Tier B contains no physics.

Layering

spectraldiffx Tier A     FourierGrid, SpectralDerivative, solvers, filters
                         (#108: spectral_in / spectral_out, rfft, multipliers)
        │
        ▼
spectraldiffx.pipekit        Tier B: pipekit Operators. Edges carry either a
(this epic)              real array (physical) or Spectral(hat) (Fourier);
                         ops skip transforms the carrier makes unnecessary
        │
        ▼
pipekit Graph / Sequential / ScanCycle      diagnostics, spectra, DA plumbing
somax Term trees                            model right-hand sides (see note)

Note on models (decided): somax owns models; the numerics libraries own operators. One small from-scratch example model may stay in spectraldiffx as documentation (the barotropic vorticity example of #155 (E4.5)); the model-zoo issues (#8–#14) move to somax. Either way, the measurement above applies to any RHS composition, including somax Sum-of-Term trees: a spectral model's state should be q̂, with terms consuming and emitting spectral fields, or every summand pays an fft/ifft round trip.

Design decisions

# Decision
1 Namespace spectraldiffx.pipekit (as finitevolx.pipekit; clear of #111's namespace moves), optional extra spectraldiffx[pipekit]; import spectraldiffx never imports pipekit.
2 Ops subclass pipekit_jax.JaxOperator (Operator + eqx.Module; pipekit E1.2). Coefficients (ν, filter strength) are leaves. Sizes, strings and the grid spec are static.
3 Representation-aware carrier. Spectral(hat) is a pytree whose layout flag (real: bool, rfft vs full fft) is a static field. Ops accept `Array
4 No physics in Tier B. Every op body is a Tier-A call with spectral= / spectral_out=. #150 (E4.0b) adds spectral_out to SpectralDerivative2D (the 2-D slice of #108) so #152 (E4.2) does not wait for the full N-D refactor.
5 Grid as config: a JSON-primitive GridSpec = (Nx, Ny, Lx, Ly, dealias), rebuilt at trace time. Independent of #102 (static fields) and #104 (cached wavenumbers), and simpler once they land.
6 Every op has a transform budget asserted in tests by counting fft_type= in compiled HLO, so a regression back to physical round trips fails CI.

Child PRs (in order)

PR title depends on
#149 (E4.0a) No Python control flow on traced Fourier parameters (Fourier part of #103) —
#150 (E4.0b) spectral_out on SpectralDerivative2D (2-D slice of #108) —
#151 (E4.1) Scaffolding: spectraldiffx.pipekit, Spectral carrier, ToSpectral / ToPhysical, GridSpec, contract test + count_transforms pipekit E1.2
#152 (E4.2) Fourier derivative ops on the spectral carrier #151 (E4.1), #149 (E4.0a), #150 (E4.0b)
#153 (E4.3) Elliptic solver ops (Poisson / Helmholtz, FFT / DST / DCT) over today's solve_helmholtz_2d; #107 later only simplifies the body #151 (E4.1)
#154 (E4.4) Spectral diagnostics: energy / enstrophy (Parseval), isotropic spectra, dealias / filter ops #151 (E4.1) (spectra after #110)
#155 (E4.5) Docs: "Spectral pipelines with pipekit" + barotropic-vorticity reference example #152 (E4.2), pipekit E1.3
#156 (E4.6) (deferred) Spherical and Chebyshev Tier-B ops #81, #83, #152 (E4.2)

Related

  • Hard prerequisites: #108 (spectral_out, rfft, multipliers) for #152 (E4.2); #103 (Python if on traced values) for learnable coefficients; #107 (one HelmholtzSolver) for #153 (E4.3); #110 (spectra, missing operators) for #154 (E4.4)'s spectra.
  • Nice to have first: #102 (static fields), #104 (cached wavenumbers).
  • Model zoo #8–#14: see the layering note above.
  • Cross-repo: pipekit E1 (JaxOperator, pytree containers, ScanCycle). The prototype showed ScanCycle history must stack arbitrary pytree carriers (a Spectral broke a state[k]-indexing implementation); this is recorded in E1.3. finitevolX E2 is the sibling epic; shared conventions are GridSpec, check_operator and the [pipekit] extra.

Done when

  • The barotropic vorticity RHS built from spectraldiffx.pipekit compiles to exactly 5 transforms per evaluation (4 inverse + 1 forward), and the RK4 step to 20.
  • Every op passes check_operator: config round-trip, eager ≡ jit, leading-axis broadcast, grad finite, and transform budget.
  • ∂L/∂ν through a 100-step ScanCycle rollout matches finite differences.

Generated by Claude Code

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.