This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add simple HPO search space validation #3877
Merged
Merged
Changes from 2 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
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 @@ | ||
import logging | ||
from typing import Any, List, Optional | ||
|
||
common_search_space_types = [ | ||
'choice', | ||
'randint', | ||
'uniform', | ||
'quniform', | ||
'loguniform', | ||
'qloguniform', | ||
'normal', | ||
'qnormal', | ||
'lognormal', | ||
'qlognormal', | ||
] | ||
|
||
def validate_search_space( | ||
search_space: Any, | ||
support_types: Optional[List[str]] = None, | ||
raise_exception: bool = False # for now, in case false positive | ||
) -> bool: | ||
|
||
if not raise_exception: | ||
try: | ||
validate_search_space(search_space, support_types, True) | ||
return True | ||
except ValueError as e: | ||
logging.getLogger(__name__).error(e.args[0]) | ||
return False | ||
|
||
if support_types is None: | ||
support_types = common_search_space_types | ||
|
||
if not isinstance(search_space, dict): | ||
raise ValueError('search space is not a dict') | ||
|
||
for name, spec in search_space.items(): | ||
if '_type' not in spec or '_value' not in spec: | ||
raise ValueError(f'search space "{name}" does not have "_type" or "_value"') | ||
liuzhe-lz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type_ = spec['_type'] | ||
if type_ not in support_types: | ||
raise ValueError(f'search space "{name}" has unsupported type {type_}') | ||
args = spec['_value'] | ||
if not isinstance(args, list): | ||
raise ValueError(f'search space "{name}"\'s value is not a list') | ||
|
||
if type_ == 'choice': | ||
continue | ||
|
||
if type_.startswith('q'): | ||
if len(args) != 3: | ||
raise ValueError(f'search space "{name}" ({type_}) must have 3 values') | ||
else: | ||
if len(args) != 2: | ||
raise ValueError(f'search space "{name}" ({type_}) must have 2 values') | ||
|
||
if type_ == 'randint': | ||
if not all(isinstance(arg, int) for arg in args): | ||
raise ValueError(f'search space "{name}" ({type_}) must have int values') | ||
else: | ||
if not all(isinstance(arg, (float, int)) for arg in args): | ||
raise ValueError(f'search space "{name}" ({type_}) must have float values') | ||
|
||
if 'normal' not in type_: | ||
if args[0] >= args[1]: | ||
raise ValueError(f'search space "{name}" ({type_}) must have high > low') | ||
if 'log' in type_ and args[0] <= 0: | ||
raise ValueError(f'search space "{name}" ({type_}) must have low > 0') | ||
else: | ||
if args[1] <= 0: | ||
raise ValueError(f'search space "{name}" ({type_}) must have sigma > 0') | ||
|
||
return True |
This file contains 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,50 @@ | ||
from nni.common.hpo_utils import validate_search_space | ||
|
||
good = { | ||
'choice': { '_type': 'choice', '_value': ['a', 'b'] }, | ||
'randint': { '_type': 'randint', '_value': [1, 10] }, | ||
'uniform': { '_type': 'uniform', '_value': [0, 1.0] }, | ||
'quniform': { '_type': 'quniform', '_value': [1, 10, 0.1] }, | ||
'loguniform': { '_type': 'loguniform', '_value': [0.001, 0.1] }, | ||
'qloguniform': { '_type': 'qloguniform', '_value': [0.001, 0.1, 0.001] }, | ||
'normal': { '_type': 'normal', '_value': [0, 0.1] }, | ||
'qnormal': { '_type': 'qnormal', '_value': [0.5, 0.1, 0.1] }, | ||
'lognormal': { '_type': 'lognormal', '_value': [0.0, 1] }, | ||
'qlognormal': { '_type': 'qlognormal', '_value': [-1, 1, 0.1] }, | ||
} | ||
good_partial = { | ||
'choice': good['choice'], | ||
'randint': good['randint'], | ||
} | ||
|
||
bad_type = 'x' | ||
bad_fields = { 'x': { 'type': 'choice', 'value': ['a', 'b'] } } | ||
bad_type_name = { 'x': { '_type': 'choic', '_value': ['a'] } } | ||
bad_value = { 'x': { '_type': 'choice', '_value': 'ab' } } | ||
bad_2_args = { 'x': { '_type': 'randint', '_value': [1, 2, 3] } } | ||
bad_3_args = { 'x': { '_type': 'quniform', '_value': [0] } } | ||
bad_int_args = { 'x': { '_type': 'randint', '_value': [1.0, 2.0] } } | ||
bad_float_args = { 'x': { '_type': 'uniform', '_value': ['0.1', '0.2'] } } | ||
bad_low_high = { 'x': { '_type': 'quniform', '_value': [2, 1, 0.1] } } | ||
bad_log = { 'x': { '_type': 'loguniform', '_value': [0, 1] } } | ||
bad_sigma = { 'x': { '_type': 'normal', '_value': [0, 0] } } | ||
|
||
def test_hpo_utils(): | ||
assert validate_search_space(good, raise_exception=False) | ||
assert not validate_search_space(bad_type, raise_exception=False) | ||
assert not validate_search_space(bad_fields, raise_exception=False) | ||
assert not validate_search_space(bad_type_name, raise_exception=False) | ||
assert not validate_search_space(bad_value, raise_exception=False) | ||
assert not validate_search_space(bad_2_args, raise_exception=False) | ||
assert not validate_search_space(bad_3_args, raise_exception=False) | ||
assert not validate_search_space(bad_int_args, raise_exception=False) | ||
assert not validate_search_space(bad_float_args, raise_exception=False) | ||
assert not validate_search_space(bad_low_high, raise_exception=False) | ||
assert not validate_search_space(bad_log, raise_exception=False) | ||
assert not validate_search_space(bad_sigma, raise_exception=False) | ||
|
||
assert validate_search_space(good_partial, ['choice', 'randint'], False) | ||
assert not validate_search_space(good, ['choice', 'randint'], False) | ||
|
||
if __name__ == '__main__': | ||
test_hpo_utils() |
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.
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.
If we never use the return value, change "validate_search_space" as void should be ok?
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.
As a public API, the caller might want validation result to decide following action.
For example, I think it's better to integrate validation into tuner API, and then perform early validation in nnictl. In this case, nnictl should halt launching when validation failed, but it won't want to raise the exception to end user.