forked from It4innovations/hyperqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_resources.py
More file actions
617 lines (499 loc) · 18.2 KB
/
test_resources.py
File metadata and controls
617 lines (499 loc) · 18.2 KB
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
import collections
import multiprocessing
import time
from typing import Any, Dict
import pytest
from .utils.wait import wait_until
from .conftest import HqEnv
from .utils import wait_for_job_state
from .utils.cmd import python
from .utils.io import read_file
from .utils.job import default_task_output, list_jobs
def test_worker_resources_display(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(
cpus="4x2",
args=[
"--resource",
"potato=range(1-12)",
"--resource",
"fairy=sum(1000_1000)",
"--resource",
"shark=[1,3,5,2]",
"--resource",
"small=sum(0.5)",
],
)
table = hq_env.command(["worker", "list"], as_table=True)
assert table.get_column_value("Resources") == ["cpus 4x2; fairy 10001000; potato 12; shark 4; small 0.5"]
table = hq_env.command(["worker", "info", "1"], as_table=True)
print(table.get_row_value("Resources"))
assert table.get_row_value("Resources") == "cpus: 4x2\nfairy: 10001000\npotato: 12\nshark: 4\nsmall: 0.5"
def test_task_resources_ignore_worker_without_resource(hq_env: HqEnv):
hq_env.start_server()
hq_env.command(
[
"submit",
"--resource",
"fairy=1",
"--resource",
"potato=1000_000",
"uname",
]
)
def check_unscheduled():
time.sleep(0.5)
table = hq_env.command(["job", "info", "1"], as_table=True)
assert table.get_row_value("State") == "WAITING"
hq_env.start_worker(cpus=4)
check_unscheduled()
hq_env.start_worker(cpus=4, args=["--resource", "fairy=sum(1000)"])
check_unscheduled()
hq_env.start_worker(cpus=4, args=["--resource", "fairy=sum(2)", "--resource", "potato=sum(500)"])
check_unscheduled()
def test_task_resources_allocate(hq_env: HqEnv):
hq_env.start_server()
hq_env.command(
[
"submit",
"--resource",
"fairy=1000",
"--resource",
"potato=1",
"--",
"bash",
"-c",
( # no comma
"echo $HQ_RESOURCE_REQUEST_fairy:$HQ_RESOURCE_REQUEST_potato"
":$HQ_RESOURCE_VALUES_fairy:$HQ_RESOURCE_VALUES_potato"
),
]
)
hq_env.start_worker(
cpus=4,
args=[
"--resource",
"fairy=sum(2000_000)",
"--resource",
"potato=range(30-35)",
],
)
wait_for_job_state(hq_env, 1, "FINISHED")
with open(default_task_output()) as f:
f_count, p_count, f_idx, p_idx = f.read().rstrip().split(":")
assert f_count == "1000 compact"
assert p_count == "1 compact"
assert f_idx == ""
assert 30 <= int(p_idx) <= 35
def test_task_resources_range_multiple_allocated_values(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(cpus=4)
hq_env.command(
[
"submit",
"--array=1-3",
"--resource",
"fairy=2",
"--",
"bash",
"-c",
"sleep 1; echo $HQ_RESOURCE_REQUEST_fairy:$HQ_RESOURCE_VALUES_fairy",
]
)
hq_env.start_worker(cpus=4, args=["--resource", "fairy=range(31-36)"])
wait_for_job_state(hq_env, 1, "FINISHED")
all_values = []
for i in range(1, 4):
with open(default_task_output(task_id=i)) as f:
rq, indices = f.read().rstrip().split(":")
assert rq == "2 compact"
values = [int(x) for x in indices.split(",")]
assert len(values) == 2
all_values += values
assert all(31 <= x <= 36 for x in all_values)
assert len(set(all_values)) == 6
def test_task_resource_fractions_sum(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(cpus=4, args=["--resource", "foo=sum(2)"])
hq_env.command(
[
"submit",
"--array=1-4",
"--resource",
"foo=0.5",
"--",
"bash",
"-c",
"date +%s%N\nsleep 1",
]
)
wait_for_job_state(hq_env, 1, "FINISHED")
times = []
for task_id in range(1, 5):
with open(default_task_output(task_id=task_id)) as f:
times.append(int(f.readline().rstrip()))
times.sort()
assert ((times[-1] - times[0]) / 1000_000_000) < 0.15
def test_task_resource_fractions_scatter(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(cpus=4, args=["--resource", "foo=[[a,b,c],[i, j, k],[x, y],[v, w]]"])
hq_env.command(
[
"submit",
"--resource",
"foo=3.5",
"--",
"bash",
"-c",
"echo $HQ_RESOURCE_VALUES_foo",
]
)
wait_for_job_state(hq_env, 1, "FINISHED")
with open(default_task_output()) as f:
r = f.readline().rstrip().split(",")
assert len(r) == 4
assert len(set(r)) == 4
def test_task_resource_fractions_sum2(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(cpus=4, args=["--resource", "foo=sum(2.2)"])
hq_env.command(
[
"submit",
"--array=1-4",
"--resource",
"foo=0.6",
"--",
"bash",
"-c",
"date +%s%N\nsleep 1",
]
)
wait_for_job_state(hq_env, 1, "FINISHED")
times = []
for task_id in range(1, 5):
with open(default_task_output(task_id=task_id)) as f:
times.append(int(f.readline().rstrip()))
times.sort()
assert 0.99 < ((times[-1] - times[0]) / 1000_000_000) < 1.25
def test_task_resource_fractions_sharing_small(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(cpus=4, args=["--resource", "foo=[a,b]"])
hq_env.command(
[
"submit",
"--array=1-4",
"--resource",
"foo=0.4",
"--",
"bash",
"-c",
"date +%s%N\necho $HQ_RESOURCE_VALUES_foo\nsleep 1",
]
)
wait_for_job_state(hq_env, 1, "FINISHED")
times = []
counts = collections.Counter()
for task_id in range(1, 5):
with open(default_task_output(task_id=task_id)) as f:
times.append(int(f.readline().rstrip()))
name = f.readline().strip()
assert name in ["a", "b"]
counts[name] += 1
times.sort()
assert ((times[-1] - times[0]) / 1000_000_000) < 0.15
assert counts == collections.Counter({"a": 2, "b": 2})
def test_task_resource_fractions_sharing_larger(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(cpus=4, args=["--resource", "foo=[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]"])
hq_env.command(
[
"submit",
"--array=1-4",
"--resource",
"foo=2.5",
"--",
"bash",
"-c",
"date +%s%N\necho $HQ_RESOURCE_VALUES_foo\nsleep 1",
]
)
wait_for_job_state(hq_env, 1, "FINISHED")
times = []
counts_frac = collections.Counter()
counts_whole = collections.Counter()
for task_id in range(1, 5):
with open(default_task_output(task_id=task_id)) as f:
times.append(int(f.readline().rstrip()))
names = f.readline().strip().split(",")
for name in names[:-1]:
counts_whole[name] += 1
counts_frac[names[-1]] += 1
times.sort()
assert ((times[-1] - times[0]) / 1000_000_000) < 0.15
assert tuple(counts_frac.values()) == (2, 2)
assert tuple(counts_whole.values()) == (1,) * 8
def test_task_resources_allocate_string(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(args=["--resource", "foo=[a,b,c]"])
hq_env.command(
[
"submit",
"--array=1-4",
"--resource",
"foo=2",
"--",
"bash",
"-c",
"echo $HQ_RESOURCE_VALUES_foo",
]
)
wait_for_job_state(hq_env, 1, "FINISHED")
for task_id in range(1, 5):
with open(default_task_output(task_id=task_id)) as f:
indices = set(f.read().rstrip().split(","))
assert len(indices) == 2
assert indices.issubset(("a", "b", "c"))
def test_worker_resource_hwdetect_mem(hq_env: HqEnv):
hq_env.start_server()
resources = hq_env.command(["worker", "hwdetect"])
assert "mem:" in resources
for resource in resources.splitlines():
if "mem:" in resource:
value = resource.split("mem: ")[1]
assert value != ""
def test_worker_set_gpu_env_for_task(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(args=["--resource", "gpus/nvidia=[0,1]"])
hq_env.command(
[
"submit",
"--resource",
"gpus/nvidia=2",
"--",
"bash",
"-c",
"""
echo $CUDA_VISIBLE_DEVICES
""",
]
)
wait_for_job_state(hq_env, 1, "FINISHED")
assert list(set(int(v) for v in line.split(",")) for line in read_file(default_task_output()).splitlines()) == [
{0, 1}
]
@pytest.mark.parametrize(
"env_and_res",
(
("CUDA_VISIBLE_DEVICES", "gpus/nvidia"),
("ROCR_VISIBLE_DEVICES", "gpus/amd"),
),
)
def test_worker_detect_gpus_from_env(hq_env: HqEnv, env_and_res: str):
env, resource = env_and_res
hq_env.start_server()
resources = hq_env.command(["worker", "hwdetect"], env={env: "1,3"})
assert f"{resource}: [1,3]" in resources
def test_worker_detect_uuid_gpus_from_env(hq_env: HqEnv):
hq_env.start_server()
resources = hq_env.command(["worker", "hwdetect"], env={"CUDA_VISIBLE_DEVICES": "foo,bar"})
assert "gpus/nvidia: [foo,bar]" in resources
def test_worker_detect_multiple_gpus_from_env(hq_env: HqEnv):
hq_env.start_server()
resources = hq_env.command(
["worker", "hwdetect"],
env={
"CUDA_VISIBLE_DEVICES": "0,1",
"ROCR_VISIBLE_DEVICES": "1",
},
)
assert "gpus/nvidia: [0,1]" in resources
assert "gpus/amd: [1]" in resources
@pytest.mark.skipif(
multiprocessing.cpu_count() < 2,
reason="This test needs at least two cores to be available",
)
def test_worker_detect_respect_cpu_mask(hq_env: HqEnv):
hq_env.start_server()
resources = hq_env.command(["worker", "hwdetect"], cmd_prefix=["taskset", "-c", "1"])
assert "cpus: [1]" in resources
def test_worker_no_detect_cpus_error(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(
cpus=None, expect_fail="You have to specify --cpus explicitly when opting out of CPU automatic detection"
)
def get_worker_resources(hq_env: HqEnv, id=1) -> Dict[str, Any]:
resources = hq_env.command(["worker", "info", str(id), "--output-mode", "json"], as_json=True)[0]
resources = resources["configuration"]["resources"]["resources"]
return {r["name"]: r for r in resources}
def test_worker_detect_mem(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(detect_resources="cpus,mem")
assert "mem" in get_worker_resources(hq_env)
def test_worker_no_detect_mem(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(detect_resources="cpus")
assert "mem" not in get_worker_resources(hq_env)
def test_worker_detect_gpus(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(detect_resources="cpus,gpus/nvidia", env={"CUDA_VISIBLE_DEVICES": "1"})
assert "gpus/nvidia" in get_worker_resources(hq_env)
def test_worker_no_detect_gpus(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(detect_resources="cpus", env={"CUDA_VISIBLE_DEVICES": "1"})
assert "gpus/nvidia" not in get_worker_resources(hq_env)
def test_task_info_resources(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker()
hq_env.command(
[
"submit",
"--array=1-3",
"--resource",
"fairy=2",
"sleep 0",
]
)
table = hq_env.command(["task", "info", "1", "1"], as_table=True)
table.check_row_value("Resources", "cpus: 1 compact\nfairy: 2 compact")
def test_string_resource_list(hq_env: HqEnv):
hq_env.start_server()
hq_env.command(
[
"submit",
"--array=1-3",
"--resource",
"fairy=2",
"sleep 0",
]
)
table = hq_env.command(["task", "info", "1", "1"], as_table=True)
table.check_row_value("Resources", "cpus: 1 compact\nfairy: 2 compact")
def test_resource_name_slash(hq_env: HqEnv):
hq_env.start_server()
res_name = "gpus/amd"
hq_env.command(["submit", "--resource", f"{res_name}=1", "ls"])
hq_env.start_worker(args=["--resource", f"{res_name}=[0]"])
wait_for_job_state(hq_env, 1, "FINISHED")
def test_resource_name_ensure_normalization(hq_env: HqEnv):
hq_env.start_server()
res_name = "gpus/amd"
hq_env.command(
[
"submit",
"--resource",
f"{res_name}=1",
"--",
*python(
"""
import os
import sys
print(os.environ["HQ_RESOURCE_REQUEST_gpus_amd"], flush=True)
print(os.environ["HQ_RESOURCE_VALUES_gpus_amd"], flush=True)
"""
),
]
)
hq_env.start_worker(args=["--resource", f"{res_name}=[0]"])
wait_for_job_state(hq_env, 1, "FINISHED")
with open(default_task_output()) as f:
assert f.read() == "1 compact\n0\n"
def test_cpu_resource_sum(hq_env: HqEnv):
hq_env.start_server()
hq_env.command(
args=["worker", "start", "--cpus=sum(5)"],
expect_fail="Resource kind `sum` cannot be used with CPUs",
)
def test_manual_cpu_resource_sum(hq_env: HqEnv):
hq_env.start_server()
hq_env.command(
args=["worker", "start", "--resource", "cpus=sum(5)"],
expect_fail="Resource kind `sum` cannot be used with CPUs",
)
def test_resources_invalid_definitions(hq_env: HqEnv):
hq_env.start_server()
hq_env.command(
args=["submit", "--resource", "foo=10", "--resource", "foo=1", "--", "sleep", "1"],
expect_fail="Resource 'foo' defined more than once",
)
hq_env.command(
args=["submit", "--resource", "foo=0", "--", "sleep", "1"],
expect_fail="Zero resources cannot be requested",
)
def test_resources_and_priorities_one_by_one_submit(hq_env: HqEnv):
hq_env.start_server()
hq_env.start_worker(args=["--resource", "foo=[1, 2, 3, 4]"], cpus=8)
for i in range(1, 5):
hq_env.command(["submit", "--resource", "foo=2", "--", "sleep", "100"])
if i < 3:
wait_for_job_state(hq_env, i, "RUNNING")
time.sleep(0.6)
wait_for_job_state(hq_env, [1, 2], "RUNNING")
wait_for_job_state(hq_env, [3, 4], "WAITING")
time.sleep(1.0)
hq_env.command(["submit", "--cpus=2", "--priority=-1", "--", "sleep", "100"])
def check():
table = list_jobs(hq_env)
return table.get_column_value("State") == ["RUNNING", "RUNNING", "WAITING", "WAITING", "RUNNING"]
wait_until(check, timeout_s=5)
def test_resources_and_priorities_submit_before_worker(hq_env: HqEnv):
hq_env.start_server()
for i in range(1, 5):
hq_env.command(["submit", "--resource", "foo=2", "--", "sleep", "100"])
hq_env.command(["submit", "--cpus=2", "--priority=-1", "--", "sleep", "100"])
hq_env.start_worker(args=["--resource", "foo=[1, 2, 3, 4]"], cpus=8)
def check():
table = list_jobs(hq_env)
s = table.get_column_value("State")
return s[-1] == "RUNNING" and s[:-1].count("RUNNING") == 2 and s[:-1].count("WAITING") == 2
wait_until(check, timeout_s=5)
def test_resources_and_many_priorities(hq_env: HqEnv):
hq_env.start_server()
for i in range(1, 10):
hq_env.command(["submit", "--resource", "foo=2", f"--priority={i * 10}", "--", "sleep", "100"])
for i in range(12):
hq_env.command(["submit", "--cpus=1", f"--priority={i // 3 - 5}", "--", "sleep", "100"])
hq_env.start_worker(args=["--resource", "foo=[1, 2, 3, 4, 5, 6]"], cpus=12)
def check():
table = list_jobs(hq_env)
s = table.get_column_value("State")
return s == 6 * ["WAITING"] + 3 * ["RUNNING"] + 3 * ["WAITING"] + 9 * ["RUNNING"]
wait_until(check, timeout_s=5)
def groups(job_id):
with open(default_task_output(job_id)) as f:
data = [int(x) // 10 for x in f.read().rstrip().split(",")]
c = collections.Counter(data)
return sorted(c.values())
def test_tight_vs_compact_policy(hq_env: HqEnv):
hq_env.start_server()
hq_env.command(["submit", "--cpus", "6 tight", "--", "bash", "-c", "echo $HQ_CPUS"])
hq_env.command(["submit", "--cpus", "6 tight!", "--", "bash", "-c", "echo $HQ_CPUS"])
table = hq_env.command(["job", "info", "1"], as_table=True)
table.check_row_value("Resources", "cpus: 6 tight")
table = hq_env.command(["job", "info", "2"], as_table=True)
table.check_row_value("Resources", "cpus: 6 tight!")
hq_env.start_worker(cpus="[[1, 2, 3, 4], [11, 12, 13, 14], [21, 22, 23, 24]]")
wait_for_job_state(hq_env, [1, 2], "FINISHED")
hq_env.command(["submit", "--cpus", "6 compact", "--", "bash", "-c", "echo $HQ_CPUS"])
hq_env.command(["submit", "--cpus", "6 compact!", "--", "bash", "-c", "echo $HQ_CPUS"])
wait_for_job_state(hq_env, [3, 4], "FINISHED")
assert groups(1) == [2, 4]
assert groups(2) == [2, 4]
assert groups(3) == [3, 3]
assert groups(4) == [3, 3]
def test_fractional_force_compact(hq_env: HqEnv):
hq_env.start_server()
hq_env.command(["submit", "--priority=10", "--array=1-100", "--cpus", "0.3 compact!", "--", "sleep", "0"])
hq_env.command(["submit", "--cpus", "2.5 compact!", "--", "bash", "-c", "echo $HQ_CPUS"])
hq_env.start_worker(cpus="[[1, 2], [11, 12], [21, 22]]")
wait_for_job_state(hq_env, [1, 2], "FINISHED")
assert groups(2) == [1, 2]
def test_fractions_blocked(hq_env: HqEnv):
hq_env.start_server()
hq_env.command(["submit", "--array=1-3", "--cpus=0.60", "--", "sleep", "1"])
hq_env.start_worker(cpus=2)
wait_for_job_state(hq_env, [1], "FINISHED")
def test_strict_compact_blocked(hq_env: HqEnv):
hq_env.start_server()
hq_env.command(["submit", "--array=1-3", "--cpus=2 compact!", "--", "sleep", "1"])
hq_env.start_worker(cpus="[[1, 2, 3], [11, 12, 13]]")
wait_for_job_state(hq_env, [1], "FINISHED")