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 #ifndef TENSORFLOW_CORE_FRAMEWORK_STATS_AGGREGATOR_H_ 16 #define TENSORFLOW_CORE_FRAMEWORK_STATS_AGGREGATOR_H_ 17 18 #include <memory> 19 #include <string> 20 21 #include "tensorflow/core/framework/resource_mgr.h" 22 #include "tensorflow/core/lib/gtl/array_slice.h" 23 24 namespace tensorflow { 25 26 class Summary; 27 28 namespace data { 29 30 // A `StatsAggregator` accumulates statistics incrementally. A 31 // `StatsAggregator` can accumulate multiple different statistics, distinguished 32 // by a string name. 33 // 34 // The class currently supports accumulating `Histogram` objects, and we expect 35 // to add other methods in future. 36 // 37 // NOTE(mrry): `StatsAggregator` is a virtual interface because we anticipate 38 // that many different implementations will the same interface. For example, the 39 // current implementation in "stats_aggregator_ops.cc" is a simple in-memory 40 // implementation that integrates with the pull-based summary API, and we may 41 // add implementations that work with the push-based `SummaryWriterInterface`, 42 // as well as custom monitoring services. 43 class StatsAggregator { 44 public: ~StatsAggregator()45 virtual ~StatsAggregator() {} 46 47 // Add the given `values` to the histogram with the given `name`. Each 48 // element of `values` will be treated as a separate sample in the histogram. 49 virtual void AddToHistogram(const string& name, 50 gtl::ArraySlice<double> values) = 0; 51 52 // TODO(shivaniagarawal): consistency in double and float usage. 53 // Add the given `value` as Scalar with the given `name`. 54 virtual void AddScalar(const string& name, float value) = 0; 55 56 // Stores a protocol buffer representation of the aggregator state in the 57 // given `out_summary`. 58 // TODO(mrry): Consider separating this method from the `StatsAggregator` 59 // interface. It is possible that not all implementations will support 60 // encoding their state as a protocol buffer. 61 virtual void EncodeToProto(Summary* out_summary) = 0; 62 63 // Increment the `label` cell of metrics mapped with `name` by given `value`. 64 virtual void IncrementCounter(const string& name, const string& label, 65 int64 val) = 0; 66 }; 67 68 // A `StatsAggregatorResource` wraps a shareable `StatsAggregator` as a resource 69 // in the TensorFlow resource manager. 70 // 71 // NOTE(mrry): This class is separate from `StatsAggregator` in order to 72 // simplify the memory management of the shared object. Most users of 73 // `StatsAggregator` interact with a `std::shared_ptr<StatsAggregator>` whereas 74 // the `ResourceBase` API requires explicit reference counting. 75 class StatsAggregatorResource : public ResourceBase { 76 public: 77 // Creates a new resource from the given `stats_aggregator`. StatsAggregatorResource(std::unique_ptr<StatsAggregator> stats_aggregator)78 StatsAggregatorResource(std::unique_ptr<StatsAggregator> stats_aggregator) 79 : stats_aggregator_(stats_aggregator.release()) {} 80 81 // Returns the wrapped `StatsAggregator`. stats_aggregator()82 std::shared_ptr<StatsAggregator> stats_aggregator() const { 83 return stats_aggregator_; 84 } 85 DebugString()86 string DebugString() const override { return "StatsAggregatorResource"; } 87 88 private: 89 const std::shared_ptr<StatsAggregator> stats_aggregator_; 90 }; 91 92 } // namespace data 93 } // namespace tensorflow 94 95 #endif // TENSORFLOW_CORE_FRAMEWORK_STATS_AGGREGATOR_H_ 96