-
Notifications
You must be signed in to change notification settings - Fork 325
Enable GPU usage for MacOS (using MPS) #464
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import platform | ||
| import warnings | ||
|
|
||
| import torch | ||
|
|
||
|
|
||
| def _get_enable_gpu_value(enable_gpu, cuda): | ||
| """Validate both the `enable_gpu` and `cuda` parameters. | ||
|
|
||
| The logic here is to: | ||
| - Raise a warning if `cuda` is set because it's deprecated. | ||
| - Raise an error if both parameters are set in a conflicting way. | ||
| - Return the resolved `enable_gpu` value. | ||
| """ | ||
| if cuda is not None: | ||
| warnings.warn( | ||
| '`cuda` parameter is deprecated and will be removed in a future release. ' | ||
| 'Please use `enable_gpu` instead.', | ||
| FutureWarning, | ||
| ) | ||
| if not enable_gpu: | ||
| raise ValueError( | ||
| 'Cannot resolve the provided values of `cuda` and `enable_gpu` parameters. ' | ||
| 'Please use only `enable_gpu`.' | ||
| ) | ||
|
|
||
| enable_gpu = cuda | ||
|
|
||
| return enable_gpu | ||
|
|
||
|
|
||
| def _set_device(enable_gpu, device=None): | ||
| """Set the torch device based on the `enable_gpu` parameter and system capabilities.""" | ||
| if device: | ||
| return torch.device(device) | ||
|
|
||
| if enable_gpu: | ||
| if platform.system() == 'Darwin': # macOS | ||
| if ( | ||
| platform.machine() == 'arm64' | ||
| and getattr(torch.backends, 'mps', None) | ||
| and torch.backends.mps.is_available() | ||
| ): | ||
| device = 'mps' | ||
| else: | ||
| device = 'cpu' | ||
| else: # Linux/Windows | ||
| device = 'cuda' if torch.cuda.is_available() else 'cpu' | ||
gsheni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else: | ||
| device = 'cpu' | ||
|
|
||
| return torch.device(device) | ||
|
|
||
|
|
||
| def validate_and_set_device(enable_gpu, cuda): | ||
| enable_gpu = _get_enable_gpu_value(enable_gpu, cuda) | ||
| return _set_device(enable_gpu) | ||
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
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
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,78 @@ | ||
| import platform | ||
| import re | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from ctgan.synthesizers._utils import _get_enable_gpu_value, _set_device, validate_and_set_device | ||
|
|
||
|
|
||
| def test__validate_gpu_parameter(): | ||
| """Test the ``_get_enable_gpu_value`` method.""" | ||
| # Setup | ||
| expected_error = re.escape( | ||
| 'Cannot resolve the provided values of `cuda` and `enable_gpu` parameters. ' | ||
| 'Please use only `enable_gpu`.' | ||
| ) | ||
| expected_warning = re.escape( | ||
| '`cuda` parameter is deprecated and will be removed in a future release. ' | ||
| 'Please use `enable_gpu` instead.' | ||
| ) | ||
|
|
||
| # Run | ||
| enable_gpu_1 = _get_enable_gpu_value(False, None) | ||
| enable_gpu_2 = _get_enable_gpu_value(True, None) | ||
| with pytest.warns(FutureWarning, match=expected_warning): | ||
| enable_gpu_3 = _get_enable_gpu_value(True, False) | ||
|
|
||
| with pytest.raises(ValueError, match=expected_error): | ||
| _get_enable_gpu_value(False, True) | ||
|
|
||
| # Assert | ||
| assert enable_gpu_1 is False | ||
| assert enable_gpu_2 is True | ||
| assert enable_gpu_3 is False | ||
|
|
||
|
|
||
| def test__set_device(): | ||
| """Test the ``_set_device`` method.""" | ||
| # Run | ||
| device_1 = _set_device(False) | ||
| device_2 = _set_device(True) | ||
| device_3 = _set_device(True, 'cpu') | ||
| device_4 = _set_device(enable_gpu=False, device='cpu') | ||
|
|
||
| # Assert | ||
| if ( | ||
| platform.machine() == 'arm64' | ||
| and getattr(torch.backends, 'mps', None) | ||
| and torch.backends.mps.is_available() | ||
| ): | ||
| expected_device_2 = torch.device('mps') | ||
| elif torch.cuda.is_available(): | ||
| expected_device_2 = torch.device('cuda') | ||
| else: | ||
| expected_device_2 = torch.device('cpu') | ||
|
|
||
| assert device_1 == torch.device('cpu') | ||
| assert device_2 == expected_device_2 | ||
| assert device_3 == torch.device('cpu') | ||
| assert device_4 == torch.device('cpu') | ||
|
|
||
|
|
||
| @patch('ctgan.synthesizers._utils._set_device') | ||
| @patch('ctgan.synthesizers._utils._get_enable_gpu_value') | ||
| def test_validate_and_set_device(mock_validate, mock_set_device): | ||
| """Test the ``validate_and_set_device`` method.""" | ||
| # Setup | ||
| mock_validate.return_value = True | ||
| mock_set_device.return_value = torch.device('cuda') | ||
|
|
||
| # Run | ||
| device = validate_and_set_device(enable_gpu=True, cuda=None) | ||
|
|
||
| # Assert | ||
| mock_validate.assert_called_once_with(True, None) | ||
| mock_set_device.assert_called_once_with(True) | ||
| assert device == torch.device('cuda') |
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
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.