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