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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add comprehensive tests for tuners (merge into master) (#1681)
* Add comprehensive tests for tuners (#1570)
- Loading branch information
1 parent
358bdb1
commit 1f9b761
Showing
11 changed files
with
410 additions
and
108 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
{ | ||
"choice_str": { | ||
"_type": "choice", | ||
"_value": ["cat", "dog", "elephant", "cow", "sheep", "panda"], | ||
"fail": ["metis", "gp"] | ||
}, | ||
"choice_int": { | ||
"_type": "choice", | ||
"_value": [42, 43, -1] | ||
}, | ||
"choice_mixed": { | ||
"_type": "choice", | ||
"_value": [0.3, "cat", 1, null], | ||
"fail": ["metis", "gp"] | ||
}, | ||
"choice_float": { | ||
"_type": "choice", | ||
"_value": [0.3, 1, 2.0] | ||
}, | ||
"choice_single": { | ||
"_type": "choice", | ||
"_value": [1] | ||
}, | ||
"randint_ok": { | ||
"_type": "randint", | ||
"_value": [-2, 3] | ||
}, | ||
"randint_single": { | ||
"_type": "randint", | ||
"_value": [10, 11] | ||
}, | ||
"randint_fail_equal": { | ||
"_type": "randint", | ||
"_value": [0, 0] | ||
}, | ||
"uniform_ok": { | ||
"_type": "uniform", | ||
"_value": [-1.0, 1.5] | ||
}, | ||
"uniform_equal": { | ||
"_type": "uniform", | ||
"_value": [99.9, 99.9] | ||
}, | ||
"quniform_ok": { | ||
"_type": "quniform", | ||
"_value": [0.0, 10.0, 2.5] | ||
}, | ||
"quniform_clip": { | ||
"_type": "quniform", | ||
"_value": [2.0, 10.0, 5.0] | ||
}, | ||
"quniform_clip_2": { | ||
"_type": "quniform", | ||
"_value": [-5.5, -0.5, 6] | ||
}, | ||
"loguniform_ok": { | ||
"_type": "loguniform", | ||
"_value": [0.001, 100] | ||
}, | ||
"loguniform_equal": { | ||
"_type": "loguniform", | ||
"_value": [1, 1] | ||
}, | ||
"qloguniform_ok": { | ||
"_type": "qloguniform", | ||
"_value": [0.001, 100, 1] | ||
}, | ||
"qloguniform_equal": { | ||
"_type": "qloguniform", | ||
"_value": [2, 2, 1] | ||
}, | ||
"normal_ok": { | ||
"_type": "normal", | ||
"_value": [-1.0, 5.0] | ||
}, | ||
"qnormal_ok": { | ||
"_type": "qnormal", | ||
"_value": [-1.5, 5.0, 0.1] | ||
}, | ||
"lognormal_ok": { | ||
"_type": "lognormal", | ||
"_value": [-1.0, 5.0] | ||
}, | ||
"qlognormal_ok": { | ||
"_type": "qlognormal", | ||
"_value": [-1.5, 5.0, 0.1] | ||
} | ||
} |
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,125 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# | ||
# MIT License | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
# associated documentation files (the "Software"), to deal in the Software without restriction, | ||
# including without limitation the rights to use, copy, modify, merge, publish, distribute, | ||
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all copies or | ||
# substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT | ||
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT | ||
# OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
# ================================================================================================== | ||
|
||
|
||
import json | ||
from io import BytesIO | ||
from unittest import TestCase, main | ||
|
||
import nni.protocol | ||
from nni.msg_dispatcher import MsgDispatcher | ||
from nni.protocol import CommandType, send, receive | ||
from nni.tuner import Tuner | ||
from nni.utils import extract_scalar_reward | ||
|
||
|
||
class NaiveTuner(Tuner): | ||
def __init__(self): | ||
self.param = 0 | ||
self.trial_results = [] | ||
self.search_space = None | ||
self._accept_customized_trials() | ||
|
||
def generate_parameters(self, parameter_id, **kwargs): | ||
# report Tuner's internal states to generated parameters, | ||
# so we don't need to pause the main loop | ||
self.param += 2 | ||
return { | ||
'param': self.param, | ||
'trial_results': self.trial_results, | ||
'search_space': self.search_space | ||
} | ||
|
||
def receive_trial_result(self, parameter_id, parameters, value, **kwargs): | ||
reward = extract_scalar_reward(value) | ||
self.trial_results.append((parameter_id, parameters['param'], reward, kwargs.get("customized"))) | ||
|
||
def update_search_space(self, search_space): | ||
self.search_space = search_space | ||
|
||
|
||
_in_buf = BytesIO() | ||
_out_buf = BytesIO() | ||
|
||
|
||
def _reverse_io(): | ||
_in_buf.seek(0) | ||
_out_buf.seek(0) | ||
nni.protocol._out_file = _in_buf | ||
nni.protocol._in_file = _out_buf | ||
|
||
|
||
def _restore_io(): | ||
_in_buf.seek(0) | ||
_out_buf.seek(0) | ||
nni.protocol._in_file = _in_buf | ||
nni.protocol._out_file = _out_buf | ||
|
||
|
||
class MsgDispatcherTestCase(TestCase): | ||
def test_msg_dispatcher(self): | ||
_reverse_io() # now we are sending to Tuner's incoming stream | ||
send(CommandType.RequestTrialJobs, '2') | ||
send(CommandType.ReportMetricData, '{"parameter_id":0,"type":"PERIODICAL","value":10}') | ||
send(CommandType.ReportMetricData, '{"parameter_id":1,"type":"FINAL","value":11}') | ||
send(CommandType.UpdateSearchSpace, '{"name":"SS0"}') | ||
send(CommandType.AddCustomizedTrialJob, '{"param":-1}') | ||
send(CommandType.ReportMetricData, '{"parameter_id":2,"type":"FINAL","value":22}') | ||
send(CommandType.RequestTrialJobs, '1') | ||
send(CommandType.KillTrialJob, 'null') | ||
_restore_io() | ||
|
||
tuner = NaiveTuner() | ||
dispatcher = MsgDispatcher(tuner) | ||
nni.msg_dispatcher_base._worker_fast_exit_on_terminate = False | ||
|
||
dispatcher.run() | ||
e = dispatcher.worker_exceptions[0] | ||
self.assertIs(type(e), AssertionError) | ||
self.assertEqual(e.args[0], 'Unsupported command: CommandType.KillTrialJob') | ||
|
||
_reverse_io() # now we are receiving from Tuner's outgoing stream | ||
self._assert_params(0, 2, [], None) | ||
self._assert_params(1, 4, [], None) | ||
|
||
command, data = receive() # this one is customized | ||
data = json.loads(data) | ||
self.assertIs(command, CommandType.NewTrialJob) | ||
self.assertEqual(data['parameter_id'], 2) | ||
self.assertEqual(data['parameter_source'], 'customized') | ||
self.assertEqual(data['parameters'], {'param': -1}) | ||
|
||
self._assert_params(3, 6, [[1, 4, 11, False], [2, -1, 22, True]], {'name': 'SS0'}) | ||
|
||
self.assertEqual(len(_out_buf.read()), 0) # no more commands | ||
|
||
def _assert_params(self, parameter_id, param, trial_results, search_space): | ||
command, data = receive() | ||
self.assertIs(command, CommandType.NewTrialJob) | ||
data = json.loads(data) | ||
self.assertEqual(data['parameter_id'], parameter_id) | ||
self.assertEqual(data['parameter_source'], 'algorithm') | ||
self.assertEqual(data['parameters']['param'], param) | ||
self.assertEqual(data['parameters']['trial_results'], trial_results) | ||
self.assertEqual(data['parameters']['search_space'], search_space) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.