1# Copyright 2017 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"""Experimental API for gathering statistics from `tf.data` pipelines.""" 16from __future__ import absolute_import 17from __future__ import division 18from __future__ import print_function 19 20from tensorflow.python.data.ops import dataset_ops 21from tensorflow.python.framework import dtypes 22from tensorflow.python.framework import ops 23from tensorflow.python.ops import gen_experimental_dataset_ops 24from tensorflow.python.util import deprecation 25from tensorflow.python.util.tf_export import tf_export 26 27 28@deprecation.deprecated(None, "Use `tf.data.experimental.StatsOptions`.") 29def set_stats_aggregator(stats_aggregator, prefix="", counter_prefix=""): 30 """Set the given `stats_aggregator` for aggregating the input dataset stats. 31 32 Args: 33 stats_aggregator: A `tf.data.experimental.StatsAggregator` object. 34 prefix: (Optional) String, all statistics recorded for the input `dataset` 35 will have given `prefix` prepend with the name. 36 counter_prefix: (Optional) String, all statistics recorded as `counters` 37 will have the given `prefix` for the counter. Defaults to "/tensorflow". 38 39 Returns: 40 A `Dataset` transformation function, which can be passed to 41 `tf.data.Dataset.apply`. 42 """ 43 44 def _apply_fn(dataset): 45 return dataset_ops._SetStatsAggregatorDataset( # pylint: disable=protected-access 46 dataset, stats_aggregator, prefix, counter_prefix) 47 48 return _apply_fn 49 50 51@tf_export("data.experimental.bytes_produced_stats") 52def bytes_produced_stats(tag): 53 """Records the number of bytes produced by each element of the input dataset. 54 55 To consume the statistics, associate a `StatsAggregator` with the output 56 dataset. 57 58 Args: 59 tag: String. All statistics recorded by the returned transformation will 60 be associated with the given `tag`. 61 62 Returns: 63 A `Dataset` transformation function, which can be passed to 64 `tf.data.Dataset.apply`. 65 """ 66 67 def _apply_fn(dataset): 68 return _StatsDataset( 69 dataset, gen_experimental_dataset_ops.bytes_produced_stats_dataset, tag) 70 71 return _apply_fn 72 73 74@tf_export("data.experimental.latency_stats") 75def latency_stats(tag): 76 """Records the latency of producing each element of the input dataset. 77 78 To consume the statistics, associate a `StatsAggregator` with the output 79 dataset. 80 81 Args: 82 tag: String. All statistics recorded by the returned transformation will 83 be associated with the given `tag`. 84 85 Returns: 86 A `Dataset` transformation function, which can be passed to 87 `tf.data.Dataset.apply`. 88 """ 89 90 def _apply_fn(dataset): 91 return _StatsDataset( 92 dataset, gen_experimental_dataset_ops.latency_stats_dataset, tag) 93 94 return _apply_fn 95 96 97class _StatsDataset(dataset_ops.UnaryUnchangedStructureDataset): 98 """A `Dataset` that acts as an identity, and also records statistics.""" 99 100 def __init__(self, input_dataset, op_function, tag): 101 self._input_dataset = input_dataset 102 self._op_function = op_function 103 self._tag = ops.convert_to_tensor(tag, dtype=dtypes.string) 104 variant_tensor = self._op_function( 105 self._input_dataset._variant_tensor, # pylint: disable=protected-access 106 self._tag, 107 **self._flat_structure) 108 super(_StatsDataset, self).__init__(input_dataset, variant_tensor) 109