-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(transforms): preserve device and dtype in squarepulse #9005
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
Open
aymuos15
wants to merge
7
commits into
Project-MONAI:dev
Choose a base branch
from
aymuos15:fix/squarepulse-cpu-return
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
33aaf9d
fix(transforms): preserve device and dtype in squarepulse
aymuos15 2529047
Merge branch 'dev' into fix/squarepulse-cpu-return
aymuos15 ae0021c
Merge branch 'dev' into fix/squarepulse-cpu-return
aymuos15 80f2276
Update monai/transforms/utils.py
aymuos15 2526a5a
Merge branch 'dev' into fix/squarepulse-cpu-return
aymuos15 6be9b73
fix(transforms): preserve float dtype in squarepulse
aymuos15 f1cd798
Merge branch 'dev' into fix/squarepulse-cpu-return
aymuos15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Copyright (c) MONAI Consortium | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import unittest | ||
|
|
||
| import torch | ||
| from parameterized import parameterized | ||
|
|
||
| from monai.transforms.utils import squarepulse | ||
| from tests.test_utils import skip_if_no_cuda | ||
|
|
||
| # Duty cycle used for value-equality checks across devices / dtypes. | ||
| _DUTY = 0.5 | ||
|
|
||
|
|
||
| class TestSquarePulse(unittest.TestCase): | ||
| @parameterized.expand([(torch.float32,), (torch.float64,)]) | ||
| def test_device_and_dtype_follow_floating_input(self, dtype): | ||
| t = torch.linspace(0, 12, 25, dtype=dtype) | ||
| y = squarepulse(t, duty=_DUTY) | ||
| self.assertEqual(y.device, t.device) | ||
| self.assertEqual(y.dtype, dtype) | ||
| self.assertEqual(tuple(y.shape), tuple(t.shape)) | ||
| # Square wave is in {-1, 1} for valid duty. | ||
| self.assertTrue(torch.all((y == 1) | (y == -1))) | ||
|
|
||
| def test_integer_input_promotes_to_default_float(self): | ||
| t = torch.arange(0, 25) | ||
| y = squarepulse(t, duty=_DUTY) | ||
| self.assertEqual(y.device, t.device) | ||
| self.assertEqual(y.dtype, torch.get_default_dtype()) | ||
| self.assertTrue(torch.all((y == 1) | (y == -1))) | ||
|
|
||
| def test_values_match_cpu_reference(self): | ||
| t = torch.linspace(0, 12, 25) | ||
| y = squarepulse(t, duty=_DUTY) | ||
| # Reference: same math on a fresh CPU float32 buffer (pre-fix default path). | ||
| tmod = torch.remainder(t, 2 * torch.pi) | ||
| expected = torch.where(tmod < _DUTY * 2 * torch.pi, torch.tensor(1.0), torch.tensor(-1.0)) | ||
| self.assertTrue(torch.equal(y, expected)) | ||
|
|
||
|
|
||
| @skip_if_no_cuda | ||
| class TestSquarePulseCuda(unittest.TestCase): | ||
| def test_cuda_input_stays_on_cuda(self): | ||
| """Regression: squarepulse must not silently return CPU for CUDA input.""" | ||
| t = torch.linspace(0, 12, 25, device="cuda") | ||
| y = squarepulse(t, duty=_DUTY) | ||
| self.assertEqual(y.device.type, "cuda") | ||
| self.assertEqual(y.dtype, t.dtype) | ||
| # Values must match the CPU reference (device is the only difference). | ||
| y_cpu = squarepulse(t.cpu(), duty=_DUTY) | ||
| self.assertTrue(torch.equal(y.cpu(), y_cpu)) | ||
|
|
||
| def test_float64_cuda_preserves_dtype_and_device(self): | ||
| t = torch.linspace(0, 12, 25, dtype=torch.float64, device="cuda") | ||
| y = squarepulse(t, duty=_DUTY) | ||
| self.assertEqual(y.device.type, "cuda") | ||
| self.assertEqual(y.dtype, torch.float64) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.