Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions autofit/campaign/scheduler/slurm.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,23 @@ def _cache_exports(cache_root: str) -> List[str]:
Fill-if-unset exports sending temp files and caches under ``cache_root``.

A value already in the job's environment (for example from the submitting shell)
wins. ``JAX_COMPILATION_CACHE_DIR`` uses ``${VAR-...}`` so an explicitly empty value
still disables the JAX cache. A stage's own ``env`` is exported afterwards and so
overrides these.
wins, with one exception: ``TMPDIR=/tmp`` is replaced too, because Slurm sets
``TMPDIR`` to its ``TmpFS`` (``/tmp``) itself when the submit environment has none,
so a plain fill-if-unset would never fire. ``JAX_COMPILATION_CACHE_DIR`` uses
``${VAR-...}`` so an explicitly empty value still disables the JAX cache. A stage's
own ``env`` is exported afterwards and so overrides these.
"""
root = cache_root.rstrip("/") or "/"
lines = []
for key, relative in CACHE_DIRS:
operator = "-" if key == "JAX_COMPILATION_CACHE_DIR" else ":-"
# The default sits inside "${...}", so escape for a double-quoted context.
# The default sits inside double quotes, so escape for that context.
path = re.sub(r'([\\"$`])', r"\\\1", f"{root}/{relative}")
if key == "TMPDIR":
lines.append(
f'if [ -z "${{TMPDIR:-}}" ] || [ "$TMPDIR" = /tmp ]; then export TMPDIR="{path}"; fi'
)
continue
operator = "-" if key == "JAX_COMPILATION_CACHE_DIR" else ":-"
lines.append(f'export {key}="${{{key}{operator}{path}}}"')
lines.append('mkdir -p "$TMPDIR" "$XDG_CACHE_HOME" 2>/dev/null || true')
return lines
Expand Down
9 changes: 6 additions & 3 deletions test_autofit/campaign/test_slurm.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_sbatch_script_cache_root_exports_fill_if_unset():
request.cache_root = "/mnt/ral/x/"
lines = sbatch_script(request).splitlines()
for line in [
'export TMPDIR="${TMPDIR:-/mnt/ral/x/tmp}"',
'if [ -z "${TMPDIR:-}" ] || [ "$TMPDIR" = /tmp ]; then export TMPDIR="/mnt/ral/x/tmp"; fi',
'export XDG_CACHE_HOME="${XDG_CACHE_HOME:-/mnt/ral/x/.cache}"',
'export NUMBA_CACHE_DIR="${NUMBA_CACHE_DIR:-/mnt/ral/x/.cache/numba}"',
# "-" not ":-": an explicitly empty value still disables the JAX cache.
Expand All @@ -98,8 +98,8 @@ def test_sbatch_script_cache_root_exports_fill_if_unset():
]:
assert line in lines, line
# After `set -euo pipefail` and before the stage env, so a stage env value wins.
assert lines.index("set -euo pipefail") < lines.index('export TMPDIR="${TMPDIR:-/mnt/ral/x/tmp}"')
assert lines.index('export TMPDIR="${TMPDIR:-/mnt/ral/x/tmp}"') < lines.index("export NAME='a b'")
assert lines.index("set -euo pipefail") < lines.index('if [ -z "${TMPDIR:-}" ] || [ "$TMPDIR" = /tmp ]; then export TMPDIR="/mnt/ral/x/tmp"; fi')
assert lines.index('if [ -z "${TMPDIR:-}" ] || [ "$TMPDIR" = /tmp ]; then export TMPDIR="/mnt/ral/x/tmp"; fi') < lines.index("export NAME='a b'")


def _cache_env(tmp_path, cache_root, environment):
Expand Down Expand Up @@ -130,6 +130,9 @@ def test_sbatch_script_cache_block_runs_under_bash(tmp_path):
tmp_path, root, {"PATH": "/usr/bin:/bin", "TMPDIR": "/scratch", "JAX_COMPILATION_CACHE_DIR": ""}
)
assert (tmpdir, jax) == ("/scratch", "")
# Slurm sets TMPDIR=/tmp itself when the submit environment has none: replace it.
tmpdir, _ = _cache_env(tmp_path, root, {"PATH": "/usr/bin:/bin", "TMPDIR": "/tmp"})
assert tmpdir == f"{root}/tmp"


def _run_generated_script(tmp_path, fit_body, signal_usr1):
Expand Down
3 changes: 2 additions & 1 deletion test_autofit/campaign/test_submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ def test_dry_run_carries_cache_root(tmp_path):

path = write_campaign(tmp_path / "p", CAMPAIGN.replace("site: ral\n", "site: ral\ncache_root: /mnt/ral/x\n"))
entries = submit(Campaign.load(path), None, dry_run=True)
assert 'export TMPDIR="${TMPDIR:-/mnt/ral/x/tmp}"' in entries[0]["script"].splitlines()
assert 'XDG_CACHE_HOME="${XDG_CACHE_HOME:-/mnt/ral/x/.cache}"' in entries[0]["script"]
assert 'export TMPDIR="/mnt/ral/x/tmp"' in entries[0]["script"]
Loading