forked from studioml/studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_worker_test.py
467 lines (386 loc) · 15.6 KB
/
local_worker_test.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import unittest
import os
import shutil
import tempfile
import uuid
import subprocess
import time
from timeout_decorator import timeout
import traceback
import numpy as np
try:
import keras
except BaseException:
keras = None
from studio import model, logs
from studio.local_queue import LocalQueue, get_local_queue_lock
from studio.util import has_aws_credentials, sixdecode, retry
from env_detect import on_gcp, on_aws
TEST_TIMEOUT = 600
class LocalWorkerTest(unittest.TestCase):
@timeout(TEST_TIMEOUT, use_signals=False)
def test_runner_local(self):
with stubtest_worker(
self,
experiment_name='test_runner_local_' + str(uuid.uuid4()),
config_name='test_config_http_client.yaml',
test_script='tf_hello_world.py',
runner_args=[],
script_args=['arg0'],
expected_output='[ 2.0 6.0 ]'
):
pass
@timeout(TEST_TIMEOUT, use_signals=False)
def test_args_conflict(self):
with stubtest_worker(
self,
experiment_name='test_runner_conflict_' + str(uuid.uuid4()),
config_name='test_config.yaml',
runner_args=[],
test_script='conflicting_args.py',
script_args=['--experiment', 'aaa'],
expected_output='Experiment key = aaa'
):
pass
@timeout(TEST_TIMEOUT, use_signals=False)
def test_local_hyperparam(self):
with stubtest_worker(
self,
experiment_name='test_local_hyperparam' + str(uuid.uuid4()),
runner_args=['--verbose=debug'],
config_name='test_config_http_client.yaml',
test_script='hyperparam_hello_world.py',
expected_output='0.3'
):
pass
with stubtest_worker(
self,
experiment_name='test_local_hyperparam' + str(uuid.uuid4()),
runner_args=[
'--verbose=debug',
'--hyperparam=learning_rate=0.4'
],
config_name='test_config_http_client.yaml',
test_script='hyperparam_hello_world.py',
expected_output='0.4'
):
pass
@unittest.skip('peterz figure out the failure - happens intermittently ' +
'when running in parallel')
@timeout(TEST_TIMEOUT, use_signals=False)
def test_local_worker_ce(self):
tmpfile = os.path.join(tempfile.gettempdir(),
'tmpfile_ce_' +
str(uuid.uuid4()) + '.txt')
random_str1 = str(uuid.uuid4())
with open(tmpfile, 'w') as f:
f.write(random_str1)
random_str2 = str(uuid.uuid4())
experiment_name = 'test_local_worker_c' + str(uuid.uuid4())
print("random_str1 = " + random_str1)
print("random_str2 = " + random_str2)
print("experiment_name = " + experiment_name)
print("tmpfile = " + tmpfile)
with stubtest_worker(
self,
experiment_name=experiment_name,
runner_args=['--capture=' + tmpfile + ':f',
'--verbose=debug'],
config_name='test_config_http_client.yaml',
test_script='art_hello_world.py',
script_args=[random_str2],
expected_output=random_str1,
delete_when_done=False
) as db:
pass
tmppath = db.get_artifact(
db.get_experiment(experiment_name).artifacts['f']
)
with open(tmppath, 'r') as f:
self.assertTrue(f.read() == random_str2)
os.remove(tmppath)
with stubtest_worker(
self,
experiment_name='test_local_worker_e' + str(uuid.uuid4()),
runner_args=['--reuse={}/f:f'.format(experiment_name)],
config_name='test_config_http_client.yaml',
test_script='art_hello_world.py',
script_args=[],
expected_output=random_str2
) as db:
db.delete_experiment(experiment_name)
@timeout(TEST_TIMEOUT, use_signals=False)
def test_local_worker_co(self):
tmpfile = os.path.join(tempfile.gettempdir(),
'tmpfile' +
str(uuid.uuid4()) + '.txt')
random_str = str(uuid.uuid4())
with open(tmpfile, 'w') as f:
f.write(random_str)
# with get_local_queue_lock():
with stubtest_worker(
self,
experiment_name='test_local_worker_co' + str(uuid.uuid4()),
runner_args=['--capture-once=' + tmpfile + ':f'],
config_name='test_config_http_client.yaml',
test_script='art_hello_world.py',
script_args=[],
expected_output=random_str
):
pass
@timeout(TEST_TIMEOUT, use_signals=False)
def test_local_worker_co_url(self):
expected_str = 'Zabil zaryad ya v pushku tugo'
url = 'https://storage.googleapis.com/studio-ed756.appspot.com/' + \
'tests/url_artifact.txt'
# with get_local_queue_lock():
with stubtest_worker(
self,
experiment_name='test_local_worker_co_url' + str(uuid.uuid4()),
runner_args=['--capture-once=' + url + ':f'],
config_name='test_config_http_client.yaml',
test_script='art_hello_world.py',
script_args=[],
expected_output=expected_str
):
pass
@unittest.skipIf(
not on_aws(),
'User indicated not on aws')
class UserIndicatedOnAWSTest(unittest.TestCase):
def test_on_enviornment(self):
self.assertTrue(has_aws_credentials())
@unittest.skipIf(
(not on_aws()) or not has_aws_credentials(),
'Skipping due to userinput or AWS Not detected')
@timeout(TEST_TIMEOUT, use_signals=False)
def test_local_worker_co_s3(self):
expected_str = 'No4 ulica fonar apteka, bessmyslennyj i tusklyj svet'
s3loc = 's3://studioml-artifacts/tests/download_test/download_test.txt'
# with get_local_queue_lock():
with stubtest_worker(
self,
experiment_name='test_local_worker_co_s3' + str(uuid.uuid4()),
runner_args=['--capture-once=' + s3loc + ':f'],
config_name='test_config_http_client.yaml',
test_script='art_hello_world.py',
script_args=[],
expected_output=expected_str
):
pass
@unittest.skipIf(keras is None,
'keras is required for this test')
@timeout(TEST_TIMEOUT, use_signals=False)
def test_save_get_model(self):
experiment_name = 'test_save_get_model' + str(uuid.uuid4())
# with get_local_queue_lock():
with stubtest_worker(
self,
experiment_name=experiment_name,
runner_args=[],
config_name='test_config_http_client.yaml',
test_script='save_model.py',
script_args=[],
expected_output='',
delete_when_done=False,
test_output=False
) as db:
experiment = db.get_experiment(experiment_name)
saved_model = experiment.get_model(db)
v = np.random.rand(1, 2)
prediction = saved_model.predict(v)
expected = v * 2
self.assertTrue(np.isclose(prediction, expected).all())
db.delete_experiment(experiment)
@timeout(TEST_TIMEOUT, use_signals=False)
def test_stop_experiment(self):
my_path = os.path.dirname(os.path.realpath(__file__))
logger = logs.getLogger('test_stop_experiment')
logger.setLevel(10)
config_name = os.path.join(my_path, 'test_config_http_client.yaml')
key = 'test_stop_experiment' + str(uuid.uuid4())
with model.get_db_provider(model.get_config(config_name)) as db:
try:
db.delete_experiment(key)
except Exception:
pass
p = subprocess.Popen(['studio', 'run',
'--config=' + config_name,
'--experiment=' + key,
'--force-git',
'--verbose=debug',
'stop_experiment.py'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=my_path)
# wait till experiment spins up
experiment = None
while experiment is None or experiment.status == 'waiting':
time.sleep(1)
try:
experiment = db.get_experiment(key)
except BaseException:
pass
logger.info('Stopping experiment')
db.stop_experiment(key)
pout, _ = p.communicate()
if pout:
logger.debug("studio run output: \n" + pout.decode())
db.delete_experiment(key)
@timeout(TEST_TIMEOUT, use_signals=False)
def test_experiment_maxduration(self):
my_path = os.path.dirname(os.path.realpath(__file__))
logger = logs.getLogger('test_experiment_maxduration')
logger.setLevel(10)
config_name = os.path.join(my_path, 'test_config_http_client.yaml')
key = 'test_experiment_maxduration' + str(uuid.uuid4())
with model.get_db_provider(model.get_config(config_name)) as db:
try:
db.delete_experiment(key)
except Exception:
pass
p = subprocess.Popen(['studio', 'run',
'--config=' + config_name,
'--experiment=' + key,
'--force-git',
'--verbose=debug',
'--max-duration=10s',
'stop_experiment.py'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=my_path)
pout, _ = p.communicate()
if pout:
logger.debug("studio run output: \n" + pout.decode())
db.delete_experiment(key)
@timeout(TEST_TIMEOUT, use_signals=False)
def test_experiment_lifetime(self):
my_path = os.path.dirname(os.path.realpath(__file__))
logger = logs.getLogger('test_experiment_lifetime')
logger.setLevel(10)
config_name = os.path.join(my_path, 'test_config_http_client.yaml')
key = 'test_experiment_lifetime' + str(uuid.uuid4())
with model.get_db_provider(model.get_config(config_name)) as db:
try:
db.delete_experiment(key)
except Exception:
pass
p = subprocess.Popen(['studio', 'run',
'--config=' + config_name,
'--experiment=' + key,
'--force-git',
'--verbose=debug',
'--lifetime=-10m',
'stop_experiment.py'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=my_path)
pout, _ = p.communicate()
if pout:
logger.debug("studio run output: \n" + pout.decode())
db.delete_experiment(key)
def stubtest_worker(
testclass,
experiment_name,
runner_args,
config_name,
test_script,
expected_output,
script_args=[],
queue=LocalQueue(),
wait_for_experiment=True,
delete_when_done=True,
test_output=True,
test_workspace=True):
my_path = os.path.dirname(os.path.realpath(__file__))
config_name = os.path.join(my_path, config_name)
logger = logs.getLogger('stubtest_worker')
logger.setLevel(10)
queue.clean()
with model.get_db_provider(model.get_config(config_name)) as db:
try:
db.delete_experiment(experiment_name)
except Exception:
pass
os.environ['PYTHONUNBUFFERED'] = 'True'
p = subprocess.Popen(['studio', 'run'] + runner_args +
['--config=' + config_name,
'--verbose=debug',
'--force-git',
'--experiment=' + experiment_name,
test_script] + script_args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
close_fds=True,
cwd=my_path)
pout, _ = p.communicate()
if pout:
logger.debug("studio run output: \n" + sixdecode(pout))
splitpout = sixdecode(pout).split('\n')
experiments = [line.split(' ')[-1] for line in splitpout
if line.startswith('studio run: submitted experiment')]
logger.debug("added experiments: {}".format(experiments))
db = model.get_db_provider(model.get_config(config_name))
experiment_name = experiments[0]
try:
experiment = db.get_experiment(experiment_name)
if wait_for_experiment:
while not experiment or not experiment.status == 'finished':
experiment = db.get_experiment(experiment_name)
if test_output:
with open(db.get_artifact(experiment.artifacts['output']),
'r') as f:
data = f.read()
split_data = data.strip().split('\n')
print(data)
testclass.assertEquals(split_data[-1], expected_output)
if test_workspace:
check_workspace(testclass, db, experiment_name)
if delete_when_done:
retry(lambda: db.delete_experiment(experiment_name), sleep_time=10)
return db
except Exception as e:
print("Exception {} raised during test".format(e))
print("worker output: \n {}".format(pout))
print("Exception trace:")
print(traceback.format_exc())
raise e
def check_workspace(testclass, db, key):
tmpdir = os.path.join(tempfile.gettempdir(), str(uuid.uuid4()))
os.mkdir(tmpdir)
experiment = retry(lambda: db.get_experiment(key), sleep_time=5)
artifact = experiment.artifacts['workspace']
localpath = retry(
lambda: db.get_artifact(
artifact,
tmpdir,
only_newer=False),
sleep_time=5)
for _, _, files in os.walk(localpath, topdown=False):
for filename in files:
downloaded_filename = os.path.join(tmpdir, filename)
if downloaded_filename.endswith('.pyc'):
continue
with open(downloaded_filename, 'rb') as f1:
data1 = f1.read()
with open(os.path.join(db.get_artifact(artifact), filename),
'rb') as f2:
data2 = f2.read()
testclass.assertTrue(
data1 == data2,
('File comparison between local {} ' +
'and downloaded {} has failed')
.format(
filename,
downloaded_filename))
for _, _, files in os.walk('tmpdir', topdown=False):
for filename in files:
downloaded_filename = os.path.join(tmpdir, filename)
with open(downloaded_filename, 'rb') as f1:
data1 = f1.read()
with open(filename, 'rb') as f2:
data2 = f2.read()
testclass.assertTrue(data1 == data2)
shutil.rmtree(tmpdir)
if __name__ == "__main__":
unittest.main()