Skip to content

PERF: Compute B-spline fit kernel values once per axis per point - #7072

Merged
hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
gdevenyi:perf-bspline-fit-kernel-cache
Nov 3, 2026
Merged

hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
gdevenyi:perf-bspline-fit-kernel-cache

Conversation

@gdevenyi

@gdevenyi gdevenyi commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Compute the B-spline kernel values of each fit point once per axis, not once per axis for each lattice neighbour, and scatter into the lattices through buffer offsets. The output is bit-identical, and N4 runs about 5x faster.

Details

For cubic splines in 3-D, ThreadedGenerateDataForFitting called the virtual kernel Evaluate 192 times per point, but only 12 values differ. A profile of N4BiasFieldCorrection put about 89 % of its cycles in this function (ANTsX/ANTs#2037, item N4-6).

The kernel values are kept as double, B is multiplied in the same axis order from 1.0, and the neighbours are visited in the same raster order, so w2Sum and the lattice sums are unchanged. The closed-dimension wrap is applied to each per-axis lattice index before it is turned into an offset, so closed dimensions use the same path.

Measurements

AMD Ryzen 9 9950X, GCC 13.3, Release, 8 threads. N4 on MNI152 2009c T1 (1 mm, shrink 2), 3 levels x 15 iterations, four runs (no mask; mask; mask and confidence image; oblique direction):

wall user
main 43.8 s 325 s
this PR 8.0 s 40 s
Test results
  • ctest -R 'BSpline|N4|BiasCorrection': 31/31 pass. The build adds no warnings.
  • Bit-identity: a local harness writes the raw phi lattice and output of BSplineScatteredDataPointSetToImageFilter for spline orders 1-5, 2-D and 3-D, open and closed axis 0, several levels and 2-component point data, and the raw output and control-point lattice of the four N4 runs above. cmp of main against this PR: identical (81 MB and 25 MB). Orders other than 3 are not covered by the existing tests.
AI assistance

🤖 Generated with Claude Code

@dzenanz

dzenanz commented Nov 2, 2026

Copy link
Copy Markdown
Member

I do not have access to the claude session linked in PR description. Either make it accessible publicly, or remove it.

@dzenanz

dzenanz commented Nov 2, 2026

Copy link
Copy Markdown
Member

Someone more familiar with this code should review it. Bit-identical results with 5x speedup sounds great!

@gdevenyi

gdevenyi commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

@ntustison could you review this when you have a chance? It is one of the upstream items from ANTsX/ANTs#2037.

@hjmjohnson hjmjohnson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gdevenyi. The speedup is welcome, and I could not find a path where the reordered loops change the result. Before merge, the bit-identity claim needs an in-tree test: the harness is not committed, the existing tests use spline order 3 only, and closed dimensions are reached only through a uint8_t PNG compare (Test5).

Please add a GoogleTest in Modules/Filtering/ImageGrid/test (*GTest.cxx, TEST(BSplineScatteredDataPointSetToImageFilter, ...)) covering:

  • spline orders 1, 2, and 3 (the FastEvaluate cases) and at least 4 and 5 (the m_Kernel[i]->Evaluate default; SetSplineOrder accepts any order >= 1)
  • mixed per-axis orders, e.g. {1, 3, 5}
  • closed dimensions, alone and mixed with open ones
  • ImageDimension 1 (if supported), 2, 3, and 4
  • scalar and vector-valued point data

Expected values must be exact (EXPECT_EQ): stored baselines generated from the pre-PR code, or a per-neighbor reference evaluation in the test using the kernel's Evaluate(). Smaller items are inline.

@gdevenyi

gdevenyi commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

@dzenanz Thanks, the session link has been removed from the description (and from the other PRs in this series).

Only order + 1 kernel values per axis differ among the (order + 1)^D
lattice neighbors of a point. Compute them once, and scatter into the
per-thread lattices through buffer offsets. The kernel values stay
double and the products and sums keep their order, so the lattice is
bit-identical.

A GoogleTest compares the fitted lattice exactly with a reference that
evaluates the kernel for each neighbor and axis, for spline orders 1 to
5, mixed per-axis orders, closed dimensions, 1 to 4 dimensions, and
scalar and vector point data.
@gdevenyi
gdevenyi force-pushed the perf-bspline-fit-kernel-cache branch from 32af356 to 9d3770b Compare November 2, 2026 21:11
@github-actions github-actions Bot added type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct labels Nov 2, 2026
@gdevenyi

gdevenyi commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

@hjmjohnson Thanks for the review. Updated in 9d3770b (amended, force-pushed):

  • New itkBSplineScatteredDataPointSetToImageFilterGTest.cxx fits one level with one work unit and compares the control point lattice with EXPECT_EQ-exact values from an in-test reference that evaluates the kernel's Evaluate() for each neighbor and axis, with the expressions of the previous loop. It covers spline orders 1-5 (FastEvaluate and the Cox-de Boor default), mixed per-axis orders ({1,3,5}, {5,2,4}, {2,3,1,4}), closed dimensions alone and mixed with open ones, ImageDimension 1, 2, 3 and 4, and 1- and 3-component (scalar and vector) point data.
  • The test also passes when built against main (so the reference reproduces the old code), and it fails if the kernel values are stored as float, so it catches rounding-level changes.
  • Inline points are addressed in the threads.

@hjmjohnson
hjmjohnson dismissed their stale review November 3, 2026 01:13

Round-1 requested changes are addressed; dismissing my change request.

@hjmjohnson hjmjohnson added the status:Confirmed Confirmed/reproduced issue on a different machine with same or similar settings to those reported label Nov 3, 2026
@hjmjohnson
hjmjohnson marked this pull request as ready for review November 3, 2026 01:28
@greptile-apps

greptile-apps Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Speeds up B-spline fitting by computing each kernel value once per axis and point.

  • B-spline fitting reuses each point’s per-axis kernel values across its neighbors.

No actionable issues found. Tests were reviewed but not run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Input point] --> B[Compute kernel values and offsets per axis]
  B --> C[Build neighbor weights in raster order]
  C --> D[Sum squared weights]
  D --> E[Add weighted values to thread-local lattices]
Loading

Reviews (1) · Last reviewed commit: "PERF: Compute B-spline fit kernel values..." · Reviewed by Greptile

@hjmjohnson
hjmjohnson merged commit f77bbc9 into InsightSoftwareConsortium:main Nov 3, 2026
18 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Filtering Issues affecting the Filtering module status:Confirmed Confirmed/reproduced issue on a different machine with same or similar settings to those reported type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Performance Improvement in terms of compilation or execution time type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants