blob: 9fd2dc2630fafaed423f146c903188055293f536 [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)
Benoit Steiner0dadbfe2018-03-22 18:54:27 -070044_DEFAULT_TENSORRT_PATH_LINUX = '/usr/lib/%s-linux-gnu' % platform.machine()
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070045_TF_OPENCL_VERSION = '1.2'
46_DEFAULT_COMPUTECPP_TOOLKIT_PATH = '/usr/local/computecpp'
Yifei Fengb1d8c592017-11-22 13:42:21 -080047_DEFAULT_TRISYCL_INCLUDE_DIR = '/usr/local/triSYCL/include'
A. Unique TensorFlowerd340f472018-08-30 14:00:41 -070048_SUPPORTED_ANDROID_NDK_VERSIONS = [10, 11, 12, 13, 14, 15, 16]
Austin Anderson6afface2017-12-05 11:59:17 -080049
50_DEFAULT_PROMPT_ASK_ATTEMPTS = 10
51
Shanqing Cai71445712018-03-12 19:33:52 -070052_TF_WORKSPACE_ROOT = os.path.abspath(os.path.dirname(__file__))
53_TF_BAZELRC_FILENAME = '.tf_configure.bazelrc'
54_TF_BAZELRC = os.path.join(_TF_WORKSPACE_ROOT, _TF_BAZELRC_FILENAME)
55_TF_WORKSPACE = os.path.join(_TF_WORKSPACE_ROOT, 'WORKSPACE')
56
Jason Furmanek7c234152018-09-26 04:44:12 +000057NCCL_LIB_PATHS = [
58 "lib64/",
59 "lib/powerpc64le-linux-gnu/",
60 "lib/x86_64-linux-gnu/",
61 ""
62]
Austin Anderson6afface2017-12-05 11:59:17 -080063
64class UserInputError(Exception):
65 pass
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070066
67
68def is_windows():
69 return platform.system() == 'Windows'
70
71
72def is_linux():
73 return platform.system() == 'Linux'
74
75
76def is_macos():
77 return platform.system() == 'Darwin'
78
79
80def is_ppc64le():
81 return platform.machine() == 'ppc64le'
82
83
Jonathan Hseu008910f2017-08-25 14:01:05 -070084def is_cygwin():
85 return platform.system().startswith('CYGWIN_NT')
86
87
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -070088def get_input(question):
89 try:
90 try:
91 answer = raw_input(question)
92 except NameError:
93 answer = input(question) # pylint: disable=bad-builtin
94 except EOFError:
95 answer = ''
96 return answer
97
98
99def symlink_force(target, link_name):
100 """Force symlink, equivalent of 'ln -sf'.
101
102 Args:
103 target: items to link to.
104 link_name: name of the link.
105 """
106 try:
107 os.symlink(target, link_name)
108 except OSError as e:
109 if e.errno == errno.EEXIST:
110 os.remove(link_name)
111 os.symlink(target, link_name)
112 else:
113 raise e
114
115
116def sed_in_place(filename, old, new):
117 """Replace old string with new string in file.
118
119 Args:
120 filename: string for filename.
121 old: string to replace.
122 new: new string to replace to.
123 """
124 with open(filename, 'r') as f:
125 filedata = f.read()
126 newdata = filedata.replace(old, new)
127 with open(filename, 'w') as f:
128 f.write(newdata)
129
130
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700131def write_to_bazelrc(line):
132 with open(_TF_BAZELRC, 'a') as f:
133 f.write(line + '\n')
134
135
136def write_action_env_to_bazelrc(var_name, var):
137 write_to_bazelrc('build --action_env %s="%s"' % (var_name, str(var)))
138
139
Jonathan Hseu008910f2017-08-25 14:01:05 -0700140def run_shell(cmd, allow_non_zero=False):
141 if allow_non_zero:
142 try:
143 output = subprocess.check_output(cmd)
144 except subprocess.CalledProcessError as e:
145 output = e.output
146 else:
147 output = subprocess.check_output(cmd)
148 return output.decode('UTF-8').strip()
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700149
150
151def cygpath(path):
152 """Convert path from posix to windows."""
Martin Wicked57572e2017-09-02 19:21:45 -0700153 return os.path.abspath(path).replace('\\', '/')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700154
155
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700156def get_python_path(environ_cp, python_bin_path):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700157 """Get the python site package paths."""
158 python_paths = []
159 if environ_cp.get('PYTHONPATH'):
160 python_paths = environ_cp.get('PYTHONPATH').split(':')
161 try:
Jonathan Hseu008910f2017-08-25 14:01:05 -0700162 library_paths = run_shell(
163 [python_bin_path, '-c',
Austin Anderson6afface2017-12-05 11:59:17 -0800164 'import site; print("\\n".join(site.getsitepackages()))']).split('\n')
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700165 except subprocess.CalledProcessError:
Jonathan Hseu008910f2017-08-25 14:01:05 -0700166 library_paths = [run_shell(
167 [python_bin_path, '-c',
168 'from distutils.sysconfig import get_python_lib;'
169 'print(get_python_lib())'])]
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700170
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700171 all_paths = set(python_paths + library_paths)
172
173 paths = []
174 for path in all_paths:
175 if os.path.isdir(path):
176 paths.append(path)
177 return paths
178
179
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700180def get_python_major_version(python_bin_path):
181 """Get the python major version."""
Jonathan Hseu008910f2017-08-25 14:01:05 -0700182 return run_shell([python_bin_path, '-c', 'import sys; print(sys.version[0])'])
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700183
184
Gunhan Gulsoyed89a2b2017-09-19 18:36:26 -0700185def setup_python(environ_cp):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700186 """Setup python related env variables."""
187 # Get PYTHON_BIN_PATH, default is the current running python.
188 default_python_bin_path = sys.executable
189 ask_python_bin_path = ('Please specify the location of python. [Default is '
190 '%s]: ') % default_python_bin_path
191 while True:
192 python_bin_path = get_from_env_or_user_or_default(
193 environ_cp, 'PYTHON_BIN_PATH', ask_python_bin_path,
194 default_python_bin_path)
195 # Check if the path is valid
Jonathan Hseu008910f2017-08-25 14:01:05 -0700196 if os.path.isfile(python_bin_path) and os.access(
197 python_bin_path, os.X_OK):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700198 break
199 elif not os.path.exists(python_bin_path):
200 print('Invalid python path: %s cannot be found.' % python_bin_path)
201 else:
202 print('%s is not executable. Is it the python binary?' % python_bin_path)
203 environ_cp['PYTHON_BIN_PATH'] = ''
204
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700205 # Convert python path to Windows style before checking lib and version
Martin Wicked57572e2017-09-02 19:21:45 -0700206 if is_windows() or is_cygwin():
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700207 python_bin_path = cygpath(python_bin_path)
208
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700209 # Get PYTHON_LIB_PATH
210 python_lib_path = environ_cp.get('PYTHON_LIB_PATH')
211 if not python_lib_path:
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700212 python_lib_paths = get_python_path(environ_cp, python_bin_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700213 if environ_cp.get('USE_DEFAULT_PYTHON_LIB_PATH') == '1':
Vijay Vasudevana1fba7f2017-07-28 10:58:56 -0700214 python_lib_path = python_lib_paths[0]
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700215 else:
Jonathan Hseu008910f2017-08-25 14:01:05 -0700216 print('Found possible Python library paths:\n %s' %
217 '\n '.join(python_lib_paths))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700218 default_python_lib_path = python_lib_paths[0]
219 python_lib_path = get_input(
Jonathan Hseu008910f2017-08-25 14:01:05 -0700220 'Please input the desired Python library path to use. '
221 'Default is [%s]\n' % python_lib_paths[0])
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700222 if not python_lib_path:
223 python_lib_path = default_python_lib_path
Vijay Vasudevana1fba7f2017-07-28 10:58:56 -0700224 environ_cp['PYTHON_LIB_PATH'] = python_lib_path
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700225
Andrew Harp6e3e7d12017-08-21 12:10:44 -0700226 python_major_version = get_python_major_version(python_bin_path)
227
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700228 # Convert python path to Windows style before writing into bazel.rc
Martin Wicked57572e2017-09-02 19:21:45 -0700229 if is_windows() or is_cygwin():
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700230 python_lib_path = cygpath(python_lib_path)
231
232 # Set-up env variables used by python_configure.bzl
233 write_action_env_to_bazelrc('PYTHON_BIN_PATH', python_bin_path)
234 write_action_env_to_bazelrc('PYTHON_LIB_PATH', python_lib_path)
Gunhan Gulsoyed89a2b2017-09-19 18:36:26 -0700235 write_to_bazelrc('build --python_path=\"%s"' % python_bin_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700236 environ_cp['PYTHON_BIN_PATH'] = python_bin_path
237
238 # Write tools/python_bin_path.sh
Shanqing Cai71445712018-03-12 19:33:52 -0700239 with open(os.path.join(
240 _TF_WORKSPACE_ROOT, 'tools', 'python_bin_path.sh'), 'w') as f:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700241 f.write('export PYTHON_BIN_PATH="%s"' % python_bin_path)
242
243
Shanqing Cai71445712018-03-12 19:33:52 -0700244def reset_tf_configure_bazelrc(workspace_path):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700245 """Reset file that contains customized config settings."""
246 open(_TF_BAZELRC, 'w').close()
Shanqing Cai71445712018-03-12 19:33:52 -0700247 bazelrc_path = os.path.join(workspace_path, '.bazelrc')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700248
Shanqing Cai71445712018-03-12 19:33:52 -0700249 data = []
250 if os.path.exists(bazelrc_path):
251 with open(bazelrc_path, 'r') as f:
252 data = f.read().splitlines()
253 with open(bazelrc_path, 'w') as f:
254 for l in data:
255 if _TF_BAZELRC_FILENAME in l:
256 continue
257 f.write('%s\n' % l)
258 if is_windows():
259 tf_bazelrc_path = _TF_BAZELRC.replace("\\", "/")
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700260 else:
Shanqing Cai71445712018-03-12 19:33:52 -0700261 tf_bazelrc_path = _TF_BAZELRC
262 f.write('import %s\n' % tf_bazelrc_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700263
264
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 """
Shanqing Cai71445712018-03-12 19:33:52 -0700270 makefile_download_dir = os.path.join(
271 _TF_WORKSPACE_ROOT, 'tensorflow', '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'
339 'Current value is %s.' % (
340 var_name, ', '.join(true_strings), ', '.join(false_strings),
341 var))
342
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700343 while var is None:
344 user_input_origin = get_input(question)
345 user_input = user_input_origin.strip().lower()
346 if user_input == 'y':
347 print(yes_reply)
348 var = True
349 elif user_input == 'n':
350 print(no_reply)
351 var = False
352 elif not user_input:
353 if enabled_by_default:
354 print(yes_reply)
355 var = True
356 else:
357 print(no_reply)
358 var = False
359 else:
360 print('Invalid selection: %s' % user_input_origin)
361 return var
362
363
364def set_build_var(environ_cp, var_name, query_item, option_name,
Michael Case98850a52017-09-14 13:35:57 -0700365 enabled_by_default, bazel_config_name=None):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700366 """Set if query_item will be enabled for the build.
367
368 Ask user if query_item will be enabled. Default is used if no input is given.
369 Set subprocess environment variable and write to .bazelrc if enabled.
370
371 Args:
372 environ_cp: copy of the os.environ.
373 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
374 query_item: string for feature related to the variable, e.g. "Hadoop File
375 System".
376 option_name: string for option to define in .bazelrc.
377 enabled_by_default: boolean for default behavior.
Michael Case98850a52017-09-14 13:35:57 -0700378 bazel_config_name: Name for Bazel --config argument to enable build feature.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700379 """
380
381 var = str(int(get_var(environ_cp, var_name, query_item, enabled_by_default)))
382 environ_cp[var_name] = var
383 if var == '1':
384 write_to_bazelrc('build --define %s=true' % option_name)
Michael Case98850a52017-09-14 13:35:57 -0700385 elif bazel_config_name is not None:
386 # TODO(mikecase): Migrate all users of configure.py to use --config Bazel
387 # options and not to set build configs through environment variables.
388 write_to_bazelrc('build:%s --define %s=true'
389 % (bazel_config_name, option_name))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700390
391
392def set_action_env_var(environ_cp,
393 var_name,
394 query_item,
395 enabled_by_default,
396 question=None,
397 yes_reply=None,
398 no_reply=None):
399 """Set boolean action_env variable.
400
401 Ask user if query_item will be enabled. Default is used if no input is given.
402 Set environment variable and write to .bazelrc.
403
404 Args:
405 environ_cp: copy of the os.environ.
406 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
407 query_item: string for feature related to the variable, e.g. "Hadoop File
408 System".
409 enabled_by_default: boolean for default behavior.
410 question: optional string for how to ask for user input.
Michael Cased90054e2018-02-07 14:36:00 -0800411 yes_reply: optional string for reply when feature is enabled.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700412 no_reply: optional string for reply when feature is disabled.
413 """
414 var = int(
415 get_var(environ_cp, var_name, query_item, enabled_by_default, question,
416 yes_reply, no_reply))
417
418 write_action_env_to_bazelrc(var_name, var)
419 environ_cp[var_name] = str(var)
420
421
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700422def convert_version_to_int(version):
423 """Convert a version number to a integer that can be used to compare.
424
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700425 Version strings of the form X.YZ and X.Y.Z-xxxxx are supported. The
426 'xxxxx' part, for instance 'homebrew' on OS/X, is ignored.
427
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700428 Args:
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700429 version: a version to be converted
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700430
431 Returns:
432 An integer if converted successfully, otherwise return None.
433 """
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700434 version = version.split('-')[0]
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700435 version_segments = version.split('.')
436 for seg in version_segments:
437 if not seg.isdigit():
438 return None
439
440 version_str = ''.join(['%03d' % int(seg) for seg in version_segments])
441 return int(version_str)
442
443
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700444def check_bazel_version(min_version):
Yifei Fengdce9a492018-02-22 14:24:57 -0800445 """Check installed bazel version is at least min_version.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700446
447 Args:
448 min_version: string for minimum bazel version.
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700449
450 Returns:
451 The bazel version detected.
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700452 """
Jonathan Hseu008910f2017-08-25 14:01:05 -0700453 if which('bazel') is None:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700454 print('Cannot find bazel. Please install bazel.')
455 sys.exit(0)
Shanqing Cai71445712018-03-12 19:33:52 -0700456 curr_version = run_shell(['bazel', '--batch', '--bazelrc=/dev/null', 'version'])
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700457
458 for line in curr_version.split('\n'):
459 if 'Build label: ' in line:
460 curr_version = line.split('Build label: ')[1]
461 break
462
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700463 min_version_int = convert_version_to_int(min_version)
464 curr_version_int = convert_version_to_int(curr_version)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700465
466 # Check if current bazel version can be detected properly.
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700467 if not curr_version_int:
468 print('WARNING: current bazel installation is not a release version.')
469 print('Make sure you are running at least bazel %s' % min_version)
470 return curr_version
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700471
Michael Cased94271a2017-08-22 17:26:52 -0700472 print('You have bazel %s installed.' % curr_version)
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -0700473
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700474 if curr_version_int < min_version_int:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700475 print('Please upgrade your bazel installation to version %s or higher to '
476 'build TensorFlow!' % min_version)
477 sys.exit(0)
A. Unique TensorFlower6252d292017-08-04 00:52:34 -0700478 return curr_version
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700479
480
481def set_cc_opt_flags(environ_cp):
482 """Set up architecture-dependent optimization flags.
483
484 Also append CC optimization flags to bazel.rc..
485
486 Args:
487 environ_cp: copy of the os.environ.
488 """
489 if is_ppc64le():
490 # gcc on ppc64le does not support -march, use mcpu instead
491 default_cc_opt_flags = '-mcpu=native'
A. Unique TensorFlower1bba94a2018-04-04 15:45:20 -0700492 elif is_windows():
493 default_cc_opt_flags = '/arch:AVX'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700494 else:
495 default_cc_opt_flags = '-march=native'
496 question = ('Please specify optimization flags to use during compilation when'
497 ' bazel option "--config=opt" is specified [Default is %s]: '
498 ) % default_cc_opt_flags
499 cc_opt_flags = get_from_env_or_user_or_default(environ_cp, 'CC_OPT_FLAGS',
500 question, default_cc_opt_flags)
501 for opt in cc_opt_flags.split():
Michael Case00177422017-11-10 13:14:03 -0800502 write_to_bazelrc('build:opt --copt=%s' % opt)
503 # It should be safe on the same build host.
A. Unique TensorFlower1bba94a2018-04-04 15:45:20 -0700504 if not is_ppc64le() and not is_windows():
Shanqing Cai71445712018-03-12 19:33:52 -0700505 write_to_bazelrc('build:opt --host_copt=-march=native')
Michael Casebb3355d2017-11-09 08:46:31 -0800506 write_to_bazelrc('build:opt --define with_default_optimizations=true')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700507
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700508def set_tf_cuda_clang(environ_cp):
509 """set TF_CUDA_CLANG action_env.
510
511 Args:
512 environ_cp: copy of the os.environ.
513 """
514 question = 'Do you want to use clang as CUDA compiler?'
515 yes_reply = 'Clang will be used as CUDA compiler.'
516 no_reply = 'nvcc will be used as CUDA compiler.'
517 set_action_env_var(
518 environ_cp,
519 'TF_CUDA_CLANG',
520 None,
521 False,
522 question=question,
523 yes_reply=yes_reply,
524 no_reply=no_reply)
525
526
A. Unique TensorFlowerfd29e952017-12-22 03:07:51 -0800527def set_tf_download_clang(environ_cp):
528 """Set TF_DOWNLOAD_CLANG action_env."""
Ilya Biryukov9e651e42018-03-22 05:33:42 -0700529 question = 'Do you wish to download a fresh release of clang? (Experimental)'
A. Unique TensorFlowerfd29e952017-12-22 03:07:51 -0800530 yes_reply = 'Clang will be downloaded and used to compile tensorflow.'
531 no_reply = 'Clang will not be downloaded.'
532 set_action_env_var(
533 environ_cp,
534 'TF_DOWNLOAD_CLANG',
535 None,
536 False,
537 question=question,
538 yes_reply=yes_reply,
539 no_reply=no_reply)
540
541
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700542def get_from_env_or_user_or_default(environ_cp, var_name, ask_for_var,
543 var_default):
544 """Get var_name either from env, or user or default.
545
546 If var_name has been set as environment variable, use the preset value, else
547 ask for user input. If no input is provided, the default is used.
548
549 Args:
550 environ_cp: copy of the os.environ.
551 var_name: string for name of environment variable, e.g. "TF_NEED_HDFS".
552 ask_for_var: string for how to ask for user input.
553 var_default: default value string.
554
555 Returns:
556 string value for var_name
557 """
558 var = environ_cp.get(var_name)
559 if not var:
560 var = get_input(ask_for_var)
Michael Cased94271a2017-08-22 17:26:52 -0700561 print('\n')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700562 if not var:
563 var = var_default
564 return var
565
566
567def set_clang_cuda_compiler_path(environ_cp):
568 """Set CLANG_CUDA_COMPILER_PATH."""
Jonathan Hseu008910f2017-08-25 14:01:05 -0700569 default_clang_path = which('clang') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700570 ask_clang_path = ('Please specify which clang should be used as device and '
571 'host compiler. [Default is %s]: ') % default_clang_path
572
573 while True:
574 clang_cuda_compiler_path = get_from_env_or_user_or_default(
575 environ_cp, 'CLANG_CUDA_COMPILER_PATH', ask_clang_path,
576 default_clang_path)
577 if os.path.exists(clang_cuda_compiler_path):
578 break
579
580 # Reset and retry
581 print('Invalid clang path: %s cannot be found.' % clang_cuda_compiler_path)
582 environ_cp['CLANG_CUDA_COMPILER_PATH'] = ''
583
584 # Set CLANG_CUDA_COMPILER_PATH
585 environ_cp['CLANG_CUDA_COMPILER_PATH'] = clang_cuda_compiler_path
586 write_action_env_to_bazelrc('CLANG_CUDA_COMPILER_PATH',
587 clang_cuda_compiler_path)
588
589
Austin Anderson6afface2017-12-05 11:59:17 -0800590def prompt_loop_or_load_from_env(
591 environ_cp,
592 var_name,
593 var_default,
594 ask_for_var,
595 check_success,
596 error_msg,
597 suppress_default_error=False,
598 n_ask_attempts=_DEFAULT_PROMPT_ASK_ATTEMPTS
599):
600 """Loop over user prompts for an ENV param until receiving a valid response.
601
602 For the env param var_name, read from the environment or verify user input
603 until receiving valid input. When done, set var_name in the environ_cp to its
604 new value.
605
606 Args:
607 environ_cp: (Dict) copy of the os.environ.
608 var_name: (String) string for name of environment variable, e.g. "TF_MYVAR".
609 var_default: (String) default value string.
610 ask_for_var: (String) string for how to ask for user input.
611 check_success: (Function) function that takes one argument and returns a
612 boolean. Should return True if the value provided is considered valid. May
613 contain a complex error message if error_msg does not provide enough
614 information. In that case, set suppress_default_error to True.
615 error_msg: (String) String with one and only one '%s'. Formatted with each
616 invalid response upon check_success(input) failure.
617 suppress_default_error: (Bool) Suppress the above error message in favor of
618 one from the check_success function.
619 n_ask_attempts: (Integer) Number of times to query for valid input before
620 raising an error and quitting.
621
622 Returns:
623 [String] The value of var_name after querying for input.
624
625 Raises:
626 UserInputError: if a query has been attempted n_ask_attempts times without
Frank Chenc4ef9272018-01-10 11:36:52 -0800627 success, assume that the user has made a scripting error, and will
628 continue to provide invalid input. Raise the error to avoid infinitely
629 looping.
Austin Anderson6afface2017-12-05 11:59:17 -0800630 """
631 default = environ_cp.get(var_name) or var_default
632 full_query = '%s [Default is %s]: ' % (
633 ask_for_var,
634 default,
635 )
636
637 for _ in range(n_ask_attempts):
638 val = get_from_env_or_user_or_default(environ_cp,
639 var_name,
640 full_query,
641 default)
642 if check_success(val):
643 break
644 if not suppress_default_error:
645 print(error_msg % val)
646 environ_cp[var_name] = ''
647 else:
648 raise UserInputError('Invalid %s setting was provided %d times in a row. '
649 'Assuming to be a scripting mistake.' %
650 (var_name, n_ask_attempts))
651
652 environ_cp[var_name] = val
653 return val
654
655
656def create_android_ndk_rule(environ_cp):
657 """Set ANDROID_NDK_HOME and write Android NDK WORKSPACE rule."""
658 if is_windows() or is_cygwin():
659 default_ndk_path = cygpath('%s/Android/Sdk/ndk-bundle' %
660 environ_cp['APPDATA'])
661 elif is_macos():
662 default_ndk_path = '%s/library/Android/Sdk/ndk-bundle' % environ_cp['HOME']
663 else:
664 default_ndk_path = '%s/Android/Sdk/ndk-bundle' % environ_cp['HOME']
665
666 def valid_ndk_path(path):
667 return (os.path.exists(path) and
668 os.path.exists(os.path.join(path, 'source.properties')))
669
670 android_ndk_home_path = prompt_loop_or_load_from_env(
671 environ_cp,
672 var_name='ANDROID_NDK_HOME',
673 var_default=default_ndk_path,
674 ask_for_var='Please specify the home path of the Android NDK to use.',
675 check_success=valid_ndk_path,
676 error_msg=('The path %s or its child file "source.properties" '
677 'does not exist.')
678 )
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):
712 return os.path.exists(os.path.join(android_sdk_home_path,
713 'platforms',
714 'android-' + api_level))
715
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):
729 return os.path.exists(os.path.join(android_sdk_home_path,
730 'build-tools',
731 version))
732
733 android_build_tools_version = prompt_loop_or_load_from_env(
734 environ_cp,
735 var_name='ANDROID_BUILD_TOOLS_VERSION',
736 var_default=versions[-1],
737 ask_for_var=('Please specify an Android build tools version to use. '
738 '[Available versions: %s]') % versions,
739 check_success=valid_build_tools,
740 error_msg=('The selected SDK does not have build-tools version %s '
741 'available.'))
742
Michael Case51053502018-06-05 17:47:19 -0700743 write_action_env_to_bazelrc('ANDROID_BUILD_TOOLS_VERSION',
744 android_build_tools_version)
745 write_action_env_to_bazelrc('ANDROID_SDK_API_LEVEL',
746 android_api_level)
747 write_action_env_to_bazelrc('ANDROID_SDK_HOME',
748 android_sdk_home_path)
Austin Anderson6afface2017-12-05 11:59:17 -0800749
750
751def check_ndk_level(android_ndk_home_path):
752 """Check the revision number of an Android NDK path."""
753 properties_path = '%s/source.properties' % android_ndk_home_path
754 if is_windows() or is_cygwin():
755 properties_path = cygpath(properties_path)
756 with open(properties_path, 'r') as f:
757 filedata = f.read()
758
759 revision = re.search(r'Pkg.Revision = (\d+)', filedata)
760 if revision:
Michael Case51053502018-06-05 17:47:19 -0700761 ndk_api_level = revision.group(1)
762 else:
763 raise Exception('Unable to parse NDK revision.')
764 if int(ndk_api_level) not in _SUPPORTED_ANDROID_NDK_VERSIONS:
765 print('WARNING: The API level of the NDK in %s is %s, which is not '
766 'supported by Bazel (officially supported versions: %s). Please use '
767 'another version. Compiling Android targets may result in confusing '
768 'errors.\n' % (android_ndk_home_path, ndk_api_level,
769 _SUPPORTED_ANDROID_NDK_VERSIONS))
770 return ndk_api_level
Austin Anderson6afface2017-12-05 11:59:17 -0800771
772
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700773def set_gcc_host_compiler_path(environ_cp):
774 """Set GCC_HOST_COMPILER_PATH."""
Jonathan Hseu008910f2017-08-25 14:01:05 -0700775 default_gcc_host_compiler_path = which('gcc') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700776 cuda_bin_symlink = '%s/bin/gcc' % environ_cp.get('CUDA_TOOLKIT_PATH')
777
778 if os.path.islink(cuda_bin_symlink):
779 # os.readlink is only available in linux
Jonathan Hseu008910f2017-08-25 14:01:05 -0700780 default_gcc_host_compiler_path = os.path.realpath(cuda_bin_symlink)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700781
Austin Anderson6afface2017-12-05 11:59:17 -0800782 gcc_host_compiler_path = prompt_loop_or_load_from_env(
783 environ_cp,
784 var_name='GCC_HOST_COMPILER_PATH',
785 var_default=default_gcc_host_compiler_path,
786 ask_for_var=
787 'Please specify which gcc should be used by nvcc as the host compiler.',
788 check_success=os.path.exists,
789 error_msg='Invalid gcc path. %s cannot be found.',
790 )
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700791
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700792 write_action_env_to_bazelrc('GCC_HOST_COMPILER_PATH', gcc_host_compiler_path)
793
794
Ankur Taly0e6f39d2018-02-16 18:22:55 -0800795def reformat_version_sequence(version_str, sequence_count):
796 """Reformat the version string to have the given number of sequences.
797
798 For example:
799 Given (7, 2) -> 7.0
800 (7.0.1, 2) -> 7.0
801 (5, 1) -> 5
802 (5.0.3.2, 1) -> 5
803
804 Args:
805 version_str: String, the version string.
806 sequence_count: int, an integer.
807 Returns:
808 string, reformatted version string.
809 """
810 v = version_str.split('.')
811 if len(v) < sequence_count:
812 v = v + (['0'] * (sequence_count - len(v)))
813
814 return '.'.join(v[:sequence_count])
815
816
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700817def set_tf_cuda_version(environ_cp):
818 """Set CUDA_TOOLKIT_PATH and TF_CUDA_VERSION."""
819 ask_cuda_version = (
A. Unique TensorFlowerb15500b2018-05-08 12:04:38 -0700820 'Please specify the CUDA SDK version you want to use. '
821 '[Leave empty to default to CUDA %s]: ') % _DEFAULT_CUDA_VERSION
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700822
Austin Andersonf9a88f82017-12-13 11:49:40 -0800823 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700824 # Configure the Cuda SDK version to use.
825 tf_cuda_version = get_from_env_or_user_or_default(
826 environ_cp, 'TF_CUDA_VERSION', ask_cuda_version, _DEFAULT_CUDA_VERSION)
Ankur Taly0e6f39d2018-02-16 18:22:55 -0800827 tf_cuda_version = reformat_version_sequence(str(tf_cuda_version), 2)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700828
829 # Find out where the CUDA toolkit is installed
830 default_cuda_path = _DEFAULT_CUDA_PATH
Martin Wicked57572e2017-09-02 19:21:45 -0700831 if is_windows() or is_cygwin():
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700832 default_cuda_path = cygpath(
833 environ_cp.get('CUDA_PATH', _DEFAULT_CUDA_PATH_WIN))
834 elif is_linux():
835 # If the default doesn't exist, try an alternative default.
836 if (not os.path.exists(default_cuda_path)
837 ) and os.path.exists(_DEFAULT_CUDA_PATH_LINUX):
838 default_cuda_path = _DEFAULT_CUDA_PATH_LINUX
839 ask_cuda_path = ('Please specify the location where CUDA %s toolkit is'
840 ' installed. Refer to README.md for more details. '
841 '[Default is %s]: ') % (tf_cuda_version, default_cuda_path)
842 cuda_toolkit_path = get_from_env_or_user_or_default(
843 environ_cp, 'CUDA_TOOLKIT_PATH', ask_cuda_path, default_cuda_path)
A. Unique TensorFlower02f17fe2018-07-07 06:59:19 -0700844 if is_windows() or is_cygwin():
845 cuda_toolkit_path = cygpath(cuda_toolkit_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700846
847 if is_windows():
Niall Moranb7d97e82018-08-09 00:29:49 +0100848 cuda_rt_lib_paths = ['lib/x64/cudart.lib']
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700849 elif is_linux():
Niall Moranb7d97e82018-08-09 00:29:49 +0100850 cuda_rt_lib_paths = ['%s/libcudart.so.%s' % (x, tf_cuda_version)
851 for x in ['lib64', 'lib/x86_64-linux-gnu']]
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700852 elif is_macos():
Niall Moranb7d97e82018-08-09 00:29:49 +0100853 cuda_rt_lib_paths = ['lib/libcudart.%s.dylib' % tf_cuda_version]
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700854
Niall Moranb7d97e82018-08-09 00:29:49 +0100855 cuda_toolkit_paths_full = [os.path.join(cuda_toolkit_path, x) for x in cuda_rt_lib_paths]
856 if any([os.path.exists(x) for x in cuda_toolkit_paths_full]):
Yifei Feng5198cb82018-08-17 13:53:06 -0700857 break
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700858
859 # Reset and retry
860 print('Invalid path to CUDA %s toolkit. %s cannot be found' %
hellcom9a13fc32018-09-12 10:58:24 +0300861 (tf_cuda_version, cuda_toolkit_paths_full))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700862 environ_cp['TF_CUDA_VERSION'] = ''
863 environ_cp['CUDA_TOOLKIT_PATH'] = ''
864
Austin Andersonf9a88f82017-12-13 11:49:40 -0800865 else:
866 raise UserInputError('Invalid TF_CUDA_SETTING setting was provided %d '
867 'times in a row. Assuming to be a scripting mistake.' %
868 _DEFAULT_PROMPT_ASK_ATTEMPTS)
869
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700870 # Set CUDA_TOOLKIT_PATH and TF_CUDA_VERSION
871 environ_cp['CUDA_TOOLKIT_PATH'] = cuda_toolkit_path
872 write_action_env_to_bazelrc('CUDA_TOOLKIT_PATH', cuda_toolkit_path)
873 environ_cp['TF_CUDA_VERSION'] = tf_cuda_version
874 write_action_env_to_bazelrc('TF_CUDA_VERSION', tf_cuda_version)
875
876
Yifei Fengb1d8c592017-11-22 13:42:21 -0800877def set_tf_cudnn_version(environ_cp):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700878 """Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION."""
879 ask_cudnn_version = (
Jonathan Hseu008910f2017-08-25 14:01:05 -0700880 'Please specify the cuDNN version you want to use. '
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700881 '[Leave empty to default to cuDNN %s.0]: ') % _DEFAULT_CUDNN_VERSION
882
Austin Andersonf9a88f82017-12-13 11:49:40 -0800883 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700884 tf_cudnn_version = get_from_env_or_user_or_default(
885 environ_cp, 'TF_CUDNN_VERSION', ask_cudnn_version,
886 _DEFAULT_CUDNN_VERSION)
Ankur Taly0e6f39d2018-02-16 18:22:55 -0800887 tf_cudnn_version = reformat_version_sequence(str(tf_cudnn_version), 1)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700888
889 default_cudnn_path = environ_cp.get('CUDA_TOOLKIT_PATH')
890 ask_cudnn_path = (r'Please specify the location where cuDNN %s library is '
891 'installed. Refer to README.md for more details. [Default'
A. Unique TensorFlower1b212352018-07-19 13:48:50 -0700892 ' is %s]: ') % (tf_cudnn_version, default_cudnn_path)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700893 cudnn_install_path = get_from_env_or_user_or_default(
894 environ_cp, 'CUDNN_INSTALL_PATH', ask_cudnn_path, default_cudnn_path)
895
896 # Result returned from "read" will be used unexpanded. That make "~"
897 # unusable. Going through one more level of expansion to handle that.
898 cudnn_install_path = os.path.realpath(
899 os.path.expanduser(cudnn_install_path))
Martin Wicked57572e2017-09-02 19:21:45 -0700900 if is_windows() or is_cygwin():
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700901 cudnn_install_path = cygpath(cudnn_install_path)
902
903 if is_windows():
904 cuda_dnn_lib_path = 'lib/x64/cudnn.lib'
905 cuda_dnn_lib_alt_path = 'lib/x64/cudnn.lib'
906 elif is_linux():
907 cuda_dnn_lib_path = 'lib64/libcudnn.so.%s' % tf_cudnn_version
908 cuda_dnn_lib_alt_path = 'libcudnn.so.%s' % tf_cudnn_version
909 elif is_macos():
910 cuda_dnn_lib_path = 'lib/libcudnn.%s.dylib' % tf_cudnn_version
911 cuda_dnn_lib_alt_path = 'libcudnn.%s.dylib' % tf_cudnn_version
912
913 cuda_dnn_lib_path_full = os.path.join(cudnn_install_path, cuda_dnn_lib_path)
914 cuda_dnn_lib_alt_path_full = os.path.join(cudnn_install_path,
915 cuda_dnn_lib_alt_path)
916 if os.path.exists(cuda_dnn_lib_path_full) or os.path.exists(
917 cuda_dnn_lib_alt_path_full):
918 break
919
920 # Try another alternative for Linux
921 if is_linux():
Jonathan Hseu008910f2017-08-25 14:01:05 -0700922 ldconfig_bin = which('ldconfig') or '/sbin/ldconfig'
923 cudnn_path_from_ldconfig = run_shell([ldconfig_bin, '-p'])
924 cudnn_path_from_ldconfig = re.search('.*libcudnn.so .* => (.*)',
A. Unique TensorFlowere7223582017-09-06 17:57:04 -0700925 cudnn_path_from_ldconfig)
926 if cudnn_path_from_ldconfig:
927 cudnn_path_from_ldconfig = cudnn_path_from_ldconfig.group(1)
928 if os.path.exists('%s.%s' % (cudnn_path_from_ldconfig,
929 tf_cudnn_version)):
930 cudnn_install_path = os.path.dirname(cudnn_path_from_ldconfig)
931 break
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700932
933 # Reset and Retry
934 print(
935 'Invalid path to cuDNN %s toolkit. None of the following files can be '
936 'found:' % tf_cudnn_version)
937 print(cuda_dnn_lib_path_full)
938 print(cuda_dnn_lib_alt_path_full)
939 if is_linux():
940 print('%s.%s' % (cudnn_path_from_ldconfig, tf_cudnn_version))
941
942 environ_cp['TF_CUDNN_VERSION'] = ''
Austin Andersonf9a88f82017-12-13 11:49:40 -0800943 else:
944 raise UserInputError('Invalid TF_CUDNN setting was provided %d '
945 'times in a row. Assuming to be a scripting mistake.' %
946 _DEFAULT_PROMPT_ASK_ATTEMPTS)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -0700947
948 # Set CUDNN_INSTALL_PATH and TF_CUDNN_VERSION
949 environ_cp['CUDNN_INSTALL_PATH'] = cudnn_install_path
950 write_action_env_to_bazelrc('CUDNN_INSTALL_PATH', cudnn_install_path)
951 environ_cp['TF_CUDNN_VERSION'] = tf_cudnn_version
952 write_action_env_to_bazelrc('TF_CUDNN_VERSION', tf_cudnn_version)
953
954
Mingxing Tan1e7b0e42018-06-28 19:13:20 -0700955def is_cuda_compatible(lib, cuda_ver, cudnn_ver):
956 """Check compatibility between given library and cudnn/cudart libraries."""
957 ldd_bin = which('ldd') or '/usr/bin/ldd'
958 ldd_out = run_shell([ldd_bin, lib], True)
959 ldd_out = ldd_out.split(os.linesep)
960 cudnn_pattern = re.compile('.*libcudnn.so\\.?(.*) =>.*$')
961 cuda_pattern = re.compile('.*libcudart.so\\.?(.*) =>.*$')
962 cudnn = None
963 cudart = None
964 cudnn_ok = True # assume no cudnn dependency by default
965 cuda_ok = True # assume no cuda dependency by default
966 for line in ldd_out:
967 if 'libcudnn.so' in line:
968 cudnn = cudnn_pattern.search(line)
969 cudnn_ok = False
970 elif 'libcudart.so' in line:
971 cudart = cuda_pattern.search(line)
972 cuda_ok = False
973 if cudnn and len(cudnn.group(1)):
974 cudnn = convert_version_to_int(cudnn.group(1))
975 if cudart and len(cudart.group(1)):
976 cudart = convert_version_to_int(cudart.group(1))
977 if cudnn is not None:
978 cudnn_ok = (cudnn == cudnn_ver)
979 if cudart is not None:
980 cuda_ok = (cudart == cuda_ver)
981 return cudnn_ok and cuda_ok
982
983
Guangda Lai76f69382018-01-25 23:59:19 -0800984def set_tf_tensorrt_install_path(environ_cp):
985 """Set TENSORRT_INSTALL_PATH and TF_TENSORRT_VERSION.
986
987 Adapted from code contributed by Sami Kama (https://github.com/samikama).
988
989 Args:
990 environ_cp: copy of the os.environ.
991
992 Raises:
993 ValueError: if this method was called under non-Linux platform.
994 UserInputError: if user has provided invalid input multiple times.
995 """
996 if not is_linux():
997 raise ValueError('Currently TensorRT is only supported on Linux platform.')
998
999 # Ask user whether to add TensorRT support.
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001000 if str(int(get_var(environ_cp, 'TF_NEED_TENSORRT', 'TensorRT',
1001 False))) != '1':
Guangda Lai76f69382018-01-25 23:59:19 -08001002 return
1003
1004 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
1005 ask_tensorrt_path = (r'Please specify the location where TensorRT is '
1006 'installed. [Default is %s]:') % (
1007 _DEFAULT_TENSORRT_PATH_LINUX)
1008 trt_install_path = get_from_env_or_user_or_default(
1009 environ_cp, 'TENSORRT_INSTALL_PATH', ask_tensorrt_path,
1010 _DEFAULT_TENSORRT_PATH_LINUX)
1011
1012 # Result returned from "read" will be used unexpanded. That make "~"
1013 # unusable. Going through one more level of expansion to handle that.
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001014 trt_install_path = os.path.realpath(os.path.expanduser(trt_install_path))
Guangda Lai76f69382018-01-25 23:59:19 -08001015
1016 def find_libs(search_path):
1017 """Search for libnvinfer.so in "search_path"."""
1018 fl = set()
1019 if os.path.exists(search_path) and os.path.isdir(search_path):
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001020 fl.update([
1021 os.path.realpath(os.path.join(search_path, x))
1022 for x in os.listdir(search_path)
1023 if 'libnvinfer.so' in x
1024 ])
Guangda Lai76f69382018-01-25 23:59:19 -08001025 return fl
1026
1027 possible_files = find_libs(trt_install_path)
1028 possible_files.update(find_libs(os.path.join(trt_install_path, 'lib')))
1029 possible_files.update(find_libs(os.path.join(trt_install_path, 'lib64')))
Guangda Lai76f69382018-01-25 23:59:19 -08001030 cuda_ver = convert_version_to_int(environ_cp['TF_CUDA_VERSION'])
1031 cudnn_ver = convert_version_to_int(environ_cp['TF_CUDNN_VERSION'])
1032 nvinfer_pattern = re.compile('.*libnvinfer.so.?(.*)$')
1033 highest_ver = [0, None, None]
1034
1035 for lib_file in possible_files:
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001036 if is_cuda_compatible(lib_file, cuda_ver, cudnn_ver):
Jacques Pienaar2d0531d2018-03-21 12:07:51 -07001037 matches = nvinfer_pattern.search(lib_file)
1038 if len(matches.groups()) == 0:
1039 continue
1040 ver_str = matches.group(1)
Guangda Lai76f69382018-01-25 23:59:19 -08001041 ver = convert_version_to_int(ver_str) if len(ver_str) else 0
1042 if ver > highest_ver[0]:
1043 highest_ver = [ver, ver_str, lib_file]
1044 if highest_ver[1] is not None:
1045 trt_install_path = os.path.dirname(highest_ver[2])
1046 tf_tensorrt_version = highest_ver[1]
1047 break
1048
1049 # Try another alternative from ldconfig.
1050 ldconfig_bin = which('ldconfig') or '/sbin/ldconfig'
1051 ldconfig_output = run_shell([ldconfig_bin, '-p'])
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001052 search_result = re.search('.*libnvinfer.so\\.?([0-9.]*).* => (.*)',
1053 ldconfig_output)
Guangda Lai76f69382018-01-25 23:59:19 -08001054 if search_result:
1055 libnvinfer_path_from_ldconfig = search_result.group(2)
1056 if os.path.exists(libnvinfer_path_from_ldconfig):
Mingxing Tan1e7b0e42018-06-28 19:13:20 -07001057 if is_cuda_compatible(libnvinfer_path_from_ldconfig, cuda_ver,
1058 cudnn_ver):
Guangda Lai76f69382018-01-25 23:59:19 -08001059 trt_install_path = os.path.dirname(libnvinfer_path_from_ldconfig)
1060 tf_tensorrt_version = search_result.group(1)
1061 break
1062
1063 # Reset and Retry
Yifei Fengdce9a492018-02-22 14:24:57 -08001064 if possible_files:
1065 print('TensorRT libraries found in one the following directories',
1066 'are not compatible with selected cuda and cudnn installations')
1067 print(trt_install_path)
1068 print(os.path.join(trt_install_path, 'lib'))
1069 print(os.path.join(trt_install_path, 'lib64'))
1070 if search_result:
1071 print(libnvinfer_path_from_ldconfig)
1072 else:
1073 print(
1074 'Invalid path to TensorRT. None of the following files can be found:')
1075 print(trt_install_path)
1076 print(os.path.join(trt_install_path, 'lib'))
1077 print(os.path.join(trt_install_path, 'lib64'))
1078 if search_result:
1079 print(libnvinfer_path_from_ldconfig)
Guangda Lai76f69382018-01-25 23:59:19 -08001080
1081 else:
1082 raise UserInputError('Invalid TF_TENSORRT setting was provided %d '
1083 'times in a row. Assuming to be a scripting mistake.' %
1084 _DEFAULT_PROMPT_ASK_ATTEMPTS)
1085
1086 # Set TENSORRT_INSTALL_PATH and TF_TENSORRT_VERSION
1087 environ_cp['TENSORRT_INSTALL_PATH'] = trt_install_path
1088 write_action_env_to_bazelrc('TENSORRT_INSTALL_PATH', trt_install_path)
1089 environ_cp['TF_TENSORRT_VERSION'] = tf_tensorrt_version
1090 write_action_env_to_bazelrc('TF_TENSORRT_VERSION', tf_tensorrt_version)
1091
1092
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001093def set_tf_nccl_install_path(environ_cp):
Jason Furmanek7c234152018-09-26 04:44:12 +00001094 """Set NCCL_INSTALL_PATH, NCCL_HDR_PATH and TF_NCCL_VERSION.
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001095
1096 Args:
1097 environ_cp: copy of the os.environ.
1098
1099 Raises:
1100 ValueError: if this method was called under non-Linux platform.
1101 UserInputError: if user has provided invalid input multiple times.
1102 """
1103 if not is_linux():
1104 raise ValueError('Currently NCCL is only supported on Linux platforms.')
1105
1106 ask_nccl_version = (
Smit Hinsu63e6b9b2018-07-13 12:46:24 -07001107 'Please specify the NCCL version you want to use. If NCCL %s is not '
1108 'installed, then you can use version 1.3 that can be fetched '
1109 'automatically but it may have worse performance with multiple GPUs. '
1110 '[Default is %s]: ') % (_DEFAULT_NCCL_VERSION, _DEFAULT_NCCL_VERSION)
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001111
1112 for _ in range(_DEFAULT_PROMPT_ASK_ATTEMPTS):
1113 tf_nccl_version = get_from_env_or_user_or_default(
1114 environ_cp, 'TF_NCCL_VERSION', ask_nccl_version, _DEFAULT_NCCL_VERSION)
1115 tf_nccl_version = reformat_version_sequence(str(tf_nccl_version), 1)
1116
1117 if tf_nccl_version == '1':
1118 break # No need to get install path, NCCL 1 is a GitHub repo.
1119
Jason Furmanek7c234152018-09-26 04:44:12 +00001120 # Look with ldconfig first if we can find the library in paths
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001121 # like /usr/lib/x86_64-linux-gnu and the header file in the corresponding
1122 # include directory. This is where the NCCL .deb packages install them.
Jason Furmanek7c234152018-09-26 04:44:12 +00001123
1124 # First check to see if NCCL is in the ldconfig.
1125 # If its found, use that location.
1126 if is_linux():
1127 ldconfig_bin = which('ldconfig') or '/sbin/ldconfig'
1128 nccl2_path_from_ldconfig = run_shell([ldconfig_bin, '-p'])
1129 nccl2_path_from_ldconfig = re.search('.*libnccl.so .* => (.*)',
1130 nccl2_path_from_ldconfig)
1131 if nccl2_path_from_ldconfig:
1132 nccl2_path_from_ldconfig = nccl2_path_from_ldconfig.group(1)
1133 if os.path.exists('%s.%s' % (nccl2_path_from_ldconfig, tf_nccl_version)):
1134 nccl_install_path = os.path.dirname(nccl2_path_from_ldconfig)
1135 print('NCCL libraries found in ' + nccl2_path_from_ldconfig)
1136
1137 # Check if this is the main system lib location
1138 if re.search('.*linux-gnu', nccl_install_path):
1139 trunc_nccl_install_path = "/usr"
1140 print("This looks like a system path.")
1141 else:
1142 trunc_nccl_install_path = nccl_install_path + "/.."
1143
1144 # Look for header
1145 nccl_hdr_path = trunc_nccl_install_path + "/include"
1146 print("Assuming NCCL header path is " + nccl_hdr_path)
1147 if os.path.exists(nccl_hdr_path + "/nccl.h"):
1148 # Set NCCL_INSTALL_PATH
1149 environ_cp['NCCL_INSTALL_PATH'] = nccl_install_path
1150 write_action_env_to_bazelrc('NCCL_INSTALL_PATH', nccl_install_path)
1151
1152 # Set NCCL_HDR_PATH
1153 environ_cp['NCCL_HDR_PATH'] = nccl_hdr_path
1154 write_action_env_to_bazelrc('NCCL_HDR_PATH', nccl_hdr_path)
1155 break
1156 else:
1157 print('The header for NCCL2 cannot be found. Please install the libnccl-dev package.')
1158 else:
1159 print('NCCL2 is listed by ldconfig but the library is not found. '
1160 'Your ldconfig is out of date. Please run sudo ldconfig.')
1161 else:
1162 # NCCL is not found in ldconfig. Ask the user for the location.
1163 default_nccl_path = environ_cp.get('CUDA_TOOLKIT_PATH')
1164 ask_nccl_path = (r'Please specify the location where NCCL %s library is '
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001165 'installed. Refer to README.md for more details. [Default '
1166 'is %s]:') % (tf_nccl_version, default_nccl_path)
Jason Furmanek7c234152018-09-26 04:44:12 +00001167 nccl_install_path = get_from_env_or_user_or_default(
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001168 environ_cp, 'NCCL_INSTALL_PATH', ask_nccl_path, default_nccl_path)
1169
Jason Furmanek7c234152018-09-26 04:44:12 +00001170 # Result returned from "read" will be used unexpanded. That make "~"
1171 # unusable. Going through one more level of expansion to handle that.
1172 nccl_install_path = os.path.realpath(os.path.expanduser(nccl_install_path))
1173 if is_windows() or is_cygwin():
1174 nccl_install_path = cygpath(nccl_install_path)
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001175
Jason Furmanek7c234152018-09-26 04:44:12 +00001176 if is_windows():
1177 nccl_lib_path = 'lib/x64/nccl.lib'
1178 elif is_linux():
1179 nccl_lib_filename = 'libnccl.so.%s' % tf_nccl_version
1180 nccl_lpath = '%s/lib/%s' % (nccl_install_path, nccl_lib_filename)
1181 if not os.path.exists(nccl_lpath):
1182 for relative_path in NCCL_LIB_PATHS:
1183 path = '%s/%s%s' % (nccl_install_path, relative_path, nccl_lib_filename)
1184 if os.path.exists(path):
1185 print("NCCL found at " + path)
1186 nccl_lib_path = path
1187 break
1188 else:
1189 nccl_lib_path = nccl_lpath
1190 elif is_macos():
1191 nccl_lib_path = 'lib/libnccl.%s.dylib' % tf_nccl_version
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001192
Jason Furmanek7c234152018-09-26 04:44:12 +00001193 nccl_lib_path = os.path.join(nccl_install_path, nccl_lib_path)
1194 nccl_hdr_path = os.path.join(os.path.dirname(nccl_lib_path), '../include/nccl.h')
1195 print("Assuming NCCL header path is "+nccl_hdr_path)
1196 if os.path.exists(nccl_lib_path) and os.path.exists(nccl_hdr_path):
1197 # Set NCCL_INSTALL_PATH
1198 environ_cp['NCCL_INSTALL_PATH'] = os.path.dirname(nccl_lib_path)
1199 write_action_env_to_bazelrc('NCCL_INSTALL_PATH', os.path.dirname(nccl_lib_path))
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001200
Jason Furmanek7c234152018-09-26 04:44:12 +00001201 # Set NCCL_HDR_PATH
1202 environ_cp['NCCL_HDR_PATH'] = os.path.dirname(nccl_hdr_path)
1203 write_action_env_to_bazelrc('NCCL_HDR_PATH', os.path.dirname(nccl_hdr_path))
1204 break
1205
1206 # Reset and Retry
1207 print('Invalid path to NCCL %s toolkit, %s or %s not found. Please use the '
1208 'O/S agnostic package of NCCL 2' % (tf_nccl_version, nccl_lib_path,
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001209 nccl_hdr_path))
1210
Jason Furmanek7c234152018-09-26 04:44:12 +00001211 environ_cp['TF_NCCL_VERSION'] = ''
A. Unique TensorFlower1fda7642018-04-05 03:09:27 -07001212 else:
1213 raise UserInputError('Invalid TF_NCCL setting was provided %d '
1214 'times in a row. Assuming to be a scripting mistake.' %
1215 _DEFAULT_PROMPT_ASK_ATTEMPTS)
1216
1217 # Set TF_NCCL_VERSION
1218 environ_cp['TF_NCCL_VERSION'] = tf_nccl_version
1219 write_action_env_to_bazelrc('TF_NCCL_VERSION', tf_nccl_version)
1220
1221
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001222def get_native_cuda_compute_capabilities(environ_cp):
1223 """Get native cuda compute capabilities.
1224
1225 Args:
1226 environ_cp: copy of the os.environ.
1227 Returns:
1228 string of native cuda compute capabilities, separated by comma.
1229 """
1230 device_query_bin = os.path.join(
1231 environ_cp.get('CUDA_TOOLKIT_PATH'), 'extras/demo_suite/deviceQuery')
Jonathan Hseu008910f2017-08-25 14:01:05 -07001232 if os.path.isfile(device_query_bin) and os.access(device_query_bin, os.X_OK):
1233 try:
1234 output = run_shell(device_query_bin).split('\n')
1235 pattern = re.compile('[0-9]*\\.[0-9]*')
1236 output = [pattern.search(x) for x in output if 'Capability' in x]
1237 output = ','.join(x.group() for x in output if x is not None)
1238 except subprocess.CalledProcessError:
1239 output = ''
1240 else:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001241 output = ''
1242 return output
1243
1244
1245def set_tf_cuda_compute_capabilities(environ_cp):
1246 """Set TF_CUDA_COMPUTE_CAPABILITIES."""
1247 while True:
1248 native_cuda_compute_capabilities = get_native_cuda_compute_capabilities(
1249 environ_cp)
1250 if not native_cuda_compute_capabilities:
1251 default_cuda_compute_capabilities = _DEFAULT_CUDA_COMPUTE_CAPABILITIES
1252 else:
1253 default_cuda_compute_capabilities = native_cuda_compute_capabilities
1254
1255 ask_cuda_compute_capabilities = (
1256 'Please specify a list of comma-separated '
1257 'Cuda compute capabilities you want to '
1258 'build with.\nYou can find the compute '
1259 'capability of your device at: '
1260 'https://developer.nvidia.com/cuda-gpus.\nPlease'
1261 ' note that each additional compute '
1262 'capability significantly increases your '
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001263 'build time and binary size. [Default is: %s]: ' %
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001264 default_cuda_compute_capabilities)
1265 tf_cuda_compute_capabilities = get_from_env_or_user_or_default(
1266 environ_cp, 'TF_CUDA_COMPUTE_CAPABILITIES',
1267 ask_cuda_compute_capabilities, default_cuda_compute_capabilities)
1268 # Check whether all capabilities from the input is valid
1269 all_valid = True
Maciejd0f5bc12018-04-30 22:30:58 -05001270 # Remove all whitespace characters before splitting the string
Michael Case51053502018-06-05 17:47:19 -07001271 # that users may insert by accident, as this will result in error
Maciejd0f5bc12018-04-30 22:30:58 -05001272 tf_cuda_compute_capabilities = ''.join(tf_cuda_compute_capabilities.split())
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001273 for compute_capability in tf_cuda_compute_capabilities.split(','):
Andrew Harp6e3e7d12017-08-21 12:10:44 -07001274 m = re.match('[0-9]+.[0-9]+', compute_capability)
1275 if not m:
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001276 print('Invalid compute capability: ' % compute_capability)
1277 all_valid = False
Andrew Harp6e3e7d12017-08-21 12:10:44 -07001278 else:
1279 ver = int(m.group(0).split('.')[0])
1280 if ver < 3:
1281 print('Only compute capabilities 3.0 or higher are supported.')
1282 all_valid = False
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001283
1284 if all_valid:
1285 break
1286
1287 # Reset and Retry
1288 environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = ''
1289
1290 # Set TF_CUDA_COMPUTE_CAPABILITIES
1291 environ_cp['TF_CUDA_COMPUTE_CAPABILITIES'] = tf_cuda_compute_capabilities
1292 write_action_env_to_bazelrc('TF_CUDA_COMPUTE_CAPABILITIES',
1293 tf_cuda_compute_capabilities)
1294
1295
1296def set_other_cuda_vars(environ_cp):
1297 """Set other CUDA related variables."""
A. Unique TensorFlowerab391982018-07-11 04:52:49 -07001298 # If CUDA is enabled, always use GPU during build and test.
1299 if environ_cp.get('TF_CUDA_CLANG') == '1':
1300 write_to_bazelrc('build --config=cuda_clang')
1301 write_to_bazelrc('test --config=cuda_clang')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001302 else:
A. Unique TensorFlowerab391982018-07-11 04:52:49 -07001303 write_to_bazelrc('build --config=cuda')
1304 write_to_bazelrc('test --config=cuda')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001305
1306
1307def set_host_cxx_compiler(environ_cp):
1308 """Set HOST_CXX_COMPILER."""
Jonathan Hseu008910f2017-08-25 14:01:05 -07001309 default_cxx_host_compiler = which('g++') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001310
Austin Anderson6afface2017-12-05 11:59:17 -08001311 host_cxx_compiler = prompt_loop_or_load_from_env(
1312 environ_cp,
1313 var_name='HOST_CXX_COMPILER',
1314 var_default=default_cxx_host_compiler,
1315 ask_for_var=('Please specify which C++ compiler should be used as the '
1316 'host C++ compiler.'),
1317 check_success=os.path.exists,
1318 error_msg='Invalid C++ compiler path. %s cannot be found.',
1319 )
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001320
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001321 write_action_env_to_bazelrc('HOST_CXX_COMPILER', host_cxx_compiler)
1322
1323
1324def set_host_c_compiler(environ_cp):
1325 """Set HOST_C_COMPILER."""
Jonathan Hseu008910f2017-08-25 14:01:05 -07001326 default_c_host_compiler = which('gcc') or ''
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001327
Austin Anderson6afface2017-12-05 11:59:17 -08001328 host_c_compiler = prompt_loop_or_load_from_env(
1329 environ_cp,
1330 var_name='HOST_C_COMPILER',
1331 var_default=default_c_host_compiler,
Shanqing Cai71445712018-03-12 19:33:52 -07001332 ask_for_var=('Please specify which C compiler should be used as the host '
Austin Anderson6afface2017-12-05 11:59:17 -08001333 'C compiler.'),
1334 check_success=os.path.exists,
1335 error_msg='Invalid C compiler path. %s cannot be found.',
1336 )
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001337
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001338 write_action_env_to_bazelrc('HOST_C_COMPILER', host_c_compiler)
1339
1340
1341def set_computecpp_toolkit_path(environ_cp):
1342 """Set COMPUTECPP_TOOLKIT_PATH."""
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001343
Austin Anderson6afface2017-12-05 11:59:17 -08001344 def toolkit_exists(toolkit_path):
1345 """Check if a computecpp toolkit path is valid."""
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001346 if is_linux():
1347 sycl_rt_lib_path = 'lib/libComputeCpp.so'
1348 else:
1349 sycl_rt_lib_path = ''
1350
Austin Anderson6afface2017-12-05 11:59:17 -08001351 sycl_rt_lib_path_full = os.path.join(toolkit_path,
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001352 sycl_rt_lib_path)
Austin Anderson6afface2017-12-05 11:59:17 -08001353 exists = os.path.exists(sycl_rt_lib_path_full)
1354 if not exists:
1355 print('Invalid SYCL %s library path. %s cannot be found' %
1356 (_TF_OPENCL_VERSION, sycl_rt_lib_path_full))
1357 return exists
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001358
Austin Anderson6afface2017-12-05 11:59:17 -08001359 computecpp_toolkit_path = prompt_loop_or_load_from_env(
1360 environ_cp,
1361 var_name='COMPUTECPP_TOOLKIT_PATH',
1362 var_default=_DEFAULT_COMPUTECPP_TOOLKIT_PATH,
1363 ask_for_var=(
1364 'Please specify the location where ComputeCpp for SYCL %s is '
1365 'installed.' % _TF_OPENCL_VERSION),
1366 check_success=toolkit_exists,
1367 error_msg='Invalid SYCL compiler path. %s cannot be found.',
1368 suppress_default_error=True)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001369
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001370 write_action_env_to_bazelrc('COMPUTECPP_TOOLKIT_PATH',
1371 computecpp_toolkit_path)
1372
Michael Cased31531a2018-01-05 14:09:41 -08001373
Dandelion Man?90e42f32017-12-15 18:15:07 -08001374def set_trisycl_include_dir(environ_cp):
Michael Cased31531a2018-01-05 14:09:41 -08001375 """Set TRISYCL_INCLUDE_DIR."""
Frank Chenc4ef9272018-01-10 11:36:52 -08001376
Dandelion Man?90e42f32017-12-15 18:15:07 -08001377 ask_trisycl_include_dir = ('Please specify the location of the triSYCL '
1378 'include directory. (Use --config=sycl_trisycl '
1379 'when building with Bazel) '
1380 '[Default is %s]: '
Michael Cased31531a2018-01-05 14:09:41 -08001381 ) % (_DEFAULT_TRISYCL_INCLUDE_DIR)
Frank Chenc4ef9272018-01-10 11:36:52 -08001382
Dandelion Man?90e42f32017-12-15 18:15:07 -08001383 while True:
1384 trisycl_include_dir = get_from_env_or_user_or_default(
Michael Cased31531a2018-01-05 14:09:41 -08001385 environ_cp, 'TRISYCL_INCLUDE_DIR', ask_trisycl_include_dir,
1386 _DEFAULT_TRISYCL_INCLUDE_DIR)
Dandelion Man?90e42f32017-12-15 18:15:07 -08001387 if os.path.exists(trisycl_include_dir):
1388 break
1389
1390 print('Invalid triSYCL include directory, %s cannot be found'
1391 % (trisycl_include_dir))
1392
1393 # Set TRISYCL_INCLUDE_DIR
1394 environ_cp['TRISYCL_INCLUDE_DIR'] = trisycl_include_dir
1395 write_action_env_to_bazelrc('TRISYCL_INCLUDE_DIR',
1396 trisycl_include_dir)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001397
Yifei Fengb1d8c592017-11-22 13:42:21 -08001398
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001399def set_mpi_home(environ_cp):
1400 """Set MPI_HOME."""
Austin Anderson6afface2017-12-05 11:59:17 -08001401
Jonathan Hseu008910f2017-08-25 14:01:05 -07001402 default_mpi_home = which('mpirun') or which('mpiexec') or ''
1403 default_mpi_home = os.path.dirname(os.path.dirname(default_mpi_home))
1404
Austin Anderson6afface2017-12-05 11:59:17 -08001405 def valid_mpi_path(mpi_home):
1406 exists = (os.path.exists(os.path.join(mpi_home, 'include')) and
1407 os.path.exists(os.path.join(mpi_home, 'lib')))
1408 if not exists:
1409 print('Invalid path to the MPI Toolkit. %s or %s cannot be found' %
1410 (os.path.join(mpi_home, 'include'),
1411 os.path.exists(os.path.join(mpi_home, 'lib'))))
1412 return exists
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001413
Austin Anderson6afface2017-12-05 11:59:17 -08001414 _ = prompt_loop_or_load_from_env(
1415 environ_cp,
1416 var_name='MPI_HOME',
1417 var_default=default_mpi_home,
1418 ask_for_var='Please specify the MPI toolkit folder.',
1419 check_success=valid_mpi_path,
1420 error_msg='',
1421 suppress_default_error=True)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001422
1423
1424def set_other_mpi_vars(environ_cp):
1425 """Set other MPI related variables."""
1426 # Link the MPI header files
1427 mpi_home = environ_cp.get('MPI_HOME')
1428 symlink_force('%s/include/mpi.h' % mpi_home, 'third_party/mpi/mpi.h')
1429
1430 # Determine if we use OpenMPI or MVAPICH, these require different header files
1431 # to be included here to make bazel dependency checker happy
1432 if os.path.exists(os.path.join(mpi_home, 'include/mpi_portable_platform.h')):
1433 symlink_force(
1434 os.path.join(mpi_home, 'include/mpi_portable_platform.h'),
1435 'third_party/mpi/mpi_portable_platform.h')
1436 # TODO(gunan): avoid editing files in configure
1437 sed_in_place('third_party/mpi/mpi.bzl', 'MPI_LIB_IS_OPENMPI=False',
1438 'MPI_LIB_IS_OPENMPI=True')
1439 else:
1440 # MVAPICH / MPICH
1441 symlink_force(
1442 os.path.join(mpi_home, 'include/mpio.h'), 'third_party/mpi/mpio.h')
1443 symlink_force(
1444 os.path.join(mpi_home, 'include/mpicxx.h'), 'third_party/mpi/mpicxx.h')
1445 # TODO(gunan): avoid editing files in configure
1446 sed_in_place('third_party/mpi/mpi.bzl', 'MPI_LIB_IS_OPENMPI=True',
1447 'MPI_LIB_IS_OPENMPI=False')
1448
1449 if os.path.exists(os.path.join(mpi_home, 'lib/libmpi.so')):
1450 symlink_force(
1451 os.path.join(mpi_home, 'lib/libmpi.so'), 'third_party/mpi/libmpi.so')
1452 else:
1453 raise ValueError('Cannot find the MPI library file in %s/lib' % mpi_home)
1454
1455
A. Unique TensorFlower061c3592017-11-13 14:21:04 -08001456def set_grpc_build_flags():
1457 write_to_bazelrc('build --define grpc_no_ares=true')
1458
Michael Cased31531a2018-01-05 14:09:41 -08001459
Yifei Feng5198cb82018-08-17 13:53:06 -07001460def set_system_libs_flag(environ_cp):
1461 syslibs = environ_cp.get('TF_SYSTEM_LIBS', '')
Jason Furmanek7c234152018-09-26 04:44:12 +00001462 syslibs = ','.join(sorted(syslibs.split(',')))
Yifei Feng5198cb82018-08-17 13:53:06 -07001463 if syslibs and syslibs != '':
1464 write_action_env_to_bazelrc('TF_SYSTEM_LIBS', syslibs)
1465
1466
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001467def set_windows_build_flags(environ_cp):
1468 """Set Windows specific build options."""
1469 # The non-monolithic build is not supported yet
1470 write_to_bazelrc('build --config monolithic')
1471 # Suppress warning messages
1472 write_to_bazelrc('build --copt=-w --host_copt=-w')
1473 # Output more verbose information when something goes wrong
1474 write_to_bazelrc('build --verbose_failures')
1475 # The host and target platforms are the same in Windows build. So we don't
1476 # have to distinct them. This avoids building the same targets twice.
1477 write_to_bazelrc('build --distinct_host_configuration=false')
1478 # Enable short object file path to avoid long path issue on Windows.
1479 # TODO(pcloudy): Remove this flag when upgrading Bazel to 0.16.0
1480 # Short object file path will be enabled by default.
1481 write_to_bazelrc('build --experimental_shortened_obj_file_path=true')
1482
1483 if get_var(
1484 environ_cp, 'TF_OVERRIDE_EIGEN_STRONG_INLINE', 'Eigen strong inline',
1485 True,
1486 ('Would you like to override eigen strong inline for some C++ '
A. Unique TensorFlower8ed40cd2018-07-20 10:55:31 -07001487 'compilation to reduce the compilation time?'),
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001488 'Eigen strong inline overridden.',
1489 'Not overriding eigen strong inline, '
1490 'some compilations could take more than 20 mins.'):
1491 # Due to a known MSVC compiler issue
1492 # https://github.com/tensorflow/tensorflow/issues/10521
1493 # Overriding eigen strong inline speeds up the compiling of
1494 # conv_grad_ops_3d.cc and conv_ops_3d.cc by 20 minutes,
1495 # but this also hurts the performance. Let users decide what they want.
1496 write_to_bazelrc('build --define=override_eigen_strong_inline=true')
Dandelion Man?90e42f32017-12-15 18:15:07 -08001497
A. Unique TensorFlower061c3592017-11-13 14:21:04 -08001498
Michael Cased31531a2018-01-05 14:09:41 -08001499def config_info_line(name, help_text):
1500 """Helper function to print formatted help text for Bazel config options."""
1501 print('\t--config=%-12s\t# %s' % (name, help_text))
1502
1503
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001504def main():
Shanqing Cai71445712018-03-12 19:33:52 -07001505 parser = argparse.ArgumentParser()
1506 parser.add_argument("--workspace",
1507 type=str,
1508 default=_TF_WORKSPACE_ROOT,
1509 help="The absolute path to your active Bazel workspace.")
1510 args = parser.parse_args()
1511
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001512 # Make a copy of os.environ to be clear when functions and getting and setting
1513 # environment variables.
1514 environ_cp = dict(os.environ)
1515
Yifei Fengbb384112018-07-24 13:12:54 -07001516 check_bazel_version('0.15.0')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001517
Shanqing Cai71445712018-03-12 19:33:52 -07001518 reset_tf_configure_bazelrc(args.workspace)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001519 cleanup_makefile()
Gunhan Gulsoyed89a2b2017-09-19 18:36:26 -07001520 setup_python(environ_cp)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001521
1522 if is_windows():
Yong Tanga7b7aa82018-07-02 07:41:42 -07001523 environ_cp['TF_NEED_AWS'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001524 environ_cp['TF_NEED_GCP'] = '0'
1525 environ_cp['TF_NEED_HDFS'] = '0'
1526 environ_cp['TF_NEED_JEMALLOC'] = '0'
Michael Cased90054e2018-02-07 14:36:00 -08001527 environ_cp['TF_NEED_KAFKA'] = '0'
Yifei Fengb1d8c592017-11-22 13:42:21 -08001528 environ_cp['TF_NEED_OPENCL_SYCL'] = '0'
1529 environ_cp['TF_NEED_COMPUTECPP'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001530 environ_cp['TF_NEED_OPENCL'] = '0'
1531 environ_cp['TF_CUDA_CLANG'] = '0'
Guangda Lai76f69382018-01-25 23:59:19 -08001532 environ_cp['TF_NEED_TENSORRT'] = '0'
Ilya Biryukov9e651e42018-03-22 05:33:42 -07001533 # TODO(ibiryukov): Investigate using clang as a cpu or cuda compiler on
1534 # Windows.
1535 environ_cp['TF_DOWNLOAD_CLANG'] = '0'
A. Unique TensorFlower6e97fb32018-07-16 14:07:29 -07001536 environ_cp['TF_ENABLE_XLA'] = '0'
1537 environ_cp['TF_NEED_GDR'] = '0'
1538 environ_cp['TF_NEED_VERBS'] = '0'
1539 environ_cp['TF_NEED_MPI'] = '0'
1540 environ_cp['TF_SET_ANDROID_WORKSPACE'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001541
1542 if is_macos():
1543 environ_cp['TF_NEED_JEMALLOC'] = '0'
Guangda Lai76f69382018-01-25 23:59:19 -08001544 environ_cp['TF_NEED_TENSORRT'] = '0'
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001545
Jon Triebenbach6896a742018-06-27 13:29:53 -05001546 # The numpy package on ppc64le uses OpenBLAS which has multi-threading
1547 # issues that lead to incorrect answers. Set OMP_NUM_THREADS=1 at
1548 # runtime to allow the Tensorflow testcases which compare numpy
1549 # results to Tensorflow results to succeed.
1550 if is_ppc64le():
1551 write_action_env_to_bazelrc("OMP_NUM_THREADS", 1)
1552
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001553 set_build_var(environ_cp, 'TF_NEED_JEMALLOC', 'jemalloc as malloc',
1554 'with_jemalloc', True)
1555 set_build_var(environ_cp, 'TF_NEED_GCP', 'Google Cloud Platform',
Benoit Steiner355e25e2017-10-24 19:47:46 -07001556 'with_gcp_support', True, 'gcp')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001557 set_build_var(environ_cp, 'TF_NEED_HDFS', 'Hadoop File System',
Benoit Steiner355e25e2017-10-24 19:47:46 -07001558 'with_hdfs_support', True, 'hdfs')
Yong Tanga7b7aa82018-07-02 07:41:42 -07001559 set_build_var(environ_cp, 'TF_NEED_AWS', 'Amazon AWS Platform',
1560 'with_aws_support', True, 'aws')
Michael Cased90054e2018-02-07 14:36:00 -08001561 set_build_var(environ_cp, 'TF_NEED_KAFKA', 'Apache Kafka Platform',
Jianwei Xie63dffd52018-03-29 10:50:46 -07001562 'with_kafka_support', True, 'kafka')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001563 set_build_var(environ_cp, 'TF_ENABLE_XLA', 'XLA JIT', 'with_xla_support',
Michael Case98850a52017-09-14 13:35:57 -07001564 False, 'xla')
A. Unique TensorFlower28ce1d12017-08-15 12:08:29 -07001565 set_build_var(environ_cp, 'TF_NEED_GDR', 'GDR', 'with_gdr_support',
Michael Case98850a52017-09-14 13:35:57 -07001566 False, 'gdr')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001567 set_build_var(environ_cp, 'TF_NEED_VERBS', 'VERBS', 'with_verbs_support',
Michael Case98850a52017-09-14 13:35:57 -07001568 False, 'verbs')
Avijitf88a6f92018-07-24 23:58:58 -07001569 set_build_var(environ_cp, 'TF_NEED_NGRAPH', 'nGraph',
1570 'with_ngraph_support', False, 'ngraph')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001571
Yifei Fengb1d8c592017-11-22 13:42:21 -08001572 set_action_env_var(environ_cp, 'TF_NEED_OPENCL_SYCL', 'OpenCL SYCL', False)
1573 if environ_cp.get('TF_NEED_OPENCL_SYCL') == '1':
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001574 set_host_cxx_compiler(environ_cp)
1575 set_host_c_compiler(environ_cp)
Yifei Fengb1d8c592017-11-22 13:42:21 -08001576 set_action_env_var(environ_cp, 'TF_NEED_COMPUTECPP', 'ComputeCPP', True)
1577 if environ_cp.get('TF_NEED_COMPUTECPP') == '1':
1578 set_computecpp_toolkit_path(environ_cp)
1579 else:
1580 set_trisycl_include_dir(environ_cp)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001581
1582 set_action_env_var(environ_cp, 'TF_NEED_CUDA', 'CUDA', False)
A. Unique TensorFlower24cbb2a2017-09-08 07:45:44 -07001583 if (environ_cp.get('TF_NEED_CUDA') == '1' and
1584 'TF_CUDA_CONFIG_REPO' not in environ_cp):
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001585 set_tf_cuda_version(environ_cp)
Yifei Fengb1d8c592017-11-22 13:42:21 -08001586 set_tf_cudnn_version(environ_cp)
Guangda Lai76f69382018-01-25 23:59:19 -08001587 if is_linux():
1588 set_tf_tensorrt_install_path(environ_cp)
Michael Case0073d132018-04-11 09:34:44 -07001589 set_tf_nccl_install_path(environ_cp)
1590
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001591 set_tf_cuda_compute_capabilities(environ_cp)
Ankur Taly0e6f39d2018-02-16 18:22:55 -08001592 if 'LD_LIBRARY_PATH' in environ_cp and environ_cp.get(
1593 'LD_LIBRARY_PATH') != '1':
1594 write_action_env_to_bazelrc('LD_LIBRARY_PATH',
1595 environ_cp.get('LD_LIBRARY_PATH'))
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001596
1597 set_tf_cuda_clang(environ_cp)
1598 if environ_cp.get('TF_CUDA_CLANG') == '1':
Ilya Biryukov9e651e42018-03-22 05:33:42 -07001599 # Ask whether we should download the clang toolchain.
1600 set_tf_download_clang(environ_cp)
A. Unique TensorFlowerfd29e952017-12-22 03:07:51 -08001601 if environ_cp.get('TF_DOWNLOAD_CLANG') != '1':
1602 # Set up which clang we should use as the cuda / host compiler.
1603 set_clang_cuda_compiler_path(environ_cp)
Ilya Biryukov1c3d02e2018-09-04 03:09:52 -07001604 else:
1605 # Use downloaded LLD for linking.
1606 write_to_bazelrc('build:cuda_clang --config=download_clang_use_lld')
1607 write_to_bazelrc('test:cuda_clang --config=download_clang_use_lld')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001608 else:
1609 # Set up which gcc nvcc should use as the host compiler
1610 # No need to set this on Windows
1611 if not is_windows():
1612 set_gcc_host_compiler_path(environ_cp)
1613 set_other_cuda_vars(environ_cp)
Ilya Biryukov9e651e42018-03-22 05:33:42 -07001614 else:
1615 # CUDA not required. Ask whether we should download the clang toolchain and
1616 # use it for the CPU build.
1617 set_tf_download_clang(environ_cp)
1618 if environ_cp.get('TF_DOWNLOAD_CLANG') == '1':
1619 write_to_bazelrc('build --config=download_clang')
1620 write_to_bazelrc('test --config=download_clang')
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001621
1622 set_build_var(environ_cp, 'TF_NEED_MPI', 'MPI', 'with_mpi_support', False)
1623 if environ_cp.get('TF_NEED_MPI') == '1':
1624 set_mpi_home(environ_cp)
1625 set_other_mpi_vars(environ_cp)
1626
A. Unique TensorFlower061c3592017-11-13 14:21:04 -08001627 set_grpc_build_flags()
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001628 set_cc_opt_flags(environ_cp)
Yifei Feng5198cb82018-08-17 13:53:06 -07001629 set_system_libs_flag(environ_cp)
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001630 if is_windows():
1631 set_windows_build_flags(environ_cp)
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001632
Anna Ra9a1d5a2018-09-14 12:44:31 -07001633 # Add a config option to build TensorFlow 2.0 API.
1634 write_to_bazelrc('build:v2 --define=tf_api_version=2')
1635
Michael Case51053502018-06-05 17:47:19 -07001636 if get_var(
1637 environ_cp, 'TF_SET_ANDROID_WORKSPACE', 'android workspace',
1638 False,
1639 ('Would you like to interactively configure ./WORKSPACE for '
1640 'Android builds?'),
1641 'Searching for NDK and SDK installations.',
1642 'Not configuring the WORKSPACE for Android builds.'):
1643 create_android_ndk_rule(environ_cp)
1644 create_android_sdk_rule(environ_cp)
Austin Anderson6afface2017-12-05 11:59:17 -08001645
A. Unique TensorFlower1b212352018-07-19 13:48:50 -07001646 # On Windows, we don't have MKL support and the build is always monolithic.
1647 # So no need to print the following message.
1648 # TODO(pcloudy): remove the following if check when they make sense on Windows
1649 if not is_windows():
1650 print('Preconfigured Bazel build configs. You can use any of the below by '
1651 'adding "--config=<>" to your build command. See tools/bazel.rc for '
1652 'more details.')
1653 config_info_line('mkl', 'Build with MKL support.')
1654 config_info_line('monolithic', 'Config for mostly static monolithic build.')
Austin Anderson6afface2017-12-05 11:59:17 -08001655
A. Unique TensorFlower73ea2872017-07-25 13:30:03 -07001656if __name__ == '__main__':
1657 main()