示例#1
0
    def test_bool_gauge(self):
        gauge = monitoring.BoolGauge('test/gauge', 'test gauge')
        gauge.get_cell().set(True)
        self.assertTrue(gauge.get_cell().value())
        gauge.get_cell().set(False)
        self.assertFalse(gauge.get_cell().value())

        gauge1 = monitoring.BoolGauge('test/gauge1', 'test gauge1', 'label1')
        gauge1.get_cell('foo').set(True)
        self.assertTrue(gauge1.get_cell('foo').value())
示例#2
0
class MetricsTest(tf.test.TestCase):

    gauge = monitoring.BoolGauge('/tfmot/metrics/testing', 'testing', 'labels')

    def setUp(self):
        super(MetricsTest, self).setUp()
        self.test_label = tf.keras.layers.Conv2D(1, 1).__class__.__name__
        for label in [
                self.test_label, metrics.MonitorBoolGauge._SUCCESS_LABEL,
                metrics.MonitorBoolGauge._FAILURE_LABEL
        ]:
            MetricsTest.gauge.get_cell(label).set(False)

        with mock.patch.object(metrics.MonitorBoolGauge,
                               'get_usage_gauge',
                               return_value=MetricsTest.gauge):
            self.monitor = metrics.MonitorBoolGauge('testing')

    def test_DecoratorTest(self):
        @self.monitor
        def func(x):
            return x + 1

        self.assertEqual(func(1), 2)
        self.assertTrue(
            MetricsTest.gauge.get_cell(
                metrics.MonitorBoolGauge._SUCCESS_LABEL).value())

    def test_DecoratorFailureTest(self):
        @self.monitor
        def func(x):
            raise ValueError()

        with self.assertRaises(ValueError):
            func(1)
        self.assertTrue(
            MetricsTest.gauge.get_cell(
                metrics.MonitorBoolGauge._FAILURE_LABEL).value())

    def test_UndecoratedTest(self):
        with self.assertRaises(ValueError):

            @metrics.MonitorBoolGauge('unknown')
            def func(x):
                return x + 1

            func(1)

    def test_SetTest(self):
        self.monitor.set(self.test_label)
        self.assertTrue(MetricsTest.gauge.get_cell(self.test_label).value())
示例#3
0
# pylint:disable=g-direct-tensorflow-import
from tensorflow.core.protobuf import struct_pb2  # TF internal
from tensorflow.python import tf2 as tf2_checker  # TF internal
from tensorflow.python.eager import monitoring  # TF internal
from tensorflow.python.saved_model import nested_structure_coder  # TF internal
# pylint:enable=g-direct-tensorflow-import

try:
    importlib.import_module('tf_agents.utils.allow_tf1')
except ImportError:
    _TF1_MODE_ALLOWED = False
else:
    _TF1_MODE_ALLOWED = True

tf_agents_gauge = monitoring.BoolGauge('/tensorflow/agents/agents',
                                       'TF-Agents usage', 'method')

MISSING_RESOURCE_VARIABLES_ERROR = """
Resource variables are not enabled.  Please enable them by adding the following
code to your main() method:
  tf.compat.v1.enable_resource_variables()
For unit tests, subclass `tf_agents.utils.test_utils.TestCase`.
"""


def check_tf1_allowed():
    """Raises an error if running in TF1 (non-eager) mode and this is disabled."""
    if _TF1_MODE_ALLOWED:
        return
    if not tf2_checker.enabled():
        raise RuntimeError(
示例#4
0
"""Helper classes for tensor shape inference."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import six

from tensorflow.core.framework import tensor_shape_pb2
from tensorflow.python import tf2
from tensorflow.python.eager import monitoring
from tensorflow.python.util.tf_export import tf_export

_TENSORSHAPE_V2_OVERRIDE = None

_api_usage_gauge = monitoring.BoolGauge(
    "/tensorflow/api/v2_tensorshape",
    "Whether tensor_shape.enable_v2_tensorshape() is called.")


@tf_export(v1=["enable_v2_tensorshape"])
def enable_v2_tensorshape():
    """In TensorFlow 2.0, iterating over a TensorShape instance returns values.

  This enables the new behavior.

  Concretely, `tensor_shape[i]` returned a Dimension instance in V1, but
  it V2 it returns either an integer, or None.

  Examples:

  ```
示例#5
0
from tensorflow.python.compat import v2_compat

from tensorflow.python.util.all_util import make_all
from tensorflow.python.util.tf_export import tf_export

# Eager execution
from tensorflow.python.eager.context import executing_eagerly
from tensorflow.python.eager.remote import connect_to_remote_host
from tensorflow.python.eager.def_function import function
from tensorflow.python.framework.ops import enable_eager_execution

# Check whether TF2_BEHAVIOR is turned on.
from tensorflow.python.eager import monitoring as _monitoring
from tensorflow.python import tf2 as _tf2
_tf2_gauge = _monitoring.BoolGauge(
    '/tensorflow/api/tf2_enable', 'Environment variable TF2_BEHAVIOR is set".')
_tf2_gauge.get_cell().set(_tf2.enabled())

# Necessary for the symbols in this module to be taken into account by
# the namespace management system (API decorators).
from tensorflow.python.ops import rnn
from tensorflow.python.ops import rnn_cell

# TensorFlow Debugger (tfdbg).
from tensorflow.python.debug.lib import check_numerics_callback
from tensorflow.python.debug.lib import dumping_callback
from tensorflow.python.ops import gen_debug_ops

# DLPack
from tensorflow.python.dlpack.dlpack import from_dlpack
from tensorflow.python.dlpack.dlpack import to_dlpack
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import errors
from tensorflow.python.framework import ops
from tensorflow.python.framework import sparse_tensor
from tensorflow.python.framework import type_spec
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.engine import training_generator_v1
from tensorflow.python.keras.engine.base_layer import Layer
from tensorflow.python.keras.utils import tf_utils
from tensorflow.python.ops import sparse_ops
from tensorflow.python.ops.ragged import ragged_tensor
from tensorflow.python.util.tf_export import keras_export


keras_kpl_gauge = monitoring.BoolGauge(
    '/tensorflow/api/keras/layers/preprocessing',
    'keras preprocessing layers usage', 'method')


@keras_export('keras.layers.experimental.preprocessing.PreprocessingLayer')
@six.add_metaclass(abc.ABCMeta)
class PreprocessingLayer(Layer):
  """Base class for PreprocessingLayers."""
  _must_restore_from_config = True

  def adapt(self, data, reset_state=True):
    # TODO(momernick): Add examples.
    """Fits the state of the preprocessing layer to the data being passed.

    Arguments:
        data: The data to train on. It can be passed either as a tf.data
示例#7
0
# ==============================================================================
"""Keras estimator API."""

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

from tensorflow.python.eager import monitoring
from tensorflow.python.util.tf_export import keras_export

# Keras has undeclared dependency on tensorflow/estimator:estimator_py.
# As long as you depend //third_party/py/tensorflow:tensorflow target
# everything will work as normal.

_model_to_estimator_usage_gauge = monitoring.BoolGauge(
    '/tensorflow/api/keras/model_to_estimator',
    'Whether tf.keras.estimator.model_to_estimator() is called.', 'version')


# LINT.IfChange
@keras_export(v1=['keras.estimator.model_to_estimator'])
def model_to_estimator(
    keras_model=None,
    keras_model_path=None,
    custom_objects=None,
    model_dir=None,
    config=None,
    checkpoint_format='saver'):
  """Constructs an `Estimator` instance from given keras model.

  If you use infrastructure or other tooling that relies on Estimators, you can
示例#8
0
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import importlib

from tensorflow.python.eager import monitoring
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.util import fast_module_type
from tensorflow.python.util import tf_decorator
from tensorflow.python.util import tf_inspect
from tensorflow.tools.compatibility import all_renames_v2

FastModuleType = fast_module_type.get_fast_module_type_class()
_PER_MODULE_WARNING_LIMIT = 1
compat_v1_usage_gauge = monitoring.BoolGauge('/tensorflow/api/compat/v1',
                                             'compat.v1 usage')


def get_rename_v2(name):
  if name not in all_renames_v2.symbol_renames:
    return None
  return all_renames_v2.symbol_renames[name]


def _call_location():
  """Extracts the caller filename and line number as a string.

  Returns:
    A string describing the caller source location.
  """
  frame = tf_inspect.currentframe()
示例#9
0
from tensorflow.python.data.experimental.ops import interleave_ops
from tensorflow.python.data.experimental.ops import random_ops
from tensorflow.python.data.experimental.ops import readers as exp_readers
from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.data.ops import readers
from tensorflow.python.eager import monitoring
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_shape
from tensorflow.python.ops import control_flow_v2_toggles
from tensorflow.python.ops import variable_scope

from tensorflow.python.util.tf_export import tf_export

# Metrics to track the status of v2_behavior
_v2_behavior_usage_gauge = monitoring.BoolGauge(
    "/tensorflow/version/v2_behavior",
    "whether v2_behavior is enabled or disabled", "status")


@tf_export(v1=["enable_v2_behavior"])
def enable_v2_behavior():
    """Enables TensorFlow 2.x behaviors.

  This function can be called at the beginning of the program (before `Tensors`,
  `Graphs` or other structures have been created, and before devices have been
  initialized. It switches all global behaviors that are different between
  TensorFlow 1.x and 2.x to behave as intended for 2.x.

  This function is called in the main TensorFlow `__init__.py` file, user should
  not need to call it, except during complex migrations.
  """
示例#10
0
class MonitorBoolGauge():
    """Monitoring utility class for usage metrics."""

    _PRUNE_FOR_BENCHMARK_USAGE = monitoring.BoolGauge(
        '/tfmot/api/sparsity/prune_for_benchmark',
        'prune_for_benchmark usage.', 'status')

    _PRUNE_LOW_MAGNITUDE_USAGE = monitoring.BoolGauge(
        '/tfmot/api/sparsity/prune_low_magnitude',
        'prune_low_magnitude usage.', 'status')

    _PRUNE_WRAPPER_USAGE = monitoring.BoolGauge(
        '/tfmot/api/sparsity/pruning_wrapper', 'Pruning wrapper class usage.',
        'layer')

    _QUANTIZE_APPLY_USAGE = monitoring.BoolGauge(
        '/tfmot/api/quantization/quantize_apply', 'quantize_apply usage.',
        'status')

    _QUANTIZE_WRAPPER_USAGE = monitoring.BoolGauge(
        '/tfmot/api/quantization/quantize_wrapper',
        'Quantize wrapper class usage.', 'layer')

    _SUCCESS_LABEL = 'success'
    _FAILURE_LABEL = 'failure'

    def __init__(self, name):
        self.bool_gauge = self.get_usage_gauge(name)

    def get_usage_gauge(self, name):
        """Gets a gauge by name."""
        if name == 'prune_for_benchmark_usage':
            return MonitorBoolGauge._PRUNE_FOR_BENCHMARK_USAGE
        if name == 'prune_low_magnitude_usage':
            return MonitorBoolGauge._PRUNE_LOW_MAGNITUDE_USAGE
        if name == 'prune_low_magnitude_wrapper_usage':
            return MonitorBoolGauge._PRUNE_WRAPPER_USAGE
        if name == 'quantize_apply_usage':
            return MonitorBoolGauge._QUANTIZE_APPLY_USAGE
        if name == 'quantize_wrapper_usage':
            return MonitorBoolGauge._QUANTIZE_WRAPPER_USAGE
        raise ValueError('Invalid gauge name: {}'.format(name))

    def __call__(self, func):
        def inner(*args, **kwargs):
            try:
                results = func(*args, **kwargs)
                self.bool_gauge.get_cell(
                    MonitorBoolGauge._SUCCESS_LABEL).set(True)
                return results
            except Exception as error:
                self.bool_gauge.get_cell(
                    MonitorBoolGauge._FAILURE_LABEL).set(True)
                raise error

        if self.bool_gauge:
            return inner

        return func

    def set(self, label=None, value=True):
        """Set the bool gauge to value if initialized.

    Args:
      label: optional string label defaults to None.
      value: optional bool value defaults to True.
    """
        if self.bool_gauge:
            self.bool_gauge.get_cell(label).set(value)