Repository navigation
PERF: Compute B-spline fit kernel values once per axis per point - #7072
hjmjohnson merged 1 commit into
Conversation
|
I do not have access to the claude session linked in PR description. Either make it accessible publicly, or remove it. |
|
Someone more familiar with this code should review it. Bit-identical results with 5x speedup sounds great! |
|
@ntustison could you review this when you have a chance? It is one of the upstream items from ANTsX/ANTs#2037. |
hjmjohnson
left a comment
There was a problem hiding this comment.
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
FastEvaluatecases) and at least 4 and 5 (them_Kernel[i]->Evaluatedefault;SetSplineOrderaccepts 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.
|
@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.
32af356 to
9d3770b
Compare
|
@hjmjohnson Thanks for the review. Updated in 9d3770b (amended, force-pushed):
|
Round-1 requested changes are addressed; dismissing my change request.
[Medium impact] The PR appears safe to merge.
|
f77bbc9
into
InsightSoftwareConsortium:main
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,
ThreadedGenerateDataForFittingcalled the virtual kernelEvaluate192 times per point, but only 12 values differ. A profile ofN4BiasFieldCorrectionput about 89 % of its cycles in this function (ANTsX/ANTs#2037, item N4-6).The kernel values are kept as
double,Bis multiplied in the same axis order from1.0, and the neighbours are visited in the same raster order, sow2Sumand 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):
mainTest results
ctest -R 'BSpline|N4|BiasCorrection': 31/31 pass. The build adds no warnings.BSplineScatteredDataPointSetToImageFilterfor 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.cmpofmainagainst 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