xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/tf2tensorrt/convert/op_converter_registry.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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 #ifndef TENSORFLOW_COMPILER_TF2TENSORRT_CONVERT_OP_CONVERTER_REGISTRY_H_
16 #define TENSORFLOW_COMPILER_TF2TENSORRT_CONVERT_OP_CONVERTER_REGISTRY_H_
17 
18 #include <initializer_list>
19 #if GOOGLE_CUDA && GOOGLE_TENSORRT
20 
21 #include <array>
22 #include <type_traits>
23 #include <vector>
24 
25 #include "tensorflow/compiler/tf2tensorrt/convert/op_converter.h"
26 #include "tensorflow/core/lib/core/status.h"
27 
28 namespace tensorflow {
29 namespace tensorrt {
30 namespace convert {
31 
32 class OpConverterRegistry {
33  public:
34   OpConverterRegistry();
35   ~OpConverterRegistry() = default;
36 
37   InitOnStartupMarker Register(const string& name, const int priority,
38                                OpConverter converter);
39 
Register(const std::initializer_list<std::string> & names,const int priority,OpConverter converter)40   InitOnStartupMarker Register(const std::initializer_list<std::string>& names,
41                                const int priority, OpConverter converter) {
42     for (const auto& name : names) {
43       Register(name, priority, converter);
44     }
45     return {};
46   }
47 
48   template <typename T,
49             typename std::enable_if<std::is_convertible<
50                 typename T::value_type, std::string>::value>::type* = nullptr>
Register(const T & names,const int priority,OpConverter converter)51   InitOnStartupMarker Register(const T& names, const int priority,
52                                OpConverter converter) {
53     for (const auto& name : names) {
54       Register(name, priority, converter);
55     }
56     return {};
57   }
58 
59   // Clear all registered converters for the given Tensorflow operation name.
60   void Clear(const std::string& name);
61 
62   StatusOr<OpConverter> LookUp(const string& name);
63 
64   std::vector<std::string> ListRegisteredOps() const;
65 
66  private:
67   class Impl;
68   std::unique_ptr<Impl> impl_;
69 };
70 
71 OpConverterRegistry* GetOpConverterRegistry();
72 
73 class RegisterOpConverter {
74  public:
RegisterOpConverter(const string & name,const int priority,OpConverter converter)75   RegisterOpConverter(const string& name, const int priority,
76                       OpConverter converter) {
77     GetOpConverterRegistry()->Register(name, priority, converter);
78   }
79 };
80 
81 constexpr int kDefaultConverterPriority = 1;
82 
83 }  // namespace convert
84 }  // namespace tensorrt
85 
86 #define REGISTER_TRT_OP_CONVERTER_IMPL(ctr, func, priority, ...)    \
87   static ::tensorflow::InitOnStartupMarker const                    \
88       register_trt_op_converter##ctr TF_ATTRIBUTE_UNUSED =          \
89           TF_INIT_ON_STARTUP_IF(true)                               \
90           << tensorrt::convert::GetOpConverterRegistry()->Register( \
91                  __VA_ARGS__, priority, func)
92 
93 #define REGISTER_TRT_OP_CONVERTER(func, priority, ...)               \
94   TF_NEW_ID_FOR_INIT(REGISTER_TRT_OP_CONVERTER_IMPL, func, priority, \
95                      __VA_ARGS__)
96 
97 #define REGISTER_DEFAULT_TRT_OP_CONVERTER(func, ...) \
98   REGISTER_TRT_OP_CONVERTER(                         \
99       func, tensorrt::convert::kDefaultConverterPriority, __VA_ARGS__)
100 
101 }  // namespace tensorflow
102 
103 #endif  // GOOGLE_CUDA && GOOGLE_TENSORRT
104 #endif  // TENSORFLOW_COMPILER_TF2TENSORRT_CONVERT_OP_CONVERTER_REGISTRY_H_
105