Repository navigation
Expand file tree
/
Copy pathtest_slurm_scripts.py
More file actions
202 lines (192 loc) · 7.13 KB
/
Copy pathtest_slurm_scripts.py
File metadata and controls
202 lines (192 loc) · 7.13 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
from __future__ import annotations
import json
import os
import shlex
import shutil
import subprocess
import sys
from pathlib import Path
import pytest
pytestmark = pytest.mark.skipif(
os.name != "posix", reason="Slurm scripts require a POSIX shell and filesystem"
)
ROOT = Path(__file__).resolve().parents[1]
@pytest.mark.parametrize("upload_chunks", [None, "3"])
def test_refresh_logs_use_invocation_directory(tmp_path, upload_chunks):
scripts = tmp_path / "repo" / "scripts"
scripts.mkdir(parents=True)
submission = tmp_path / "submitted from 50%"
submission.mkdir()
shutil.copy(ROOT / "scripts/submit-refresh-jobs.sh", scripts)
setup = scripts / "ensure-poetry-env.sh"
setup.write_text("#!/usr/bin/env bash\nexit 0\n")
setup.chmod(0o755)
record = tmp_path / "submissions.jsonl"
capture = (
"import json, os, sys; "
'f=open(os.environ["SAPS_TEST_SUBMISSIONS"], "a"); '
'f.write(json.dumps(sys.argv[1:])+"\\n"); f.close(); print("12345")'
)
shell_env = tmp_path / "shell-env"
shell_env.write_text(
'aws() { if [[ "$1 $2" == "configure list-profiles" ]]; then '
'printf "dataset-upload\\n"; fi; }\n'
"poetry() { return 0; }\n"
"sbatch() { "
+ shlex.quote(sys.executable)
+ " -c "
+ shlex.quote(capture)
+ ' "$@"; }\n'
)
env = {
**os.environ,
"BASH_ENV": str(shell_env),
"SAPS_TEST_SUBMISSIONS": str(record),
}
env.pop("SAPS_UPLOAD_CHUNK_COUNT", None)
if upload_chunks is not None:
env["SAPS_UPLOAD_CHUNK_COUNT"] = upload_chunks
subprocess.run(
["bash", str(scripts / "submit-refresh-jobs.sh")],
cwd=submission,
env=env,
check=True,
capture_output=True,
text=True,
)
submissions = [json.loads(line) for line in record.read_text().splitlines()]
names = ["upload-%A_%a.log", "trace-%A_%a.log", "finalize-metadata-%j.log"]
assert len(submissions) == len(names)
assert f"--array=0-{int(upload_chunks or '8') - 1}" in submissions[0]
assert "--dependency=afterok:12345" in submissions[1]
for args, name in zip(submissions, names, strict=True):
expected = str(submission.resolve()).replace("%", "%%") + "/" + name
assert args[args.index("--output") + 1] == expected
assert args[args.index("--chdir") + 1] == str(scripts.parent.resolve())
def test_competition_resume_uses_original_task_directory(tmp_path):
run_root = tmp_path / "old run" / "run_12345"
run_root.mkdir(parents=True)
record = tmp_path / "commands.jsonl"
capture = (
"import json, os, sys; "
'f=open(os.environ["SAPS_TEST_COMMANDS"], "a"); '
'f.write(json.dumps({"args": sys.argv[1:], '
'"pip_cache": os.environ["PIP_CACHE_DIR"], '
'"virtualenv_cache": os.environ["VIRTUALENV_OVERRIDE_APP_DATA"]})'
'+"\\n"); f.close()'
)
shell_env = tmp_path / "shell-env"
shell_env.write_text(
"poetry() { "
+ shlex.quote(sys.executable)
+ " -c "
+ shlex.quote(capture)
+ ' "$@"; }\n'
)
env = {
**os.environ,
"BASH_ENV": str(shell_env),
"SAPS_TEST_COMMANDS": str(record),
"SAPS_REPO_DIRECTORY": str(ROOT),
"SLURM_ARRAY_JOB_ID": "99999",
"SLURM_ARRAY_TASK_ID": "2",
"SLURM_ARRAY_TASK_COUNT": "5",
"TMPDIR": str(tmp_path / "local scratch"),
}
env.pop("SAPS_COMPETITION_ARGS", None)
subprocess.run(
[
"bash",
str(ROOT / "scripts/run-competition.slurm"),
"--resume",
str(run_root),
],
cwd=tmp_path,
env=env,
check=True,
capture_output=True,
text=True,
)
calls = [json.loads(line) for line in record.read_text().splitlines()]
run, combine = [call["args"] for call in calls]
task_scratch = tmp_path / "local scratch" / "saps-competition-99999-2"
assert task_scratch.is_dir()
assert run[run.index("--env-dir") + 1] == str(task_scratch / "env")
for call in calls:
assert call["pip_cache"] == str(task_scratch / "pip-cache")
assert call["virtualenv_cache"] == str(task_scratch / "virtualenv-cache")
task_directory = str(run_root.resolve() / "task_2")
assert "--resume" in run
assert run[run.index("--saps-dir") + 1] == task_directory
assert run[run.index("--results-dir") + 1] == task_directory + "/results"
assert run[run.index("--machine") + 1] == "run_12345-task-2"
assert combine[:2] == ["run", "./scripts/combine_competition_results.py"]
assert combine[combine.index("--run-directory") + 1] == str(run_root.resolve())
assert "--output" not in combine
for script in (ROOT / "scripts").glob("*.slurm"):
text = script.read_text()
assert "#SBATCH --output=" in text
assert "#SBATCH --output=/dev/null" not in text
assert "exec >" not in text
@pytest.mark.parametrize(
"script_name,commands",
[
(
"run-competition.slurm",
["run_benchmark.py", "combine_competition_results.py"],
),
("upload-dataset.slurm", ["run_benchmark.py"]),
("trace-statistics.slurm", ["run_benchmark.py"]),
("finalize-metadata.slurm", ["merge_statistics.py", "generate_metadata.py"]),
],
)
def test_slurm_submission_from_scripts_directory(tmp_path, script_name, commands):
# Slurm runs a copied script, so its own location cannot identify the repo.
spooled_script = tmp_path / "slurm_script"
shutil.copy(ROOT / "scripts" / script_name, spooled_script)
record = tmp_path / "commands"
shell_env = tmp_path / "shell-env"
shell_env.write_text(
"poetry() {\n"
' [[ "$PWD" == "$SAPS_TEST_ROOT" ]] || return 90\n'
' [[ "$1" == run && -f "$2" ]] || return 91\n'
' if [[ "$SAPS_TEST_SCRIPT" == upload-dataset.slurm ]]; then\n'
' [[ "$3 $4 $5" == "--cache-datasets --chunk-count 5" ]] || return 92\n'
' [[ "$6 $7" == "--chunk-index 0" ]] || return 93\n'
" fi\n"
' printf "%s\\n" "$2" >> "$SAPS_TEST_COMMANDS"\n'
"}\n"
)
trace_dir = tmp_path / "trace"
trace_dir.mkdir()
(trace_dir / "statistics-0.json").write_text("{}")
env = {
**os.environ,
"BASH_ENV": str(shell_env),
"SAPS_TEST_ROOT": str(ROOT),
"SAPS_TEST_COMMANDS": str(record),
"SAPS_TEST_SCRIPT": script_name,
"SLURM_SUBMIT_DIR": str(ROOT / "scripts"),
"SLURM_ARRAY_JOB_ID": "12345",
"SLURM_ARRAY_TASK_ID": "0",
"SLURM_ARRAY_TASK_COUNT": "5",
"SAPS_TRACE_CHUNK_COUNT": "1",
"SAPS_TRACE_OUTPUT_DIR": str(trace_dir),
"TMPDIR": str(tmp_path / "local scratch"),
}
env.pop("SAPS_REPO_DIRECTORY", None)
env.pop("SAPS_COMPETITION_ARGS", None)
subprocess.run(
["bash", str(spooled_script)],
cwd=ROOT / "scripts",
env=env,
check=True,
capture_output=True,
text=True,
)
assert record.read_text().splitlines() == [
f"./scripts/{name}"
if name == "combine_competition_results.py"
else f"./bin/{name}"
for name in commands
]