blob: 0a7d697c406c3248bbece222beb4a4a4af56315c [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
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -08005## Set up python-related environment settings
6while true; do
7 fromuser=""
8 if [ -z "$PYTHON_BIN_PATH" ]; then
9 default_python_bin_path=$(which python)
10 read -p "Please specify the location of python. [Default is $default_python_bin_path]: " PYTHON_BIN_PATH
11 fromuser="1"
12 if [ -z "$PYTHON_BIN_PATH" ]; then
13 PYTHON_BIN_PATH=$default_python_bin_path
14 fi
15 fi
16 if [ -e "$PYTHON_BIN_PATH" ]; then
17 break
18 fi
19 echo "Invalid python path. ${PYTHON_BIN_PATH} cannot be found" 1>&2
20 if [ -z "$fromuser" ]; then
21 exit 1
22 fi
23 PYTHON_BIN_PATH=""
24 # Retry
25done
26
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -080027## Find swig path
28if [ -z "$SWIG_PATH" ]; then
29 SWIG_PATH=`type -p swig 2> /dev/null`
30fi
31if [[ ! -e "$SWIG_PATH" ]]; then
32 echo "Can't find swig. Ensure swig is in \$PATH or set \$SWIG_PATH."
33 exit 1
34fi
35echo "$SWIG_PATH" > tensorflow/tools/swig/swig_path
36
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080037# Invoke python_config and set up symlinks to python includes
38(./util/python/python_config.sh --setup "$PYTHON_BIN_PATH";) || exit -1
39
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080040## Set up Cuda-related environment settings
41
42while [ "$TF_NEED_CUDA" == "" ]; do
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080043 read -p "Do you wish to build TensorFlow with GPU support? [y/N] " INPUT
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080044 case $INPUT in
Vijay Vasudevanbf6b5362015-12-02 15:04:40 -080045 [Yy]* ) echo "GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=1;;
46 [Nn]* ) echo "No GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=0;;
47 "" ) echo "No GPU support will be enabled for TensorFlow"; TF_NEED_CUDA=0;;
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080048 * ) echo "Invalid selection: " $INPUT;;
49 esac
50done
51
52if [ "$TF_NEED_CUDA" == "0" ]; then
53 echo "Configuration finished"
54 exit
55fi
56
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -080057# Set up which gcc nvcc should use as the host compiler
58while true; do
59 fromuser=""
60 if [ -z "$GCC_HOST_COMPILER_PATH" ]; then
61 default_gcc_host_compiler_path=$(which gcc)
62 read -p "Please specify which gcc nvcc should use as the host compiler. [Default is $default_gcc_host_compiler_path]: " GCC_HOST_COMPILER_PATH
63 fromuser="1"
64 if [ -z "$GCC_HOST_COMPILER_PATH" ]; then
65 GCC_HOST_COMPILER_PATH=$default_gcc_host_compiler_path
66 fi
67 fi
68 if [ -e "$GCC_HOST_COMPILER_PATH" ]; then
69 break
70 fi
71 echo "Invalid gcc path. ${GCC_HOST_COMPILER_PATH} cannot be found" 1>&2
72 if [ -z "$fromuser" ]; then
73 exit 1
74 fi
75 GCC_HOST_COMPILER_PATH=""
76 # Retry
77done
78
79
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080080# Find out where the CUDA toolkit is installed
81while true; do
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080082 # Configure the Cuda SDK version to use.
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080083 if [ -z "$TF_CUDA_VERSION" ]; then
Vijay Vasudevanfe056f02016-02-17 11:42:30 -080084 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 -080085 fi
86
Manjunath Kudlurf41959c2015-11-06 16:27:58 -080087 fromuser=""
88 if [ -z "$CUDA_TOOLKIT_PATH" ]; then
89 default_cuda_path=/usr/local/cuda
A. Unique TensorFlower8a597482016-01-29 09:34:18 -080090 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 -080091 fromuser="1"
92 if [ -z "$CUDA_TOOLKIT_PATH" ]; then
93 CUDA_TOOLKIT_PATH=$default_cuda_path
94 fi
95 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -080096 if [[ -z "$TF_CUDA_VERSION" ]]; then
97 TF_CUDA_EXT=""
98 else
99 TF_CUDA_EXT=".$TF_CUDA_VERSION"
100 fi
101 if [ -e $CUDA_TOOLKIT_PATH/lib64/libcudart.so$TF_CUDA_EXT ]; then
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800102 break
103 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800104 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 -0800105 if [ -z "$fromuser" ]; then
106 exit 1
107 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800108 # Retry
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800109 TF_CUDA_VERSION=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800110 CUDA_TOOLKIT_PATH=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800111done
112
Martin Wicke916776a2016-01-14 07:30:00 -0800113# Find out where the cuDNN library is installed
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800114while true; do
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800115 # Configure the Cudnn version to use.
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800116 if [ -z "$TF_CUDNN_VERSION" ]; then
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800117 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 -0800118 fi
119
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800120 fromuser=""
121 if [ -z "$CUDNN_INSTALL_PATH" ]; then
122 default_cudnn_path=${CUDA_TOOLKIT_PATH}
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800123 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 -0800124 fromuser="1"
125 if [ -z "$CUDNN_INSTALL_PATH" ]; then
126 CUDNN_INSTALL_PATH=$default_cudnn_path
127 fi
128 # Result returned from "read" will be used unexpanded. That make "~" unuseable.
129 # Going through one more level of expansion to handle that.
130 CUDNN_INSTALL_PATH=$(bash -c "readlink -f $CUDNN_INSTALL_PATH")
131 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800132 if [[ -z "$TF_CUDNN_VERSION" ]]; then
133 TF_CUDNN_EXT=""
134 else
135 TF_CUDNN_EXT=".$TF_CUDNN_VERSION"
136 fi
Eugene Brevdo56f1d642016-03-10 17:18:30 -0800137 if [ -e "$CUDNN_INSTALL_PATH/libcudnn.so${TF_CUDNN_EXT}" -o -e "$CUDNN_INSTALL_PATH/lib64/libcudnn.so${TF_CUDNN_EXT}" ]; then
138 break
139 fi
140 CUDNN_PATH_FROM_LDCONFIG="$(ldconfig -p | sed -n 's/.*libcudnn.so .* => \(.*\)/\1/p')"
141 if [ -e "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}" ]; then
142 CUDNN_INSTALL_PATH="$(dirname ${CUDNN_PATH_FROM_LDCONFIG})"
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800143 break
144 fi
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800145 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 -0800146 echo "$CUDNN_INSTALL_PATH/lib64/libcudnn.so${TF_CUDNN_EXT}"
147 echo "$CUDNN_INSTALL_PATH/libcudnn.so${TF_CUDNN_EXT}"
Eugene Brevdo56f1d642016-03-10 17:18:30 -0800148 echo "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}"
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800149 if [ -z "$fromuser" ]; then
150 exit 1
151 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800152 # Retry
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800153 TF_CUDNN_VERSION=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800154 CUDNN_INSTALL_PATH=""
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800155done
156
157cat > third_party/gpus/cuda/cuda.config <<EOF
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800158# CUDA_TOOLKIT_PATH refers to the CUDA toolkit.
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800159CUDA_TOOLKIT_PATH="$CUDA_TOOLKIT_PATH"
160
Martin Wicke916776a2016-01-14 07:30:00 -0800161# CUDNN_INSTALL_PATH refers to the cuDNN toolkit. The cuDNN header and library
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800162# files can be either in this directory, or under include/ and lib64/
163# directories separately.
164CUDNN_INSTALL_PATH="$CUDNN_INSTALL_PATH"
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800165
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800166# The Cuda SDK version that should be used in this build (empty to use libcudart.so symlink)
167TF_CUDA_VERSION=$TF_CUDA_EXT
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800168
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800169# The Cudnn version that should be used in this build (empty to use libcudnn.so symlink)
170TF_CUDNN_VERSION=$TF_CUDNN_EXT
A. Unique TensorFlower8a597482016-01-29 09:34:18 -0800171
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800172EOF
173
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -0800174# Configure the gcc host compiler to use
175export WARNING=$DO_NOT_SUBMIT_WARNING
176perl -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
177perl -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
178
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800179# Configure the Cuda toolkit version to work with.
Vijay Vasudevan97f6b6f2016-02-25 13:44:06 -0800180perl -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 -0800181perl -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 -0800182
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800183# Configure the Cudnn version to work with.
Vijay Vasudevan97f6b6f2016-02-25 13:44:06 -0800184perl -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 -0800185perl -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 -0800186
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800187# Configure the compute capabilities that TensorFlow builds for.
188# Since Cuda toolkit is not backward-compatible, this is not guaranteed to work.
189while true; do
190 fromuser=""
191 if [ -z "$TF_CUDA_COMPUTE_CAPABILITIES" ]; then
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800192cat << EOF
193Please specify a list of comma-separated Cuda compute capabilities you want to build with.
194You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
195Please note that each additional compute capability significantly increases your build time and binary size.
196EOF
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800197 read -p "[Default is: \"3.5,5.2\"]: " TF_CUDA_COMPUTE_CAPABILITIES
198 fromuser=1
199 fi
200 # Check whether all capabilities from the input is valid
201 COMPUTE_CAPABILITIES=${TF_CUDA_COMPUTE_CAPABILITIES//,/ }
202 ALL_VALID=1
203 for CAPABILITY in $COMPUTE_CAPABILITIES; do
204 if [[ ! "$CAPABILITY" =~ [0-9]+.[0-9]+ ]]; then
205 echo "Invalid compute capability: " $CAPABILITY
206 ALL_VALID=0
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800207 break
208 fi
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800209 done
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800210 if [ "$ALL_VALID" == "0" ]; then
211 if [ -z "$fromuser" ]; then
212 exit 1
213 fi
214 else
215 break
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800216 fi
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800217 TF_CUDA_COMPUTE_CAPABILITIES=""
218done
Vijay Vasudevan4dffee72015-11-12 11:27:00 -0800219
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800220if [ ! -z "$TF_CUDA_COMPUTE_CAPABILITIES" ]; then
Vijay Vasudevan80a5a3e2016-03-29 18:23:11 -0800221 export WARNING=$DO_NOT_SUBMIT_WARNING
Vijay Vasudevanfe056f02016-02-17 11:42:30 -0800222 function CudaGenCodeOpts() {
223 OUTPUT=""
224 for CAPABILITY in $@; do
225 OUTPUT=${OUTPUT}" \"${CAPABILITY}\", "
226 done
227 echo $OUTPUT
228 }
229 export CUDA_GEN_CODES_OPTS=$(CudaGenCodeOpts ${TF_CUDA_COMPUTE_CAPABILITIES//,/ })
230 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
231 function CudaVersionOpts() {
232 OUTPUT=""
233 for CAPABILITY in $@; do
234 OUTPUT=$OUTPUT"CudaVersion(\"${CAPABILITY}\"), "
235 done
236 echo $OUTPUT
237 }
238 export CUDA_VERSION_OPTS=$(CudaVersionOpts ${TF_CUDA_COMPUTE_CAPABILITIES//,/ })
239 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 -0800240fi
241
Manjunath Kudlurf41959c2015-11-06 16:27:58 -0800242# Invoke the cuda_config.sh and set up the TensorFlow's canonical view of the Cuda libraries
243(cd third_party/gpus/cuda; ./cuda_config.sh;) || exit -1
244
245echo "Configuration finished"