ARG BASE_IMAGE=ubuntu:24.04
FROM ${BASE_IMAGE} as dev-base
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
build-essential \
ca-certificates \
ccache \
cmake \
curl \
git \
libjpeg-dev \
libpng-dev \
python3 \
python3-pip \
python-is-python3 \
python3-dev && \
rm -rf /var/lib/apt/lists/*
RUN rm -f /usr/lib/python*/EXTERNALLY-MANAGED
RUN /usr/sbin/update-ccache-symlinks
RUN mkdir /opt/ccache && ccache --set-config=cache_dir=/opt/ccache
FROM dev-base as python-deps
COPY requirements.txt requirements-build.txt .
RUN pip3 install --upgrade --ignore-installed pip setuptools wheel && \
pip3 install cmake pyyaml numpy ipython -r requirements.txt
FROM dev-base as submodule-update
WORKDIR /opt/pytorch
COPY . .
RUN git submodule update --init --recursive
FROM scratch as torch-pip-args
COPY <<'EOF' /torch_pip_args.sh
case "${TARGETPLATFORM}" in
"linux/arm64") TORCH_INDEX_ARGS="--extra-index-url https://download.pytorch.org/whl/cpu/" ;;
*) TORCH_INDEX_ARGS="--index-url https://download.pytorch.org/${INSTALL_CHANNEL}/${CUDA_PATH}/" ;;
esac
TORCH_PACKAGES="torch torchvision torchaudio"
if [ "${TARGETPLATFORM}" != "linux/arm64" ] && [ "${CUDA_PATH}" = "cu132" ]; then
TORCH_PACKAGES="torch torchvision"
fi
TORCHVISION_PACKAGES=""
TORCHVISION_INDEX_ARGS=""
CUDA_PATH_NUMBER="${CUDA_PATH#cu}"
case "${CUDA_PATH_NUMBER}" in
""|*[!0-9]*) CUDA_PATH_NUMBER=0 ;;
esac
if [ "${TARGETPLATFORM}" != "linux/arm64" ] && [ "${INSTALL_CHANNEL}" = "whl/test" ] && [ "${CUDA_PATH_NUMBER}" -ge 135 ]; then
TORCH_PACKAGES="torch"
TORCHVISION_PACKAGES="torchvision"
TORCHVISION_INDEX_ARGS="--index-url https://download.pytorch.org/${INSTALL_CHANNEL}/cu134/"
fi
EOF
FROM dev-base as resolve-torch-deps
ARG CUDA_PATH=cu121
ARG INSTALL_CHANNEL=whl/nightly
ARG TARGETPLATFORM
ARG RESOLVE_CACHE_BUST
RUN --mount=type=bind,from=torch-pip-args,source=/torch_pip_args.sh,target=/torch_pip_args.sh <<'EOF'
set -eu
. /torch_pip_args.sh
pip3 install --upgrade --ignore-installed pip
echo "Resolving ${TORCH_PACKAGES} with pip $(pip3 --version | cut -d' ' -f2) from ${TORCH_INDEX_ARGS} (${RESOLVE_CACHE_BUST:-})"
pip3 install --dry-run --ignore-installed --quiet --report /tmp/report.json ${TORCH_INDEX_ARGS} ${TORCH_PACKAGES}
python3 - <<'PY'
import json
import re
TORCH = {"torch", "torchvision", "torchaudio"}
REQUIREMENT = re.compile(
r"\s*([A-Za-z0-9][A-Za-z0-9._-]*)\s*(?:\[[^\]]*\])?\s*"
r"(?:\(([^)]*)\)|([<>=!~][^;]*))?\s*(?:;.*)?$"
)
EXACT = re.compile(r"==\s*[^\s,]+")
def normalize(name):
return re.sub(r"[-_.]+", "-", name).lower()
def exact_pins(requires_dist):
"""Names that a wheel requires with a single `==` specifier."""
for requirement in requires_dist or ():
match = REQUIREMENT.match(requirement)
if match and EXACT.fullmatch((match[2] or match[3] or "").strip()):
yield normalize(match[1])
metadata = {}
for item in json.load(open("/tmp/report.json"))["install"]:
metadata[normalize(item["metadata"]["name"])] = item["metadata"]
pinned, todo = set(), [name for name in metadata if name in TORCH]
while todo:
for dep in exact_pins(metadata[todo.pop()].get("requires_dist")):
if dep in metadata and dep not in TORCH and dep not in pinned:
pinned.add(dep)
todo.append(dep)
locks = {"/torch.lock": [], "/deps-pinned.lock": [], "/deps-floating.lock": []}
for name, meta in metadata.items():
if name in TORCH:
path = "/torch.lock"
elif name in pinned:
path = "/deps-pinned.lock"
else:
path = "/deps-floating.lock"
locks[path].append(f"{meta['name']}=={meta['version']}")
for path, pins in locks.items():
with open(path, "w") as f:
f.write("".join(f"{pin}\n" for pin in sorted(pins, key=str.lower)))
PY
: > /torchvision.lock
if [ -n "${TORCHVISION_PACKAGES}" ]; then
echo "Resolving ${TORCHVISION_PACKAGES} from ${TORCHVISION_INDEX_ARGS}"
pip3 install --dry-run --ignore-installed --quiet --report /tmp/torchvision-report.json ${TORCHVISION_INDEX_ARGS} ${TORCHVISION_PACKAGES}
python3 - <<'PY'
import json
wanted = {"torchvision"}
pins = [
f"{item['metadata']['name']}=={item['metadata']['version']}\n"
for item in json.load(open("/tmp/torchvision-report.json"))["install"]
if item["metadata"]["name"].lower() in wanted
]
open("/torchvision.lock", "w").write("".join(sorted(pins)))
PY
fi
cat /torch.lock /deps-pinned.lock /deps-floating.lock /torchvision.lock
EOF
FROM ${BASE_IMAGE} as official
ARG TARGETPLATFORM
ARG CUDA_VERSION
ARG CUDA_PATH=cu121
ARG INSTALL_CHANNEL=whl/nightly
LABEL com.nvidia.volumes.needed="nvidia_driver"
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
ca-certificates \
libjpeg-dev \
libpng-dev \
python-is-python3 \
python3 \
python3-dev \
python3-pip \
&& rm -rf /var/lib/apt/lists/*
RUN if test -n "${CUDA_VERSION}" -a "${TARGETPLATFORM}" != "linux/arm64"; then \
apt-get update -qq && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends gcc && \
rm -rf /var/lib/apt/lists/*; \
fi
ARG BUILD_TYPE
RUN if [ "${BUILD_TYPE}" = "dev" ] && [ -n "${CUDA_VERSION}" ]; then \
apt-get update && apt-get install -y --no-install-recommends \
wget gnupg2 ca-certificates && \
NVARCH=$(uname -m | sed 's/x86_64/x86_64/' | sed 's/aarch64/sbsa/') && \
wget -qO - https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/${NVARCH}/3bf863cc.pub | apt-key add - && \
echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/${NVARCH} /" > /etc/apt/sources.list.d/cuda.list && \
CUDA_PKG_VERSION=$(echo ${CUDA_VERSION} | cut -d'.' -f1,2 | tr '.' '-') && \
apt-get update && apt-get install -y --no-install-recommends \
cuda-toolkit-${CUDA_PKG_VERSION} && \
apt-get clean && rm -rf /var/lib/apt/lists/* && \
echo "/usr/local/cuda/lib64" >> /etc/ld.so.conf.d/cuda.conf && \
ldconfig; \
fi
RUN --mount=type=bind,source=requirements.txt,target=/tmp/req/requirements.txt \
--mount=type=bind,source=requirements-build.txt,target=/tmp/req/requirements-build.txt \
pip3 install --break-system-packages --no-cache-dir --upgrade --ignore-installed pip setuptools wheel && \
pip3 install --break-system-packages --no-cache-dir cmake pyyaml numpy ipython -r /tmp/req/requirements.txt
RUN --mount=type=bind,from=torch-pip-args,source=/torch_pip_args.sh,target=/torch_pip_args.sh \
--mount=type=bind,from=resolve-torch-deps,source=/deps-pinned.lock,target=/tmp/deps-pinned.lock \
. /torch_pip_args.sh && \
pip3 install --break-system-packages --no-cache-dir --no-deps ${TORCH_INDEX_ARGS} -r /tmp/deps-pinned.lock
RUN --mount=type=bind,from=torch-pip-args,source=/torch_pip_args.sh,target=/torch_pip_args.sh \
--mount=type=bind,from=resolve-torch-deps,source=/deps-floating.lock,target=/tmp/deps-floating.lock \
. /torch_pip_args.sh && \
pip3 install --break-system-packages --no-cache-dir --no-deps ${TORCH_INDEX_ARGS} -r /tmp/deps-floating.lock
RUN --mount=type=bind,from=torch-pip-args,source=/torch_pip_args.sh,target=/torch_pip_args.sh \
--mount=type=bind,from=resolve-torch-deps,source=/torch.lock,target=/tmp/torch.lock \
. /torch_pip_args.sh && \
pip3 install --break-system-packages --no-cache-dir --no-deps ${TORCH_INDEX_ARGS} -r /tmp/torch.lock
RUN --mount=type=bind,from=torch-pip-args,source=/torch_pip_args.sh,target=/torch_pip_args.sh \
--mount=type=bind,from=resolve-torch-deps,source=/torch.lock,target=/tmp/torch.lock \
--mount=type=bind,from=resolve-torch-deps,source=/torchvision.lock,target=/tmp/torchvision.lock \
. /torch_pip_args.sh && \
if [ -s /tmp/torchvision.lock ]; then \
pip3 install --break-system-packages --no-cache-dir ${TORCHVISION_INDEX_ARGS} -r /tmp/torchvision.lock && \
expected=$(grep -i '^torch==' /tmp/torch.lock | cut -d= -f3) && \
installed=$(python3 -c 'import torch; print(torch.__version__)') && \
echo "torch ${installed} (expected ${expected})" && \
test "${installed}" = "${expected}"; \
fi
RUN pip3 install --break-system-packages --no-cache-dir torchelastic
RUN IS_CUDA=$(python3 -c 'import torch ; print(torch.cuda._is_compiled())'); \
echo "Is torch compiled with cuda: ${IS_CUDA}"; \
if test "${IS_CUDA}" != "True" -a ! -z "${CUDA_VERSION}"; then \
exit 1; \
fi
ARG PYTORCH_VERSION
ARG TRITON_VERSION
ENV NVIDIA_VISIBLE_DEVICES all
ENV NVIDIA_DRIVER_CAPABILITIES compute,utility
ENV LD_LIBRARY_PATH /usr/local/nvidia/lib:/usr/local/nvidia/lib64
ENV PATH /usr/local/nvidia/bin:/usr/local/cuda/bin:$PATH
ENV PYTORCH_VERSION ${PYTORCH_VERSION}
WORKDIR /workspace
FROM official as dev
ENV PATH=/usr/local/cuda/bin:${PATH}
ENV LD_LIBRARY_PATH=/usr/local/cuda/lib64:${LD_LIBRARY_PATH}
ENV CUDA_HOME=/usr/local/cuda
COPY --from=python-deps /usr/local/lib/python3.12 /usr/local/lib/python3.12
COPY --from=python-deps /usr/local/bin /usr/local/bin
COPY --from=submodule-update /opt/pytorch /opt/pytorch