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 #include "tensorflow/contrib/tensorrt/shape_fn/trt_shfn.h" 17 18 #include <string> 19 #include <vector> 20 21 #if GOOGLE_CUDA 22 #if GOOGLE_TENSORRT 23 #include "tensorflow/contrib/tensorrt/log/trt_logger.h" 24 #include "tensorflow/core/lib/core/errors.h" 25 #include "tensorrt/include/NvInfer.h" 26 27 namespace tensorflow { 28 namespace shape_inference { 29 30 tensorflow::Status TRTEngineOpShapeInference(InferenceContext* context) { 31 tensorflow::tensorrt::Logger logger; 32 string serialized_engine; 33 TF_RETURN_IF_ERROR(context->GetAttr("serialized_engine", &serialized_engine)); 34 nvinfer1::IRuntime* infer = nvinfer1::createInferRuntime(logger); 35 nvinfer1::ICudaEngine* trt_engine = infer->deserializeCudaEngine( 36 serialized_engine.c_str(), serialized_engine.size(), nullptr); 37 38 int num_batch = -1; 39 std::vector<::tensorflow::DataType> input_type; 40 TF_RETURN_IF_ERROR(context->GetAttr("InT", &input_type)); 41 for (size_t i = 0; i < context->num_inputs(); i++) { 42 // Check if input shape is legit 43 auto input_shape = context->input(i); 44 for (int j = 0; j < context->Rank(input_shape); j++) { 45 auto dim_handler = context->Dim(input_shape, j); 46 if (j == 0) { 47 if (i == 0) { 48 num_batch = context->Value(dim_handler); 49 } else if (num_batch != context->Value(dim_handler)) { 50 // TODO(jie): TensorRT engine requires consistent batch between inputs 51 // tensors. Segmenter should be aware of this. 52 LOG(FATAL) << "TensorRT engine requires consistent batch size"; 53 } 54 } 55 } 56 } 57 58 // Arrange input here 59 std::vector<string> input_nodes; 60 TF_RETURN_IF_ERROR(context->GetAttr("input_nodes", &input_nodes)); 61 62 // Arrange output here 63 std::vector<string> output_nodes; 64 TF_RETURN_IF_ERROR(context->GetAttr("output_nodes", &output_nodes)); 65 for (size_t i = 0; i < output_nodes.size(); i++) { 66 int binding_index = trt_engine->getBindingIndex(output_nodes[i].c_str()); 67 ShapeHandle output_shape; 68 std::vector<DimensionHandle> dim_vec; 69 dim_vec.emplace_back(context->MakeDim(num_batch)); 70 if (binding_index != -1) { 71 auto dims = trt_engine->getBindingDimensions(binding_index); 72 for (int j = 0; j < dims.nbDims; j++) { 73 dim_vec.emplace_back(context->MakeDim(dims.d[j])); 74 } 75 } else { 76 LOG(FATAL) << "TensorRT engine cannot find binding: " << output_nodes[i]; 77 } 78 output_shape = context->MakeShape(dim_vec); 79 context->set_output(i, output_shape); 80 } 81 82 return Status::OK(); 83 } 84 85 } // namespace shape_inference 86 } // namespace tensorflow 87 88 #endif // GOOGLE_TENSORRT 89 #endif // GOOGLE_CUDA 90