1# Copyright 2021 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"""Provides wrapper for TensorFlow modules.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21import importlib 22import inspect 23 24from tensorflow.python.platform import tf_logging as logging 25from tensorflow.python.util import fast_module_type 26from tensorflow.python.util import tf_decorator 27from tensorflow.python.util import tf_inspect 28from tensorflow.tools.compatibility import all_renames_v2 29 30FastModuleType = fast_module_type.get_fast_module_type_class() 31_PER_MODULE_WARNING_LIMIT = 1 32 33 34def get_rename_v2(name): 35 if name not in all_renames_v2.symbol_renames: 36 return None 37 return all_renames_v2.symbol_renames[name] 38 39 40def _call_location(): 41 # We want to get stack frame 3 frames up from current frame, 42 # i.e. above __getattr__, _tfmw_add_deprecation_warning, 43 # and _call_location calls. 44 frame = inspect.currentframe() 45 for _ in range(4): 46 parent = frame.f_back 47 if parent is None: 48 break 49 return '{}:{}'.format(frame.f_code.co_filename, frame.f_lineno) 50 51 52def contains_deprecation_decorator(decorators): 53 return any(d.decorator_name == 'deprecated' for d in decorators) 54 55 56def has_deprecation_decorator(symbol): 57 """Checks if given object has a deprecation decorator. 58 59 We check if deprecation decorator is in decorators as well as 60 whether symbol is a class whose __init__ method has a deprecation 61 decorator. 62 Args: 63 symbol: Python object. 64 65 Returns: 66 True if symbol has deprecation decorator. 67 """ 68 decorators, symbol = tf_decorator.unwrap(symbol) 69 if contains_deprecation_decorator(decorators): 70 return True 71 if tf_inspect.isfunction(symbol): 72 return False 73 if not tf_inspect.isclass(symbol): 74 return False 75 if not hasattr(symbol, '__init__'): 76 return False 77 init_decorators, _ = tf_decorator.unwrap(symbol.__init__) 78 return contains_deprecation_decorator(init_decorators) 79 80 81class TFModuleWrapper(FastModuleType): 82 """Wrapper for TF modules to support deprecation messages and lazyloading.""" 83 84 def __init__( # pylint: disable=super-on-old-class 85 self, 86 wrapped, 87 module_name, 88 public_apis=None, 89 deprecation=True, 90 has_lite=False): # pylint: enable=super-on-old-class 91 super(TFModuleWrapper, self).__init__(wrapped.__name__) 92 FastModuleType.set_getattr_callback(self, TFModuleWrapper._getattr) 93 FastModuleType.set_getattribute_callback(self, 94 TFModuleWrapper._getattribute) 95 self.__dict__.update(wrapped.__dict__) 96 # Prefix all local attributes with _tfmw_ so that we can 97 # handle them differently in attribute access methods. 98 self._tfmw_wrapped_module = wrapped 99 self._tfmw_module_name = module_name 100 self._tfmw_public_apis = public_apis 101 self._tfmw_print_deprecation_warnings = deprecation 102 self._tfmw_has_lite = has_lite 103 # Set __all__ so that import * work for lazy loaded modules 104 if self._tfmw_public_apis: 105 self._tfmw_wrapped_module.__all__ = list(self._tfmw_public_apis.keys()) 106 self.__all__ = list(self._tfmw_public_apis.keys()) 107 else: 108 if hasattr(self._tfmw_wrapped_module, '__all__'): 109 self.__all__ = self._tfmw_wrapped_module.__all__ 110 else: 111 self._tfmw_wrapped_module.__all__ = [ 112 attr for attr in dir(self._tfmw_wrapped_module) 113 if not attr.startswith('_') 114 ] 115 self.__all__ = self._tfmw_wrapped_module.__all__ 116 117 # names we already checked for deprecation 118 self._tfmw_deprecated_checked = set() 119 self._tfmw_warning_count = 0 120 121 def _tfmw_add_deprecation_warning(self, name, attr): 122 """Print deprecation warning for attr with given name if necessary.""" 123 if (self._tfmw_warning_count < _PER_MODULE_WARNING_LIMIT and 124 name not in self._tfmw_deprecated_checked): 125 126 self._tfmw_deprecated_checked.add(name) 127 128 if self._tfmw_module_name: 129 full_name = 'tf.%s.%s' % (self._tfmw_module_name, name) 130 else: 131 full_name = 'tf.%s' % name 132 rename = get_rename_v2(full_name) 133 if rename and not has_deprecation_decorator(attr): 134 call_location = _call_location() 135 # skip locations in Python source 136 if not call_location.startswith('<'): 137 logging.warning( 138 'From %s: The name %s is deprecated. Please use %s instead.\n', 139 _call_location(), full_name, rename) 140 self._tfmw_warning_count += 1 141 return True 142 return False 143 144 def _tfmw_import_module(self, name): 145 """Lazily loading the modules.""" 146 symbol_loc_info = self._tfmw_public_apis[name] 147 if symbol_loc_info[0]: 148 module = importlib.import_module(symbol_loc_info[0]) 149 attr = getattr(module, symbol_loc_info[1]) 150 else: 151 attr = importlib.import_module(symbol_loc_info[1]) 152 setattr(self._tfmw_wrapped_module, name, attr) 153 self.__dict__[name] = attr 154 # Cache the pair 155 self._fastdict_insert(name, attr) 156 return attr 157 158 def _getattribute(self, name): 159 # pylint: disable=g-doc-return-or-yield,g-doc-args 160 """Imports and caches pre-defined API. 161 162 Warns if necessary. 163 164 This method is a replacement for __getattribute__(). It will be added into 165 the extended python module as a callback to reduce API overhead. 166 """ 167 # Avoid infinite recursions 168 func__fastdict_insert = object.__getattribute__(self, '_fastdict_insert') 169 170 # Make sure we do not import from tensorflow/lite/__init__.py 171 if name == 'lite': 172 if self._tfmw_has_lite: 173 attr = self._tfmw_import_module(name) 174 setattr(self._tfmw_wrapped_module, 'lite', attr) 175 func__fastdict_insert(name, attr) 176 return attr 177 # Placeholder for Google-internal contrib error 178 179 attr = object.__getattribute__(self, name) 180 181 # Return and cache dunders and our own members. 182 # This is necessary to guarantee successful construction. 183 # In addition, all the accessed attributes used during the construction must 184 # begin with "__" or "_tfmw" or "_fastdict_". 185 if name.startswith('__') or name.startswith('_tfmw_') or name.startswith( 186 '_fastdict_'): 187 func__fastdict_insert(name, attr) 188 return attr 189 190 # Print deprecations, only cache functions after deprecation warnings have 191 # stopped. 192 if not (self._tfmw_print_deprecation_warnings and 193 self._tfmw_add_deprecation_warning(name, attr)): 194 func__fastdict_insert(name, attr) 195 196 return attr 197 198 def _getattr(self, name): 199 # pylint: disable=g-doc-return-or-yield,g-doc-args 200 """Imports and caches pre-defined API. 201 202 Warns if necessary. 203 204 This method is a replacement for __getattr__(). It will be added into the 205 extended python module as a callback to reduce API overhead. Instead of 206 relying on implicit AttributeError handling, this added callback function 207 will 208 be called explicitly from the extended C API if the default attribute lookup 209 fails. 210 """ 211 try: 212 attr = getattr(self._tfmw_wrapped_module, name) 213 except AttributeError: 214 # Placeholder for Google-internal contrib error 215 216 if not self._tfmw_public_apis: 217 raise 218 if name not in self._tfmw_public_apis: 219 raise 220 attr = self._tfmw_import_module(name) 221 222 if self._tfmw_print_deprecation_warnings: 223 self._tfmw_add_deprecation_warning(name, attr) 224 return attr 225 226 def __setattr__(self, arg, val): # pylint: disable=super-on-old-class 227 if not arg.startswith('_tfmw_'): 228 setattr(self._tfmw_wrapped_module, arg, val) 229 self.__dict__[arg] = val 230 if arg not in self.__all__ and arg != '__all__': 231 self.__all__.append(arg) 232 # Update the cache 233 if self._fastdict_key_in(arg): 234 self._fastdict_insert(arg, val) 235 super(TFModuleWrapper, self).__setattr__(arg, val) 236 237 def __dir__(self): 238 if self._tfmw_public_apis: 239 return list( 240 set(self._tfmw_public_apis.keys()).union( 241 set([ 242 attr for attr in dir(self._tfmw_wrapped_module) 243 if not attr.startswith('_') 244 ]))) 245 else: 246 return dir(self._tfmw_wrapped_module) 247 248 def __delattr__(self, name): # pylint: disable=super-on-old-class 249 if name.startswith('_tfmw_'): 250 super(TFModuleWrapper, self).__delattr__(name) 251 else: 252 delattr(self._tfmw_wrapped_module, name) 253 254 def __repr__(self): 255 return self._tfmw_wrapped_module.__repr__() 256 257 def __reduce__(self): 258 return importlib.import_module, (self.__name__,) 259