Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/__init__.py: 92%

416 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

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""" 

16Top-level module of TensorFlow. By convention, we refer to this module as 

17`tf` instead of `tensorflow`, following the common practice of importing 

18TensorFlow via the command `import tensorflow as tf`. 

19 

20The primary function of this module is to import all of the public TensorFlow 

21interfaces into a single place. The interfaces themselves are located in 

22sub-modules, as described below. 

23 

24Note that the file `__init__.py` in the TensorFlow source code tree is actually 

25only a placeholder to enable test cases to run. The TensorFlow build replaces 

26this file with a file generated from [`api_template.__init__.py`](https://www.github.com/tensorflow/tensorflow/blob/master/tensorflow/api_template.__init__.py) 

27""" 

28 

29import distutils as _distutils 

30import importlib 

31import inspect as _inspect 

32import logging as _logging 

33import os as _os 

34import site as _site 

35import sys as _sys 

36import typing as _typing 

37 

38from tensorflow.python.tools import module_util as _module_util 

39from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader 

40 

41# Make sure code inside the TensorFlow codebase can use tf2.enabled() at import. 

42_os.environ["TF2_BEHAVIOR"] = "1" 

43from tensorflow.python import tf2 as _tf2 

44_tf2.enable() 

45 

46from ._api.v2 import __internal__ 

47from ._api.v2 import __operators__ 

48from ._api.v2 import audio 

49from ._api.v2 import autodiff 

50from ._api.v2 import autograph 

51from ._api.v2 import bitwise 

52from ._api.v2 import compat 

53from ._api.v2 import config 

54from ._api.v2 import data 

55from ._api.v2 import debugging 

56from ._api.v2 import distribute 

57from ._api.v2 import dtypes 

58from ._api.v2 import errors 

59from ._api.v2 import experimental 

60from ._api.v2 import feature_column 

61from ._api.v2 import graph_util 

62from ._api.v2 import image 

63from ._api.v2 import io 

64from ._api.v2 import linalg 

65from ._api.v2 import lite 

66from ._api.v2 import lookup 

67from ._api.v2 import math 

68from ._api.v2 import mlir 

69from ._api.v2 import nest 

70from ._api.v2 import nn 

71from ._api.v2 import profiler 

72from ._api.v2 import quantization 

73from ._api.v2 import queue 

74from ._api.v2 import ragged 

75from ._api.v2 import random 

76from ._api.v2 import raw_ops 

77from ._api.v2 import saved_model 

78from ._api.v2 import sets 

79from ._api.v2 import signal 

80from ._api.v2 import sparse 

81from ._api.v2 import strings 

82from ._api.v2 import summary 

83from ._api.v2 import sysconfig 

84from ._api.v2 import test 

85from ._api.v2 import tpu 

86from ._api.v2 import train 

87from ._api.v2 import types 

88from ._api.v2 import version 

89from ._api.v2 import xla 

90from tensorflow.python.data.ops.optional_ops import OptionalSpec 

91from tensorflow.python.eager.backprop import GradientTape 

92from tensorflow.python.eager.context import executing_eagerly 

93from tensorflow.python.eager.polymorphic_function.polymorphic_function import function 

94from tensorflow.python.framework.constant_op import constant 

95from tensorflow.python.framework.device_spec import DeviceSpecV2 as DeviceSpec 

96from tensorflow.python.framework.dtypes import DType 

97from tensorflow.python.framework.dtypes import as_dtype 

98from tensorflow.python.framework.dtypes import bfloat16 

99from tensorflow.python.framework.dtypes import bool 

100from tensorflow.python.framework.dtypes import complex128 

101from tensorflow.python.framework.dtypes import complex64 

102from tensorflow.python.framework.dtypes import double 

103from tensorflow.python.framework.dtypes import float16 

104from tensorflow.python.framework.dtypes import float32 

105from tensorflow.python.framework.dtypes import float64 

106from tensorflow.python.framework.dtypes import half 

107from tensorflow.python.framework.dtypes import int16 

108from tensorflow.python.framework.dtypes import int32 

109from tensorflow.python.framework.dtypes import int64 

110from tensorflow.python.framework.dtypes import int8 

111from tensorflow.python.framework.dtypes import qint16 

112from tensorflow.python.framework.dtypes import qint32 

113from tensorflow.python.framework.dtypes import qint8 

114from tensorflow.python.framework.dtypes import quint16 

115from tensorflow.python.framework.dtypes import quint8 

116from tensorflow.python.framework.dtypes import resource 

117from tensorflow.python.framework.dtypes import string 

118from tensorflow.python.framework.dtypes import uint16 

119from tensorflow.python.framework.dtypes import uint32 

120from tensorflow.python.framework.dtypes import uint64 

121from tensorflow.python.framework.dtypes import uint8 

122from tensorflow.python.framework.dtypes import variant 

123from tensorflow.python.framework.importer import import_graph_def 

124from tensorflow.python.framework.indexed_slices import IndexedSlices 

125from tensorflow.python.framework.indexed_slices import IndexedSlicesSpec 

126from tensorflow.python.framework.load_library import load_library 

127from tensorflow.python.framework.load_library import load_op_library 

128from tensorflow.python.framework.ops import Graph 

129from tensorflow.python.framework.ops import Operation 

130from tensorflow.python.framework.ops import RegisterGradient 

131from tensorflow.python.framework.ops import Tensor 

132from tensorflow.python.framework.ops import control_dependencies 

133from tensorflow.python.framework.ops import device_v2 as device 

134from tensorflow.python.framework.ops import get_current_name_scope 

135from tensorflow.python.framework.ops import init_scope 

136from tensorflow.python.framework.ops import inside_function 

137from tensorflow.python.framework.ops import is_symbolic_tensor 

138from tensorflow.python.framework.ops import name_scope_v2 as name_scope 

139from tensorflow.python.framework.ops import no_gradient 

140from tensorflow.python.framework.sparse_tensor import SparseTensor 

141from tensorflow.python.framework.sparse_tensor import SparseTensorSpec 

142from tensorflow.python.framework.tensor import TensorSpec 

143from tensorflow.python.framework.tensor_conversion import convert_to_tensor_v2_with_dispatch as convert_to_tensor 

144from tensorflow.python.framework.tensor_conversion_registry import register_tensor_conversion_function 

145from tensorflow.python.framework.tensor_shape import TensorShape 

146from tensorflow.python.framework.tensor_util import MakeNdarray as make_ndarray 

147from tensorflow.python.framework.tensor_util import constant_value as get_static_value 

148from tensorflow.python.framework.tensor_util import is_tf_type as is_tensor 

149from tensorflow.python.framework.tensor_util import make_tensor_proto 

150from tensorflow.python.framework.type_spec import TypeSpec 

151from tensorflow.python.framework.type_spec import type_spec_from_value 

152from tensorflow.python.framework.versions import COMPILER_VERSION as __compiler_version__ 

153from tensorflow.python.framework.versions import CXX11_ABI_FLAG as __cxx11_abi_flag__ 

154from tensorflow.python.framework.versions import CXX_VERSION as __cxx_version__ 

155from tensorflow.python.framework.versions import GIT_VERSION as __git_version__ 

156from tensorflow.python.framework.versions import MONOLITHIC_BUILD as __monolithic_build__ 

157from tensorflow.python.framework.versions import VERSION as __version__ 

158from tensorflow.python.module.module import Module 

159from tensorflow.python.ops.array_ops import batch_to_space_v2 as batch_to_space 

160from tensorflow.python.ops.array_ops import boolean_mask_v2 as boolean_mask 

161from tensorflow.python.ops.array_ops import broadcast_dynamic_shape 

162from tensorflow.python.ops.array_ops import broadcast_static_shape 

163from tensorflow.python.ops.array_ops import concat 

164from tensorflow.python.ops.array_ops import edit_distance 

165from tensorflow.python.ops.array_ops import expand_dims_v2 as expand_dims 

166from tensorflow.python.ops.array_ops import fill 

167from tensorflow.python.ops.array_ops import fingerprint 

168from tensorflow.python.ops.array_ops import gather_nd_v2 as gather_nd 

169from tensorflow.python.ops.array_ops import gather_v2 as gather 

170from tensorflow.python.ops.array_ops import guarantee_const 

171from tensorflow.python.ops.array_ops import identity 

172from tensorflow.python.ops.array_ops import meshgrid 

173from tensorflow.python.ops.array_ops import newaxis 

174from tensorflow.python.ops.array_ops import one_hot 

175from tensorflow.python.ops.array_ops import ones 

176from tensorflow.python.ops.array_ops import ones_like_v2 as ones_like 

177from tensorflow.python.ops.array_ops import pad_v2 as pad 

178from tensorflow.python.ops.array_ops import parallel_stack 

179from tensorflow.python.ops.array_ops import rank 

180from tensorflow.python.ops.array_ops import repeat 

181from tensorflow.python.ops.array_ops import required_space_to_batch_paddings 

182from tensorflow.python.ops.array_ops import reshape 

183from tensorflow.python.ops.array_ops import reverse_sequence_v2 as reverse_sequence 

184from tensorflow.python.ops.array_ops import searchsorted 

185from tensorflow.python.ops.array_ops import sequence_mask 

186from tensorflow.python.ops.array_ops import shape_n 

187from tensorflow.python.ops.array_ops import shape_v2 as shape 

188from tensorflow.python.ops.array_ops import size_v2 as size 

189from tensorflow.python.ops.array_ops import slice 

190from tensorflow.python.ops.array_ops import space_to_batch_v2 as space_to_batch 

191from tensorflow.python.ops.array_ops import split 

192from tensorflow.python.ops.array_ops import squeeze_v2 as squeeze 

193from tensorflow.python.ops.array_ops import stop_gradient 

194from tensorflow.python.ops.array_ops import strided_slice 

195from tensorflow.python.ops.array_ops import tensor_scatter_nd_update 

196from tensorflow.python.ops.array_ops import transpose_v2 as transpose 

197from tensorflow.python.ops.array_ops import unique 

198from tensorflow.python.ops.array_ops import unique_with_counts 

199from tensorflow.python.ops.array_ops import where_v2 as where 

200from tensorflow.python.ops.array_ops import zeros 

201from tensorflow.python.ops.array_ops import zeros_like_v2 as zeros_like 

202from tensorflow.python.ops.array_ops_stack import stack 

203from tensorflow.python.ops.array_ops_stack import unstack 

204from tensorflow.python.ops.batch_ops import batch_function as nondifferentiable_batch_function 

205from tensorflow.python.ops.check_ops import assert_equal_v2 as assert_equal 

206from tensorflow.python.ops.check_ops import assert_greater_v2 as assert_greater 

207from tensorflow.python.ops.check_ops import assert_less_v2 as assert_less 

208from tensorflow.python.ops.check_ops import assert_rank_v2 as assert_rank 

209from tensorflow.python.ops.check_ops import ensure_shape 

210from tensorflow.python.ops.clip_ops import clip_by_global_norm 

211from tensorflow.python.ops.clip_ops import clip_by_norm 

212from tensorflow.python.ops.clip_ops import clip_by_value 

213from tensorflow.python.ops.cond import cond_for_tf_v2 as cond 

214from tensorflow.python.ops.control_flow_assert import Assert 

215from tensorflow.python.ops.control_flow_case import case_v2 as case 

216from tensorflow.python.ops.control_flow_ops import group 

217from tensorflow.python.ops.control_flow_ops import tuple_v2 as tuple 

218from tensorflow.python.ops.control_flow_switch_case import switch_case 

219from tensorflow.python.ops.critical_section_ops import CriticalSection 

220from tensorflow.python.ops.custom_gradient import custom_gradient 

221from tensorflow.python.ops.custom_gradient import grad_pass_through 

222from tensorflow.python.ops.custom_gradient import recompute_grad 

223from tensorflow.python.ops.functional_ops import foldl_v2 as foldl 

224from tensorflow.python.ops.functional_ops import foldr_v2 as foldr 

225from tensorflow.python.ops.functional_ops import scan_v2 as scan 

226from tensorflow.python.ops.gen_array_ops import bitcast 

227from tensorflow.python.ops.gen_array_ops import broadcast_to 

228from tensorflow.python.ops.gen_array_ops import extract_volume_patches 

229from tensorflow.python.ops.gen_array_ops import identity_n 

230from tensorflow.python.ops.gen_array_ops import reverse_v2 as reverse 

231from tensorflow.python.ops.gen_array_ops import scatter_nd 

232from tensorflow.python.ops.gen_array_ops import space_to_batch_nd 

233from tensorflow.python.ops.gen_array_ops import tensor_scatter_add as tensor_scatter_nd_add 

234from tensorflow.python.ops.gen_array_ops import tensor_scatter_max as tensor_scatter_nd_max 

235from tensorflow.python.ops.gen_array_ops import tensor_scatter_min as tensor_scatter_nd_min 

236from tensorflow.python.ops.gen_array_ops import tensor_scatter_sub as tensor_scatter_nd_sub 

237from tensorflow.python.ops.gen_array_ops import tile 

238from tensorflow.python.ops.gen_array_ops import unravel_index 

239from tensorflow.python.ops.gen_control_flow_ops import no_op 

240from tensorflow.python.ops.gen_data_flow_ops import dynamic_partition 

241from tensorflow.python.ops.gen_data_flow_ops import dynamic_stitch 

242from tensorflow.python.ops.gen_linalg_ops import matrix_square_root 

243from tensorflow.python.ops.gen_logging_ops import timestamp 

244from tensorflow.python.ops.gen_math_ops import acosh 

245from tensorflow.python.ops.gen_math_ops import asin 

246from tensorflow.python.ops.gen_math_ops import asinh 

247from tensorflow.python.ops.gen_math_ops import atan 

248from tensorflow.python.ops.gen_math_ops import atan2 

249from tensorflow.python.ops.gen_math_ops import atanh 

250from tensorflow.python.ops.gen_math_ops import cos 

251from tensorflow.python.ops.gen_math_ops import cosh 

252from tensorflow.python.ops.gen_math_ops import greater 

253from tensorflow.python.ops.gen_math_ops import greater_equal 

254from tensorflow.python.ops.gen_math_ops import less 

255from tensorflow.python.ops.gen_math_ops import less_equal 

256from tensorflow.python.ops.gen_math_ops import logical_and 

257from tensorflow.python.ops.gen_math_ops import logical_not 

258from tensorflow.python.ops.gen_math_ops import logical_or 

259from tensorflow.python.ops.gen_math_ops import maximum 

260from tensorflow.python.ops.gen_math_ops import minimum 

261from tensorflow.python.ops.gen_math_ops import neg as negative 

262from tensorflow.python.ops.gen_math_ops import real_div as realdiv 

263from tensorflow.python.ops.gen_math_ops import sin 

264from tensorflow.python.ops.gen_math_ops import sinh 

265from tensorflow.python.ops.gen_math_ops import square 

266from tensorflow.python.ops.gen_math_ops import tan 

267from tensorflow.python.ops.gen_math_ops import tanh 

268from tensorflow.python.ops.gen_math_ops import truncate_div as truncatediv 

269from tensorflow.python.ops.gen_math_ops import truncate_mod as truncatemod 

270from tensorflow.python.ops.gen_nn_ops import approx_top_k 

271from tensorflow.python.ops.gen_nn_ops import conv2d_backprop_filter_v2 

272from tensorflow.python.ops.gen_nn_ops import conv2d_backprop_input_v2 

273from tensorflow.python.ops.gen_ragged_array_ops import ragged_fill_empty_rows 

274from tensorflow.python.ops.gen_ragged_array_ops import ragged_fill_empty_rows_grad 

275from tensorflow.python.ops.gen_random_index_shuffle_ops import random_index_shuffle 

276from tensorflow.python.ops.gen_string_ops import as_string 

277from tensorflow.python.ops.gradients_impl import HessiansV2 as hessians 

278from tensorflow.python.ops.gradients_impl import gradients_v2 as gradients 

279from tensorflow.python.ops.gradients_util import AggregationMethod 

280from tensorflow.python.ops.histogram_ops import histogram_fixed_width 

281from tensorflow.python.ops.histogram_ops import histogram_fixed_width_bins 

282from tensorflow.python.ops.init_ops_v2 import Constant as constant_initializer 

283from tensorflow.python.ops.init_ops_v2 import Ones as ones_initializer 

284from tensorflow.python.ops.init_ops_v2 import RandomNormal as random_normal_initializer 

285from tensorflow.python.ops.init_ops_v2 import RandomUniform as random_uniform_initializer 

286from tensorflow.python.ops.init_ops_v2 import Zeros as zeros_initializer 

287from tensorflow.python.ops.linalg_ops import eig 

288from tensorflow.python.ops.linalg_ops import eigvals 

289from tensorflow.python.ops.linalg_ops import eye 

290from tensorflow.python.ops.linalg_ops import norm_v2 as norm 

291from tensorflow.python.ops.logging_ops import print_v2 as print 

292from tensorflow.python.ops.manip_ops import roll 

293from tensorflow.python.ops.map_fn import map_fn_v2 as map_fn 

294from tensorflow.python.ops.math_ops import abs 

295from tensorflow.python.ops.math_ops import acos 

296from tensorflow.python.ops.math_ops import add 

297from tensorflow.python.ops.math_ops import add_n 

298from tensorflow.python.ops.math_ops import argmax_v2 as argmax 

299from tensorflow.python.ops.math_ops import argmin_v2 as argmin 

300from tensorflow.python.ops.math_ops import cast 

301from tensorflow.python.ops.math_ops import complex 

302from tensorflow.python.ops.math_ops import cumsum 

303from tensorflow.python.ops.math_ops import divide 

304from tensorflow.python.ops.math_ops import equal 

305from tensorflow.python.ops.math_ops import exp 

306from tensorflow.python.ops.math_ops import floor 

307from tensorflow.python.ops.math_ops import linspace_nd as linspace 

308from tensorflow.python.ops.math_ops import matmul 

309from tensorflow.python.ops.math_ops import multiply 

310from tensorflow.python.ops.math_ops import not_equal 

311from tensorflow.python.ops.math_ops import pow 

312from tensorflow.python.ops.math_ops import range 

313from tensorflow.python.ops.math_ops import reduce_all 

314from tensorflow.python.ops.math_ops import reduce_any 

315from tensorflow.python.ops.math_ops import reduce_logsumexp 

316from tensorflow.python.ops.math_ops import reduce_max 

317from tensorflow.python.ops.math_ops import reduce_mean 

318from tensorflow.python.ops.math_ops import reduce_min 

319from tensorflow.python.ops.math_ops import reduce_prod 

320from tensorflow.python.ops.math_ops import reduce_sum 

321from tensorflow.python.ops.math_ops import round 

322from tensorflow.python.ops.math_ops import saturate_cast 

323from tensorflow.python.ops.math_ops import scalar_mul_v2 as scalar_mul 

324from tensorflow.python.ops.math_ops import sigmoid 

325from tensorflow.python.ops.math_ops import sign 

326from tensorflow.python.ops.math_ops import sqrt 

327from tensorflow.python.ops.math_ops import subtract 

328from tensorflow.python.ops.math_ops import tensordot 

329from tensorflow.python.ops.math_ops import truediv 

330from tensorflow.python.ops.parallel_for.control_flow_ops import vectorized_map 

331from tensorflow.python.ops.ragged.ragged_tensor import RaggedTensor 

332from tensorflow.python.ops.ragged.ragged_tensor import RaggedTensorSpec 

333from tensorflow.python.ops.script_ops import eager_py_func as py_function 

334from tensorflow.python.ops.script_ops import numpy_function 

335from tensorflow.python.ops.sort_ops import argsort 

336from tensorflow.python.ops.sort_ops import sort 

337from tensorflow.python.ops.special_math_ops import einsum 

338from tensorflow.python.ops.tensor_array_ops import TensorArray 

339from tensorflow.python.ops.tensor_array_ops import TensorArraySpec 

340from tensorflow.python.ops.unconnected_gradients import UnconnectedGradients 

341from tensorflow.python.ops.variable_scope import variable_creator_scope 

342from tensorflow.python.ops.variables import Variable 

343from tensorflow.python.ops.variables import VariableAggregationV2 as VariableAggregation 

344from tensorflow.python.ops.variables import VariableSynchronization 

345from tensorflow.python.ops.while_loop import while_loop_v2 as while_loop 

346from tensorflow.python.platform.tf_logging import get_logger 

347 

348# WRAPPER_PLACEHOLDER 

349 

350# Make sure directory containing top level submodules is in 

351# the __path__ so that "from tensorflow.foo import bar" works. 

352# We're using bitwise, but there's nothing special about that. 

353_API_MODULE = _sys.modules[__name__].bitwise 

354_tf_api_dir = _os.path.dirname(_os.path.dirname(_API_MODULE.__file__)) 

355_current_module = _sys.modules[__name__] 

356 

357if not hasattr(_current_module, "__path__"): 

358 __path__ = [_tf_api_dir] 

359elif _tf_api_dir not in __path__: 

360 __path__.append(_tf_api_dir) 

361 

362# Hook external TensorFlow modules. 

363# Import compat before trying to import summary from tensorboard, so that 

364# reexport_tf_summary can get compat from sys.modules. Only needed if using 

365# lazy loading. 

366_current_module.compat.v2 # pylint: disable=pointless-statement 

367try: 

368 from tensorboard.summary._tf import summary 

369 _current_module.__path__ = ( 

370 [_module_util.get_parent_dir(summary)] + _current_module.__path__) 

371 setattr(_current_module, "summary", summary) 

372except ImportError: 

373 _logging.warning( 

374 "Limited tf.summary API due to missing TensorBoard installation.") 

375 

376# Load tensorflow-io-gcs-filesystem if enabled 

377# pylint: disable=g-import-not-at-top 

378if (_os.getenv("TF_USE_MODULAR_FILESYSTEM", "0") == "true" or 

379 _os.getenv("TF_USE_MODULAR_FILESYSTEM", "0") == "1"): 

380 import tensorflow_io_gcs_filesystem as _tensorflow_io_gcs_filesystem 

381# pylint: enable=g-import-not-at-top 

382 

383# Lazy-load estimator. 

384_estimator_module = "tensorflow_estimator.python.estimator.api._v2.estimator" 

385estimator = _LazyLoader("estimator", globals(), _estimator_module) 

386_module_dir = _module_util.get_parent_dir_for_name(_estimator_module) 

387if _module_dir: 

388 _current_module.__path__ = [_module_dir] + _current_module.__path__ 

389setattr(_current_module, "estimator", estimator) 

390 

391_keras_module = "keras.api._v2.keras" 

392_keras = _LazyLoader("keras", globals(), _keras_module) 

393_module_dir = _module_util.get_parent_dir_for_name(_keras_module) 

394if _module_dir: 

395 _current_module.__path__ = [_module_dir] + _current_module.__path__ 

396setattr(_current_module, "keras", _keras) 

397 

398 

399# Enable TF2 behaviors 

400from tensorflow.python.compat import v2_compat as _compat # pylint: disable=g-import-not-at-top 

401_compat.enable_v2_behavior() 

402_major_api_version = 2 

403 

404 

405# Load all plugin libraries from site-packages/tensorflow-plugins if we are 

406# running under pip. 

407# TODO(gunan): Find a better location for this code snippet. 

408from tensorflow.python.framework import load_library as _ll 

409from tensorflow.python.lib.io import file_io as _fi 

410 

411# Get sitepackages directories for the python installation. 

412_site_packages_dirs = [] 

413if _site.ENABLE_USER_SITE and _site.USER_SITE is not None: 

414 _site_packages_dirs += [_site.USER_SITE] 

415_site_packages_dirs += [p for p in _sys.path if "site-packages" in p] 

416if "getsitepackages" in dir(_site): 

417 _site_packages_dirs += _site.getsitepackages() 

418 

419if "sysconfig" in dir(_distutils): 

420 _site_packages_dirs += [_distutils.sysconfig.get_python_lib()] 

421 

422_site_packages_dirs = list(set(_site_packages_dirs)) 

423 

424# Find the location of this exact file. 

425_current_file_location = _inspect.getfile(_inspect.currentframe()) 

426 

427def _running_from_pip_package(): 

428 return any( 

429 _current_file_location.startswith(dir_) for dir_ in _site_packages_dirs) 

430 

431if _running_from_pip_package(): 

432 # TODO(gunan): Add sanity checks to loaded modules here. 

433 

434 # Load first party dynamic kernels. 

435 _tf_dir = _os.path.dirname(_current_file_location) 

436 _kernel_dir = _os.path.join(_tf_dir, "core", "kernels") 

437 if _os.path.exists(_kernel_dir): 

438 _ll.load_library(_kernel_dir) 

439 

440 # Load third party dynamic kernels. 

441 for _s in _site_packages_dirs: 

442 _plugin_dir = _os.path.join(_s, "tensorflow-plugins") 

443 if _os.path.exists(_plugin_dir): 

444 _ll.load_library(_plugin_dir) 

445 # Load Pluggable Device Library 

446 _ll.load_pluggable_device_library(_plugin_dir) 

447 

448if _os.getenv("TF_PLUGGABLE_DEVICE_LIBRARY_PATH", ""): 

449 _ll.load_pluggable_device_library( 

450 _os.getenv("TF_PLUGGABLE_DEVICE_LIBRARY_PATH") 

451 ) 

452 

453# Add module aliases 

454if hasattr(_current_module, "keras"): 

455 # It is possible that keras is a lazily loaded module, which might break when 

456 # actually trying to import it. Have a Try-Catch to make sure it doesn't break 

457 # when it doing some very initial loading, like tf.compat.v2, etc. 

458 try: 

459 _keras_package = "keras.api._v2.keras." 

460 _losses = _LazyLoader("losses", globals(), _keras_package + "losses") 

461 _metrics = _LazyLoader("metrics", globals(), _keras_package + "metrics") 

462 _optimizers = _LazyLoader( 

463 "optimizers", globals(), _keras_package + "optimizers") 

464 _initializers = _LazyLoader( 

465 "initializers", globals(), _keras_package + "initializers") 

466 setattr(_current_module, "losses", _losses) 

467 setattr(_current_module, "metrics", _metrics) 

468 setattr(_current_module, "optimizers", _optimizers) 

469 setattr(_current_module, "initializers", _initializers) 

470 except ImportError: 

471 pass 

472 

473 # Do an eager load for Keras' code so that any function/method that needs to 

474 # happen at load time will trigger, eg registration of optimizers in the 

475 # SavedModel registry. 

476 # See b/196254385 for more details. 

477 try: 

478 importlib.import_module("keras.optimizers") 

479 except (ImportError, AttributeError): 

480 pass 

481 try: 

482 importlib.import_module("keras.src.optimizers") 

483 except (ImportError, AttributeError): 

484 pass 

485del importlib 

486 

487# Explicitly import lazy-loaded modules to support autocompletion. 

488# pylint: disable=g-import-not-at-top 

489if _typing.TYPE_CHECKING: 

490 from tensorflow_estimator.python.estimator.api._v2 import estimator as estimator 

491 from keras.api._v2 import keras 

492 from keras.api._v2.keras import losses 

493 from keras.api._v2.keras import metrics 

494 from keras.api._v2.keras import optimizers 

495 from keras.api._v2.keras import initializers 

496# pylint: enable=g-import-not-at-top 

497 

498# pylint: enable=undefined-variable 

499 

500# Delete modules that should be hidden from dir(). 

501# Don't fail if these modules are not available. 

502# For e.g. this file will be originally placed under tensorflow/_api/v1 which 

503# does not have "python", "core" directories. Then, it will be copied 

504# to tensorflow/ which does have these two directories. 

505# pylint: disable=undefined-variable 

506try: 

507 del python 

508except NameError: 

509 pass 

510try: 

511 del core 

512except NameError: 

513 pass 

514try: 

515 del compiler 

516except NameError: 

517 pass 

518 

519 

520_names_with_underscore = ['__compiler_version__', '__cxx11_abi_flag__', '__cxx_version__', '__git_version__', '__internal__', '__monolithic_build__', '__operators__', '__version__'] 

521__all__ = [_s for _s in dir() if not _s.startswith('_')] 

522__all__.extend([_s for _s in _names_with_underscore]) 

523