Repository navigation
Commit cb35d45
fix(networks): AffineHead follows the module's dtype, not just device (#9104)
Fixes # .
### Description
`GlobalNet`/`LocalNet` (both build on `AffineHead`) crash immediately on
the first half-precision forward call:
```python
import torch
from monai.networks.nets import GlobalNet
net = GlobalNet(image_size=[16,16], spatial_dims=2, in_channels=1,
num_channel_initial=4, depth=2).half()
net(torch.randn(1, 1, 16, 16, dtype=torch.float16))
# RuntimeError: expected scalar type Half but found Float
```
`AffineHead.grid` is a plain attribute
(`torch.stack(...).to(dtype=torch.float)`
in `get_reference_grid`, never `register_buffer`'d), so `.half()` on the
containing module never touches it. `forward()` re-derives only its
*device* every call:
```python
self.grid = self.grid.to(device=f.device)
```
never its dtype. So after `.half()`, `theta` (from `self.fc`, whose
weights *do* move) becomes `float16`, but `self.grid` stays `float32`,
and `affine_transform`'s `torch.einsum(..., grid_padded, theta...)`
raises immediately since it requires matching dtypes.
### Fix
Re-derive dtype alongside device every forward call:
```python
self.grid = self.grid.to(device=f.device, dtype=f.dtype)
```
`f`'s dtype is a safe reference point here: `self.fc(f.reshape(...))`
(the very next line) already requires `f`'s dtype to match `self.fc`'s
weights for that call to succeed, so by the time
`affine_transform(theta)
- self.grid` runs, `f.dtype` is guaranteed consistent with `theta`'s
dtype.
### Verified
- Half-precision forward no longer crashes, on `AffineHead` directly and
through the full `GlobalNet` network; output is `float16` at the
correct shape.
- `float32` path is unaffected (`f.dtype` was already `torch.float` in
that case, so this is a no-op there).
- Added `test_half_precision_forward` to `test_globalnet.py`
(parametrized over the existing `TEST_CASES_GLOBAL_NET`). Confirmed it
fails with the exact `RuntimeError` above on unpatched code, passes
with the fix.
- Full existing `test_globalnet.py` and `test_regunet.py` suites pass
unchanged (8/8 and 4/4 real tests respectively).
### Types of changes
- [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 (`pytest
tests/networks/nets/test_globalnet.py
tests/networks/nets/regunet/test_regunet.py`).
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.
### AI-assistance disclosure
Found via the same metamorphic dtype-preservation sweep that flagged
#9103 (checking whether a module's registered state actually follows
`.to()`/`.half()`). Claude (Anthropic) was used to trace the root cause,
write the fix and the test, and verify fail-on-unpatched / pass-with-fix
by actually running the code. I reviewed the root cause, the fix, and
the test before opening this PR.
---------
Signed-off-by: tritsystem <gbranaa4@gmail.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>1 parent e74ef7d commit cb35d45
2 files changed
Lines changed: 32 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
294 | 294 | | |
295 | 295 | | |
296 | 296 | | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
297 | 308 | | |
298 | | - | |
| 309 | + | |
299 | 310 | | |
300 | 311 | | |
301 | 312 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
101 | 121 | | |
102 | 122 | | |
103 | 123 | | |
0 commit comments