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