1 /* Copyright 2020 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_ENGINE_UTILS_H_ 17 #define TENSORFLOW_COMPILER_TF2TENSORRT_UTILS_TRT_ENGINE_UTILS_H_ 18 19 #include <string> 20 #include <vector> 21 22 #include "tensorflow/compiler/tf2tensorrt/common/datavec.h" 23 #include "tensorflow/compiler/tf2tensorrt/utils/trt_shape_optimization_profiles.h" 24 #include "tensorflow/core/framework/op_kernel.h" 25 #include "tensorflow/core/framework/tensor.h" 26 #include "tensorflow/core/framework/tensor_shape.h" 27 #include "tensorflow/core/lib/core/status.h" 28 #include "tensorflow/stream_executor/lib/statusor.h" 29 30 #if GOOGLE_CUDA && GOOGLE_TENSORRT 31 #include "third_party/tensorrt/NvInfer.h" 32 33 namespace tensorflow { 34 namespace tensorrt { 35 using ::stream_executor::port::StatusOr; 36 37 // Creates a TensorRT execution context. 38 ExecutionContext CreateExecutionContext(nvinfer1::ICudaEngine* cuda_engine); 39 40 // Gets the binding index of a tensor in an engine. 41 // 42 // The binding index is looked up using the tensor's name and the profile index. 43 // Profile index should be set to zero, if we do not have optimization profiles. 44 Status GetTrtBindingIndex(const char* tensor_name, int profile_index, 45 const nvinfer1::ICudaEngine* cuda_engine, 46 int* binding_index); 47 48 // Sets input buffers for TRT from a list of input tensors. The input tensors 49 // are either defined by ctx or by input_vec. 50 Status SetTrtEngineInputs(nvinfer1::ICudaEngine* cuda_engine, 51 nvinfer1::IExecutionContext* execution_context, 52 const int trt_profile_idx, 53 std::vector<void*>& buffers, bool use_implicit_batch, 54 int num_batch, 55 const TrtShapeOptimizationProfile& profiles, 56 OpKernelContext* ctx = nullptr, 57 const DataVec* input_vec = nullptr); 58 59 // Returns the shape of a binding from TensorRT. 60 // 61 // The binding is identified by its binding_index. The batch_size argument is 62 // ignored if use_implicit_batch==false. The shape is returned in the last 63 // argument. 64 Status GetTrtBindingShape(const nvinfer1::ICudaEngine* cuda_engine, 65 const nvinfer1::IExecutionContext* execution_context, 66 int binding_index, bool use_implicit_batch, 67 int batch_size, TensorShape& shape); 68 69 // Defines output buffers for TRT. The buffers are allocated by ctx, if ctx is 70 // not null. Otherwise it is expected that the outputs DataVec is not null, and 71 // the Tensors in outputs are already allocated. 72 Status SetTrtEngineOutputs(nvinfer1::ICudaEngine* cuda_engine, 73 nvinfer1::IExecutionContext* execution_context, 74 int trt_profile_idx, std::vector<void*>& buffers, 75 bool use_implicit_batch, int batch_size = 0, 76 OpKernelContext* ctx = nullptr, 77 DataVec* outputs = nullptr); 78 79 // Enqueues TensorRT inference job. The batch_size argument is only relevant in 80 // implicit batch mode. 81 Status TrtEnqueue(nvinfer1::IExecutionContext* execution_context, 82 std::vector<void*>& buffers, cudaStream_t stream, 83 bool use_implicit_batch, int batch_size = 1); 84 85 } // namespace tensorrt 86 } // namespace tensorflow 87 88 #endif // GOOGLE_CUDA && GOOGLE_TENSORRT 89 90 #endif // TENSORFLOW_COMPILER_TF2TENSORRT_UTILS_TRT_ENGINE_UTILS_H_ 91