blob: 75098c17eeab5ac1f294439b465718c3d05a97c7 [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
92while true; do
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080093 # Configure the Cuda SDK version to use.
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080094 if [ -z "$TF_CUDA_VERSION" ]; then
Vijay Vasudevanfe056f02016-02-17 11:42:30 -080095 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 -080096 fi
97
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080098 fromuser=""
99 if [ -z "$CUDA_TOOLKIT_PATH" ]; then
100 default_cuda_path=/usr/local/cuda
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800101 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 -0800102 fromuser="1"
103 if [ -z "$CUDA_TOOLKIT_PATH" ]; then
104 CUDA_TOOLKIT_PATH=$default_cuda_path
105 fi
106 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800107 if [[ -z "$TF_CUDA_VERSION" ]]; then
108 TF_CUDA_EXT=""
109 else
110 TF_CUDA_EXT=".$TF_CUDA_VERSION"
111 fi
112 if [ -e $CUDA_TOOLKIT_PATH/lib64/libcudart.so$TF_CUDA_EXT ]; then
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800113 break
114 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800115 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 -0800116 if [ -z "$fromuser" ]; then
117 exit 1
118 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800119 # Retry
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800120 TF_CUDA_VERSION=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800121 CUDA_TOOLKIT_PATH=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800122done
123
Martin Wicke916776a2016-01-14 07:30:00 -0800124# Find out where the cuDNN library is installed
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800125while true; do
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800126 # Configure the Cudnn version to use.
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800127 if [ -z "$TF_CUDNN_VERSION" ]; then
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800128 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 -0800129 fi
130
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800131 fromuser=""
132 if [ -z "$CUDNN_INSTALL_PATH" ]; then
133 default_cudnn_path=${CUDA_TOOLKIT_PATH}
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800134 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 -0800135 fromuser="1"
136 if [ -z "$CUDNN_INSTALL_PATH" ]; then
137 CUDNN_INSTALL_PATH=$default_cudnn_path
138 fi
139 # Result returned from "read" will be used unexpanded. That make "~" unuseable.
140 # Going through one more level of expansion to handle that.
141 CUDNN_INSTALL_PATH=$(bash -c "readlink -f $CUDNN_INSTALL_PATH")
142 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800143 if [[ -z "$TF_CUDNN_VERSION" ]]; then
144 TF_CUDNN_EXT=""
145 else
146 TF_CUDNN_EXT=".$TF_CUDNN_VERSION"
147 fi
Eugene Brevdo56f1d642016-03-10 17:18:30 -0800148 if [ -e "$CUDNN_INSTALL_PATH/libcudnn.so${TF_CUDNN_EXT}" -o -e "$CUDNN_INSTALL_PATH/lib64/libcudnn.so${TF_CUDNN_EXT}" ]; then
149 break
150 fi
151 CUDNN_PATH_FROM_LDCONFIG="$(ldconfig -p | sed -n 's/.*libcudnn.so .* => \(.*\)/\1/p')"
152 if [ -e "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}" ]; then
153 CUDNN_INSTALL_PATH="$(dirname ${CUDNN_PATH_FROM_LDCONFIG})"
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800154 break
155 fi
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800156 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 -0800157 echo "$CUDNN_INSTALL_PATH/lib64/libcudnn.so${TF_CUDNN_EXT}"
158 echo "$CUDNN_INSTALL_PATH/libcudnn.so${TF_CUDNN_EXT}"
Eugene Brevdo56f1d642016-03-10 17:18:30 -0800159 echo "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}"
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800160 if [ -z "$fromuser" ]; then
161 exit 1
162 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800163 # Retry
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800164 TF_CUDNN_VERSION=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800165 CUDNN_INSTALL_PATH=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800166done
167
168cat > third_party/gpus/cuda/cuda.config <<EOF
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800169# CUDA_TOOLKIT_PATH refers to the CUDA toolkit.
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800170CUDA_TOOLKIT_PATH="$CUDA_TOOLKIT_PATH"
171
Martin Wicke916776a2016-01-14 07:30:00 -0800172# CUDNN_INSTALL_PATH refers to the cuDNN toolkit. The cuDNN header and library
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800173# files can be either in this directory, or under include/ and lib64/
174# directories separately.
175CUDNN_INSTALL_PATH="$CUDNN_INSTALL_PATH"
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800176
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800177# The Cuda SDK version that should be used in this build (empty to use libcudart.so symlink)
178TF_CUDA_VERSION=$TF_CUDA_EXT
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800179
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800180# The Cudnn version that should be used in this build (empty to use libcudnn.so symlink)
181TF_CUDNN_VERSION=$TF_CUDNN_EXT
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800182
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800183EOF
184
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -0800185# Configure the gcc host compiler to use
186export WARNING=$DO_NOT_SUBMIT_WARNING
187perl -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
188perl -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
189
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800190# Configure the Cuda toolkit version to work with.
Vijay Vasudevan97f6b6f2016-02-25 13:44:06 -0800191perl -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 -0800192perl -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 -0800193
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800194# Configure the Cudnn version to work with.
Vijay Vasudevan97f6b6f2016-02-25 13:44:06 -0800195perl -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 -0800196perl -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 -0800197
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800198# Configure the compute capabilities that TensorFlow builds for.
199# Since Cuda toolkit is not backward-compatible, this is not guaranteed to work.
200while true; do
201 fromuser=""
202 if [ -z "$TF_CUDA_COMPUTE_CAPABILITIES" ]; then
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800203cat << EOF
204Please specify a list of comma-separated Cuda compute capabilities you want to build with.
205You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
206Please note that each additional compute capability significantly increases your build time and binary size.
207EOF
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800208 read -p "[Default is: \"3.5,5.2\"]: " TF_CUDA_COMPUTE_CAPABILITIES
209 fromuser=1
210 fi
211 # Check whether all capabilities from the input is valid
212 COMPUTE_CAPABILITIES=${TF_CUDA_COMPUTE_CAPABILITIES//,/ }
213 ALL_VALID=1
214 for CAPABILITY in $COMPUTE_CAPABILITIES; do
215 if [[ ! "$CAPABILITY" =~ [0-9]+.[0-9]+ ]]; then
216 echo "Invalid compute capability: " $CAPABILITY
217 ALL_VALID=0
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800218 break
219 fi
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800220 done
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800221 if [ "$ALL_VALID" == "0" ]; then
222 if [ -z "$fromuser" ]; then
223 exit 1
224 fi
225 else
226 break
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800227 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800228 TF_CUDA_COMPUTE_CAPABILITIES=""
229done
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800230
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800231if [ ! -z "$TF_CUDA_COMPUTE_CAPABILITIES" ]; then
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -0800232 export WARNING=$DO_NOT_SUBMIT_WARNING
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800233 function CudaGenCodeOpts() {
234 OUTPUT=""
235 for CAPABILITY in $@; do
236 OUTPUT=${OUTPUT}" \"${CAPABILITY}\", "
237 done
238 echo $OUTPUT
239 }
240 export CUDA_GEN_CODES_OPTS=$(CudaGenCodeOpts ${TF_CUDA_COMPUTE_CAPABILITIES//,/ })
241 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
242 function CudaVersionOpts() {
243 OUTPUT=""
244 for CAPABILITY in $@; do
245 OUTPUT=$OUTPUT"CudaVersion(\"${CAPABILITY}\"), "
246 done
247 echo $OUTPUT
248 }
249 export CUDA_VERSION_OPTS=$(CudaVersionOpts ${TF_CUDA_COMPUTE_CAPABILITIES//,/ })
250 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 -0800251fi
252
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800253# Invoke the cuda_config.sh and set up the TensorFlow's canonical view of the Cuda libraries
254(cd third_party/gpus/cuda; ./cuda_config.sh;) || exit -1
255
256echo "Configuration finished"