-
Notifications
You must be signed in to change notification settings - Fork 7
chore(devicePicker): handle more and return device type #113
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
5 commits
Select commit
Hold shift + click to select a range
a7df29c
feat(torch): Refactor pick_device to return DeviceType
HaoZeke 1d5d58b
refactor(torch): Clean up pick_device Python bindings
HaoZeke 8b4f6a6
Update metatomic-torch/src/register.cpp
HaoZeke e38c3e0
tst(torch): Update and add tests for pick_device
HaoZeke b09069c
chore(CI): Update setuptools dependency in tox.ini
HaoZeke 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
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,53 @@ | ||
| import pytest | ||
| import torch | ||
|
|
||
| import metatomic.torch as mta | ||
|
|
||
|
|
||
| def test_pick_device_basic(): | ||
| # basic call should return a non-empty string describing a device | ||
| res = mta.pick_device(["cpu", "cuda", "mps"], None) | ||
| assert isinstance(res, str) | ||
| assert len(res) > 0 | ||
| # sanity: in typical environments we'll at least see "cpu" somewhere | ||
| assert "cpu" == res or "cuda" == res or "mps" == res | ||
|
|
||
|
|
||
| def test_pick_device_requested_if_available(): | ||
| # if CUDA is available, requesting it should yield a cuda device string | ||
| if torch.cuda.is_available(): | ||
| res = mta.pick_device(["cpu", "cuda"], "cuda") | ||
| assert isinstance(res, str) | ||
| assert "cuda" == res | ||
| # if MPS is available, requesting it should yield an mps device string | ||
| if hasattr(torch.backends, "mps") and torch.backends.mps.is_available(): | ||
| res = mta.pick_device(["cpu", "mps"], "mps") | ||
| assert isinstance(res, str) | ||
| assert "mps" == res | ||
|
|
||
|
|
||
| def test_pick_device_ignores_unrecognized_and_warns(capfd): | ||
| # Ensure unrecognized device names are ignored and a warning is emitted | ||
| res = mta.pick_device(["cpu", "fooo"], None) | ||
| assert isinstance(res, str) | ||
| # should pick cpu (ignore "fooo") | ||
| assert "cpu" == res | ||
| # at least one warning should have been produced about the unrecognized/ | ||
| # ignored entry | ||
| captured = capfd.readouterr() | ||
| err_output = captured.err.lower() | ||
| assert "fooo" in err_output | ||
| assert "ignoring" in err_output or "unknown" in err_output | ||
|
|
||
|
|
||
| def test_pick_device_error_on_unavailable_requested(): | ||
| # If a device is explicitly requested but isn't available/declared, | ||
| # the python wrapper is expected to raise a RuntimeError (propagated from C++). | ||
| only_cuda = ["cuda"] | ||
| if not torch.cuda.is_available(): | ||
| with pytest.raises(RuntimeError): | ||
| mta.pick_device(only_cuda, "cuda") | ||
| else: | ||
| # If CUDA is available, requesting a non-present device should raise | ||
| with pytest.raises(RuntimeError): | ||
| mta.pick_device(["cpu"], "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
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.