xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/tf2tensorrt/plugin/trt_plugin.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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_PLUGIN_TRT_PLUGIN_H_
17 #define TENSORFLOW_COMPILER_TF2TENSORRT_PLUGIN_TRT_PLUGIN_H_
18 
19 #include <vector>
20 
21 #include "tensorflow/core/platform/logging.h"
22 
23 #if GOOGLE_CUDA && GOOGLE_TENSORRT
24 #include "third_party/tensorrt/NvInfer.h"
25 
26 namespace tensorflow {
27 namespace tensorrt {
28 
29 extern const char* kTfTrtPluginVersion;
30 extern const char* kTfTrtPluginNamespace;
31 
32 // A wrapper class for TensorRT plugin. User application should inherit from
33 // this class to write custom kernels.
34 class TrtPlugin : public nvinfer1::IPluginV2Ext {
35  public:
TrtPlugin()36   TrtPlugin() { setPluginNamespace(kTfTrtPluginNamespace); }
37 
TrtPlugin(const void * serialized_data,size_t length)38   TrtPlugin(const void* serialized_data, size_t length) {}
39 
TrtPlugin(const TrtPlugin & rhs)40   TrtPlugin(const TrtPlugin& rhs) : namespace_(rhs.namespace_) {}
41 
initialize()42   int initialize() noexcept override { return 0; }
43 
terminate()44   void terminate() noexcept override {}
45 
destroy()46   void destroy() noexcept override { delete this; }
47 
setPluginNamespace(const char * plugin_namespace)48   void setPluginNamespace(const char* plugin_namespace) noexcept override {
49     namespace_ = plugin_namespace;
50   }
51 
getPluginNamespace()52   const char* getPluginNamespace() const noexcept override {
53     return namespace_.c_str();
54   }
55 
56  protected:
57   template <typename T>
WriteToBuffer(const T & val,char ** buffer)58   void WriteToBuffer(const T& val, char** buffer) const {
59     *reinterpret_cast<T*>(*buffer) = val;
60     *buffer += sizeof(T);
61   }
62 
63   template <typename T>
ReadFromBuffer(const char ** buffer)64   T ReadFromBuffer(const char** buffer) {
65     T val = *reinterpret_cast<const T*>(*buffer);
66     *buffer += sizeof(T);
67     return val;
68   }
69 
70  private:
71   std::string namespace_;
72 };
73 
74 template <typename T>
75 class TrtPluginRegistrar {
76  public:
TrtPluginRegistrar()77   TrtPluginRegistrar() {
78     getPluginRegistry()->registerCreator(creator, kTfTrtPluginNamespace);
79   }
80 
81  private:
82   T creator;
83 };
84 
85 #define REGISTER_TFTRT_PLUGIN(name)                       \
86   static ::tensorflow::tensorrt::TrtPluginRegistrar<name> \
87       plugin_registrar_##name {}
88 
89 }  // namespace tensorrt
90 }  // namespace tensorflow
91 
92 #endif  // GOOGLE_CUDA && GOOGLE_TENSORRT
93 
94 #endif  // TENSORFLOW_COMPILER_TF2TENSORRT_PLUGIN_TRT_PLUGIN_H_
95