Example #1
0
from astronet.contrib import testing
from astronet.contrib import tfprof
from astronet.contrib import timeseries
from astronet.contrib import tpu
from astronet.contrib import training
from astronet.contrib import util
from astronet.contrib.eager.python import tfe as eager
from astronet.contrib.optimizer_v2 import optimizer_v2_symbols as optimizer_v2
from astronet.contrib.receptive_field import receptive_field_api as receptive_field
from astronet.contrib.recurrent.python import recurrent_api as recurrent
from astronet.contrib.remote_fused_graph import pylib as remote_fused_graph
from astronet.contrib.specs import python as specs
from astronet.contrib.summary import summary

if os.name != "nt" and platform.machine() != "s390x":
    try:
        from astronet.contrib import cloud
    except ImportError:
        pass

from tensorflow.python.util.lazy_loader import LazyLoader
ffmpeg = LazyLoader("ffmpeg", globals(), "astronet.contrib.ffmpeg")
del os
del platform

del LazyLoader

del absolute_import
del division
del print_function
Example #2
0
import abc
import numpy as np
import six

from tensorflow.python import pywrap_tensorflow
from tensorflow.python.framework import composite_tensor
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import tensor_shape
from tensorflow.python.util import compat
from tensorflow.python.util import nest
from tensorflow.python.util import tf_decorator
from tensorflow.python.util.lazy_loader import LazyLoader
from tensorflow.python.util.tf_export import tf_export

# Use LazyLoader to avoid circular dependencies.
tensor_spec = LazyLoader("tensor_spec", globals(),
                         "tensorflow.python.framework.tensor_spec")
ops = LazyLoader("ops", globals(), "tensorflow.python.framework.ops")


# TODO(b/133606651) Export this as "TypeSpec" (or experimental.TypeSpec?) and
# deprecate the tf.data.experimental.Structure endpoint.
@tf_export("data.experimental.Structure")
@six.add_metaclass(abc.ABCMeta)
class TypeSpec(object):
    """Specifies a TensorFlow value type.

  A `tf.TypeSpec` provides metadata describing an object accepted or returned
  by TensorFlow APIs.  Concrete subclasses, such as `tf.TensorSpec` and
  `tf.RaggedTensorSpec`, are used to describe different value types.
  """
    # === Subclassing ===
Example #3
0
from __future__ import division
from __future__ import print_function

from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_shape
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gen_list_ops
# go/tf-wildcard-import
# pylint: disable=wildcard-import
from tensorflow.python.ops.gen_list_ops import *
# pylint: enable=wildcard-import
from tensorflow.python.util.lazy_loader import LazyLoader

# list_ops -> control_flow_ops -> tensor_array_ops -> list_ops
control_flow_ops = LazyLoader("control_flow_ops", globals(),
                              "tensorflow.python.ops.control_flow_ops")

ops.NotDifferentiable("TensorListConcatLists")
ops.NotDifferentiable("TensorListElementShape")
ops.NotDifferentiable("TensorListLength")
ops.NotDifferentiable("TensorListPushBackBatch")


def empty_tensor_list(element_shape,
                      element_dtype,
                      max_num_elements=None,
                      name=None):
    if max_num_elements is None:
        max_num_elements = -1

    return gen_list_ops.empty_tensor_list(
Example #4
0
"""Gradient tape utilites."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import contextlib

from tensorflow.python import pywrap_tensorflow
from tensorflow.python.util.lazy_loader import LazyLoader

# There is a circular dependency between this, ops.py, and
# distribution_strategy_context.
# TODO(b/117329403): Remove this circular dependency.
distribution_strategy_context = LazyLoader(
    "distribution_strategy_context", globals(),
    "tensorflow.python.distribute."
    "distribution_strategy_context")


class Tape(object):
  """Represents a gradient propagation trace."""

  def __init__(self, tape):
    self._tape = tape

  def watched_variables(self):
    return pywrap_tensorflow.TFE_Py_TapeWatchedVariables(self._tape)


def push_new_tape(persistent=False, watch_accessed_variables=True):
  """Pushes a new tape onto the tape stack."""
Example #5
0
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import gen_string_ops
from tensorflow.python.ops import string_ops
from tensorflow.python.ops.ragged import ragged_array_ops
from tensorflow.python.ops.ragged import ragged_functional_ops
from tensorflow.python.ops.ragged import ragged_math_ops
from tensorflow.python.ops.ragged import ragged_tensor
from tensorflow.python.util import compat as util_compat
from tensorflow.python.util import deprecation
from tensorflow.python.util import dispatch
from tensorflow.python.util.lazy_loader import LazyLoader
from tensorflow.python.util.tf_export import tf_export


map_fn_lib = LazyLoader("map_fn_lib", globals(),
                        "tensorflow.python.ops.map_fn")


@tf_export("strings.bytes_split")
@dispatch.add_dispatch_support
def string_bytes_split(input, name=None):  # pylint: disable=redefined-builtin
  """Split string elements of `input` into bytes.

  Examples:

  >>> tf.strings.bytes_split('hello').numpy()
  array([b'h', b'e', b'l', b'l', b'o'], dtype=object)
  >>> tf.strings.bytes_split(['hello', '123'])
  <tf.RaggedTensor [[b'h', b'e', b'l', b'l', b'o'], [b'1', b'2', b'3']]>

  Note that this op splits strings into bytes, not unicode characters.  To
from tensorflow.python.framework.auto_control_deps import AutomaticControlDependencies
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import custom_gradient
from tensorflow.python.ops import resource_variable_ops
from tensorflow.python.ops import tensor_array_ops
from tensorflow.python.ops import variable_scope
from tensorflow.python.util import compat
from tensorflow.python.util import memory
from tensorflow.python.util import nest
from tensorflow.python.util import tf_contextlib
from tensorflow.python.util import tf_decorator
from tensorflow.python.util.lazy_loader import LazyLoader

# This is to avoid a circular dependency:
# function -> func_graph
function = LazyLoader("function", globals(),
                      "tensorflow.python.eager.function")
def_function = LazyLoader(
    "def_function", globals(),
    "tensorflow.python.eager.def_function")

WHITELIST_COLLECTIONS = [
    ops.GraphKeys.GLOBAL_VARIABLES,
    ops.GraphKeys.LOCAL_VARIABLES,
    ops.GraphKeys.TRAINABLE_VARIABLES,
    variable_scope._VARSTORE_KEY,  # pylint: disable=protected-access
    variable_scope._VARSCOPESTORE_KEY  # pylint: disable=protected-access
]


class UnknownArgument(object):
  """Signifies an argument which is not currently handled."""
Example #7
0
# pylint: disable=unused-import
from tensorflow.contrib.lite.python.op_hint import convert_op_hints_to_stubs
from tensorflow.contrib.lite.python.op_hint import OpHint
# pylint: enable=unused-import
from tensorflow.contrib.lite.toco import model_flags_pb2 as _model_flags_pb2
from tensorflow.contrib.lite.toco import toco_flags_pb2 as _toco_flags_pb2
from tensorflow.contrib.lite.toco import types_pb2 as _types_pb2
from tensorflow.python.framework import dtypes as _dtypes
from tensorflow.python.platform import resource_loader as _resource_loader
from tensorflow.python.util.all_util import remove_undocumented
from tensorflow.python.util.lazy_loader import LazyLoader

# Lazy load since some of the performance benchmark skylark rules
# break dependencies.
_toco_python = LazyLoader(
    "tensorflow_wrap_toco", globals(), "tensorflow.contrib.lite.toco.python."
    "tensorflow_wrap_toco")
del LazyLoader

# Enum types from the protobuf promoted to the API
FLOAT = _types_pb2.FLOAT
INT32 = _types_pb2.INT32
INT64 = _types_pb2.INT64
STRING = _types_pb2.STRING
QUANTIZED_UINT8 = _types_pb2.QUANTIZED_UINT8
TENSORFLOW_GRAPHDEF = _toco_flags_pb2.TENSORFLOW_GRAPHDEF
TFLITE = _toco_flags_pb2.TFLITE
GRAPHVIZ_DOT = _toco_flags_pb2.GRAPHVIZ_DOT

# Currently the default mode of operation is to shell to another python process
# to protect against crashes. However, it breaks some dependent targets because
Example #8
0
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import tensor_conversion_registry
from tensorflow.python.framework import tensor_shape
from tensorflow.python.framework import type_spec
from tensorflow.python.types import internal
from tensorflow.python.util.compat import collections_abc
from tensorflow.python.util.lazy_loader import LazyLoader
from tensorflow.python.util.tf_export import tf_export

# Use LazyLoader to avoid circular dependencies.
#
# Note: these can all be changed to regular imports once all code has been
# updated to refer the symbols defined in this module directly, rather than
# using the backwards-compatible aliases in ops.py.  (E.g.,
# "indexed_slices.IndexedSlices" rather than "ops.IndexedSlices".)
math_ops = LazyLoader("math_ops", globals(), "tensorflow.python.ops.math_ops")
ops = LazyLoader("ops", globals(), "tensorflow.python.framework.ops")
tensor_spec = LazyLoader("tensor_spec", globals(),
                         "tensorflow.python.framework.tensor_spec")
tensor_util = LazyLoader("tensor_util", globals(),
                         "tensorflow.python.framework.tensor_util")


class IndexedSlicesCompositeTensorGradient(
        composite_tensor_gradient.CompositeTensorGradient):
    """CompositeTensorGradient for IndexedSlices."""
    def get_gradient_components(self, value):
        return value.values

    def replace_gradient_components(self, value, component_grads):
        return IndexedSlices(component_grads, value.indices, value.dense_shape)
from tensorflow.python.keras.utils import version_utils
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.training.tracking import base as trackable
from tensorflow.python.training.tracking import data_structures
from tensorflow.python.util import nest
from tensorflow.python.util import tf_decorator
from tensorflow.python.util import tf_inspect
from tensorflow.python.util.lazy_loader import LazyLoader

# To avoid circular dependencies between keras/engine and keras/saving,
# code in keras/saving must delay imports.

# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
base_layer = LazyLoader("base_layer", globals(),
                        "tensorflow.python.keras.engine.base_layer")
metrics = LazyLoader("metrics", globals(), "tensorflow.python.keras.metrics")
input_layer = LazyLoader("input_layer", globals(),
                         "tensorflow.python.keras.engine.input_layer")
training_lib = LazyLoader("training_lib", globals(),
                          "tensorflow.python.keras.engine.training")
sequential_lib = LazyLoader("sequential_lib", globals(),
                            "tensorflow.python.keras.engine.sequential")
# pylint:enable=g-inconsistent-quotes


def should_skip_serialization(layer):
    """Skip serializing extra objects and functions if layer inputs aren't set."""
    saved_model_input_spec_set = (
        isinstance(layer, training_lib.Model)
        and layer._saved_model_inputs_spec is not None)  # pylint: disable=protected-access
Example #10
0
from tensorflow.python.saved_model import nested_structure_coder
from tensorflow.python.saved_model import revived_types
from tensorflow.python.training.tracking import base as trackable
from tensorflow.python.training.tracking.tracking import delete_tracking
from tensorflow.python.util import compat
from tensorflow.python.util import nest
from tensorflow.python.util import object_identity
from tensorflow.python.util.lazy_loader import LazyLoader

# To avoid circular dependencies between keras/engine and keras/saving,
# code in keras/saving must delay imports.

# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
models_lib = LazyLoader("models_lib", globals(),
                        "tensorflow.python.keras.models")
base_layer = LazyLoader(
    "base_layer", globals(),
    "tensorflow.python.keras.engine.base_layer")
layers_module = LazyLoader(
    "layers_module", globals(),
    "tensorflow.python.keras.layers")
input_layer = LazyLoader(
    "input_layer", globals(),
    "tensorflow.python.keras.engine.input_layer")
network_lib = LazyLoader(
    "network_lib", globals(),
    "tensorflow.python.keras.engine.network")
training_lib = LazyLoader(
    "training_lib", globals(),
    "tensorflow.python.keras.engine.training")
Example #11
0
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Python wrapper for convert models from dense to sparse format."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.python.util.lazy_loader import LazyLoader

# Lazy load since some of the performance benchmark skylark rules
# break dependencies. Must use double quotes to match code internal rewrite
# rule.
_sparsification_wrapper = LazyLoader(
    "_sparsification_wrapper", globals(), "tensorflow.lite.python.optimize."
    "tensorflow_lite_wrap_sparsification_wrapper")


class Sparsifier(object):
    """Convert a model from dense to sparse format.

  This is an internal class, not a public interface.
  """
    def __init__(self, model_content):
        """Constructor.

    Args:
      model_content: Content of a TF-Lite Flatbuffer file.

    Raises:
Example #12
0
"""Gradient tape utilites."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import contextlib

from tensorflow.python import pywrap_tensorflow
from tensorflow.python.util.lazy_loader import LazyLoader

# There is a circular dependency between this, ops.py, and
# distribution_strategy_context.
# TODO(b/117329403): Remove this circular dependency.
distribution_strategy_context = LazyLoader(
    "distribute_lib", globals(), "tensorflow.python.training."
    "distribution_strategy_context")


class Tape(object):
    """Represents a gradient propagation trace."""
    def __init__(self, tape):
        self._tape = tape

    def watched_variables(self):
        return pywrap_tensorflow.TFE_Py_TapeWatchedVariables(self._tape)


def push_new_tape(persistent=False, watch_accessed_variables=True):
    """Pushes a new tape onto the tape stack."""
    tape = pywrap_tensorflow.TFE_Py_TapeSetNew(persistent,
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""The execution context for ClusterCoordinator."""

import contextlib
import threading

from tensorflow.python.util.lazy_loader import LazyLoader

# There is a circular dependency between this and the `cluster_coordinator`
# module. So we load it lazily to work around this.
cluster_coordinator = LazyLoader(
    "cluster_coordinator", globals(),
    "tensorflow.python.distribute.coordinator.cluster_coordinator")

_dispatch_context = threading.local()


def get_current_dispatch_context():
    try:
        return _dispatch_context.current
    except AttributeError:
        return None


@contextlib.contextmanager
def with_dispatch_context(worker_obj):
    previous_context = getattr(_dispatch_context, "current", None)
Example #14
0
import re

from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_shape
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import sparse_ops
from tensorflow.python.platform import tf_logging
from tensorflow.python.util.lazy_loader import LazyLoader
from tensorflow.python.util.tf_export import tf_export

# Avoid circular dependencies with RaggedTensor.
# TODO(b/141170488) Refactor ragged modules so this is unnecessary.
ragged_tensor = LazyLoader("ragged_tensor", globals(),
                           "tensorflow.python.ops.ragged.ragged_tensor")
ragged_math_ops = LazyLoader("ragged_math_ops", globals(),
                             "tensorflow.python.ops.ragged.ragged_math_ops")

# TODO(b/122887740) Refactor code:
#   * Move input verification to feature configuration objects (e.g.,
#     VarLenFeature should check that dtype is a valid dtype).
#   * Add an _add_feature() method to each feature configuration object
#     (rather than using a dispatch table in _ParseOpParams._add_feature).
#   * Update _construct_tensors_for_composite_features() to call a method
#     on the feature object (rather than using dispatch).


@tf_export("io.VarLenFeature", v1=["VarLenFeature", "io.VarLenFeature"])
class VarLenFeature(collections.namedtuple("VarLenFeature", ["dtype"])):
    """Configuration for parsing a variable-length input feature.
Example #15
0
from tensorflow.python.saved_model import save as save_lib
from tensorflow.python.training.tracking import base as trackable
from tensorflow.python.training.tracking import data_structures
from tensorflow.python.training.tracking import layer_utils as trackable_layer_utils
from tensorflow.python.util import nest
from tensorflow.python.util import tf_decorator
from tensorflow.python.util import tf_inspect
from tensorflow.python.util.lazy_loader import LazyLoader

# To avoid circular dependencies between keras/engine and keras/saving,
# code in keras/saving must delay imports.

# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
base_layer = LazyLoader("base_layer", globals(),
                        "tensorflow.python.keras.engine.base_layer")
training_lib = LazyLoader("training_lib", globals(),
                          "tensorflow.python.keras.engine.training")
# pylint:enable=g-inconsistent-quotes


def save(model, filepath, overwrite, include_optimizer, signatures=None):
    """Saves a model as a SavedModel to the filepath.

  Args:
    model: Keras model instance to be saved.
    filepath: String path to save the model.
    overwrite: whether to overwrite the existing filepath.
    include_optimizer: If True, save the model's optimizer state.
    signatures: Signatures to save with the SavedModel. Applicable to the 'tf'
      format only. Please see the `signatures` argument in `tf.saved_model.save`
Example #16
0
from tensorflow.python.framework import tensor_shape
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import resource_variable_ops
from tensorflow.python.ops import variable_scope as vs
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.training import server_lib
from tensorflow.python.training.tracking import base as trackable
from tensorflow.python.util import nest
from tensorflow.python.util import tf_inspect
from tensorflow.python.util.lazy_loader import LazyLoader
from tensorflow.python.util.tf_export import tf_export

ALLOWED_TASK_TYPES = ("chief", "worker", "ps")

cluster_coordinator = LazyLoader(
    "cluster_coordinator", globals(),
    "tensorflow.python.distribute.coordinator.cluster_coordinator"
)

load_context = LazyLoader(
    "load_context", globals(),
    "tensorflow.python.keras.saving.saved_model.load_context"
)


@tf_export(
    "distribute.experimental.ParameterServerStrategy",
    "distribute.ParameterServerStrategy",
    v1=[])
class ParameterServerStrategyV2(distribute_lib.Strategy):
  """An multi-worker tf.distribute strategy with parameter servers.
Example #17
0
import ctypes
import sys
import numpy as np

# pylint: disable=g-import-not-at-top
try:
  from tensorflow.python.util.lazy_loader import LazyLoader
  from tensorflow.python.util.tf_export import tf_export as _tf_export

  # Lazy load since some of the performance benchmark skylark rules
  # break dependencies. Must use double quotes to match code internal rewrite
  # rule.
  # pylint: disable=g-inconsistent-quotes
  _interpreter_wrapper = LazyLoader(
      "_interpreter_wrapper", globals(),
      "tensorflow.lite.python.interpreter_wrapper."
      "tensorflow_wrap_interpreter_wrapper")
  # pylint: enable=g-inconsistent-quotes

  del LazyLoader
except ImportError:
  # When full Tensorflow Python PIP is not available do not use lazy load
  # and instead of the tflite_runtime path.
  from tflite_runtime.lite.python import interpreter_wrapper as _interpreter_wrapper

  def tf_export_dummy(*x, **kwargs):
    del x, kwargs
    return lambda x: x
  _tf_export = tf_export_dummy

Example #18
0
from tensorflow.python.keras.saving.saved_model.serialized_attributes import CommonEndpoints
from tensorflow.python.keras.utils.generic_utils import deserialize_keras_object
from tensorflow.python.saved_model import load as tf_load
from tensorflow.python.training.tracking import base as trackable
from tensorflow.python.training.tracking.tracking import delete_tracking
from tensorflow.python.util import compat
from tensorflow.python.util import nest
from tensorflow.python.util.lazy_loader import LazyLoader

# To avoid circular dependencies between keras/engine and keras/saving,
# code in keras/saving must delay imports.

# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
models_lib = LazyLoader("models_lib", globals(),
                        "tensorflow.python.keras.models")
base_layer = LazyLoader("base_layer", globals(),
                        "tensorflow.python.keras.engine.base_layer")
input_layer = LazyLoader("input_layer", globals(),
                         "tensorflow.python.keras.engine.input_layer")
network_lib = LazyLoader("network_lib", globals(),
                         "tensorflow.python.keras.engine.network")
training_lib = LazyLoader("training_lib", globals(),
                          "tensorflow.python.keras.engine.training")
training_lib_v1 = LazyLoader("training_lib", globals(),
                             "tensorflow.python.keras.engine.training_v1")
# pylint:enable=g-inconsistent-quotes

PUBLIC_ATTRIBUTES = CommonEndpoints.all_functions.union(
    CommonEndpoints.all_checkpointable_objects)
PUBLIC_ATTRIBUTES.add(constants.KERAS_ATTR)
Example #19
0
from tensorflow.contrib import tfprof
from tensorflow.contrib import timeseries
from tensorflow.contrib import tpu
from tensorflow.contrib import training
from tensorflow.contrib import util
from tensorflow.contrib import decent_q
from tensorflow.contrib.eager.python import tfe as eager
from tensorflow.contrib.optimizer_v2 import optimizer_v2_symbols as optimizer_v2
from tensorflow.contrib.receptive_field import receptive_field_api as receptive_field
from tensorflow.contrib.recurrent.python import recurrent_api as recurrent
from tensorflow.contrib.remote_fused_graph import pylib as remote_fused_graph
from tensorflow.contrib.specs import python as specs
from tensorflow.contrib.summary import summary

if os.name != "nt" and platform.machine() != "s390x":
    try:
        from tensorflow.contrib import cloud
    except ImportError:
        pass

from tensorflow.python.util.lazy_loader import LazyLoader
ffmpeg = LazyLoader("ffmpeg", globals(), "tensorflow.contrib.ffmpeg")
del os
del platform

del LazyLoader

del absolute_import
del division
del print_function
Example #20
0
from tensorflow.python.saved_model import utils_impl as saved_model_utils
from tensorflow.python.training import saver as saver_lib
from tensorflow.python.training.tracking import graph_view
from tensorflow.python.util import compat
from tensorflow.python.util import deprecation
from tensorflow.python.util import nest
from tensorflow.python.util.lazy_loader import LazyLoader
from tensorflow.python.util.tf_export import keras_export

# To avoid circular dependencies between keras/engine and keras/saving,
# code in keras/saving must delay imports.

# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
metrics_lib = LazyLoader("metrics_lib", globals(),
                         "tensorflow.python.keras.metrics")
models_lib = LazyLoader("models_lib", globals(),
                        "tensorflow.python.keras.models")
sequential = LazyLoader(
    "sequential", globals(),
    "tensorflow.python.keras.engine.sequential")
# pylint:enable=g-inconsistent-quotes


@deprecation.deprecated(
    date=None,
    instructions=('Please use `model.save(..., save_format="tf")` or '
                  '`tf.keras.models.save_model(..., save_format="tf")`.'))
@keras_export('keras.experimental.export_saved_model')
def export_saved_model(model,
                       saved_model_path,
Example #21
0
from __future__ import print_function

import itertools
import types

from tensorflow.python.eager import context
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.engine import base_layer_utils
from tensorflow.python.keras.utils import tf_utils
from tensorflow.python.training.tracking import layer_utils as trackable_layer_utils
from tensorflow.python.util import tf_decorator
from tensorflow.python.util import tf_inspect
from tensorflow.python.util.lazy_loader import LazyLoader

# pylint:disable=g-inconsistent-quotes
training_lib = LazyLoader("training_lib", globals(),
                          "tensorflow.python.keras.engine.training")
# pylint:enable=g-inconsistent-quotes


def use_wrapped_call(layer,
                     call_fn,
                     default_training_value=None,
                     return_method=False):
    """Creates fn that adds the losses returned by call_fn & returns the outputs.

  Args:
    layer: A Keras layer object
    call_fn: tf.function that takes layer inputs (and possibly a training arg),
      and returns a tuple of (outputs, list of losses).
    default_training_value: Default value of the training kwarg. If `None`, the
      default is `K.learning_phase()`.
Example #22
0
from tensorflow.python.saved_model import builder
from tensorflow.python.saved_model import load
from tensorflow.python.saved_model import loader
from tensorflow.python.saved_model import loader_impl
from tensorflow.python.saved_model import save
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import signature_def_utils
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.saved_model import utils
from tensorflow.python.tools import saved_model_utils
from tensorflow.python.training.tracking import tracking
from tensorflow.python.util.lazy_loader import LazyLoader

_SAVED_MODEL_SIGNATURE_KEY = "mypredict"

gen_trt_ops = LazyLoader("gen_trt_ops", globals(),
                         "tensorflow.compiler.tf2tensorrt.ops.gen_trt_ops")


class TrtConvertTest(test_util.TensorFlowTestCase, parameterized.TestCase):
    """Class to test Tensorflow-TensorRT integration python API."""

    # Use a small max_workspace_size for tests so they don't consume too much GPU
    # memory.
    _TRT_MAX_WORKSPACE_SIZE_BYTES = 2 << 20

    def mkdtemp(self):
        return tempfile.mkdtemp(dir=self.get_temp_dir())

    def testTRTEngineInstanceAvailable(self):
        # test if we can access the TRTEngineInstance protobuf
        assert hasattr(TRTEngineInstance(), "serialized_engine")
Example #23
0
import numpy as np

import tensorflow as tf

from conf import conf
from models.tensorflow.common import TfModel
from utils import get_all_2_element_combinations

tfk = tf.keras
K = tfk.backend
import models.tensorflow.mykeras.layers as mylayers

from tensorflow.python.util.lazy_loader import LazyLoader

pfor_ops = LazyLoader("pfor_ops", globals(),
                      "tensorflow.python.ops.parallel_for.control_flow_ops")


class MonotonicConstraint(tfk.constraints.Constraint):
    def __init__(self, mon_size_in, non_mon_size_in, mon_size_out,
                 non_mon_size_out, blocks):
        self._mon_size_in = mon_size_in
        self._non_mon_size_in = non_mon_size_in
        self._mon_size_out = mon_size_out
        self._non_mon_size_out = non_mon_size_out
        self._blocks = blocks

    def __call__(self, w):
        w_shape = w.shape
        block_in_size = self._mon_size_in + self._non_mon_size_in
        block_out_size = self._mon_size_out + self._non_mon_size_out
# ==============================================================================
"""FeatureColumn serialization, deserialization logic."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import six

from tensorflow.python.feature_column import feature_column_v2 as fc_lib
from tensorflow.python.feature_column import sequence_feature_column as sfc_lib
from tensorflow.python.ops import init_ops
from tensorflow.python.util.lazy_loader import LazyLoader

# Prevent circular dependencies with Keras serialization.
generic_utils = LazyLoader('generic_utils', globals(),
                           'tensorflow.python.keras.utils.generic_utils')

_FEATURE_COLUMNS = [
    fc_lib.BucketizedColumn, fc_lib.CrossedColumn, fc_lib.EmbeddingColumn,
    fc_lib.HashedCategoricalColumn, fc_lib.IdentityCategoricalColumn,
    fc_lib.IndicatorColumn, fc_lib.NumericColumn,
    fc_lib.SequenceCategoricalColumn, fc_lib.SequenceDenseColumn,
    fc_lib.SharedEmbeddingColumn, fc_lib.VocabularyFileCategoricalColumn,
    fc_lib.VocabularyListCategoricalColumn, fc_lib.WeightedCategoricalColumn,
    init_ops.TruncatedNormal, sfc_lib.SequenceNumericColumn
]


def serialize_feature_column(fc):
    """Serializes a FeatureColumn or a raw string key.
Example #25
0
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Python wrapper for post training quantization with calibration."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
from tensorflow.python.util.lazy_loader import LazyLoader

# Lazy load since some of the performance benchmark skylark rules
# break dependencies. Must use double quotes to match code internal rewrite
# rule.
_calibration_wrapper = LazyLoader(
    "_calibration_wrapper", globals(), "tensorflow.lite.python.optimize."
    "tensorflow_lite_wrap_calibration_wrapper")


class Calibrator(object):
    """Calibrates a floating point model and then quantizes it.

  This is an internal class, not a public interface.
  """
    def __init__(self, model_content):
        """Constructor.

    Args:
      model_content: Content of a TF-Lite Flatbuffer file.

    Raises:
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Utility to get tf.distribute.Strategy related contexts."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.python.framework import ops
from tensorflow.python.util.lazy_loader import LazyLoader
from tensorflow.python.util.tf_export import tf_export

# There is a circular dependency between this and `distribute` module. So we
# load it lazily to workaround this.
distribute_lib = LazyLoader("distribute_lib", globals(),
                            "tensorflow.python.distribute.distribute_lib")

# ------------------------------------------------------------------------------
# Internal API for setting the current thread mode as being either in a
# replica or cross-replica context for a particular tf.distribute.Strategy.


class _ThreadMode(object):
    def __init__(self, dist, cross, replica):
        self.strategy = dist
        self.cross_replica_context = cross
        self.replica_context = replica


class _CrossReplicaThreadMode(_ThreadMode):
    def __init__(self, strategy):
if not _six.PY2:
    import typing as _typing
    if _typing.TYPE_CHECKING:
        from tensorflow_estimator.python.estimator.api._v1 import estimator
# pylint: enable=g-import-not-at-top

from tensorflow.python.util.lazy_loader import LazyLoader  # pylint: disable=g-import-not-at-top
_CONTRIB_WARNING = """
The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:
  * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
  * https://github.com/tensorflow/addons
  * https://github.com/tensorflow/io (for I/O related ops)
If you depend on functionality not listed there, please file an issue.
"""
contrib = LazyLoader('contrib', globals(), 'tensorflow.contrib',
                     _CONTRIB_WARNING)
del LazyLoader
# The templated code that replaces the placeholder above sometimes
# sets the __all__ variable. If it does, we have to be sure to add
# "contrib".
if '__all__' in vars():
    vars()['__all__'].append('contrib')

from tensorflow.python.platform import flags  # pylint: disable=g-import-not-at-top
# The 'app' module will be imported as part of the placeholder section above.
_current_module.app.flags = flags  # pylint: disable=undefined-variable
setattr(_current_module, "flags", flags)

_major_api_version = 1

# Add module aliases from Keras to TF.
Example #28
0
from tensorflow.python.ops import gen_array_ops
from tensorflow.python.ops import gen_math_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import resource_variable_ops
from tensorflow.python.ops.unconnected_gradients import UnconnectedGradients
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.util import nest
from tensorflow.python.util import tf_contextlib
from tensorflow.python.util import tf_inspect
from tensorflow.python.util.lazy_loader import LazyLoader
from tensorflow.python.util.tf_export import tf_export

# Note that we need to lazy load the following two modules to avoid creating
# circular dependencies.
# TODO(b/119775953): fix the circular dependencies.
pfor_ops = LazyLoader("pfor_ops", globals(),
                      "tensorflow.python.ops.parallel_for.control_flow_ops")

function = LazyLoader("function", globals(),
                      "tensorflow.python.eager.function")

_op_attr_type_cache = {}


def op_attr_type(op_type, attr_name):
    try:
        return _op_attr_type_cache[(op_type, attr_name)]
    except KeyError:
        context.ensure_initialized()
        h = context.context()._handle  # pylint: disable=protected-access
        attr_type = pywrap_tfe.TFE_OpNameGetAttrType(h, op_type, attr_name)
    _op_attr_type_cache[(op_type, attr_name)] = attr_type
Example #29
0
# ==============================================================================

# Bring in all of the public TensorFlow interface into this
# module.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

# pylint: disable=wildcard-import
print("init_of_tensorflow_init_of_tensorflow_init_of_tensorflow_init_of_tensorflow_init_of_tensorflow_init_of_tensorflow_")
from tensorflow.python import *
# pylint: enable=wildcard-import

from tensorflow.python.util.lazy_loader import LazyLoader
contrib = LazyLoader('contrib', globals(), 'tensorflow.contrib')
del LazyLoader

del absolute_import
del division
del print_function

# These symbols appear because we import the python package which
# in turn imports from tensorflow.core and tensorflow.python. They
# must come from this module. So python adds these symbols for the
# resolution to succeed.
# pylint: disable=undefined-variable
del python
del core
# pylint: enable=undefined-variable
Example #30
0
from tensorflow.core.protobuf import struct_pb2
from tensorflow.python.framework import composite_tensor
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import tensor_shape
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.types import trace
from tensorflow.python.util import _pywrap_utils
from tensorflow.python.util import compat
from tensorflow.python.util import deprecation
from tensorflow.python.util import nest
from tensorflow.python.util import tf_decorator
from tensorflow.python.util.lazy_loader import LazyLoader
from tensorflow.python.util.tf_export import tf_export

# Use LazyLoader to avoid circular dependencies.
tensor_spec = LazyLoader("tensor_spec", globals(),
                         "tensorflow.python.framework.tensor_spec")
ops = LazyLoader("ops", globals(), "tensorflow.python.framework.ops")
# TODO(b/238903802): Remove this dependency.
nested_structure_coder = LazyLoader(
    "nested_structure_coder", globals(),
    "tensorflow.python.saved_model.nested_structure_coder")


@tf_export("TypeSpec", v1=["TypeSpec", "data.experimental.Structure"])
class TypeSpec(trace.TraceType, trace_type.Serializable,
               metaclass=abc.ABCMeta):
    """Specifies a TensorFlow value type.

  A `tf.TypeSpec` provides metadata describing an object accepted or returned
  by TensorFlow APIs.  Concrete subclasses, such as `tf.TensorSpec` and
  `tf.RaggedTensorSpec`, are used to describe different value types.