Home | History | Annotate | Download | only in platform
      1 # Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #     http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 # ==============================================================================
     15 
     16 """Platform-specific code for checking the integrity of the TensorFlow build."""
     17 from __future__ import absolute_import
     18 from __future__ import division
     19 from __future__ import print_function
     20 
     21 import os
     22 
     23 
     24 try:
     25   from tensorflow.python.platform import build_info
     26 except ImportError:
     27   raise ImportError("Could not import tensorflow. Do not import tensorflow "
     28                     "from its source directory; change directory to outside "
     29                     "the TensorFlow source tree, and relaunch your Python "
     30                     "interpreter from there.")
     31 
     32 
     33 def preload_check():
     34   """Raises an exception if the environment is not correctly configured.
     35 
     36   Raises:
     37     ImportError: If the check detects that the environment is not correctly
     38       configured, and attempting to load the TensorFlow runtime will fail.
     39   """
     40   if os.name == "nt":
     41     # Attempt to load any DLLs that the Python extension depends on before
     42     # we load the Python extension, so that we can raise an actionable error
     43     # message if they are not found.
     44     import ctypes  # pylint: disable=g-import-not-at-top
     45     if hasattr(build_info, "msvcp_dll_name"):
     46       try:
     47         ctypes.WinDLL(build_info.msvcp_dll_name)
     48       except OSError:
     49         raise ImportError(
     50             "Could not find %r. TensorFlow requires that this DLL be "
     51             "installed in a directory that is named in your %%PATH%% "
     52             "environment variable. You may install this DLL by downloading "
     53             "Visual C++ 2015 Redistributable Update 3 from this URL: "
     54             "https://www.microsoft.com/en-us/download/details.aspx?id=53587"
     55             % build_info.msvcp_dll_name)
     56 
     57     if build_info.is_cuda_build:
     58       # Attempt to check that the necessary CUDA DLLs are loadable.
     59 
     60       if hasattr(build_info, "nvcuda_dll_name"):
     61         try:
     62           ctypes.WinDLL(build_info.nvcuda_dll_name)
     63         except OSError:
     64           raise ImportError(
     65               "Could not find %r. TensorFlow requires that this DLL "
     66               "be installed in a directory that is named in your %%PATH%% "
     67               "environment variable. Typically it is installed in "
     68               "'C:\\Windows\\System32'. If it is not present, ensure that you "
     69               "have a CUDA-capable GPU with the correct driver installed."
     70               % build_info.nvcuda_dll_name)
     71 
     72       if hasattr(build_info, "cudart_dll_name") and hasattr(
     73           build_info, "cuda_version_number"):
     74         try:
     75           ctypes.WinDLL(build_info.cudart_dll_name)
     76         except OSError:
     77           raise ImportError(
     78               "Could not find %r. TensorFlow requires that this DLL be "
     79               "installed in a directory that is named in your %%PATH%% "
     80               "environment variable. Download and install CUDA %s from "
     81               "this URL: https://developer.nvidia.com/cuda-toolkit"
     82               % (build_info.cudart_dll_name, build_info.cuda_version_number))
     83 
     84       if hasattr(build_info, "cudnn_dll_name") and hasattr(
     85           build_info, "cudnn_version_number"):
     86         try:
     87           ctypes.WinDLL(build_info.cudnn_dll_name)
     88         except OSError:
     89           raise ImportError(
     90               "Could not find %r. TensorFlow requires that this DLL be "
     91               "installed in a directory that is named in your %%PATH%% "
     92               "environment variable. Note that installing cuDNN is a separate "
     93               "step from installing CUDA, and this DLL is often found in a "
     94               "different directory from the CUDA DLLs. You may install the "
     95               "necessary DLL by downloading cuDNN %s from this URL: "
     96               "https://developer.nvidia.com/cudnn"
     97               % (build_info.cudnn_dll_name, build_info.cudnn_version_number))
     98 
     99   else:
    100     # TODO(mrry): Consider adding checks for the Linux and Mac OS X builds.
    101     pass
    102