fix(patching): keep overlap-blend reassembly in the patch dtype (halve peak memory) - #27
Merged
Merged
Conversation
Accumulator.assemble allocated weight_sum as a default float32 tensor, so the final result / weight_sum normalization promoted the whole (channels x volume) accumulator to float32 even when the patches were float16. For large multi-class reassemblies -- e.g. a 118-class whole-body segmentation -- this silently doubled the peak memory of the assembled volume and could OOM. Allocate weight_sum in the result dtype so the division stays in the patch dtype. Adds a dtype-preservation regression test.
This was referenced Jul 5, 2026
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.
Summary
Accumulator.assemble(overlap-blend reassembly) allocatedweight_sumas a default float32tensor. The final normalization
result = result / weight_sumthen promoted the whole(channels × volume)accumulator to float32 even when the patches were float16 (as they are forautocast/fp16 inference, where
ModelCompositecasts outputs to fp16).For large multi-class reassemblies this silently doubled the peak memory of the assembled volume.
Concretely, TotalSegmentator's 118-class whole-body output at inference has an assembled tensor of
shape
(118, D, H, W)— fortotal-3mmthat is ~3.1e9 elements = 12.5 GB in fp32 vs 6.3 GB in fp16;for the full
totalat 1.5 mm it is ~8× larger and was OOM-ing.The fix
Allocate
weight_sumwithdtype=result.dtype, soresult / weight_sumstays in the patch dtype andthe accumulator is never promoted to float32.
Effect (measured on
TotalSegmentator-KonfAI:total-3mm, autocast fp16)class-boundary ties. The pipeline is already fp16 end-to-end (composite casts to fp16 before
accumulation), so the float32 division was a spurious upcast, not a source of truth.
Test plan
test_blended_reassembly_preserves_patch_dtype: a multi-channel float16 Cosinus-blendedreassembly must stay float16.
test_patch_overlap_border,test_patching) andtest_predictor_memorystill pass (17 passed).Independent of #26 (device placement); together they let TotalSegmentator run without crashing or OOM.