Perf: skip redundant full-image mask on StdShiftIntensity nonzero=False path#8975
Open
aymuos15 wants to merge 1 commit into
Open
Perf: skip redundant full-image mask on StdShiftIntensity nonzero=False path#8975aymuos15 wants to merge 1 commit into
aymuos15 wants to merge 1 commit into
Conversation
Contributor
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthrough
Estimated code review effort: 1 (Trivial) | ~3 minutes Related issues: Suggested labels: performance, transforms 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
a9f6888 to
f72c648
Compare
On the default nonzero=False path the boolean mask is all-True, so img[slices] just gathers and scatters the whole image. Shift the image directly instead, avoiding the mask allocation, .any(), gather and scatter. Output is unchanged. Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
f72c648 to
2e4fafe
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #8972 .
Description
On the default
nonzero=FalsepathStdShiftIntensity._stdshiftbuilt an all-True boolean mask the size of the image and shifted through it (img[slices] = img[slices] + offset), forcing a full advanced-index gather and scatter plus the mask allocation and.any()scan even though every voxel is selected. That is equivalent to shifting the image directly, so thenonzero=Falsebranch now doesimg + factor * std(img). Output is bit-for-bit identical and thenonzero=Truepath is untouched.RandStdShiftIntensitybenefits too, since it also defaults tononzero=False. Measured across 2D/3D, single and multi channel, float32 and float64 on both backends (best-of-3, CPU); output verified equal in every configuration:numpy ranges 1.55x to 2.65x and torch 8.5x to 66x across the full sweep; the largest gains are on torch, where all-True boolean-mask indexing is especially costly.
Types of changes