• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2022 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_LIB_MONITORING_TEST_UTILS_H_
16 #define TENSORFLOW_CORE_LIB_MONITORING_TEST_UTILS_H_
17 
18 #include <cstdint>
19 
20 #include "tensorflow/core/framework/summary.pb.h"
21 #include "tensorflow/core/lib/monitoring/types.h"
22 #include "tensorflow/core/platform/statusor.h"
23 
24 namespace tensorflow {
25 namespace monitoring {
26 namespace testing {
27 
28 // Represents a `HistogramProto` but with a restricted API. This is used by
29 // a `CellReader` to return collected histograms in unit tests.
30 // Refer to core/framework/summary.proto for documentation of relevant fields.
31 class Histogram final {
32  public:
33   Histogram() = default;
Histogram(const HistogramProto & histogram_proto)34   explicit Histogram(const HistogramProto& histogram_proto)
35       : histogram_proto_(histogram_proto) {}
36 
37   // Returns the number of samples.
38   double num() const;
39 
40   // Returns the number of samples in the `bucket`-th bucket.
41   //
42   // The range for a bucket is:
43   //   bucket == 0:  -DBL_MAX .. bucket_limit(0)
44   //   bucket != 0:  bucket_limit(bucket - 1) .. bucket_limit(bucket)
45   double num(size_t bucket) const;
46 
47   // Returns the sum of the samples.
48   double sum() const;
49 
50   // Returns the sum of squares of the samples.
51   double sum_squares() const;
52 
53   // Subtracts the histogram by `other`. This is used by `CellReader` to compute
54   // the delta of the metrics.
55   //
56   // REQUIRES:
57   //   - The histograms have the same bucket boundaries.
58   //   - This histogram has more or equal number of samples than `other` in
59   //     every bucket.
60   // Returns an InvalidArgument error if the requirements are violated.
61   StatusOr<Histogram> Subtract(const Histogram& other) const;
62 
63  private:
64   HistogramProto histogram_proto_;
65 };
66 
67 // Represents a collected `Percentiles` but with a restricted API. Subtracting
68 // two `Percentiles` does not produce a meaningful `Percentiles`, so we only
69 // expose a limited API that supports testing the number and sum of the samples.
70 class Percentiles final {
71  public:
72   Percentiles() = default;
Percentiles(const tensorflow::monitoring::Percentiles & percentiles)73   explicit Percentiles(const tensorflow::monitoring::Percentiles& percentiles)
74       : percentiles_(percentiles) {}
75 
76   // Returns the number of samples.
77   size_t num() const;
78 
79   // Returns the sum of samples.
80   double sum() const;
81 
82   // Subtracts the percentiles by `other`. This is used by `CellReader` to
83   // compute the delta of the metrics.
84   Percentiles Subtract(const Percentiles& other) const;
85 
86  private:
87   tensorflow::monitoring::Percentiles percentiles_;
88 };
89 
90 }  // namespace testing
91 }  // namespace monitoring
92 }  // namespace tensorflow
93 
94 #endif  // TENSORFLOW_CORE_LIB_MONITORING_TEST_UTILS_H_
95