Skip to content

Speed up cubic interp1d - #213

Open
nstarman wants to merge 6 commits into
f0uriest:mainfrom
nstarman:perf/interp1d-hermite-basis
Open

nstarman wants to merge 6 commits into
f0uriest:mainfrom
nstarman:perf/interp1d-hermite-basis

Conversation

@nstarman

@nstarman nstarman commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Speeds up cubic interp1d (and so Interpolator1D), most of all for many trailing axes.

Two changes, both in the cubic branch:

  • The four Hermite basis weights are computed once per query and broadcast over the trailing axes of f. Before, the coefficients of every cubic were formed with a 4×4 matmul per query and per trailing element, then contracted with [1, t, t², t³].
  • A scalar query indexes f with scalars. jnp.atleast_1d made it a length-1 gather, and a dynamic slice costs about half as much on CPU.

The weights are computed in at least float32 and at least the precision of f and fx. The sum is taken in that precision too. f1 - f0 is formed first so the value weights cancel exactly, and the dxi**derivative factor is applied once to the sum. The result has the dtype of f, fx and x, as before.

float64 results match main to round-off. In float32 and bfloat16 the errors are comparable to main's and often lower, most of all for float32 derivatives 1 and 2, since f1 - f0 cancels any constant part of f exactly. Single datasets can still come out several times worse than on main.

The table shows what I measured: random, sine and offset data with and without fx, 5 to 15 seeds each, against the same rounded inputs interpolated in float64. The ratio is main's max error over this PR's max error, so above 1 means this PR is more accurate.

knots and values derivative cases median ratio lowest ratio
float32 0 90 1.7 0.79
float32 1 90 8.2 0.60
float32 2 90 10 0.19
float32 3 90 1.0 0.31
bfloat16 0 85 2.2 1.0
bfloat16 1 85 3.8 1.3
bfloat16 2 85 1.0 1.0
bfloat16 3 85 1.0 0.69

The results match CubicHermiteSpline to 1e-12 for derivatives 0–3, for real and complex f, inside and outside the knots, and for scalar and array queries. Higher derivatives are exactly zero, even on cells small enough that 1/dx**4 overflows. On the test data, bfloat16 and float16 derivatives 0–2 stay within 3% of the largest value of the same inputs interpolated in float64. That bound is empirical. Complex fx with real f, and float32 knots with float64 values, keep their dtype and precision. These are the new tests.

Timings on CPU, single-threaded XLA, jax 0.8.0, float64, 128 knots, f of shape (128, 25), fx given, extrap=True. "Scalar" is one query per step inside a lax.scan.

0.3.16 this PR
scalar query 2.5 µs 0.6 µs
scalar query, even_spacing=True 1.1 µs 0.07 µs
d/dxq, scalar query 2.8 µs 0.7 µs
4096 queries 3.6 ms 0.3 ms
4096 queries, even_spacing=True 1.7 ms 0.14 ms

The other methods, interp2d/interp3d and PPoly are unchanged. The 2D and 3D kernels form their coefficients the same way, so they could get the same treatment in a follow-up.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.36%. Comparing base (98a3fee) to head (db98927).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #213      +/-   ##
==========================================
+ Coverage   98.35%   98.36%   +0.01%     
==========================================
  Files           8        8              
  Lines        1699     1711      +12     
==========================================
+ Hits         1671     1683      +12     
  Misses         28       28              
Files with missing lines Coverage Δ
interpax/_spline.py 98.69% <100.00%> (+0.02%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

nstarman and others added 6 commits November 24, 2026 09:00
Compute the Hermite basis weights once per query and broadcast them over the
trailing axes of f, instead of forming every cubic's coefficients with a 4x4
matmul per query and trailing element. Index f with scalars for a scalar
query, so XLA slices rather than gathers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Scaling the zero weights of derivatives above the third by dxi**4 gave
0 * inf = nan where that factor overflows. Scale each derivative branch by
its own power of dxi, and clamp a negative derivative to 0 as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summing the Hermite terms elementwise in half precision rounded every term,
where the old matmul ran at full precision. Cast the result back to the dtype
of f.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cast the result to the dtype of f and fx rather than of f alone, compute the
weights in at least the precision of f and fx, and take f1 - f0 first so the
value weights cancel exactly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Hermite basis now returns the weights as polynomials in t together
with a single scale (1, dxi, dxi**2 or 6*dxi**3), applied once after the
weighted sum. This keeps the rounding of f32 and bf16 derivatives close
to the 4x4 matmul it replaces, and the zero branch uses scale 1 so tiny
cells never form 0*inf.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nstarman
nstarman force-pushed the perf/interp1d-hermite-basis branch from dafa11e to db98927 Compare November 24, 2026 14:05

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant