1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14==============================================================================*/ 15 16#include "tensorflow/core/lib/monitoring/sampler.h" 17 18// We replace this implementation with a null implementation for mobile 19// platforms. 20#include "tensorflow/core/platform/platform.h" 21#ifdef IS_MOBILE_PLATFORM 22// Do nothing. 23#else 24 25namespace tensorflow { 26namespace monitoring { 27namespace { 28 29class ExplicitBuckets : public Buckets { 30 public: 31 ~ExplicitBuckets() override = default; 32 33 explicit ExplicitBuckets(std::vector<double> bucket_limits) 34 : bucket_limits_(std::move(bucket_limits)) { 35 CHECK_GT(bucket_limits_.size(), 0); 36 // Verify that the bucket boundaries are strictly increasing 37 for (size_t i = 1; i < bucket_limits_.size(); i++) { 38 CHECK_GT(bucket_limits_[i], bucket_limits_[i - 1]); 39 } 40 // We augment the bucket limits so that all boundaries are within [-DBL_MAX, 41 // DBL_MAX]. 42 // 43 // Since we use ThreadSafeHistogram, we don't have to explicitly add 44 // -DBL_MAX, because it uses these limits as upper-bounds, so 45 // bucket_count[0] is always the number of elements in 46 // [-DBL_MAX, bucket_limits[0]). 47 if (bucket_limits_.back() != DBL_MAX) { 48 bucket_limits_.push_back(DBL_MAX); 49 } 50 } 51 52 const std::vector<double>& explicit_bounds() const override { 53 return bucket_limits_; 54 } 55 56 private: 57 std::vector<double> bucket_limits_; 58 59 TF_DISALLOW_COPY_AND_ASSIGN(ExplicitBuckets); 60}; 61 62class ExponentialBuckets : public Buckets { 63 public: 64 ~ExponentialBuckets() override = default; 65 66 ExponentialBuckets(double scale, double growth_factor, int bucket_count) 67 : explicit_buckets_( 68 ComputeBucketLimits(scale, growth_factor, bucket_count)) {} 69 70 const std::vector<double>& explicit_bounds() const override { 71 return explicit_buckets_.explicit_bounds(); 72 } 73 74 private: 75 static std::vector<double> ComputeBucketLimits(double scale, 76 double growth_factor, 77 int bucket_count) { 78 CHECK_GT(bucket_count, 0); 79 std::vector<double> bucket_limits; 80 double bound = scale; 81 for (int i = 0; i < bucket_count; i++) { 82 bucket_limits.push_back(bound); 83 bound *= growth_factor; 84 } 85 return bucket_limits; 86 } 87 88 ExplicitBuckets explicit_buckets_; 89 90 TF_DISALLOW_COPY_AND_ASSIGN(ExponentialBuckets); 91}; 92 93} // namespace 94 95// static 96std::unique_ptr<Buckets> Buckets::Explicit( 97 std::initializer_list<double> bucket_limits) { 98 return std::unique_ptr<Buckets>(new ExplicitBuckets(bucket_limits)); 99} 100 101// static 102std::unique_ptr<Buckets> Buckets::Exponential(double scale, 103 double growth_factor, 104 int bucket_count) { 105 return std::unique_ptr<Buckets>( 106 new ExponentialBuckets(scale, growth_factor, bucket_count)); 107} 108 109} // namespace monitoring 110} // namespace tensorflow 111 112#endif // IS_MOBILE_PLATFORM 113