-
Notifications
You must be signed in to change notification settings - Fork 27
Add explicit dGPU target device support #243
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
3 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
Some comments aren't visible on the classic Files Changed page.
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
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,49 @@ | ||
| """ | ||
| Utilities for validating benchmark target device arguments. | ||
| """ | ||
|
|
||
| import argparse | ||
| import os | ||
| import re | ||
|
|
||
|
|
||
| _GPU_INDEX_PATTERN = re.compile(r"^GPU\.(\d+)$", re.IGNORECASE) | ||
|
|
||
|
|
||
| def validate_target_device(value: str) -> str: | ||
| """Validate and normalize target device values. | ||
|
|
||
| Accepted values: | ||
| - CPU | ||
| - GPU | ||
| - GPU.<index> where index is a non-negative integer | ||
| - NPU | ||
| """ | ||
| normalized = value.strip() | ||
| upper_value = normalized.upper() | ||
|
|
||
| if upper_value in {"CPU", "GPU", "NPU"}: | ||
| return upper_value | ||
|
|
||
| gpu_match = _GPU_INDEX_PATTERN.fullmatch(normalized) | ||
| if gpu_match: | ||
| return f"GPU.{gpu_match.group(1)}" | ||
|
|
||
| raise argparse.ArgumentTypeError( | ||
| "invalid target device '%s'. Expected one of: CPU, GPU, NPU, GPU.<index>" | ||
| % value | ||
| ) | ||
|
|
||
|
|
||
| def resolve_target_device_default(default_value: str, | ||
| env_var_name: str = "TARGET_DEVICE") -> str: | ||
| """Resolve default target device using env var and validate/normalize it. | ||
|
|
||
| Precedence: | ||
| 1) explicit CLI value (handled by argparse separately) | ||
| 2) environment variable | ||
| 3) hard-coded default | ||
| """ | ||
| env_value = os.getenv(env_var_name) | ||
| candidate = env_value if env_value and env_value.strip() else default_value | ||
| return validate_target_device(candidate) |
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,56 @@ | ||
| """ | ||
| Unit tests for benchmark target device validation. | ||
| """ | ||
|
|
||
| import argparse | ||
| import os | ||
| import unittest | ||
| from unittest import mock | ||
|
|
||
| from device_validation import validate_target_device, resolve_target_device_default | ||
|
|
||
|
|
||
| class TestDeviceValidation(unittest.TestCase): | ||
|
|
||
| def test_valid_target_devices(self): | ||
| test_cases = { | ||
| 'CPU': 'CPU', | ||
| 'GPU': 'GPU', | ||
| 'GPU.0': 'GPU.0', | ||
| 'GPU.1': 'GPU.1', | ||
| 'GPU.2': 'GPU.2', | ||
| 'NPU': 'NPU', | ||
| } | ||
|
|
||
| for user_value, expected in test_cases.items(): | ||
| with self.subTest(user_value=user_value): | ||
| self.assertEqual(validate_target_device(user_value), expected) | ||
|
|
||
| def test_invalid_target_devices(self): | ||
| invalid_values = ['GPU.', 'GPU.A', 'GPU.-1', 'GPU.abc'] | ||
|
|
||
| for user_value in invalid_values: | ||
| with self.subTest(user_value=user_value): | ||
| with self.assertRaises(argparse.ArgumentTypeError): | ||
| validate_target_device(user_value) | ||
|
|
||
| def test_env_target_device_valid(self): | ||
| with mock.patch.dict(os.environ, {'TARGET_DEVICE': 'gpu.2'}, clear=False): | ||
| self.assertEqual(resolve_target_device_default('CPU'), 'GPU.2') | ||
|
|
||
| def test_env_target_device_invalid(self): | ||
| with mock.patch.dict(os.environ, {'TARGET_DEVICE': 'GPU.-1'}, clear=False): | ||
| with self.assertRaises(argparse.ArgumentTypeError): | ||
| resolve_target_device_default('CPU') | ||
|
|
||
| def test_env_target_device_fallback_when_missing(self): | ||
| with mock.patch.dict(os.environ, {}, clear=True): | ||
| self.assertEqual(resolve_target_device_default('CPU'), 'CPU') | ||
|
|
||
| def test_env_target_device_fallback_when_empty(self): | ||
| with mock.patch.dict(os.environ, {'TARGET_DEVICE': ' '}, clear=False): | ||
| self.assertEqual(resolve_target_device_default('GPU'), 'GPU') | ||
|
|
||
|
|
||
| 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.