Skip to content

Commit 758becd

Browse files
committed
Fix Slurm job Python environment isolation
1 parent 81e4c77 commit 758becd

3 files changed

Lines changed: 51 additions & 5 deletions

File tree

configs/prefetch-ai-dynamo-wheel.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,31 @@ runtime_wheel_path() {
2929
find "${wheel_dir}" -maxdepth 1 -type f -name "${DYNAMO_RUNTIME_WHEEL_PATTERN}" -print -quit
3030
}
3131

32+
python_with_pip() {
33+
if python3 -m pip --version >/dev/null 2>&1; then
34+
command -v python3
35+
return
36+
fi
37+
38+
if ! command -v uv >/dev/null 2>&1; then
39+
echo "ERROR: python3 does not provide pip, and uv is unavailable to create a pip-seeded prefetch venv" >&2
40+
return 1
41+
fi
42+
43+
local prefetch_venv="${DYNAMO_PREFETCH_VENV:-${wheel_dir}/.prefetch-venv}"
44+
uv venv --seed "${prefetch_venv}" >/dev/null
45+
echo "${prefetch_venv}/bin/python"
46+
}
47+
3248
if [ -f "${wheel_path}" ] && [ -n "$(runtime_wheel_path)" ]; then
3349
echo "ai-dynamo wheels already staged: ${wheel_dir}"
3450
exit 0
3551
fi
3652

3753
download_wheels() {
38-
python3 -m pip download \
54+
local python_bin
55+
python_bin="$(python_with_pip)"
56+
"${python_bin}" -m pip download \
3957
--no-deps \
4058
--pre \
4159
--only-binary=:all: \

src/srtctl/templates/job_script_minimal.j2

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,37 @@ export {{ key }}={{ value }}
8282
echo ""
8383
echo "Preparing srtctl environment..."
8484

85-
# Install uv if not present (single binary, no dependencies)
86-
if ! command -v uv &> /dev/null; then
85+
# SLURM inherits the submitter environment by default. If srtctl was submitted
86+
# from an activated virtualenv, that venv can point at a pipless or wrong-arch
87+
# Python on the compute node. Drop it before selecting Python/uv for the job.
88+
if [ -n "${VIRTUAL_ENV:-}" ]; then
89+
echo "Ignoring inherited virtualenv: ${VIRTUAL_ENV}"
90+
CLEAN_PATH=""
91+
OLD_IFS="${IFS}"
92+
IFS=":"
93+
for PATH_ENTRY in ${PATH}; do
94+
if [ "${PATH_ENTRY}" != "${VIRTUAL_ENV}/bin" ]; then
95+
if [ -z "${CLEAN_PATH}" ]; then
96+
CLEAN_PATH="${PATH_ENTRY}"
97+
else
98+
CLEAN_PATH="${CLEAN_PATH}:${PATH_ENTRY}"
99+
fi
100+
fi
101+
done
102+
IFS="${OLD_IFS}"
103+
export PATH="${CLEAN_PATH}"
104+
unset VIRTUAL_ENV
105+
fi
106+
107+
# Install a job-local uv so inherited submitter binaries cannot shadow the
108+
# compute-node architecture.
109+
UV_BIN_DIR="${OUTPUT_DIR}/uv-bin"
110+
mkdir -p "${UV_BIN_DIR}"
111+
if ! "${UV_BIN_DIR}/uv" --version >/dev/null 2>&1; then
87112
echo "Installing uv package manager..."
88-
curl -LsSf https://astral.sh/uv/install.sh | sh
89-
export PATH="$HOME/.local/bin:$PATH"
113+
curl -LsSf https://astral.sh/uv/install.sh | env XDG_BIN_HOME="${UV_BIN_DIR}" INSTALLER_NO_MODIFY_PATH=1 sh
90114
fi
115+
export PATH="${UV_BIN_DIR}:$PATH"
91116

92117
echo "Using uv with Python 3.12..."
93118

tests/test_configs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,9 @@ def test_sbatch_template_prefetches_dynamo_wheel(self):
596596

597597
assert "export DYNAMO_VERSION=1.2.0.dev20260426" in script
598598
assert "export DYNAMO_WHEEL_NAME=ai_dynamo-1.2.0.dev20260426-py3-none-any.whl" in script
599+
assert "Ignoring inherited virtualenv" in script
600+
assert 'unset VIRTUAL_ENV' in script
601+
assert 'UV_BIN_DIR="${OUTPUT_DIR}/uv-bin"' in script
599602
assert "configs/prefetch-ai-dynamo-wheel.sh" in script
600603

601604
def test_setup_script_env_var_override(self, monkeypatch):

0 commit comments

Comments
 (0)