A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # ============================================================================== |
| 15 | """configure script to get build parameters from user.""" |
| 16 | |
| 17 | from __future__ import absolute_import |
| 18 | from __future__ import division |
| 19 | from __future__ import print_function |
| 20 | |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 21 | import argparse |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 22 | import errno |
| 23 | import os |
| 24 | import platform |
| 25 | import re |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 26 | import subprocess |
| 27 | import sys |
| 28 | |
Andrew Selle | c9885ea | 2017-11-06 09:37:03 -0800 | [diff] [blame] | 29 | # pylint: disable=g-import-not-at-top |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 30 | try: |
| 31 | from shutil import which |
| 32 | except ImportError: |
| 33 | from distutils.spawn import find_executable as which |
Andrew Selle | c9885ea | 2017-11-06 09:37:03 -0800 | [diff] [blame] | 34 | # pylint: enable=g-import-not-at-top |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 35 | |
Gunhan Gulsoy | c59db93 | 2018-12-28 16:32:01 -0800 | [diff] [blame] | 36 | _DEFAULT_CUDA_VERSION = '10.0' |
Dandelion Man? | 90e42f3 | 2017-12-15 18:15:07 -0800 | [diff] [blame] | 37 | _DEFAULT_CUDNN_VERSION = '7' |
Smit Hinsu | fe7d1d9 | 2018-07-14 13:16:58 -0700 | [diff] [blame] | 38 | _DEFAULT_CUDA_COMPUTE_CAPABILITIES = '3.5,7.0' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 39 | _DEFAULT_CUDA_PATH = '/usr/local/cuda' |
| 40 | _DEFAULT_CUDA_PATH_LINUX = '/opt/cuda' |
| 41 | _DEFAULT_CUDA_PATH_WIN = ('C:/Program Files/NVIDIA GPU Computing ' |
| 42 | 'Toolkit/CUDA/v%s' % _DEFAULT_CUDA_VERSION) |
| 43 | _TF_OPENCL_VERSION = '1.2' |
| 44 | _DEFAULT_COMPUTECPP_TOOLKIT_PATH = '/usr/local/computecpp' |
Yifei Feng | b1d8c59 | 2017-11-22 13:42:21 -0800 | [diff] [blame] | 45 | _DEFAULT_TRISYCL_INCLUDE_DIR = '/usr/local/triSYCL/include' |
A. Unique TensorFlower | 82820ef | 2018-11-12 13:22:13 -0800 | [diff] [blame] | 46 | _SUPPORTED_ANDROID_NDK_VERSIONS = [10, 11, 12, 13, 14, 15, 16, 17, 18] |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 47 | |
| 48 | _DEFAULT_PROMPT_ASK_ATTEMPTS = 10 |
| 49 | |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 50 | _TF_BAZELRC_FILENAME = '.tf_configure.bazelrc' |
A. Unique TensorFlower | 7c5eb35 | 2018-10-01 07:15:23 -0700 | [diff] [blame] | 51 | _TF_WORKSPACE_ROOT = '' |
| 52 | _TF_BAZELRC = '' |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 53 | |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 54 | NCCL_LIB_PATHS = [ |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 55 | 'lib64/', 'lib/powerpc64le-linux-gnu/', 'lib/x86_64-linux-gnu/', '' |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 56 | ] |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 57 | |
A. Unique TensorFlower | 93e7073 | 2019-02-14 16:45:32 -0800 | [diff] [blame] | 58 | # List of files to be configured for using Bazel on Apple platforms. |
| 59 | APPLE_BAZEL_FILES = [ |
| 60 | 'tensorflow/lite/experimental/objc/BUILD', |
| 61 | 'tensorflow/lite/experimental/swift/BUILD' |
| 62 | ] |
| 63 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 64 | if platform.machine() == 'ppc64le': |
| 65 | _DEFAULT_TENSORRT_PATH_LINUX = '/usr/lib/powerpc64le-linux-gnu/' |
| 66 | else: |
| 67 | _DEFAULT_TENSORRT_PATH_LINUX = '/usr/lib/%s-linux-gnu' % platform.machine() |
| 68 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 69 | |
| 70 | class UserInputError(Exception): |
| 71 | pass |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 72 | |
| 73 | |
| 74 | def is_windows(): |
| 75 | return platform.system() == 'Windows' |
| 76 | |
| 77 | |
| 78 | def is_linux(): |
| 79 | return platform.system() == 'Linux' |
| 80 | |
| 81 | |
| 82 | def is_macos(): |
| 83 | return platform.system() == 'Darwin' |
| 84 | |
| 85 | |
| 86 | def is_ppc64le(): |
| 87 | return platform.machine() == 'ppc64le' |
| 88 | |
| 89 | |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 90 | def is_cygwin(): |
| 91 | return platform.system().startswith('CYGWIN_NT') |
| 92 | |
| 93 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 94 | def get_input(question): |
| 95 | try: |
| 96 | try: |
| 97 | answer = raw_input(question) |
| 98 | except NameError: |
| 99 | answer = input(question) # pylint: disable=bad-builtin |
| 100 | except EOFError: |
| 101 | answer = '' |
| 102 | return answer |
| 103 | |
| 104 | |
| 105 | def symlink_force(target, link_name): |
| 106 | """Force symlink, equivalent of 'ln -sf'. |
| 107 | |
| 108 | Args: |
| 109 | target: items to link to. |
| 110 | link_name: name of the link. |
| 111 | """ |
| 112 | try: |
| 113 | os.symlink(target, link_name) |
| 114 | except OSError as e: |
| 115 | if e.errno == errno.EEXIST: |
| 116 | os.remove(link_name) |
| 117 | os.symlink(target, link_name) |
| 118 | else: |
| 119 | raise e |
| 120 | |
| 121 | |
| 122 | def sed_in_place(filename, old, new): |
| 123 | """Replace old string with new string in file. |
| 124 | |
| 125 | Args: |
| 126 | filename: string for filename. |
| 127 | old: string to replace. |
| 128 | new: new string to replace to. |
| 129 | """ |
| 130 | with open(filename, 'r') as f: |
| 131 | filedata = f.read() |
| 132 | newdata = filedata.replace(old, new) |
| 133 | with open(filename, 'w') as f: |
| 134 | f.write(newdata) |
| 135 | |
| 136 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 137 | def write_to_bazelrc(line): |
| 138 | with open(_TF_BAZELRC, 'a') as f: |
| 139 | f.write(line + '\n') |
| 140 | |
| 141 | |
| 142 | def write_action_env_to_bazelrc(var_name, var): |
| 143 | write_to_bazelrc('build --action_env %s="%s"' % (var_name, str(var))) |
| 144 | |
| 145 | |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 146 | def run_shell(cmd, allow_non_zero=False): |
| 147 | if allow_non_zero: |
| 148 | try: |
| 149 | output = subprocess.check_output(cmd) |
| 150 | except subprocess.CalledProcessError as e: |
| 151 | output = e.output |
| 152 | else: |
| 153 | output = subprocess.check_output(cmd) |
| 154 | return output.decode('UTF-8').strip() |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 155 | |
| 156 | |
| 157 | def cygpath(path): |
| 158 | """Convert path from posix to windows.""" |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 159 | return os.path.abspath(path).replace('\\', '/') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 160 | |
| 161 | |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 162 | def get_python_path(environ_cp, python_bin_path): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 163 | """Get the python site package paths.""" |
| 164 | python_paths = [] |
| 165 | if environ_cp.get('PYTHONPATH'): |
| 166 | python_paths = environ_cp.get('PYTHONPATH').split(':') |
| 167 | try: |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 168 | library_paths = run_shell([ |
| 169 | python_bin_path, '-c', |
| 170 | 'import site; print("\\n".join(site.getsitepackages()))' |
| 171 | ]).split('\n') |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 172 | except subprocess.CalledProcessError: |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 173 | library_paths = [ |
| 174 | run_shell([ |
| 175 | python_bin_path, '-c', |
| 176 | 'from distutils.sysconfig import get_python_lib;' |
| 177 | 'print(get_python_lib())' |
| 178 | ]) |
| 179 | ] |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 180 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 181 | all_paths = set(python_paths + library_paths) |
| 182 | |
| 183 | paths = [] |
| 184 | for path in all_paths: |
| 185 | if os.path.isdir(path): |
| 186 | paths.append(path) |
| 187 | return paths |
| 188 | |
| 189 | |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 190 | def get_python_major_version(python_bin_path): |
| 191 | """Get the python major version.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 192 | return run_shell([python_bin_path, '-c', 'import sys; print(sys.version[0])']) |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 193 | |
| 194 | |
Gunhan Gulsoy | ed89a2b | 2017-09-19 18:36:26 -0700 | [diff] [blame] | 195 | def setup_python(environ_cp): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 196 | """Setup python related env variables.""" |
| 197 | # Get PYTHON_BIN_PATH, default is the current running python. |
| 198 | default_python_bin_path = sys.executable |
| 199 | ask_python_bin_path = ('Please specify the location of python. [Default is ' |
| 200 | '%s]: ') % default_python_bin_path |
| 201 | while True: |
| 202 | python_bin_path = get_from_env_or_user_or_default( |
| 203 | environ_cp, 'PYTHON_BIN_PATH', ask_python_bin_path, |
| 204 | default_python_bin_path) |
| 205 | # Check if the path is valid |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 206 | if os.path.isfile(python_bin_path) and os.access(python_bin_path, os.X_OK): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 207 | break |
| 208 | elif not os.path.exists(python_bin_path): |
| 209 | print('Invalid python path: %s cannot be found.' % python_bin_path) |
| 210 | else: |
| 211 | print('%s is not executable. Is it the python binary?' % python_bin_path) |
| 212 | environ_cp['PYTHON_BIN_PATH'] = '' |
| 213 | |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 214 | # Convert python path to Windows style before checking lib and version |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 215 | if is_windows() or is_cygwin(): |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 216 | python_bin_path = cygpath(python_bin_path) |
| 217 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 218 | # Get PYTHON_LIB_PATH |
| 219 | python_lib_path = environ_cp.get('PYTHON_LIB_PATH') |
| 220 | if not python_lib_path: |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 221 | python_lib_paths = get_python_path(environ_cp, python_bin_path) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 222 | if environ_cp.get('USE_DEFAULT_PYTHON_LIB_PATH') == '1': |
Vijay Vasudevan | a1fba7f | 2017-07-28 10:58:56 -0700 | [diff] [blame] | 223 | python_lib_path = python_lib_paths[0] |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 224 | else: |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 225 | print('Found possible Python library paths:\n %s' % |
| 226 | '\n '.join(python_lib_paths)) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 227 | default_python_lib_path = python_lib_paths[0] |
| 228 | python_lib_path = get_input( |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 229 | 'Please input the desired Python library path to use. ' |
| 230 | 'Default is [%s]\n' % python_lib_paths[0]) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 231 | if not python_lib_path: |
| 232 | python_lib_path = default_python_lib_path |
Vijay Vasudevan | a1fba7f | 2017-07-28 10:58:56 -0700 | [diff] [blame] | 233 | environ_cp['PYTHON_LIB_PATH'] = python_lib_path |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 234 | |
TensorFlower Gardener | 61a8720 | 2018-10-01 12:25:39 -0700 | [diff] [blame] | 235 | _ = get_python_major_version(python_bin_path) |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 236 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 237 | # Convert python path to Windows style before writing into bazel.rc |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 238 | if is_windows() or is_cygwin(): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 239 | python_lib_path = cygpath(python_lib_path) |
| 240 | |
| 241 | # Set-up env variables used by python_configure.bzl |
| 242 | write_action_env_to_bazelrc('PYTHON_BIN_PATH', python_bin_path) |
| 243 | write_action_env_to_bazelrc('PYTHON_LIB_PATH', python_lib_path) |
Gunhan Gulsoy | ed89a2b | 2017-09-19 18:36:26 -0700 | [diff] [blame] | 244 | write_to_bazelrc('build --python_path=\"%s"' % python_bin_path) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 245 | environ_cp['PYTHON_BIN_PATH'] = python_bin_path |
| 246 | |
William D. Irons | dcc76a5 | 2018-11-20 10:35:18 -0600 | [diff] [blame] | 247 | # If choosen python_lib_path is from a path specified in the PYTHONPATH |
| 248 | # variable, need to tell bazel to include PYTHONPATH |
| 249 | if environ_cp.get('PYTHONPATH'): |
| 250 | python_paths = environ_cp.get('PYTHONPATH').split(':') |
| 251 | if python_lib_path in python_paths: |
TensorFlower Gardener | 968cd18 | 2018-11-28 11:33:16 -0800 | [diff] [blame] | 252 | write_action_env_to_bazelrc('PYTHONPATH', environ_cp.get('PYTHONPATH')) |
William D. Irons | dcc76a5 | 2018-11-20 10:35:18 -0600 | [diff] [blame] | 253 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 254 | # Write tools/python_bin_path.sh |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 255 | with open( |
| 256 | os.path.join(_TF_WORKSPACE_ROOT, 'tools', 'python_bin_path.sh'), |
| 257 | 'w') as f: |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 258 | f.write('export PYTHON_BIN_PATH="%s"' % python_bin_path) |
| 259 | |
| 260 | |
A. Unique TensorFlower | 7c5eb35 | 2018-10-01 07:15:23 -0700 | [diff] [blame] | 261 | def reset_tf_configure_bazelrc(): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 262 | """Reset file that contains customized config settings.""" |
| 263 | open(_TF_BAZELRC, 'w').close() |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 264 | |
TensorFlower Gardener | abb763b | 2019-02-06 16:23:03 -0800 | [diff] [blame] | 265 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 266 | def cleanup_makefile(): |
| 267 | """Delete any leftover BUILD files from the Makefile build. |
| 268 | |
| 269 | These files could interfere with Bazel parsing. |
| 270 | """ |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 271 | makefile_download_dir = os.path.join(_TF_WORKSPACE_ROOT, 'tensorflow', |
| 272 | 'contrib', 'makefile', 'downloads') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 273 | if os.path.isdir(makefile_download_dir): |
| 274 | for root, _, filenames in os.walk(makefile_download_dir): |
| 275 | for f in filenames: |
| 276 | if f.endswith('BUILD'): |
| 277 | os.remove(os.path.join(root, f)) |
| 278 | |
| 279 | |
| 280 | def get_var(environ_cp, |
| 281 | var_name, |
| 282 | query_item, |
| 283 | enabled_by_default, |
| 284 | question=None, |
| 285 | yes_reply=None, |
| 286 | no_reply=None): |
| 287 | """Get boolean input from user. |
| 288 | |
| 289 | If var_name is not set in env, ask user to enable query_item or not. If the |
| 290 | response is empty, use the default. |
| 291 | |
| 292 | Args: |
| 293 | environ_cp: copy of the os.environ. |
| 294 | var_name: string for name of environment variable, e.g. "TF_NEED_HDFS". |
| 295 | query_item: string for feature related to the variable, e.g. "Hadoop File |
| 296 | System". |
| 297 | enabled_by_default: boolean for default behavior. |
| 298 | question: optional string for how to ask for user input. |
Michael Case | d90054e | 2018-02-07 14:36:00 -0800 | [diff] [blame] | 299 | yes_reply: optional string for reply when feature is enabled. |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 300 | no_reply: optional string for reply when feature is disabled. |
| 301 | |
| 302 | Returns: |
| 303 | boolean value of the variable. |
Frank Chen | c4ef927 | 2018-01-10 11:36:52 -0800 | [diff] [blame] | 304 | |
| 305 | Raises: |
| 306 | UserInputError: if an environment variable is set, but it cannot be |
| 307 | interpreted as a boolean indicator, assume that the user has made a |
| 308 | scripting error, and will continue to provide invalid input. |
| 309 | Raise the error to avoid infinitely looping. |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 310 | """ |
| 311 | if not question: |
| 312 | question = 'Do you wish to build TensorFlow with %s support?' % query_item |
| 313 | if not yes_reply: |
| 314 | yes_reply = '%s support will be enabled for TensorFlow.' % query_item |
| 315 | if not no_reply: |
| 316 | no_reply = 'No %s' % yes_reply |
| 317 | |
| 318 | yes_reply += '\n' |
| 319 | no_reply += '\n' |
| 320 | |
| 321 | if enabled_by_default: |
| 322 | question += ' [Y/n]: ' |
| 323 | else: |
| 324 | question += ' [y/N]: ' |
| 325 | |
| 326 | var = environ_cp.get(var_name) |
Frank Chen | c4ef927 | 2018-01-10 11:36:52 -0800 | [diff] [blame] | 327 | if var is not None: |
| 328 | var_content = var.strip().lower() |
| 329 | true_strings = ('1', 't', 'true', 'y', 'yes') |
| 330 | false_strings = ('0', 'f', 'false', 'n', 'no') |
| 331 | if var_content in true_strings: |
| 332 | var = True |
| 333 | elif var_content in false_strings: |
| 334 | var = False |
| 335 | else: |
| 336 | raise UserInputError( |
| 337 | 'Environment variable %s must be set as a boolean indicator.\n' |
| 338 | 'The following are accepted as TRUE : %s.\n' |
| 339 | 'The following are accepted as FALSE: %s.\n' |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 340 | 'Current value is %s.' % (var_name, ', '.join(true_strings), |
| 341 | ', '.join(false_strings), var)) |
Frank Chen | c4ef927 | 2018-01-10 11:36:52 -0800 | [diff] [blame] | 342 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 343 | while var is None: |
| 344 | user_input_origin = get_input(question) |
| 345 | user_input = user_input_origin.strip().lower() |
| 346 | if user_input == 'y': |
| 347 | print(yes_reply) |
| 348 | var = True |
| 349 | elif user_input == 'n': |
| 350 | print(no_reply) |
| 351 | var = False |
| 352 | elif not user_input: |
| 353 | if enabled_by_default: |
| 354 | print(yes_reply) |
| 355 | var = True |
| 356 | else: |
| 357 | print(no_reply) |
| 358 | var = False |
| 359 | else: |
| 360 | print('Invalid selection: %s' % user_input_origin) |
| 361 | return var |
| 362 | |
| 363 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 364 | def set_build_var(environ_cp, |
| 365 | var_name, |
| 366 | query_item, |
| 367 | option_name, |
| 368 | enabled_by_default, |
| 369 | bazel_config_name=None): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 370 | """Set if query_item will be enabled for the build. |
| 371 | |
| 372 | Ask user if query_item will be enabled. Default is used if no input is given. |
| 373 | Set subprocess environment variable and write to .bazelrc if enabled. |
| 374 | |
| 375 | Args: |
| 376 | environ_cp: copy of the os.environ. |
| 377 | var_name: string for name of environment variable, e.g. "TF_NEED_HDFS". |
| 378 | query_item: string for feature related to the variable, e.g. "Hadoop File |
| 379 | System". |
| 380 | option_name: string for option to define in .bazelrc. |
| 381 | enabled_by_default: boolean for default behavior. |
Michael Case | 98850a5 | 2017-09-14 13:35:57 -0700 | [diff] [blame] | 382 | bazel_config_name: Name for Bazel --config argument to enable build feature. |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 383 | """ |
| 384 | |
| 385 | var = str(int(get_var(environ_cp, var_name, query_item, enabled_by_default))) |
| 386 | environ_cp[var_name] = var |
| 387 | if var == '1': |
Yifei Feng | ec451f5 | 2018-10-05 12:53:50 -0700 | [diff] [blame] | 388 | write_to_bazelrc( |
| 389 | 'build:%s --define %s=true' % (bazel_config_name, option_name)) |
| 390 | write_to_bazelrc('build --config=%s' % bazel_config_name) |
Michael Case | 98850a5 | 2017-09-14 13:35:57 -0700 | [diff] [blame] | 391 | elif bazel_config_name is not None: |
| 392 | # TODO(mikecase): Migrate all users of configure.py to use --config Bazel |
| 393 | # options and not to set build configs through environment variables. |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 394 | write_to_bazelrc( |
| 395 | 'build:%s --define %s=true' % (bazel_config_name, option_name)) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 396 | |
| 397 | |
| 398 | def set_action_env_var(environ_cp, |
| 399 | var_name, |
| 400 | query_item, |
| 401 | enabled_by_default, |
| 402 | question=None, |
| 403 | yes_reply=None, |
| 404 | no_reply=None): |
| 405 | """Set boolean action_env variable. |
| 406 | |
| 407 | Ask user if query_item will be enabled. Default is used if no input is given. |
| 408 | Set environment variable and write to .bazelrc. |
| 409 | |
| 410 | Args: |
| 411 | environ_cp: copy of the os.environ. |
| 412 | var_name: string for name of environment variable, e.g. "TF_NEED_HDFS". |
| 413 | query_item: string for feature related to the variable, e.g. "Hadoop File |
| 414 | System". |
| 415 | enabled_by_default: boolean for default behavior. |
| 416 | question: optional string for how to ask for user input. |
Michael Case | d90054e | 2018-02-07 14:36:00 -0800 | [diff] [blame] | 417 | yes_reply: optional string for reply when feature is enabled. |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 418 | no_reply: optional string for reply when feature is disabled. |
| 419 | """ |
| 420 | var = int( |
| 421 | get_var(environ_cp, var_name, query_item, enabled_by_default, question, |
| 422 | yes_reply, no_reply)) |
| 423 | |
| 424 | write_action_env_to_bazelrc(var_name, var) |
| 425 | environ_cp[var_name] = str(var) |
| 426 | |
| 427 | |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 428 | def convert_version_to_int(version): |
| 429 | """Convert a version number to a integer that can be used to compare. |
| 430 | |
A. Unique TensorFlower | 28ce1d1 | 2017-08-15 12:08:29 -0700 | [diff] [blame] | 431 | Version strings of the form X.YZ and X.Y.Z-xxxxx are supported. The |
| 432 | 'xxxxx' part, for instance 'homebrew' on OS/X, is ignored. |
| 433 | |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 434 | Args: |
A. Unique TensorFlower | 28ce1d1 | 2017-08-15 12:08:29 -0700 | [diff] [blame] | 435 | version: a version to be converted |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 436 | |
| 437 | Returns: |
| 438 | An integer if converted successfully, otherwise return None. |
| 439 | """ |
A. Unique TensorFlower | 28ce1d1 | 2017-08-15 12:08:29 -0700 | [diff] [blame] | 440 | version = version.split('-')[0] |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 441 | version_segments = version.split('.') |
| 442 | for seg in version_segments: |
| 443 | if not seg.isdigit(): |
| 444 | return None |
| 445 | |
| 446 | version_str = ''.join(['%03d' % int(seg) for seg in version_segments]) |
| 447 | return int(version_str) |
| 448 | |
| 449 | |
Mihai Maruseac | e7a123f | 2018-11-30 13:33:25 -0800 | [diff] [blame] | 450 | def check_bazel_version(min_version, max_version): |
| 451 | """Check installed bazel version is between min_version and max_version. |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 452 | |
| 453 | Args: |
| 454 | min_version: string for minimum bazel version. |
Mihai Maruseac | e7a123f | 2018-11-30 13:33:25 -0800 | [diff] [blame] | 455 | max_version: string for maximum bazel version. |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 456 | |
| 457 | Returns: |
| 458 | The bazel version detected. |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 459 | """ |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 460 | if which('bazel') is None: |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 461 | print('Cannot find bazel. Please install bazel.') |
| 462 | sys.exit(0) |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 463 | curr_version = run_shell( |
| 464 | ['bazel', '--batch', '--bazelrc=/dev/null', 'version']) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 465 | |
| 466 | for line in curr_version.split('\n'): |
| 467 | if 'Build label: ' in line: |
| 468 | curr_version = line.split('Build label: ')[1] |
| 469 | break |
| 470 | |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 471 | min_version_int = convert_version_to_int(min_version) |
| 472 | curr_version_int = convert_version_to_int(curr_version) |
Mihai Maruseac | e7a123f | 2018-11-30 13:33:25 -0800 | [diff] [blame] | 473 | max_version_int = convert_version_to_int(max_version) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 474 | |
| 475 | # Check if current bazel version can be detected properly. |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 476 | if not curr_version_int: |
| 477 | print('WARNING: current bazel installation is not a release version.') |
| 478 | print('Make sure you are running at least bazel %s' % min_version) |
| 479 | return curr_version |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 480 | |
Michael Case | d94271a | 2017-08-22 17:26:52 -0700 | [diff] [blame] | 481 | print('You have bazel %s installed.' % curr_version) |
A. Unique TensorFlower | 28ce1d1 | 2017-08-15 12:08:29 -0700 | [diff] [blame] | 482 | |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 483 | if curr_version_int < min_version_int: |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 484 | print('Please upgrade your bazel installation to version %s or higher to ' |
| 485 | 'build TensorFlow!' % min_version) |
Jason Zaman | b41761c | 2018-10-14 11:28:53 +0800 | [diff] [blame] | 486 | sys.exit(1) |
TensorFlower Gardener | 78c246b | 2018-12-13 12:37:42 -0800 | [diff] [blame] | 487 | if (curr_version_int > max_version_int and |
| 488 | 'TF_IGNORE_MAX_BAZEL_VERSION' not in os.environ): |
Mihai Maruseac | e7a123f | 2018-11-30 13:33:25 -0800 | [diff] [blame] | 489 | print('Please downgrade your bazel installation to version %s or lower to ' |
Mihai Maruseac | e0963c4 | 2018-12-20 14:27:40 -0800 | [diff] [blame] | 490 | 'build TensorFlow! To downgrade: download the installer for the old ' |
| 491 | 'version (from https://github.com/bazelbuild/bazel/releases) then ' |
| 492 | 'run the installer.' % max_version) |
Jason Zaman | b41761c | 2018-10-14 11:28:53 +0800 | [diff] [blame] | 493 | sys.exit(1) |
A. Unique TensorFlower | 6252d29 | 2017-08-04 00:52:34 -0700 | [diff] [blame] | 494 | return curr_version |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 495 | |
| 496 | |
| 497 | def set_cc_opt_flags(environ_cp): |
| 498 | """Set up architecture-dependent optimization flags. |
| 499 | |
| 500 | Also append CC optimization flags to bazel.rc.. |
| 501 | |
| 502 | Args: |
| 503 | environ_cp: copy of the os.environ. |
| 504 | """ |
| 505 | if is_ppc64le(): |
| 506 | # gcc on ppc64le does not support -march, use mcpu instead |
| 507 | default_cc_opt_flags = '-mcpu=native' |
A. Unique TensorFlower | 1bba94a | 2018-04-04 15:45:20 -0700 | [diff] [blame] | 508 | elif is_windows(): |
| 509 | default_cc_opt_flags = '/arch:AVX' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 510 | else: |
Justin Lebar | 9ef04f5 | 2018-10-10 18:52:45 -0700 | [diff] [blame] | 511 | default_cc_opt_flags = '-march=native -Wno-sign-compare' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 512 | question = ('Please specify optimization flags to use during compilation when' |
| 513 | ' bazel option "--config=opt" is specified [Default is %s]: ' |
| 514 | ) % default_cc_opt_flags |
| 515 | cc_opt_flags = get_from_env_or_user_or_default(environ_cp, 'CC_OPT_FLAGS', |
| 516 | question, default_cc_opt_flags) |
| 517 | for opt in cc_opt_flags.split(): |
Michael Case | 0017742 | 2017-11-10 13:14:03 -0800 | [diff] [blame] | 518 | write_to_bazelrc('build:opt --copt=%s' % opt) |
| 519 | # It should be safe on the same build host. |
A. Unique TensorFlower | 1bba94a | 2018-04-04 15:45:20 -0700 | [diff] [blame] | 520 | if not is_ppc64le() and not is_windows(): |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 521 | write_to_bazelrc('build:opt --host_copt=-march=native') |
Michael Case | bb3355d | 2017-11-09 08:46:31 -0800 | [diff] [blame] | 522 | write_to_bazelrc('build:opt --define with_default_optimizations=true') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 523 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 524 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 525 | def set_tf_cuda_clang(environ_cp): |
| 526 | """set TF_CUDA_CLANG action_env. |
| 527 | |
| 528 | Args: |
| 529 | environ_cp: copy of the os.environ. |
| 530 | """ |
| 531 | question = 'Do you want to use clang as CUDA compiler?' |
| 532 | yes_reply = 'Clang will be used as CUDA compiler.' |
| 533 | no_reply = 'nvcc will be used as CUDA compiler.' |
| 534 | set_action_env_var( |
| 535 | environ_cp, |
| 536 | 'TF_CUDA_CLANG', |
| 537 | None, |
| 538 | False, |
| 539 | question=question, |
| 540 | yes_reply=yes_reply, |
| 541 | no_reply=no_reply) |
| 542 | |
| 543 | |
A. Unique TensorFlower | fd29e95 | 2017-12-22 03:07:51 -0800 | [diff] [blame] | 544 | def set_tf_download_clang(environ_cp): |
| 545 | """Set TF_DOWNLOAD_CLANG action_env.""" |
Ilya Biryukov | 9e651e4 | 2018-03-22 05:33:42 -0700 | [diff] [blame] | 546 | question = 'Do you wish to download a fresh release of clang? (Experimental)' |
A. Unique TensorFlower | fd29e95 | 2017-12-22 03:07:51 -0800 | [diff] [blame] | 547 | yes_reply = 'Clang will be downloaded and used to compile tensorflow.' |
| 548 | no_reply = 'Clang will not be downloaded.' |
| 549 | set_action_env_var( |
| 550 | environ_cp, |
| 551 | 'TF_DOWNLOAD_CLANG', |
| 552 | None, |
| 553 | False, |
| 554 | question=question, |
| 555 | yes_reply=yes_reply, |
| 556 | no_reply=no_reply) |
| 557 | |
| 558 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 559 | def get_from_env_or_user_or_default(environ_cp, var_name, ask_for_var, |
| 560 | var_default): |
| 561 | """Get var_name either from env, or user or default. |
| 562 | |
| 563 | If var_name has been set as environment variable, use the preset value, else |
| 564 | ask for user input. If no input is provided, the default is used. |
| 565 | |
| 566 | Args: |
| 567 | environ_cp: copy of the os.environ. |
| 568 | var_name: string for name of environment variable, e.g. "TF_NEED_HDFS". |
| 569 | ask_for_var: string for how to ask for user input. |
| 570 | var_default: default value string. |
| 571 | |
| 572 | Returns: |
| 573 | string value for var_name |
| 574 | """ |
| 575 | var = environ_cp.get(var_name) |
| 576 | if not var: |
| 577 | var = get_input(ask_for_var) |
Michael Case | d94271a | 2017-08-22 17:26:52 -0700 | [diff] [blame] | 578 | print('\n') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 579 | if not var: |
| 580 | var = var_default |
| 581 | return var |
| 582 | |
| 583 | |
| 584 | def set_clang_cuda_compiler_path(environ_cp): |
| 585 | """Set CLANG_CUDA_COMPILER_PATH.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 586 | default_clang_path = which('clang') or '' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 587 | ask_clang_path = ('Please specify which clang should be used as device and ' |
| 588 | 'host compiler. [Default is %s]: ') % default_clang_path |
| 589 | |
| 590 | while True: |
| 591 | clang_cuda_compiler_path = get_from_env_or_user_or_default( |
| 592 | environ_cp, 'CLANG_CUDA_COMPILER_PATH', ask_clang_path, |
| 593 | default_clang_path) |
| 594 | if os.path.exists(clang_cuda_compiler_path): |
| 595 | break |
| 596 | |
| 597 | # Reset and retry |
| 598 | print('Invalid clang path: %s cannot be found.' % clang_cuda_compiler_path) |
| 599 | environ_cp['CLANG_CUDA_COMPILER_PATH'] = '' |
| 600 | |
| 601 | # Set CLANG_CUDA_COMPILER_PATH |
| 602 | environ_cp['CLANG_CUDA_COMPILER_PATH'] = clang_cuda_compiler_path |
| 603 | write_action_env_to_bazelrc('CLANG_CUDA_COMPILER_PATH', |
| 604 | clang_cuda_compiler_path) |
| 605 | |
| 606 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 607 | def prompt_loop_or_load_from_env(environ_cp, |
| 608 | var_name, |
| 609 | var_default, |
| 610 | ask_for_var, |
| 611 | check_success, |
| 612 | error_msg, |
| 613 | suppress_default_error=False, |
| 614 | n_ask_attempts=_DEFAULT_PROMPT_ASK_ATTEMPTS): |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 615 | """Loop over user prompts for an ENV param until receiving a valid response. |
| 616 | |
| 617 | For the env param var_name, read from the environment or verify user input |
| 618 | until receiving valid input. When done, set var_name in the environ_cp to its |
| 619 | new value. |
| 620 | |
| 621 | Args: |
| 622 | environ_cp: (Dict) copy of the os.environ. |
| 623 | var_name: (String) string for name of environment variable, e.g. "TF_MYVAR". |
| 624 | var_default: (String) default value string. |
| 625 | ask_for_var: (String) string for how to ask for user input. |
| 626 | check_success: (Function) function that takes one argument and returns a |
| 627 | boolean. Should return True if the value provided is considered valid. May |
| 628 | contain a complex error message if error_msg does not provide enough |
| 629 | information. In that case, set suppress_default_error to True. |
| 630 | error_msg: (String) String with one and only one '%s'. Formatted with each |
| 631 | invalid response upon check_success(input) failure. |
| 632 | suppress_default_error: (Bool) Suppress the above error message in favor of |
| 633 | one from the check_success function. |
| 634 | n_ask_attempts: (Integer) Number of times to query for valid input before |
| 635 | raising an error and quitting. |
| 636 | |
| 637 | Returns: |
| 638 | [String] The value of var_name after querying for input. |
| 639 | |
| 640 | Raises: |
| 641 | UserInputError: if a query has been attempted n_ask_attempts times without |
Frank Chen | c4ef927 | 2018-01-10 11:36:52 -0800 | [diff] [blame] | 642 | success, assume that the user has made a scripting error, and will |
| 643 | continue to provide invalid input. Raise the error to avoid infinitely |
| 644 | looping. |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 645 | """ |
| 646 | default = environ_cp.get(var_name) or var_default |
| 647 | full_query = '%s [Default is %s]: ' % ( |
| 648 | ask_for_var, |
| 649 | default, |
| 650 | ) |
| 651 | |
| 652 | for _ in range(n_ask_attempts): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 653 | val = get_from_env_or_user_or_default(environ_cp, var_name, full_query, |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 654 | default) |
| 655 | if check_success(val): |
| 656 | break |
| 657 | if not suppress_default_error: |
| 658 | print(error_msg % val) |
| 659 | environ_cp[var_name] = '' |
| 660 | else: |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 661 | raise UserInputError( |
| 662 | 'Invalid %s setting was provided %d times in a row. ' |
| 663 | 'Assuming to be a scripting mistake.' % (var_name, n_ask_attempts)) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 664 | |
| 665 | environ_cp[var_name] = val |
| 666 | return val |
| 667 | |
| 668 | |
| 669 | def create_android_ndk_rule(environ_cp): |
| 670 | """Set ANDROID_NDK_HOME and write Android NDK WORKSPACE rule.""" |
| 671 | if is_windows() or is_cygwin(): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 672 | default_ndk_path = cygpath( |
| 673 | '%s/Android/Sdk/ndk-bundle' % environ_cp['APPDATA']) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 674 | elif is_macos(): |
| 675 | default_ndk_path = '%s/library/Android/Sdk/ndk-bundle' % environ_cp['HOME'] |
| 676 | else: |
| 677 | default_ndk_path = '%s/Android/Sdk/ndk-bundle' % environ_cp['HOME'] |
| 678 | |
| 679 | def valid_ndk_path(path): |
| 680 | return (os.path.exists(path) and |
| 681 | os.path.exists(os.path.join(path, 'source.properties'))) |
| 682 | |
| 683 | android_ndk_home_path = prompt_loop_or_load_from_env( |
| 684 | environ_cp, |
| 685 | var_name='ANDROID_NDK_HOME', |
| 686 | var_default=default_ndk_path, |
| 687 | ask_for_var='Please specify the home path of the Android NDK to use.', |
| 688 | check_success=valid_ndk_path, |
| 689 | error_msg=('The path %s or its child file "source.properties" ' |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 690 | 'does not exist.')) |
Michael Case | 5105350 | 2018-06-05 17:47:19 -0700 | [diff] [blame] | 691 | write_action_env_to_bazelrc('ANDROID_NDK_HOME', android_ndk_home_path) |
| 692 | write_action_env_to_bazelrc('ANDROID_NDK_API_LEVEL', |
| 693 | check_ndk_level(android_ndk_home_path)) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 694 | |
| 695 | |
| 696 | def create_android_sdk_rule(environ_cp): |
| 697 | """Set Android variables and write Android SDK WORKSPACE rule.""" |
| 698 | if is_windows() or is_cygwin(): |
| 699 | default_sdk_path = cygpath('%s/Android/Sdk' % environ_cp['APPDATA']) |
| 700 | elif is_macos(): |
Shashi Shekhar | c0ff0cc | 2018-07-17 09:00:24 -0700 | [diff] [blame] | 701 | default_sdk_path = '%s/library/Android/Sdk' % environ_cp['HOME'] |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 702 | else: |
| 703 | default_sdk_path = '%s/Android/Sdk' % environ_cp['HOME'] |
| 704 | |
| 705 | def valid_sdk_path(path): |
| 706 | return (os.path.exists(path) and |
| 707 | os.path.exists(os.path.join(path, 'platforms')) and |
| 708 | os.path.exists(os.path.join(path, 'build-tools'))) |
| 709 | |
| 710 | android_sdk_home_path = prompt_loop_or_load_from_env( |
| 711 | environ_cp, |
| 712 | var_name='ANDROID_SDK_HOME', |
| 713 | var_default=default_sdk_path, |
| 714 | ask_for_var='Please specify the home path of the Android SDK to use.', |
| 715 | check_success=valid_sdk_path, |
| 716 | error_msg=('Either %s does not exist, or it does not contain the ' |
| 717 | 'subdirectories "platforms" and "build-tools".')) |
| 718 | |
| 719 | platforms = os.path.join(android_sdk_home_path, 'platforms') |
| 720 | api_levels = sorted(os.listdir(platforms)) |
| 721 | api_levels = [x.replace('android-', '') for x in api_levels] |
| 722 | |
| 723 | def valid_api_level(api_level): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 724 | return os.path.exists( |
| 725 | os.path.join(android_sdk_home_path, 'platforms', |
| 726 | 'android-' + api_level)) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 727 | |
| 728 | android_api_level = prompt_loop_or_load_from_env( |
| 729 | environ_cp, |
| 730 | var_name='ANDROID_API_LEVEL', |
| 731 | var_default=api_levels[-1], |
| 732 | ask_for_var=('Please specify the Android SDK API level to use. ' |
| 733 | '[Available levels: %s]') % api_levels, |
| 734 | check_success=valid_api_level, |
| 735 | error_msg='Android-%s is not present in the SDK path.') |
| 736 | |
| 737 | build_tools = os.path.join(android_sdk_home_path, 'build-tools') |
| 738 | versions = sorted(os.listdir(build_tools)) |
| 739 | |
| 740 | def valid_build_tools(version): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 741 | return os.path.exists( |
| 742 | os.path.join(android_sdk_home_path, 'build-tools', version)) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 743 | |
| 744 | android_build_tools_version = prompt_loop_or_load_from_env( |
| 745 | environ_cp, |
| 746 | var_name='ANDROID_BUILD_TOOLS_VERSION', |
| 747 | var_default=versions[-1], |
| 748 | ask_for_var=('Please specify an Android build tools version to use. ' |
| 749 | '[Available versions: %s]') % versions, |
| 750 | check_success=valid_build_tools, |
| 751 | error_msg=('The selected SDK does not have build-tools version %s ' |
| 752 | 'available.')) |
| 753 | |
Michael Case | 5105350 | 2018-06-05 17:47:19 -0700 | [diff] [blame] | 754 | write_action_env_to_bazelrc('ANDROID_BUILD_TOOLS_VERSION', |
| 755 | android_build_tools_version) |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 756 | write_action_env_to_bazelrc('ANDROID_SDK_API_LEVEL', android_api_level) |
| 757 | write_action_env_to_bazelrc('ANDROID_SDK_HOME', android_sdk_home_path) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 758 | |
| 759 | |
| 760 | def check_ndk_level(android_ndk_home_path): |
| 761 | """Check the revision number of an Android NDK path.""" |
| 762 | properties_path = '%s/source.properties' % android_ndk_home_path |
| 763 | if is_windows() or is_cygwin(): |
| 764 | properties_path = cygpath(properties_path) |
| 765 | with open(properties_path, 'r') as f: |
| 766 | filedata = f.read() |
| 767 | |
| 768 | revision = re.search(r'Pkg.Revision = (\d+)', filedata) |
| 769 | if revision: |
Michael Case | 5105350 | 2018-06-05 17:47:19 -0700 | [diff] [blame] | 770 | ndk_api_level = revision.group(1) |
| 771 | else: |
| 772 | raise Exception('Unable to parse NDK revision.') |
| 773 | if int(ndk_api_level) not in _SUPPORTED_ANDROID_NDK_VERSIONS: |
| 774 | print('WARNING: The API level of the NDK in %s is %s, which is not ' |
| 775 | 'supported by Bazel (officially supported versions: %s). Please use ' |
| 776 | 'another version. Compiling Android targets may result in confusing ' |
| 777 | 'errors.\n' % (android_ndk_home_path, ndk_api_level, |
| 778 | _SUPPORTED_ANDROID_NDK_VERSIONS)) |
| 779 | return ndk_api_level |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 780 | |
| 781 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 782 | def set_gcc_host_compiler_path(environ_cp): |
| 783 | """Set GCC_HOST_COMPILER_PATH.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 784 | default_gcc_host_compiler_path = which('gcc') or '' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 785 | cuda_bin_symlink = '%s/bin/gcc' % environ_cp.get('CUDA_TOOLKIT_PATH') |
| 786 | |
| 787 | if os.path.islink(cuda_bin_symlink): |
| 788 | # os.readlink is only available in linux |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 789 | default_gcc_host_compiler_path = os.path.realpath(cuda_bin_symlink) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 790 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 791 | gcc_host_compiler_path = prompt_loop_or_load_from_env( |
| 792 | environ_cp, |
| 793 | var_name='GCC_HOST_COMPILER_PATH', |
| 794 | var_default=default_gcc_host_compiler_path, |
TensorFlower Gardener | abb763b | 2019-02-06 16:23:03 -0800 | [diff] [blame] | 795 | ask_for_var='Please specify which gcc should be used by nvcc as the host compiler.', |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 796 | check_success=os.path.exists, |
| 797 | error_msg='Invalid gcc path. %s cannot be found.', |
| 798 | ) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 799 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 800 | write_action_env_to_bazelrc('GCC_HOST_COMPILER_PATH', gcc_host_compiler_path) |
| 801 | |
| 802 | |
Ankur Taly | 0e6f39d | 2018-02-16 18:22:55 -0800 | [diff] [blame] | 803 | def reformat_version_sequence(version_str, sequence_count): |
| 804 | """Reformat the version string to have the given number of sequences. |
| 805 | |
| 806 | For example: |
| 807 | Given (7, 2) -> 7.0 |
| 808 | (7.0.1, 2) -> 7.0 |
| 809 | (5, 1) -> 5 |
| 810 | (5.0.3.2, 1) -> 5 |
| 811 | |
| 812 | Args: |
| 813 | version_str: String, the version string. |
| 814 | sequence_count: int, an integer. |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 815 | |
Ankur Taly | 0e6f39d | 2018-02-16 18:22:55 -0800 | [diff] [blame] | 816 | Returns: |
| 817 | string, reformatted version string. |
| 818 | """ |
| 819 | v = version_str.split('.') |
| 820 | if len(v) < sequence_count: |
| 821 | v = v + (['0'] * (sequence_count - len(v))) |
| 822 | |
| 823 | return '.'.join(v[:sequence_count]) |
| 824 | |
| 825 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 826 | def set_tf_cuda_version(environ_cp): |
| 827 | """Set CUDA_TOOLKIT_PATH and TF_CUDA_VERSION.""" |
| 828 | ask_cuda_version = ( |
A. Unique TensorFlower | b15500b | 2018-05-08 12:04:38 -0700 | [diff] [blame] | 829 | 'Please specify the CUDA SDK version you want to use. ' |
| 830 | '[Leave empty to default to CUDA %s]: ') % _DEFAULT_CUDA_VERSION |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 831 | |
Austin Anderson | f9a88f8 | 2017-12-13 11:49:40 -0800 | [diff] [blame] | 832 | for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 833 | # Configure the Cuda SDK version to use. |
| 834 | tf_cuda_version = get_from_env_or_user_or_default( |
| 835 | environ_cp, 'TF_CUDA_VERSION', ask_cuda_version, _DEFAULT_CUDA_VERSION) |
Ankur Taly | 0e6f39d | 2018-02-16 18:22:55 -0800 | [diff] [blame] | 836 | tf_cuda_version = reformat_version_sequence(str(tf_cuda_version), 2) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 837 | |
| 838 | # Find out where the CUDA toolkit is installed |
| 839 | default_cuda_path = _DEFAULT_CUDA_PATH |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 840 | if is_windows() or is_cygwin(): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 841 | default_cuda_path = cygpath( |
| 842 | environ_cp.get('CUDA_PATH', _DEFAULT_CUDA_PATH_WIN)) |
| 843 | elif is_linux(): |
| 844 | # If the default doesn't exist, try an alternative default. |
| 845 | if (not os.path.exists(default_cuda_path) |
| 846 | ) and os.path.exists(_DEFAULT_CUDA_PATH_LINUX): |
| 847 | default_cuda_path = _DEFAULT_CUDA_PATH_LINUX |
| 848 | ask_cuda_path = ('Please specify the location where CUDA %s toolkit is' |
| 849 | ' installed. Refer to README.md for more details. ' |
| 850 | '[Default is %s]: ') % (tf_cuda_version, default_cuda_path) |
| 851 | cuda_toolkit_path = get_from_env_or_user_or_default( |
| 852 | environ_cp, 'CUDA_TOOLKIT_PATH', ask_cuda_path, default_cuda_path) |
A. Unique TensorFlower | 02f17fe | 2018-07-07 06:59:19 -0700 | [diff] [blame] | 853 | if is_windows() or is_cygwin(): |
| 854 | cuda_toolkit_path = cygpath(cuda_toolkit_path) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 855 | |
| 856 | if is_windows(): |
Niall Moran | b7d97e8 | 2018-08-09 00:29:49 +0100 | [diff] [blame] | 857 | cuda_rt_lib_paths = ['lib/x64/cudart.lib'] |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 858 | elif is_linux(): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 859 | cuda_rt_lib_paths = [ |
| 860 | '%s/libcudart.so.%s' % (x, tf_cuda_version) for x in [ |
| 861 | 'lib64', |
| 862 | 'lib/powerpc64le-linux-gnu', |
| 863 | 'lib/x86_64-linux-gnu', |
| 864 | ] |
| 865 | ] |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 866 | elif is_macos(): |
Niall Moran | b7d97e8 | 2018-08-09 00:29:49 +0100 | [diff] [blame] | 867 | cuda_rt_lib_paths = ['lib/libcudart.%s.dylib' % tf_cuda_version] |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 868 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 869 | cuda_toolkit_paths_full = [ |
| 870 | os.path.join(cuda_toolkit_path, x) for x in cuda_rt_lib_paths |
| 871 | ] |
Sergei Lebedev | 95d7bbb | 2018-11-21 10:40:10 -0800 | [diff] [blame] | 872 | if any(os.path.exists(x) for x in cuda_toolkit_paths_full): |
Yifei Feng | 5198cb8 | 2018-08-17 13:53:06 -0700 | [diff] [blame] | 873 | break |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 874 | |
| 875 | # Reset and retry |
| 876 | print('Invalid path to CUDA %s toolkit. %s cannot be found' % |
hellcom | 9a13fc3 | 2018-09-12 10:58:24 +0300 | [diff] [blame] | 877 | (tf_cuda_version, cuda_toolkit_paths_full)) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 878 | environ_cp['TF_CUDA_VERSION'] = '' |
| 879 | environ_cp['CUDA_TOOLKIT_PATH'] = '' |
| 880 | |
Austin Anderson | f9a88f8 | 2017-12-13 11:49:40 -0800 | [diff] [blame] | 881 | else: |
| 882 | raise UserInputError('Invalid TF_CUDA_SETTING setting was provided %d ' |
| 883 | 'times in a row. Assuming to be a scripting mistake.' % |
| 884 | _DEFAULT_PROMPT_ASK_ATTEMPTS) |
| 885 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 886 | # Set CUDA_TOOLKIT_PATH and TF_CUDA_VERSION |
| 887 | environ_cp['CUDA_TOOLKIT_PATH'] = cuda_toolkit_path |
| 888 | write_action_env_to_bazelrc('CUDA_TOOLKIT_PATH', cuda_toolkit_path) |
| 889 | environ_cp['TF_CUDA_VERSION'] = tf_cuda_version |
| 890 | write_action_env_to_bazelrc('TF_CUDA_VERSION', tf_cuda_version) |
| 891 | |
| 892 | |
Yifei Feng | b1d8c59 | 2017-11-22 13:42:21 -0800 | [diff] [blame] | 893 | def set_tf_cudnn_version(environ_cp): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 894 | """Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION.""" |
| 895 | ask_cudnn_version = ( |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 896 | 'Please specify the cuDNN version you want to use. ' |
A. Unique TensorFlower | 44acd83 | 2018-10-01 13:42:40 -0700 | [diff] [blame] | 897 | '[Leave empty to default to cuDNN %s]: ') % _DEFAULT_CUDNN_VERSION |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 898 | |
Austin Anderson | f9a88f8 | 2017-12-13 11:49:40 -0800 | [diff] [blame] | 899 | for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 900 | tf_cudnn_version = get_from_env_or_user_or_default( |
| 901 | environ_cp, 'TF_CUDNN_VERSION', ask_cudnn_version, |
| 902 | _DEFAULT_CUDNN_VERSION) |
Ankur Taly | 0e6f39d | 2018-02-16 18:22:55 -0800 | [diff] [blame] | 903 | tf_cudnn_version = reformat_version_sequence(str(tf_cudnn_version), 1) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 904 | |
| 905 | default_cudnn_path = environ_cp.get('CUDA_TOOLKIT_PATH') |
| 906 | ask_cudnn_path = (r'Please specify the location where cuDNN %s library is ' |
| 907 | 'installed. Refer to README.md for more details. [Default' |
A. Unique TensorFlower | 1b21235 | 2018-07-19 13:48:50 -0700 | [diff] [blame] | 908 | ' is %s]: ') % (tf_cudnn_version, default_cudnn_path) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 909 | cudnn_install_path = get_from_env_or_user_or_default( |
| 910 | environ_cp, 'CUDNN_INSTALL_PATH', ask_cudnn_path, default_cudnn_path) |
| 911 | |
| 912 | # Result returned from "read" will be used unexpanded. That make "~" |
| 913 | # unusable. Going through one more level of expansion to handle that. |
| 914 | cudnn_install_path = os.path.realpath( |
| 915 | os.path.expanduser(cudnn_install_path)) |
Martin Wicke | d57572e | 2017-09-02 19:21:45 -0700 | [diff] [blame] | 916 | if is_windows() or is_cygwin(): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 917 | cudnn_install_path = cygpath(cudnn_install_path) |
| 918 | |
| 919 | if is_windows(): |
| 920 | cuda_dnn_lib_path = 'lib/x64/cudnn.lib' |
| 921 | cuda_dnn_lib_alt_path = 'lib/x64/cudnn.lib' |
| 922 | elif is_linux(): |
| 923 | cuda_dnn_lib_path = 'lib64/libcudnn.so.%s' % tf_cudnn_version |
| 924 | cuda_dnn_lib_alt_path = 'libcudnn.so.%s' % tf_cudnn_version |
| 925 | elif is_macos(): |
| 926 | cuda_dnn_lib_path = 'lib/libcudnn.%s.dylib' % tf_cudnn_version |
| 927 | cuda_dnn_lib_alt_path = 'libcudnn.%s.dylib' % tf_cudnn_version |
| 928 | |
| 929 | cuda_dnn_lib_path_full = os.path.join(cudnn_install_path, cuda_dnn_lib_path) |
| 930 | cuda_dnn_lib_alt_path_full = os.path.join(cudnn_install_path, |
| 931 | cuda_dnn_lib_alt_path) |
| 932 | if os.path.exists(cuda_dnn_lib_path_full) or os.path.exists( |
| 933 | cuda_dnn_lib_alt_path_full): |
| 934 | break |
| 935 | |
| 936 | # Try another alternative for Linux |
| 937 | if is_linux(): |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 938 | ldconfig_bin = which('ldconfig') or '/sbin/ldconfig' |
| 939 | cudnn_path_from_ldconfig = run_shell([ldconfig_bin, '-p']) |
| 940 | cudnn_path_from_ldconfig = re.search('.*libcudnn.so .* => (.*)', |
A. Unique TensorFlower | e722358 | 2017-09-06 17:57:04 -0700 | [diff] [blame] | 941 | cudnn_path_from_ldconfig) |
| 942 | if cudnn_path_from_ldconfig: |
| 943 | cudnn_path_from_ldconfig = cudnn_path_from_ldconfig.group(1) |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 944 | if os.path.exists( |
| 945 | '%s.%s' % (cudnn_path_from_ldconfig, tf_cudnn_version)): |
A. Unique TensorFlower | e722358 | 2017-09-06 17:57:04 -0700 | [diff] [blame] | 946 | cudnn_install_path = os.path.dirname(cudnn_path_from_ldconfig) |
| 947 | break |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 948 | |
| 949 | # Reset and Retry |
| 950 | print( |
| 951 | 'Invalid path to cuDNN %s toolkit. None of the following files can be ' |
| 952 | 'found:' % tf_cudnn_version) |
| 953 | print(cuda_dnn_lib_path_full) |
| 954 | print(cuda_dnn_lib_alt_path_full) |
| 955 | if is_linux(): |
| 956 | print('%s.%s' % (cudnn_path_from_ldconfig, tf_cudnn_version)) |
| 957 | |
| 958 | environ_cp['TF_CUDNN_VERSION'] = '' |
Austin Anderson | f9a88f8 | 2017-12-13 11:49:40 -0800 | [diff] [blame] | 959 | else: |
| 960 | raise UserInputError('Invalid TF_CUDNN setting was provided %d ' |
| 961 | 'times in a row. Assuming to be a scripting mistake.' % |
| 962 | _DEFAULT_PROMPT_ASK_ATTEMPTS) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 963 | |
| 964 | # Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION |
| 965 | environ_cp['CUDNN_INSTALL_PATH'] = cudnn_install_path |
| 966 | write_action_env_to_bazelrc('CUDNN_INSTALL_PATH', cudnn_install_path) |
| 967 | environ_cp['TF_CUDNN_VERSION'] = tf_cudnn_version |
| 968 | write_action_env_to_bazelrc('TF_CUDNN_VERSION', tf_cudnn_version) |
| 969 | |
| 970 | |
Mingxing Tan | 1e7b0e4 | 2018-06-28 19:13:20 -0700 | [diff] [blame] | 971 | def is_cuda_compatible(lib, cuda_ver, cudnn_ver): |
| 972 | """Check compatibility between given library and cudnn/cudart libraries.""" |
| 973 | ldd_bin = which('ldd') or '/usr/bin/ldd' |
| 974 | ldd_out = run_shell([ldd_bin, lib], True) |
| 975 | ldd_out = ldd_out.split(os.linesep) |
| 976 | cudnn_pattern = re.compile('.*libcudnn.so\\.?(.*) =>.*$') |
| 977 | cuda_pattern = re.compile('.*libcudart.so\\.?(.*) =>.*$') |
| 978 | cudnn = None |
| 979 | cudart = None |
| 980 | cudnn_ok = True # assume no cudnn dependency by default |
| 981 | cuda_ok = True # assume no cuda dependency by default |
| 982 | for line in ldd_out: |
| 983 | if 'libcudnn.so' in line: |
| 984 | cudnn = cudnn_pattern.search(line) |
| 985 | cudnn_ok = False |
| 986 | elif 'libcudart.so' in line: |
| 987 | cudart = cuda_pattern.search(line) |
| 988 | cuda_ok = False |
| 989 | if cudnn and len(cudnn.group(1)): |
| 990 | cudnn = convert_version_to_int(cudnn.group(1)) |
| 991 | if cudart and len(cudart.group(1)): |
| 992 | cudart = convert_version_to_int(cudart.group(1)) |
| 993 | if cudnn is not None: |
| 994 | cudnn_ok = (cudnn == cudnn_ver) |
| 995 | if cudart is not None: |
| 996 | cuda_ok = (cudart == cuda_ver) |
| 997 | return cudnn_ok and cuda_ok |
| 998 | |
| 999 | |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1000 | def set_tf_tensorrt_install_path(environ_cp): |
| 1001 | """Set TENSORRT_INSTALL_PATH and TF_TENSORRT_VERSION. |
| 1002 | |
| 1003 | Adapted from code contributed by Sami Kama (https://github.com/samikama). |
| 1004 | |
| 1005 | Args: |
| 1006 | environ_cp: copy of the os.environ. |
| 1007 | |
| 1008 | Raises: |
| 1009 | ValueError: if this method was called under non-Linux platform. |
| 1010 | UserInputError: if user has provided invalid input multiple times. |
| 1011 | """ |
| 1012 | if not is_linux(): |
| 1013 | raise ValueError('Currently TensorRT is only supported on Linux platform.') |
| 1014 | |
| 1015 | # Ask user whether to add TensorRT support. |
Mingxing Tan | 1e7b0e4 | 2018-06-28 19:13:20 -0700 | [diff] [blame] | 1016 | if str(int(get_var(environ_cp, 'TF_NEED_TENSORRT', 'TensorRT', |
| 1017 | False))) != '1': |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1018 | return |
| 1019 | |
| 1020 | for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS): |
| 1021 | ask_tensorrt_path = (r'Please specify the location where TensorRT is ' |
| 1022 | 'installed. [Default is %s]:') % ( |
| 1023 | _DEFAULT_TENSORRT_PATH_LINUX) |
| 1024 | trt_install_path = get_from_env_or_user_or_default( |
| 1025 | environ_cp, 'TENSORRT_INSTALL_PATH', ask_tensorrt_path, |
| 1026 | _DEFAULT_TENSORRT_PATH_LINUX) |
| 1027 | |
| 1028 | # Result returned from "read" will be used unexpanded. That make "~" |
| 1029 | # unusable. Going through one more level of expansion to handle that. |
Mingxing Tan | 1e7b0e4 | 2018-06-28 19:13:20 -0700 | [diff] [blame] | 1030 | trt_install_path = os.path.realpath(os.path.expanduser(trt_install_path)) |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1031 | |
| 1032 | def find_libs(search_path): |
| 1033 | """Search for libnvinfer.so in "search_path".""" |
| 1034 | fl = set() |
| 1035 | if os.path.exists(search_path) and os.path.isdir(search_path): |
Mingxing Tan | 1e7b0e4 | 2018-06-28 19:13:20 -0700 | [diff] [blame] | 1036 | fl.update([ |
| 1037 | os.path.realpath(os.path.join(search_path, x)) |
| 1038 | for x in os.listdir(search_path) |
| 1039 | if 'libnvinfer.so' in x |
| 1040 | ]) |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1041 | return fl |
| 1042 | |
| 1043 | possible_files = find_libs(trt_install_path) |
| 1044 | possible_files.update(find_libs(os.path.join(trt_install_path, 'lib'))) |
| 1045 | possible_files.update(find_libs(os.path.join(trt_install_path, 'lib64'))) |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1046 | cuda_ver = convert_version_to_int(environ_cp['TF_CUDA_VERSION']) |
| 1047 | cudnn_ver = convert_version_to_int(environ_cp['TF_CUDNN_VERSION']) |
| 1048 | nvinfer_pattern = re.compile('.*libnvinfer.so.?(.*)$') |
| 1049 | highest_ver = [0, None, None] |
| 1050 | |
| 1051 | for lib_file in possible_files: |
Mingxing Tan | 1e7b0e4 | 2018-06-28 19:13:20 -0700 | [diff] [blame] | 1052 | if is_cuda_compatible(lib_file, cuda_ver, cudnn_ver): |
Jacques Pienaar | 2d0531d | 2018-03-21 12:07:51 -0700 | [diff] [blame] | 1053 | matches = nvinfer_pattern.search(lib_file) |
TensorFlower Gardener | 61a8720 | 2018-10-01 12:25:39 -0700 | [diff] [blame] | 1054 | if not matches.groups(): |
Jacques Pienaar | 2d0531d | 2018-03-21 12:07:51 -0700 | [diff] [blame] | 1055 | continue |
| 1056 | ver_str = matches.group(1) |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1057 | ver = convert_version_to_int(ver_str) if len(ver_str) else 0 |
| 1058 | if ver > highest_ver[0]: |
| 1059 | highest_ver = [ver, ver_str, lib_file] |
| 1060 | if highest_ver[1] is not None: |
| 1061 | trt_install_path = os.path.dirname(highest_ver[2]) |
| 1062 | tf_tensorrt_version = highest_ver[1] |
| 1063 | break |
| 1064 | |
| 1065 | # Try another alternative from ldconfig. |
| 1066 | ldconfig_bin = which('ldconfig') or '/sbin/ldconfig' |
| 1067 | ldconfig_output = run_shell([ldconfig_bin, '-p']) |
Mingxing Tan | 1e7b0e4 | 2018-06-28 19:13:20 -0700 | [diff] [blame] | 1068 | search_result = re.search('.*libnvinfer.so\\.?([0-9.]*).* => (.*)', |
| 1069 | ldconfig_output) |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1070 | if search_result: |
| 1071 | libnvinfer_path_from_ldconfig = search_result.group(2) |
| 1072 | if os.path.exists(libnvinfer_path_from_ldconfig): |
Mingxing Tan | 1e7b0e4 | 2018-06-28 19:13:20 -0700 | [diff] [blame] | 1073 | if is_cuda_compatible(libnvinfer_path_from_ldconfig, cuda_ver, |
| 1074 | cudnn_ver): |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1075 | trt_install_path = os.path.dirname(libnvinfer_path_from_ldconfig) |
| 1076 | tf_tensorrt_version = search_result.group(1) |
| 1077 | break |
| 1078 | |
| 1079 | # Reset and Retry |
Yifei Feng | dce9a49 | 2018-02-22 14:24:57 -0800 | [diff] [blame] | 1080 | if possible_files: |
| 1081 | print('TensorRT libraries found in one the following directories', |
| 1082 | 'are not compatible with selected cuda and cudnn installations') |
| 1083 | print(trt_install_path) |
| 1084 | print(os.path.join(trt_install_path, 'lib')) |
| 1085 | print(os.path.join(trt_install_path, 'lib64')) |
| 1086 | if search_result: |
| 1087 | print(libnvinfer_path_from_ldconfig) |
| 1088 | else: |
| 1089 | print( |
| 1090 | 'Invalid path to TensorRT. None of the following files can be found:') |
| 1091 | print(trt_install_path) |
| 1092 | print(os.path.join(trt_install_path, 'lib')) |
| 1093 | print(os.path.join(trt_install_path, 'lib64')) |
| 1094 | if search_result: |
| 1095 | print(libnvinfer_path_from_ldconfig) |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1096 | |
| 1097 | else: |
| 1098 | raise UserInputError('Invalid TF_TENSORRT setting was provided %d ' |
| 1099 | 'times in a row. Assuming to be a scripting mistake.' % |
| 1100 | _DEFAULT_PROMPT_ASK_ATTEMPTS) |
| 1101 | |
| 1102 | # Set TENSORRT_INSTALL_PATH and TF_TENSORRT_VERSION |
| 1103 | environ_cp['TENSORRT_INSTALL_PATH'] = trt_install_path |
| 1104 | write_action_env_to_bazelrc('TENSORRT_INSTALL_PATH', trt_install_path) |
| 1105 | environ_cp['TF_TENSORRT_VERSION'] = tf_tensorrt_version |
| 1106 | write_action_env_to_bazelrc('TF_TENSORRT_VERSION', tf_tensorrt_version) |
| 1107 | |
| 1108 | |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1109 | def set_tf_nccl_install_path(environ_cp): |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1110 | """Set NCCL_INSTALL_PATH, NCCL_HDR_PATH and TF_NCCL_VERSION. |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1111 | |
| 1112 | Args: |
| 1113 | environ_cp: copy of the os.environ. |
| 1114 | |
| 1115 | Raises: |
| 1116 | ValueError: if this method was called under non-Linux platform. |
| 1117 | UserInputError: if user has provided invalid input multiple times. |
| 1118 | """ |
| 1119 | if not is_linux(): |
| 1120 | raise ValueError('Currently NCCL is only supported on Linux platforms.') |
| 1121 | |
| 1122 | ask_nccl_version = ( |
A. Unique TensorFlower | 53faa31 | 2018-10-05 08:46:54 -0700 | [diff] [blame] | 1123 | 'Please specify the locally installed NCCL version you want to use. ' |
| 1124 | '[Default is to use https://github.com/nvidia/nccl]: ') |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1125 | |
| 1126 | for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS): |
| 1127 | tf_nccl_version = get_from_env_or_user_or_default( |
A. Unique TensorFlower | 53faa31 | 2018-10-05 08:46:54 -0700 | [diff] [blame] | 1128 | environ_cp, 'TF_NCCL_VERSION', ask_nccl_version, '') |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1129 | |
A. Unique TensorFlower | 53faa31 | 2018-10-05 08:46:54 -0700 | [diff] [blame] | 1130 | if not tf_nccl_version: |
| 1131 | break # No need to get install path, building the open source code. |
| 1132 | |
| 1133 | tf_nccl_version = reformat_version_sequence(str(tf_nccl_version), 1) |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1134 | |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1135 | # Look with ldconfig first if we can find the library in paths |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1136 | # like /usr/lib/x86_64-linux-gnu and the header file in the corresponding |
| 1137 | # include directory. This is where the NCCL .deb packages install them. |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1138 | |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1139 | # First check to see if NCCL is in the ldconfig. |
| 1140 | # If its found, use that location. |
| 1141 | if is_linux(): |
| 1142 | ldconfig_bin = which('ldconfig') or '/sbin/ldconfig' |
| 1143 | nccl2_path_from_ldconfig = run_shell([ldconfig_bin, '-p']) |
| 1144 | nccl2_path_from_ldconfig = re.search('.*libnccl.so .* => (.*)', |
| 1145 | nccl2_path_from_ldconfig) |
| 1146 | if nccl2_path_from_ldconfig: |
| 1147 | nccl2_path_from_ldconfig = nccl2_path_from_ldconfig.group(1) |
| 1148 | if os.path.exists('%s.%s' % (nccl2_path_from_ldconfig, tf_nccl_version)): |
| 1149 | nccl_install_path = os.path.dirname(nccl2_path_from_ldconfig) |
| 1150 | print('NCCL libraries found in ' + nccl2_path_from_ldconfig) |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1151 | |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1152 | # Check if this is the main system lib location |
| 1153 | if re.search('.*linux-gnu', nccl_install_path): |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 1154 | trunc_nccl_install_path = '/usr' |
| 1155 | print('This looks like a system path.') |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1156 | else: |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 1157 | trunc_nccl_install_path = nccl_install_path + '/..' |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1158 | |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1159 | # Look for header |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 1160 | nccl_hdr_path = trunc_nccl_install_path + '/include' |
| 1161 | print('Assuming NCCL header path is ' + nccl_hdr_path) |
| 1162 | if os.path.exists(nccl_hdr_path + '/nccl.h'): |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1163 | # Set NCCL_INSTALL_PATH |
| 1164 | environ_cp['NCCL_INSTALL_PATH'] = nccl_install_path |
| 1165 | write_action_env_to_bazelrc('NCCL_INSTALL_PATH', nccl_install_path) |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1166 | |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1167 | # Set NCCL_HDR_PATH |
| 1168 | environ_cp['NCCL_HDR_PATH'] = nccl_hdr_path |
| 1169 | write_action_env_to_bazelrc('NCCL_HDR_PATH', nccl_hdr_path) |
| 1170 | break |
| 1171 | else: |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 1172 | print( |
| 1173 | 'The header for NCCL2 cannot be found. Please install the libnccl-dev package.' |
| 1174 | ) |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1175 | else: |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 1176 | print('NCCL2 is listed by ldconfig but the library is not found. ' |
| 1177 | 'Your ldconfig is out of date. Please run sudo ldconfig.') |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1178 | else: |
| 1179 | # NCCL is not found in ldconfig. Ask the user for the location. |
| 1180 | default_nccl_path = environ_cp.get('CUDA_TOOLKIT_PATH') |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 1181 | ask_nccl_path = ( |
| 1182 | r'Please specify the location where NCCL %s library is ' |
| 1183 | 'installed. Refer to README.md for more details. [Default ' |
| 1184 | 'is %s]:') % (tf_nccl_version, default_nccl_path) |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1185 | nccl_install_path = get_from_env_or_user_or_default( |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 1186 | environ_cp, 'NCCL_INSTALL_PATH', ask_nccl_path, default_nccl_path) |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1187 | |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1188 | # Result returned from "read" will be used unexpanded. That make "~" |
| 1189 | # unusable. Going through one more level of expansion to handle that. |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 1190 | nccl_install_path = os.path.realpath( |
| 1191 | os.path.expanduser(nccl_install_path)) |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1192 | if is_windows() or is_cygwin(): |
| 1193 | nccl_install_path = cygpath(nccl_install_path) |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1194 | |
Guangda Lai | 62ebf62 | 2018-10-23 07:44:13 -0700 | [diff] [blame] | 1195 | nccl_lib_path = '' |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1196 | if is_windows(): |
| 1197 | nccl_lib_path = 'lib/x64/nccl.lib' |
| 1198 | elif is_linux(): |
| 1199 | nccl_lib_filename = 'libnccl.so.%s' % tf_nccl_version |
| 1200 | nccl_lpath = '%s/lib/%s' % (nccl_install_path, nccl_lib_filename) |
| 1201 | if not os.path.exists(nccl_lpath): |
| 1202 | for relative_path in NCCL_LIB_PATHS: |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 1203 | path = '%s/%s%s' % (nccl_install_path, relative_path, |
| 1204 | nccl_lib_filename) |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1205 | if os.path.exists(path): |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 1206 | print('NCCL found at ' + path) |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1207 | nccl_lib_path = path |
| 1208 | break |
| 1209 | else: |
| 1210 | nccl_lib_path = nccl_lpath |
| 1211 | elif is_macos(): |
| 1212 | nccl_lib_path = 'lib/libnccl.%s.dylib' % tf_nccl_version |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1213 | |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1214 | nccl_lib_path = os.path.join(nccl_install_path, nccl_lib_path) |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 1215 | nccl_hdr_path = os.path.join( |
| 1216 | os.path.dirname(nccl_lib_path), '../include/nccl.h') |
| 1217 | print('Assuming NCCL header path is ' + nccl_hdr_path) |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1218 | if os.path.exists(nccl_lib_path) and os.path.exists(nccl_hdr_path): |
| 1219 | # Set NCCL_INSTALL_PATH |
| 1220 | environ_cp['NCCL_INSTALL_PATH'] = os.path.dirname(nccl_lib_path) |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 1221 | write_action_env_to_bazelrc('NCCL_INSTALL_PATH', |
| 1222 | os.path.dirname(nccl_lib_path)) |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1223 | |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1224 | # Set NCCL_HDR_PATH |
| 1225 | environ_cp['NCCL_HDR_PATH'] = os.path.dirname(nccl_hdr_path) |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 1226 | write_action_env_to_bazelrc('NCCL_HDR_PATH', |
| 1227 | os.path.dirname(nccl_hdr_path)) |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1228 | break |
| 1229 | |
| 1230 | # Reset and Retry |
TensorFlower Gardener | ea5c529 | 2018-10-01 17:28:26 -0700 | [diff] [blame] | 1231 | print( |
| 1232 | 'Invalid path to NCCL %s toolkit, %s or %s not found. Please use the ' |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1233 | 'O/S agnostic package of NCCL 2' % (tf_nccl_version, nccl_lib_path, |
| 1234 | nccl_hdr_path)) |
| 1235 | |
Jason Furmanek | 7c23415 | 2018-09-26 04:44:12 +0000 | [diff] [blame] | 1236 | environ_cp['TF_NCCL_VERSION'] = '' |
A. Unique TensorFlower | 1fda764 | 2018-04-05 03:09:27 -0700 | [diff] [blame] | 1237 | else: |
| 1238 | raise UserInputError('Invalid TF_NCCL setting was provided %d ' |
| 1239 | 'times in a row. Assuming to be a scripting mistake.' % |
| 1240 | _DEFAULT_PROMPT_ASK_ATTEMPTS) |
| 1241 | |
| 1242 | # Set TF_NCCL_VERSION |
| 1243 | environ_cp['TF_NCCL_VERSION'] = tf_nccl_version |
| 1244 | write_action_env_to_bazelrc('TF_NCCL_VERSION', tf_nccl_version) |
| 1245 | |
TensorFlower Gardener | abb763b | 2019-02-06 16:23:03 -0800 | [diff] [blame] | 1246 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1247 | def get_native_cuda_compute_capabilities(environ_cp): |
| 1248 | """Get native cuda compute capabilities. |
| 1249 | |
| 1250 | Args: |
| 1251 | environ_cp: copy of the os.environ. |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 1252 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1253 | Returns: |
| 1254 | string of native cuda compute capabilities, separated by comma. |
| 1255 | """ |
| 1256 | device_query_bin = os.path.join( |
| 1257 | environ_cp.get('CUDA_TOOLKIT_PATH'), 'extras/demo_suite/deviceQuery') |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 1258 | if os.path.isfile(device_query_bin) and os.access(device_query_bin, os.X_OK): |
| 1259 | try: |
| 1260 | output = run_shell(device_query_bin).split('\n') |
| 1261 | pattern = re.compile('[0-9]*\\.[0-9]*') |
| 1262 | output = [pattern.search(x) for x in output if 'Capability' in x] |
| 1263 | output = ','.join(x.group() for x in output if x is not None) |
| 1264 | except subprocess.CalledProcessError: |
| 1265 | output = '' |
| 1266 | else: |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1267 | output = '' |
| 1268 | return output |
| 1269 | |
| 1270 | |
| 1271 | def set_tf_cuda_compute_capabilities(environ_cp): |
| 1272 | """Set TF_CUDA_COMPUTE_CAPABILITIES.""" |
| 1273 | while True: |
| 1274 | native_cuda_compute_capabilities = get_native_cuda_compute_capabilities( |
| 1275 | environ_cp) |
| 1276 | if not native_cuda_compute_capabilities: |
| 1277 | default_cuda_compute_capabilities = _DEFAULT_CUDA_COMPUTE_CAPABILITIES |
| 1278 | else: |
| 1279 | default_cuda_compute_capabilities = native_cuda_compute_capabilities |
| 1280 | |
| 1281 | ask_cuda_compute_capabilities = ( |
| 1282 | 'Please specify a list of comma-separated ' |
P Sudeepam | 5209356 | 2019-02-17 17:34:01 +0530 | [diff] [blame] | 1283 | 'CUDA compute capabilities you want to ' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1284 | 'build with.\nYou can find the compute ' |
| 1285 | 'capability of your device at: ' |
| 1286 | 'https://developer.nvidia.com/cuda-gpus.\nPlease' |
| 1287 | ' note that each additional compute ' |
| 1288 | 'capability significantly increases your ' |
P Sudeepam | 5209356 | 2019-02-17 17:34:01 +0530 | [diff] [blame] | 1289 | 'build time and binary size, and that ' |
| 1290 | 'TensorFlow only supports compute ' |
P Sudeepam | 765ceda | 2019-02-17 17:39:08 +0530 | [diff] [blame] | 1291 | 'capabilities >= 3.5 [Default is: %s]: ' % |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1292 | default_cuda_compute_capabilities) |
| 1293 | tf_cuda_compute_capabilities = get_from_env_or_user_or_default( |
| 1294 | environ_cp, 'TF_CUDA_COMPUTE_CAPABILITIES', |
| 1295 | ask_cuda_compute_capabilities, default_cuda_compute_capabilities) |
| 1296 | # Check whether all capabilities from the input is valid |
| 1297 | all_valid = True |
Maciej | d0f5bc1 | 2018-04-30 22:30:58 -0500 | [diff] [blame] | 1298 | # Remove all whitespace characters before splitting the string |
Michael Case | 5105350 | 2018-06-05 17:47:19 -0700 | [diff] [blame] | 1299 | # that users may insert by accident, as this will result in error |
Maciej | d0f5bc1 | 2018-04-30 22:30:58 -0500 | [diff] [blame] | 1300 | tf_cuda_compute_capabilities = ''.join(tf_cuda_compute_capabilities.split()) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1301 | for compute_capability in tf_cuda_compute_capabilities.split(','): |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 1302 | m = re.match('[0-9]+.[0-9]+', compute_capability) |
| 1303 | if not m: |
Austin Anderson | 32202dc | 2019-02-19 10:46:27 -0800 | [diff] [blame] | 1304 | print('Invalid compute capability: %s' % compute_capability) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1305 | all_valid = False |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 1306 | else: |
P Sudeepam | 5209356 | 2019-02-17 17:34:01 +0530 | [diff] [blame] | 1307 | ver = float(m.group(0)) |
| 1308 | if ver < 3.5: |
| 1309 | print('ERROR: TensorFlow only supports CUDA compute capabilities 3.5 ' |
Austin Anderson | 32202dc | 2019-02-19 10:46:27 -0800 | [diff] [blame] | 1310 | 'and higher. Please re-specify the list of compute ' |
| 1311 | 'capabilities excluding version %s.' % ver) |
Andrew Harp | 6e3e7d1 | 2017-08-21 12:10:44 -0700 | [diff] [blame] | 1312 | all_valid = False |
P Sudeepam | 765ceda | 2019-02-17 17:39:08 +0530 | [diff] [blame] | 1313 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1314 | if all_valid: |
| 1315 | break |
| 1316 | |
| 1317 | # Reset and Retry |
| 1318 | environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = '' |
| 1319 | |
| 1320 | # Set TF_CUDA_COMPUTE_CAPABILITIES |
| 1321 | environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = tf_cuda_compute_capabilities |
| 1322 | write_action_env_to_bazelrc('TF_CUDA_COMPUTE_CAPABILITIES', |
| 1323 | tf_cuda_compute_capabilities) |
| 1324 | |
| 1325 | |
| 1326 | def set_other_cuda_vars(environ_cp): |
| 1327 | """Set other CUDA related variables.""" |
A. Unique TensorFlower | ab39198 | 2018-07-11 04:52:49 -0700 | [diff] [blame] | 1328 | # If CUDA is enabled, always use GPU during build and test. |
| 1329 | if environ_cp.get('TF_CUDA_CLANG') == '1': |
| 1330 | write_to_bazelrc('build --config=cuda_clang') |
| 1331 | write_to_bazelrc('test --config=cuda_clang') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1332 | else: |
A. Unique TensorFlower | ab39198 | 2018-07-11 04:52:49 -0700 | [diff] [blame] | 1333 | write_to_bazelrc('build --config=cuda') |
| 1334 | write_to_bazelrc('test --config=cuda') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1335 | |
| 1336 | |
| 1337 | def set_host_cxx_compiler(environ_cp): |
| 1338 | """Set HOST_CXX_COMPILER.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 1339 | default_cxx_host_compiler = which('g++') or '' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1340 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1341 | host_cxx_compiler = prompt_loop_or_load_from_env( |
| 1342 | environ_cp, |
| 1343 | var_name='HOST_CXX_COMPILER', |
| 1344 | var_default=default_cxx_host_compiler, |
| 1345 | ask_for_var=('Please specify which C++ compiler should be used as the ' |
| 1346 | 'host C++ compiler.'), |
| 1347 | check_success=os.path.exists, |
| 1348 | error_msg='Invalid C++ compiler path. %s cannot be found.', |
| 1349 | ) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1350 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1351 | write_action_env_to_bazelrc('HOST_CXX_COMPILER', host_cxx_compiler) |
| 1352 | |
| 1353 | |
| 1354 | def set_host_c_compiler(environ_cp): |
| 1355 | """Set HOST_C_COMPILER.""" |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 1356 | default_c_host_compiler = which('gcc') or '' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1357 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1358 | host_c_compiler = prompt_loop_or_load_from_env( |
| 1359 | environ_cp, |
| 1360 | var_name='HOST_C_COMPILER', |
| 1361 | var_default=default_c_host_compiler, |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 1362 | ask_for_var=('Please specify which C compiler should be used as the host ' |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1363 | 'C compiler.'), |
| 1364 | check_success=os.path.exists, |
| 1365 | error_msg='Invalid C compiler path. %s cannot be found.', |
| 1366 | ) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1367 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1368 | write_action_env_to_bazelrc('HOST_C_COMPILER', host_c_compiler) |
| 1369 | |
| 1370 | |
| 1371 | def set_computecpp_toolkit_path(environ_cp): |
| 1372 | """Set COMPUTECPP_TOOLKIT_PATH.""" |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1373 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1374 | def toolkit_exists(toolkit_path): |
| 1375 | """Check if a computecpp toolkit path is valid.""" |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1376 | if is_linux(): |
| 1377 | sycl_rt_lib_path = 'lib/libComputeCpp.so' |
| 1378 | else: |
| 1379 | sycl_rt_lib_path = '' |
| 1380 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 1381 | sycl_rt_lib_path_full = os.path.join(toolkit_path, sycl_rt_lib_path) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1382 | exists = os.path.exists(sycl_rt_lib_path_full) |
| 1383 | if not exists: |
| 1384 | print('Invalid SYCL %s library path. %s cannot be found' % |
| 1385 | (_TF_OPENCL_VERSION, sycl_rt_lib_path_full)) |
| 1386 | return exists |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1387 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1388 | computecpp_toolkit_path = prompt_loop_or_load_from_env( |
| 1389 | environ_cp, |
| 1390 | var_name='COMPUTECPP_TOOLKIT_PATH', |
| 1391 | var_default=_DEFAULT_COMPUTECPP_TOOLKIT_PATH, |
| 1392 | ask_for_var=( |
| 1393 | 'Please specify the location where ComputeCpp for SYCL %s is ' |
| 1394 | 'installed.' % _TF_OPENCL_VERSION), |
| 1395 | check_success=toolkit_exists, |
| 1396 | error_msg='Invalid SYCL compiler path. %s cannot be found.', |
| 1397 | suppress_default_error=True) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1398 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1399 | write_action_env_to_bazelrc('COMPUTECPP_TOOLKIT_PATH', |
| 1400 | computecpp_toolkit_path) |
| 1401 | |
Michael Case | d31531a | 2018-01-05 14:09:41 -0800 | [diff] [blame] | 1402 | |
Dandelion Man? | 90e42f3 | 2017-12-15 18:15:07 -0800 | [diff] [blame] | 1403 | def set_trisycl_include_dir(environ_cp): |
Michael Case | d31531a | 2018-01-05 14:09:41 -0800 | [diff] [blame] | 1404 | """Set TRISYCL_INCLUDE_DIR.""" |
Frank Chen | c4ef927 | 2018-01-10 11:36:52 -0800 | [diff] [blame] | 1405 | |
Dandelion Man? | 90e42f3 | 2017-12-15 18:15:07 -0800 | [diff] [blame] | 1406 | ask_trisycl_include_dir = ('Please specify the location of the triSYCL ' |
| 1407 | 'include directory. (Use --config=sycl_trisycl ' |
| 1408 | 'when building with Bazel) ' |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 1409 | '[Default is %s]: ') % ( |
| 1410 | _DEFAULT_TRISYCL_INCLUDE_DIR) |
Frank Chen | c4ef927 | 2018-01-10 11:36:52 -0800 | [diff] [blame] | 1411 | |
Dandelion Man? | 90e42f3 | 2017-12-15 18:15:07 -0800 | [diff] [blame] | 1412 | while True: |
| 1413 | trisycl_include_dir = get_from_env_or_user_or_default( |
Michael Case | d31531a | 2018-01-05 14:09:41 -0800 | [diff] [blame] | 1414 | environ_cp, 'TRISYCL_INCLUDE_DIR', ask_trisycl_include_dir, |
| 1415 | _DEFAULT_TRISYCL_INCLUDE_DIR) |
Dandelion Man? | 90e42f3 | 2017-12-15 18:15:07 -0800 | [diff] [blame] | 1416 | if os.path.exists(trisycl_include_dir): |
| 1417 | break |
| 1418 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 1419 | print('Invalid triSYCL include directory, %s cannot be found' % |
| 1420 | (trisycl_include_dir)) |
Dandelion Man? | 90e42f3 | 2017-12-15 18:15:07 -0800 | [diff] [blame] | 1421 | |
| 1422 | # Set TRISYCL_INCLUDE_DIR |
| 1423 | environ_cp['TRISYCL_INCLUDE_DIR'] = trisycl_include_dir |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 1424 | write_action_env_to_bazelrc('TRISYCL_INCLUDE_DIR', trisycl_include_dir) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1425 | |
Yifei Feng | b1d8c59 | 2017-11-22 13:42:21 -0800 | [diff] [blame] | 1426 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1427 | def set_mpi_home(environ_cp): |
| 1428 | """Set MPI_HOME.""" |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1429 | |
Jonathan Hseu | 008910f | 2017-08-25 14:01:05 -0700 | [diff] [blame] | 1430 | default_mpi_home = which('mpirun') or which('mpiexec') or '' |
| 1431 | default_mpi_home = os.path.dirname(os.path.dirname(default_mpi_home)) |
| 1432 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1433 | def valid_mpi_path(mpi_home): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 1434 | exists = ( |
| 1435 | os.path.exists(os.path.join(mpi_home, 'include')) and |
TensorFlower Gardener | f7c861f | 2018-11-01 11:29:40 -0700 | [diff] [blame] | 1436 | (os.path.exists(os.path.join(mpi_home, 'lib')) or |
| 1437 | os.path.exists(os.path.join(mpi_home, 'lib64')) or |
| 1438 | os.path.exists(os.path.join(mpi_home, 'lib32')))) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1439 | if not exists: |
TensorFlower Gardener | f7c861f | 2018-11-01 11:29:40 -0700 | [diff] [blame] | 1440 | print( |
| 1441 | 'Invalid path to the MPI Toolkit. %s or %s or %s or %s cannot be found' |
| 1442 | % (os.path.join(mpi_home, 'include'), |
Christian Goll | ba95d09 | 2018-10-04 17:06:23 +0200 | [diff] [blame] | 1443 | os.path.exists(os.path.join(mpi_home, 'lib')), |
| 1444 | os.path.exists(os.path.join(mpi_home, 'lib64')), |
| 1445 | os.path.exists(os.path.join(mpi_home, 'lib32')))) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1446 | return exists |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1447 | |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1448 | _ = prompt_loop_or_load_from_env( |
| 1449 | environ_cp, |
| 1450 | var_name='MPI_HOME', |
| 1451 | var_default=default_mpi_home, |
| 1452 | ask_for_var='Please specify the MPI toolkit folder.', |
| 1453 | check_success=valid_mpi_path, |
| 1454 | error_msg='', |
| 1455 | suppress_default_error=True) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1456 | |
| 1457 | |
| 1458 | def set_other_mpi_vars(environ_cp): |
| 1459 | """Set other MPI related variables.""" |
| 1460 | # Link the MPI header files |
| 1461 | mpi_home = environ_cp.get('MPI_HOME') |
| 1462 | symlink_force('%s/include/mpi.h' % mpi_home, 'third_party/mpi/mpi.h') |
| 1463 | |
| 1464 | # Determine if we use OpenMPI or MVAPICH, these require different header files |
| 1465 | # to be included here to make bazel dependency checker happy |
| 1466 | if os.path.exists(os.path.join(mpi_home, 'include/mpi_portable_platform.h')): |
| 1467 | symlink_force( |
| 1468 | os.path.join(mpi_home, 'include/mpi_portable_platform.h'), |
| 1469 | 'third_party/mpi/mpi_portable_platform.h') |
| 1470 | # TODO(gunan): avoid editing files in configure |
| 1471 | sed_in_place('third_party/mpi/mpi.bzl', 'MPI_LIB_IS_OPENMPI=False', |
| 1472 | 'MPI_LIB_IS_OPENMPI=True') |
| 1473 | else: |
| 1474 | # MVAPICH / MPICH |
| 1475 | symlink_force( |
| 1476 | os.path.join(mpi_home, 'include/mpio.h'), 'third_party/mpi/mpio.h') |
| 1477 | symlink_force( |
| 1478 | os.path.join(mpi_home, 'include/mpicxx.h'), 'third_party/mpi/mpicxx.h') |
| 1479 | # TODO(gunan): avoid editing files in configure |
| 1480 | sed_in_place('third_party/mpi/mpi.bzl', 'MPI_LIB_IS_OPENMPI=True', |
| 1481 | 'MPI_LIB_IS_OPENMPI=False') |
| 1482 | |
| 1483 | if os.path.exists(os.path.join(mpi_home, 'lib/libmpi.so')): |
| 1484 | symlink_force( |
| 1485 | os.path.join(mpi_home, 'lib/libmpi.so'), 'third_party/mpi/libmpi.so') |
Christian Goll | ba95d09 | 2018-10-04 17:06:23 +0200 | [diff] [blame] | 1486 | elif os.path.exists(os.path.join(mpi_home, 'lib64/libmpi.so')): |
| 1487 | symlink_force( |
| 1488 | os.path.join(mpi_home, 'lib64/libmpi.so'), 'third_party/mpi/libmpi.so') |
| 1489 | elif os.path.exists(os.path.join(mpi_home, 'lib32/libmpi.so')): |
| 1490 | symlink_force( |
| 1491 | os.path.join(mpi_home, 'lib32/libmpi.so'), 'third_party/mpi/libmpi.so') |
| 1492 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1493 | else: |
TensorFlower Gardener | f7c861f | 2018-11-01 11:29:40 -0700 | [diff] [blame] | 1494 | raise ValueError( |
| 1495 | 'Cannot find the MPI library file in %s/lib or %s/lib64 or %s/lib32' % |
Mihai Maruseac | 91ebeec | 2019-01-29 17:07:38 -0800 | [diff] [blame] | 1496 | (mpi_home, mpi_home, mpi_home)) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1497 | |
Gunhan Gulsoy | fff6696 | 2019-02-20 15:28:43 -0800 | [diff] [blame^] | 1498 | def system_specific_test_config(env): |
| 1499 | """Add default test flags required for TF tests to bazelrc.""" |
| 1500 | write_to_bazelrc('test --flaky_test_attempts=3') |
| 1501 | write_to_bazelrc('test --test_size_filters=small,medium') |
| 1502 | write_to_bazelrc( |
| 1503 | 'test --test_tag_filters=-benchmark-test,-no_oss,-oss_serial') |
| 1504 | write_to_bazelrc('test --build_tag_filters=-benchmark-test,-no_oss') |
| 1505 | if is_windows(): |
| 1506 | if env.get('TF_NEED_CUDA', None) == 1: |
| 1507 | write_to_bazelrc( |
| 1508 | 'test --test_tag_filters=-no_windows,-no_windows_gpu,-no_gpu') |
| 1509 | write_to_bazelrc( |
| 1510 | 'test --build_tag_filters=-no_windows,-no_windows_gpu,-no_gpu') |
| 1511 | else: |
| 1512 | write_to_bazelrc('test --test_tag_filters=-no_windows,-gpu') |
| 1513 | write_to_bazelrc('test --build_tag_filters=-no_windows,-gpu') |
| 1514 | elif is_macos(): |
| 1515 | write_to_bazelrc('test --test_tag_filters=-gpu,-nomac,-no_mac') |
| 1516 | write_to_bazelrc('test --build_tag_filters=-gpu,-nomac,-no_mac') |
| 1517 | elif is_linux(): |
| 1518 | if env.get('TF_NEED_CUDA', None) == 1: |
| 1519 | write_to_bazelrc('test --test_tag_filters=-no_gpu') |
| 1520 | write_to_bazelrc('test --build_tag_filters=-no_gpu') |
| 1521 | write_to_bazelrc('test --test_env=LD_LIBRARY_PATH') |
| 1522 | else: |
| 1523 | write_to_bazelrc('test --test_tag_filters=-gpu') |
| 1524 | write_to_bazelrc('test --build_tag_filters=-gpu') |
| 1525 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1526 | |
Yifei Feng | 5198cb8 | 2018-08-17 13:53:06 -0700 | [diff] [blame] | 1527 | def set_system_libs_flag(environ_cp): |
| 1528 | syslibs = environ_cp.get('TF_SYSTEM_LIBS', '') |
TensorFlower Gardener | 61a8720 | 2018-10-01 12:25:39 -0700 | [diff] [blame] | 1529 | if syslibs: |
Jason Zaman | 5fc39bd | 2018-09-16 01:38:55 +0800 | [diff] [blame] | 1530 | if ',' in syslibs: |
| 1531 | syslibs = ','.join(sorted(syslibs.split(','))) |
| 1532 | else: |
| 1533 | syslibs = ','.join(sorted(syslibs.split())) |
Yifei Feng | 5198cb8 | 2018-08-17 13:53:06 -0700 | [diff] [blame] | 1534 | write_action_env_to_bazelrc('TF_SYSTEM_LIBS', syslibs) |
| 1535 | |
Jason Zaman | 5fc39bd | 2018-09-16 01:38:55 +0800 | [diff] [blame] | 1536 | if 'PREFIX' in environ_cp: |
| 1537 | write_to_bazelrc('build --define=PREFIX=%s' % environ_cp['PREFIX']) |
| 1538 | if 'LIBDIR' in environ_cp: |
| 1539 | write_to_bazelrc('build --define=LIBDIR=%s' % environ_cp['LIBDIR']) |
| 1540 | if 'INCLUDEDIR' in environ_cp: |
| 1541 | write_to_bazelrc('build --define=INCLUDEDIR=%s' % environ_cp['INCLUDEDIR']) |
| 1542 | |
Yifei Feng | 5198cb8 | 2018-08-17 13:53:06 -0700 | [diff] [blame] | 1543 | |
A. Unique TensorFlower | 1b21235 | 2018-07-19 13:48:50 -0700 | [diff] [blame] | 1544 | def set_windows_build_flags(environ_cp): |
| 1545 | """Set Windows specific build options.""" |
| 1546 | # The non-monolithic build is not supported yet |
| 1547 | write_to_bazelrc('build --config monolithic') |
| 1548 | # Suppress warning messages |
| 1549 | write_to_bazelrc('build --copt=-w --host_copt=-w') |
| 1550 | # Output more verbose information when something goes wrong |
| 1551 | write_to_bazelrc('build --verbose_failures') |
| 1552 | # The host and target platforms are the same in Windows build. So we don't |
| 1553 | # have to distinct them. This avoids building the same targets twice. |
| 1554 | write_to_bazelrc('build --distinct_host_configuration=false') |
A. Unique TensorFlower | 1b21235 | 2018-07-19 13:48:50 -0700 | [diff] [blame] | 1555 | |
| 1556 | if get_var( |
| 1557 | environ_cp, 'TF_OVERRIDE_EIGEN_STRONG_INLINE', 'Eigen strong inline', |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 1558 | True, ('Would you like to override eigen strong inline for some C++ ' |
| 1559 | 'compilation to reduce the compilation time?'), |
| 1560 | 'Eigen strong inline overridden.', 'Not overriding eigen strong inline, ' |
A. Unique TensorFlower | 1b21235 | 2018-07-19 13:48:50 -0700 | [diff] [blame] | 1561 | 'some compilations could take more than 20 mins.'): |
| 1562 | # Due to a known MSVC compiler issue |
| 1563 | # https://github.com/tensorflow/tensorflow/issues/10521 |
| 1564 | # Overriding eigen strong inline speeds up the compiling of |
| 1565 | # conv_grad_ops_3d.cc and conv_ops_3d.cc by 20 minutes, |
| 1566 | # but this also hurts the performance. Let users decide what they want. |
| 1567 | write_to_bazelrc('build --define=override_eigen_strong_inline=true') |
Dandelion Man? | 90e42f3 | 2017-12-15 18:15:07 -0800 | [diff] [blame] | 1568 | |
A. Unique TensorFlower | 061c359 | 2017-11-13 14:21:04 -0800 | [diff] [blame] | 1569 | |
Michael Case | d31531a | 2018-01-05 14:09:41 -0800 | [diff] [blame] | 1570 | def config_info_line(name, help_text): |
| 1571 | """Helper function to print formatted help text for Bazel config options.""" |
| 1572 | print('\t--config=%-12s\t# %s' % (name, help_text)) |
| 1573 | |
| 1574 | |
A. Unique TensorFlower | 93e7073 | 2019-02-14 16:45:32 -0800 | [diff] [blame] | 1575 | def configure_apple_bazel_rules(): |
| 1576 | """Configures Bazel rules for building on Apple platforms. |
| 1577 | |
| 1578 | Enables analyzing and building Apple Bazel rules on Apple platforms. This |
| 1579 | function will only be executed if `is_macos()` is true. |
| 1580 | """ |
| 1581 | if not is_macos(): |
| 1582 | return |
| 1583 | for filepath in APPLE_BAZEL_FILES: |
| 1584 | print( |
| 1585 | 'Configuring %s file to analyze and build Bazel rules on Apple platforms.' |
| 1586 | % filepath) |
| 1587 | existing_filepath = os.path.join(_TF_WORKSPACE_ROOT, filepath + '.apple') |
| 1588 | renamed_filepath = os.path.join(_TF_WORKSPACE_ROOT, filepath) |
| 1589 | os.rename(existing_filepath, renamed_filepath) |
| 1590 | |
| 1591 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1592 | def main(): |
A. Unique TensorFlower | 7c5eb35 | 2018-10-01 07:15:23 -0700 | [diff] [blame] | 1593 | global _TF_WORKSPACE_ROOT |
| 1594 | global _TF_BAZELRC |
| 1595 | |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 1596 | parser = argparse.ArgumentParser() |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 1597 | parser.add_argument( |
| 1598 | '--workspace', |
| 1599 | type=str, |
A. Unique TensorFlower | 7c5eb35 | 2018-10-01 07:15:23 -0700 | [diff] [blame] | 1600 | default=os.path.abspath(os.path.dirname(__file__)), |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 1601 | help='The absolute path to your active Bazel workspace.') |
Shanqing Cai | 7144571 | 2018-03-12 19:33:52 -0700 | [diff] [blame] | 1602 | args = parser.parse_args() |
| 1603 | |
A. Unique TensorFlower | 7c5eb35 | 2018-10-01 07:15:23 -0700 | [diff] [blame] | 1604 | _TF_WORKSPACE_ROOT = args.workspace |
| 1605 | _TF_BAZELRC = os.path.join(_TF_WORKSPACE_ROOT, _TF_BAZELRC_FILENAME) |
| 1606 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1607 | # Make a copy of os.environ to be clear when functions and getting and setting |
| 1608 | # environment variables. |
| 1609 | environ_cp = dict(os.environ) |
| 1610 | |
Gunhan Gulsoy | 91da898 | 2019-02-04 13:31:05 -0800 | [diff] [blame] | 1611 | check_bazel_version('0.19.0', '0.22.0') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1612 | |
A. Unique TensorFlower | 7c5eb35 | 2018-10-01 07:15:23 -0700 | [diff] [blame] | 1613 | reset_tf_configure_bazelrc() |
Yun Peng | 03e63a2 | 2018-11-07 11:18:53 +0100 | [diff] [blame] | 1614 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1615 | cleanup_makefile() |
Gunhan Gulsoy | ed89a2b | 2017-09-19 18:36:26 -0700 | [diff] [blame] | 1616 | setup_python(environ_cp) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1617 | |
| 1618 | if is_windows(): |
Yifei Feng | b1d8c59 | 2017-11-22 13:42:21 -0800 | [diff] [blame] | 1619 | environ_cp['TF_NEED_OPENCL_SYCL'] = '0' |
| 1620 | environ_cp['TF_NEED_COMPUTECPP'] = '0' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1621 | environ_cp['TF_NEED_OPENCL'] = '0' |
| 1622 | environ_cp['TF_CUDA_CLANG'] = '0' |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1623 | environ_cp['TF_NEED_TENSORRT'] = '0' |
Ilya Biryukov | 9e651e4 | 2018-03-22 05:33:42 -0700 | [diff] [blame] | 1624 | # TODO(ibiryukov): Investigate using clang as a cpu or cuda compiler on |
| 1625 | # Windows. |
| 1626 | environ_cp['TF_DOWNLOAD_CLANG'] = '0' |
A. Unique TensorFlower | 6e97fb3 | 2018-07-16 14:07:29 -0700 | [diff] [blame] | 1627 | environ_cp['TF_NEED_MPI'] = '0' |
| 1628 | environ_cp['TF_SET_ANDROID_WORKSPACE'] = '0' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1629 | |
| 1630 | if is_macos(): |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1631 | environ_cp['TF_NEED_TENSORRT'] = '0' |
A. Unique TensorFlower | 93e7073 | 2019-02-14 16:45:32 -0800 | [diff] [blame] | 1632 | else: |
| 1633 | environ_cp['TF_CONFIGURE_APPLE_BAZEL_RULES'] = '0' |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1634 | |
Jon Triebenbach | 6896a74 | 2018-06-27 13:29:53 -0500 | [diff] [blame] | 1635 | # The numpy package on ppc64le uses OpenBLAS which has multi-threading |
| 1636 | # issues that lead to incorrect answers. Set OMP_NUM_THREADS=1 at |
| 1637 | # runtime to allow the Tensorflow testcases which compare numpy |
| 1638 | # results to Tensorflow results to succeed. |
| 1639 | if is_ppc64le(): |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 1640 | write_action_env_to_bazelrc('OMP_NUM_THREADS', 1) |
Jon Triebenbach | 6896a74 | 2018-06-27 13:29:53 -0500 | [diff] [blame] | 1641 | |
Grzegorz Pawelczak | ec82efd | 2018-10-09 15:03:46 +0100 | [diff] [blame] | 1642 | xla_enabled_by_default = is_linux() |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1643 | set_build_var(environ_cp, 'TF_ENABLE_XLA', 'XLA JIT', 'with_xla_support', |
Grzegorz Pawelczak | ec82efd | 2018-10-09 15:03:46 +0100 | [diff] [blame] | 1644 | xla_enabled_by_default, 'xla') |
Gunhan Gulsoy | 3da0dff | 2018-09-26 11:55:50 -0700 | [diff] [blame] | 1645 | |
Yifei Feng | b1d8c59 | 2017-11-22 13:42:21 -0800 | [diff] [blame] | 1646 | set_action_env_var(environ_cp, 'TF_NEED_OPENCL_SYCL', 'OpenCL SYCL', False) |
| 1647 | if environ_cp.get('TF_NEED_OPENCL_SYCL') == '1': |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1648 | set_host_cxx_compiler(environ_cp) |
| 1649 | set_host_c_compiler(environ_cp) |
Yifei Feng | b1d8c59 | 2017-11-22 13:42:21 -0800 | [diff] [blame] | 1650 | set_action_env_var(environ_cp, 'TF_NEED_COMPUTECPP', 'ComputeCPP', True) |
| 1651 | if environ_cp.get('TF_NEED_COMPUTECPP') == '1': |
| 1652 | set_computecpp_toolkit_path(environ_cp) |
| 1653 | else: |
| 1654 | set_trisycl_include_dir(environ_cp) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1655 | |
Wen-Heng (Jack) Chung | 69d3b8f | 2018-06-22 23:09:43 -0500 | [diff] [blame] | 1656 | set_action_env_var(environ_cp, 'TF_NEED_ROCM', 'ROCm', False) |
| 1657 | if (environ_cp.get('TF_NEED_ROCM') == '1' and |
TensorFlower Gardener | 62e6016 | 2018-09-27 10:22:55 -0700 | [diff] [blame] | 1658 | 'LD_LIBRARY_PATH' in environ_cp and |
| 1659 | environ_cp.get('LD_LIBRARY_PATH') != '1'): |
| 1660 | write_action_env_to_bazelrc('LD_LIBRARY_PATH', |
| 1661 | environ_cp.get('LD_LIBRARY_PATH')) |
Wen-Heng (Jack) Chung | 69d3b8f | 2018-06-22 23:09:43 -0500 | [diff] [blame] | 1662 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1663 | set_action_env_var(environ_cp, 'TF_NEED_CUDA', 'CUDA', False) |
A. Unique TensorFlower | 24cbb2a | 2017-09-08 07:45:44 -0700 | [diff] [blame] | 1664 | if (environ_cp.get('TF_NEED_CUDA') == '1' and |
| 1665 | 'TF_CUDA_CONFIG_REPO' not in environ_cp): |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1666 | set_tf_cuda_version(environ_cp) |
Yifei Feng | b1d8c59 | 2017-11-22 13:42:21 -0800 | [diff] [blame] | 1667 | set_tf_cudnn_version(environ_cp) |
Guangda Lai | 76f6938 | 2018-01-25 23:59:19 -0800 | [diff] [blame] | 1668 | if is_linux(): |
| 1669 | set_tf_tensorrt_install_path(environ_cp) |
Michael Case | 0073d13 | 2018-04-11 09:34:44 -0700 | [diff] [blame] | 1670 | set_tf_nccl_install_path(environ_cp) |
| 1671 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1672 | set_tf_cuda_compute_capabilities(environ_cp) |
Ankur Taly | 0e6f39d | 2018-02-16 18:22:55 -0800 | [diff] [blame] | 1673 | if 'LD_LIBRARY_PATH' in environ_cp and environ_cp.get( |
| 1674 | 'LD_LIBRARY_PATH') != '1': |
| 1675 | write_action_env_to_bazelrc('LD_LIBRARY_PATH', |
| 1676 | environ_cp.get('LD_LIBRARY_PATH')) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1677 | |
| 1678 | set_tf_cuda_clang(environ_cp) |
| 1679 | if environ_cp.get('TF_CUDA_CLANG') == '1': |
Ilya Biryukov | 9e651e4 | 2018-03-22 05:33:42 -0700 | [diff] [blame] | 1680 | # Ask whether we should download the clang toolchain. |
| 1681 | set_tf_download_clang(environ_cp) |
A. Unique TensorFlower | fd29e95 | 2017-12-22 03:07:51 -0800 | [diff] [blame] | 1682 | if environ_cp.get('TF_DOWNLOAD_CLANG') != '1': |
| 1683 | # Set up which clang we should use as the cuda / host compiler. |
| 1684 | set_clang_cuda_compiler_path(environ_cp) |
Ilya Biryukov | 1c3d02e | 2018-09-04 03:09:52 -0700 | [diff] [blame] | 1685 | else: |
| 1686 | # Use downloaded LLD for linking. |
| 1687 | write_to_bazelrc('build:cuda_clang --config=download_clang_use_lld') |
| 1688 | write_to_bazelrc('test:cuda_clang --config=download_clang_use_lld') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1689 | else: |
| 1690 | # Set up which gcc nvcc should use as the host compiler |
| 1691 | # No need to set this on Windows |
| 1692 | if not is_windows(): |
| 1693 | set_gcc_host_compiler_path(environ_cp) |
| 1694 | set_other_cuda_vars(environ_cp) |
Ilya Biryukov | 9e651e4 | 2018-03-22 05:33:42 -0700 | [diff] [blame] | 1695 | else: |
| 1696 | # CUDA not required. Ask whether we should download the clang toolchain and |
| 1697 | # use it for the CPU build. |
| 1698 | set_tf_download_clang(environ_cp) |
| 1699 | if environ_cp.get('TF_DOWNLOAD_CLANG') == '1': |
| 1700 | write_to_bazelrc('build --config=download_clang') |
| 1701 | write_to_bazelrc('test --config=download_clang') |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1702 | |
Wen-Heng (Jack) Chung | 69d3b8f | 2018-06-22 23:09:43 -0500 | [diff] [blame] | 1703 | # SYCL / ROCm / CUDA are mutually exclusive. |
| 1704 | # At most 1 GPU platform can be configured. |
| 1705 | gpu_platform_count = 0 |
| 1706 | if environ_cp.get('TF_NEED_OPENCL_SYCL') == '1': |
| 1707 | gpu_platform_count += 1 |
| 1708 | if environ_cp.get('TF_NEED_ROCM') == '1': |
| 1709 | gpu_platform_count += 1 |
| 1710 | if environ_cp.get('TF_NEED_CUDA') == '1': |
| 1711 | gpu_platform_count += 1 |
| 1712 | if gpu_platform_count >= 2: |
| 1713 | raise UserInputError('SYCL / CUDA / ROCm are mututally exclusive. ' |
| 1714 | 'At most 1 GPU platform can be configured.') |
| 1715 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1716 | set_build_var(environ_cp, 'TF_NEED_MPI', 'MPI', 'with_mpi_support', False) |
| 1717 | if environ_cp.get('TF_NEED_MPI') == '1': |
| 1718 | set_mpi_home(environ_cp) |
| 1719 | set_other_mpi_vars(environ_cp) |
| 1720 | |
| 1721 | set_cc_opt_flags(environ_cp) |
Yifei Feng | 5198cb8 | 2018-08-17 13:53:06 -0700 | [diff] [blame] | 1722 | set_system_libs_flag(environ_cp) |
A. Unique TensorFlower | 1b21235 | 2018-07-19 13:48:50 -0700 | [diff] [blame] | 1723 | if is_windows(): |
| 1724 | set_windows_build_flags(environ_cp) |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1725 | |
Anna R | a9a1d5a | 2018-09-14 12:44:31 -0700 | [diff] [blame] | 1726 | # Add a config option to build TensorFlow 2.0 API. |
| 1727 | write_to_bazelrc('build:v2 --define=tf_api_version=2') |
| 1728 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 1729 | if get_var(environ_cp, 'TF_SET_ANDROID_WORKSPACE', 'android workspace', False, |
| 1730 | ('Would you like to interactively configure ./WORKSPACE for ' |
| 1731 | 'Android builds?'), 'Searching for NDK and SDK installations.', |
| 1732 | 'Not configuring the WORKSPACE for Android builds.'): |
Michael Case | 5105350 | 2018-06-05 17:47:19 -0700 | [diff] [blame] | 1733 | create_android_ndk_rule(environ_cp) |
| 1734 | create_android_sdk_rule(environ_cp) |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1735 | |
Gunhan Gulsoy | fff6696 | 2019-02-20 15:28:43 -0800 | [diff] [blame^] | 1736 | system_specific_test_config(os.environ) |
| 1737 | |
A. Unique TensorFlower | 93e7073 | 2019-02-14 16:45:32 -0800 | [diff] [blame] | 1738 | if get_var( |
| 1739 | environ_cp, 'TF_CONFIGURE_APPLE_BAZEL_RULES', |
| 1740 | 'Configure Bazel rules for Apple platforms', False, |
| 1741 | ('Would you like to configure Bazel rules for building on Apple platforms?' |
| 1742 | ), 'Configuring Bazel rules for Apple platforms.', |
| 1743 | 'Not configuring Bazel rules for Apple platforms.'): |
| 1744 | configure_apple_bazel_rules() |
| 1745 | |
Gunhan Gulsoy | 3437098 | 2018-10-12 15:26:01 -0700 | [diff] [blame] | 1746 | print('Preconfigured Bazel build configs. You can use any of the below by ' |
| 1747 | 'adding "--config=<>" to your build command. See .bazelrc for more ' |
| 1748 | 'details.') |
| 1749 | config_info_line('mkl', 'Build with MKL support.') |
| 1750 | config_info_line('monolithic', 'Config for mostly static monolithic build.') |
| 1751 | config_info_line('gdr', 'Build with GDR support.') |
| 1752 | config_info_line('verbs', 'Build with libverbs support.') |
| 1753 | config_info_line('ngraph', 'Build with Intel nGraph support.') |
TensorFlower Gardener | abb763b | 2019-02-06 16:23:03 -0800 | [diff] [blame] | 1754 | config_info_line( |
| 1755 | 'dynamic_kernels', |
| 1756 | '(Experimental) Build kernels into separate shared objects.') |
Gunhan Gulsoy | 3437098 | 2018-10-12 15:26:01 -0700 | [diff] [blame] | 1757 | |
| 1758 | print('Preconfigured Bazel build configs to DISABLE default on features:') |
| 1759 | config_info_line('noaws', 'Disable AWS S3 filesystem support.') |
| 1760 | config_info_line('nogcp', 'Disable GCP support.') |
| 1761 | config_info_line('nohdfs', 'Disable HDFS support.') |
Penporn Koanantakool | 489f1dc | 2019-01-10 22:07:22 -0800 | [diff] [blame] | 1762 | config_info_line('noignite', 'Disable Apache Ignite support.') |
Gunhan Gulsoy | 3437098 | 2018-10-12 15:26:01 -0700 | [diff] [blame] | 1763 | config_info_line('nokafka', 'Disable Apache Kafka support.') |
Gunhan Gulsoy | eea8168 | 2018-11-26 16:51:23 -0800 | [diff] [blame] | 1764 | config_info_line('nonccl', 'Disable NVIDIA NCCL support.') |
Austin Anderson | 6afface | 2017-12-05 11:59:17 -0800 | [diff] [blame] | 1765 | |
Gunhan Gulsoy | ffa90fc | 2018-09-26 01:38:55 -0700 | [diff] [blame] | 1766 | |
A. Unique TensorFlower | 73ea287 | 2017-07-25 13:30:03 -0700 | [diff] [blame] | 1767 | if __name__ == '__main__': |
| 1768 | main() |