Commit 24da7c7
Register GlobalMutualInformationLoss bin_centers as buffer (#8869)
Fixes #8866.
### Description
This PR fixes a device placement bug in
`GlobalMutualInformationLoss.__init__` where `bin_centers` was assigned
as a plain Python attribute instead of being registered as a buffer.
- Registered `bin_centers` as a non-persistent buffer in
`image_dissimilarity.py`, ensuring that `GlobalMutualInformationLoss`
now follows normal `nn.Module` buffer semantics for `.to(device)` /
`dtype` moves and avoids silent gradient tracking.
- Added a regression test in `test_global_mutual_information_loss.py` to
verify that `bin_centers` is properly exposed through `named_buffers()`,
does not require gradients, and successfully changes dtype when the
module is moved.
*Verification:* Passed `python -m pytest
tests/losses/image_dissimilarity/test_global_mutual_information_loss.py
-q -k gaussian_bin_centers_registered_buffer` and `-k ill_opts`.
### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [x] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [x] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.
Signed-off-by: ugbotueferhire <ugbotueferhire@gmail.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>1 parent 837dc32 commit 24da7c7
2 files changed
Lines changed: 24 additions & 1 deletion
File tree
- monai/losses
- tests/losses/image_dissimilarity
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
| 236 | + | |
| 237 | + | |
236 | 238 | | |
237 | 239 | | |
238 | | - | |
| 240 | + | |
239 | 241 | | |
240 | 242 | | |
241 | 243 | | |
| |||
314 | 316 | | |
315 | 317 | | |
316 | 318 | | |
| 319 | + | |
| 320 | + | |
317 | 321 | | |
318 | 322 | | |
319 | 323 | | |
| |||
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
119 | 138 | | |
120 | 139 | | |
121 | 140 | | |
| |||
0 commit comments