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_CONTRIB_BOOSTED_TREES_RESOURCES_QUANTILE_STREAM_RESOURCE_H_ 16 #define TENSORFLOW_CONTRIB_BOOSTED_TREES_RESOURCES_QUANTILE_STREAM_RESOURCE_H_ 17 18 #include "tensorflow/contrib/boosted_trees/lib/quantiles/weighted_quantiles_stream.h" 19 #include "tensorflow/contrib/boosted_trees/proto/quantiles.pb.h" // NOLINT 20 #include "tensorflow/contrib/boosted_trees/resources/stamped_resource.h" 21 #include "tensorflow/core/framework/resource_mgr.h" 22 #include "tensorflow/core/platform/macros.h" 23 #include "tensorflow/core/platform/mutex.h" 24 25 namespace tensorflow { 26 namespace boosted_trees { 27 28 using QuantileStream = 29 boosted_trees::quantiles::WeightedQuantilesStream<float, float>; 30 31 // Resource for accumulating summaries for multiple columns. 32 class QuantileStreamResource : public StampedResource { 33 public: QuantileStreamResource(const float epsilon,const int32 num_quantiles,const int64 max_elements,bool generate_quantiles,int64 stamp_token)34 QuantileStreamResource(const float epsilon, const int32 num_quantiles, 35 const int64 max_elements, bool generate_quantiles, 36 int64 stamp_token) 37 : stream_(epsilon, max_elements), 38 are_buckets_ready_(false), 39 epsilon_(epsilon), 40 num_quantiles_(num_quantiles), 41 max_elements_(max_elements), 42 generate_quantiles_(generate_quantiles) { 43 set_stamp(stamp_token); 44 } 45 DebugString()46 string DebugString() const override { return "QuantileStreamResource"; } 47 mutex()48 tensorflow::mutex* mutex() { return &mu_; } 49 stream(int64 stamp)50 QuantileStream* stream(int64 stamp) { 51 CHECK(is_stamp_valid(stamp)); 52 return &stream_; 53 } 54 boundaries(int64 stamp)55 const std::vector<float>& boundaries(int64 stamp) { 56 CHECK(is_stamp_valid(stamp)); 57 return boundaries_; 58 } 59 set_boundaries(int64 stamp,const std::vector<float> & boundaries)60 void set_boundaries(int64 stamp, const std::vector<float>& boundaries) { 61 CHECK(is_stamp_valid(stamp)); 62 are_buckets_ready_ = true; 63 boundaries_ = boundaries; 64 } 65 epsilon()66 float epsilon() const { return epsilon_; } num_quantiles()67 int32 num_quantiles() const { return num_quantiles_; } 68 Reset(int64 stamp)69 void Reset(int64 stamp) { 70 set_stamp(stamp); 71 stream_ = QuantileStream(epsilon_, max_elements_); 72 } 73 are_buckets_ready()74 bool are_buckets_ready() const { return are_buckets_ready_; } set_buckets_ready(bool are_buckets_ready)75 void set_buckets_ready(bool are_buckets_ready) { 76 are_buckets_ready_ = are_buckets_ready; 77 } 78 generate_quantiles()79 bool generate_quantiles() const { return generate_quantiles_; } set_generate_quantiles(bool generate_quantiles)80 void set_generate_quantiles(bool generate_quantiles) { 81 generate_quantiles_ = generate_quantiles; 82 } 83 84 private: ~QuantileStreamResource()85 ~QuantileStreamResource() override {} 86 87 // Mutex for the whole resource. 88 tensorflow::mutex mu_; 89 90 // Quantile stream. 91 QuantileStream stream_; 92 93 // Stores the boundaries from the previous iteration. Empty during the first 94 // iteration. 95 std::vector<float> boundaries_; 96 97 // Whether boundaries are created. Initially boundaries are empty until 98 // set_boundaries are called. 99 bool are_buckets_ready_; 100 101 const float epsilon_; 102 const int32 num_quantiles_; 103 // An upper-bound for the number of elements. 104 int64 max_elements_; 105 106 // Generate quantiles instead of approximate boundaries. 107 // If true, exactly `num_quantiles` will be produced in the final summary. 108 bool generate_quantiles_; 109 110 TF_DISALLOW_COPY_AND_ASSIGN(QuantileStreamResource); 111 }; 112 113 } // namespace boosted_trees 114 } // namespace tensorflow 115 116 #endif // TENSORFLOW_CONTRIB_BOOSTED_TREES_RESOURCES_QUANTILE_STREAM_RESOURCE_H_ 117