• 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_COMPILER_MLIR_QUANTIZATION_TENSORFLOW_CALIBRATOR_CALIBRATOR_SINGLETON_H_
16 #define TENSORFLOW_COMPILER_MLIR_QUANTIZATION_TENSORFLOW_CALIBRATOR_CALIBRATOR_SINGLETON_H_
17 
18 #include <map>
19 #include <string>
20 #include <utility>
21 
22 #include "absl/container/flat_hash_map.h"
23 #include "absl/strings/string_view.h"
24 #include "absl/synchronization/mutex.h"
25 #include "absl/types/optional.h"
26 
27 namespace tensorflow {
28 namespace calibrator {
29 
30 class CalibratorSingleton {
31  public:
32   // Clears the collected information.
33   static void ClearCollectedInformation();
34 
35   // Clears the collected data of the given node id.
36   static void ClearData(absl::string_view id);
37 
38   // Collects min and max values from the TensorFlow operator executions.
39   static void ReportMinMax(absl::string_view id, float min_val, float max_val);
40 
41   // Returns the min and max values of the given id.
42   static std::optional<std::pair<float, float>> GetMinMax(absl::string_view id);
43 
44  private:
45   static CalibratorSingleton& GetInstance();
46   static absl::Mutex lock_;
47 
48   absl::flat_hash_map<std::string, float> id_to_min_;
49   absl::flat_hash_map<std::string, float> id_to_max_;
50 
51   CalibratorSingleton() = default;
52   ~CalibratorSingleton() = default;
53 };
54 
55 }  // namespace calibrator
56 }  // namespace tensorflow
57 
58 #endif  // TENSORFLOW_COMPILER_MLIR_QUANTIZATION_TENSORFLOW_CALIBRATOR_CALIBRATOR_SINGLETON_H_
59