pkg_helpers.bash 14.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
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#
# 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)
#   FORCE_CUDA (respected by torchvision setup.py)
#   NVCC_FLAGS (respected by torchvision setup.py)
#
# 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
35
36
37

  # First, compute version suffixes.  By default, assume no version suffixes
  export VERSION_SUFFIX=""
  export PYTORCH_VERSION_SUFFIX=""
  export WHEEL_DIR=""
  # 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
    cu115)
      if [[ "$OSTYPE" == "msys" ]]; then
        export CUDA_HOME="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.5"
      else
        export CUDA_HOME=/usr/local/cuda-11.5/
      fi
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0;7.5;8.0;8.6"
      ;;
57
58
59
60
61
62
63
64
    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"
      ;;
65
66
67
68
69
70
    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
71
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0;7.5;8.0;8.6"
72
      ;;
73
74
75
76
77
78
    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
79
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0;7.5;8.0;8.6"
80
      ;;
peterjc123's avatar
peterjc123 committed
81
82
83
84
85
86
    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
87
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0;7.5;8.0"
peterjc123's avatar
peterjc123 committed
88
      ;;
89
90
91
92
93
94
    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
95
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0;7.5"
96
      ;;
Francisco Massa's avatar
Francisco Massa committed
97
    cu101)
98
99
100
101
102
      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
103
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0;7.5"
Francisco Massa's avatar
Francisco Massa committed
104
      ;;
105
    cu100)
106
107
108
109
110
      if [[ "$OSTYPE" == "msys" ]]; then
        export CUDA_HOME="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v10.0"
      else
        export CUDA_HOME=/usr/local/cuda-10.0/
      fi
111
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0;7.5"
112
113
      ;;
    cu92)
114
115
116
117
118
      if [[ "$OSTYPE" == "msys" ]]; then
        export CUDA_HOME="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v9.2"
      else
        export CUDA_HOME=/usr/local/cuda-9.2/
      fi
119
      export TORCH_CUDA_ARCH_LIST="3.5;5.0+PTX;6.0;7.0"
120
121
122
      ;;
    cpu)
      ;;
Jeff Daily's avatar
Jeff Daily committed
123
124
125
    rocm*)
      export FORCE_CUDA=1
      ;;
126
127
128
129
130
    *)
      echo "Unrecognized CU_VERSION=$CU_VERSION"
      exit 1
      ;;
  esac
131
132
133
134
135
  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"
    export FORCE_CUDA=1
  fi
136
137
}

Edward Z. Yang's avatar
Edward Z. Yang committed
138
139
140
141
# Populate build version if necessary, and add version suffix
#
# Inputs:
#   BUILD_VERSION (e.g., 0.2.0 or empty)
142
#   VERSION_SUFFIX (e.g., +cpu)
Edward Z. Yang's avatar
Edward Z. Yang committed
143
144
145
146
147
148
#
# 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
149
150
setup_build_version() {
  if [[ -z "$BUILD_VERSION" ]]; then
Edward Z. Yang's avatar
Edward Z. Yang committed
151
152
153
    export BUILD_VERSION="$1.dev$(date "+%Y%m%d")$VERSION_SUFFIX"
  else
    export BUILD_VERSION="$BUILD_VERSION$VERSION_SUFFIX"
154
  fi
155
156
157
158

  # Set build version based on tag if on tag
  if [[ -n "${CIRCLE_TAG}" ]]; then
    # Strip tag
159
    export BUILD_VERSION="$(echo "${CIRCLE_TAG}" | sed -e 's/^v//' -e 's/-.*$//')${VERSION_SUFFIX}"
160
  fi
161
162
163
164
165
166
167
168
169
}

# 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
}

170

Edward Z. Yang's avatar
Edward Z. Yang committed
171
172
173
174
175
176
177
178
179
# Top-level entry point for things every package will need to do
#
# Usage: setup_env 0.2.0
setup_env() {
  setup_cuda
  setup_build_version "$1"
  setup_macos
}

180
181
182
183
184
# 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
185
# Inputs:
186
#   PYTHON_VERSION (3.7, 3.8, 3.9)
Edward Z. Yang's avatar
Edward Z. Yang committed
187
188
189
190
191
192
193
#   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() {
194
  if [[ "$(uname)" == Darwin || "$OSTYPE" == "msys" ]]; then
Edward Z. Yang's avatar
Edward Z. Yang committed
195
196
    eval "$(conda shell.bash hook)"
    conda env remove -n "env$PYTHON_VERSION" || true
197
    conda create ${CONDA_CHANNEL_FLAGS} -yn "env$PYTHON_VERSION" python="$PYTHON_VERSION"
Edward Z. Yang's avatar
Edward Z. Yang committed
198
    conda activate "env$PYTHON_VERSION"
199
    # Install libpng from Anaconda (defaults)
200
    conda install ${CONDA_CHANNEL_FLAGS} libpng "jpeg<=9b" -y
Edward Z. Yang's avatar
Edward Z. Yang committed
201
  else
202
203
    # Install native CentOS libJPEG, freetype and GnuTLS
    yum install -y libjpeg-turbo-devel freetype gnutls
Edward Z. Yang's avatar
Edward Z. Yang committed
204
205
    case "$PYTHON_VERSION" in
      3.7) python_abi=cp37-cp37m ;;
206
      3.8) python_abi=cp38-cp38 ;;
207
      3.9) python_abi=cp39-cp39 ;;
Edward Z. Yang's avatar
Edward Z. Yang committed
208
209
210
211
212
      *)
        echo "Unrecognized PYTHON_VERSION=$PYTHON_VERSION"
        exit 1
        ;;
    esac
213
214
215
216
217
218
219
    # Download all the dependencies required to compile image and video_reader
    # extensions

    mkdir -p ext_libraries
    pushd ext_libraries
    popd
    export PATH="/opt/python/$python_abi/bin:$(pwd)/ext_libraries/bin:$PATH"
Edward Z. Yang's avatar
Edward Z. Yang committed
220
221
222
  fi
}

223
224
225
226
227
228
229
230
231
232
233
# 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
    # Install latest prerelease version of torch, per our nightlies, consistent
    # with the requested cuda version
Edward Z. Yang's avatar
Edward Z. Yang committed
234
    pip_install --pre torch -f "https://download.pytorch.org/whl/nightly/${WHEEL_DIR}torch_nightly.html"
235
236
237
238
239
240
241
242
    if [[ "$CUDA_VERSION" == "cpu" ]]; then
      # 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/+.\+//')"
    else
      export PYTORCH_VERSION="$(pip show torch | grep ^Version: | sed 's/Version:  *//')"
    fi
  else
243
    pip_install "torch==$PYTORCH_VERSION$PYTORCH_VERSION_SUFFIX" \
244
      -f "https://download.pytorch.org/whl/${CU_VERSION}/torch_stable.html" \
245
      -f "https://download.pytorch.org/whl/${UPLOAD_CHANNEL}/${CU_VERSION}/torch_${UPLOAD_CHANNEL}.html"
246
247
248
249
250
251
  fi
}

# Fill PYTORCH_VERSION with the latest conda nightly version, and
# CONDA_CHANNEL_FLAGS with appropriate flags to retrieve these versions
#
252
# You MUST have populated PYTORCH_VERSION_SUFFIX before hand.
253
254
setup_conda_pytorch_constraint() {
  if [[ -z "$PYTORCH_VERSION" ]]; then
255
    export CONDA_CHANNEL_FLAGS="${CONDA_CHANNEL_FLAGS} -c pytorch-nightly -c pytorch"
Francisco Massa's avatar
Francisco Massa committed
256
257
    export PYTORCH_VERSION="$(conda search --json 'pytorch[channel=pytorch-nightly]' | \
                              python -c "import os, sys, json, re; cuver = os.environ.get('CU_VERSION'); \
258
259
                               cuver_1 = cuver.replace('cu', 'cuda') if cuver != 'cpu' else cuver; \
                               cuver_2 = (cuver[:-1] + '.' + cuver[-1]).replace('cu', 'cuda') if cuver != 'cpu' else cuver; \
Francisco Massa's avatar
Francisco Massa committed
260
261
                               print(re.sub(r'\\+.*$', '', \
                                [x['version'] for x in json.load(sys.stdin)['pytorch'] \
262
                                  if (x['platform'] == 'darwin' or cuver_1 in x['fn'] or cuver_2 in x['fn']) \
Francisco Massa's avatar
Francisco Massa committed
263
264
265
266
267
268
                                    and 'py' + os.environ['PYTHON_VERSION'] in x['fn']][-1]))")"
    if [[ -z "$PYTORCH_VERSION" ]]; then
      echo "PyTorch version auto detection failed"
      echo "No package found for CU_VERSION=$CU_VERSION and PYTHON_VERSION=$PYTHON_VERSION"
      exit 1
    fi
269
  else
270
    export CONDA_CHANNEL_FLAGS="${CONDA_CHANNEL_FLAGS} -c pytorch -c pytorch-${UPLOAD_CHANNEL}"
271
  fi
Edward Z. Yang's avatar
Edward Z. Yang committed
272
273
  if [[ "$CU_VERSION" == cpu ]]; then
    export CONDA_PYTORCH_BUILD_CONSTRAINT="- pytorch==$PYTORCH_VERSION${PYTORCH_VERSION_SUFFIX}"
274
275
    export CONDA_PYTORCH_CONSTRAINT="- pytorch==$PYTORCH_VERSION"
  else
Edward Z. Yang's avatar
Edward Z. Yang committed
276
277
    export CONDA_PYTORCH_BUILD_CONSTRAINT="- pytorch==${PYTORCH_VERSION}${PYTORCH_VERSION_SUFFIX}"
    export CONDA_PYTORCH_CONSTRAINT="- pytorch==${PYTORCH_VERSION}${PYTORCH_VERSION_SUFFIX}"
278
  fi
279
280
281
  if [[ "$OSTYPE" == msys && "$CU_VERSION" == cu92 ]]; then
    export CONDA_CHANNEL_FLAGS="${CONDA_CHANNEL_FLAGS} -c defaults -c numba/label/dev"
  fi
282
283
284
285
}

# Translate CUDA_VERSION into CUDA_CUDATOOLKIT_CONSTRAINT
setup_conda_cudatoolkit_constraint() {
286
  export CONDA_BUILD_VARIANT="cuda"
287
  if [[ "$(uname)" == Darwin ]]; then
288
    export CONDA_BUILD_VARIANT="cpu"
289
  else
Edward Z. Yang's avatar
Edward Z. Yang committed
290
    case "$CU_VERSION" in
291
292
293
      cu115)
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=11.5,<11.6 # [not osx]"
        ;;
294
295
296
      cu113)
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=11.3,<11.4 # [not osx]"
        ;;
297
298
299
      cu112)
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=11.2,<11.3 # [not osx]"
        ;;
300
301
302
      cu111)
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=11.1,<11.2 # [not osx]"
        ;;
peterjc123's avatar
peterjc123 committed
303
304
305
      cu110)
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=11.0,<11.1 # [not osx]"
        ;;
306
307
308
      cu102)
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=10.2,<10.3 # [not osx]"
        ;;
Francisco Massa's avatar
Francisco Massa committed
309
310
311
      cu101)
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=10.1,<10.2 # [not osx]"
        ;;
Edward Z. Yang's avatar
Edward Z. Yang committed
312
      cu100)
313
314
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=10.0,<10.1 # [not osx]"
        ;;
Edward Z. Yang's avatar
Edward Z. Yang committed
315
      cu92)
316
317
318
319
        export CONDA_CUDATOOLKIT_CONSTRAINT="- cudatoolkit >=9.2,<9.3 # [not osx]"
        ;;
      cpu)
        export CONDA_CUDATOOLKIT_CONSTRAINT=""
320
        export CONDA_BUILD_VARIANT="cpu"
321
        ;;
Edward Z. Yang's avatar
Edward Z. Yang committed
322
323
324
325
      *)
        echo "Unrecognized CU_VERSION=$CU_VERSION"
        exit 1
        ;;
326
327
328
    esac
  fi
}
329

330
setup_conda_cudatoolkit_plain_constraint() {
331
  export CONDA_BUILD_VARIANT="cuda"
332
333
  export CMAKE_USE_CUDA=1
  if [[ "$(uname)" == Darwin ]]; then
334
    export CONDA_BUILD_VARIANT="cpu"
335
336
337
    export CMAKE_USE_CUDA=0
  else
    case "$CU_VERSION" in
338
339
340
      cu115)
        export CONDA_CUDATOOLKIT_CONSTRAINT="cudatoolkit=11.5"
        ;;
341
342
343
      cu113)
        export CONDA_CUDATOOLKIT_CONSTRAINT="cudatoolkit=11.3"
        ;;
344
345
346
      cu112)
        export CONDA_CUDATOOLKIT_CONSTRAINT="cudatoolkit=11.2"
        ;;
347
348
349
      cu111)
        export CONDA_CUDATOOLKIT_CONSTRAINT="cudatoolkit=11.1"
        ;;
350
351
352
353
354
355
356
357
358
359
360
361
362
363
      cu102)
        export CONDA_CUDATOOLKIT_CONSTRAINT="cudatoolkit=10.2"
        ;;
      cu101)
        export CONDA_CUDATOOLKIT_CONSTRAINT="cudatoolkit=10.1"
        ;;
      cu100)
        export CONDA_CUDATOOLKIT_CONSTRAINT="cudatoolkit=10.0"
        ;;
      cu92)
        export CONDA_CUDATOOLKIT_CONSTRAINT="cudatoolkit=9.2"
        ;;
      cpu)
        export CONDA_CUDATOOLKIT_CONSTRAINT=""
364
        export CONDA_BUILD_VARIANT="cpu"
365
366
367
368
369
370
371
372
373
374
        export CMAKE_USE_CUDA=0
        ;;
      *)
        echo "Unrecognized CU_VERSION=$CU_VERSION"
        exit 1
        ;;
    esac
  fi
}

375
376
377
# Build the proper compiler package before building the final package
setup_visual_studio_constraint() {
  if [[ "$OSTYPE" == "msys" ]]; then
378
      export VSTOOLCHAIN_PACKAGE=vs$VC_YEAR
379
380
381
382
      conda build $CONDA_CHANNEL_FLAGS --no-anaconda-upload packaging/$VSTOOLCHAIN_PACKAGE
      cp packaging/$VSTOOLCHAIN_PACKAGE/conda_build_config.yaml packaging/torchvision/conda_build_config.yaml
  fi
}
383
384
385
386
387
388

setup_junit_results_folder() {
  if [[ "$CI" == "true" ]]; then
    export CONDA_PYTORCH_BUILD_RESULTS_DIRECTORY="${SOURCE_ROOT_DIR}/build_results/results.xml"
  fi
}
389
390
391
392


download_copy_ffmpeg() {
  if [[ "$OSTYPE" == "msys" ]]; then
393
    # conda install -yq ffmpeg=4.2 -c pytorch
394
395
396
397
398
399
400
401
402
    # curl -L -q https://anaconda.org/pytorch/ffmpeg/4.3/download/win-64/ffmpeg-4.3-ha925a31_0.tar.bz2 --output ffmpeg-4.3-ha925a31_0.tar.bz2
    # bzip2 --decompress --stdout ffmpeg-4.3-ha925a31_0.tar.bz2 | tar -x --file=-
    # cp Library/bin/*.dll ../torchvision
    echo "FFmpeg is disabled currently on Windows"
  else
    if [[ "$(uname)" == Darwin ]]; then
      conda install -yq ffmpeg=4.2 -c pytorch
      conda install -yq wget
    else
403
404
405
406
407
408
409
410
      # pushd ext_libraries
      # wget -q https://anaconda.org/pytorch/ffmpeg/4.2/download/linux-64/ffmpeg-4.2-hf484d3e_0.tar.bz2
      # tar -xjvf ffmpeg-4.2-hf484d3e_0.tar.bz2
      # rm -rf ffmpeg-4.2-hf484d3e_0.tar.bz2
      # ldconfig
      # which ffmpeg
      # popd
      echo "FFmpeg is disabled currently on Linux"
411
412
413
    fi
  fi
}