1 /* Copyright 2019 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 #include "tensorflow/core/platform/env.h"
16 #include "tensorflow/stream_executor/platform/dso_loader.h"
17 #include "third_party/tensorrt/NvInfer.h"
18 
19 // Implements the TensorRT API by forwarding to TensorRT loaded from the DSO.
20 
21 namespace {
22 // Returns DSO handle or null if loading the DSO fails.
GetDsoHandle()23 void* GetDsoHandle() {
24 #ifdef PLATFORM_GOOGLE
25   return nullptr;
26 #else
27   static auto handle = []() -> void* {
28     auto handle_or =
29         stream_executor::internal::DsoLoader::GetNvInferDsoHandle();
30     if (!handle_or.ok()) return nullptr;
31     return handle_or.ValueOrDie();
32   }();
33   return handle;
34 #endif
35 }
36 
37 template <typename T>
LoadSymbol(const char * symbol_name)38 T LoadSymbol(const char* symbol_name) {
39   void* symbol = nullptr;
40   if (auto handle = GetDsoHandle()) {
41     tensorflow::Env::Default()
42         ->GetSymbolFromLibrary(handle, symbol_name, &symbol)
43         .IgnoreError();
44   }
45   return reinterpret_cast<T>(symbol);
46 }
47 
LogFatalSymbolNotFound(const char * symbol_name)48 void LogFatalSymbolNotFound(const char* symbol_name) {
49   LOG(FATAL) << symbol_name << " symbol not found.";
50 }
51 }  // namespace
52 
53 #if NV_TENSORRT_MAJOR < 5
54 #error TensorRT version earlier than 5 is not supported.
55 #elif NV_TENSORRT_MINOR < 1
56 #include "tensorflow/compiler/tf2tensorrt/stub/NvInfer_5_0.inc"
57 #else
58 #include "tensorflow/compiler/tf2tensorrt/stub/NvInfer_5_1.inc"
59 #endif
60