xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/tf2tensorrt/utils/trt_int8_calibrator.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2018 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 
16 #ifndef TENSORFLOW_COMPILER_TF2TENSORRT_UTILS_TRT_INT8_CALIBRATOR_H_
17 #define TENSORFLOW_COMPILER_TF2TENSORRT_UTILS_TRT_INT8_CALIBRATOR_H_
18 
19 #include <atomic>
20 #include <string>
21 #include <unordered_map>
22 #include <utility>
23 
24 #include "tensorflow/core/platform/mutex.h"
25 
26 #if GOOGLE_CUDA && GOOGLE_TENSORRT
27 
28 #include "third_party/gpus/cuda/include/cuda_runtime_api.h"
29 #include "third_party/tensorrt/NvInfer.h"
30 
31 namespace tensorflow {
32 namespace tensorrt {
33 // This class provides a 1 element queue to match TFs push model to
34 // TRTs pull model for calibration. When TRT implements a means for
35 // a push calibration This class should be updated accordingly
36 
37 // IInt8EntropyCalibrator2 is preferred for TRT 5.1+.
38 struct TRTInt8Calibrator : public nvinfer1::IInt8EntropyCalibrator2 {
39  public:
40   // Construct a calibrator for future calibration.
41   TRTInt8Calibrator(
42       const std::unordered_map<string, std::pair<void*, size_t>>& dev_buffers,
43       int batch_size, string engine_name);
44 
45   // Construct a finalized calibrator where we don't need to run calibration any
46   // more, as the calibration data is provided.
47   TRTInt8Calibrator(const string& calibration_data);
48 
49   ~TRTInt8Calibrator();
50 
51   int getBatchSize() const noexcept override;
52 
53   bool getBatch(void* bindings[], const char* names[],
54                 int num_bindings) noexcept override;
55 
56   // Feed calibration data to the calibrator, and return true if the data is
57   // accepted. Return false if the calibrator has been terminated.
58   bool setBatch(const std::unordered_map<string, void*>& data,
59                 const cudaStream_t stream);
60 
61   // Wait until the last batch is consumed by the calibrator and set done.
62   void waitAndSetDone();
63 
64   // Notify that calibration is done and future batches provided by setBatch()
65   // will be ignored.
66   void setDone();
67 
68   // If not null, calibration is skipped.
69   const void* readCalibrationCache(std::size_t& length) noexcept override;
70 
71   void writeCalibrationCache(const void* ptr,
72                              std::size_t length) noexcept override;
73 
getCalibrationTableAsStringTRTInt8Calibrator74   const string& getCalibrationTableAsString() { return calibration_table_; }
75 
76  private:
77   const int batch_size_;
78 
79   // mutex for condition_variable
80   mutex cond_mtx_;
81 
82   // condition variable to implement producer-consumer queue for calibration
83   condition_variable cond_;
84 
85   // Is calibration finished?
86   bool done_;
87 
88   // Map to keep tensorrt input buffers and sizes keyed with buffer names
89   std::unordered_map<string, std::pair<void*, size_t>> dev_buffers_;
90 
91   bool calib_running_;
92   bool batch_is_set_;
93 
94   string engine_name_;
95   string calibration_table_;
96 };
97 
98 }  // namespace tensorrt
99 }  // namespace tensorflow
100 
101 #endif  // GOOGLE_CUDA && GOOGLE_TENSORRT
102 #endif  // TENSORFLOW_COMPILER_TF2TENSORRT_UTILS_TRT_INT8_CALIBRATOR_H_
103