1# Copyright 2015 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"""Bring in all of the public TensorFlow interface into this module.""" 16 17from __future__ import absolute_import as _absolute_import 18from __future__ import division as _division 19from __future__ import print_function as _print_function 20 21import distutils as _distutils 22import inspect as _inspect 23import os as _os 24import site as _site 25import six as _six 26import sys as _sys 27 28# pylint: disable=g-bad-import-order 29from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import 30from tensorflow.python.tools import module_util as _module_util 31from tensorflow.python.platform import tf_logging as _logging 32from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader 33 34# API IMPORTS PLACEHOLDER 35 36# WRAPPER_PLACEHOLDER 37 38if "dev" in __version__: # pylint: disable=undefined-variable 39 _logging.warning(""" 40 41 TensorFlow's `tf-nightly` package will soon be updated to TensorFlow 2.0. 42 43 Please upgrade your code to TensorFlow 2.0: 44 * https://www.tensorflow.org/guide/migrate 45 46 Or install the latest stable TensorFlow 1.X release: 47 * `pip install -U "tensorflow==1.*"` 48 49 Otherwise your code may be broken by the change. 50 51 """) 52 53# Make sure directory containing top level submodules is in 54# the __path__ so that "from tensorflow.foo import bar" works. 55# We're using bitwise, but there's nothing special about that. 56_API_MODULE = _sys.modules[__name__].bitwise # pylint: disable=undefined-variable 57_current_module = _sys.modules[__name__] 58_tf_api_dir = _os.path.dirname(_os.path.dirname(_API_MODULE.__file__)) 59if not hasattr(_current_module, '__path__'): 60 __path__ = [_tf_api_dir] 61elif _tf_api_dir not in __path__: 62 __path__.append(_tf_api_dir) 63 64# Hook external TensorFlow modules. 65# Import compat before trying to import summary from tensorboard, so that 66# reexport_tf_summary can get compat from sys.modules. Only needed if using 67# lazy loading. 68_current_module.compat.v2 # pylint: disable=pointless-statement 69 70# Lazy-load estimator. 71_estimator_module = "tensorflow_estimator.python.estimator.api._v1.estimator" 72estimator = _LazyLoader("estimator", globals(), _estimator_module) 73_module_dir = _module_util.get_parent_dir_for_name(_estimator_module) 74if _module_dir: 75 _current_module.__path__ = [_module_dir] + _current_module.__path__ 76setattr(_current_module, "estimator", estimator) 77 78try: 79 from .python.keras.api._v1 import keras 80 _current_module.__path__ = ( 81 [_module_util.get_parent_dir(keras)] + _current_module.__path__) 82 setattr(_current_module, "keras", keras) 83except ImportError: 84 pass 85 86# Explicitly import lazy-loaded modules to support autocompletion. 87# pylint: disable=g-import-not-at-top 88if not _six.PY2: 89 import typing as _typing 90 if _typing.TYPE_CHECKING: 91 from tensorflow_estimator.python.estimator.api._v1 import estimator 92# pylint: enable=g-import-not-at-top 93 94from tensorflow.python.util.lazy_loader import LazyLoader # pylint: disable=g-import-not-at-top 95_CONTRIB_WARNING = """ 96The TensorFlow contrib module will not be included in TensorFlow 2.0. 97For more information, please see: 98 * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md 99 * https://github.com/tensorflow/addons 100 * https://github.com/tensorflow/io (for I/O related ops) 101If you depend on functionality not listed there, please file an issue. 102""" 103contrib = LazyLoader('contrib', globals(), 'tensorflow.contrib', 104 _CONTRIB_WARNING) 105del LazyLoader 106# The templated code that replaces the placeholder above sometimes 107# sets the __all__ variable. If it does, we have to be sure to add 108# "contrib". 109if '__all__' in vars(): 110 vars()['__all__'].append('contrib') 111 112from tensorflow.python.platform import flags # pylint: disable=g-import-not-at-top 113# The 'app' module will be imported as part of the placeholder section above. 114_current_module.app.flags = flags # pylint: disable=undefined-variable 115setattr(_current_module, "flags", flags) 116 117_major_api_version = 1 118 119# Load all plugin libraries from site-packages/tensorflow-plugins if we are 120# running under pip. 121# TODO(gunan): Enable setting an environment variable to define arbitrary plugin 122# directories. 123# TODO(gunan): Find a better location for this code snippet. 124from tensorflow.python.framework import load_library as _ll 125from tensorflow.python.lib.io import file_io as _fi 126 127# Get sitepackages directories for the python installation. 128_site_packages_dirs = [] 129_site_packages_dirs += [] if _site.USER_SITE is None else [_site.USER_SITE] 130_site_packages_dirs += [_p for _p in _sys.path if 'site-packages' in _p] 131if 'getsitepackages' in dir(_site): 132 _site_packages_dirs += _site.getsitepackages() 133 134if 'sysconfig' in dir(_distutils): 135 _site_packages_dirs += [_distutils.sysconfig.get_python_lib()] 136 137_site_packages_dirs = list(set(_site_packages_dirs)) 138 139# Find the location of this exact file. 140_current_file_location = _inspect.getfile(_inspect.currentframe()) 141 142def _running_from_pip_package(): 143 return any( 144 _current_file_location.startswith(dir_) for dir_ in _site_packages_dirs) 145 146if _running_from_pip_package(): 147 # TODO(gunan): Add sanity checks to loaded modules here. 148 for _s in _site_packages_dirs: 149 # Load first party dynamic kernels. 150 _main_dir = _os.path.join(_s, 'tensorflow/core/kernels') 151 if _os.path.exists(_main_dir): 152 _ll.load_library(_main_dir) 153 154 # Load third party dynamic kernels. 155 _plugin_dir = _os.path.join(_s, 'tensorflow-plugins') 156 if _os.path.exists(_plugin_dir): 157 _ll.load_library(_plugin_dir) 158 # Load Pluggable Device Library 159 _ll.load_pluggable_device_library(_plugin_dir) 160 161# Delete modules that should be hidden from dir(). 162# Don't fail if these modules are not available. 163# For e.g. this file will be originally placed under tensorflow/_api/v1 which 164# does not have 'python', 'core' directories. Then, it will be copied 165# to tensorflow/ which does have these two directories. 166 167# pylint: disable=undefined-variable 168try: 169 del python 170except NameError: 171 pass 172try: 173 del core 174except NameError: 175 pass 176try: 177 del compiler 178except NameError: 179 pass 180 181 182# __all__ PLACEHOLDER 183