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