xref: /aosp_15_r20/external/pytorch/Dockerfile (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Worker# syntax=docker/dockerfile:1
2*da0073e9SAndroid Build Coastguard Worker
3*da0073e9SAndroid Build Coastguard Worker# NOTE: Building this image require's docker version >= 23.0.
4*da0073e9SAndroid Build Coastguard Worker#
5*da0073e9SAndroid Build Coastguard Worker# For reference:
6*da0073e9SAndroid Build Coastguard Worker# - https://docs.docker.com/build/dockerfile/frontend/#stable-channel
7*da0073e9SAndroid Build Coastguard Worker
8*da0073e9SAndroid Build Coastguard WorkerARG BASE_IMAGE=ubuntu:22.04
9*da0073e9SAndroid Build Coastguard WorkerARG PYTHON_VERSION=3.11
10*da0073e9SAndroid Build Coastguard Worker
11*da0073e9SAndroid Build Coastguard WorkerFROM ${BASE_IMAGE} as dev-base
12*da0073e9SAndroid Build Coastguard WorkerRUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
13*da0073e9SAndroid Build Coastguard Worker        build-essential \
14*da0073e9SAndroid Build Coastguard Worker        ca-certificates \
15*da0073e9SAndroid Build Coastguard Worker        ccache \
16*da0073e9SAndroid Build Coastguard Worker        cmake \
17*da0073e9SAndroid Build Coastguard Worker        curl \
18*da0073e9SAndroid Build Coastguard Worker        git \
19*da0073e9SAndroid Build Coastguard Worker        libjpeg-dev \
20*da0073e9SAndroid Build Coastguard Worker        libpng-dev && \
21*da0073e9SAndroid Build Coastguard Worker    rm -rf /var/lib/apt/lists/*
22*da0073e9SAndroid Build Coastguard WorkerRUN /usr/sbin/update-ccache-symlinks
23*da0073e9SAndroid Build Coastguard WorkerRUN mkdir /opt/ccache && ccache --set-config=cache_dir=/opt/ccache
24*da0073e9SAndroid Build Coastguard WorkerENV PATH /opt/conda/bin:$PATH
25*da0073e9SAndroid Build Coastguard Worker
26*da0073e9SAndroid Build Coastguard WorkerFROM dev-base as conda
27*da0073e9SAndroid Build Coastguard WorkerARG PYTHON_VERSION=3.11
28*da0073e9SAndroid Build Coastguard Worker# Automatically set by buildx
29*da0073e9SAndroid Build Coastguard WorkerARG TARGETPLATFORM
30*da0073e9SAndroid Build Coastguard Worker# translating Docker's TARGETPLATFORM into miniconda arches
31*da0073e9SAndroid Build Coastguard WorkerRUN case ${TARGETPLATFORM} in \
32*da0073e9SAndroid Build Coastguard Worker         "linux/arm64")  MINICONDA_ARCH=aarch64  ;; \
33*da0073e9SAndroid Build Coastguard Worker         *)              MINICONDA_ARCH=x86_64   ;; \
34*da0073e9SAndroid Build Coastguard Worker    esac && \
35*da0073e9SAndroid Build Coastguard Worker    curl -fsSL -v -o ~/miniconda.sh -O  "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-${MINICONDA_ARCH}.sh"
36*da0073e9SAndroid Build Coastguard WorkerCOPY requirements.txt .
37*da0073e9SAndroid Build Coastguard Worker# Manually invoke bash on miniconda script per https://github.com/conda/conda/issues/10431
38*da0073e9SAndroid Build Coastguard WorkerRUN chmod +x ~/miniconda.sh && \
39*da0073e9SAndroid Build Coastguard Worker    bash ~/miniconda.sh -b -p /opt/conda && \
40*da0073e9SAndroid Build Coastguard Worker    rm ~/miniconda.sh && \
41*da0073e9SAndroid Build Coastguard Worker    /opt/conda/bin/conda install -y python=${PYTHON_VERSION} cmake conda-build pyyaml numpy ipython && \
42*da0073e9SAndroid Build Coastguard Worker    /opt/conda/bin/python -mpip install -r requirements.txt && \
43*da0073e9SAndroid Build Coastguard Worker    /opt/conda/bin/conda clean -ya
44*da0073e9SAndroid Build Coastguard Worker
45*da0073e9SAndroid Build Coastguard WorkerFROM dev-base as submodule-update
46*da0073e9SAndroid Build Coastguard WorkerWORKDIR /opt/pytorch
47*da0073e9SAndroid Build Coastguard WorkerCOPY . .
48*da0073e9SAndroid Build Coastguard WorkerRUN git submodule update --init --recursive
49*da0073e9SAndroid Build Coastguard Worker
50*da0073e9SAndroid Build Coastguard WorkerFROM conda as build
51*da0073e9SAndroid Build Coastguard WorkerARG CMAKE_VARS
52*da0073e9SAndroid Build Coastguard WorkerWORKDIR /opt/pytorch
53*da0073e9SAndroid Build Coastguard WorkerCOPY --from=conda /opt/conda /opt/conda
54*da0073e9SAndroid Build Coastguard WorkerCOPY --from=submodule-update /opt/pytorch /opt/pytorch
55*da0073e9SAndroid Build Coastguard WorkerRUN make triton
56*da0073e9SAndroid Build Coastguard WorkerRUN --mount=type=cache,target=/opt/ccache \
57*da0073e9SAndroid Build Coastguard Worker    export eval ${CMAKE_VARS} && \
58*da0073e9SAndroid Build Coastguard Worker    TORCH_CUDA_ARCH_LIST="7.0 7.2 7.5 8.0 8.6 8.7 8.9 9.0 9.0a" TORCH_NVCC_FLAGS="-Xfatbin -compress-all" \
59*da0073e9SAndroid Build Coastguard Worker    CMAKE_PREFIX_PATH="$(dirname $(which conda))/../" \
60*da0073e9SAndroid Build Coastguard Worker    python setup.py install
61*da0073e9SAndroid Build Coastguard Worker
62*da0073e9SAndroid Build Coastguard WorkerFROM conda as conda-installs
63*da0073e9SAndroid Build Coastguard WorkerARG PYTHON_VERSION=3.11
64*da0073e9SAndroid Build Coastguard WorkerARG CUDA_PATH=cu121
65*da0073e9SAndroid Build Coastguard WorkerARG CUDA_CHANNEL=nvidia
66*da0073e9SAndroid Build Coastguard WorkerARG INSTALL_CHANNEL=whl/nightly
67*da0073e9SAndroid Build Coastguard Worker# Automatically set by buildx
68*da0073e9SAndroid Build Coastguard WorkerRUN /opt/conda/bin/conda update -y -n base -c defaults conda
69*da0073e9SAndroid Build Coastguard WorkerRUN /opt/conda/bin/conda install -y python=${PYTHON_VERSION}
70*da0073e9SAndroid Build Coastguard Worker
71*da0073e9SAndroid Build Coastguard WorkerARG TARGETPLATFORM
72*da0073e9SAndroid Build Coastguard Worker
73*da0073e9SAndroid Build Coastguard Worker# INSTALL_CHANNEL whl - release, whl/nightly - nightly, whle/test - test channels
74*da0073e9SAndroid Build Coastguard WorkerRUN case ${TARGETPLATFORM} in \
75*da0073e9SAndroid Build Coastguard Worker         "linux/arm64")  pip install --extra-index-url https://download.pytorch.org/whl/cpu/ torch torchvision torchaudio ;; \
76*da0073e9SAndroid Build Coastguard Worker         *)              pip install --index-url https://download.pytorch.org/${INSTALL_CHANNEL}/${CUDA_PATH#.}/ torch torchvision torchaudio ;; \
77*da0073e9SAndroid Build Coastguard Worker    esac && \
78*da0073e9SAndroid Build Coastguard Worker    /opt/conda/bin/conda clean -ya
79*da0073e9SAndroid Build Coastguard WorkerRUN /opt/conda/bin/pip install torchelastic
80*da0073e9SAndroid Build Coastguard WorkerRUN IS_CUDA=$(python -c 'import torch ; print(torch.cuda._is_compiled())'); \
81*da0073e9SAndroid Build Coastguard Worker    echo "Is torch compiled with cuda: ${IS_CUDA}"; \
82*da0073e9SAndroid Build Coastguard Worker    if test "${IS_CUDA}" != "True" -a ! -z "${CUDA_VERSION}"; then \
83*da0073e9SAndroid Build Coastguard Worker        exit 1; \
84*da0073e9SAndroid Build Coastguard Worker    fi
85*da0073e9SAndroid Build Coastguard Worker
86*da0073e9SAndroid Build Coastguard WorkerFROM ${BASE_IMAGE} as official
87*da0073e9SAndroid Build Coastguard WorkerARG PYTORCH_VERSION
88*da0073e9SAndroid Build Coastguard WorkerARG TRITON_VERSION
89*da0073e9SAndroid Build Coastguard WorkerARG TARGETPLATFORM
90*da0073e9SAndroid Build Coastguard WorkerARG CUDA_VERSION
91*da0073e9SAndroid Build Coastguard WorkerLABEL com.nvidia.volumes.needed="nvidia_driver"
92*da0073e9SAndroid Build Coastguard WorkerRUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
93*da0073e9SAndroid Build Coastguard Worker        ca-certificates \
94*da0073e9SAndroid Build Coastguard Worker        libjpeg-dev \
95*da0073e9SAndroid Build Coastguard Worker        libpng-dev \
96*da0073e9SAndroid Build Coastguard Worker        && rm -rf /var/lib/apt/lists/*
97*da0073e9SAndroid Build Coastguard WorkerCOPY --from=conda-installs /opt/conda /opt/conda
98*da0073e9SAndroid Build Coastguard WorkerRUN if test -n "${TRITON_VERSION}" -a "${TARGETPLATFORM}" != "linux/arm64"; then \
99*da0073e9SAndroid Build Coastguard Worker        DEBIAN_FRONTEND=noninteractive apt install -y --no-install-recommends gcc; \
100*da0073e9SAndroid Build Coastguard Worker        rm -rf /var/lib/apt/lists/*; \
101*da0073e9SAndroid Build Coastguard Worker    fi
102*da0073e9SAndroid Build Coastguard WorkerENV PATH /opt/conda/bin:$PATH
103*da0073e9SAndroid Build Coastguard WorkerENV NVIDIA_VISIBLE_DEVICES all
104*da0073e9SAndroid Build Coastguard WorkerENV NVIDIA_DRIVER_CAPABILITIES compute,utility
105*da0073e9SAndroid Build Coastguard WorkerENV LD_LIBRARY_PATH /usr/local/nvidia/lib:/usr/local/nvidia/lib64
106*da0073e9SAndroid Build Coastguard WorkerENV PATH /usr/local/nvidia/bin:/usr/local/cuda/bin:$PATH
107*da0073e9SAndroid Build Coastguard WorkerENV PYTORCH_VERSION ${PYTORCH_VERSION}
108*da0073e9SAndroid Build Coastguard WorkerWORKDIR /workspace
109*da0073e9SAndroid Build Coastguard Worker
110*da0073e9SAndroid Build Coastguard WorkerFROM official as dev
111*da0073e9SAndroid Build Coastguard Worker# Should override the already installed version from the official-image stage
112*da0073e9SAndroid Build Coastguard WorkerCOPY --from=build /opt/conda /opt/conda
113