Suggest default CUDA/cuDNN/TensorRT versions after initial auto-detection.

Reduce spew on error message when some file was not found.

PiperOrigin-RevId: 243810113
diff --git a/configure.py b/configure.py
index fe0e6d7..1a69b06 100644
--- a/configure.py
+++ b/configure.py
@@ -33,6 +33,9 @@
   from distutils.spawn import find_executable as which
 # pylint: enable=g-import-not-at-top
 
+_DEFAULT_CUDA_VERSION = '10'
+_DEFAULT_CUDNN_VERSION = '7'
+_DEFAULT_TENSORRT_VERSION = '5'
 _DEFAULT_CUDA_COMPUTE_CAPABILITIES = '3.5,7.0'
 
 _TF_OPENCL_VERSION = '1.2'
@@ -865,21 +868,25 @@
 
 def set_tf_cuda_version(environ_cp):
   """Set TF_CUDA_VERSION."""
-  ask_cuda_version = ('Please specify the CUDA SDK version you want to use. '
-                      '[Leave empty to accept any version]: ')
+  ask_cuda_version = (
+      'Please specify the CUDA SDK version you want to use. '
+      '[Leave empty to default to CUDA %s]: ') % _DEFAULT_CUDA_VERSION
   tf_cuda_version = get_from_env_or_user_or_default(environ_cp,
                                                     'TF_CUDA_VERSION',
-                                                    ask_cuda_version, '')
+                                                    ask_cuda_version,
+                                                    _DEFAULT_CUDA_VERSION)
   environ_cp['TF_CUDA_VERSION'] = tf_cuda_version
 
 
 def set_tf_cudnn_version(environ_cp):
   """Set TF_CUDNN_VERSION."""
-  ask_cudnn_version = ('Please specify the cuDNN version you want to use. '
-                       '[Leave empty to accept any version]: ')
+  ask_cudnn_version = (
+      'Please specify the cuDNN version you want to use. '
+      '[Leave empty to default to cuDNN %s]: ') % _DEFAULT_CUDNN_VERSION
   tf_cudnn_version = get_from_env_or_user_or_default(environ_cp,
                                                      'TF_CUDNN_VERSION',
-                                                     ask_cudnn_version, '')
+                                                     ask_cudnn_version,
+                                                     _DEFAULT_CUDNN_VERSION)
   environ_cp['TF_CUDNN_VERSION'] = tf_cudnn_version
 
 
@@ -922,11 +929,10 @@
 
   ask_tensorrt_version = (
       'Please specify the TensorRT version you want to use. '
-      '[Leave empty to accept any version]: ')
-  tf_tensorrt_version = get_from_env_or_user_or_default(environ_cp,
-                                                        'TF_TENSORRT_VERSION',
-                                                        ask_tensorrt_version,
-                                                        '')
+      '[Leave empty to  default to TensorRT %s]: ') % _DEFAULT_TENSORRT_VERSION
+  tf_tensorrt_version = get_from_env_or_user_or_default(
+      environ_cp, 'TF_TENSORRT_VERSION', ask_tensorrt_version,
+      _DEFAULT_TENSORRT_VERSION)
   environ_cp['TF_TENSORRT_VERSION'] = tf_tensorrt_version
 
 
@@ -1331,7 +1337,7 @@
 
   if proc.wait():
     # Errors from find_cuda_config.py were sent to stderr.
-    print('\n\nAsking for detailed CUDA configuration...\n')
+    print('Asking for detailed CUDA configuration...\n')
     return False
 
   config = dict(
diff --git a/third_party/gpus/find_cuda_config.py b/third_party/gpus/find_cuda_config.py
index c8977a0..faabd82 100644
--- a/third_party/gpus/find_cuda_config.py
+++ b/third_party/gpus/find_cuda_config.py
@@ -139,7 +139,7 @@
     match = pattern.match(line.decode("ascii"))
     if match:
       result.add(os.path.dirname(match.group(1)))
-  return list(result)
+  return sorted(list(result))
 
 
 def _get_default_cuda_paths(cuda_version):
@@ -155,42 +155,47 @@
             "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v%s\\" %
             cuda_version)
     ]
-  return ["/usr/local/cuda-%s" % cuda_version, "/usr"] + _get_ld_config_paths()
+  return ["/usr/local/cuda-%s" % cuda_version, "/usr/local/cuda", "/usr"
+         ] + _get_ld_config_paths()
 
 
-def _header_paths(base_paths):
-  return _cartesian_product(base_paths, [
+def _header_paths():
+  """Returns hard-coded set of relative paths to look for header files."""
+  return [
       "",
       "include",
       "include/cuda",
       "include/*-linux-gnu",
       "extras/CUPTI/include",
       "include/cuda/CUPTI",
-  ])
+  ]
 
 
-def _library_paths(base_paths):
-  return _cartesian_product(base_paths, [
+def _library_paths():
+  """Returns hard-coded set of relative paths to look for library files."""
+  return [
       "",
       "lib64",
       "lib",
       "lib/*-linux-gnu",
       "lib/x64",
       "extras/CUPTI/*",
-  ])
+  ]
 
 
-def _not_found_error(paths, filepattern):
+def _not_found_error(base_paths, relative_paths, filepattern):
+  base_paths = "".join(["\n        '%s'" % path for path in sorted(base_paths)])
+  relative_paths = "".join(["\n        '%s'" % path for path in relative_paths])
   return ConfigError(
-      "Could not find any %s in:%s\n" %
-      (filepattern, "".join(["\n        %s" % path for path in sorted(paths)])))
+      "Could not find any %s in any subdirectory:%s\nof:%s\n" %
+      (filepattern, relative_paths, base_paths))
 
 
-def _find_file(paths, filepattern):
-  for path in paths:
+def _find_file(base_paths, relative_paths, filepattern):
+  for path in _cartesian_product(base_paths, relative_paths):
     for file in glob.glob(os.path.join(path, filepattern)):
       return file
-  raise _not_found_error(paths, filepattern)
+  raise _not_found_error(base_paths, relative_paths, filepattern)
 
 
 def _find_library(base_paths, library_name, required_version):
@@ -203,24 +208,26 @@
   else:
     filepattern = ".".join(["lib" + library_name, "so"] +
                            required_version.split(".")[:1]) + "*"
-  return _find_file(_library_paths(base_paths), filepattern)
+  return _find_file(base_paths, _library_paths(), filepattern)
 
 
-def _find_versioned_file(paths, filepattern, required_version, get_version):
+def _find_versioned_file(base_paths, relative_paths, filepattern,
+                         required_version, get_version):
   """Returns first valid path to a file that matches the requested version."""
-  for path in paths:
+  for path in _cartesian_product(base_paths, relative_paths):
     for file in glob.glob(os.path.join(path, filepattern)):
       actual_version = get_version(file)
       if _matches_version(actual_version, required_version):
         return file, actual_version
   raise _not_found_error(
-      paths, filepattern + " matching version '%s'" % required_version)
+      base_paths, relative_paths,
+      filepattern + " matching version '%s'" % required_version)
 
 
 def _find_header(base_paths, header_name, required_version, get_version):
   """Returns first valid path to a header that matches the requested version."""
-  return _find_versioned_file(
-      _header_paths(base_paths), header_name, required_version, get_version)
+  return _find_versioned_file(base_paths, _header_paths(), header_name,
+                              required_version, get_version)
 
 
 def _find_cuda_config(base_paths, required_version):
@@ -247,20 +254,18 @@
     return None
 
   nvcc_name = "nvcc.exe" if _is_windows() else "nvcc"
-  nvcc_path, nvcc_version = _find_versioned_file(
-      _cartesian_product(base_paths, [
-          "",
-          "bin",
-      ]), nvcc_name, cuda_version, get_nvcc_version)
+  nvcc_path, nvcc_version = _find_versioned_file(base_paths, [
+      "",
+      "bin",
+  ], nvcc_name, cuda_version, get_nvcc_version)
 
-  nvvm_path = _find_file(
-      _cartesian_product(base_paths, [
-          "nvvm/libdevice",
-          "share/cuda",
-          "lib/nvidia-cuda-toolkit/libdevice",
-      ]), "libdevice*.10.bc")
+  nvvm_path = _find_file(base_paths, [
+      "nvvm/libdevice",
+      "share/cuda",
+      "lib/nvidia-cuda-toolkit/libdevice",
+  ], "libdevice*.10.bc")
 
-  cupti_header_path = _find_file(_header_paths(base_paths), "cupti.h")
+  cupti_header_path = _find_file(base_paths, _header_paths(), "cupti.h")
   cupti_library_path = _find_library(base_paths, "cupti", required_version)
 
   cuda_binary_dir = os.path.dirname(nvcc_path)
@@ -311,7 +316,7 @@
 
   else:
     # There is no version info available before CUDA 10.1, just find the file.
-    header_path = _find_file(_header_paths(base_paths), "cublas_api.h")
+    header_path = _find_file(base_paths, _header_paths(), "cublas_api.h")
     # cuBLAS version is the same as CUDA version (x.y).
     cublas_version = required_version
 
@@ -411,6 +416,7 @@
   cuda_version = os.environ.get("TF_CUDA_VERSION", "")
   base_paths = _list_from_env("TF_CUDA_PATHS",
                               _get_default_cuda_paths(cuda_version))
+  base_paths = [path for path in base_paths if os.path.exists(path)]
 
   result = {}
   if "cuda" in libraries: