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 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21import argparse 22import errno 23import glob 24import os 25import platform 26import re 27import subprocess 28import sys 29 30# pylint: disable=g-import-not-at-top 31try: 32 from shutil import which 33except ImportError: 34 from distutils.spawn import find_executable as which 35# pylint: enable=g-import-not-at-top 36 37_DEFAULT_CUDA_VERSION = '10' 38_DEFAULT_CUDNN_VERSION = '7' 39_DEFAULT_TENSORRT_VERSION = '6' 40_DEFAULT_CUDA_COMPUTE_CAPABILITIES = '3.5,7.0' 41 42_SUPPORTED_ANDROID_NDK_VERSIONS = [10, 11, 12, 13, 14, 15, 16, 17, 18] 43 44_DEFAULT_PROMPT_ASK_ATTEMPTS = 10 45 46_TF_BAZELRC_FILENAME = '.tf_configure.bazelrc' 47_TF_WORKSPACE_ROOT = '' 48_TF_BAZELRC = '' 49_TF_CURRENT_BAZEL_VERSION = None 50_TF_MIN_BAZEL_VERSION = '3.7.2' 51_TF_MAX_BAZEL_VERSION = '3.99.0' 52 53NCCL_LIB_PATHS = [ 54 'lib64/', 'lib/powerpc64le-linux-gnu/', 'lib/x86_64-linux-gnu/', '' 55] 56 57# List of files to configure when building Bazel on Apple platforms. 58APPLE_BAZEL_FILES = [ 59 'tensorflow/lite/ios/BUILD', 'tensorflow/lite/objc/BUILD', 60 'tensorflow/lite/swift/BUILD', 61 'tensorflow/lite/tools/benchmark/experimental/ios/BUILD' 62] 63 64# List of files to move when building for iOS. 65IOS_FILES = [ 66 'tensorflow/lite/objc/TensorFlowLiteObjC.podspec', 67 'tensorflow/lite/swift/TensorFlowLiteSwift.podspec', 68] 69 70 71class UserInputError(Exception): 72 pass 73 74 75def is_windows(): 76 return platform.system() == 'Windows' 77 78 79def is_linux(): 80 return platform.system() == 'Linux' 81 82 83def is_macos(): 84 return platform.system() == 'Darwin' 85 86 87def is_ppc64le(): 88 return platform.machine() == 'ppc64le' 89 90 91def is_cygwin(): 92 return platform.system().startswith('CYGWIN_NT') 93 94 95def get_input(question): 96 try: 97 try: 98 answer = raw_input(question) 99 except NameError: 100 answer = input(question) # pylint: disable=bad-builtin 101 except EOFError: 102 answer = '' 103 return answer 104 105 106def symlink_force(target, link_name): 107 """Force symlink, equivalent of 'ln -sf'. 108 109 Args: 110 target: items to link to. 111 link_name: name of the link. 112 """ 113 try: 114 os.symlink(target, link_name) 115 except OSError as e: 116 if e.errno == errno.EEXIST: 117 os.remove(link_name) 118 os.symlink(target, link_name) 119 else: 120 raise e 121 122 123def sed_in_place(filename, old, new): 124 """Replace old string with new string in file. 125 126 Args: 127 filename: string for filename. 128 old: string to replace. 129 new: new string to replace to. 130 """ 131 with open(filename, 'r') as f: 132 filedata = f.read() 133 newdata = filedata.replace(old, new) 134 with open(filename, 'w') as f: 135 f.write(newdata) 136 137 138def write_to_bazelrc(line): 139 with open(_TF_BAZELRC, 'a') as f: 140 f.write(line + '\n') 141 142 143def write_action_env_to_bazelrc(var_name, var): 144 write_to_bazelrc('build --action_env {}="{}"'.format(var_name, str(var))) 145 146 147def run_shell(cmd, allow_non_zero=False, stderr=None): 148 if stderr is None: 149 stderr = sys.stdout 150 if allow_non_zero: 151 try: 152 output = subprocess.check_output(cmd, stderr=stderr) 153 except subprocess.CalledProcessError as e: 154 output = e.output 155 else: 156 output = subprocess.check_output(cmd, stderr=stderr) 157 return output.decode('UTF-8').strip() 158 159 160def cygpath(path): 161 """Convert path from posix to windows.""" 162 return os.path.abspath(path).replace('\\', '/') 163 164 165def get_python_path(environ_cp, python_bin_path): 166 """Get the python site package paths.""" 167 python_paths = [] 168 if environ_cp.get('PYTHONPATH'): 169 python_paths = environ_cp.get('PYTHONPATH').split(':') 170 try: 171 stderr = open(os.devnull, 'wb') 172 library_paths = run_shell([ 173 python_bin_path, '-c', 174 'import site; print("\\n".join(site.getsitepackages()))' 175 ], 176 stderr=stderr).split('\n') 177 except subprocess.CalledProcessError: 178 library_paths = [ 179 run_shell([ 180 python_bin_path, '-c', 181 'from distutils.sysconfig import get_python_lib;' 182 'print(get_python_lib())' 183 ]) 184 ] 185 186 all_paths = set(python_paths + library_paths) 187 # Sort set so order is deterministic 188 all_paths = sorted(all_paths) 189 190 paths = [] 191 for path in all_paths: 192 if os.path.isdir(path): 193 paths.append(path) 194 return paths 195 196 197def get_python_major_version(python_bin_path): 198 """Get the python major version.""" 199 return run_shell([python_bin_path, '-c', 'import sys; print(sys.version[0])']) 200 201 202def setup_python(environ_cp): 203 """Setup python related env variables.""" 204 # Get PYTHON_BIN_PATH, default is the current running python. 205 default_python_bin_path = sys.executable 206 ask_python_bin_path = ('Please specify the location of python. [Default is ' 207 '{}]: ').format(default_python_bin_path) 208 while True: 209 python_bin_path = get_from_env_or_user_or_default(environ_cp, 210 'PYTHON_BIN_PATH', 211 ask_python_bin_path, 212 default_python_bin_path) 213 # Check if the path is valid 214 if os.path.isfile(python_bin_path) and os.access(python_bin_path, os.X_OK): 215 break 216 elif not os.path.exists(python_bin_path): 217 print('Invalid python path: {} cannot be found.'.format(python_bin_path)) 218 else: 219 print('{} is not executable. Is it the python binary?'.format( 220 python_bin_path)) 221 environ_cp['PYTHON_BIN_PATH'] = '' 222 223 # Convert python path to Windows style before checking lib and version 224 if is_windows() or is_cygwin(): 225 python_bin_path = cygpath(python_bin_path) 226 227 # Get PYTHON_LIB_PATH 228 python_lib_path = environ_cp.get('PYTHON_LIB_PATH') 229 if not python_lib_path: 230 python_lib_paths = get_python_path(environ_cp, python_bin_path) 231 if environ_cp.get('USE_DEFAULT_PYTHON_LIB_PATH') == '1': 232 python_lib_path = python_lib_paths[0] 233 else: 234 print('Found possible Python library paths:\n %s' % 235 '\n '.join(python_lib_paths)) 236 default_python_lib_path = python_lib_paths[0] 237 python_lib_path = get_input( 238 'Please input the desired Python library path to use. ' 239 'Default is [{}]\n'.format(python_lib_paths[0])) 240 if not python_lib_path: 241 python_lib_path = default_python_lib_path 242 environ_cp['PYTHON_LIB_PATH'] = python_lib_path 243 244 python_major_version = get_python_major_version(python_bin_path) 245 if python_major_version == '2': 246 write_to_bazelrc('build --host_force_python=PY2') 247 248 # Convert python path to Windows style before writing into bazel.rc 249 if is_windows() or is_cygwin(): 250 python_lib_path = cygpath(python_lib_path) 251 252 # Set-up env variables used by python_configure.bzl 253 write_action_env_to_bazelrc('PYTHON_BIN_PATH', python_bin_path) 254 write_action_env_to_bazelrc('PYTHON_LIB_PATH', python_lib_path) 255 write_to_bazelrc('build --python_path=\"{}"'.format(python_bin_path)) 256 environ_cp['PYTHON_BIN_PATH'] = python_bin_path 257 258 # If choosen python_lib_path is from a path specified in the PYTHONPATH 259 # variable, need to tell bazel to include PYTHONPATH 260 if environ_cp.get('PYTHONPATH'): 261 python_paths = environ_cp.get('PYTHONPATH').split(':') 262 if python_lib_path in python_paths: 263 write_action_env_to_bazelrc('PYTHONPATH', environ_cp.get('PYTHONPATH')) 264 265 # Write tools/python_bin_path.sh 266 with open( 267 os.path.join(_TF_WORKSPACE_ROOT, 'tools', 'python_bin_path.sh'), 268 'w') as f: 269 f.write('export PYTHON_BIN_PATH="{}"'.format(python_bin_path)) 270 271 272def reset_tf_configure_bazelrc(): 273 """Reset file that contains customized config settings.""" 274 open(_TF_BAZELRC, 'w').close() 275 276 277def cleanup_makefile(): 278 """Delete any leftover BUILD files from the Makefile build. 279 280 These files could interfere with Bazel parsing. 281 """ 282 makefile_download_dir = os.path.join(_TF_WORKSPACE_ROOT, 'tensorflow', 283 'contrib', 'makefile', 'downloads') 284 if os.path.isdir(makefile_download_dir): 285 for root, _, filenames in os.walk(makefile_download_dir): 286 for f in filenames: 287 if f.endswith('BUILD'): 288 os.remove(os.path.join(root, f)) 289 290 291def get_var(environ_cp, 292 var_name, 293 query_item, 294 enabled_by_default, 295 question=None, 296 yes_reply=None, 297 no_reply=None): 298 """Get boolean input from user. 299 300 If var_name is not set in env, ask user to enable query_item or not. If the 301 response is empty, use the default. 302 303 Args: 304 environ_cp: copy of the os.environ. 305 var_name: string for name of environment variable, e.g. "TF_NEED_CUDA". 306 query_item: string for feature related to the variable, e.g. "CUDA for 307 Nvidia GPUs". 308 enabled_by_default: boolean for default behavior. 309 question: optional string for how to ask for user input. 310 yes_reply: optional string for reply when feature is enabled. 311 no_reply: optional string for reply when feature is disabled. 312 313 Returns: 314 boolean value of the variable. 315 316 Raises: 317 UserInputError: if an environment variable is set, but it cannot be 318 interpreted as a boolean indicator, assume that the user has made a 319 scripting error, and will continue to provide invalid input. 320 Raise the error to avoid infinitely looping. 321 """ 322 if not question: 323 question = 'Do you wish to build TensorFlow with {} support?'.format( 324 query_item) 325 if not yes_reply: 326 yes_reply = '{} support will be enabled for TensorFlow.'.format(query_item) 327 if not no_reply: 328 no_reply = 'No {}'.format(yes_reply) 329 330 yes_reply += '\n' 331 no_reply += '\n' 332 333 if enabled_by_default: 334 question += ' [Y/n]: ' 335 else: 336 question += ' [y/N]: ' 337 338 var = environ_cp.get(var_name) 339 if var is not None: 340 var_content = var.strip().lower() 341 true_strings = ('1', 't', 'true', 'y', 'yes') 342 false_strings = ('0', 'f', 'false', 'n', 'no') 343 if var_content in true_strings: 344 var = True 345 elif var_content in false_strings: 346 var = False 347 else: 348 raise UserInputError( 349 'Environment variable %s must be set as a boolean indicator.\n' 350 'The following are accepted as TRUE : %s.\n' 351 'The following are accepted as FALSE: %s.\n' 352 'Current value is %s.' % 353 (var_name, ', '.join(true_strings), ', '.join(false_strings), var)) 354 355 while var is None: 356 user_input_origin = get_input(question) 357 user_input = user_input_origin.strip().lower() 358 if user_input == 'y': 359 print(yes_reply) 360 var = True 361 elif user_input == 'n': 362 print(no_reply) 363 var = False 364 elif not user_input: 365 if enabled_by_default: 366 print(yes_reply) 367 var = True 368 else: 369 print(no_reply) 370 var = False 371 else: 372 print('Invalid selection: {}'.format(user_input_origin)) 373 return var 374 375 376def set_build_var(environ_cp, 377 var_name, 378 query_item, 379 option_name, 380 enabled_by_default, 381 bazel_config_name=None): 382 """Set if query_item will be enabled for the build. 383 384 Ask user if query_item will be enabled. Default is used if no input is given. 385 Set subprocess environment variable and write to .bazelrc if enabled. 386 387 Args: 388 environ_cp: copy of the os.environ. 389 var_name: string for name of environment variable, e.g. "TF_NEED_CUDA". 390 query_item: string for feature related to the variable, e.g. "CUDA for 391 Nvidia GPUs". 392 option_name: string for option to define in .bazelrc. 393 enabled_by_default: boolean for default behavior. 394 bazel_config_name: Name for Bazel --config argument to enable build feature. 395 """ 396 397 var = str(int(get_var(environ_cp, var_name, query_item, enabled_by_default))) 398 environ_cp[var_name] = var 399 if var == '1': 400 write_to_bazelrc('build:%s --define %s=true' % 401 (bazel_config_name, option_name)) 402 write_to_bazelrc('build --config=%s' % bazel_config_name) 403 elif bazel_config_name is not None: 404 # TODO(mikecase): Migrate all users of configure.py to use --config Bazel 405 # options and not to set build configs through environment variables. 406 write_to_bazelrc('build:%s --define %s=true' % 407 (bazel_config_name, option_name)) 408 409 410def set_action_env_var(environ_cp, 411 var_name, 412 query_item, 413 enabled_by_default, 414 question=None, 415 yes_reply=None, 416 no_reply=None, 417 bazel_config_name=None): 418 """Set boolean action_env variable. 419 420 Ask user if query_item will be enabled. Default is used if no input is given. 421 Set environment variable and write to .bazelrc. 422 423 Args: 424 environ_cp: copy of the os.environ. 425 var_name: string for name of environment variable, e.g. "TF_NEED_CUDA". 426 query_item: string for feature related to the variable, e.g. "CUDA for 427 Nvidia GPUs". 428 enabled_by_default: boolean for default behavior. 429 question: optional string for how to ask for user input. 430 yes_reply: optional string for reply when feature is enabled. 431 no_reply: optional string for reply when feature is disabled. 432 bazel_config_name: adding config to .bazelrc instead of action_env. 433 """ 434 var = int( 435 get_var(environ_cp, var_name, query_item, enabled_by_default, question, 436 yes_reply, no_reply)) 437 438 if not bazel_config_name: 439 write_action_env_to_bazelrc(var_name, var) 440 elif var: 441 write_to_bazelrc('build --config=%s' % bazel_config_name) 442 environ_cp[var_name] = str(var) 443 444 445def convert_version_to_int(version): 446 """Convert a version number to a integer that can be used to compare. 447 448 Version strings of the form X.YZ and X.Y.Z-xxxxx are supported. The 449 'xxxxx' part, for instance 'homebrew' on OS/X, is ignored. 450 451 Args: 452 version: a version to be converted 453 454 Returns: 455 An integer if converted successfully, otherwise return None. 456 """ 457 version = version.split('-')[0] 458 version_segments = version.split('.') 459 # Treat "0.24" as "0.24.0" 460 if len(version_segments) == 2: 461 version_segments.append('0') 462 for seg in version_segments: 463 if not seg.isdigit(): 464 return None 465 466 version_str = ''.join(['%03d' % int(seg) for seg in version_segments]) 467 return int(version_str) 468 469 470def check_bazel_version(min_version, max_version): 471 """Check installed bazel version is between min_version and max_version. 472 473 Args: 474 min_version: string for minimum bazel version (must exist!). 475 max_version: string for maximum bazel version (must exist!). 476 477 Returns: 478 The bazel version detected. 479 """ 480 if which('bazel') is None: 481 print('Cannot find bazel. Please install bazel.') 482 sys.exit(1) 483 484 stderr = open(os.devnull, 'wb') 485 curr_version = run_shell(['bazel', '--version'], 486 allow_non_zero=True, 487 stderr=stderr) 488 if curr_version.startswith('bazel '): 489 curr_version = curr_version.split('bazel ')[1] 490 491 min_version_int = convert_version_to_int(min_version) 492 curr_version_int = convert_version_to_int(curr_version) 493 max_version_int = convert_version_to_int(max_version) 494 495 # Check if current bazel version can be detected properly. 496 if not curr_version_int: 497 print('WARNING: current bazel installation is not a release version.') 498 print('Make sure you are running at least bazel %s' % min_version) 499 return curr_version 500 501 print('You have bazel %s installed.' % curr_version) 502 503 if curr_version_int < min_version_int: 504 print('Please upgrade your bazel installation to version %s or higher to ' 505 'build TensorFlow!' % min_version) 506 sys.exit(1) 507 if (curr_version_int > max_version_int and 508 'TF_IGNORE_MAX_BAZEL_VERSION' not in os.environ): 509 print('Please downgrade your bazel installation to version %s or lower to ' 510 'build TensorFlow! To downgrade: download the installer for the old ' 511 'version (from https://github.com/bazelbuild/bazel/releases) then ' 512 'run the installer.' % max_version) 513 sys.exit(1) 514 return curr_version 515 516 517def set_cc_opt_flags(environ_cp): 518 """Set up architecture-dependent optimization flags. 519 520 Also append CC optimization flags to bazel.rc.. 521 522 Args: 523 environ_cp: copy of the os.environ. 524 """ 525 if is_ppc64le(): 526 # gcc on ppc64le does not support -march, use mcpu instead 527 default_cc_opt_flags = '-mcpu=native' 528 elif is_windows(): 529 default_cc_opt_flags = '/arch:AVX' 530 else: 531 # On all other platforms, no longer use `-march=native` as this can result 532 # in instructions that are too modern being generated. Users that want 533 # maximum performance should compile TF in their environment and can pass 534 # `-march=native` there. 535 # See https://github.com/tensorflow/tensorflow/issues/45744 and duplicates 536 default_cc_opt_flags = '-Wno-sign-compare' 537 question = ('Please specify optimization flags to use during compilation when' 538 ' bazel option "--config=opt" is specified [Default is %s]: ' 539 ) % default_cc_opt_flags 540 cc_opt_flags = get_from_env_or_user_or_default(environ_cp, 'CC_OPT_FLAGS', 541 question, default_cc_opt_flags) 542 for opt in cc_opt_flags.split(): 543 write_to_bazelrc('build:opt --copt=%s' % opt) 544 write_to_bazelrc('build:opt --host_copt=%s' % opt) 545 write_to_bazelrc('build:opt --define with_default_optimizations=true') 546 547 548def set_tf_cuda_clang(environ_cp): 549 """set TF_CUDA_CLANG action_env. 550 551 Args: 552 environ_cp: copy of the os.environ. 553 """ 554 question = 'Do you want to use clang as CUDA compiler?' 555 yes_reply = 'Clang will be used as CUDA compiler.' 556 no_reply = 'nvcc will be used as CUDA compiler.' 557 set_action_env_var( 558 environ_cp, 559 'TF_CUDA_CLANG', 560 None, 561 False, 562 question=question, 563 yes_reply=yes_reply, 564 no_reply=no_reply, 565 bazel_config_name='cuda_clang') 566 567 568def set_tf_download_clang(environ_cp): 569 """Set TF_DOWNLOAD_CLANG action_env.""" 570 question = 'Do you wish to download a fresh release of clang? (Experimental)' 571 yes_reply = 'Clang will be downloaded and used to compile tensorflow.' 572 no_reply = 'Clang will not be downloaded.' 573 set_action_env_var( 574 environ_cp, 575 'TF_DOWNLOAD_CLANG', 576 None, 577 False, 578 question=question, 579 yes_reply=yes_reply, 580 no_reply=no_reply, 581 bazel_config_name='download_clang') 582 583 584def get_from_env_or_user_or_default(environ_cp, var_name, ask_for_var, 585 var_default): 586 """Get var_name either from env, or user or default. 587 588 If var_name has been set as environment variable, use the preset value, else 589 ask for user input. If no input is provided, the default is used. 590 591 Args: 592 environ_cp: copy of the os.environ. 593 var_name: string for name of environment variable, e.g. "TF_NEED_CUDA". 594 ask_for_var: string for how to ask for user input. 595 var_default: default value string. 596 597 Returns: 598 string value for var_name 599 """ 600 var = environ_cp.get(var_name) 601 if not var: 602 var = get_input(ask_for_var) 603 print('\n') 604 if not var: 605 var = var_default 606 return var 607 608 609def set_clang_cuda_compiler_path(environ_cp): 610 """Set CLANG_CUDA_COMPILER_PATH.""" 611 default_clang_path = which('clang') or '' 612 ask_clang_path = ('Please specify which clang should be used as device and ' 613 'host compiler. [Default is %s]: ') % default_clang_path 614 615 while True: 616 clang_cuda_compiler_path = get_from_env_or_user_or_default( 617 environ_cp, 'CLANG_CUDA_COMPILER_PATH', ask_clang_path, 618 default_clang_path) 619 if os.path.exists(clang_cuda_compiler_path): 620 break 621 622 # Reset and retry 623 print('Invalid clang path: %s cannot be found.' % clang_cuda_compiler_path) 624 environ_cp['CLANG_CUDA_COMPILER_PATH'] = '' 625 626 # Set CLANG_CUDA_COMPILER_PATH 627 environ_cp['CLANG_CUDA_COMPILER_PATH'] = clang_cuda_compiler_path 628 write_action_env_to_bazelrc('CLANG_CUDA_COMPILER_PATH', 629 clang_cuda_compiler_path) 630 631 632def prompt_loop_or_load_from_env(environ_cp, 633 var_name, 634 var_default, 635 ask_for_var, 636 check_success, 637 error_msg, 638 suppress_default_error=False, 639 resolve_symlinks=False, 640 n_ask_attempts=_DEFAULT_PROMPT_ASK_ATTEMPTS): 641 """Loop over user prompts for an ENV param until receiving a valid response. 642 643 For the env param var_name, read from the environment or verify user input 644 until receiving valid input. When done, set var_name in the environ_cp to its 645 new value. 646 647 Args: 648 environ_cp: (Dict) copy of the os.environ. 649 var_name: (String) string for name of environment variable, e.g. "TF_MYVAR". 650 var_default: (String) default value string. 651 ask_for_var: (String) string for how to ask for user input. 652 check_success: (Function) function that takes one argument and returns a 653 boolean. Should return True if the value provided is considered valid. May 654 contain a complex error message if error_msg does not provide enough 655 information. In that case, set suppress_default_error to True. 656 error_msg: (String) String with one and only one '%s'. Formatted with each 657 invalid response upon check_success(input) failure. 658 suppress_default_error: (Bool) Suppress the above error message in favor of 659 one from the check_success function. 660 resolve_symlinks: (Bool) Translate symbolic links into the real filepath. 661 n_ask_attempts: (Integer) Number of times to query for valid input before 662 raising an error and quitting. 663 664 Returns: 665 [String] The value of var_name after querying for input. 666 667 Raises: 668 UserInputError: if a query has been attempted n_ask_attempts times without 669 success, assume that the user has made a scripting error, and will 670 continue to provide invalid input. Raise the error to avoid infinitely 671 looping. 672 """ 673 default = environ_cp.get(var_name) or var_default 674 full_query = '%s [Default is %s]: ' % ( 675 ask_for_var, 676 default, 677 ) 678 679 for _ in range(n_ask_attempts): 680 val = get_from_env_or_user_or_default(environ_cp, var_name, full_query, 681 default) 682 if check_success(val): 683 break 684 if not suppress_default_error: 685 print(error_msg % val) 686 environ_cp[var_name] = '' 687 else: 688 raise UserInputError('Invalid %s setting was provided %d times in a row. ' 689 'Assuming to be a scripting mistake.' % 690 (var_name, n_ask_attempts)) 691 692 if resolve_symlinks and os.path.islink(val): 693 val = os.path.realpath(val) 694 environ_cp[var_name] = val 695 return val 696 697 698def create_android_ndk_rule(environ_cp): 699 """Set ANDROID_NDK_HOME and write Android NDK WORKSPACE rule.""" 700 if is_windows() or is_cygwin(): 701 default_ndk_path = cygpath('%s/Android/Sdk/ndk-bundle' % 702 environ_cp['APPDATA']) 703 elif is_macos(): 704 default_ndk_path = '%s/library/Android/Sdk/ndk-bundle' % environ_cp['HOME'] 705 else: 706 default_ndk_path = '%s/Android/Sdk/ndk-bundle' % environ_cp['HOME'] 707 708 def valid_ndk_path(path): 709 return (os.path.exists(path) and 710 os.path.exists(os.path.join(path, 'source.properties'))) 711 712 android_ndk_home_path = prompt_loop_or_load_from_env( 713 environ_cp, 714 var_name='ANDROID_NDK_HOME', 715 var_default=default_ndk_path, 716 ask_for_var='Please specify the home path of the Android NDK to use.', 717 check_success=valid_ndk_path, 718 error_msg=('The path %s or its child file "source.properties" ' 719 'does not exist.')) 720 write_action_env_to_bazelrc('ANDROID_NDK_HOME', android_ndk_home_path) 721 write_action_env_to_bazelrc( 722 'ANDROID_NDK_API_LEVEL', 723 get_ndk_api_level(environ_cp, android_ndk_home_path)) 724 725 726def create_android_sdk_rule(environ_cp): 727 """Set Android variables and write Android SDK WORKSPACE rule.""" 728 if is_windows() or is_cygwin(): 729 default_sdk_path = cygpath('%s/Android/Sdk' % environ_cp['APPDATA']) 730 elif is_macos(): 731 default_sdk_path = '%s/library/Android/Sdk' % environ_cp['HOME'] 732 else: 733 default_sdk_path = '%s/Android/Sdk' % environ_cp['HOME'] 734 735 def valid_sdk_path(path): 736 return (os.path.exists(path) and 737 os.path.exists(os.path.join(path, 'platforms')) and 738 os.path.exists(os.path.join(path, 'build-tools'))) 739 740 android_sdk_home_path = prompt_loop_or_load_from_env( 741 environ_cp, 742 var_name='ANDROID_SDK_HOME', 743 var_default=default_sdk_path, 744 ask_for_var='Please specify the home path of the Android SDK to use.', 745 check_success=valid_sdk_path, 746 error_msg=('Either %s does not exist, or it does not contain the ' 747 'subdirectories "platforms" and "build-tools".')) 748 749 platforms = os.path.join(android_sdk_home_path, 'platforms') 750 api_levels = sorted(os.listdir(platforms)) 751 api_levels = [x.replace('android-', '') for x in api_levels] 752 753 def valid_api_level(api_level): 754 return os.path.exists( 755 os.path.join(android_sdk_home_path, 'platforms', 756 'android-' + api_level)) 757 758 android_api_level = prompt_loop_or_load_from_env( 759 environ_cp, 760 var_name='ANDROID_API_LEVEL', 761 var_default=api_levels[-1], 762 ask_for_var=('Please specify the Android SDK API level to use. ' 763 '[Available levels: %s]') % api_levels, 764 check_success=valid_api_level, 765 error_msg='Android-%s is not present in the SDK path.') 766 767 build_tools = os.path.join(android_sdk_home_path, 'build-tools') 768 versions = sorted(os.listdir(build_tools)) 769 770 def valid_build_tools(version): 771 return os.path.exists( 772 os.path.join(android_sdk_home_path, 'build-tools', version)) 773 774 android_build_tools_version = prompt_loop_or_load_from_env( 775 environ_cp, 776 var_name='ANDROID_BUILD_TOOLS_VERSION', 777 var_default=versions[-1], 778 ask_for_var=('Please specify an Android build tools version to use. ' 779 '[Available versions: %s]') % versions, 780 check_success=valid_build_tools, 781 error_msg=('The selected SDK does not have build-tools version %s ' 782 'available.')) 783 784 write_action_env_to_bazelrc('ANDROID_BUILD_TOOLS_VERSION', 785 android_build_tools_version) 786 write_action_env_to_bazelrc('ANDROID_SDK_API_LEVEL', android_api_level) 787 write_action_env_to_bazelrc('ANDROID_SDK_HOME', android_sdk_home_path) 788 789 790def get_ndk_api_level(environ_cp, android_ndk_home_path): 791 """Gets the appropriate NDK API level to use for the provided Android NDK path.""" 792 793 # First check to see if we're using a blessed version of the NDK. 794 properties_path = '%s/source.properties' % android_ndk_home_path 795 if is_windows() or is_cygwin(): 796 properties_path = cygpath(properties_path) 797 with open(properties_path, 'r') as f: 798 filedata = f.read() 799 800 revision = re.search(r'Pkg.Revision = (\d+)', filedata) 801 if revision: 802 ndk_version = revision.group(1) 803 else: 804 raise Exception('Unable to parse NDK revision.') 805 if int(ndk_version) not in _SUPPORTED_ANDROID_NDK_VERSIONS: 806 print('WARNING: The NDK version in %s is %s, which is not ' 807 'supported by Bazel (officially supported versions: %s). Please use ' 808 'another version. Compiling Android targets may result in confusing ' 809 'errors.\n' % 810 (android_ndk_home_path, ndk_version, _SUPPORTED_ANDROID_NDK_VERSIONS)) 811 812 # Now grab the NDK API level to use. Note that this is different from the 813 # SDK API level, as the NDK API level is effectively the *min* target SDK 814 # version. 815 platforms = os.path.join(android_ndk_home_path, 'platforms') 816 api_levels = sorted(os.listdir(platforms)) 817 api_levels = [ 818 x.replace('android-', '') for x in api_levels if 'android-' in x 819 ] 820 821 def valid_api_level(api_level): 822 return os.path.exists( 823 os.path.join(android_ndk_home_path, 'platforms', 824 'android-' + api_level)) 825 826 android_ndk_api_level = prompt_loop_or_load_from_env( 827 environ_cp, 828 var_name='ANDROID_NDK_API_LEVEL', 829 var_default='21', # 21 is required for ARM64 support. 830 ask_for_var=('Please specify the (min) Android NDK API level to use. ' 831 '[Available levels: %s]') % api_levels, 832 check_success=valid_api_level, 833 error_msg='Android-%s is not present in the NDK path.') 834 835 return android_ndk_api_level 836 837 838def set_gcc_host_compiler_path(environ_cp): 839 """Set GCC_HOST_COMPILER_PATH.""" 840 default_gcc_host_compiler_path = which('gcc') or '' 841 cuda_bin_symlink = '%s/bin/gcc' % environ_cp.get('CUDA_TOOLKIT_PATH') 842 843 if os.path.islink(cuda_bin_symlink): 844 # os.readlink is only available in linux 845 default_gcc_host_compiler_path = os.path.realpath(cuda_bin_symlink) 846 847 gcc_host_compiler_path = prompt_loop_or_load_from_env( 848 environ_cp, 849 var_name='GCC_HOST_COMPILER_PATH', 850 var_default=default_gcc_host_compiler_path, 851 ask_for_var='Please specify which gcc should be used by nvcc as the host compiler.', 852 check_success=os.path.exists, 853 resolve_symlinks=True, 854 error_msg='Invalid gcc path. %s cannot be found.', 855 ) 856 857 write_action_env_to_bazelrc('GCC_HOST_COMPILER_PATH', gcc_host_compiler_path) 858 859 860def reformat_version_sequence(version_str, sequence_count): 861 """Reformat the version string to have the given number of sequences. 862 863 For example: 864 Given (7, 2) -> 7.0 865 (7.0.1, 2) -> 7.0 866 (5, 1) -> 5 867 (5.0.3.2, 1) -> 5 868 869 Args: 870 version_str: String, the version string. 871 sequence_count: int, an integer. 872 873 Returns: 874 string, reformatted version string. 875 """ 876 v = version_str.split('.') 877 if len(v) < sequence_count: 878 v = v + (['0'] * (sequence_count - len(v))) 879 880 return '.'.join(v[:sequence_count]) 881 882 883def set_tf_cuda_paths(environ_cp): 884 """Set TF_CUDA_PATHS.""" 885 ask_cuda_paths = ( 886 'Please specify the comma-separated list of base paths to look for CUDA ' 887 'libraries and headers. [Leave empty to use the default]: ') 888 tf_cuda_paths = get_from_env_or_user_or_default(environ_cp, 'TF_CUDA_PATHS', 889 ask_cuda_paths, '') 890 if tf_cuda_paths: 891 environ_cp['TF_CUDA_PATHS'] = tf_cuda_paths 892 893 894def set_tf_cuda_version(environ_cp): 895 """Set TF_CUDA_VERSION.""" 896 ask_cuda_version = ( 897 'Please specify the CUDA SDK version you want to use. ' 898 '[Leave empty to default to CUDA %s]: ') % _DEFAULT_CUDA_VERSION 899 tf_cuda_version = get_from_env_or_user_or_default(environ_cp, 900 'TF_CUDA_VERSION', 901 ask_cuda_version, 902 _DEFAULT_CUDA_VERSION) 903 environ_cp['TF_CUDA_VERSION'] = tf_cuda_version 904 905 906def set_tf_cudnn_version(environ_cp): 907 """Set TF_CUDNN_VERSION.""" 908 ask_cudnn_version = ( 909 'Please specify the cuDNN version you want to use. ' 910 '[Leave empty to default to cuDNN %s]: ') % _DEFAULT_CUDNN_VERSION 911 tf_cudnn_version = get_from_env_or_user_or_default(environ_cp, 912 'TF_CUDNN_VERSION', 913 ask_cudnn_version, 914 _DEFAULT_CUDNN_VERSION) 915 environ_cp['TF_CUDNN_VERSION'] = tf_cudnn_version 916 917 918def is_cuda_compatible(lib, cuda_ver, cudnn_ver): 919 """Check compatibility between given library and cudnn/cudart libraries.""" 920 ldd_bin = which('ldd') or '/usr/bin/ldd' 921 ldd_out = run_shell([ldd_bin, lib], True) 922 ldd_out = ldd_out.split(os.linesep) 923 cudnn_pattern = re.compile('.*libcudnn.so\\.?(.*) =>.*$') 924 cuda_pattern = re.compile('.*libcudart.so\\.?(.*) =>.*$') 925 cudnn = None 926 cudart = None 927 cudnn_ok = True # assume no cudnn dependency by default 928 cuda_ok = True # assume no cuda dependency by default 929 for line in ldd_out: 930 if 'libcudnn.so' in line: 931 cudnn = cudnn_pattern.search(line) 932 cudnn_ok = False 933 elif 'libcudart.so' in line: 934 cudart = cuda_pattern.search(line) 935 cuda_ok = False 936 if cudnn and len(cudnn.group(1)): 937 cudnn = convert_version_to_int(cudnn.group(1)) 938 if cudart and len(cudart.group(1)): 939 cudart = convert_version_to_int(cudart.group(1)) 940 if cudnn is not None: 941 cudnn_ok = (cudnn == cudnn_ver) 942 if cudart is not None: 943 cuda_ok = (cudart == cuda_ver) 944 return cudnn_ok and cuda_ok 945 946 947def set_tf_tensorrt_version(environ_cp): 948 """Set TF_TENSORRT_VERSION.""" 949 if not is_linux(): 950 raise ValueError('Currently TensorRT is only supported on Linux platform.') 951 952 if not int(environ_cp.get('TF_NEED_TENSORRT', False)): 953 return 954 955 ask_tensorrt_version = ( 956 'Please specify the TensorRT version you want to use. ' 957 '[Leave empty to default to TensorRT %s]: ') % _DEFAULT_TENSORRT_VERSION 958 tf_tensorrt_version = get_from_env_or_user_or_default( 959 environ_cp, 'TF_TENSORRT_VERSION', ask_tensorrt_version, 960 _DEFAULT_TENSORRT_VERSION) 961 environ_cp['TF_TENSORRT_VERSION'] = tf_tensorrt_version 962 963 964def set_tf_nccl_version(environ_cp): 965 """Set TF_NCCL_VERSION.""" 966 if not is_linux(): 967 raise ValueError('Currently NCCL is only supported on Linux platform.') 968 969 if 'TF_NCCL_VERSION' in environ_cp: 970 return 971 972 ask_nccl_version = ( 973 'Please specify the locally installed NCCL version you want to use. ' 974 '[Leave empty to use http://github.com/nvidia/nccl]: ') 975 tf_nccl_version = get_from_env_or_user_or_default(environ_cp, 976 'TF_NCCL_VERSION', 977 ask_nccl_version, '') 978 environ_cp['TF_NCCL_VERSION'] = tf_nccl_version 979 980 981def get_native_cuda_compute_capabilities(environ_cp): 982 """Get native cuda compute capabilities. 983 984 Args: 985 environ_cp: copy of the os.environ. 986 987 Returns: 988 string of native cuda compute capabilities, separated by comma. 989 """ 990 device_query_bin = os.path.join( 991 environ_cp.get('CUDA_TOOLKIT_PATH'), 'extras/demo_suite/deviceQuery') 992 if os.path.isfile(device_query_bin) and os.access(device_query_bin, os.X_OK): 993 try: 994 output = run_shell(device_query_bin).split('\n') 995 pattern = re.compile('[0-9]*\\.[0-9]*') 996 output = [pattern.search(x) for x in output if 'Capability' in x] 997 output = ','.join(x.group() for x in output if x is not None) 998 except subprocess.CalledProcessError: 999 output = '' 1000 else: 1001 output = '' 1002 return output 1003 1004 1005def set_tf_cuda_compute_capabilities(environ_cp): 1006 """Set TF_CUDA_COMPUTE_CAPABILITIES.""" 1007 while True: 1008 native_cuda_compute_capabilities = get_native_cuda_compute_capabilities( 1009 environ_cp) 1010 if not native_cuda_compute_capabilities: 1011 default_cuda_compute_capabilities = _DEFAULT_CUDA_COMPUTE_CAPABILITIES 1012 else: 1013 default_cuda_compute_capabilities = native_cuda_compute_capabilities 1014 1015 ask_cuda_compute_capabilities = ( 1016 'Please specify a list of comma-separated CUDA compute capabilities ' 1017 'you want to build with.\nYou can find the compute capability of your ' 1018 'device at: https://developer.nvidia.com/cuda-gpus. Each capability ' 1019 'can be specified as "x.y" or "compute_xy" to include both virtual and' 1020 ' binary GPU code, or as "sm_xy" to only include the binary ' 1021 'code.\nPlease note that each additional compute capability ' 1022 'significantly increases your build time and binary size, and that ' 1023 'TensorFlow only supports compute capabilities >= 3.5 [Default is: ' 1024 '%s]: ' % default_cuda_compute_capabilities) 1025 tf_cuda_compute_capabilities = get_from_env_or_user_or_default( 1026 environ_cp, 'TF_CUDA_COMPUTE_CAPABILITIES', 1027 ask_cuda_compute_capabilities, default_cuda_compute_capabilities) 1028 # Check whether all capabilities from the input is valid 1029 all_valid = True 1030 # Remove all whitespace characters before splitting the string 1031 # that users may insert by accident, as this will result in error 1032 tf_cuda_compute_capabilities = ''.join(tf_cuda_compute_capabilities.split()) 1033 for compute_capability in tf_cuda_compute_capabilities.split(','): 1034 m = re.match('[0-9]+.[0-9]+', compute_capability) 1035 if not m: 1036 # We now support sm_35,sm_50,sm_60,compute_70. 1037 sm_compute_match = re.match('(sm|compute)_?([0-9]+[0-9]+)', 1038 compute_capability) 1039 if not sm_compute_match: 1040 print('Invalid compute capability: %s' % compute_capability) 1041 all_valid = False 1042 else: 1043 ver = int(sm_compute_match.group(2)) 1044 if ver < 30: 1045 print( 1046 'ERROR: TensorFlow only supports small CUDA compute' 1047 ' capabilities of sm_30 and higher. Please re-specify the list' 1048 ' of compute capabilities excluding version %s.' % ver) 1049 all_valid = False 1050 if ver < 35: 1051 print('WARNING: XLA does not support CUDA compute capabilities ' 1052 'lower than sm_35. Disable XLA when running on older GPUs.') 1053 else: 1054 ver = float(m.group(0)) 1055 if ver < 3.0: 1056 print('ERROR: TensorFlow only supports CUDA compute capabilities 3.0 ' 1057 'and higher. Please re-specify the list of compute ' 1058 'capabilities excluding version %s.' % ver) 1059 all_valid = False 1060 if ver < 3.5: 1061 print('WARNING: XLA does not support CUDA compute capabilities ' 1062 'lower than 3.5. Disable XLA when running on older GPUs.') 1063 1064 if all_valid: 1065 break 1066 1067 # Reset and Retry 1068 environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = '' 1069 1070 # Set TF_CUDA_COMPUTE_CAPABILITIES 1071 environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = tf_cuda_compute_capabilities 1072 write_action_env_to_bazelrc('TF_CUDA_COMPUTE_CAPABILITIES', 1073 tf_cuda_compute_capabilities) 1074 1075 1076def set_other_cuda_vars(environ_cp): 1077 """Set other CUDA related variables.""" 1078 # If CUDA is enabled, always use GPU during build and test. 1079 if environ_cp.get('TF_CUDA_CLANG') == '1': 1080 write_to_bazelrc('build --config=cuda_clang') 1081 else: 1082 write_to_bazelrc('build --config=cuda') 1083 1084 1085def set_host_cxx_compiler(environ_cp): 1086 """Set HOST_CXX_COMPILER.""" 1087 default_cxx_host_compiler = which('g++') or '' 1088 1089 host_cxx_compiler = prompt_loop_or_load_from_env( 1090 environ_cp, 1091 var_name='HOST_CXX_COMPILER', 1092 var_default=default_cxx_host_compiler, 1093 ask_for_var=('Please specify which C++ compiler should be used as the ' 1094 'host C++ compiler.'), 1095 check_success=os.path.exists, 1096 error_msg='Invalid C++ compiler path. %s cannot be found.', 1097 ) 1098 1099 write_action_env_to_bazelrc('HOST_CXX_COMPILER', host_cxx_compiler) 1100 1101 1102def set_host_c_compiler(environ_cp): 1103 """Set HOST_C_COMPILER.""" 1104 default_c_host_compiler = which('gcc') or '' 1105 1106 host_c_compiler = prompt_loop_or_load_from_env( 1107 environ_cp, 1108 var_name='HOST_C_COMPILER', 1109 var_default=default_c_host_compiler, 1110 ask_for_var=('Please specify which C compiler should be used as the host ' 1111 'C compiler.'), 1112 check_success=os.path.exists, 1113 error_msg='Invalid C compiler path. %s cannot be found.', 1114 ) 1115 1116 write_action_env_to_bazelrc('HOST_C_COMPILER', host_c_compiler) 1117 1118 1119def system_specific_test_config(environ_cp): 1120 """Add default build and test flags required for TF tests to bazelrc.""" 1121 write_to_bazelrc('test --flaky_test_attempts=3') 1122 write_to_bazelrc('test --test_size_filters=small,medium') 1123 1124 # Each instance of --test_tag_filters or --build_tag_filters overrides all 1125 # previous instances, so we need to build up a complete list and write a 1126 # single list of filters for the .bazelrc file. 1127 1128 # Filters to use with both --test_tag_filters and --build_tag_filters 1129 test_and_build_filters = ['-benchmark-test', '-no_oss'] 1130 # Additional filters for --test_tag_filters beyond those in 1131 # test_and_build_filters 1132 test_only_filters = ['-oss_serial'] 1133 if is_windows(): 1134 test_and_build_filters.append('-no_windows') 1135 if ((environ_cp.get('TF_NEED_CUDA', None) == '1') or 1136 (environ_cp.get('TF_NEED_ROCM', None) == '1')): 1137 test_and_build_filters += ['-no_windows_gpu', '-no_gpu'] 1138 else: 1139 test_and_build_filters.append('-gpu') 1140 elif is_macos(): 1141 test_and_build_filters += ['-gpu', '-nomac', '-no_mac'] 1142 elif is_linux(): 1143 if ((environ_cp.get('TF_NEED_CUDA', None) == '1') or 1144 (environ_cp.get('TF_NEED_ROCM', None) == '1')): 1145 test_and_build_filters.append('-no_gpu') 1146 write_to_bazelrc('test --test_env=LD_LIBRARY_PATH') 1147 else: 1148 test_and_build_filters.append('-gpu') 1149 1150 # Disable tests with "v1only" tag in "v2" Bazel config, but not in "v1" config 1151 write_to_bazelrc('test:v1 --test_tag_filters=%s' % 1152 ','.join(test_and_build_filters + test_only_filters)) 1153 write_to_bazelrc('test:v1 --build_tag_filters=%s' % 1154 ','.join(test_and_build_filters)) 1155 write_to_bazelrc( 1156 'test:v2 --test_tag_filters=%s' % 1157 ','.join(test_and_build_filters + test_only_filters + ['-v1only'])) 1158 write_to_bazelrc('test:v2 --build_tag_filters=%s' % 1159 ','.join(test_and_build_filters + ['-v1only'])) 1160 1161 1162def set_system_libs_flag(environ_cp): 1163 syslibs = environ_cp.get('TF_SYSTEM_LIBS', '') 1164 if syslibs: 1165 if ',' in syslibs: 1166 syslibs = ','.join(sorted(syslibs.split(','))) 1167 else: 1168 syslibs = ','.join(sorted(syslibs.split())) 1169 write_action_env_to_bazelrc('TF_SYSTEM_LIBS', syslibs) 1170 1171 for varname in ('PREFIX', 'LIBDIR', 'INCLUDEDIR', 'PROTOBUF_INCLUDE_PATH'): 1172 if varname in environ_cp: 1173 write_to_bazelrc('build --define=%s=%s' % (varname, environ_cp[varname])) 1174 1175 1176def set_windows_build_flags(environ_cp): 1177 """Set Windows specific build options.""" 1178 1179 # First available in VS 16.4. Speeds up Windows compile times by a lot. See 1180 # https://groups.google.com/a/tensorflow.org/d/topic/build/SsW98Eo7l3o/discussion 1181 # pylint: disable=line-too-long 1182 write_to_bazelrc( 1183 'build --copt=/d2ReducedOptimizeHugeFunctions --host_copt=/d2ReducedOptimizeHugeFunctions' 1184 ) 1185 1186 if get_var( 1187 environ_cp, 'TF_OVERRIDE_EIGEN_STRONG_INLINE', 'Eigen strong inline', 1188 True, ('Would you like to override eigen strong inline for some C++ ' 1189 'compilation to reduce the compilation time?'), 1190 'Eigen strong inline overridden.', 'Not overriding eigen strong inline, ' 1191 'some compilations could take more than 20 mins.'): 1192 # Due to a known MSVC compiler issue 1193 # https://github.com/tensorflow/tensorflow/issues/10521 1194 # Overriding eigen strong inline speeds up the compiling of 1195 # conv_grad_ops_3d.cc and conv_ops_3d.cc by 20 minutes, 1196 # but this also hurts the performance. Let users decide what they want. 1197 write_to_bazelrc('build --define=override_eigen_strong_inline=true') 1198 1199 1200def config_info_line(name, help_text): 1201 """Helper function to print formatted help text for Bazel config options.""" 1202 print('\t--config=%-12s\t# %s' % (name, help_text)) 1203 1204 1205def configure_ios(): 1206 """Configures TensorFlow for iOS builds. 1207 1208 This function will only be executed if `is_macos()` is true. 1209 """ 1210 if not is_macos(): 1211 return 1212 for filepath in APPLE_BAZEL_FILES: 1213 existing_filepath = os.path.join(_TF_WORKSPACE_ROOT, filepath + '.apple') 1214 renamed_filepath = os.path.join(_TF_WORKSPACE_ROOT, filepath) 1215 symlink_force(existing_filepath, renamed_filepath) 1216 for filepath in IOS_FILES: 1217 filename = os.path.basename(filepath) 1218 new_filepath = os.path.join(_TF_WORKSPACE_ROOT, filename) 1219 symlink_force(filepath, new_filepath) 1220 1221 1222def validate_cuda_config(environ_cp): 1223 """Run find_cuda_config.py and return cuda_toolkit_path, or None.""" 1224 1225 def maybe_encode_env(env): 1226 """Encodes unicode in env to str on Windows python 2.x.""" 1227 if not is_windows() or sys.version_info[0] != 2: 1228 return env 1229 for k, v in env.items(): 1230 if isinstance(k, unicode): 1231 k = k.encode('ascii') 1232 if isinstance(v, unicode): 1233 v = v.encode('ascii') 1234 env[k] = v 1235 return env 1236 1237 cuda_libraries = ['cuda', 'cudnn'] 1238 if is_linux(): 1239 if int(environ_cp.get('TF_NEED_TENSORRT', False)): 1240 cuda_libraries.append('tensorrt') 1241 if environ_cp.get('TF_NCCL_VERSION', None): 1242 cuda_libraries.append('nccl') 1243 1244 paths = glob.glob('**/third_party/gpus/find_cuda_config.py', recursive=True) 1245 if not paths: 1246 raise FileNotFoundError( 1247 "Can't find 'find_cuda_config.py' script inside working directory") 1248 proc = subprocess.Popen( 1249 [environ_cp['PYTHON_BIN_PATH'], paths[0]] + cuda_libraries, 1250 stdout=subprocess.PIPE, 1251 env=maybe_encode_env(environ_cp)) 1252 1253 if proc.wait(): 1254 # Errors from find_cuda_config.py were sent to stderr. 1255 print('Asking for detailed CUDA configuration...\n') 1256 return False 1257 1258 config = dict( 1259 tuple(line.decode('ascii').rstrip().split(': ')) for line in proc.stdout) 1260 1261 print('Found CUDA %s in:' % config['cuda_version']) 1262 print(' %s' % config['cuda_library_dir']) 1263 print(' %s' % config['cuda_include_dir']) 1264 1265 print('Found cuDNN %s in:' % config['cudnn_version']) 1266 print(' %s' % config['cudnn_library_dir']) 1267 print(' %s' % config['cudnn_include_dir']) 1268 1269 if 'tensorrt_version' in config: 1270 print('Found TensorRT %s in:' % config['tensorrt_version']) 1271 print(' %s' % config['tensorrt_library_dir']) 1272 print(' %s' % config['tensorrt_include_dir']) 1273 1274 if config.get('nccl_version', None): 1275 print('Found NCCL %s in:' % config['nccl_version']) 1276 print(' %s' % config['nccl_library_dir']) 1277 print(' %s' % config['nccl_include_dir']) 1278 1279 print('\n') 1280 1281 environ_cp['CUDA_TOOLKIT_PATH'] = config['cuda_toolkit_path'] 1282 return True 1283 1284 1285def main(): 1286 global _TF_WORKSPACE_ROOT 1287 global _TF_BAZELRC 1288 global _TF_CURRENT_BAZEL_VERSION 1289 1290 parser = argparse.ArgumentParser() 1291 parser.add_argument( 1292 '--workspace', 1293 type=str, 1294 default=os.path.abspath(os.path.dirname(__file__)), 1295 help='The absolute path to your active Bazel workspace.') 1296 args = parser.parse_args() 1297 1298 _TF_WORKSPACE_ROOT = args.workspace 1299 _TF_BAZELRC = os.path.join(_TF_WORKSPACE_ROOT, _TF_BAZELRC_FILENAME) 1300 1301 # Make a copy of os.environ to be clear when functions and getting and setting 1302 # environment variables. 1303 environ_cp = dict(os.environ) 1304 1305 try: 1306 current_bazel_version = check_bazel_version(_TF_MIN_BAZEL_VERSION, 1307 _TF_MAX_BAZEL_VERSION) 1308 except subprocess.CalledProcessError as e: 1309 print('Error checking bazel version: ', e.output.decode('UTF-8').strip()) 1310 raise e 1311 1312 _TF_CURRENT_BAZEL_VERSION = convert_version_to_int(current_bazel_version) 1313 1314 reset_tf_configure_bazelrc() 1315 1316 cleanup_makefile() 1317 setup_python(environ_cp) 1318 1319 if is_windows(): 1320 environ_cp['TF_NEED_OPENCL'] = '0' 1321 environ_cp['TF_CUDA_CLANG'] = '0' 1322 environ_cp['TF_NEED_TENSORRT'] = '0' 1323 # TODO(ibiryukov): Investigate using clang as a cpu or cuda compiler on 1324 # Windows. 1325 environ_cp['TF_DOWNLOAD_CLANG'] = '0' 1326 environ_cp['TF_NEED_MPI'] = '0' 1327 1328 if is_macos(): 1329 environ_cp['TF_NEED_TENSORRT'] = '0' 1330 else: 1331 environ_cp['TF_CONFIGURE_IOS'] = '0' 1332 1333 if environ_cp.get('TF_ENABLE_XLA', '1') == '1': 1334 write_to_bazelrc('build --config=xla') 1335 1336 set_action_env_var( 1337 environ_cp, 'TF_NEED_ROCM', 'ROCm', False, bazel_config_name='rocm') 1338 if (environ_cp.get('TF_NEED_ROCM') == '1' and 1339 'LD_LIBRARY_PATH' in environ_cp and 1340 environ_cp.get('LD_LIBRARY_PATH') != '1'): 1341 write_action_env_to_bazelrc('LD_LIBRARY_PATH', 1342 environ_cp.get('LD_LIBRARY_PATH')) 1343 1344 if (environ_cp.get('TF_NEED_ROCM') == '1' and environ_cp.get('ROCM_PATH')): 1345 write_action_env_to_bazelrc('ROCM_PATH', environ_cp.get('ROCM_PATH')) 1346 1347 if ((environ_cp.get('TF_NEED_ROCM') == '1') and 1348 (environ_cp.get('TF_ENABLE_MLIR_GENERATED_GPU_KERNELS') == '1')): 1349 write_to_bazelrc( 1350 'build:rocm --define tensorflow_enable_mlir_generated_gpu_kernels=1') 1351 1352 environ_cp['TF_NEED_CUDA'] = str( 1353 int(get_var(environ_cp, 'TF_NEED_CUDA', 'CUDA', False))) 1354 if (environ_cp.get('TF_NEED_CUDA') == '1' and 1355 'TF_CUDA_CONFIG_REPO' not in environ_cp): 1356 1357 set_action_env_var( 1358 environ_cp, 1359 'TF_NEED_TENSORRT', 1360 'TensorRT', 1361 False, 1362 bazel_config_name='tensorrt') 1363 1364 environ_save = dict(environ_cp) 1365 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS): 1366 1367 if validate_cuda_config(environ_cp): 1368 cuda_env_names = [ 1369 'TF_CUDA_VERSION', 1370 'TF_CUBLAS_VERSION', 1371 'TF_CUDNN_VERSION', 1372 'TF_TENSORRT_VERSION', 1373 'TF_NCCL_VERSION', 1374 'TF_CUDA_PATHS', 1375 # Items below are for backwards compatibility when not using 1376 # TF_CUDA_PATHS. 1377 'CUDA_TOOLKIT_PATH', 1378 'CUDNN_INSTALL_PATH', 1379 'NCCL_INSTALL_PATH', 1380 'NCCL_HDR_PATH', 1381 'TENSORRT_INSTALL_PATH' 1382 ] 1383 # Note: set_action_env_var above already writes to bazelrc. 1384 for name in cuda_env_names: 1385 if name in environ_cp: 1386 write_action_env_to_bazelrc(name, environ_cp[name]) 1387 break 1388 1389 # Restore settings changed below if CUDA config could not be validated. 1390 environ_cp = dict(environ_save) 1391 1392 set_tf_cuda_version(environ_cp) 1393 set_tf_cudnn_version(environ_cp) 1394 if is_linux(): 1395 set_tf_tensorrt_version(environ_cp) 1396 set_tf_nccl_version(environ_cp) 1397 1398 set_tf_cuda_paths(environ_cp) 1399 1400 else: 1401 raise UserInputError( 1402 'Invalid CUDA setting were provided %d ' 1403 'times in a row. Assuming to be a scripting mistake.' % 1404 _DEFAULT_PROMPT_ASK_ATTEMPTS) 1405 1406 set_tf_cuda_compute_capabilities(environ_cp) 1407 if 'LD_LIBRARY_PATH' in environ_cp and environ_cp.get( 1408 'LD_LIBRARY_PATH') != '1': 1409 write_action_env_to_bazelrc('LD_LIBRARY_PATH', 1410 environ_cp.get('LD_LIBRARY_PATH')) 1411 1412 set_tf_cuda_clang(environ_cp) 1413 if environ_cp.get('TF_CUDA_CLANG') == '1': 1414 # Ask whether we should download the clang toolchain. 1415 set_tf_download_clang(environ_cp) 1416 if environ_cp.get('TF_DOWNLOAD_CLANG') != '1': 1417 # Set up which clang we should use as the cuda / host compiler. 1418 set_clang_cuda_compiler_path(environ_cp) 1419 else: 1420 # Use downloaded LLD for linking. 1421 write_to_bazelrc('build:cuda_clang --config=download_clang_use_lld') 1422 else: 1423 # Set up which gcc nvcc should use as the host compiler 1424 # No need to set this on Windows 1425 if not is_windows(): 1426 set_gcc_host_compiler_path(environ_cp) 1427 set_other_cuda_vars(environ_cp) 1428 else: 1429 # CUDA not required. Ask whether we should download the clang toolchain and 1430 # use it for the CPU build. 1431 set_tf_download_clang(environ_cp) 1432 1433 # ROCm / CUDA are mutually exclusive. 1434 # At most 1 GPU platform can be configured. 1435 gpu_platform_count = 0 1436 if environ_cp.get('TF_NEED_ROCM') == '1': 1437 gpu_platform_count += 1 1438 if environ_cp.get('TF_NEED_CUDA') == '1': 1439 gpu_platform_count += 1 1440 if gpu_platform_count >= 2: 1441 raise UserInputError('CUDA / ROCm are mututally exclusive. ' 1442 'At most 1 GPU platform can be configured.') 1443 1444 set_cc_opt_flags(environ_cp) 1445 set_system_libs_flag(environ_cp) 1446 if is_windows(): 1447 set_windows_build_flags(environ_cp) 1448 1449 if get_var(environ_cp, 'TF_SET_ANDROID_WORKSPACE', 'android workspace', False, 1450 ('Would you like to interactively configure ./WORKSPACE for ' 1451 'Android builds?'), 'Searching for NDK and SDK installations.', 1452 'Not configuring the WORKSPACE for Android builds.'): 1453 create_android_ndk_rule(environ_cp) 1454 create_android_sdk_rule(environ_cp) 1455 1456 system_specific_test_config(environ_cp) 1457 1458 set_action_env_var(environ_cp, 'TF_CONFIGURE_IOS', 'iOS', False) 1459 if environ_cp.get('TF_CONFIGURE_IOS') == '1': 1460 configure_ios() 1461 1462 print('Preconfigured Bazel build configs. You can use any of the below by ' 1463 'adding "--config=<>" to your build command. See .bazelrc for more ' 1464 'details.') 1465 config_info_line('mkl', 'Build with MKL support.') 1466 config_info_line('mkl_aarch64', 'Build with oneDNN support for Aarch64.') 1467 config_info_line('monolithic', 'Config for mostly static monolithic build.') 1468 config_info_line('numa', 'Build with NUMA support.') 1469 config_info_line( 1470 'dynamic_kernels', 1471 '(Experimental) Build kernels into separate shared objects.') 1472 config_info_line('v2', 'Build TensorFlow 2.x instead of 1.x.') 1473 1474 print('Preconfigured Bazel build configs to DISABLE default on features:') 1475 config_info_line('noaws', 'Disable AWS S3 filesystem support.') 1476 config_info_line('nogcp', 'Disable GCP support.') 1477 config_info_line('nohdfs', 'Disable HDFS support.') 1478 config_info_line('nonccl', 'Disable NVIDIA NCCL support.') 1479 1480 1481if __name__ == '__main__': 1482 main() 1483