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.contrib.data.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, 70 gen_experimental_dataset_ops.experimental_bytes_produced_stats_dataset, 71 tag) 72 73 return _apply_fn 74 75 76@tf_export("data.experimental.latency_stats") 77def latency_stats(tag): 78 """Records the latency of producing each element of the input dataset. 79 80 To consume the statistics, associate a `StatsAggregator` with the output 81 dataset. 82 83 Args: 84 tag: String. All statistics recorded by the returned transformation will 85 be associated with the given `tag`. 86 87 Returns: 88 A `Dataset` transformation function, which can be passed to 89 `tf.data.Dataset.apply`. 90 """ 91 92 def _apply_fn(dataset): 93 return _StatsDataset( 94 dataset, 95 gen_experimental_dataset_ops.experimental_latency_stats_dataset, tag) 96 97 return _apply_fn 98 99 100class _StatsDataset(dataset_ops.UnaryUnchangedStructureDataset): 101 """A `Dataset` that acts as an identity, and also records statistics.""" 102 103 def __init__(self, input_dataset, op_function, tag): 104 self._input_dataset = input_dataset 105 self._op_function = op_function 106 self._tag = ops.convert_to_tensor(tag, dtype=dtypes.string) 107 variant_tensor = self._op_function( 108 self._input_dataset._variant_tensor, # pylint: disable=protected-access 109 self._tag, 110 **dataset_ops.flat_structure(self)) 111 super(_StatsDataset, self).__init__(input_dataset, variant_tensor) 112