Repository navigation
PERF: Reuse N4 iteration buffers to cut per-iteration allocations - #7085
hjmjohnson merged 3 commits into
Conversation
|
@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 buffer reuse in N4 looks sound, but the B-spline filter change needs to come out of this commit, and the equivalence claim needs an in-tree test before merge. These findings cover only this PR's own commit (87c3d0e); #7075 below it has a blocking release-data defect (the cached point set can be freed under GlobalReleaseDataFlag) that must be fixed first.
- Please pin the bit-identity claim with an in-tree test: an exact-baseline GoogleTest or the committed comparison harness. Nothing in the current N4 tests would detect a value change.
- The title overstates the change: each iteration still allocates one full-size
Vector<float,1>output in the reconstructer (hxx:662), so allocations drop from about six per iteration to one, not to zero. #include "itkSubtractImageFilter.h"(hxx:31) is now unused; please drop it.
Details are inline.
87c3d0e to
53324d1
Compare
|
Thanks @hjmjohnson. I rebased onto the revised #7075 (76f7e68) and force-pushed. There are now two commits:
The bit-identity claim is now pinned in-tree by the N4 GoogleTests that #7075 adds. They compare a mask label with a binary mask exactly and a binary confidence image with a mask exactly, and compare a mask label plus confidence run against values generated with Details are inline. |
|
The B-spline re- |
|
@greptileai review |
[Medium impact] The PR appears safe to merge; no actionable defects were found.
|
hjmjohnson
left a comment
There was a problem hiding this comment.
Thanks. The B-spline output change is now its own commit with a doc line and a GTest that fails on main; ReconstructBiasFieldInto and MakeBiasFieldReconstructer replace the three copies; <utility> is included; the no-op DisconnectPipeline() is gone; and all buffers use one region.
Requesting changes, and the main item is my fault: the "empty buffered region, largest region = m_Size" state I suggested in round 1 lets a second Update() re-execute. Details inline.
Also, the bit-identity claim is not pinned in tree. Two of the #7075 GTests compare two runs of the same patched code, and the baseline test checks 5 pixels at relative 1e-4. All of them set ConvergenceThreshold(0.0) and never read the convergence measurement, so the rewritten CalculateConvergenceMeasurement has no test.
Non-blocking:
itkN4BiasFieldCorrectionImageFilter.hxxstill includesitkBSplineControlPointImageFilter.h, which the header now provides.testing.mdnames GTest filesitkFooGTest.cxx;...OutputGTest.cxxdeparts from that.- The PR body still ends with the "Generated with Claude Code" footer; the AI disclosure in
<details>is enough (non-blocking).
53324d1 to
751c0ea
Compare
|
Thanks @hjmjohnson. I force-pushed 7f6f9b5 (N4) and 751c0ea (B-spline) with the inline items. Bit identity. The convergence measurement is now asserted. On the outputs: the two exact-equality tests from #7075 compare equivalent configurations (a mask label against a binary mask, and a binary confidence image against a mask), both run on the patched code. The only comparison against Non-blocking items:
|
|
Rebased onto |
751c0ea to
9c176f9
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The test tolerance is smaller than the documented thread-dependent variation, so an unchanged implementation can fail when the fitting thread configuration differs from the single-thread reference.
1 open issue
What changed in this PR
This PR reduces allocation overhead in ITK’s N4 bias correction filter, building on the fit-point reuse work in #7075 while preserving the existing numerical operations.
Changes:
- Reuse residual and bias-field buffers, update the uncorrected image in place, and compute convergence differences without a temporary image.
- Skip sampled B-spline output allocation when output generation is disabled.
- Add regression coverage for N4 results and B-spline output state across repeated updates.
| File | Description |
|---|---|
Modules/Filtering/ImageGrid/test/itkBSplineScatteredDataPointSetToImageFilterGTest.cxx |
Tests unbuffered output, lattice equivalence, and repeated updates. |
Modules/Filtering/ImageGrid/include/itkBSplineScatteredDataPointSetToImageFilter.hxx |
Allocates sampled output only when requested. |
Modules/Filtering/ImageGrid/include/itkBSplineScatteredDataPointSetToImageFilter.h |
Documents output state when generation is disabled. |
Modules/Filtering/BiasCorrection/test/itkN4BiasFieldCorrectionImageFilterGTest.cxx |
Adds mask, confidence, baseline, and convergence checks. |
Modules/Filtering/BiasCorrection/include/itkN4BiasFieldCorrectionImageFilter.hxx |
Reuses fit data and iteration buffers and consolidates reconstruction setup. |
Modules/Filtering/BiasCorrection/include/itkN4BiasFieldCorrectionImageFilter.h |
Declares reusable fit data and private reconstruction helpers. |
Review effort: Balanced
9c176f9 to
848906e
Compare
Requested changes are addressed; dismissing my change request.
|
@greptileai review |
848906e to
7af9818
Compare
7af9818 to
ef3eff1
Compare
The fit points and weights depend only on the mask, the confidence image and the input geometry. Build their containers once in GenerateData, and in each iteration gather only the residual field values, in parallel, into a fresh point set that shares the containers, so releasing the fit's input cannot empty them. The points, weights and values are unchanged, so the output is bit-identical. GoogleTests compare a mask label with a binary mask, a binary confidence image with a mask, and a mask label with a confidence image against values from main.
Each N4 iteration allocated new residual, uncorrected and bias field images through pipeline filters, plus a difference image for the convergence test. Keep one residual buffer and two bias field buffers that swap, and compute the subtractions and the component copy in parallel loops with the same float arithmetic. One reconstructer output per iteration remains. The reconstructer setup is shared by one helper.
With GenerateOutputImage off, BSplineScatteredDataPointSetToImageFilter still allocated a full output image that it never wrote. Leave the output with its geometry but empty regions then, so a caller cannot read an unwritten buffer, and a later Update() of the unmodified filter does not re-execute. N4 turns GenerateOutputImage off for each of its fits. A GoogleTest checks the output state, a second Update(), and that the control point lattice is unchanged.
ef3eff1 to
bb504fe
Compare
|
Rebased onto |

Reuse N4's per-iteration buffers, cutting allocations from 5-6 images per iteration to one. Bit-identical; full-resolution N4 is 7 % faster with #7072 and #7075. Stacked on #7075; the last two commits are new.
Details
Each N4 iteration created new images for the residual (
SubtractImageFilter), the reconstructed field (B-spline output plus aVectorIndexSelectionCastImageFiltercopy), the convergence difference image and the uncorrected image, and the B-spline fit allocated a full-size output that it never wrote (ANTsX/ANTs#2037, item N4-5).ReconstructBiasField(lattice)is unchanged; a privateReconstructBiasFieldIntowrites into a given image, and one helper sets up the reconstructer for both and for the lattice refinement. One reconstructer output per iteration remains.CalculateConvergenceMeasurementcomputes the difference in its loop, with the same float subtraction and the same serial summation order, so the convergence value is unchanged.BSplineScatteredDataPointSetToImageFilterallocates its output only whenGenerateOutputImageis on. When it is off, the output has its geometry but empty regions and no buffer (documented onGenerateOutputImage), so a caller cannot read an unwritten buffer and a secondUpdate()of the unmodified filter does not re-execute.Measurements
AMD Ryzen 9 9950X, GCC 13.3, Release, 16 threads. N4 on MNI152 2009c T1 (1 mm, no shrink), 3 levels x 15 iterations, four runs (no mask; mask; mask and confidence image; oblique direction), two repeats:
Test results
ctest -R 'BSpline|N4|BiasCorrection': 34/34 pass, including the N4 GoogleTests from PERF: Build N4 B-spline fit points once instead of in each iteration #7075 (mask label, confidence image, and values frommain, now also the convergence measurement) and the new B-spline output-state GoogleTest with a secondUpdate(). The build adds no warnings.Update()after removing the mask, and the raw lattice and output ofBSplineScatteredDataPointSetToImageFilterfor spline orders 1-5, 2-D and 3-D and closed dimensions, withcmpagainstmain: identical.AI assistance