forked from kubeflow/katib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
201 lines (158 loc) · 7.67 KB
/
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
# Copyright 2022 The Kubeflow Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from logging import getLogger, StreamHandler, INFO
import json
import grpc
from pkg.suggestion.v1beta1.internal.base_health_service import HealthServicer
from pkg.apis.manager.v1beta1.python import api_pb2
from pkg.apis.manager.v1beta1.python import api_pb2_grpc
from pkg.suggestion.v1beta1.nas.common.validation import validate_operations
class DartsService(api_pb2_grpc.SuggestionServicer, HealthServicer):
def __init__(self):
super(DartsService, self).__init__()
self.is_first_run = True
self.logger = getLogger(__name__)
FORMAT = '%(asctime)-15s Experiment %(experiment_name)s %(message)s'
logging.basicConfig(format=FORMAT)
handler = StreamHandler()
handler.setLevel(INFO)
self.logger.setLevel(INFO)
self.logger.addHandler(handler)
self.logger.propagate = False
def ValidateAlgorithmSettings(self, request, context):
is_valid, message = validate_algorithm_spec(request.experiment.spec)
if not is_valid:
context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
context.set_details(message)
self.logger.error(message)
return api_pb2.ValidateAlgorithmSettingsReply()
def GetSuggestions(self, request, context):
if self.is_first_run:
nas_config = request.experiment.spec.nas_config
num_layers = str(nas_config.graph_config.num_layers)
search_space = get_search_space(nas_config.operations)
settings_raw = request.experiment.spec.algorithm.algorithm_settings
algorithm_settings = get_algorithm_settings(settings_raw)
search_space_json = json.dumps(search_space)
algorithm_settings_json = json.dumps(algorithm_settings)
search_space_str = str(search_space_json).replace('\"', '\'')
algorithm_settings_str = str(algorithm_settings_json).replace('\"', '\'')
self.is_first_run = False
parameter_assignments = []
for i in range(request.current_request_number):
self.logger.info(">>> Generate new Darts Trial Job")
self.logger.info(">>> Number of layers {}\n".format(num_layers))
self.logger.info(">>> Search Space")
self.logger.info("{}\n".format(search_space_str))
self.logger.info(">>> Algorithm Settings")
self.logger.info("{}\n\n".format(algorithm_settings_str))
parameter_assignments.append(
api_pb2.GetSuggestionsReply.ParameterAssignments(
assignments=[
api_pb2.ParameterAssignment(
name="algorithm-settings",
value=algorithm_settings_str
),
api_pb2.ParameterAssignment(
name="search-space",
value=search_space_str
),
api_pb2.ParameterAssignment(
name="num-layers",
value=num_layers
)
]
)
)
return api_pb2.GetSuggestionsReply(parameter_assignments=parameter_assignments)
def get_search_space(operations):
search_space = []
for operation in list(operations.operation):
opt_type = operation.operation_type
if opt_type == "skip_connection":
search_space.append(opt_type)
else:
# Currently support only one Categorical parameter - filter size
opt_spec = list(operation.parameter_specs.parameters)[0]
for filter_size in list(opt_spec.feasible_space.list):
search_space.append(opt_type+"_{}x{}".format(filter_size, filter_size))
return search_space
def get_algorithm_settings(settings_raw):
algorithm_settings_default = {
"num_epochs": 50,
"w_lr": 0.025,
"w_lr_min": 0.001,
"w_momentum": 0.9,
"w_weight_decay": 3e-4,
"w_grad_clip": 5.,
"alpha_lr": 3e-4,
"alpha_weight_decay": 1e-3,
"batch_size": 128,
"num_workers": 4,
"init_channels": 16,
"print_step": 50,
"num_nodes": 4,
"stem_multiplier": 3,
}
for setting in settings_raw:
s_name = setting.name
s_value = None if setting.value == "None" else setting.value
algorithm_settings_default[s_name] = s_value
return algorithm_settings_default
def validate_algorithm_spec(spec: api_pb2.ExperimentSpec) -> (bool, str):
# Validate Operations
is_valid, message = validate_operations(spec.nas_config.operations.operation)
if not is_valid:
return False, message
# Validate AlgorithmSettings
is_valid, message = validate_algorithm_settings(spec.algorithm.algorithm_settings)
if not is_valid:
return False, message
return True, ""
# validate_algorithm_settings is implemented based on quark0/darts and pt.darts.
# quark0/darts: https://github.com/quark0/darts
# pt.darts: https://github.com/khanrc/pt.darts
def validate_algorithm_settings(algorithm_settings: list[api_pb2.AlgorithmSetting]) -> (bool, str):
for s in algorithm_settings:
try:
if s.name == "num_epochs":
if not int(s.value) > 0:
return False, "{} should be greater than zero".format(s.name)
# Validate learning rate
if s.name in {"w_lr", "w_lr_min", "alpha_lr"}:
if not float(s.value) >= 0.0:
return False, "{} should be greater than or equal to zero".format(s.name)
# Validate weight decay
if s.name in {"w_weight_decay", "alpha_weight_decay"}:
if not float(s.value) >= 0.0:
return False, "{} should be greater than or equal to zero".format(s.name)
# Validate w_momentum and w_grad_clip
if s.name in {"w_momentum", "w_grad_clip"}:
if not float(s.value) >= 0.0:
return False, "{} should be greater than or equal to zero".format(s.name)
if s.name == "batch_size":
if s.value != "None" and not int(s.value) >= 1:
return False, "batch_size should be greater than or equal to one"
if s.name == "num_workers":
if not int(s.value) >= 0:
return False, "num_workers should be greater than or equal to zero"
# Validate "init_channels", "print_step", "num_nodes" and "stem_multiplier"
if s.name in {"init_channels", "print_step", "num_nodes", "stem_multiplier"}:
if not int(s.value) >= 1:
return False, "{} should be greater than or equal to one".format(s.name)
except Exception as e:
return False, "failed to validate {name}({value}): {exception}".format(name=s.name, value=s.value,
exception=e)
return True, ""