Skip to content
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

[Vote] Need a SimpleTuner interface like this proposal? #2151

Open
ultmaster opened this issue Mar 15, 2020 · 0 comments
Open

[Vote] Need a SimpleTuner interface like this proposal? #2151

ultmaster opened this issue Mar 15, 2020 · 0 comments

Comments

@ultmaster
Copy link
Contributor

ultmaster commented Mar 15, 2020

In some situations (at least in evolution, I don't know about others), what tuner does is "send parameters based on previously received final results".

Currently, the interface of tuners involves a lot of details about resource management and trial status awareness. This interface can be troublesome in case they want to implement a simple tuner as stated above.

Proposed here, is a SimpleTuner interface, that has two functions in all:

class ExampleUsesSimpleTuner(SimpleTuner):
    def update_search_space(self, search_space):
        self.send_parameter(my_initial_parameters)

    def on_trial_finish(self, parameter, metrics):
        for parameter in new_generated_params:
            self.send_parameter(parameter)

And the implementation is several queues that handle all the unwanted details.

class SimpleTuner(Tuner):
    def __init__(self):
        self._sending_parameter_queue = deque()
        self._to_evaluate_queue = deque()
        self._pending_result_ids = set()
        self.parameter_history = dict()
        self.reward_history = dict()
        self._st_callback = None

    def _check_and_send_unsent(self):
        if self._st_callback is None:
            _logger.info("st_callback is None. Parameter not sent. Try again later.")
            return
        while self._sending_parameter_queue and self._to_evaluate_queue:
            parameter_id = self._sending_parameter_queue.popleft()
            parameter = self._to_evaluate_queue.popleft()
            self.parameter_history[parameter_id] = parameter
            self._pending_result_ids.add(parameter_id)
            self._st_callback(parameter_id, parameter)
            _logger.info("Sending parameter #%d: %s", parameter_id, parameter)

    def generate_multiple_parameters(self, parameter_id_list, **kwargs):
        if "st_callback" in kwargs and self._st_callback is None:
            self._st_callback = kwargs["st_callback"]
        for parameter_id in parameter_id_list:
            self._sending_parameter_queue.append(parameter_id)
        self._check_and_send_unsent()
        return []

    def receive_trial_result(self, parameter_id, parameters, value, **kwargs):
        self.reward_history[parameter_id] = value

    def trial_end(self, parameter_id, success, **kwargs):
        self._pending_result_ids.remove(parameter_id)
        metrics = self.reward_history.get(parameter_id)
        _logger.info("Parameter %d finish. Metrics: %s", parameter_id, metrics)
        self.on_trial_finish(self.parameter_history[parameter_id], metrics)

    def send_parameter(self, parameter):
        self._to_evaluate_queue.append(parameter)
        self._check_and_send_unsent()

    def on_trial_finish(self, parameter, metrics):
        # if trial doesn't work out, metrics is None
        pass

I'm not sure whether this should be included in the SDK. So I'm posting it in issues for users who are looking for similar features.

@scarlett2018 scarlett2018 changed the title A SimpleTuner interface 【Vote】 Need a SimpleTuner interface like this proposal? Mar 15, 2020
@scarlett2018 scarlett2018 pinned this issue Mar 15, 2020
@ultmaster ultmaster changed the title 【Vote】 Need a SimpleTuner interface like this proposal? [Vote] Need a SimpleTuner interface like this proposal? Mar 16, 2020
@scarlett2018 scarlett2018 unpinned this issue Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants