1 /** 2 * Copyright 2021 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef MINDSPORE_LITE_SRC_RUNTIME_DELEGATE_TENSORRT_BUILDER_ 17 #define MINDSPORE_LITE_SRC_RUNTIME_DELEGATE_TENSORRT_BUILDER_ 18 #include <NvInfer.h> 19 #include "include/errorcode.h" 20 #include "src/delegate/tensorrt/tensorrt_utils.h" 21 #include "src/delegate/tensorrt/tensorrt_allocator.h" 22 #define MAX_BATCH_SIZE 64 23 24 using mindspore::lite::RET_ERROR; 25 using mindspore::lite::RET_OK; 26 27 namespace mindspore::lite { 28 class TensorRTLogger : public nvinfer1::ILogger { log(Severity severity,const char * msg)29 void log(Severity severity, const char *msg) noexcept override { 30 if (severity == Severity::kINTERNAL_ERROR || severity == Severity::kERROR) { 31 MS_LOG(ERROR) << msg; 32 } else if (severity == Severity::kWARNING) { 33 MS_LOG(WARNING) << msg; 34 } else if (severity == Severity::kINFO) { 35 MS_LOG(INFO) << msg; 36 } else { 37 MS_LOG(DEBUG) << msg; 38 } 39 } 40 }; 41 42 class TensorRTRuntime { 43 public: 44 TensorRTRuntime() = default; 45 46 ~TensorRTRuntime(); 47 48 int Init(); 49 GetBuilder()50 nvinfer1::IBuilder *GetBuilder() { return this->builder_; } 51 GetBatchSize()52 int GetBatchSize() { return batch_size_; } 53 SetBatchSize(int batch_size)54 void SetBatchSize(int batch_size) { batch_size_ = batch_size; } 55 GetAllocator()56 TensorRTAllocator *GetAllocator() { return this->allocator_; } 57 58 private: 59 bool is_init_ = false; 60 nvinfer1::IBuilder *builder_{nullptr}; 61 TensorRTLogger logger_; 62 TensorRTAllocator *allocator_{nullptr}; 63 int batch_size_{0}; 64 }; 65 } // namespace mindspore::lite 66 #endif // MINDSPORE_LITE_SRC_RUNTIME_DELEGATE_TENSORRT_BUILDER_ 67