blob: f72820ab02ee2269f0d108d1b25aff8df23d4bc4 [file] [log] [blame]
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001# 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
Shanqing Cai71445712018-03-12 19:33:52 -070021import argparse
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070022import errno
23import os
24import platform
25import re
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070026import subprocess
27import sys
28
Andrew Sellec9885ea2017-11-06 09:37:03 -080029# pylint: disable=g-import-not-at-top
Jonathan Hseu008910f2017-08-25 14:01:05 -070030try:
31 from shutil import which
32except ImportError:
33 from distutils.spawn import find_executable as which
Andrew Sellec9885ea2017-11-06 09:37:03 -080034# pylint: enable=g-import-not-at-top
Jonathan Hseu008910f2017-08-25 14:01:05 -070035
Dandelion Man?90e42f32017-12-15 18:15:07 -080036_DEFAULT_CUDA_VERSION = '9.0'
37_DEFAULT_CUDNN_VERSION = '7'
Smit Hinsu63e6b9b2018-07-13 12:46:24 -070038_DEFAULT_NCCL_VERSION = '2.2'
Smit Hinsufe7d1d92018-07-14 13:16:58 -070039_DEFAULT_CUDA_COMPUTE_CAPABILITIES = '3.5,7.0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070040_DEFAULT_CUDA_PATH = '/usr/local/cuda'
41_DEFAULT_CUDA_PATH_LINUX = '/opt/cuda'
42_DEFAULT_CUDA_PATH_WIN = ('C:/Program Files/NVIDIA GPU Computing '
43 'Toolkit/CUDA/v%s' % _DEFAULT_CUDA_VERSION)
44_TF_OPENCL_VERSION = '1.2'
45_DEFAULT_COMPUTECPP_TOOLKIT_PATH = '/usr/local/computecpp'
Yifei Fengb1d8c592017-11-22 13:42:21 -080046_DEFAULT_TRISYCL_INCLUDE_DIR = '/usr/local/triSYCL/include'
A. Unique TensorFlowerd340f472018-08-30 14:00:41 -070047_SUPPORTED_ANDROID_NDK_VERSIONS = [10, 11, 12, 13, 14, 15, 16]
Austin Anderson6afface2017-12-05 11:59:17 -080048
49_DEFAULT_PROMPT_ASK_ATTEMPTS = 10
50
Shanqing Cai71445712018-03-12 19:33:52 -070051_TF_BAZELRC_FILENAME = '.tf_configure.bazelrc'
A. Unique TensorFlower7c5eb352018-10-01 07:15:23 -070052_TF_WORKSPACE_ROOT = ''
53_TF_BAZELRC = ''
Shanqing Cai71445712018-03-12 19:33:52 -070054
Jason Furmanek7c234152018-09-26 04:44:12 +000055NCCL_LIB_PATHS = [
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -070056 'lib64/', 'lib/powerpc64le-linux-gnu/', 'lib/x86_64-linux-gnu/', ''
Jason Furmanek7c234152018-09-26 04:44:12 +000057]
Austin Anderson6afface2017-12-05 11:59:17 -080058
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -070059if platform.machine() == 'ppc64le':
60 _DEFAULT_TENSORRT_PATH_LINUX = '/usr/lib/powerpc64le-linux-gnu/'
61else:
62 _DEFAULT_TENSORRT_PATH_LINUX = '/usr/lib/%s-linux-gnu' % platform.machine()
63
Austin Anderson6afface2017-12-05 11:59:17 -080064
65class UserInputError(Exception):
66 pass
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070067
68
69def is_windows():
70 return platform.system() == 'Windows'
71
72
73def is_linux():
74 return platform.system() == 'Linux'
75
76
77def is_macos():
78 return platform.system() == 'Darwin'
79
80
81def is_ppc64le():
82 return platform.machine() == 'ppc64le'
83
84
Jonathan Hseu008910f2017-08-25 14:01:05 -070085def is_cygwin():
86 return platform.system().startswith('CYGWIN_NT')
87
88
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070089def get_input(question):
90 try:
91 try:
92 answer = raw_input(question)
93 except NameError:
94 answer = input(question) # pylint: disable=bad-builtin
95 except EOFError:
96 answer = ''
97 return answer
98
99
100def symlink_force(target, link_name):
101 """Force symlink, equivalent of 'ln -sf'.
102
103 Args:
104 target: items to link to.
105 link_name: name of the link.
106 """
107 try:
108 os.symlink(target, link_name)
109 except OSError as e:
110 if e.errno == errno.EEXIST:
111 os.remove(link_name)
112 os.symlink(target, link_name)
113 else:
114 raise e
115
116
117def sed_in_place(filename, old, new):
118 """Replace old string with new string in file.
119
120 Args:
121 filename: string for filename.
122 old: string to replace.
123 new: new string to replace to.
124 """
125 with open(filename, 'r') as f:
126 filedata = f.read()
127 newdata = filedata.replace(old, new)
128 with open(filename, 'w') as f:
129 f.write(newdata)
130
131
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700132def write_to_bazelrc(line):
133 with open(_TF_BAZELRC, 'a') as f:
134 f.write(line + '\n')
135
136
137def write_action_env_to_bazelrc(var_name, var):
138 write_to_bazelrc('build --action_env %s="%s"' % (var_name, str(var)))
139
140
Jonathan Hseu008910f2017-08-25 14:01:05 -0700141def run_shell(cmd, allow_non_zero=False):
142 if allow_non_zero:
143 try:
144 output = subprocess.check_output(cmd)
145 except subprocess.CalledProcessError as e:
146 output = e.output
147 else:
148 output = subprocess.check_output(cmd)
149 return output.decode('UTF-8').strip()
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700150
151
152def cygpath(path):
153 """Convert path from posix to windows."""
Martin Wicked57572e2017-09-02 19:21:45 -0700154 return os.path.abspath(path).replace('\\', '/')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700155
156
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700157def get_python_path(environ_cp, python_bin_path):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700158 """Get the python site package paths."""
159 python_paths = []
160 if environ_cp.get('PYTHONPATH'):
161 python_paths = environ_cp.get('PYTHONPATH').split(':')
162 try:
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700163 library_paths = run_shell([
164 python_bin_path, '-c',
165 'import site; print("\\n".join(site.getsitepackages()))'
166 ]).split('\n')
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700167 except subprocess.CalledProcessError:
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700168 library_paths = [
169 run_shell([
170 python_bin_path, '-c',
171 'from distutils.sysconfig import get_python_lib;'
172 'print(get_python_lib())'
173 ])
174 ]
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700175
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700176 all_paths = set(python_paths + library_paths)
177
178 paths = []
179 for path in all_paths:
180 if os.path.isdir(path):
181 paths.append(path)
182 return paths
183
184
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700185def get_python_major_version(python_bin_path):
186 """Get the python major version."""
Jonathan Hseu008910f2017-08-25 14:01:05 -0700187 return run_shell([python_bin_path, '-c', 'import sys; print(sys.version[0])'])
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700188
189
Gunhan Gulsoyed89a2b2017-09-19 18:36:26 -0700190def setup_python(environ_cp):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700191 """Setup python related env variables."""
192 # Get PYTHON_BIN_PATH, default is the current running python.
193 default_python_bin_path = sys.executable
194 ask_python_bin_path = ('Please specify the location of python. [Default is '
195 '%s]: ') % default_python_bin_path
196 while True:
197 python_bin_path = get_from_env_or_user_or_default(
198 environ_cp, 'PYTHON_BIN_PATH', ask_python_bin_path,
199 default_python_bin_path)
200 # Check if the path is valid
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700201 if os.path.isfile(python_bin_path) and os.access(python_bin_path, os.X_OK):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700202 break
203 elif not os.path.exists(python_bin_path):
204 print('Invalid python path: %s cannot be found.' % python_bin_path)
205 else:
206 print('%s is not executable. Is it the python binary?' % python_bin_path)
207 environ_cp['PYTHON_BIN_PATH'] = ''
208
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700209 # Convert python path to Windows style before checking lib and version
Martin Wicked57572e2017-09-02 19:21:45 -0700210 if is_windows() or is_cygwin():
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700211 python_bin_path = cygpath(python_bin_path)
212
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700213 # Get PYTHON_LIB_PATH
214 python_lib_path = environ_cp.get('PYTHON_LIB_PATH')
215 if not python_lib_path:
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700216 python_lib_paths = get_python_path(environ_cp, python_bin_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700217 if environ_cp.get('USE_DEFAULT_PYTHON_LIB_PATH') == '1':
Vijay Vasudevana1fba7f2017-07-28 10:58:56 -0700218 python_lib_path = python_lib_paths[0]
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700219 else:
Jonathan Hseu008910f2017-08-25 14:01:05 -0700220 print('Found possible Python library paths:\n %s' %
221 '\n '.join(python_lib_paths))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700222 default_python_lib_path = python_lib_paths[0]
223 python_lib_path = get_input(
Jonathan Hseu008910f2017-08-25 14:01:05 -0700224 'Please input the desired Python library path to use. '
225 'Default is [%s]\n' % python_lib_paths[0])
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700226 if not python_lib_path:
227 python_lib_path = default_python_lib_path
Vijay Vasudevana1fba7f2017-07-28 10:58:56 -0700228 environ_cp['PYTHON_LIB_PATH'] = python_lib_path
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700229
TensorFlower Gardener61a87202018-10-01 12:25:39 -0700230 _ = get_python_major_version(python_bin_path)
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700231
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700232 # Convert python path to Windows style before writing into bazel.rc
Martin Wicked57572e2017-09-02 19:21:45 -0700233 if is_windows() or is_cygwin():
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700234 python_lib_path = cygpath(python_lib_path)
235
236 # Set-up env variables used by python_configure.bzl
237 write_action_env_to_bazelrc('PYTHON_BIN_PATH', python_bin_path)
238 write_action_env_to_bazelrc('PYTHON_LIB_PATH', python_lib_path)
Gunhan Gulsoyed89a2b2017-09-19 18:36:26 -0700239 write_to_bazelrc('build --python_path=\"%s"' % python_bin_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700240 environ_cp['PYTHON_BIN_PATH'] = python_bin_path
241
242 # Write tools/python_bin_path.sh
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700243 with open(
244 os.path.join(_TF_WORKSPACE_ROOT, 'tools', 'python_bin_path.sh'),
245 'w') as f:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700246 f.write('export PYTHON_BIN_PATH="%s"' % python_bin_path)
247
248
A. Unique TensorFlower7c5eb352018-10-01 07:15:23 -0700249def reset_tf_configure_bazelrc():
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700250 """Reset file that contains customized config settings."""
251 open(_TF_BAZELRC, 'w').close()
A. Unique TensorFlower7c5eb352018-10-01 07:15:23 -0700252 bazelrc_path = os.path.join(_TF_WORKSPACE_ROOT, '.bazelrc')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700253
Shanqing Cai71445712018-03-12 19:33:52 -0700254 data = []
255 if os.path.exists(bazelrc_path):
256 with open(bazelrc_path, 'r') as f:
257 data = f.read().splitlines()
258 with open(bazelrc_path, 'w') as f:
259 for l in data:
260 if _TF_BAZELRC_FILENAME in l:
261 continue
262 f.write('%s\n' % l)
Jason Zamand3f6b722018-08-04 14:28:02 +0800263 f.write('import %%workspace%%/%s\n' % _TF_BAZELRC_FILENAME)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700264
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700265def cleanup_makefile():
266 """Delete any leftover BUILD files from the Makefile build.
267
268 These files could interfere with Bazel parsing.
269 """
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700270 makefile_download_dir = os.path.join(_TF_WORKSPACE_ROOT, 'tensorflow',
271 'contrib', 'makefile', 'downloads')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700272 if os.path.isdir(makefile_download_dir):
273 for root, _, filenames in os.walk(makefile_download_dir):
274 for f in filenames:
275 if f.endswith('BUILD'):
276 os.remove(os.path.join(root, f))
277
278
279def get_var(environ_cp,
280 var_name,
281 query_item,
282 enabled_by_default,
283 question=None,
284 yes_reply=None,
285 no_reply=None):
286 """Get boolean input from user.
287
288 If var_name is not set in env, ask user to enable query_item or not. If the
289 response is empty, use the default.
290
291 Args:
292 environ_cp: copy of the os.environ.
293 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
294 query_item: string for feature related to the variable, e.g. "Hadoop File
295 System".
296 enabled_by_default: boolean for default behavior.
297 question: optional string for how to ask for user input.
Michael Cased90054e2018-02-07 14:36:00 -0800298 yes_reply: optional string for reply when feature is enabled.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700299 no_reply: optional string for reply when feature is disabled.
300
301 Returns:
302 boolean value of the variable.
Frank Chenc4ef9272018-01-10 11:36:52 -0800303
304 Raises:
305 UserInputError: if an environment variable is set, but it cannot be
306 interpreted as a boolean indicator, assume that the user has made a
307 scripting error, and will continue to provide invalid input.
308 Raise the error to avoid infinitely looping.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700309 """
310 if not question:
311 question = 'Do you wish to build TensorFlow with %s support?' % query_item
312 if not yes_reply:
313 yes_reply = '%s support will be enabled for TensorFlow.' % query_item
314 if not no_reply:
315 no_reply = 'No %s' % yes_reply
316
317 yes_reply += '\n'
318 no_reply += '\n'
319
320 if enabled_by_default:
321 question += ' [Y/n]: '
322 else:
323 question += ' [y/N]: '
324
325 var = environ_cp.get(var_name)
Frank Chenc4ef9272018-01-10 11:36:52 -0800326 if var is not None:
327 var_content = var.strip().lower()
328 true_strings = ('1', 't', 'true', 'y', 'yes')
329 false_strings = ('0', 'f', 'false', 'n', 'no')
330 if var_content in true_strings:
331 var = True
332 elif var_content in false_strings:
333 var = False
334 else:
335 raise UserInputError(
336 'Environment variable %s must be set as a boolean indicator.\n'
337 'The following are accepted as TRUE : %s.\n'
338 'The following are accepted as FALSE: %s.\n'
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700339 'Current value is %s.' % (var_name, ', '.join(true_strings),
340 ', '.join(false_strings), var))
Frank Chenc4ef9272018-01-10 11:36:52 -0800341
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700342 while var is None:
343 user_input_origin = get_input(question)
344 user_input = user_input_origin.strip().lower()
345 if user_input == 'y':
346 print(yes_reply)
347 var = True
348 elif user_input == 'n':
349 print(no_reply)
350 var = False
351 elif not user_input:
352 if enabled_by_default:
353 print(yes_reply)
354 var = True
355 else:
356 print(no_reply)
357 var = False
358 else:
359 print('Invalid selection: %s' % user_input_origin)
360 return var
361
362
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700363def set_build_var(environ_cp,
364 var_name,
365 query_item,
366 option_name,
367 enabled_by_default,
368 bazel_config_name=None):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700369 """Set if query_item will be enabled for the build.
370
371 Ask user if query_item will be enabled. Default is used if no input is given.
372 Set subprocess environment variable and write to .bazelrc if enabled.
373
374 Args:
375 environ_cp: copy of the os.environ.
376 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
377 query_item: string for feature related to the variable, e.g. "Hadoop File
378 System".
379 option_name: string for option to define in .bazelrc.
380 enabled_by_default: boolean for default behavior.
Michael Case98850a52017-09-14 13:35:57 -0700381 bazel_config_name: Name for Bazel --config argument to enable build feature.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700382 """
383
384 var = str(int(get_var(environ_cp, var_name, query_item, enabled_by_default)))
385 environ_cp[var_name] = var
386 if var == '1':
387 write_to_bazelrc('build --define %s=true' % option_name)
Michael Case98850a52017-09-14 13:35:57 -0700388 elif bazel_config_name is not None:
389 # TODO(mikecase): Migrate all users of configure.py to use --config Bazel
390 # options and not to set build configs through environment variables.
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700391 write_to_bazelrc(
392 'build:%s --define %s=true' % (bazel_config_name, option_name))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700393
394
395def set_action_env_var(environ_cp,
396 var_name,
397 query_item,
398 enabled_by_default,
399 question=None,
400 yes_reply=None,
401 no_reply=None):
402 """Set boolean action_env variable.
403
404 Ask user if query_item will be enabled. Default is used if no input is given.
405 Set environment variable and write to .bazelrc.
406
407 Args:
408 environ_cp: copy of the os.environ.
409 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
410 query_item: string for feature related to the variable, e.g. "Hadoop File
411 System".
412 enabled_by_default: boolean for default behavior.
413 question: optional string for how to ask for user input.
Michael Cased90054e2018-02-07 14:36:00 -0800414 yes_reply: optional string for reply when feature is enabled.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700415 no_reply: optional string for reply when feature is disabled.
416 """
417 var = int(
418 get_var(environ_cp, var_name, query_item, enabled_by_default, question,
419 yes_reply, no_reply))
420
421 write_action_env_to_bazelrc(var_name, var)
422 environ_cp[var_name] = str(var)
423
424
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700425def convert_version_to_int(version):
426 """Convert a version number to a integer that can be used to compare.
427
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700428 Version strings of the form X.YZ and X.Y.Z-xxxxx are supported. The
429 'xxxxx' part, for instance 'homebrew' on OS/X, is ignored.
430
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700431 Args:
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700432 version: a version to be converted
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700433
434 Returns:
435 An integer if converted successfully, otherwise return None.
436 """
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700437 version = version.split('-')[0]
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700438 version_segments = version.split('.')
439 for seg in version_segments:
440 if not seg.isdigit():
441 return None
442
443 version_str = ''.join(['%03d' % int(seg) for seg in version_segments])
444 return int(version_str)
445
446
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700447def check_bazel_version(min_version):
Yifei Fengdce9a492018-02-22 14:24:57 -0800448 """Check installed bazel version is at least min_version.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700449
450 Args:
451 min_version: string for minimum bazel version.
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700452
453 Returns:
454 The bazel version detected.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700455 """
Jonathan Hseu008910f2017-08-25 14:01:05 -0700456 if which('bazel') is None:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700457 print('Cannot find bazel. Please install bazel.')
458 sys.exit(0)
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700459 curr_version = run_shell(
460 ['bazel', '--batch', '--bazelrc=/dev/null', 'version'])
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700461
462 for line in curr_version.split('\n'):
463 if 'Build label: ' in line:
464 curr_version = line.split('Build label: ')[1]
465 break
466
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700467 min_version_int = convert_version_to_int(min_version)
468 curr_version_int = convert_version_to_int(curr_version)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700469
470 # Check if current bazel version can be detected properly.
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700471 if not curr_version_int:
472 print('WARNING: current bazel installation is not a release version.')
473 print('Make sure you are running at least bazel %s' % min_version)
474 return curr_version
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700475
Michael Cased94271a2017-08-22 17:26:52 -0700476 print('You have bazel %s installed.' % curr_version)
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700477
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700478 if curr_version_int < min_version_int:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700479 print('Please upgrade your bazel installation to version %s or higher to '
480 'build TensorFlow!' % min_version)
481 sys.exit(0)
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700482 return curr_version
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700483
484
485def set_cc_opt_flags(environ_cp):
486 """Set up architecture-dependent optimization flags.
487
488 Also append CC optimization flags to bazel.rc..
489
490 Args:
491 environ_cp: copy of the os.environ.
492 """
493 if is_ppc64le():
494 # gcc on ppc64le does not support -march, use mcpu instead
495 default_cc_opt_flags = '-mcpu=native'
A. Unique TensorFlower1bba94a2018-04-04 15:45:20 -0700496 elif is_windows():
497 default_cc_opt_flags = '/arch:AVX'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700498 else:
499 default_cc_opt_flags = '-march=native'
500 question = ('Please specify optimization flags to use during compilation when'
501 ' bazel option "--config=opt" is specified [Default is %s]: '
502 ) % default_cc_opt_flags
503 cc_opt_flags = get_from_env_or_user_or_default(environ_cp, 'CC_OPT_FLAGS',
504 question, default_cc_opt_flags)
505 for opt in cc_opt_flags.split():
Michael Case00177422017-11-10 13:14:03 -0800506 write_to_bazelrc('build:opt --copt=%s' % opt)
507 # It should be safe on the same build host.
A. Unique TensorFlower1bba94a2018-04-04 15:45:20 -0700508 if not is_ppc64le() and not is_windows():
Shanqing Cai71445712018-03-12 19:33:52 -0700509 write_to_bazelrc('build:opt --host_copt=-march=native')
Michael Casebb3355d2017-11-09 08:46:31 -0800510 write_to_bazelrc('build:opt --define with_default_optimizations=true')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700511
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700512
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700513def set_tf_cuda_clang(environ_cp):
514 """set TF_CUDA_CLANG action_env.
515
516 Args:
517 environ_cp: copy of the os.environ.
518 """
519 question = 'Do you want to use clang as CUDA compiler?'
520 yes_reply = 'Clang will be used as CUDA compiler.'
521 no_reply = 'nvcc will be used as CUDA compiler.'
522 set_action_env_var(
523 environ_cp,
524 'TF_CUDA_CLANG',
525 None,
526 False,
527 question=question,
528 yes_reply=yes_reply,
529 no_reply=no_reply)
530
531
A. Unique TensorFlowerfd29e952017-12-22 03:07:51 -0800532def set_tf_download_clang(environ_cp):
533 """Set TF_DOWNLOAD_CLANG action_env."""
Ilya Biryukov9e651e42018-03-22 05:33:42 -0700534 question = 'Do you wish to download a fresh release of clang? (Experimental)'
A. Unique TensorFlowerfd29e952017-12-22 03:07:51 -0800535 yes_reply = 'Clang will be downloaded and used to compile tensorflow.'
536 no_reply = 'Clang will not be downloaded.'
537 set_action_env_var(
538 environ_cp,
539 'TF_DOWNLOAD_CLANG',
540 None,
541 False,
542 question=question,
543 yes_reply=yes_reply,
544 no_reply=no_reply)
545
546
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700547def get_from_env_or_user_or_default(environ_cp, var_name, ask_for_var,
548 var_default):
549 """Get var_name either from env, or user or default.
550
551 If var_name has been set as environment variable, use the preset value, else
552 ask for user input. If no input is provided, the default is used.
553
554 Args:
555 environ_cp: copy of the os.environ.
556 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
557 ask_for_var: string for how to ask for user input.
558 var_default: default value string.
559
560 Returns:
561 string value for var_name
562 """
563 var = environ_cp.get(var_name)
564 if not var:
565 var = get_input(ask_for_var)
Michael Cased94271a2017-08-22 17:26:52 -0700566 print('\n')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700567 if not var:
568 var = var_default
569 return var
570
571
572def set_clang_cuda_compiler_path(environ_cp):
573 """Set CLANG_CUDA_COMPILER_PATH."""
Jonathan Hseu008910f2017-08-25 14:01:05 -0700574 default_clang_path = which('clang') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700575 ask_clang_path = ('Please specify which clang should be used as device and '
576 'host compiler. [Default is %s]: ') % default_clang_path
577
578 while True:
579 clang_cuda_compiler_path = get_from_env_or_user_or_default(
580 environ_cp, 'CLANG_CUDA_COMPILER_PATH', ask_clang_path,
581 default_clang_path)
582 if os.path.exists(clang_cuda_compiler_path):
583 break
584
585 # Reset and retry
586 print('Invalid clang path: %s cannot be found.' % clang_cuda_compiler_path)
587 environ_cp['CLANG_CUDA_COMPILER_PATH'] = ''
588
589 # Set CLANG_CUDA_COMPILER_PATH
590 environ_cp['CLANG_CUDA_COMPILER_PATH'] = clang_cuda_compiler_path
591 write_action_env_to_bazelrc('CLANG_CUDA_COMPILER_PATH',
592 clang_cuda_compiler_path)
593
594
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700595def prompt_loop_or_load_from_env(environ_cp,
596 var_name,
597 var_default,
598 ask_for_var,
599 check_success,
600 error_msg,
601 suppress_default_error=False,
602 n_ask_attempts=_DEFAULT_PROMPT_ASK_ATTEMPTS):
Austin Anderson6afface2017-12-05 11:59:17 -0800603 """Loop over user prompts for an ENV param until receiving a valid response.
604
605 For the env param var_name, read from the environment or verify user input
606 until receiving valid input. When done, set var_name in the environ_cp to its
607 new value.
608
609 Args:
610 environ_cp: (Dict) copy of the os.environ.
611 var_name: (String) string for name of environment variable, e.g. "TF_MYVAR".
612 var_default: (String) default value string.
613 ask_for_var: (String) string for how to ask for user input.
614 check_success: (Function) function that takes one argument and returns a
615 boolean. Should return True if the value provided is considered valid. May
616 contain a complex error message if error_msg does not provide enough
617 information. In that case, set suppress_default_error to True.
618 error_msg: (String) String with one and only one '%s'. Formatted with each
619 invalid response upon check_success(input) failure.
620 suppress_default_error: (Bool) Suppress the above error message in favor of
621 one from the check_success function.
622 n_ask_attempts: (Integer) Number of times to query for valid input before
623 raising an error and quitting.
624
625 Returns:
626 [String] The value of var_name after querying for input.
627
628 Raises:
629 UserInputError: if a query has been attempted n_ask_attempts times without
Frank Chenc4ef9272018-01-10 11:36:52 -0800630 success, assume that the user has made a scripting error, and will
631 continue to provide invalid input. Raise the error to avoid infinitely
632 looping.
Austin Anderson6afface2017-12-05 11:59:17 -0800633 """
634 default = environ_cp.get(var_name) or var_default
635 full_query = '%s [Default is %s]: ' % (
636 ask_for_var,
637 default,
638 )
639
640 for _ in range(n_ask_attempts):
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700641 val = get_from_env_or_user_or_default(environ_cp, var_name, full_query,
Austin Anderson6afface2017-12-05 11:59:17 -0800642 default)
643 if check_success(val):
644 break
645 if not suppress_default_error:
646 print(error_msg % val)
647 environ_cp[var_name] = ''
648 else:
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700649 raise UserInputError(
650 'Invalid %s setting was provided %d times in a row. '
651 'Assuming to be a scripting mistake.' % (var_name, n_ask_attempts))
Austin Anderson6afface2017-12-05 11:59:17 -0800652
653 environ_cp[var_name] = val
654 return val
655
656
657def create_android_ndk_rule(environ_cp):
658 """Set ANDROID_NDK_HOME and write Android NDK WORKSPACE rule."""
659 if is_windows() or is_cygwin():
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700660 default_ndk_path = cygpath(
661 '%s/Android/Sdk/ndk-bundle' % environ_cp['APPDATA'])
Austin Anderson6afface2017-12-05 11:59:17 -0800662 elif is_macos():
663 default_ndk_path = '%s/library/Android/Sdk/ndk-bundle' % environ_cp['HOME']
664 else:
665 default_ndk_path = '%s/Android/Sdk/ndk-bundle' % environ_cp['HOME']
666
667 def valid_ndk_path(path):
668 return (os.path.exists(path) and
669 os.path.exists(os.path.join(path, 'source.properties')))
670
671 android_ndk_home_path = prompt_loop_or_load_from_env(
672 environ_cp,
673 var_name='ANDROID_NDK_HOME',
674 var_default=default_ndk_path,
675 ask_for_var='Please specify the home path of the Android NDK to use.',
676 check_success=valid_ndk_path,
677 error_msg=('The path %s or its child file "source.properties" '
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700678 'does not exist.'))
Michael Case51053502018-06-05 17:47:19 -0700679 write_action_env_to_bazelrc('ANDROID_NDK_HOME', android_ndk_home_path)
680 write_action_env_to_bazelrc('ANDROID_NDK_API_LEVEL',
681 check_ndk_level(android_ndk_home_path))
Austin Anderson6afface2017-12-05 11:59:17 -0800682
683
684def create_android_sdk_rule(environ_cp):
685 """Set Android variables and write Android SDK WORKSPACE rule."""
686 if is_windows() or is_cygwin():
687 default_sdk_path = cygpath('%s/Android/Sdk' % environ_cp['APPDATA'])
688 elif is_macos():
Shashi Shekharc0ff0cc2018-07-17 09:00:24 -0700689 default_sdk_path = '%s/library/Android/Sdk' % environ_cp['HOME']
Austin Anderson6afface2017-12-05 11:59:17 -0800690 else:
691 default_sdk_path = '%s/Android/Sdk' % environ_cp['HOME']
692
693 def valid_sdk_path(path):
694 return (os.path.exists(path) and
695 os.path.exists(os.path.join(path, 'platforms')) and
696 os.path.exists(os.path.join(path, 'build-tools')))
697
698 android_sdk_home_path = prompt_loop_or_load_from_env(
699 environ_cp,
700 var_name='ANDROID_SDK_HOME',
701 var_default=default_sdk_path,
702 ask_for_var='Please specify the home path of the Android SDK to use.',
703 check_success=valid_sdk_path,
704 error_msg=('Either %s does not exist, or it does not contain the '
705 'subdirectories "platforms" and "build-tools".'))
706
707 platforms = os.path.join(android_sdk_home_path, 'platforms')
708 api_levels = sorted(os.listdir(platforms))
709 api_levels = [x.replace('android-', '') for x in api_levels]
710
711 def valid_api_level(api_level):
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700712 return os.path.exists(
713 os.path.join(android_sdk_home_path, 'platforms',
714 'android-' + api_level))
Austin Anderson6afface2017-12-05 11:59:17 -0800715
716 android_api_level = prompt_loop_or_load_from_env(
717 environ_cp,
718 var_name='ANDROID_API_LEVEL',
719 var_default=api_levels[-1],
720 ask_for_var=('Please specify the Android SDK API level to use. '
721 '[Available levels: %s]') % api_levels,
722 check_success=valid_api_level,
723 error_msg='Android-%s is not present in the SDK path.')
724
725 build_tools = os.path.join(android_sdk_home_path, 'build-tools')
726 versions = sorted(os.listdir(build_tools))
727
728 def valid_build_tools(version):
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700729 return os.path.exists(
730 os.path.join(android_sdk_home_path, 'build-tools', version))
Austin Anderson6afface2017-12-05 11:59:17 -0800731
732 android_build_tools_version = prompt_loop_or_load_from_env(
733 environ_cp,
734 var_name='ANDROID_BUILD_TOOLS_VERSION',
735 var_default=versions[-1],
736 ask_for_var=('Please specify an Android build tools version to use. '
737 '[Available versions: %s]') % versions,
738 check_success=valid_build_tools,
739 error_msg=('The selected SDK does not have build-tools version %s '
740 'available.'))
741
Michael Case51053502018-06-05 17:47:19 -0700742 write_action_env_to_bazelrc('ANDROID_BUILD_TOOLS_VERSION',
743 android_build_tools_version)
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700744 write_action_env_to_bazelrc('ANDROID_SDK_API_LEVEL', android_api_level)
745 write_action_env_to_bazelrc('ANDROID_SDK_HOME', android_sdk_home_path)
Austin Anderson6afface2017-12-05 11:59:17 -0800746
747
748def check_ndk_level(android_ndk_home_path):
749 """Check the revision number of an Android NDK path."""
750 properties_path = '%s/source.properties' % android_ndk_home_path
751 if is_windows() or is_cygwin():
752 properties_path = cygpath(properties_path)
753 with open(properties_path, 'r') as f:
754 filedata = f.read()
755
756 revision = re.search(r'Pkg.Revision = (\d+)', filedata)
757 if revision:
Michael Case51053502018-06-05 17:47:19 -0700758 ndk_api_level = revision.group(1)
759 else:
760 raise Exception('Unable to parse NDK revision.')
761 if int(ndk_api_level) not in _SUPPORTED_ANDROID_NDK_VERSIONS:
762 print('WARNING: The API level of the NDK in %s is %s, which is not '
763 'supported by Bazel (officially supported versions: %s). Please use '
764 'another version. Compiling Android targets may result in confusing '
765 'errors.\n' % (android_ndk_home_path, ndk_api_level,
766 _SUPPORTED_ANDROID_NDK_VERSIONS))
767 return ndk_api_level
Austin Anderson6afface2017-12-05 11:59:17 -0800768
769
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700770def set_gcc_host_compiler_path(environ_cp):
771 """Set GCC_HOST_COMPILER_PATH."""
Jonathan Hseu008910f2017-08-25 14:01:05 -0700772 default_gcc_host_compiler_path = which('gcc') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700773 cuda_bin_symlink = '%s/bin/gcc' % environ_cp.get('CUDA_TOOLKIT_PATH')
774
775 if os.path.islink(cuda_bin_symlink):
776 # os.readlink is only available in linux
Jonathan Hseu008910f2017-08-25 14:01:05 -0700777 default_gcc_host_compiler_path = os.path.realpath(cuda_bin_symlink)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700778
Austin Anderson6afface2017-12-05 11:59:17 -0800779 gcc_host_compiler_path = prompt_loop_or_load_from_env(
780 environ_cp,
781 var_name='GCC_HOST_COMPILER_PATH',
782 var_default=default_gcc_host_compiler_path,
783 ask_for_var=
784 'Please specify which gcc should be used by nvcc as the host compiler.',
785 check_success=os.path.exists,
786 error_msg='Invalid gcc path. %s cannot be found.',
787 )
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700788
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700789 write_action_env_to_bazelrc('GCC_HOST_COMPILER_PATH', gcc_host_compiler_path)
790
791
Ankur Taly0e6f39d2018-02-16 18:22:55 -0800792def reformat_version_sequence(version_str, sequence_count):
793 """Reformat the version string to have the given number of sequences.
794
795 For example:
796 Given (7, 2) -> 7.0
797 (7.0.1, 2) -> 7.0
798 (5, 1) -> 5
799 (5.0.3.2, 1) -> 5
800
801 Args:
802 version_str: String, the version string.
803 sequence_count: int, an integer.
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700804
Ankur Taly0e6f39d2018-02-16 18:22:55 -0800805 Returns:
806 string, reformatted version string.
807 """
808 v = version_str.split('.')
809 if len(v) < sequence_count:
810 v = v + (['0'] * (sequence_count - len(v)))
811
812 return '.'.join(v[:sequence_count])
813
814
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700815def set_tf_cuda_version(environ_cp):
816 """Set CUDA_TOOLKIT_PATH and TF_CUDA_VERSION."""
817 ask_cuda_version = (
A. Unique TensorFlowerb15500b2018-05-08 12:04:38 -0700818 'Please specify the CUDA SDK version you want to use. '
819 '[Leave empty to default to CUDA %s]: ') % _DEFAULT_CUDA_VERSION
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700820
Austin Andersonf9a88f82017-12-13 11:49:40 -0800821 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700822 # Configure the Cuda SDK version to use.
823 tf_cuda_version = get_from_env_or_user_or_default(
824 environ_cp, 'TF_CUDA_VERSION', ask_cuda_version, _DEFAULT_CUDA_VERSION)
Ankur Taly0e6f39d2018-02-16 18:22:55 -0800825 tf_cuda_version = reformat_version_sequence(str(tf_cuda_version), 2)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700826
827 # Find out where the CUDA toolkit is installed
828 default_cuda_path = _DEFAULT_CUDA_PATH
Martin Wicked57572e2017-09-02 19:21:45 -0700829 if is_windows() or is_cygwin():
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700830 default_cuda_path = cygpath(
831 environ_cp.get('CUDA_PATH', _DEFAULT_CUDA_PATH_WIN))
832 elif is_linux():
833 # If the default doesn't exist, try an alternative default.
834 if (not os.path.exists(default_cuda_path)
835 ) and os.path.exists(_DEFAULT_CUDA_PATH_LINUX):
836 default_cuda_path = _DEFAULT_CUDA_PATH_LINUX
837 ask_cuda_path = ('Please specify the location where CUDA %s toolkit is'
838 ' installed. Refer to README.md for more details. '
839 '[Default is %s]: ') % (tf_cuda_version, default_cuda_path)
840 cuda_toolkit_path = get_from_env_or_user_or_default(
841 environ_cp, 'CUDA_TOOLKIT_PATH', ask_cuda_path, default_cuda_path)
A. Unique TensorFlower02f17fe2018-07-07 06:59:19 -0700842 if is_windows() or is_cygwin():
843 cuda_toolkit_path = cygpath(cuda_toolkit_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700844
845 if is_windows():
Niall Moranb7d97e82018-08-09 00:29:49 +0100846 cuda_rt_lib_paths = ['lib/x64/cudart.lib']
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700847 elif is_linux():
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700848 cuda_rt_lib_paths = [
849 '%s/libcudart.so.%s' % (x, tf_cuda_version) for x in [
850 'lib64',
851 'lib/powerpc64le-linux-gnu',
852 'lib/x86_64-linux-gnu',
853 ]
854 ]
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700855 elif is_macos():
Niall Moranb7d97e82018-08-09 00:29:49 +0100856 cuda_rt_lib_paths = ['lib/libcudart.%s.dylib' % tf_cuda_version]
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700857
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700858 cuda_toolkit_paths_full = [
859 os.path.join(cuda_toolkit_path, x) for x in cuda_rt_lib_paths
860 ]
Niall Moranb7d97e82018-08-09 00:29:49 +0100861 if any([os.path.exists(x) for x in cuda_toolkit_paths_full]):
Yifei Feng5198cb82018-08-17 13:53:06 -0700862 break
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700863
864 # Reset and retry
865 print('Invalid path to CUDA %s toolkit. %s cannot be found' %
hellcom9a13fc32018-09-12 10:58:24 +0300866 (tf_cuda_version, cuda_toolkit_paths_full))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700867 environ_cp['TF_CUDA_VERSION'] = ''
868 environ_cp['CUDA_TOOLKIT_PATH'] = ''
869
Austin Andersonf9a88f82017-12-13 11:49:40 -0800870 else:
871 raise UserInputError('Invalid TF_CUDA_SETTING setting was provided %d '
872 'times in a row. Assuming to be a scripting mistake.' %
873 _DEFAULT_PROMPT_ASK_ATTEMPTS)
874
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700875 # Set CUDA_TOOLKIT_PATH and TF_CUDA_VERSION
876 environ_cp['CUDA_TOOLKIT_PATH'] = cuda_toolkit_path
877 write_action_env_to_bazelrc('CUDA_TOOLKIT_PATH', cuda_toolkit_path)
878 environ_cp['TF_CUDA_VERSION'] = tf_cuda_version
879 write_action_env_to_bazelrc('TF_CUDA_VERSION', tf_cuda_version)
880
881
Yifei Fengb1d8c592017-11-22 13:42:21 -0800882def set_tf_cudnn_version(environ_cp):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700883 """Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION."""
884 ask_cudnn_version = (
Jonathan Hseu008910f2017-08-25 14:01:05 -0700885 'Please specify the cuDNN version you want to use. '
A. Unique TensorFlower44acd832018-10-01 13:42:40 -0700886 '[Leave empty to default to cuDNN %s]: ') % _DEFAULT_CUDNN_VERSION
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700887
Austin Andersonf9a88f82017-12-13 11:49:40 -0800888 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700889 tf_cudnn_version = get_from_env_or_user_or_default(
890 environ_cp, 'TF_CUDNN_VERSION', ask_cudnn_version,
891 _DEFAULT_CUDNN_VERSION)
Ankur Taly0e6f39d2018-02-16 18:22:55 -0800892 tf_cudnn_version = reformat_version_sequence(str(tf_cudnn_version), 1)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700893
894 default_cudnn_path = environ_cp.get('CUDA_TOOLKIT_PATH')
895 ask_cudnn_path = (r'Please specify the location where cuDNN %s library is '
896 'installed. Refer to README.md for more details. [Default'
A. Unique TensorFlower1b212352018-07-19 13:48:50 -0700897 ' is %s]: ') % (tf_cudnn_version, default_cudnn_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700898 cudnn_install_path = get_from_env_or_user_or_default(
899 environ_cp, 'CUDNN_INSTALL_PATH', ask_cudnn_path, default_cudnn_path)
900
901 # Result returned from "read" will be used unexpanded. That make "~"
902 # unusable. Going through one more level of expansion to handle that.
903 cudnn_install_path = os.path.realpath(
904 os.path.expanduser(cudnn_install_path))
Martin Wicked57572e2017-09-02 19:21:45 -0700905 if is_windows() or is_cygwin():
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700906 cudnn_install_path = cygpath(cudnn_install_path)
907
908 if is_windows():
909 cuda_dnn_lib_path = 'lib/x64/cudnn.lib'
910 cuda_dnn_lib_alt_path = 'lib/x64/cudnn.lib'
911 elif is_linux():
912 cuda_dnn_lib_path = 'lib64/libcudnn.so.%s' % tf_cudnn_version
913 cuda_dnn_lib_alt_path = 'libcudnn.so.%s' % tf_cudnn_version
914 elif is_macos():
915 cuda_dnn_lib_path = 'lib/libcudnn.%s.dylib' % tf_cudnn_version
916 cuda_dnn_lib_alt_path = 'libcudnn.%s.dylib' % tf_cudnn_version
917
918 cuda_dnn_lib_path_full = os.path.join(cudnn_install_path, cuda_dnn_lib_path)
919 cuda_dnn_lib_alt_path_full = os.path.join(cudnn_install_path,
920 cuda_dnn_lib_alt_path)
921 if os.path.exists(cuda_dnn_lib_path_full) or os.path.exists(
922 cuda_dnn_lib_alt_path_full):
923 break
924
925 # Try another alternative for Linux
926 if is_linux():
Jonathan Hseu008910f2017-08-25 14:01:05 -0700927 ldconfig_bin = which('ldconfig') or '/sbin/ldconfig'
928 cudnn_path_from_ldconfig = run_shell([ldconfig_bin, '-p'])
929 cudnn_path_from_ldconfig = re.search('.*libcudnn.so .* => (.*)',
A. Unique TensorFlowere7223582017-09-06 17:57:04 -0700930 cudnn_path_from_ldconfig)
931 if cudnn_path_from_ldconfig:
932 cudnn_path_from_ldconfig = cudnn_path_from_ldconfig.group(1)
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -0700933 if os.path.exists(
934 '%s.%s' % (cudnn_path_from_ldconfig, tf_cudnn_version)):
A. Unique TensorFlowere7223582017-09-06 17:57:04 -0700935 cudnn_install_path = os.path.dirname(cudnn_path_from_ldconfig)
936 break
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700937
938 # Reset and Retry
939 print(
940 'Invalid path to cuDNN %s toolkit. None of the following files can be '
941 'found:' % tf_cudnn_version)
942 print(cuda_dnn_lib_path_full)
943 print(cuda_dnn_lib_alt_path_full)
944 if is_linux():
945 print('%s.%s' % (cudnn_path_from_ldconfig, tf_cudnn_version))
946
947 environ_cp['TF_CUDNN_VERSION'] = ''
Austin Andersonf9a88f82017-12-13 11:49:40 -0800948 else:
949 raise UserInputError('Invalid TF_CUDNN setting was provided %d '
950 'times in a row. Assuming to be a scripting mistake.' %
951 _DEFAULT_PROMPT_ASK_ATTEMPTS)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700952
953 # Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION
954 environ_cp['CUDNN_INSTALL_PATH'] = cudnn_install_path
955 write_action_env_to_bazelrc('CUDNN_INSTALL_PATH', cudnn_install_path)
956 environ_cp['TF_CUDNN_VERSION'] = tf_cudnn_version
957 write_action_env_to_bazelrc('TF_CUDNN_VERSION', tf_cudnn_version)
958
959
Mingxing Tan1e7b0e42018-06-28 19:13:20 -0700960def is_cuda_compatible(lib, cuda_ver, cudnn_ver):
961 """Check compatibility between given library and cudnn/cudart libraries."""
962 ldd_bin = which('ldd') or '/usr/bin/ldd'
963 ldd_out = run_shell([ldd_bin, lib], True)
964 ldd_out = ldd_out.split(os.linesep)
965 cudnn_pattern = re.compile('.*libcudnn.so\\.?(.*) =>.*$')
966 cuda_pattern = re.compile('.*libcudart.so\\.?(.*) =>.*$')
967 cudnn = None
968 cudart = None
969 cudnn_ok = True # assume no cudnn dependency by default
970 cuda_ok = True # assume no cuda dependency by default
971 for line in ldd_out:
972 if 'libcudnn.so' in line:
973 cudnn = cudnn_pattern.search(line)
974 cudnn_ok = False
975 elif 'libcudart.so' in line:
976 cudart = cuda_pattern.search(line)
977 cuda_ok = False
978 if cudnn and len(cudnn.group(1)):
979 cudnn = convert_version_to_int(cudnn.group(1))
980 if cudart and len(cudart.group(1)):
981 cudart = convert_version_to_int(cudart.group(1))
982 if cudnn is not None:
983 cudnn_ok = (cudnn == cudnn_ver)
984 if cudart is not None:
985 cuda_ok = (cudart == cuda_ver)
986 return cudnn_ok and cuda_ok
987
988
Guangda Lai76f69382018-01-25 23:59:19 -0800989def set_tf_tensorrt_install_path(environ_cp):
990 """Set TENSORRT_INSTALL_PATH and TF_TENSORRT_VERSION.
991
992 Adapted from code contributed by Sami Kama (https://github.com/samikama).
993
994 Args:
995 environ_cp: copy of the os.environ.
996
997 Raises:
998 ValueError: if this method was called under non-Linux platform.
999 UserInputError: if user has provided invalid input multiple times.
1000 """
1001 if not is_linux():
1002 raise ValueError('Currently TensorRT is only supported on Linux platform.')
1003
1004 # Ask user whether to add TensorRT support.
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001005 if str(int(get_var(environ_cp, 'TF_NEED_TENSORRT', 'TensorRT',
1006 False))) != '1':
Guangda Lai76f69382018-01-25 23:59:19 -08001007 return
1008
1009 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
1010 ask_tensorrt_path = (r'Please specify the location where TensorRT is '
1011 'installed. [Default is %s]:') % (
1012 _DEFAULT_TENSORRT_PATH_LINUX)
1013 trt_install_path = get_from_env_or_user_or_default(
1014 environ_cp, 'TENSORRT_INSTALL_PATH', ask_tensorrt_path,
1015 _DEFAULT_TENSORRT_PATH_LINUX)
1016
1017 # Result returned from "read" will be used unexpanded. That make "~"
1018 # unusable. Going through one more level of expansion to handle that.
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001019 trt_install_path = os.path.realpath(os.path.expanduser(trt_install_path))
Guangda Lai76f69382018-01-25 23:59:19 -08001020
1021 def find_libs(search_path):
1022 """Search for libnvinfer.so in "search_path"."""
1023 fl = set()
1024 if os.path.exists(search_path) and os.path.isdir(search_path):
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001025 fl.update([
1026 os.path.realpath(os.path.join(search_path, x))
1027 for x in os.listdir(search_path)
1028 if 'libnvinfer.so' in x
1029 ])
Guangda Lai76f69382018-01-25 23:59:19 -08001030 return fl
1031
1032 possible_files = find_libs(trt_install_path)
1033 possible_files.update(find_libs(os.path.join(trt_install_path, 'lib')))
1034 possible_files.update(find_libs(os.path.join(trt_install_path, 'lib64')))
Guangda Lai76f69382018-01-25 23:59:19 -08001035 cuda_ver = convert_version_to_int(environ_cp['TF_CUDA_VERSION'])
1036 cudnn_ver = convert_version_to_int(environ_cp['TF_CUDNN_VERSION'])
1037 nvinfer_pattern = re.compile('.*libnvinfer.so.?(.*)$')
1038 highest_ver = [0, None, None]
1039
1040 for lib_file in possible_files:
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001041 if is_cuda_compatible(lib_file, cuda_ver, cudnn_ver):
Jacques Pienaar2d0531d2018-03-21 12:07:51 -07001042 matches = nvinfer_pattern.search(lib_file)
TensorFlower Gardener61a87202018-10-01 12:25:39 -07001043 if not matches.groups():
Jacques Pienaar2d0531d2018-03-21 12:07:51 -07001044 continue
1045 ver_str = matches.group(1)
Guangda Lai76f69382018-01-25 23:59:19 -08001046 ver = convert_version_to_int(ver_str) if len(ver_str) else 0
1047 if ver > highest_ver[0]:
1048 highest_ver = [ver, ver_str, lib_file]
1049 if highest_ver[1] is not None:
1050 trt_install_path = os.path.dirname(highest_ver[2])
1051 tf_tensorrt_version = highest_ver[1]
1052 break
1053
1054 # Try another alternative from ldconfig.
1055 ldconfig_bin = which('ldconfig') or '/sbin/ldconfig'
1056 ldconfig_output = run_shell([ldconfig_bin, '-p'])
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001057 search_result = re.search('.*libnvinfer.so\\.?([0-9.]*).* => (.*)',
1058 ldconfig_output)
Guangda Lai76f69382018-01-25 23:59:19 -08001059 if search_result:
1060 libnvinfer_path_from_ldconfig = search_result.group(2)
1061 if os.path.exists(libnvinfer_path_from_ldconfig):
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001062 if is_cuda_compatible(libnvinfer_path_from_ldconfig, cuda_ver,
1063 cudnn_ver):
Guangda Lai76f69382018-01-25 23:59:19 -08001064 trt_install_path = os.path.dirname(libnvinfer_path_from_ldconfig)
1065 tf_tensorrt_version = search_result.group(1)
1066 break
1067
1068 # Reset and Retry
Yifei Fengdce9a492018-02-22 14:24:57 -08001069 if possible_files:
1070 print('TensorRT libraries found in one the following directories',
1071 'are not compatible with selected cuda and cudnn installations')
1072 print(trt_install_path)
1073 print(os.path.join(trt_install_path, 'lib'))
1074 print(os.path.join(trt_install_path, 'lib64'))
1075 if search_result:
1076 print(libnvinfer_path_from_ldconfig)
1077 else:
1078 print(
1079 'Invalid path to TensorRT. None of the following files can be found:')
1080 print(trt_install_path)
1081 print(os.path.join(trt_install_path, 'lib'))
1082 print(os.path.join(trt_install_path, 'lib64'))
1083 if search_result:
1084 print(libnvinfer_path_from_ldconfig)
Guangda Lai76f69382018-01-25 23:59:19 -08001085
1086 else:
1087 raise UserInputError('Invalid TF_TENSORRT setting was provided %d '
1088 'times in a row. Assuming to be a scripting mistake.' %
1089 _DEFAULT_PROMPT_ASK_ATTEMPTS)
1090
1091 # Set TENSORRT_INSTALL_PATH and TF_TENSORRT_VERSION
1092 environ_cp['TENSORRT_INSTALL_PATH'] = trt_install_path
1093 write_action_env_to_bazelrc('TENSORRT_INSTALL_PATH', trt_install_path)
1094 environ_cp['TF_TENSORRT_VERSION'] = tf_tensorrt_version
1095 write_action_env_to_bazelrc('TF_TENSORRT_VERSION', tf_tensorrt_version)
1096
1097
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001098def set_tf_nccl_install_path(environ_cp):
Jason Furmanek7c234152018-09-26 04:44:12 +00001099 """Set NCCL_INSTALL_PATH, NCCL_HDR_PATH and TF_NCCL_VERSION.
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001100
1101 Args:
1102 environ_cp: copy of the os.environ.
1103
1104 Raises:
1105 ValueError: if this method was called under non-Linux platform.
1106 UserInputError: if user has provided invalid input multiple times.
1107 """
1108 if not is_linux():
1109 raise ValueError('Currently NCCL is only supported on Linux platforms.')
1110
1111 ask_nccl_version = (
Smit Hinsu63e6b9b2018-07-13 12:46:24 -07001112 'Please specify the NCCL version you want to use. If NCCL %s is not '
1113 'installed, then you can use version 1.3 that can be fetched '
1114 'automatically but it may have worse performance with multiple GPUs. '
1115 '[Default is %s]: ') % (_DEFAULT_NCCL_VERSION, _DEFAULT_NCCL_VERSION)
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001116
1117 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
1118 tf_nccl_version = get_from_env_or_user_or_default(
1119 environ_cp, 'TF_NCCL_VERSION', ask_nccl_version, _DEFAULT_NCCL_VERSION)
1120 tf_nccl_version = reformat_version_sequence(str(tf_nccl_version), 1)
1121
1122 if tf_nccl_version == '1':
1123 break # No need to get install path, NCCL 1 is a GitHub repo.
1124
Jason Furmanek7c234152018-09-26 04:44:12 +00001125 # Look with ldconfig first if we can find the library in paths
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001126 # like /usr/lib/x86_64-linux-gnu and the header file in the corresponding
1127 # include directory. This is where the NCCL .deb packages install them.
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001128
Jason Furmanek7c234152018-09-26 04:44:12 +00001129 # First check to see if NCCL is in the ldconfig.
1130 # If its found, use that location.
1131 if is_linux():
1132 ldconfig_bin = which('ldconfig') or '/sbin/ldconfig'
1133 nccl2_path_from_ldconfig = run_shell([ldconfig_bin, '-p'])
1134 nccl2_path_from_ldconfig = re.search('.*libnccl.so .* => (.*)',
1135 nccl2_path_from_ldconfig)
1136 if nccl2_path_from_ldconfig:
1137 nccl2_path_from_ldconfig = nccl2_path_from_ldconfig.group(1)
1138 if os.path.exists('%s.%s' % (nccl2_path_from_ldconfig, tf_nccl_version)):
1139 nccl_install_path = os.path.dirname(nccl2_path_from_ldconfig)
1140 print('NCCL libraries found in ' + nccl2_path_from_ldconfig)
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001141
Jason Furmanek7c234152018-09-26 04:44:12 +00001142 # Check if this is the main system lib location
1143 if re.search('.*linux-gnu', nccl_install_path):
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -07001144 trunc_nccl_install_path = '/usr'
1145 print('This looks like a system path.')
Jason Furmanek7c234152018-09-26 04:44:12 +00001146 else:
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -07001147 trunc_nccl_install_path = nccl_install_path + '/..'
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001148
Jason Furmanek7c234152018-09-26 04:44:12 +00001149 # Look for header
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -07001150 nccl_hdr_path = trunc_nccl_install_path + '/include'
1151 print('Assuming NCCL header path is ' + nccl_hdr_path)
1152 if os.path.exists(nccl_hdr_path + '/nccl.h'):
Jason Furmanek7c234152018-09-26 04:44:12 +00001153 # Set NCCL_INSTALL_PATH
1154 environ_cp['NCCL_INSTALL_PATH'] = nccl_install_path
1155 write_action_env_to_bazelrc('NCCL_INSTALL_PATH', nccl_install_path)
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001156
Jason Furmanek7c234152018-09-26 04:44:12 +00001157 # Set NCCL_HDR_PATH
1158 environ_cp['NCCL_HDR_PATH'] = nccl_hdr_path
1159 write_action_env_to_bazelrc('NCCL_HDR_PATH', nccl_hdr_path)
1160 break
1161 else:
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -07001162 print(
1163 'The header for NCCL2 cannot be found. Please install the libnccl-dev package.'
1164 )
Jason Furmanek7c234152018-09-26 04:44:12 +00001165 else:
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -07001166 print('NCCL2 is listed by ldconfig but the library is not found. '
1167 'Your ldconfig is out of date. Please run sudo ldconfig.')
Jason Furmanek7c234152018-09-26 04:44:12 +00001168 else:
1169 # NCCL is not found in ldconfig. Ask the user for the location.
1170 default_nccl_path = environ_cp.get('CUDA_TOOLKIT_PATH')
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -07001171 ask_nccl_path = (
1172 r'Please specify the location where NCCL %s library is '
1173 'installed. Refer to README.md for more details. [Default '
1174 'is %s]:') % (tf_nccl_version, default_nccl_path)
Jason Furmanek7c234152018-09-26 04:44:12 +00001175 nccl_install_path = get_from_env_or_user_or_default(
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -07001176 environ_cp, 'NCCL_INSTALL_PATH', ask_nccl_path, default_nccl_path)
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001177
Jason Furmanek7c234152018-09-26 04:44:12 +00001178 # Result returned from "read" will be used unexpanded. That make "~"
1179 # unusable. Going through one more level of expansion to handle that.
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -07001180 nccl_install_path = os.path.realpath(
1181 os.path.expanduser(nccl_install_path))
Jason Furmanek7c234152018-09-26 04:44:12 +00001182 if is_windows() or is_cygwin():
1183 nccl_install_path = cygpath(nccl_install_path)
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001184
Jason Furmanek7c234152018-09-26 04:44:12 +00001185 if is_windows():
1186 nccl_lib_path = 'lib/x64/nccl.lib'
1187 elif is_linux():
1188 nccl_lib_filename = 'libnccl.so.%s' % tf_nccl_version
1189 nccl_lpath = '%s/lib/%s' % (nccl_install_path, nccl_lib_filename)
1190 if not os.path.exists(nccl_lpath):
1191 for relative_path in NCCL_LIB_PATHS:
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -07001192 path = '%s/%s%s' % (nccl_install_path, relative_path,
1193 nccl_lib_filename)
Jason Furmanek7c234152018-09-26 04:44:12 +00001194 if os.path.exists(path):
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -07001195 print('NCCL found at ' + path)
Jason Furmanek7c234152018-09-26 04:44:12 +00001196 nccl_lib_path = path
1197 break
1198 else:
1199 nccl_lib_path = nccl_lpath
1200 elif is_macos():
1201 nccl_lib_path = 'lib/libnccl.%s.dylib' % tf_nccl_version
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001202
Jason Furmanek7c234152018-09-26 04:44:12 +00001203 nccl_lib_path = os.path.join(nccl_install_path, nccl_lib_path)
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -07001204 nccl_hdr_path = os.path.join(
1205 os.path.dirname(nccl_lib_path), '../include/nccl.h')
1206 print('Assuming NCCL header path is ' + nccl_hdr_path)
Jason Furmanek7c234152018-09-26 04:44:12 +00001207 if os.path.exists(nccl_lib_path) and os.path.exists(nccl_hdr_path):
1208 # Set NCCL_INSTALL_PATH
1209 environ_cp['NCCL_INSTALL_PATH'] = os.path.dirname(nccl_lib_path)
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -07001210 write_action_env_to_bazelrc('NCCL_INSTALL_PATH',
1211 os.path.dirname(nccl_lib_path))
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001212
Jason Furmanek7c234152018-09-26 04:44:12 +00001213 # Set NCCL_HDR_PATH
1214 environ_cp['NCCL_HDR_PATH'] = os.path.dirname(nccl_hdr_path)
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -07001215 write_action_env_to_bazelrc('NCCL_HDR_PATH',
1216 os.path.dirname(nccl_hdr_path))
Jason Furmanek7c234152018-09-26 04:44:12 +00001217 break
1218
1219 # Reset and Retry
TensorFlower Gardenerea5c5292018-10-01 17:28:26 -07001220 print(
1221 'Invalid path to NCCL %s toolkit, %s or %s not found. Please use the '
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001222 'O/S agnostic package of NCCL 2' % (tf_nccl_version, nccl_lib_path,
1223 nccl_hdr_path))
1224
Jason Furmanek7c234152018-09-26 04:44:12 +00001225 environ_cp['TF_NCCL_VERSION'] = ''
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001226 else:
1227 raise UserInputError('Invalid TF_NCCL setting was provided %d '
1228 'times in a row. Assuming to be a scripting mistake.' %
1229 _DEFAULT_PROMPT_ASK_ATTEMPTS)
1230
1231 # Set TF_NCCL_VERSION
1232 environ_cp['TF_NCCL_VERSION'] = tf_nccl_version
1233 write_action_env_to_bazelrc('TF_NCCL_VERSION', tf_nccl_version)
1234
1235
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001236def get_native_cuda_compute_capabilities(environ_cp):
1237 """Get native cuda compute capabilities.
1238
1239 Args:
1240 environ_cp: copy of the os.environ.
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001241
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001242 Returns:
1243 string of native cuda compute capabilities, separated by comma.
1244 """
1245 device_query_bin = os.path.join(
1246 environ_cp.get('CUDA_TOOLKIT_PATH'), 'extras/demo_suite/deviceQuery')
Jonathan Hseu008910f2017-08-25 14:01:05 -07001247 if os.path.isfile(device_query_bin) and os.access(device_query_bin, os.X_OK):
1248 try:
1249 output = run_shell(device_query_bin).split('\n')
1250 pattern = re.compile('[0-9]*\\.[0-9]*')
1251 output = [pattern.search(x) for x in output if 'Capability' in x]
1252 output = ','.join(x.group() for x in output if x is not None)
1253 except subprocess.CalledProcessError:
1254 output = ''
1255 else:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001256 output = ''
1257 return output
1258
1259
1260def set_tf_cuda_compute_capabilities(environ_cp):
1261 """Set TF_CUDA_COMPUTE_CAPABILITIES."""
1262 while True:
1263 native_cuda_compute_capabilities = get_native_cuda_compute_capabilities(
1264 environ_cp)
1265 if not native_cuda_compute_capabilities:
1266 default_cuda_compute_capabilities = _DEFAULT_CUDA_COMPUTE_CAPABILITIES
1267 else:
1268 default_cuda_compute_capabilities = native_cuda_compute_capabilities
1269
1270 ask_cuda_compute_capabilities = (
1271 'Please specify a list of comma-separated '
1272 'Cuda compute capabilities you want to '
1273 'build with.\nYou can find the compute '
1274 'capability of your device at: '
1275 'https://developer.nvidia.com/cuda-gpus.\nPlease'
1276 ' note that each additional compute '
1277 'capability significantly increases your '
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001278 'build time and binary size. [Default is: %s]: ' %
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001279 default_cuda_compute_capabilities)
1280 tf_cuda_compute_capabilities = get_from_env_or_user_or_default(
1281 environ_cp, 'TF_CUDA_COMPUTE_CAPABILITIES',
1282 ask_cuda_compute_capabilities, default_cuda_compute_capabilities)
1283 # Check whether all capabilities from the input is valid
1284 all_valid = True
Maciejd0f5bc12018-04-30 22:30:58 -05001285 # Remove all whitespace characters before splitting the string
Michael Case51053502018-06-05 17:47:19 -07001286 # that users may insert by accident, as this will result in error
Maciejd0f5bc12018-04-30 22:30:58 -05001287 tf_cuda_compute_capabilities = ''.join(tf_cuda_compute_capabilities.split())
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001288 for compute_capability in tf_cuda_compute_capabilities.split(','):
Andrew Harp6e3e7d12017-08-21 12:10:44 -07001289 m = re.match('[0-9]+.[0-9]+', compute_capability)
1290 if not m:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001291 print('Invalid compute capability: ' % compute_capability)
1292 all_valid = False
Andrew Harp6e3e7d12017-08-21 12:10:44 -07001293 else:
1294 ver = int(m.group(0).split('.')[0])
1295 if ver < 3:
1296 print('Only compute capabilities 3.0 or higher are supported.')
1297 all_valid = False
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001298
1299 if all_valid:
1300 break
1301
1302 # Reset and Retry
1303 environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = ''
1304
1305 # Set TF_CUDA_COMPUTE_CAPABILITIES
1306 environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = tf_cuda_compute_capabilities
1307 write_action_env_to_bazelrc('TF_CUDA_COMPUTE_CAPABILITIES',
1308 tf_cuda_compute_capabilities)
1309
1310
1311def set_other_cuda_vars(environ_cp):
1312 """Set other CUDA related variables."""
A. Unique TensorFlowerab391982018-07-11 04:52:49 -07001313 # If CUDA is enabled, always use GPU during build and test.
1314 if environ_cp.get('TF_CUDA_CLANG') == '1':
1315 write_to_bazelrc('build --config=cuda_clang')
1316 write_to_bazelrc('test --config=cuda_clang')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001317 else:
A. Unique TensorFlowerab391982018-07-11 04:52:49 -07001318 write_to_bazelrc('build --config=cuda')
1319 write_to_bazelrc('test --config=cuda')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001320
1321
1322def set_host_cxx_compiler(environ_cp):
1323 """Set HOST_CXX_COMPILER."""
Jonathan Hseu008910f2017-08-25 14:01:05 -07001324 default_cxx_host_compiler = which('g++') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001325
Austin Anderson6afface2017-12-05 11:59:17 -08001326 host_cxx_compiler = prompt_loop_or_load_from_env(
1327 environ_cp,
1328 var_name='HOST_CXX_COMPILER',
1329 var_default=default_cxx_host_compiler,
1330 ask_for_var=('Please specify which C++ compiler should be used as the '
1331 'host C++ compiler.'),
1332 check_success=os.path.exists,
1333 error_msg='Invalid C++ compiler path. %s cannot be found.',
1334 )
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001335
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001336 write_action_env_to_bazelrc('HOST_CXX_COMPILER', host_cxx_compiler)
1337
1338
1339def set_host_c_compiler(environ_cp):
1340 """Set HOST_C_COMPILER."""
Jonathan Hseu008910f2017-08-25 14:01:05 -07001341 default_c_host_compiler = which('gcc') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001342
Austin Anderson6afface2017-12-05 11:59:17 -08001343 host_c_compiler = prompt_loop_or_load_from_env(
1344 environ_cp,
1345 var_name='HOST_C_COMPILER',
1346 var_default=default_c_host_compiler,
Shanqing Cai71445712018-03-12 19:33:52 -07001347 ask_for_var=('Please specify which C compiler should be used as the host '
Austin Anderson6afface2017-12-05 11:59:17 -08001348 'C compiler.'),
1349 check_success=os.path.exists,
1350 error_msg='Invalid C compiler path. %s cannot be found.',
1351 )
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001352
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001353 write_action_env_to_bazelrc('HOST_C_COMPILER', host_c_compiler)
1354
1355
1356def set_computecpp_toolkit_path(environ_cp):
1357 """Set COMPUTECPP_TOOLKIT_PATH."""
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001358
Austin Anderson6afface2017-12-05 11:59:17 -08001359 def toolkit_exists(toolkit_path):
1360 """Check if a computecpp toolkit path is valid."""
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001361 if is_linux():
1362 sycl_rt_lib_path = 'lib/libComputeCpp.so'
1363 else:
1364 sycl_rt_lib_path = ''
1365
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001366 sycl_rt_lib_path_full = os.path.join(toolkit_path, sycl_rt_lib_path)
Austin Anderson6afface2017-12-05 11:59:17 -08001367 exists = os.path.exists(sycl_rt_lib_path_full)
1368 if not exists:
1369 print('Invalid SYCL %s library path. %s cannot be found' %
1370 (_TF_OPENCL_VERSION, sycl_rt_lib_path_full))
1371 return exists
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001372
Austin Anderson6afface2017-12-05 11:59:17 -08001373 computecpp_toolkit_path = prompt_loop_or_load_from_env(
1374 environ_cp,
1375 var_name='COMPUTECPP_TOOLKIT_PATH',
1376 var_default=_DEFAULT_COMPUTECPP_TOOLKIT_PATH,
1377 ask_for_var=(
1378 'Please specify the location where ComputeCpp for SYCL %s is '
1379 'installed.' % _TF_OPENCL_VERSION),
1380 check_success=toolkit_exists,
1381 error_msg='Invalid SYCL compiler path. %s cannot be found.',
1382 suppress_default_error=True)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001383
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001384 write_action_env_to_bazelrc('COMPUTECPP_TOOLKIT_PATH',
1385 computecpp_toolkit_path)
1386
Michael Cased31531a2018-01-05 14:09:41 -08001387
Dandelion Man?90e42f32017-12-15 18:15:07 -08001388def set_trisycl_include_dir(environ_cp):
Michael Cased31531a2018-01-05 14:09:41 -08001389 """Set TRISYCL_INCLUDE_DIR."""
Frank Chenc4ef9272018-01-10 11:36:52 -08001390
Dandelion Man?90e42f32017-12-15 18:15:07 -08001391 ask_trisycl_include_dir = ('Please specify the location of the triSYCL '
1392 'include directory. (Use --config=sycl_trisycl '
1393 'when building with Bazel) '
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001394 '[Default is %s]: ') % (
1395 _DEFAULT_TRISYCL_INCLUDE_DIR)
Frank Chenc4ef9272018-01-10 11:36:52 -08001396
Dandelion Man?90e42f32017-12-15 18:15:07 -08001397 while True:
1398 trisycl_include_dir = get_from_env_or_user_or_default(
Michael Cased31531a2018-01-05 14:09:41 -08001399 environ_cp, 'TRISYCL_INCLUDE_DIR', ask_trisycl_include_dir,
1400 _DEFAULT_TRISYCL_INCLUDE_DIR)
Dandelion Man?90e42f32017-12-15 18:15:07 -08001401 if os.path.exists(trisycl_include_dir):
1402 break
1403
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001404 print('Invalid triSYCL include directory, %s cannot be found' %
1405 (trisycl_include_dir))
Dandelion Man?90e42f32017-12-15 18:15:07 -08001406
1407 # Set TRISYCL_INCLUDE_DIR
1408 environ_cp['TRISYCL_INCLUDE_DIR'] = trisycl_include_dir
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001409 write_action_env_to_bazelrc('TRISYCL_INCLUDE_DIR', trisycl_include_dir)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001410
Yifei Fengb1d8c592017-11-22 13:42:21 -08001411
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001412def set_mpi_home(environ_cp):
1413 """Set MPI_HOME."""
Austin Anderson6afface2017-12-05 11:59:17 -08001414
Jonathan Hseu008910f2017-08-25 14:01:05 -07001415 default_mpi_home = which('mpirun') or which('mpiexec') or ''
1416 default_mpi_home = os.path.dirname(os.path.dirname(default_mpi_home))
1417
Austin Anderson6afface2017-12-05 11:59:17 -08001418 def valid_mpi_path(mpi_home):
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001419 exists = (
Christian Gollba95d092018-10-04 17:06:23 +02001420 os.path.exists(os.path.join(mpi_home, 'include')) and (
1421 os.path.exists(os.path.join(mpi_home, 'lib')) or
1422 os.path.exists(os.path.join(mpi_home, 'lib64')) or
1423 os.path.exists(os.path.join(mpi_home, 'lib32'))))
Austin Anderson6afface2017-12-05 11:59:17 -08001424 if not exists:
Christian Gollba95d092018-10-04 17:06:23 +02001425 print('Invalid path to the MPI Toolkit. %s or %s or %s or %s cannot be found' %
Austin Anderson6afface2017-12-05 11:59:17 -08001426 (os.path.join(mpi_home, 'include'),
Christian Gollba95d092018-10-04 17:06:23 +02001427 os.path.exists(os.path.join(mpi_home, 'lib')),
1428 os.path.exists(os.path.join(mpi_home, 'lib64')),
1429 os.path.exists(os.path.join(mpi_home, 'lib32'))))
Austin Anderson6afface2017-12-05 11:59:17 -08001430 return exists
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001431
Austin Anderson6afface2017-12-05 11:59:17 -08001432 _ = prompt_loop_or_load_from_env(
1433 environ_cp,
1434 var_name='MPI_HOME',
1435 var_default=default_mpi_home,
1436 ask_for_var='Please specify the MPI toolkit folder.',
1437 check_success=valid_mpi_path,
1438 error_msg='',
1439 suppress_default_error=True)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001440
1441
1442def set_other_mpi_vars(environ_cp):
1443 """Set other MPI related variables."""
1444 # Link the MPI header files
1445 mpi_home = environ_cp.get('MPI_HOME')
1446 symlink_force('%s/include/mpi.h' % mpi_home, 'third_party/mpi/mpi.h')
1447
1448 # Determine if we use OpenMPI or MVAPICH, these require different header files
1449 # to be included here to make bazel dependency checker happy
1450 if os.path.exists(os.path.join(mpi_home, 'include/mpi_portable_platform.h')):
1451 symlink_force(
1452 os.path.join(mpi_home, 'include/mpi_portable_platform.h'),
1453 'third_party/mpi/mpi_portable_platform.h')
1454 # TODO(gunan): avoid editing files in configure
1455 sed_in_place('third_party/mpi/mpi.bzl', 'MPI_LIB_IS_OPENMPI=False',
1456 'MPI_LIB_IS_OPENMPI=True')
1457 else:
1458 # MVAPICH / MPICH
1459 symlink_force(
1460 os.path.join(mpi_home, 'include/mpio.h'), 'third_party/mpi/mpio.h')
1461 symlink_force(
1462 os.path.join(mpi_home, 'include/mpicxx.h'), 'third_party/mpi/mpicxx.h')
1463 # TODO(gunan): avoid editing files in configure
1464 sed_in_place('third_party/mpi/mpi.bzl', 'MPI_LIB_IS_OPENMPI=True',
1465 'MPI_LIB_IS_OPENMPI=False')
1466
1467 if os.path.exists(os.path.join(mpi_home, 'lib/libmpi.so')):
1468 symlink_force(
1469 os.path.join(mpi_home, 'lib/libmpi.so'), 'third_party/mpi/libmpi.so')
Christian Gollba95d092018-10-04 17:06:23 +02001470 elif os.path.exists(os.path.join(mpi_home, 'lib64/libmpi.so')):
1471 symlink_force(
1472 os.path.join(mpi_home, 'lib64/libmpi.so'), 'third_party/mpi/libmpi.so')
1473 elif os.path.exists(os.path.join(mpi_home, 'lib32/libmpi.so')):
1474 symlink_force(
1475 os.path.join(mpi_home, 'lib32/libmpi.so'), 'third_party/mpi/libmpi.so')
1476
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001477 else:
Christian Gollba95d092018-10-04 17:06:23 +02001478 raise ValueError('Cannot find the MPI library file in %s/lib or %s/lib64 or %s/lib32' % mpi_home, mpi_home, mpi_home)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001479
1480
Yifei Feng5198cb82018-08-17 13:53:06 -07001481def set_system_libs_flag(environ_cp):
1482 syslibs = environ_cp.get('TF_SYSTEM_LIBS', '')
TensorFlower Gardener61a87202018-10-01 12:25:39 -07001483 if syslibs:
Jason Zaman5fc39bd2018-09-16 01:38:55 +08001484 if ',' in syslibs:
1485 syslibs = ','.join(sorted(syslibs.split(',')))
1486 else:
1487 syslibs = ','.join(sorted(syslibs.split()))
Yifei Feng5198cb82018-08-17 13:53:06 -07001488 write_action_env_to_bazelrc('TF_SYSTEM_LIBS', syslibs)
1489
Jason Zaman5fc39bd2018-09-16 01:38:55 +08001490 if 'PREFIX' in environ_cp:
1491 write_to_bazelrc('build --define=PREFIX=%s' % environ_cp['PREFIX'])
1492 if 'LIBDIR' in environ_cp:
1493 write_to_bazelrc('build --define=LIBDIR=%s' % environ_cp['LIBDIR'])
1494 if 'INCLUDEDIR' in environ_cp:
1495 write_to_bazelrc('build --define=INCLUDEDIR=%s' % environ_cp['INCLUDEDIR'])
1496
Yifei Feng5198cb82018-08-17 13:53:06 -07001497
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001498def set_windows_build_flags(environ_cp):
1499 """Set Windows specific build options."""
1500 # The non-monolithic build is not supported yet
1501 write_to_bazelrc('build --config monolithic')
1502 # Suppress warning messages
1503 write_to_bazelrc('build --copt=-w --host_copt=-w')
1504 # Output more verbose information when something goes wrong
1505 write_to_bazelrc('build --verbose_failures')
1506 # The host and target platforms are the same in Windows build. So we don't
1507 # have to distinct them. This avoids building the same targets twice.
1508 write_to_bazelrc('build --distinct_host_configuration=false')
1509 # Enable short object file path to avoid long path issue on Windows.
1510 # TODO(pcloudy): Remove this flag when upgrading Bazel to 0.16.0
1511 # Short object file path will be enabled by default.
1512 write_to_bazelrc('build --experimental_shortened_obj_file_path=true')
A. Unique TensorFlower77e26862018-09-27 06:19:58 -07001513 # When building zip file for some py_binary and py_test targets, don't
1514 # include its dependencies. This is for:
1515 # 1. Running python tests against the system installed TF pip package.
1516 # 2. Avoiding redundant files in
1517 # //tensorflow/tools/pip_package:simple_console_windows,
1518 # which is a py_binary used during creating TF pip package.
1519 # See https://github.com/tensorflow/tensorflow/issues/22390
1520 write_to_bazelrc('build --define=no_tensorflow_py_deps=true')
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001521
1522 if get_var(
1523 environ_cp, 'TF_OVERRIDE_EIGEN_STRONG_INLINE', 'Eigen strong inline',
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001524 True, ('Would you like to override eigen strong inline for some C++ '
1525 'compilation to reduce the compilation time?'),
1526 'Eigen strong inline overridden.', 'Not overriding eigen strong inline, '
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001527 'some compilations could take more than 20 mins.'):
1528 # Due to a known MSVC compiler issue
1529 # https://github.com/tensorflow/tensorflow/issues/10521
1530 # Overriding eigen strong inline speeds up the compiling of
1531 # conv_grad_ops_3d.cc and conv_ops_3d.cc by 20 minutes,
1532 # but this also hurts the performance. Let users decide what they want.
1533 write_to_bazelrc('build --define=override_eigen_strong_inline=true')
Dandelion Man?90e42f32017-12-15 18:15:07 -08001534
A. Unique TensorFlower061c3592017-11-13 14:21:04 -08001535
Michael Cased31531a2018-01-05 14:09:41 -08001536def config_info_line(name, help_text):
1537 """Helper function to print formatted help text for Bazel config options."""
1538 print('\t--config=%-12s\t# %s' % (name, help_text))
1539
1540
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001541def main():
A. Unique TensorFlower7c5eb352018-10-01 07:15:23 -07001542 global _TF_WORKSPACE_ROOT
1543 global _TF_BAZELRC
1544
Shanqing Cai71445712018-03-12 19:33:52 -07001545 parser = argparse.ArgumentParser()
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001546 parser.add_argument(
1547 '--workspace',
1548 type=str,
A. Unique TensorFlower7c5eb352018-10-01 07:15:23 -07001549 default=os.path.abspath(os.path.dirname(__file__)),
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001550 help='The absolute path to your active Bazel workspace.')
Shanqing Cai71445712018-03-12 19:33:52 -07001551 args = parser.parse_args()
1552
A. Unique TensorFlower7c5eb352018-10-01 07:15:23 -07001553 _TF_WORKSPACE_ROOT = args.workspace
1554 _TF_BAZELRC = os.path.join(_TF_WORKSPACE_ROOT, _TF_BAZELRC_FILENAME)
1555
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001556 # Make a copy of os.environ to be clear when functions and getting and setting
1557 # environment variables.
1558 environ_cp = dict(os.environ)
1559
Yifei Fengbb384112018-07-24 13:12:54 -07001560 check_bazel_version('0.15.0')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001561
A. Unique TensorFlower7c5eb352018-10-01 07:15:23 -07001562 reset_tf_configure_bazelrc()
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001563 cleanup_makefile()
Gunhan Gulsoyed89a2b2017-09-19 18:36:26 -07001564 setup_python(environ_cp)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001565
1566 if is_windows():
Yifei Fengb1d8c592017-11-22 13:42:21 -08001567 environ_cp['TF_NEED_OPENCL_SYCL'] = '0'
1568 environ_cp['TF_NEED_COMPUTECPP'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001569 environ_cp['TF_NEED_OPENCL'] = '0'
1570 environ_cp['TF_CUDA_CLANG'] = '0'
Guangda Lai76f69382018-01-25 23:59:19 -08001571 environ_cp['TF_NEED_TENSORRT'] = '0'
Ilya Biryukov9e651e42018-03-22 05:33:42 -07001572 # TODO(ibiryukov): Investigate using clang as a cpu or cuda compiler on
1573 # Windows.
1574 environ_cp['TF_DOWNLOAD_CLANG'] = '0'
A. Unique TensorFlower6e97fb32018-07-16 14:07:29 -07001575 environ_cp['TF_ENABLE_XLA'] = '0'
A. Unique TensorFlower6e97fb32018-07-16 14:07:29 -07001576 environ_cp['TF_NEED_MPI'] = '0'
1577 environ_cp['TF_SET_ANDROID_WORKSPACE'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001578
1579 if is_macos():
Guangda Lai76f69382018-01-25 23:59:19 -08001580 environ_cp['TF_NEED_TENSORRT'] = '0'
Todd Wang35459cb2018-09-28 08:56:06 -07001581 environ_cp['TF_ENABLE_XLA'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001582
Jon Triebenbach6896a742018-06-27 13:29:53 -05001583 # The numpy package on ppc64le uses OpenBLAS which has multi-threading
1584 # issues that lead to incorrect answers. Set OMP_NUM_THREADS=1 at
1585 # runtime to allow the Tensorflow testcases which compare numpy
1586 # results to Tensorflow results to succeed.
1587 if is_ppc64le():
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001588 write_action_env_to_bazelrc('OMP_NUM_THREADS', 1)
Jon Triebenbach6896a742018-06-27 13:29:53 -05001589
Anton Dmitriev85301672018-08-24 16:52:07 +03001590 set_build_var(environ_cp, 'TF_NEED_IGNITE', 'Apache Ignite',
1591 'with_ignite_support', True, 'ignite')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001592 set_build_var(environ_cp, 'TF_ENABLE_XLA', 'XLA JIT', 'with_xla_support',
Todd Wang35459cb2018-09-28 08:56:06 -07001593 True, 'xla')
Gunhan Gulsoy3da0dff2018-09-26 11:55:50 -07001594
Yifei Fengb1d8c592017-11-22 13:42:21 -08001595 set_action_env_var(environ_cp, 'TF_NEED_OPENCL_SYCL', 'OpenCL SYCL', False)
1596 if environ_cp.get('TF_NEED_OPENCL_SYCL') == '1':
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001597 set_host_cxx_compiler(environ_cp)
1598 set_host_c_compiler(environ_cp)
Yifei Fengb1d8c592017-11-22 13:42:21 -08001599 set_action_env_var(environ_cp, 'TF_NEED_COMPUTECPP', 'ComputeCPP', True)
1600 if environ_cp.get('TF_NEED_COMPUTECPP') == '1':
1601 set_computecpp_toolkit_path(environ_cp)
1602 else:
1603 set_trisycl_include_dir(environ_cp)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001604
Wen-Heng (Jack) Chung69d3b8f2018-06-22 23:09:43 -05001605 set_action_env_var(environ_cp, 'TF_NEED_ROCM', 'ROCm', False)
1606 if (environ_cp.get('TF_NEED_ROCM') == '1' and
TensorFlower Gardener62e60162018-09-27 10:22:55 -07001607 'LD_LIBRARY_PATH' in environ_cp and
1608 environ_cp.get('LD_LIBRARY_PATH') != '1'):
1609 write_action_env_to_bazelrc('LD_LIBRARY_PATH',
1610 environ_cp.get('LD_LIBRARY_PATH'))
Wen-Heng (Jack) Chung69d3b8f2018-06-22 23:09:43 -05001611
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001612 set_action_env_var(environ_cp, 'TF_NEED_CUDA', 'CUDA', False)
A. Unique TensorFlower24cbb2a2017-09-08 07:45:44 -07001613 if (environ_cp.get('TF_NEED_CUDA') == '1' and
1614 'TF_CUDA_CONFIG_REPO' not in environ_cp):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001615 set_tf_cuda_version(environ_cp)
Yifei Fengb1d8c592017-11-22 13:42:21 -08001616 set_tf_cudnn_version(environ_cp)
Guangda Lai76f69382018-01-25 23:59:19 -08001617 if is_linux():
1618 set_tf_tensorrt_install_path(environ_cp)
Michael Case0073d132018-04-11 09:34:44 -07001619 set_tf_nccl_install_path(environ_cp)
1620
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001621 set_tf_cuda_compute_capabilities(environ_cp)
Ankur Taly0e6f39d2018-02-16 18:22:55 -08001622 if 'LD_LIBRARY_PATH' in environ_cp and environ_cp.get(
1623 'LD_LIBRARY_PATH') != '1':
1624 write_action_env_to_bazelrc('LD_LIBRARY_PATH',
1625 environ_cp.get('LD_LIBRARY_PATH'))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001626
1627 set_tf_cuda_clang(environ_cp)
1628 if environ_cp.get('TF_CUDA_CLANG') == '1':
Ilya Biryukov9e651e42018-03-22 05:33:42 -07001629 # Ask whether we should download the clang toolchain.
1630 set_tf_download_clang(environ_cp)
A. Unique TensorFlowerfd29e952017-12-22 03:07:51 -08001631 if environ_cp.get('TF_DOWNLOAD_CLANG') != '1':
1632 # Set up which clang we should use as the cuda / host compiler.
1633 set_clang_cuda_compiler_path(environ_cp)
Ilya Biryukov1c3d02e2018-09-04 03:09:52 -07001634 else:
1635 # Use downloaded LLD for linking.
1636 write_to_bazelrc('build:cuda_clang --config=download_clang_use_lld')
1637 write_to_bazelrc('test:cuda_clang --config=download_clang_use_lld')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001638 else:
1639 # Set up which gcc nvcc should use as the host compiler
1640 # No need to set this on Windows
1641 if not is_windows():
1642 set_gcc_host_compiler_path(environ_cp)
1643 set_other_cuda_vars(environ_cp)
Ilya Biryukov9e651e42018-03-22 05:33:42 -07001644 else:
1645 # CUDA not required. Ask whether we should download the clang toolchain and
1646 # use it for the CPU build.
1647 set_tf_download_clang(environ_cp)
1648 if environ_cp.get('TF_DOWNLOAD_CLANG') == '1':
1649 write_to_bazelrc('build --config=download_clang')
1650 write_to_bazelrc('test --config=download_clang')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001651
Wen-Heng (Jack) Chung69d3b8f2018-06-22 23:09:43 -05001652 # SYCL / ROCm / CUDA are mutually exclusive.
1653 # At most 1 GPU platform can be configured.
1654 gpu_platform_count = 0
1655 if environ_cp.get('TF_NEED_OPENCL_SYCL') == '1':
1656 gpu_platform_count += 1
1657 if environ_cp.get('TF_NEED_ROCM') == '1':
1658 gpu_platform_count += 1
1659 if environ_cp.get('TF_NEED_CUDA') == '1':
1660 gpu_platform_count += 1
1661 if gpu_platform_count >= 2:
1662 raise UserInputError('SYCL / CUDA / ROCm are mututally exclusive. '
1663 'At most 1 GPU platform can be configured.')
1664
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001665 set_build_var(environ_cp, 'TF_NEED_MPI', 'MPI', 'with_mpi_support', False)
1666 if environ_cp.get('TF_NEED_MPI') == '1':
1667 set_mpi_home(environ_cp)
1668 set_other_mpi_vars(environ_cp)
1669
1670 set_cc_opt_flags(environ_cp)
Yifei Feng5198cb82018-08-17 13:53:06 -07001671 set_system_libs_flag(environ_cp)
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001672 if is_windows():
1673 set_windows_build_flags(environ_cp)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001674
Anna Ra9a1d5a2018-09-14 12:44:31 -07001675 # Add a config option to build TensorFlow 2.0 API.
1676 write_to_bazelrc('build:v2 --define=tf_api_version=2')
1677
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001678 if get_var(environ_cp, 'TF_SET_ANDROID_WORKSPACE', 'android workspace', False,
1679 ('Would you like to interactively configure ./WORKSPACE for '
1680 'Android builds?'), 'Searching for NDK and SDK installations.',
1681 'Not configuring the WORKSPACE for Android builds.'):
Michael Case51053502018-06-05 17:47:19 -07001682 create_android_ndk_rule(environ_cp)
1683 create_android_sdk_rule(environ_cp)
Austin Anderson6afface2017-12-05 11:59:17 -08001684
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001685 # On Windows, we don't have MKL support and the build is always monolithic.
1686 # So no need to print the following message.
1687 # TODO(pcloudy): remove the following if check when they make sense on Windows
1688 if not is_windows():
1689 print('Preconfigured Bazel build configs. You can use any of the below by '
Yifei Fenged904612018-10-03 14:01:16 -07001690 'adding "--config=<>" to your build command. See .bazelrc for more '
1691 'details.')
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001692 config_info_line('mkl', 'Build with MKL support.')
1693 config_info_line('monolithic', 'Config for mostly static monolithic build.')
Gunhan Gulsoy3da0dff2018-09-26 11:55:50 -07001694 config_info_line('gdr', 'Build with GDR support.')
1695 config_info_line('verbs', 'Build with libverbs support.')
avijit-nervanaf172c522018-09-27 12:57:24 -07001696 config_info_line('ngraph', 'Build with Intel nGraph support.')
Austin Anderson6afface2017-12-05 11:59:17 -08001697
Gunhan Gulsoyffa90fc2018-09-26 01:38:55 -07001698
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001699if __name__ == '__main__':
1700 main()