Download code/v2/tdprm/tests/test_queue.py from JoshuaW1997/tdprm-results: direct link, hf CLI and curl.
- Browser
- Download file 5.03 kB
-
https://huggingface.co/datasets/JoshuaW1997/tdprm-results/resolve/main/code/v2/tdprm/tests/test_queue.py
- Command line
-
hf download hf://datasets/JoshuaW1997/tdprm-results/code/v2/tdprm/tests/test_queue.py
-
curl -L -o test_queue.py https://huggingface.co/datasets/JoshuaW1997/tdprm-results/resolve/main/code/v2/tdprm/tests/test_queue.py
5.03 kB
| import json | |
| import subprocess | |
| import sys | |
| import time | |
| from tdprm.queue import gpuq | |
| def write_job(tmp, name, body): | |
| p = tmp / f"{name}.sh" | |
| p.write_text("#!/usr/bin/env bash\n" + body + "\n") | |
| return str(p) | |
| def test_queue_runs_dependencies_files_failures_and_pairs(tmp_path): | |
| marker = tmp_path / "ckpt" / "READY" | |
| seen = tmp_path / "seen.txt" | |
| jobs = [ | |
| {"id": "a", "gpus": 1, "priority": 0, "deps": [], | |
| "script": write_job(tmp_path, "a", f"echo a:$CUDA_VISIBLE_DEVICES >> {seen}; mkdir -p {marker.parent}; " | |
| f"touch {marker}")}, | |
| {"id": "b", "gpus": 2, "priority": 1, "deps": ["a"], | |
| "script": write_job(tmp_path, "b", f"echo b:$CUDA_VISIBLE_DEVICES >> {seen}")}, | |
| {"id": "c", "gpus": 1, "priority": 2, "deps": [f"file:{marker}"], | |
| "script": write_job(tmp_path, "c", f"echo c:$TDPRM_GPU0 >> {seen}")}, | |
| {"id": "d", "gpus": 0, "priority": 0, "deps": [], "script": write_job(tmp_path, "d", "exit 3")}, | |
| {"id": "e", "gpus": 1, "priority": 0, "deps": ["d"], "script": write_job(tmp_path, "e", "echo never")}, | |
| ] | |
| plan = tmp_path / "plan.json" | |
| plan.write_text(json.dumps({"jobs": jobs, "reservations": []})) | |
| proc = subprocess.run([sys.executable, "-m", "tdprm.queue.gpuq", "run", "--plan", str(plan), "--gpus", "0,1", | |
| "--poll", "0.2", "--max-retries", "0", "--max-used-mib", str(10**9)], | |
| capture_output=True, text=True, timeout=60) | |
| assert proc.returncode == 0, proc.stderr | |
| state = json.loads((tmp_path / "plan.state.json").read_text()) | |
| assert [state[j]["status"] for j in "abcde"] == ["done", "done", "done", "failed", "blocked"] | |
| lines = seen.read_text().split() | |
| assert "b:0,1" in lines and any(x.startswith("a:") for x in lines) and any(x.startswith("c:") for x in lines) | |
| def test_queue_auto_retries_a_failed_job_and_unblocks_dependents(tmp_path): | |
| flag = tmp_path / "failed_once" | |
| jobs = [{"id": "flaky", "gpus": 0, "priority": 0, "deps": [], | |
| "script": write_job(tmp_path, "flaky", f"if [ -f {flag} ]; then exit 0; fi; touch {flag}; exit 1")}, | |
| {"id": "after", "gpus": 0, "priority": 1, "deps": ["flaky"], "script": write_job(tmp_path, "after", "exit 0")}] | |
| plan = tmp_path / "plan.json" | |
| plan.write_text(json.dumps({"jobs": jobs, "reservations": []})) | |
| proc = subprocess.run([sys.executable, "-m", "tdprm.queue.gpuq", "run", "--plan", str(plan), "--gpus", "0", | |
| "--poll", "0.2", "--max-retries", "1", "--retry-after", "0"], | |
| capture_output=True, text=True, timeout=60) | |
| assert proc.returncode == 0, proc.stderr | |
| state = json.loads((tmp_path / "plan.state.json").read_text()) | |
| assert state["flaky"]["status"] == "done" and state["flaky"]["attempts"] == 1 | |
| assert state["after"]["status"] == "done" | |
| def test_simulate_respects_reservations(tmp_path, capsys): | |
| jobs = [{"id": "jev", "gpus": 1, "priority": 5, "deps": ["prep"], "est_hours": 2.0, "script": "x"}, | |
| {"id": "prep", "gpus": 0, "priority": 0, "deps": [], "est_hours": 0.5, "script": "x"}, | |
| {"id": "t1", "gpus": 1, "priority": 10, "deps": [], "est_hours": 3.0, "script": "x"}, | |
| {"id": "t2", "gpus": 1, "priority": 10, "deps": [], "est_hours": 3.0, "script": "x"}] | |
| plan = tmp_path / "plan.json" | |
| plan.write_text(json.dumps({"jobs": jobs, "reservations": [{"gpus": [1], "jobs": ["jev"]}]})) | |
| class A: | |
| pass | |
| args = A() | |
| args.plan, args.gpus, args.all, args.backfill_max_hours = str(plan), "0,1", True, 0.5 | |
| gpuq.simulate(args) | |
| out = capsys.readouterr().out | |
| # t1 takes GPU 0, GPU 1 is held for "jev" (starts at 0.5h), t2 waits until a GPU frees up. | |
| assert "gpus=[1] jev" in out and "0.00h -> 3.00h gpus=[0] t1" in out | |
| assert "2.50h -> 5.50h" in out | |
| def test_after_dependency_waits_for_any_final_outcome(tmp_path): | |
| """`after:X` runs once X is finished for good (here: failed with no retries left), unlike a plain dep.""" | |
| seen = tmp_path / "seen.txt" | |
| jobs = [{"id": "x", "gpus": 0, "priority": 0, "deps": [], "script": write_job(tmp_path, "x", "exit 1")}, | |
| {"id": "y", "gpus": 0, "priority": 1, "deps": ["after:x"], | |
| "script": write_job(tmp_path, "y", f"echo y >> {seen}")}, | |
| {"id": "z", "gpus": 0, "priority": 1, "deps": ["x"], "script": write_job(tmp_path, "z", "exit 0")}] | |
| plan = tmp_path / "plan.json" | |
| plan.write_text(json.dumps({"jobs": jobs, "reservations": []})) | |
| proc = subprocess.run([sys.executable, "-m", "tdprm.queue.gpuq", "run", "--plan", str(plan), "--gpus", "0", | |
| "--poll", "0.2", "--max-retries", "0"], capture_output=True, text=True, timeout=60) | |
| assert proc.returncode == 0, proc.stderr | |
| state = json.loads((tmp_path / "plan.state.json").read_text()) | |
| assert [state[j]["status"] for j in "xyz"] == ["failed", "done", "blocked"] and seen.read_text() == "y\n" | |