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