-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(networks): AffineHead follows the module's dtype, not just device #9104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
cf074d6
9a258e9
906662f
5c1e07c
6012454
d2bab07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,6 +98,26 @@ def test_script(self, input_param, input_shape, _): | |
| test_data = torch.randn(input_shape) | ||
| test_script_save(net, test_data) | ||
|
|
||
| @parameterized.expand(TEST_CASES_GLOBAL_NET) | ||
| def test_half_precision_forward(self, input_param, input_shape, expected_shape): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Document The 🤖 Prompt for AI Agents |
||
| """Check that a GlobalNet cast to half precision can run a forward pass and returns float16. | ||
|
|
||
| `AffineHead.grid` used to be moved by device only, so casting the network left it at float32 while theta | ||
| became float16, and the einsum in `affine_transform` raised `RuntimeError: expected scalar type Half but | ||
| found Float` on the first half-precision forward call. | ||
|
|
||
| Args: | ||
| input_param: keyword arguments used to build the GlobalNet. | ||
| input_shape: shape of the random float16 input. | ||
| expected_shape: shape the returned displacement field must have. | ||
| """ | ||
| net = GlobalNet(**input_param).half() | ||
| with eval_mode(net): | ||
| img = torch.randn(input_shape, dtype=torch.float16) | ||
| result = net(img) | ||
| self.assertEqual(result.dtype, torch.float16) | ||
| self.assertEqual(result.shape, expected_shape) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
sed -n '280,315p' monai/networks/nets/regunet.py git diff --unified=20 -- monai/networks/nets/regunet.py tests/networks/nets/test_globalnet.pyRepository: Project-MONAI/MONAI
Length of output: 1585
Add a Google-style docstring to
AffineHead.forward.This changed definition has no docstring. The repository rule requires docstrings for every definition. Document
x,image_size, and the returned displacement field.🤖 Prompt for AI Agents