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