Prevent half-precision mutual information overflow#9017
Conversation
📝 WalkthroughWalkthrough
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 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 |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
monai/losses/image_dissimilarity.py (1)
320-344: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueAdd required Google-style docstrings.
monai/losses/image_dissimilarity.py#L320-L344: document returned weight/probability tensors and the buffer-relatedValueError.
monai/losses/image_dissimilarity.py#L359-L383: document the returned reduced loss tensor.
tests/losses/image_dissimilarity/test_global_mutual_information_loss.py#L167-L197: add docstrings for the new test class and methods.As per path instructions, “Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings.”
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@monai/losses/image_dissimilarity.py` around lines 320 - 344, The Gaussian Parzen windowing implementation in monai/losses/image_dissimilarity.py:320-344 must document the returned weight and probability tensors and the ValueError raised when bin_centers or preterm is unset; the reduced loss implementation in monai/losses/image_dissimilarity.py:359-383 must document its returned loss tensor; and the new test class and methods in tests/losses/image_dissimilarity/test_global_mutual_information_loss.py:167-197 must receive Google-style docstrings describing their purpose, variables, returns, and exceptions where applicable.Source: Path instructions
tests/losses/image_dissimilarity/test_global_mutual_information_loss.py (1)
168-195: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winCover
bfloat16and direct Parzen return contracts.The implementation supports
bfloat16, but these regressions exercise onlyfloat16. Run both dtypes and assertweight/probabilitypreserve dtype and device as well as finiteness.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/losses/image_dissimilarity/test_global_mutual_information_loss.py` around lines 168 - 195, Update the tests test_half_precision_gaussian_weights_with_many_bins_are_finite and test_half_precision_large_constant_volume_is_finite to run for both float16 and bfloat16. In the direct parzen_windowing_gaussian assertions, also verify weight and probability preserve the input dtype and device, while retaining the existing finiteness checks and loss/gradient contract assertions.Source: Path instructions
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@monai/losses/image_dissimilarity.py`:
- Around line 320-344: The Gaussian Parzen windowing implementation in
monai/losses/image_dissimilarity.py:320-344 must document the returned weight
and probability tensors and the ValueError raised when bin_centers or preterm is
unset; the reduced loss implementation in
monai/losses/image_dissimilarity.py:359-383 must document its returned loss
tensor; and the new test class and methods in
tests/losses/image_dissimilarity/test_global_mutual_information_loss.py:167-197
must receive Google-style docstrings describing their purpose, variables,
returns, and exceptions where applicable.
In `@tests/losses/image_dissimilarity/test_global_mutual_information_loss.py`:
- Around line 168-195: Update the tests
test_half_precision_gaussian_weights_with_many_bins_are_finite and
test_half_precision_large_constant_volume_is_finite to run for both float16 and
bfloat16. In the direct parzen_windowing_gaussian assertions, also verify weight
and probability preserve the input dtype and device, while retaining the
existing finiteness checks and loss/gradient contract assertions.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 0da9611f-61d1-4c73-98fc-b32ddc2ca78b
📒 Files selected for processing (2)
monai/losses/image_dissimilarity.pytests/losses/image_dissimilarity/test_global_mutual_information_loss.py
0ee73aa to
e700a64
Compare
Signed-off-by: kyinhub <kevinpyin@gmail.com>
e700a64 to
27eb62d
Compare
|
@coderabbitai review |
✅ Action performedReview finished.
|
Closes #7622.
Summary
Root cause
Two reduced-precision operations can become non-finite:
bmmaccumulates over all voxels before division, so AMP can overflow the float16 accumulator and return-infloss with non-finite gradients.The fix keeps public outputs in their original dtype while performing the numerically sensitive kernel and histogram accumulation in float32. Float64 inputs remain float64.
Validation
upstream/devproduces non-finite many-bin float16 weightsupstream/devreturns-infwith non-finite gradients on a48³volumegit diff --checkpassTypes of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.