-
Notifications
You must be signed in to change notification settings - Fork 438
/
hyperband_service.py
341 lines (308 loc) · 13.8 KB
/
hyperband_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import math
import traceback
import grpc
import logging
from logging import getLogger, StreamHandler, DEBUG
from pkg.apis.manager.v1alpha3.python import api_pb2
from pkg.apis.manager.v1alpha3.python import api_pb2_grpc
from pkg.suggestion.v1alpha3 import parsing_util
from pkg.suggestion.v1alpha3.base_health_service import HealthServicer
logger = getLogger(__name__)
FORMAT = '%(asctime)-15s Experiment %(experiment_name)s %(message)s'
logging.basicConfig(format=FORMAT)
handler = StreamHandler()
handler.setLevel(DEBUG)
logger.setLevel(DEBUG)
logger.addHandler(handler)
class HyperbandService(api_pb2_grpc.SuggestionServicer, HealthServicer):
def GetSuggestions(self, request, context):
"""
Main function to provide suggestion.
"""
try:
reply = api_pb2.GetSuggestionsReply()
experiment = request.experiment
self.all_trials = request.trials
alg_settings = experiment.spec.algorithm.algorithm_setting
param = HyperBandParam.convert(alg_settings)
if param.current_s < 0:
# Hyperband outlerloop has finished
return reply
# This is a hack to get request number.
param.n = request.request_number
trials = self._make_bracket(experiment, param)
for trial in trials:
reply.parameter_assignments.add(assignments=trial.parameter_assignments.assignments)
reply.algorithm.CopyFrom(HyperBandParam.generate(param))
return reply
except Exception as e:
logger.error("Fail to generate trials: \n%s",
traceback.format_exc(), extra={"experiment_name": experiment.name})
raise e
def _update_hbParameters(self, param):
param.current_i += 1
if param.current_i > param.current_s:
self._new_hbParameters(param)
def _new_hbParameters(self, param):
param.current_s -= 1
param.current_i = 0
if param.current_s >= 0:
# when param.current_s < 0, hyperband algorithm reaches the end
param.n = int(math.ceil(float(param.s_max + 1) * (
float(param.eta**param.current_s) / float(param.current_s+1))))
param.r = param.r_l * \
param.eta**(-param.current_s)
def _make_bracket(self, experiment, param):
if param.evaluating_trials == 0:
trialSpecs = self._make_master_bracket(experiment, param)
else:
trialSpecs = self._make_child_bracket(experiment, param)
if param.current_i < param.current_s:
param.evaluating_trials = len(trialSpecs)
else:
param.evaluating_trials = 0
logger.info("HyperBand Param eta %d.",
param.eta, extra={"experiment_name": experiment.name})
logger.info("HyperBand Param R %d.",
param.r_l, extra={"experiment_name": experiment.name})
logger.info("HyperBand Param sMax %d.",
param.s_max, extra={"experiment_name": experiment.name})
logger.info("HyperBand Param B %d.",
param.b_l, extra={"experiment_name": experiment.name})
logger.info("HyperBand Param n %d.",
param.n, extra={"experiment_name": experiment.name})
logger.info("HyperBand Param r %d.",
param.r, extra={"experiment_name": experiment.name})
logger.info("HyperBand Param s %d.",
param.current_s, extra={"experiment_name": experiment.name})
logger.info("HyperBand Param i %d.",
param.current_i, extra={"experiment_name": experiment.name})
logger.info("HyperBand evaluating trials count %d.",
param.evaluating_trials, extra={"experiment_name": experiment.name})
logger.info("HyperBand budget resource name %s.",
param.resource_name, extra={"experiment_name": experiment.name})
if param.evaluating_trials == 0:
self._new_hbParameters(param)
return trialSpecs
def _make_child_bracket(self, experiment, param):
n_i = math.ceil(param.n * param.eta**(-param.current_i))
top_trials_num = int(math.ceil(n_i / param.eta))
self._update_hbParameters(param)
r_i = int(param.r * param.eta**param.current_i)
last_trials = self._get_top_trial(
param.evaluating_trials, top_trials_num, experiment)
trialSpecs = self._copy_trials(
last_trials, r_i, param.resource_name)
logger.info("Generate %d trials by child bracket.",
top_trials_num, extra={"experiment_name": experiment.name})
return trialSpecs
def _get_last_trials(self, all_trials, latest_trials_num):
sorted_trials = sorted(
all_trials, key=lambda trial: trial.status.start_time)
if len(sorted_trials) > latest_trials_num:
return sorted_trials[-latest_trials_num:]
else:
return sorted_trials
def _get_top_trial(self, latest_trials_num, top_trials_num, experiment):
objective_metric = experiment.spec.objective.objective_metric_name
objective_type = experiment.spec.objective.type
def get_objective_value(t):
for m in t.status.observation.metrics:
if m.name == objective_metric:
return float(m.value)
top_trials = []
all_trials = self.all_trials
latest_trials = self._get_last_trials(all_trials, latest_trials_num)
for t in latest_trials:
if t.status.condition != api_pb2.TrialStatus.TrialConditionType.SUCCEEDED:
raise Exception(
"There are some trials which are not completed yet for experiment %s." % experiment.name)
if objective_type == api_pb2.MAXIMIZE:
top_trials.extend(
sorted(latest_trials, key=get_objective_value, reverse=True))
else:
top_trials.extend(sorted(latest_trials, key=get_objective_value))
return top_trials[:top_trials_num]
def _copy_trials(self, trials, r_i, resourceName):
trialSpecs = []
for t in trials:
trial_spec = api_pb2.TrialSpec()
for assignment in t.spec.parameter_assignments.assignments:
if assignment.name == resourceName:
value = str(r_i)
else:
value = assignment.value
trial_spec.parameter_assignments.assignments.add(name=assignment.name,
value=value)
trialSpecs.append(trial_spec)
return trialSpecs
def _make_master_bracket(self, experiment, param):
n = param.n
r = int(param.r)
parameter_config = parsing_util.parse_parameter_configs(
experiment.spec.parameter_specs.parameters)
trial_specs = []
for _ in range(n):
sample = parameter_config.random_sample()
suggestion = parsing_util.parse_x_next_vector(
sample,
parameter_config.parameter_types,
parameter_config.names,
parameter_config.discrete_info,
parameter_config.categorical_info)
trial_spec = api_pb2.TrialSpec()
trial_spec.experiment_name = experiment.name
for hp in suggestion:
if hp['name'] == param.resource_name:
hp['value'] = str(r)
trial_spec.parameter_assignments.assignments.add(name=hp['name'],
value=str(hp['value']))
trial_specs.append(trial_spec)
logger.info("Generate %d trials by master bracket.",
n, extra={"experiment_name": experiment.name})
return trial_specs
def _set_validate_context_error(self, context, error_message):
context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
context.set_details(error_message)
logger.info(error_message)
return api_pb2.ValidateAlgorithmSettingsReply()
def ValidateAlgorithmSettings(self, request, context):
params = request.experiment.spec.parameter_specs.parameters
settings = request.experiment.spec.algorithm.algorithm_setting
setting_dict = {}
for setting in settings:
setting_dict[setting.name] = setting.value
if "r_l" not in setting_dict or "resource_name" not in setting_dict:
return self._set_validate_context_error(context, "r_l and resource_name must be set.")
try:
rl = float(setting_dict["r_l"])
except:
return self._set_validate_context_error(context, "r_l must be a positive float number.")
else:
if rl < 0:
return self._set_validate_context_error(context, "r_l must be a positive float number.")
if "eta" in setting_dict:
eta = int(float(setting_dict["eta"]))
if eta <= 0:
eta = 3
else:
eta = 3
smax = int(math.log(rl)/math.log(eta))
max_parallel = int(math.ceil(eta**smax))
if request.experiment.spec.parallel_trial_count < max_parallel:
return self._set_validate_context_error(context,
"parallelTrialCount must be not less than %d." % max_parallel)
valid_resourceName = False
for param in params:
if param.name == setting_dict["resource_name"]:
valid_resourceName = True
break
if not valid_resourceName:
return self._set_validate_context_error(context,
"value of resource_name setting must be in parameters.")
return api_pb2.ValidateAlgorithmSettingsReply()
class HyperBandParam(object):
def __init__(self, eta=3, s_max=-1, r_l=-1,
b_l=-1, r=-1, n=-1, current_s=-2,
current_i=-1, resource_name="",
evaluating_trials=0):
self.eta = eta
self.s_max = s_max
self.r_l = r_l
self.b_l = b_l
self.r = r
self.n = n
self.current_s = current_s
self.current_i = current_i
self.resource_name = resource_name
self.evaluating_trials = evaluating_trials
@staticmethod
def generate(param):
algorithm_settings = [
api_pb2.AlgorithmSetting(
name="eta",
value=str(param.eta)
), api_pb2.AlgorithmSetting(
name="s_max",
value=str(param.s_max)
), api_pb2.AlgorithmSetting(
name="r_l",
value=str(param.r_l)
), api_pb2.AlgorithmSetting(
name="b_l",
value=str(param.b_l)
), api_pb2.AlgorithmSetting(
name="r",
value=str(param.r)
), api_pb2.AlgorithmSetting(
name="n",
value=str(param.n)
), api_pb2.AlgorithmSetting(
name="current_s",
value=str(param.current_s)
), api_pb2.AlgorithmSetting(
name="current_i",
value=str(param.current_i)
), api_pb2.AlgorithmSetting(
name="resource_name",
value=param.resource_name
), api_pb2.AlgorithmSetting(
name="evaluating_trials",
value=str(param.evaluating_trials)
)]
return api_pb2.AlgorithmSpec(
algorithm_setting=algorithm_settings
)
@staticmethod
def convert(alg_settings):
"""Convert the algorithm settings to HyperBandParam.
"""
param = HyperBandParam()
# Set the param from the algorithm settings.
for setting in alg_settings:
if setting.name == "eta":
param.eta = float(setting.value)
elif setting.name == "r_l":
param.r_l = float(setting.value)
elif setting.name == "b_l":
param.b_l = float(setting.value)
elif setting.name == "n":
param.n = int(float(setting.value))
elif setting.name == "r":
param.r = int(float(setting.value))
elif setting.name == "current_s":
param.current_s = int(float(setting.value))
elif setting.name == "current_i":
param.current_i = int(float(setting.value))
elif setting.name == "s_max":
param.s_max = int(float(setting.value))
elif setting.name == "evaluating_trials":
param.evaluating_trials = int(float(setting.value))
elif setting.name == "resource_name":
param.resource_name = setting.value
else:
logger.info(
"Unknown HyperBand Param %s, ignore it", setting.name)
if param.current_s == -1:
# Hyperband outlerloop has finished
logger.info("HyperBand outlerloop has finished.")
return param
# Deal with illegal parameter values.
if param.eta <= 0:
param.eta = 3
if param.s_max < 0:
param.s_max = int(
math.log(param.r_l) / math.log(param.eta))
if param.b_l < 0:
param.b_l = (param.s_max + 1) * param.r_l
if param.current_s < 0:
param.current_s = param.s_max
if param.current_i < 0:
param.current_i = 0
if param.n < 0:
param.n = int(math.ceil(float(param.s_max + 1) * (
float(param.eta**param.current_s) / float(param.current_s+1))))
if param.r < 0:
param.r = param.r_l * \
param.eta**(-param.current_s)
return param