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
/
convert.py
301 lines (254 loc) · 12.4 KB
/
convert.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
import copy
import logging
_logger = logging.getLogger(__name__)
def to_v2(v1):
v1 = copy.deepcopy(v1)
platform = v1.pop('trainingServicePlatform')
assert platform in ['local', 'remote', 'pai', 'aml', 'kubeflow', 'frameworkcontroller']
if platform == 'pai':
platform = 'openpai'
v2 = {}
_drop_field(v1, 'authorName')
_move_field(v1, v2, 'experimentName')
_drop_field(v1, 'description')
_move_field(v1, v2, 'trialConcurrency')
_move_field(v1, v2, 'maxExecDuration', 'maxExperimentDuration')
_move_field(v1, v2, 'maxTrialNum', 'maxTrialNumber')
_move_field(v1, v2, 'searchSpacePath', 'searchSpaceFile')
assert not v1.pop('multiPhase', None), 'Multi-phase is no longer supported'
_deprecate(v1, v2, 'multiThread')
_move_field(v1, v2, 'nniManagerIp')
_move_field(v1, v2, 'logDir', 'experimentWorkingDirectory')
_move_field(v1, v2, 'debug')
_deprecate(v1, v2, 'versionCheck')
_move_field(v1, v2, 'logLevel')
_deprecate(v1, v2, 'logCollection')
_move_field(v1, v2, 'useAnnotation')
if 'trial' in v1:
v1_trial = v1.pop('trial')
_move_field(v1_trial, v2, 'command', 'trialCommand')
_move_field(v1_trial, v2, 'codeDir', 'trialCodeDirectory')
_move_field(v1_trial, v2, 'gpuNum', 'trialGpuNumber')
else:
v1_trial = {}
for algo_type in ['tuner', 'assessor', 'advisor']:
v1_algo = v1.pop(algo_type, None)
if not v1_algo:
continue
builtin_name = v1_algo.pop(f'builtin{algo_type.title()}Name', None)
if builtin_name is not None:
v2_algo = {'name': builtin_name}
else:
code_directory = v1_algo.pop('codeDir')
class_file_name = v1_algo.pop('classFileName')
assert class_file_name.endswith('.py')
class_name = class_file_name[:-3] + '.' + v1_algo.pop('className')
v2_algo = {'className': class_name, 'codeDirectory': code_directory}
if 'classArgs' in v1_algo:
v2_algo['classArgs'] = v1_algo.pop('classArgs')
v2[algo_type] = v2_algo
_deprecate(v1_algo, v2, 'includeIntermediateResults')
_move_field(v1_algo, v2, 'gpuIndices', 'tunerGpuIndices')
if v1_algo:
_logger.error('%s config not fully converted: %s', algo_type, v1_algo)
ts = {'platform': platform}
v2['trainingService'] = ts
if platform == 'local':
local_config = v1.pop('localConfig', {})
_move_field(local_config, ts, 'gpuIndices')
_move_field(local_config, ts, 'maxTrialNumPerGpu', 'maxTrialNumberPerGpu')
_move_field(local_config, ts, 'useActiveGpu')
if local_config:
_logger.error('localConfig not fully converted: %s', local_config)
if platform == 'remote':
remote_config = v1.pop('remoteConfig', {})
_move_field(remote_config, ts, 'reuse', 'reuseMode')
if remote_config:
_logger.error('remoteConfig not fully converted: %s', remote_config)
ts['machineList'] = []
for v1_machine in v1.pop('machineList'):
v2_machine = {}
ts['machineList'].append(v2_machine)
_move_field(v1_machine, v2_machine, 'ip', 'host')
_move_field(v1_machine, v2_machine, 'port')
_move_field(v1_machine, v2_machine, 'username', 'user')
_move_field(v1_machine, v2_machine, 'sshKeyPath', 'sshKeyFile')
_move_field(v1_machine, v2_machine, 'passphrase')
_move_field(v1_machine, v2_machine, 'gpuIndices')
_move_field(v1_machine, v2_machine, 'maxTrialNumPerGpu', 'maxTrialNumberPerGpu')
_move_field(v1_machine, v2_machine, 'useActiveGpu')
_move_field(v1_machine, v2_machine, 'pythonPath')
_move_field(v1_machine, v2_machine, 'passwd', 'password')
if v1_machine:
_logger.error('remote machine not fully converted: %s', v1_machine)
if platform == 'openpai':
_move_field(v1_trial, ts, 'nniManagerNFSMountPath', 'localStorageMountPoint')
_move_field(v1_trial, ts, 'containerNFSMountPath', 'containerStorageMountPoint')
_move_field(v1_trial, ts, 'cpuNum', 'trialCpuNumber')
_move_field(v1_trial, ts, 'memoryMB', 'trialMemorySize')
_move_field(v1_trial, ts, 'image', 'dockerImage')
_move_field(v1_trial, ts, 'virtualCluster')
_move_field(v1_trial, ts, 'paiStorageConfigName', 'storageConfigName')
_move_field(v1_trial, ts, 'paiConfigPath', 'openpaiConfigFile')
pai_config = v1.pop('paiConfig')
_move_field(pai_config, ts, 'userName', 'username')
_deprecate(pai_config, v2, 'password')
_move_field(pai_config, ts, 'token')
_move_field(pai_config, ts, 'host')
_move_field(pai_config, ts, 'reuse', 'reuseMode')
_move_field(pai_config, ts, 'gpuNum', 'trialGpuNumber')
_move_field(pai_config, ts, 'cpuNum', 'trialCpuNumber')
_move_field(pai_config, ts, 'memoryMB', 'trialMemorySize')
_deprecate(pai_config, v2, 'maxTrialNumPerGpu')
_deprecate(pai_config, v2, 'useActiveGpu')
if pai_config:
_logger.error('paiConfig not fully converted: %s', pai_config)
if platform == 'aml':
_move_field(v1_trial, ts, 'image', 'dockerImage')
aml_config = v1.pop('amlConfig', {})
_move_field(aml_config, ts, 'subscriptionId')
_move_field(aml_config, ts, 'resourceGroup')
_move_field(aml_config, ts, 'workspaceName')
_move_field(aml_config, ts, 'computeTarget')
_move_field(aml_config, ts, 'maxTrialNumPerGpu', 'maxTrialNumberPerGpu')
_deprecate(aml_config, v2, 'useActiveGpu')
if aml_config:
_logger.error('amlConfig not fully converted: %s', aml_config)
if platform == 'kubeflow':
kf_config = v1.pop('kubeflowConfig')
_move_field(kf_config, ts, 'operator')
_move_field(kf_config, ts, 'apiVersion')
storage_name = kf_config.pop('storage', None)
if storage_name is None:
storage_name = 'nfs' if 'nfs' in kf_config else 'azureStorage'
if storage_name == 'nfs':
nfs = kf_config.pop('nfs')
ts['storage'] = {'storageType': 'nfs', 'server': nfs['server'], 'path': nfs['path']}
if storage_name == 'azureStorage':
key_vault = kf_config.pop('keyVault')
azure_storage = kf_config.pop('azureStorage')
ts['storage'] = {
'storageType': 'azureStorage',
'azureAccount': azure_storage['accountName'],
'azureShare': azure_storage['azureShare'],
'keyVaultName': key_vault['vaultName'],
'keyVaultKey': key_vault['name'],
}
_deprecate(kf_config, v2, 'uploadRetryCount')
if kf_config:
_logger.error('kubeflowConfig not fully converted: %s', kf_config)
_drop_field(v1_trial, 'nasMode')
for role_name in ['worker', 'ps', 'master']:
if role_name not in v1_trial:
continue
v1_role = v1_trial.pop(role_name)
v2_role = {}
ts[role_name] = v2_role
_move_field(v1_role, v2_role, 'replicas')
_move_field(v1_role, v2_role, 'command')
_move_field(v1_role, v2_role, 'gpuNum', 'gpuNumber')
_move_field(v1_role, v2_role, 'cpuNum', 'cpuNumber')
_move_field(v1_role, v2_role, 'memoryMB', 'memorySize')
_move_field(v1_role, v2_role, 'image', 'dockerImage')
_deprecate(v1_role, v2, 'privateRegistryAuthPath')
v2_role['codeDirectory'] = v2['trialCodeDirectory']
if v1_role:
_logger.error('kubeflow role not fully converted: %s', v1_role)
if platform == 'frameworkcontroller':
fc_config = v1.pop('frameworkcontrollerConfig')
_move_field(fc_config, ts, 'serviceAccountName')
_move_field(fc_config, ts, 'reuse', 'reuseMode')
storage_name = fc_config.pop('storage', None)
if storage_name is None:
storage_name = 'nfs' if 'nfs' in fc_config else 'azureStorage'
if storage_name == 'nfs':
nfs = fc_config.pop('nfs')
ts['storage'] = {'storageType': 'nfs', 'server': nfs['server'], 'path': nfs['path']}
if storage_name == 'azureStorage':
key_vault = fc_config.pop('keyVault')
azure_storage = fc_config.pop('azureStorage')
ts['storage'] = {
'storageType': 'azureStorage',
'azureAccount': azure_storage['accountName'],
'azureShare': azure_storage['azureShare'],
'keyVaultName': key_vault['vaultName'],
'keyVaultKey': key_vault['name'],
}
_deprecate(fc_config, v2, 'uploadRetryCount')
if fc_config:
_logger.error('frameworkcontroller not fully converted: %s', fc_config)
_drop_field(v1_trial, 'nasMode')
ts['taskRoles'] = []
for v1_role in v1_trial.pop('taskRoles', []):
v2_role = {}
ts['taskRoles'].append(v2_role)
_move_field(v1_role, v2_role, 'name')
_move_field(v1_role, v2_role, 'taskNum', 'taskNumber')
_move_field(v1_role, v2_role, 'frameworkControllerCompletionPolicy', 'frameworkAttemptCompletionPolicy')
_move_field(v1_role, v2_role, 'command')
_move_field(v1_role, v2_role, 'gpuNum', 'gpuNumber')
_move_field(v1_role, v2_role, 'cpuNum', 'cpuNumber')
_move_field(v1_role, v2_role, 'memoryMB', 'memorySize')
_move_field(v1_role, v2_role, 'image', 'dockerImage')
_deprecate(v1_role, v2, 'privateRegistryAuthPath')
policy = 'frameworkAttemptCompletionPolicy'
if v1_role[policy]:
v2_role[policy] = {}
_move_field(v1_role[policy], v2_role[policy], 'minFailedTaskCount')
_move_field(v1_role[policy], v2_role[policy], 'minSucceededTaskCount', 'minSucceedTaskCount')
if not v1_role[policy]:
v1_role.pop(policy)
if v1_role:
_logger.error('frameworkcontroller role not fully converted: %s', v1_role)
# this is required, seems a bug in nni manager
if not v2.get('trialCommand'):
v2['trialCommand'] = v2_role['command']
# hybrid mode should always use v2 schema, so no need to handle here
v1_storage = v1.pop('sharedStorage', None)
if v1_storage:
v2_storage = {}
v2['sharedStorage'] = v2_storage
_move_field(v1_storage, v2_storage, 'storageType')
_move_field(v1_storage, v2_storage, 'nfsServer')
_move_field(v1_storage, v2_storage, 'exportedDirectory')
_move_field(v1_storage, v2_storage, 'localMountPoint')
_move_field(v1_storage, v2_storage, 'remoteMountPoint')
_move_field(v1_storage, v2_storage, 'localMounted')
_move_field(v1_storage, v2_storage, 'storageAccountName')
_move_field(v1_storage, v2_storage, 'storageAccountKey')
_move_field(v1_storage, v2_storage, 'containerName')
if v1_storage:
_logger.error('shared storage not fully converted: %s', v1_storage)
if v1_trial:
_logger.error('trial config not fully converted: %s', v1_trial)
if v1:
_logger.error('Config not fully converted: %s', v1)
return v2
def _move_field(v1, v2, v1_key, v2_key=None):
if v2_key is None:
v2_key = v1_key
if v1_key in v1:
value = v1.pop(v1_key, None)
if value is not None:
v2[v2_key] = value
def _drop_field(v1, key):
if key in v1:
_logger.warning(f'Config field "{key}" is no longer supported and has been ignored')
v1.pop(key)
def _deprecate(v1, v2, key):
_drop_field(v1, key)
def convert_algo(algo_type, v1_algo):
builtin_name = v1_algo.pop(f'builtin{algo_type.title()}Name', None)
if builtin_name is not None:
v2_algo = {'name': builtin_name}
else:
code_directory = v1_algo.pop('codeDir')
class_file_name = v1_algo.pop('classFileName')
assert class_file_name.endswith('.py')
class_name = class_file_name[:-3] + '.' + v1_algo.pop('className')
v2_algo = {'className': class_name, 'codeDirectory': code_directory}
if 'classArgs' in v1_algo:
v2_algo['classArgs'] = v1_algo.pop('classArgs')
return v2_algo