blob: 3f9a53b573b75ad143bb057893ca88ea61d1f597 [file] [log] [blame]
Geoffrey Irving4c85a082016-03-16 12:20:34 -08001#!/usr/bin/env bash
Manjunath Kudlurf41959c2015-11-06 16:27:58 -08002
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -08003DO_NOT_SUBMIT_WARNING="Unofficial setting. DO NOT SUBMIT!!!"
4
Shanqing Caide6d48c2016-04-28 10:34:56 -08005## Verify that the submodule google/protobuf is available
6# TODO(cais): Remove this check once protobuf is no longer depended upon
7if [[ ! -f "google/protobuf/protobuf.bzl" ]]; then
8 echo "ERROR: It appears that the required submodule google/protobuf is not "\
9"available in this TensorFlow git clone."
10 echo "Please be sure to use the --recurse-submodules flag when performing "\
11"git clone of TensorFlow."
12
13 exit 1
14fi
15
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080016## Set up python-related environment settings
17while true; do
18 fromuser=""
19 if [ -z "$PYTHON_BIN_PATH" ]; then
20 default_python_bin_path=$(which python)
21 read -p "Please specify the location of python. [Default is $default_python_bin_path]: " PYTHON_BIN_PATH
22 fromuser="1"
23 if [ -z "$PYTHON_BIN_PATH" ]; then
24 PYTHON_BIN_PATH=$default_python_bin_path
25 fi
26 fi
27 if [ -e "$PYTHON_BIN_PATH" ]; then
28 break
29 fi
30 echo "Invalid python path. ${PYTHON_BIN_PATH} cannot be found" 1>&2
31 if [ -z "$fromuser" ]; then
32 exit 1
33 fi
34 PYTHON_BIN_PATH=""
35 # Retry
36done
37
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -080038## Find swig path
39if [ -z "$SWIG_PATH" ]; then
40 SWIG_PATH=`type -p swig 2> /dev/null`
41fi
42if [[ ! -e "$SWIG_PATH" ]]; then
43 echo "Can't find swig. Ensure swig is in \$PATH or set \$SWIG_PATH."
44 exit 1
45fi
46echo "$SWIG_PATH" > tensorflow/tools/swig/swig_path
47
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080048# Invoke python_config and set up symlinks to python includes
49(./util/python/python_config.sh --setup "$PYTHON_BIN_PATH";) || exit -1
50
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080051## Set up Cuda-related environment settings
52
53while [ "$TF_NEED_CUDA" == "" ]; do
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080054 read -p "Do you wish to build TensorFlow with GPU support? [y/N] " INPUT
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080055 case $INPUT in
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080056 [Yy]* ) echo "GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=1;;
57 [Nn]* ) echo "No GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=0;;
58 "" ) echo "No GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=0;;
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080059 * ) echo "Invalid selection: " $INPUT;;
60 esac
61done
62
63if [ "$TF_NEED_CUDA" == "0" ]; then
64 echo "Configuration finished"
65 exit
66fi
67
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -080068# Set up which gcc nvcc should use as the host compiler
69while true; do
70 fromuser=""
71 if [ -z "$GCC_HOST_COMPILER_PATH" ]; then
72 default_gcc_host_compiler_path=$(which gcc)
73 read -p "Please specify which gcc nvcc should use as the host compiler. [Default is $default_gcc_host_compiler_path]: " GCC_HOST_COMPILER_PATH
74 fromuser="1"
75 if [ -z "$GCC_HOST_COMPILER_PATH" ]; then
76 GCC_HOST_COMPILER_PATH=$default_gcc_host_compiler_path
77 fi
78 fi
79 if [ -e "$GCC_HOST_COMPILER_PATH" ]; then
80 break
81 fi
82 echo "Invalid gcc path. ${GCC_HOST_COMPILER_PATH} cannot be found" 1>&2
83 if [ -z "$fromuser" ]; then
84 exit 1
85 fi
86 GCC_HOST_COMPILER_PATH=""
87 # Retry
88done
89
90
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080091# Find out where the CUDA toolkit is installed
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -080092OSNAME=`uname -s`
93
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080094while true; do
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080095 # Configure the Cuda SDK version to use.
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080096 if [ -z "$TF_CUDA_VERSION" ]; then
Vijay Vasudevanfe056f02016-02-17 11:42:30 -080097 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 -080098 fi
99
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800100 fromuser=""
101 if [ -z "$CUDA_TOOLKIT_PATH" ]; then
102 default_cuda_path=/usr/local/cuda
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800103 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 -0800104 fromuser="1"
105 if [ -z "$CUDA_TOOLKIT_PATH" ]; then
106 CUDA_TOOLKIT_PATH=$default_cuda_path
107 fi
108 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800109
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800110 if [[ -z "$TF_CUDA_VERSION" ]]; then
111 TF_CUDA_EXT=""
112 else
113 TF_CUDA_EXT=".$TF_CUDA_VERSION"
114 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800115
116 if [ "$OSNAME" == "Linux" ]; then
117 CUDA_RT_LIB_PATH="lib64/libcudart.so${TF_CUDA_EXT}"
118 elif [ "$OSNAME" == "Darwin" ]; then
119 CUDA_RT_LIB_PATH="lib/libcudart${TF_CUDA_EXT}.dylib"
120 fi
121
122 if [ -e "${CUDA_TOOLKIT_PATH}/${CUDA_RT_LIB_PATH}" ]; then
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800123 break
124 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800125 echo "Invalid path to CUDA $TF_CUDA_VERSION toolkit. ${CUDA_TOOLKIT_PATH}/${CUDA_RT_LIB_PATH} cannot be found"
126
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800127 if [ -z "$fromuser" ]; then
128 exit 1
129 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800130 # Retry
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800131 TF_CUDA_VERSION=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800132 CUDA_TOOLKIT_PATH=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800133done
134
Martin Wicke916776a2016-01-14 07:30:00 -0800135# Find out where the cuDNN library is installed
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800136while true; do
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800137 # Configure the Cudnn version to use.
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800138 if [ -z "$TF_CUDNN_VERSION" ]; then
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800139 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 -0800140 fi
141
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800142 fromuser=""
143 if [ -z "$CUDNN_INSTALL_PATH" ]; then
144 default_cudnn_path=${CUDA_TOOLKIT_PATH}
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800145 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 -0800146 fromuser="1"
147 if [ -z "$CUDNN_INSTALL_PATH" ]; then
148 CUDNN_INSTALL_PATH=$default_cudnn_path
149 fi
150 # Result returned from "read" will be used unexpanded. That make "~" unuseable.
151 # Going through one more level of expansion to handle that.
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800152 CUDNN_INSTALL_PATH=`${PYTHON_BIN_PATH} -c "import os; print(os.path.realpath(os.path.expanduser('${CUDNN_INSTALL_PATH}')))"`
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800153 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800154
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800155 if [[ -z "$TF_CUDNN_VERSION" ]]; then
156 TF_CUDNN_EXT=""
157 else
158 TF_CUDNN_EXT=".$TF_CUDNN_VERSION"
159 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800160
161 if [ "$OSNAME" == "Linux" ]; then
162 CUDA_DNN_LIB_PATH="lib64/libcudnn.so${TF_CUDNN_EXT}"
163 CUDA_DNN_LIB_ALT_PATH="libcudnn.so${TF_CUDNN_EXT}"
164 elif [ "$OSNAME" == "Darwin" ]; then
165 CUDA_DNN_LIB_PATH="lib/libcudnn${TF_CUDNN_EXT}.dylib"
166 CUDA_DNN_LIB_ALT_PATH="libcudnn${TF_CUDNN_EXT}.dylib"
167 fi
168
169 if [ -e "$CUDNN_INSTALL_PATH/${CUDA_DNN_LIB_ALT_PATH}" -o -e "$CUDNN_INSTALL_PATH/${CUDA_DNN_LIB_PATH}" ]; then
Eugene Brevdo56f1d642016-03-10 17:18:30 -0800170 break
171 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800172
173 if [ "$OSNAME" == "Linux" ]; then
174 CUDNN_PATH_FROM_LDCONFIG="$(ldconfig -p | sed -n 's/.*libcudnn.so .* => \(.*\)/\1/p')"
175 if [ -e "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}" ]; then
176 CUDNN_INSTALL_PATH="$(dirname ${CUDNN_PATH_FROM_LDCONFIG})"
177 break
178 fi
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800179 fi
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800180 echo "Invalid path to cuDNN ${CUDNN_VERSION} toolkit. Neither of the following two files can be found:"
181 echo "${CUDNN_INSTALL_PATH}/${CUDA_DNN_LIB_PATH}"
182 echo "${CUDNN_INSTALL_PATH}/${CUDA_DNN_LIB_ALT_PATH}"
183 if [ "$OSNAME" == "Linux" ]; then
184 echo "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}"
185 fi
186
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800187 if [ -z "$fromuser" ]; then
188 exit 1
189 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800190 # Retry
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800191 TF_CUDNN_VERSION=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800192 CUDNN_INSTALL_PATH=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800193done
194
195cat > third_party/gpus/cuda/cuda.config <<EOF
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800196# CUDA_TOOLKIT_PATH refers to the CUDA toolkit.
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800197CUDA_TOOLKIT_PATH="$CUDA_TOOLKIT_PATH"
Martin Wicke916776a2016-01-14 07:30:00 -0800198# CUDNN_INSTALL_PATH refers to the cuDNN toolkit. The cuDNN header and library
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800199# files can be either in this directory, or under include/ and lib64/
200# directories separately.
201CUDNN_INSTALL_PATH="$CUDNN_INSTALL_PATH"
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800202
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800203# The Cuda SDK version that should be used in this build (empty to use libcudart.so symlink)
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800204TF_CUDA_VERSION=$TF_CUDA_VERSION
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800205
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800206# The Cudnn version that should be used in this build
207TF_CUDNN_VERSION=$TF_CUDNN_VERSION
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800208EOF
209
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -0800210# Configure the gcc host compiler to use
211export WARNING=$DO_NOT_SUBMIT_WARNING
212perl -pi -e "s,CPU_COMPILER = \('.*'\),# \$ENV{WARNING}\nCPU_COMPILER = ('$GCC_HOST_COMPILER_PATH'),s" third_party/gpus/crosstool/clang/bin/crosstool_wrapper_driver_is_not_gcc
213perl -pi -e "s,GCC_HOST_COMPILER_PATH = \('.*'\),# \$ENV{WARNING}\nGCC_HOST_COMPILER_PATH = ('$GCC_HOST_COMPILER_PATH'),s" third_party/gpus/crosstool/clang/bin/crosstool_wrapper_driver_is_not_gcc
214
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800215# Configure the platform name.
216perl -pi -e "s,PLATFORM = \".*\",PLATFORM = \"$OSNAME\",s" third_party/gpus/cuda/platform.bzl
217
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800218# Configure the Cuda toolkit version to work with.
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800219perl -pi -e "s,(GetCudaVersion.*return )\"[0-9\.]*\",\1\"$TF_CUDA_VERSION\",s" tensorflow/stream_executor/dso_loader.cc
220perl -pi -e "s,CUDA_VERSION = \"[0-9\.]*\",CUDA_VERSION = \"$TF_CUDA_VERSION\",s" third_party/gpus/cuda/platform.bzl
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800221
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800222# Configure the Cudnn version to work with.
A. Unique TensorFlower8bf6ef12016-05-05 08:36:05 -0800223perl -pi -e "s,(GetCudnnVersion.*return )\"[0-9\.]*\",\1\"$TF_CUDNN_VERSION\",s" tensorflow/stream_executor/dso_loader.cc
224perl -pi -e "s,CUDNN_VERSION = \"[0-9\.]*\",CUDNN_VERSION = \"$TF_CUDNN_VERSION\",s" third_party/gpus/cuda/platform.bzl
225
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800226
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800227# Configure the compute capabilities that TensorFlow builds for.
228# Since Cuda toolkit is not backward-compatible, this is not guaranteed to work.
229while true; do
230 fromuser=""
231 if [ -z "$TF_CUDA_COMPUTE_CAPABILITIES" ]; then
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800232cat << EOF
233Please specify a list of comma-separated Cuda compute capabilities you want to build with.
234You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
235Please note that each additional compute capability significantly increases your build time and binary size.
236EOF
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800237 read -p "[Default is: \"3.5,5.2\"]: " TF_CUDA_COMPUTE_CAPABILITIES
238 fromuser=1
239 fi
240 # Check whether all capabilities from the input is valid
241 COMPUTE_CAPABILITIES=${TF_CUDA_COMPUTE_CAPABILITIES//,/ }
242 ALL_VALID=1
243 for CAPABILITY in $COMPUTE_CAPABILITIES; do
244 if [[ ! "$CAPABILITY" =~ [0-9]+.[0-9]+ ]]; then
245 echo "Invalid compute capability: " $CAPABILITY
246 ALL_VALID=0
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800247 break
248 fi
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800249 done
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800250 if [ "$ALL_VALID" == "0" ]; then
251 if [ -z "$fromuser" ]; then
252 exit 1
253 fi
254 else
255 break
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800256 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800257 TF_CUDA_COMPUTE_CAPABILITIES=""
258done
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800259
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800260if [ ! -z "$TF_CUDA_COMPUTE_CAPABILITIES" ]; then
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -0800261 export WARNING=$DO_NOT_SUBMIT_WARNING
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800262 function CudaGenCodeOpts() {
263 OUTPUT=""
264 for CAPABILITY in $@; do
265 OUTPUT=${OUTPUT}" \"${CAPABILITY}\", "
266 done
267 echo $OUTPUT
268 }
269 export CUDA_GEN_CODES_OPTS=$(CudaGenCodeOpts ${TF_CUDA_COMPUTE_CAPABILITIES//,/ })
270 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
271 function CudaVersionOpts() {
272 OUTPUT=""
273 for CAPABILITY in $@; do
274 OUTPUT=$OUTPUT"CudaVersion(\"${CAPABILITY}\"), "
275 done
276 echo $OUTPUT
277 }
278 export CUDA_VERSION_OPTS=$(CudaVersionOpts ${TF_CUDA_COMPUTE_CAPABILITIES//,/ })
279 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 -0800280fi
281
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800282# Invoke the cuda_config.sh and set up the TensorFlow's canonical view of the Cuda libraries
283(cd third_party/gpus/cuda; ./cuda_config.sh;) || exit -1
284
285echo "Configuration finished"