blob: 0faf61c67b173403cf57d310001d87b1da216a3e [file] [log] [blame]
Geoffrey Irving4c85a082016-03-16 12:20:34 -08001#!/usr/bin/env bash
Manjunath Kudlurf41959c2015-11-06 16:27:58 -08002
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -08003## Set up python-related environment settings
4while true; do
5 fromuser=""
6 if [ -z "$PYTHON_BIN_PATH" ]; then
7 default_python_bin_path=$(which python)
8 read -p "Please specify the location of python. [Default is $default_python_bin_path]: " PYTHON_BIN_PATH
9 fromuser="1"
10 if [ -z "$PYTHON_BIN_PATH" ]; then
11 PYTHON_BIN_PATH=$default_python_bin_path
12 fi
13 fi
14 if [ -e "$PYTHON_BIN_PATH" ]; then
15 break
16 fi
17 echo "Invalid python path. ${PYTHON_BIN_PATH} cannot be found" 1>&2
18 if [ -z "$fromuser" ]; then
19 exit 1
20 fi
21 PYTHON_BIN_PATH=""
22 # Retry
23done
24
25# Invoke python_config and set up symlinks to python includes
26(./util/python/python_config.sh --setup "$PYTHON_BIN_PATH";) || exit -1
27
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080028## Set up Cuda-related environment settings
29
30while [ "$TF_NEED_CUDA" == "" ]; do
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080031 read -p "Do you wish to build TensorFlow with GPU support? [y/N] " INPUT
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080032 case $INPUT in
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080033 [Yy]* ) echo "GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=1;;
34 [Nn]* ) echo "No GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=0;;
35 "" ) echo "No GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=0;;
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080036 * ) echo "Invalid selection: " $INPUT;;
37 esac
38done
39
40if [ "$TF_NEED_CUDA" == "0" ]; then
41 echo "Configuration finished"
42 exit
43fi
44
45# Find out where the CUDA toolkit is installed
46while true; do
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080047 # Configure the Cuda SDK version to use.
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080048 if [ -z "$TF_CUDA_VERSION" ]; then
Vijay Vasudevanfe056f02016-02-17 11:42:30 -080049 read -p "Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: " TF_CUDA_VERSION
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080050 fi
51
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080052 fromuser=""
53 if [ -z "$CUDA_TOOLKIT_PATH" ]; then
54 default_cuda_path=/usr/local/cuda
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080055 read -p "Please specify the location where CUDA $TF_CUDA_VERSION toolkit is installed. Refer to README.md for more details. [Default is $default_cuda_path]: " CUDA_TOOLKIT_PATH
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080056 fromuser="1"
57 if [ -z "$CUDA_TOOLKIT_PATH" ]; then
58 CUDA_TOOLKIT_PATH=$default_cuda_path
59 fi
60 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -080061 if [[ -z "$TF_CUDA_VERSION" ]]; then
62 TF_CUDA_EXT=""
63 else
64 TF_CUDA_EXT=".$TF_CUDA_VERSION"
65 fi
66 if [ -e $CUDA_TOOLKIT_PATH/lib64/libcudart.so$TF_CUDA_EXT ]; then
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080067 break
68 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -080069 echo "Invalid path to CUDA $TF_CUDA_VERSION toolkit. $CUDA_TOOLKIT_PATH/lib64/libcudart.so$TF_CUDA_EXT cannot be found"
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080070 if [ -z "$fromuser" ]; then
71 exit 1
72 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -080073 # Retry
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080074 TF_CUDA_VERSION=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080075 CUDA_TOOLKIT_PATH=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080076done
77
Martin Wicke916776a2016-01-14 07:30:00 -080078# Find out where the cuDNN library is installed
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080079while true; do
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080080 # Configure the Cudnn version to use.
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080081 if [ -z "$TF_CUDNN_VERSION" ]; then
Vijay Vasudevanfe056f02016-02-17 11:42:30 -080082 read -p "Please specify the Cudnn version you want to use. [Leave empty to use system default]: " TF_CUDNN_VERSION
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080083 fi
84
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080085 fromuser=""
86 if [ -z "$CUDNN_INSTALL_PATH" ]; then
87 default_cudnn_path=${CUDA_TOOLKIT_PATH}
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080088 read -p "Please specify the location where cuDNN $TF_CUDNN_VERSION library is installed. Refer to README.md for more details. [Default is $default_cudnn_path]: " CUDNN_INSTALL_PATH
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080089 fromuser="1"
90 if [ -z "$CUDNN_INSTALL_PATH" ]; then
91 CUDNN_INSTALL_PATH=$default_cudnn_path
92 fi
93 # Result returned from "read" will be used unexpanded. That make "~" unuseable.
94 # Going through one more level of expansion to handle that.
95 CUDNN_INSTALL_PATH=$(bash -c "readlink -f $CUDNN_INSTALL_PATH")
96 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -080097 if [[ -z "$TF_CUDNN_VERSION" ]]; then
98 TF_CUDNN_EXT=""
99 else
100 TF_CUDNN_EXT=".$TF_CUDNN_VERSION"
101 fi
Eugene Brevdo56f1d642016-03-10 17:18:30 -0800102 if [ -e "$CUDNN_INSTALL_PATH/libcudnn.so${TF_CUDNN_EXT}" -o -e "$CUDNN_INSTALL_PATH/lib64/libcudnn.so${TF_CUDNN_EXT}" ]; then
103 break
104 fi
105 CUDNN_PATH_FROM_LDCONFIG="$(ldconfig -p | sed -n 's/.*libcudnn.so .* => \(.*\)/\1/p')"
106 if [ -e "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}" ]; then
107 CUDNN_INSTALL_PATH="$(dirname ${CUDNN_PATH_FROM_LDCONFIG})"
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800108 break
109 fi
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800110 echo "Invalid path to cuDNN ${TF_CUDNN_VERSION} toolkit. Neither of the following two files can be found:"
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800111 echo "$CUDNN_INSTALL_PATH/lib64/libcudnn.so${TF_CUDNN_EXT}"
112 echo "$CUDNN_INSTALL_PATH/libcudnn.so${TF_CUDNN_EXT}"
Eugene Brevdo56f1d642016-03-10 17:18:30 -0800113 echo "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}"
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800114 if [ -z "$fromuser" ]; then
115 exit 1
116 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800117 # Retry
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800118 TF_CUDNN_VERSION=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800119 CUDNN_INSTALL_PATH=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800120done
121
122cat > third_party/gpus/cuda/cuda.config <<EOF
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800123# CUDA_TOOLKIT_PATH refers to the CUDA toolkit.
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800124CUDA_TOOLKIT_PATH="$CUDA_TOOLKIT_PATH"
125
Martin Wicke916776a2016-01-14 07:30:00 -0800126# CUDNN_INSTALL_PATH refers to the cuDNN toolkit. The cuDNN header and library
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800127# files can be either in this directory, or under include/ and lib64/
128# directories separately.
129CUDNN_INSTALL_PATH="$CUDNN_INSTALL_PATH"
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800130
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800131# The Cuda SDK version that should be used in this build (empty to use libcudart.so symlink)
132TF_CUDA_VERSION=$TF_CUDA_EXT
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800133
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800134# The Cudnn version that should be used in this build (empty to use libcudnn.so symlink)
135TF_CUDNN_VERSION=$TF_CUDNN_EXT
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800136
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800137EOF
138
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800139# Configure the Cuda toolkit version to work with.
Vijay Vasudevan97f6b6f2016-02-25 13:44:06 -0800140perl -pi -e "s,CUDA_VERSION = \"[0-9\.]*\",CUDA_VERSION = \"$TF_CUDA_EXT\",s" tensorflow/core/platform/default/build_config.bzl
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800141perl -pi -e "s,(GetCudaVersion.*return )\"[0-9\.]*\",\1\"$TF_CUDA_EXT\",s" tensorflow/stream_executor/dso_loader.cc
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800142
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800143# Configure the Cudnn version to work with.
Vijay Vasudevan97f6b6f2016-02-25 13:44:06 -0800144perl -pi -e "s,CUDNN_VERSION = \"[0-9\.]*\",CUDNN_VERSION = \"$TF_CUDNN_EXT\",s" tensorflow/core/platform/default/build_config.bzl
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800145perl -pi -e "s,(GetCudnnVersion.*return )\"[0-9\.]*\",\1\"$TF_CUDNN_EXT\",s" tensorflow/stream_executor/dso_loader.cc
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800146
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800147# Configure the compute capabilities that TensorFlow builds for.
148# Since Cuda toolkit is not backward-compatible, this is not guaranteed to work.
149while true; do
150 fromuser=""
151 if [ -z "$TF_CUDA_COMPUTE_CAPABILITIES" ]; then
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800152cat << EOF
153Please specify a list of comma-separated Cuda compute capabilities you want to build with.
154You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
155Please note that each additional compute capability significantly increases your build time and binary size.
156EOF
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800157 read -p "[Default is: \"3.5,5.2\"]: " TF_CUDA_COMPUTE_CAPABILITIES
158 fromuser=1
159 fi
160 # Check whether all capabilities from the input is valid
161 COMPUTE_CAPABILITIES=${TF_CUDA_COMPUTE_CAPABILITIES//,/ }
162 ALL_VALID=1
163 for CAPABILITY in $COMPUTE_CAPABILITIES; do
164 if [[ ! "$CAPABILITY" =~ [0-9]+.[0-9]+ ]]; then
165 echo "Invalid compute capability: " $CAPABILITY
166 ALL_VALID=0
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800167 break
168 fi
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800169 done
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800170 if [ "$ALL_VALID" == "0" ]; then
171 if [ -z "$fromuser" ]; then
172 exit 1
173 fi
174 else
175 break
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800176 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800177 TF_CUDA_COMPUTE_CAPABILITIES=""
178done
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800179
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800180if [ ! -z "$TF_CUDA_COMPUTE_CAPABILITIES" ]; then
181 export WARNING="Unofficial setting. DO NOT"" SUBMIT!!!"
182 function CudaGenCodeOpts() {
183 OUTPUT=""
184 for CAPABILITY in $@; do
185 OUTPUT=${OUTPUT}" \"${CAPABILITY}\", "
186 done
187 echo $OUTPUT
188 }
189 export CUDA_GEN_CODES_OPTS=$(CudaGenCodeOpts ${TF_CUDA_COMPUTE_CAPABILITIES//,/ })
190 perl -pi -0 -e 's,\n( *)([^\n]*supported_cuda_compute_capabilities\s*=\s*\[).*?(\]),\n\1# $ENV{WARNING}\n\1\2$ENV{CUDA_GEN_CODES_OPTS}\3,s' third_party/gpus/crosstool/clang/bin/crosstool_wrapper_driver_is_not_gcc
191 function CudaVersionOpts() {
192 OUTPUT=""
193 for CAPABILITY in $@; do
194 OUTPUT=$OUTPUT"CudaVersion(\"${CAPABILITY}\"), "
195 done
196 echo $OUTPUT
197 }
198 export CUDA_VERSION_OPTS=$(CudaVersionOpts ${TF_CUDA_COMPUTE_CAPABILITIES//,/ })
199 perl -pi -0 -e 's,\n( *)([^\n]*supported_cuda_compute_capabilities\s*=\s*\{).*?(\}),\n\1// $ENV{WARNING}\n\1\2$ENV{CUDA_VERSION_OPTS}\3,s' tensorflow/core/common_runtime/gpu/gpu_device.cc
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800200fi
201
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800202# Invoke the cuda_config.sh and set up the TensorFlow's canonical view of the Cuda libraries
203(cd third_party/gpus/cuda; ./cuda_config.sh;) || exit -1
204
205echo "Configuration finished"