forked from FederatedAI/FATE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_run.py
379 lines (312 loc) · 11.8 KB
/
quick_run.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2019 The FATE Authors. All Rights Reserved.
#
# 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 argparse
import json
import os
import random
import subprocess
import sys
import time
import traceback
HOME_DIR = os.path.split(os.path.realpath(__file__))[0]
CONFIG_DIR = '/'.join([HOME_DIR, 'config'])
FATE_FLOW_PATH = HOME_DIR + "/../../fate_flow/fate_flow_client.py"
UPLOAD_PATH = HOME_DIR + "/upload_data.json"
GUEST = 'guest'
HOST = 'host'
# You can set up your own configuration files here
DSL_PATH = 'hetero_logistic_regression/test_hetero_lr_train_job_dsl.json'
SUBMIT_CONF_PATH = 'hetero_logistic_regression/test_hetero_lr_train_job_conf.json'
TEST_PREDICT_CONF = HOME_DIR + '/test_predict_conf.json'
# Define what type of task it is
TASK = 'train'
# TASK = 'predict'
# Put your data to /examples/data folder and indicate the data names here
GUEST_DATA_SET = 'breast_b.csv'
HOST_DATA_SET = 'breast_a.csv'
# GUEST_DATA_SET = 'breast_homo_guest.csv'
# HOST_DATA_SET = 'breast_homo_host.csv'
# Define your party ids here
GUEST_ID = 10000
HOST_ID = 10000
ARBITER_ID = 10000
# 0 represent for standalone version while 1 represent for cluster version
WORK_MODE = 0
# Time out for waiting a task
MAX_WAIT_TIME = 3600
# Time interval for querying task status
RETRY_JOB_STATUS_TIME = 10
# Your task status list
SUCCESS = 'success'
RUNNING = 'running'
FAIL = 'failed'
# Your latest trained model info is stored here and this will be used when starting a predict task
LATEST_TRAINED_RESULT = '/'.join([HOME_DIR, 'user_config', 'train_info.json'])
def get_timeid():
return str(int(time.time())) + "_" + str(random.randint(1000, 9999))
def gen_unique_path(prefix):
return HOME_DIR + "/user_config/" + prefix + ".config_" + get_timeid()
def prettify(response, verbose=True):
if verbose:
print(json.dumps(response, indent=4))
print()
return response
def save_config_file(config_dict, prefix):
config = json.dumps(config_dict)
config_path = gen_unique_path(prefix)
config_dir_path = os.path.dirname(config_path)
os.makedirs(config_dir_path, exist_ok=True)
with open(config_path, "w") as fout:
# print("path:{}".format(config_path))
fout.write(config + "\n")
return config_path
def exec_upload_task(config_dict, role):
prefix = '_'.join(['upload', role])
config_path = save_config_file(config_dict=config_dict, prefix=prefix)
subp = subprocess.Popen(["python",
FATE_FLOW_PATH,
"-f",
"upload",
"-c",
config_path],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, stderr = subp.communicate()
stdout = stdout.decode("utf-8")
print("stdout:" + str(stdout))
stdout = json.loads(stdout)
status = stdout["retcode"]
if status != 0:
raise ValueError(
"[Upload task]exec fail, status:{}, stdout:{}".format(status, stdout))
return stdout
def exec_modeling_task(dsl_dict, config_dict):
dsl_path = save_config_file(dsl_dict, 'train_dsl')
conf_path = save_config_file(config_dict, 'train_conf')
print("dsl_path: {}, conf_path: {}".format(dsl_path, conf_path))
subp = subprocess.Popen(["python",
FATE_FLOW_PATH,
"-f",
"submit_job",
"-c",
conf_path,
"-d",
dsl_path
],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, stderr = subp.communicate()
stdout = stdout.decode("utf-8")
print("stdout:" + str(stdout))
stdout = json.loads(stdout)
with open(LATEST_TRAINED_RESULT, 'w') as outfile:
json.dump(stdout, outfile)
status = stdout["retcode"]
if status != 0:
raise ValueError(
"[Trainning Task]exec fail, status:{}, stdout:{}".format(status, stdout))
return stdout
def job_status_checker(jobid):
# check_counter = 0
# while True:
subp = subprocess.Popen(["python",
FATE_FLOW_PATH,
"-f",
"query_job",
"-j",
jobid
],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, stderr = subp.communicate()
stdout = stdout.decode("utf-8")
stdout = json.loads(stdout)
status = stdout["retcode"]
if status != 0:
return RUNNING
check_data = stdout["data"]
task_status = []
for component_stats in check_data:
status = component_stats['f_status']
task_status.append(status)
if any([s == FAIL for s in task_status]):
return FAIL
if any([s == RUNNING for s in task_status]):
return RUNNING
return SUCCESS
def wait_query_job(jobid):
start = time.time()
while True:
job_status = job_status_checker(jobid)
if job_status == SUCCESS:
print("Task Finished")
break
elif job_status == FAIL:
print("Task Failed")
break
else:
time.sleep(RETRY_JOB_STATUS_TIME)
end = time.time()
print("Task is running, wait time: {}".format(end - start))
if end - start > MAX_WAIT_TIME:
print("Task Failed, may by stuck in federation")
break
def generate_data_info(role):
if role == GUEST:
data_name = GUEST_DATA_SET
else:
data_name = HOST_DATA_SET
if '.' in data_name:
table_name_list = data_name.split('.')
table_name = '_'.join(table_name_list[:-1])
else:
table_name = data_name
table_name_space = '{}_{}'.format(table_name, role)
return table_name, table_name_space
def upload(role):
with open(UPLOAD_PATH, 'r', encoding='utf-8') as f:
json_info = json.loads(f.read())
json_info['work_mode'] = int(WORK_MODE)
table_name, table_name_space = generate_data_info(role)
if role == GUEST:
file_path = 'examples/data/{}'.format(GUEST_DATA_SET)
else:
file_path = 'examples/data/{}'.format(HOST_DATA_SET)
json_info['file'] = file_path
json_info['table_name'] = table_name
json_info['namespace'] = table_name_space
print("Upload data config json: {}".format(json_info))
stdout = exec_upload_task(json_info, role)
return stdout
def submit_job():
with open(DSL_PATH, 'r', encoding='utf-8') as f:
dsl_json = json.loads(f.read())
with open(SUBMIT_CONF_PATH, 'r', encoding='utf-8') as f:
conf_json = json.loads(f.read())
conf_json['job_parameters']['work_mode'] = WORK_MODE
conf_json['initiator']['party_id'] = GUEST_ID
conf_json['role']['guest'] = [GUEST_ID]
conf_json['role']['host'] = [HOST_ID]
conf_json['role']['arbiter'] = [ARBITER_ID]
guest_table_name, guest_namespace = generate_data_info(GUEST)
host_table_name, host_namespace = generate_data_info(HOST)
conf_json['role_parameters']['guest']['args']['data']['train_data'] = [
{
'name': guest_table_name,
'namespace': guest_namespace
}
]
conf_json['role_parameters']['host']['args']['data']['train_data'] = [
{
'name': host_table_name,
'namespace': host_namespace
}
]
# print("Submit job config json: {}".format(conf_json))
stdout = exec_modeling_task(dsl_json, conf_json)
job_id = stdout['jobId']
fate_board_url = stdout['data']['board_url']
print("Please check your task in fate-board, url is : {}".format(fate_board_url))
log_path = HOME_DIR + '/../../logs/{}'.format(job_id)
print("The log info is located in {}".format(log_path))
wait_query_job(job_id)
def predict_task():
try:
with open(LATEST_TRAINED_RESULT, 'r', encoding='utf-8') as f:
model_info = json.loads(f.read())
except FileNotFoundError:
raise FileNotFoundError('Train Result not Found, please finish a train task before going to predict task')
model_id = model_info['data']['model_info']['model_id']
model_version = model_info['data']['model_info']['model_version']
with open(TEST_PREDICT_CONF, 'r', encoding='utf-8') as f:
predict_conf = json.loads(f.read())
predict_conf['initiator']['party_id'] = GUEST_ID
predict_conf['job_parameters']['work_mode'] = WORK_MODE
predict_conf['job_parameters']['model_id'] = model_id
predict_conf['job_parameters']['model_version'] = model_version
predict_conf['role']['guest'] = [GUEST_ID]
predict_conf['role']['host'] = [HOST_ID]
predict_conf['role']['arbiter'] = [ARBITER_ID]
guest_table_name, guest_namespace = generate_data_info(GUEST)
host_table_name, host_namespace = generate_data_info(HOST)
predict_conf['role_parameters']['guest']['args']['data']['eval_data'] = [
{
'name': guest_table_name,
'namespace': guest_namespace
}
]
predict_conf['role_parameters']['host']['args']['data']['eval_data'] = [
{
'name': host_table_name,
'namespace': host_namespace
}
]
predict_conf_path = save_config_file(predict_conf, 'predict_conf')
subp = subprocess.Popen(["python",
FATE_FLOW_PATH,
"-f",
"submit_job",
"-c",
predict_conf_path
],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, stderr = subp.communicate()
stdout = stdout.decode("utf-8")
print("stdout:" + str(stdout))
stdout = json.loads(stdout)
status = stdout["retcode"]
job_id = stdout['jobId']
wait_query_job(job_id)
if status != 0:
raise ValueError(
"[Upload task]exec fail, status:{}, stdout:{}".format(status, stdout))
return stdout
def upload_data():
if WORK_MODE == 0:
upload(GUEST)
time.sleep(3)
upload(HOST)
time.sleep(3)
else:
if args.role == HOST:
upload(HOST)
else:
upload(GUEST)
time.sleep(3)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--role', required=False, type=str, help="role",
choices=(GUEST, HOST), default=GUEST
)
try:
args = parser.parse_args()
upload_data()
if TASK == 'train':
submit_job()
else:
if args.role == HOST:
raise ValueError("Predict task can be initialed by guest only")
predict_task()
except Exception as e:
exc_type, exc_value, exc_traceback_obj = sys.exc_info()
response = {'retcode': 100, 'retmsg': traceback.format_exception(exc_type, exc_value, exc_traceback_obj)}
prettify(response)