Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
nni
Commits
31f11f51
Unverified
Commit
31f11f51
authored
Jan 11, 2022
by
liuzhe-lz
Committed by
GitHub
Jan 11, 2022
Browse files
Fix search space compatibility with JSON (#4455)
parent
452e69f3
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
122 additions
and
4 deletions
+122
-4
nni/experiment/config/experiment_config.py
nni/experiment/config/experiment_config.py
+22
-4
test/ut/experiment/assets/ss.yaml
test/ut/experiment/assets/ss.yaml
+9
-0
test/ut/experiment/assets/ss_comma.json
test/ut/experiment/assets/ss_comma.json
+10
-0
test/ut/experiment/assets/ss_tab.json
test/ut/experiment/assets/ss_tab.json
+10
-0
test/ut/experiment/assets/ss_tab_comma.json
test/ut/experiment/assets/ss_tab_comma.json
+10
-0
test/ut/experiment/assets/ss_yaml12.yaml
test/ut/experiment/assets/ss_yaml12.yaml
+9
-0
test/ut/experiment/test_search_space.py
test/ut/experiment/test_search_space.py
+52
-0
No files found.
nni/experiment/config/experiment_config.py
View file @
31f11f51
...
@@ -8,7 +8,9 @@ Top level experiement configuration class, ``ExperimentConfig``.
...
@@ -8,7 +8,9 @@ Top level experiement configuration class, ``ExperimentConfig``.
__all__
=
[
'ExperimentConfig'
]
__all__
=
[
'ExperimentConfig'
]
from
dataclasses
import
dataclass
from
dataclasses
import
dataclass
import
json
import
logging
import
logging
from
pathlib
import
Path
from
typing
import
Any
,
List
,
Optional
,
Union
from
typing
import
Any
,
List
,
Optional
,
Union
import
yaml
import
yaml
...
@@ -113,6 +115,16 @@ class ExperimentConfig(ConfigBase):
...
@@ -113,6 +115,16 @@ class ExperimentConfig(ConfigBase):
super
().
_canonicalize
([
self
])
super
().
_canonicalize
([
self
])
if
self
.
search_space_file
is
not
None
:
yaml_error
=
None
try
:
self
.
search_space
=
_load_search_space_file
(
self
.
search_space_file
)
except
Exception
as
e
:
yaml_error
=
repr
(
e
)
if
yaml_error
is
not
None
:
# raise it outside except block to make stack trace clear
msg
=
f
'ExperimentConfig: Failed to load search space file "
{
self
.
search_space_file
}
":
{
yaml_error
}
'
raise
ValueError
(
msg
)
if
self
.
nni_manager_ip
is
None
:
if
self
.
nni_manager_ip
is
None
:
# show a warning if user does not set nni_manager_ip. we have many issues caused by this
# show a warning if user does not set nni_manager_ip. we have many issues caused by this
# the simple detection logic won't work for hybrid, but advanced users should not need it
# the simple detection logic won't work for hybrid, but advanced users should not need it
...
@@ -133,10 +145,6 @@ class ExperimentConfig(ConfigBase):
...
@@ -133,10 +145,6 @@ class ExperimentConfig(ConfigBase):
if
not
self
.
use_annotation
and
space_cnt
<
1
:
if
not
self
.
use_annotation
and
space_cnt
<
1
:
raise
ValueError
(
'ExperimentConfig: search_space and search_space_file must be set one'
)
raise
ValueError
(
'ExperimentConfig: search_space and search_space_file must be set one'
)
if
self
.
search_space_file
is
not
None
:
with
open
(
self
.
search_space_file
)
as
ss_file
:
self
.
search_space
=
yaml
.
safe_load
(
ss_file
)
# to make the error message clear, ideally it should be:
# to make the error message clear, ideally it should be:
# `if concurrency < 0: raise ValueError('trial_concurrency ({concurrency}) must greater than 0')`
# `if concurrency < 0: raise ValueError('trial_concurrency ({concurrency}) must greater than 0')`
# but I believe there will be hardy few users make this kind of mistakes, so let's keep it simple
# but I believe there will be hardy few users make this kind of mistakes, so let's keep it simple
...
@@ -156,3 +164,13 @@ class ExperimentConfig(ConfigBase):
...
@@ -156,3 +164,13 @@ class ExperimentConfig(ConfigBase):
tuner_cnt
=
(
self
.
tuner
is
not
None
)
+
(
self
.
advisor
is
not
None
)
tuner_cnt
=
(
self
.
tuner
is
not
None
)
+
(
self
.
advisor
is
not
None
)
if
tuner_cnt
!=
1
:
if
tuner_cnt
!=
1
:
raise
ValueError
(
'ExperimentConfig: tuner and advisor must be set one'
)
raise
ValueError
(
'ExperimentConfig: tuner and advisor must be set one'
)
def
_load_search_space_file
(
search_space_path
):
# FIXME
# we need this because PyYAML 6.0 does not support YAML 1.2,
# which means it is not fully compatible with JSON
content
=
Path
(
search_space_path
).
read_text
(
encoding
=
'utf8'
)
try
:
return
json
.
loads
(
content
)
except
Exception
:
return
yaml
.
safe_load
(
content
)
test/ut/experiment/assets/ss.yaml
0 → 100644
View file @
31f11f51
pool_type
:
_type
:
choice
_value
:
-
max
-
min
-
avg
学习率
:
# test unicode
_type
:
loguniform
_value
:
[
0.0000001
,
0.1
]
test/ut/experiment/assets/ss_comma.json
0 → 100644
View file @
31f11f51
{
"pool_type"
:
{
"_type"
:
"choice"
,
"_value"
:
[
"max"
,
"min"
,
"avg"
],
},
"学习率"
:
{
"_type"
:
"loguniform"
,
"_value"
:
[
0.0000001
,
0.1
],
},
}
test/ut/experiment/assets/ss_tab.json
0 → 100644
View file @
31f11f51
{
"pool_type"
:
{
"_type"
:
"choice"
,
"_value"
:
[
"max"
,
"min"
,
"avg"
]
},
"学习率"
:
{
"_type"
:
"loguniform"
,
"_value"
:
[
1e-7
,
0.1
]
}
}
test/ut/experiment/assets/ss_tab_comma.json
0 → 100644
View file @
31f11f51
{
"pool_type"
:
{
"_type"
:
"choice"
,
"_value"
:
[
"max"
,
"min"
,
"avg"
],
},
"学习率"
:
{
"_type"
:
"loguniform"
,
"_value"
:
[
1e-7
,
0.1
],
},
}
test/ut/experiment/assets/ss_yaml12.yaml
0 → 100644
View file @
31f11f51
pool_type
:
_type
:
choice
_value
:
-
max
-
min
-
avg
学习率
:
# test unicode
_type
:
loguniform
_value
:
[
1e-7
,
0.1
]
# test scientific notation
test/ut/experiment/test_search_space.py
0 → 100644
View file @
31f11f51
import
json
from
pathlib
import
Path
import
yaml
from
nni.experiment.config
import
ExperimentConfig
,
AlgorithmConfig
,
LocalConfig
## template ##
config
=
ExperimentConfig
(
search_space_file
=
''
,
trial_command
=
'echo hello'
,
trial_concurrency
=
1
,
tuner
=
AlgorithmConfig
(
name
=
'randomm'
),
training_service
=
LocalConfig
()
)
space_correct
=
{
'pool_type'
:
{
'_type'
:
'choice'
,
'_value'
:
[
'max'
,
'min'
,
'avg'
]
},
'学习率'
:
{
'_type'
:
'loguniform'
,
'_value'
:
[
1e-7
,
0.1
]
}
}
# FIXME
# PyYAML 6.0 (YAML 1.1) does not support tab and scientific notation
# JSON does not support comment and extra comma
# So some combinations will fail to load
formats
=
[
(
'ss_tab.json'
,
'JSON (tabs + scientific notation)'
),
(
'ss_comma.json'
,
'JSON with extra comma'
),
#('ss_tab_comma.json', 'JSON (tabs + scientific notation) with extra comma'),
(
'ss.yaml'
,
'YAML'
),
#('ss_yaml12.yaml', 'YAML 1.2 with scientific notation'),
]
def
test_search_space
():
for
space_file
,
description
in
formats
:
try
:
config
.
search_space_file
=
Path
(
__file__
).
parent
/
'assets'
/
space_file
space
=
config
.
json
()[
'searchSpace'
]
assert
space
==
space_correct
except
Exception
as
e
:
print
(
'Failed to load search space format: '
+
description
)
raise
e
if
__name__
==
'__main__'
:
test_search_space
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment