xref: /aosp_15_r20/external/pytorch/.ci/docker/common/install_conda.sh (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1#!/bin/bash
2
3set -ex
4
5# Optionally install conda
6if [ -n "$ANACONDA_PYTHON_VERSION" ]; then
7  BASE_URL="https://repo.anaconda.com/miniconda"
8  CONDA_FILE="Miniconda3-latest-Linux-x86_64.sh"
9  if [[ $(uname -m) == "aarch64" ]] || [[ "$BUILD_ENVIRONMENT" == *xpu* ]]; then
10    BASE_URL="https://github.com/conda-forge/miniforge/releases/latest/download"
11    CONDA_FILE="Miniforge3-Linux-$(uname -m).sh"
12  fi
13
14  MAJOR_PYTHON_VERSION=$(echo "$ANACONDA_PYTHON_VERSION" | cut -d . -f 1)
15  MINOR_PYTHON_VERSION=$(echo "$ANACONDA_PYTHON_VERSION" | cut -d . -f 2)
16
17  case "$MAJOR_PYTHON_VERSION" in
18    3);;
19    *)
20      echo "Unsupported ANACONDA_PYTHON_VERSION: $ANACONDA_PYTHON_VERSION"
21      exit 1
22      ;;
23  esac
24
25  mkdir -p /opt/conda
26  chown jenkins:jenkins /opt/conda
27
28  source "$(dirname "${BASH_SOURCE[0]}")/common_utils.sh"
29
30  pushd /tmp
31  wget -q "${BASE_URL}/${CONDA_FILE}"
32  # NB: Manually invoke bash per https://github.com/conda/conda/issues/10431
33  as_jenkins bash "${CONDA_FILE}" -b -f -p "/opt/conda"
34  popd
35
36  # NB: Don't do this, rely on the rpath to get it right
37  #echo "/opt/conda/lib" > /etc/ld.so.conf.d/conda-python.conf
38  #ldconfig
39  sed -e 's|PATH="\(.*\)"|PATH="/opt/conda/bin:\1"|g' -i /etc/environment
40  export PATH="/opt/conda/bin:$PATH"
41
42  # Ensure we run conda in a directory that jenkins has write access to
43  pushd /opt/conda
44
45  # Prevent conda from updating to 4.14.0, which causes docker build failures
46  # See https://hud.pytorch.org/pytorch/pytorch/commit/754d7f05b6841e555cea5a4b2c505dd9e0baec1d
47  # Uncomment the below when resolved to track the latest conda update
48  # as_jenkins conda update -y -n base conda
49
50  if [[ $(uname -m) == "aarch64" ]]; then
51    export SYSROOT_DEP="sysroot_linux-aarch64=2.17"
52  else
53    export SYSROOT_DEP="sysroot_linux-64=2.17"
54  fi
55
56  # Install correct Python version
57  # Also ensure sysroot is using a modern GLIBC to match system compilers
58  as_jenkins conda create -n py_$ANACONDA_PYTHON_VERSION -y\
59             python="$ANACONDA_PYTHON_VERSION" \
60             ${SYSROOT_DEP}
61
62  # libstdcxx from conda default channels are too old, we need GLIBCXX_3.4.30
63  # which is provided in libstdcxx 12 and up.
64  conda_install libstdcxx-ng=12.3.0 -c conda-forge
65
66  # Install PyTorch conda deps, as per https://github.com/pytorch/pytorch README
67  if [[ $(uname -m) == "aarch64" ]]; then
68    CONDA_COMMON_DEPS="astunparse pyyaml setuptools openblas==0.3.25=*openmp* ninja==1.11.1 scons==4.5.2"
69
70    if [ "$ANACONDA_PYTHON_VERSION" = "3.8" ]; then
71      NUMPY_VERSION=1.24.4
72    else
73      NUMPY_VERSION=1.26.2
74    fi
75  else
76    CONDA_COMMON_DEPS="astunparse pyyaml mkl=2021.4.0 mkl-include=2021.4.0 setuptools"
77
78    if [ "$ANACONDA_PYTHON_VERSION" = "3.11" ] || [ "$ANACONDA_PYTHON_VERSION" = "3.12" ] || [ "$ANACONDA_PYTHON_VERSION" = "3.13" ]; then
79      NUMPY_VERSION=1.26.0
80    else
81      NUMPY_VERSION=1.21.2
82    fi
83  fi
84  conda_install ${CONDA_COMMON_DEPS}
85
86  # Install llvm-8 as it is required to compile llvmlite-0.30.0 from source
87  # and libpython-static for torch deploy
88  conda_install llvmdev=8.0.0 "libpython-static=${ANACONDA_PYTHON_VERSION}"
89
90  # Use conda cmake in some cases. Conda cmake will be newer than our supported
91  # min version (3.5 for xenial and 3.10 for bionic), so we only do it in those
92  # following builds that we know should use conda. Specifically, Ubuntu bionic
93  # and focal cannot find conda mkl with stock cmake, so we need a cmake from conda
94  if [ -n "${CONDA_CMAKE}" ]; then
95    conda_install cmake
96  fi
97
98  # Magma package names are concatenation of CUDA major and minor ignoring revision
99  # I.e. magma-cuda102 package corresponds to CUDA_VERSION=10.2 and CUDA_VERSION=10.2.89
100  if [ -n "$CUDA_VERSION" ]; then
101    conda_install magma-cuda$(TMP=${CUDA_VERSION/./};echo ${TMP%.*[0-9]}) -c pytorch
102  fi
103
104  # Install some other packages, including those needed for Python test reporting
105  pip_install -r /opt/conda/requirements-ci.txt
106  pip_install numpy=="$NUMPY_VERSION"
107  pip_install -U scikit-learn
108
109  if [ -n "$DOCS" ]; then
110    apt-get update
111    apt-get -y install expect-dev
112
113    # We are currently building docs with python 3.8 (min support version)
114    pip_install -r /opt/conda/requirements-docs.txt
115  fi
116
117  popd
118fi
119