pkg_helpers.bash 10.1 KB
Newer Older
1
2
3
4
# A set of useful bash functions for common functionality we need to do in
# many build scripts


Edward Z. Yang's avatar
Edward Z. Yang committed
5
6
7
8
9
# Setup CUDA environment variables, based on CU_VERSION
#
# Inputs:
#   CU_VERSION (cpu, cu92, cu100)
#   NO_CUDA_PACKAGE (bool)
10
#   BUILD_TYPE (conda, wheel)
Edward Z. Yang's avatar
Edward Z. Yang committed
11
12
13
14
15
16
#
# Outputs:
#   VERSION_SUFFIX (e.g., "")
#   PYTORCH_VERSION_SUFFIX (e.g., +cpu)
#   WHEEL_DIR (e.g., cu100/)
#   CUDA_HOME (e.g., /usr/local/cuda-9.2, respected by torch.utils.cpp_extension)
17
18
#   USE_CUDA (respected by torchaudio setup.py)
#   NVCC_FLAGS (respected by torchaudio setup.py)
Edward Z. Yang's avatar
Edward Z. Yang committed
19
20
21
22
23
24
25
26
27
28
29
30
#
# Precondition: CUDA versions are installed in their conventional locations in
# /usr/local/cuda-*
#
# NOTE: Why VERSION_SUFFIX versus PYTORCH_VERSION_SUFFIX?  If you're building
# a package with CUDA on a platform we support CUDA on, VERSION_SUFFIX ==
# PYTORCH_VERSION_SUFFIX and everyone is happy.  However, if you are building a
# package with only CPU bits (e.g., torchaudio), then VERSION_SUFFIX is always
# empty, but PYTORCH_VERSION_SUFFIX is +cpu (because that's how you get a CPU
# version of a Python package.  But that doesn't apply if you're on OS X,
# since the default CU_VERSION on OS X is cpu.
setup_cuda() {
31
32
33
34

  # First, compute version suffixes.  By default, assume no version suffixes
  export VERSION_SUFFIX=""
  export PYTORCH_VERSION_SUFFIX=""
35
  export WHEEL_DIR="cpu/"
36
37
  # Wheel builds need suffixes (but not if they're on OS X, which never has suffix)
  if [[ "$BUILD_TYPE" == "wheel" ]] && [[ "$(uname)" != Darwin ]]; then
38
    export PYTORCH_VERSION_SUFFIX="+$CU_VERSION"
39
40
41
42
    # Match the suffix scheme of pytorch, unless this package does not have
    # CUDA builds (in which case, use default)
    if [[ -z "$NO_CUDA_PACKAGE" ]]; then
      export VERSION_SUFFIX="$PYTORCH_VERSION_SUFFIX"
43
      export WHEEL_DIR="$CU_VERSION/"
Edward Z. Yang's avatar
Edward Z. Yang committed
44
    fi
45
  fi
46
47
48

  # Now work out the CUDA settings
  case "$CU_VERSION" in
49
50
51
52
53
54
55
56
    cu113)
      if [[ "$OSTYPE" == "msys" ]]; then
        export CUDA_HOME="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.3"
      else
        export CUDA_HOME=/usr/local/cuda-11.3/
      fi
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0;7.5;8.0;8.6"
      ;;
Nikita Shulga's avatar
Nikita Shulga committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    cu112)
      if [[ "$OSTYPE" == "msys" ]]; then
        export CUDA_HOME="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.2"
      else
        export CUDA_HOME=/usr/local/cuda-11.2/
      fi
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0;7.5;8.0;8.6"
      ;;
    cu111)
      if [[ "$OSTYPE" == "msys" ]]; then
        export CUDA_HOME="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.1"
      else
        export CUDA_HOME=/usr/local/cuda-11.1/
      fi
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0;7.5;8.0;8.6"
      ;;
    cu110)
      if [[ "$OSTYPE" == "msys" ]]; then
        export CUDA_HOME="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.0"
      else
        export CUDA_HOME=/usr/local/cuda-11.0/
      fi
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0;7.5;8.0"
      ;;
    cu102)
      if [[ "$OSTYPE" == "msys" ]]; then
        export CUDA_HOME="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v10.2"
      else
        export CUDA_HOME=/usr/local/cuda-10.2/
      fi
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0;7.5"
      ;;
    cu101)
      if [[ "$OSTYPE" == "msys" ]]; then
        export CUDA_HOME="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v10.1"
      else
        export CUDA_HOME=/usr/local/cuda-10.1/
      fi
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0;7.5"
      ;;
97
98
    cu100)
      export CUDA_HOME=/usr/local/cuda-10.0/
Nikita Shulga's avatar
Nikita Shulga committed
99
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0;7.5"
100
101
102
      ;;
    cu92)
      export CUDA_HOME=/usr/local/cuda-9.2/
Nikita Shulga's avatar
Nikita Shulga committed
103
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0"
104
      ;;
105
106
107
    rocm*)
      export USE_ROCM=1
      ;;
108
109
110
111
112
113
114
    cpu)
      ;;
    *)
      echo "Unrecognized CU_VERSION=$CU_VERSION"
      exit 1
      ;;
  esac
115
116
117
  if [[ -n "$CUDA_HOME" ]]; then
    # Adds nvcc binary to the search path so that CMake's `find_package(CUDA)` will pick the right one
    export PATH="$CUDA_HOME/bin:$PATH"
118
    export USE_CUDA=1
119
  fi
120
121
}

Edward Z. Yang's avatar
Edward Z. Yang committed
122
123
124
125
# Populate build version if necessary, and add version suffix
#
# Inputs:
#   BUILD_VERSION (e.g., 0.2.0 or empty)
126
#   VERSION_SUFFIX (e.g., +cpu)
Edward Z. Yang's avatar
Edward Z. Yang committed
127
128
129
130
131
132
#
# Outputs:
#   BUILD_VERSION (e.g., 0.2.0.dev20190807+cpu)
#
# Fill BUILD_VERSION if it doesn't exist already with a nightly string
# Usage: setup_build_version 0.2.0
133
134
setup_build_version() {
  if [[ -z "$BUILD_VERSION" ]]; then
Edward Z. Yang's avatar
Edward Z. Yang committed
135
136
137
    export BUILD_VERSION="$1.dev$(date "+%Y%m%d")$VERSION_SUFFIX"
  else
    export BUILD_VERSION="$BUILD_VERSION$VERSION_SUFFIX"
138
139
140
141
142
143
144
145
146
147
  fi
}

# Set some useful variables for OS X, if applicable
setup_macos() {
  if [[ "$(uname)" == Darwin ]]; then
    export MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++
  fi
}

Edward Z. Yang's avatar
Edward Z. Yang committed
148
149
150
151
# Top-level entry point for things every package will need to do
#
# Usage: setup_env 0.2.0
setup_env() {
152
  git submodule update --init --recursive
Edward Z. Yang's avatar
Edward Z. Yang committed
153
154
155
156
157
  setup_cuda
  setup_build_version "$1"
  setup_macos
}

158
159
160
161
162
# Function to retry functions that sometimes timeout or have flaky failures
retry () {
    $*  || (sleep 1 && $*) || (sleep 2 && $*) || (sleep 4 && $*) || (sleep 8 && $*)
}

Edward Z. Yang's avatar
Edward Z. Yang committed
163
164
165
166
167
168
169
170
171
# Inputs:
#   PYTHON_VERSION (2.7, 3.5, 3.6, 3.7)
#   UNICODE_ABI (bool)
#
# Outputs:
#   PATH modified to put correct Python version in PATH
#
# Precondition: If Linux, you are in a soumith/manylinux-cuda* Docker image
setup_wheel_python() {
peterjc123's avatar
peterjc123 committed
172
  if [[ "$(uname)" == Darwin || "$OSTYPE" == "msys" ]]; then
Edward Z. Yang's avatar
Edward Z. Yang committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    eval "$(conda shell.bash hook)"
    conda env remove -n "env$PYTHON_VERSION" || true
    conda create -yn "env$PYTHON_VERSION" python="$PYTHON_VERSION"
    conda activate "env$PYTHON_VERSION"
  else
    case "$PYTHON_VERSION" in
      2.7)
        if [[ -n "$UNICODE_ABI" ]]; then
          python_abi=cp27-cp27mu
        else
          python_abi=cp27-cp27m
        fi
        ;;
      3.5) python_abi=cp35-cp35m ;;
      3.6) python_abi=cp36-cp36m ;;
      3.7) python_abi=cp37-cp37m ;;
189
      3.8) python_abi=cp38-cp38 ;;
Eli Uriegas's avatar
Eli Uriegas committed
190
      3.9) python_abi=cp39-cp39 ;;
Edward Z. Yang's avatar
Edward Z. Yang committed
191
192
193
194
195
196
197
198
199
      *)
        echo "Unrecognized PYTHON_VERSION=$PYTHON_VERSION"
        exit 1
        ;;
    esac
    export PATH="/opt/python/$python_abi/bin:$PATH"
  fi
}

200
201
202
203
204
205
206
207
208
# Install with pip a bit more robustly than the default
pip_install() {
  retry pip install --progress-bar off "$@"
}

# Install torch with pip, respecting PYTORCH_VERSION, and record the installed
# version into PYTORCH_VERSION, if applicable
setup_pip_pytorch_version() {
  if [[ -z "$PYTORCH_VERSION" ]]; then
Edward Z. Yang's avatar
Edward Z. Yang committed
209
210
211
    # Install latest prerelease version of torch, per our nightlies, consistent
    # with the requested cuda version
    pip_install --pre torch -f "https://download.pytorch.org/whl/nightly/${WHEEL_DIR}torch_nightly.html"
212
213
    # CUDA and CPU are ABI compatible on the CPU-only parts, so strip in this case
    export PYTORCH_VERSION="$(pip show torch | grep ^Version: | sed 's/Version:  *//' | sed 's/+.\+//')"
214
  else
215
    pip_install "torch==$PYTORCH_VERSION$PYTORCH_VERSION_SUFFIX" \
Edward Z. Yang's avatar
Edward Z. Yang committed
216
      -f https://download.pytorch.org/whl/torch_stable.html \
217
      -f "https://download.pytorch.org/whl/${UPLOAD_CHANNEL}/torch_${UPLOAD_CHANNEL}.html"
218
219
220
221
222
223
  fi
}

# Fill PYTORCH_VERSION with the latest conda nightly version, and
# CONDA_CHANNEL_FLAGS with appropriate flags to retrieve these versions
#
224
# You MUST have populated PYTORCH_VERSION_SUFFIX before hand.
225
setup_conda_pytorch_constraint() {
Eli Uriegas's avatar
Eli Uriegas committed
226
  CONDA_CHANNEL_FLAGS="${CONDA_CHANNEL_FLAGS}"
227
  if [[ -z "$PYTORCH_VERSION" ]]; then
Eli Uriegas's avatar
Eli Uriegas committed
228
    export CONDA_CHANNEL_FLAGS="${CONDA_CHANNEL_FLAGS} -c pytorch-nightly"
229
230
    export PYTORCH_VERSION="$(conda search --json 'pytorch[channel=pytorch-nightly]' | python -c "import sys, json, re; print(re.sub(r'\\+.*$', '', json.load(sys.stdin)['pytorch'][-1]['version']))")"
  else
Eli Uriegas's avatar
Eli Uriegas committed
231
232
    export CONDA_CHANNEL_FLAGS="${CONDA_CHANNEL_FLAGS} -c pytorch -c pytorch-test -c pytorch-nightly"
  fi
Edward Z. Yang's avatar
Edward Z. Yang committed
233
  if [[ "$CU_VERSION" == cpu ]]; then
Edward Z. Yang's avatar
Edward Z. Yang committed
234
    export CONDA_PYTORCH_BUILD_CONSTRAINT="- pytorch==$PYTORCH_VERSION${PYTORCH_VERSION_SUFFIX}"
235
236
    export CONDA_PYTORCH_CONSTRAINT="- pytorch==$PYTORCH_VERSION"
  else
Edward Z. Yang's avatar
Edward Z. Yang committed
237
238
    export CONDA_PYTORCH_BUILD_CONSTRAINT="- pytorch==${PYTORCH_VERSION}${PYTORCH_VERSION_SUFFIX}"
    export CONDA_PYTORCH_CONSTRAINT="- pytorch==${PYTORCH_VERSION}${PYTORCH_VERSION_SUFFIX}"
239
  fi
Nikita Shulga's avatar
Nikita Shulga committed
240
241
242
243
244
  # TODO: Remove me later, see https://github.com/pytorch/pytorch/issues/62424 for more details
  if [[ "$(uname)" == Darwin ]]; then
    # Use less than equal to avoid version conflict in python=3.6 environment
    export CONDA_EXTRA_BUILD_CONSTRAINT="- mkl<=2021.2.0"
  fi
245
246
247
248
}

# Translate CUDA_VERSION into CUDA_CUDATOOLKIT_CONSTRAINT
setup_conda_cudatoolkit_constraint() {
249
  export CONDA_BUILD_VARIANT="cuda"
Edward Z. Yang's avatar
Edward Z. Yang committed
250
  if [[ "$(uname)" == Darwin ]]; then
251
    export CONDA_BUILD_VARIANT="cpu"
252
  else
Edward Z. Yang's avatar
Edward Z. Yang committed
253
    case "$CU_VERSION" in
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
      cu113)
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=11.3,<11.4 # [not osx]"
        ;;
      cu112)
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=11.2,<11.3 # [not osx]"
        ;;
      cu111)
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=11.1,<11.2 # [not osx]"
        ;;
      cu110)
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=11.0,<11.1 # [not osx]"
        ;;
      cu102)
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=10.2,<10.3 # [not osx]"
        ;;
      cu101)
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=10.1,<10.2 # [not osx]"
        ;;
Edward Z. Yang's avatar
Edward Z. Yang committed
272
      cu100)
Edward Z. Yang's avatar
Edward Z. Yang committed
273
274
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=10.0,<10.1 # [not osx]"
        ;;
Edward Z. Yang's avatar
Edward Z. Yang committed
275
      cu92)
Edward Z. Yang's avatar
Edward Z. Yang committed
276
277
278
279
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=9.2,<9.3 # [not osx]"
        ;;
      cpu)
        export CONDA_CUDATOOLKIT_CONSTRAINT=""
280
        export CONDA_BUILD_VARIANT="cpu"
Edward Z. Yang's avatar
Edward Z. Yang committed
281
        ;;
Edward Z. Yang's avatar
Edward Z. Yang committed
282
283
284
285
      *)
        echo "Unrecognized CU_VERSION=$CU_VERSION"
        exit 1
        ;;
Edward Z. Yang's avatar
Edward Z. Yang committed
286
    esac
287
288
  fi
}
289
290
291
292
293
294
295
296
297
298

# Build the proper compiler package before building the final package
setup_visual_studio_constraint() {
  if [[ "$OSTYPE" == "msys" ]]; then
      export VSTOOLCHAIN_PACKAGE=vs2019
      export VSDEVCMD_ARGS=''
      conda build $CONDA_CHANNEL_FLAGS --no-anaconda-upload packaging/$VSTOOLCHAIN_PACKAGE
      cp packaging/$VSTOOLCHAIN_PACKAGE/conda_build_config.yaml packaging/torchaudio/conda_build_config.yaml
  fi
}