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/compiler/tf2tensorrt/plugin/trt_plugin_factory.h"
17
18 #if GOOGLE_CUDA
19 #if GOOGLE_TENSORRT
20
21 namespace tensorflow {
22 namespace tensorrt {
23
createPlugin(const char * layer_name,const void * serial_data,size_t serial_length)24 PluginTensorRT* PluginFactoryTensorRT::createPlugin(const char* layer_name,
25 const void* serial_data,
26 size_t serial_length) {
27 size_t parsed_byte = 0;
28 // extract op_name from serial_data
29 string encoded_op_name =
30 ExtractOpName(serial_data, serial_length, &parsed_byte);
31
32 if (!IsPlugin(encoded_op_name)) {
33 return nullptr;
34 }
35
36 mutex_lock lock(instance_m_);
37 auto plugin_ptr =
38 plugin_registry_[encoded_op_name].first(serial_data, serial_length);
39 owned_plugins_.emplace_back(plugin_ptr);
40
41 return plugin_ptr;
42 }
43
CreatePlugin(const string & op_name)44 PluginTensorRT* PluginFactoryTensorRT::CreatePlugin(const string& op_name) {
45 if (!IsPlugin(op_name)) return nullptr;
46
47 mutex_lock lock(instance_m_);
48 auto plugin_ptr = plugin_registry_[op_name].second();
49 owned_plugins_.emplace_back(plugin_ptr);
50
51 return plugin_ptr;
52 }
53
RegisterPlugin(const string & op_name,PluginDeserializeFunc deserialize_func,PluginConstructFunc construct_func)54 bool PluginFactoryTensorRT::RegisterPlugin(
55 const string& op_name, PluginDeserializeFunc deserialize_func,
56 PluginConstructFunc construct_func) {
57 if (IsPlugin(op_name)) return false;
58
59 mutex_lock lock(instance_m_);
60 auto ret = plugin_registry_.emplace(
61 op_name, std::make_pair(deserialize_func, construct_func));
62
63 return ret.second;
64 }
65
DestroyPlugins()66 void PluginFactoryTensorRT::DestroyPlugins() {
67 mutex_lock lock(instance_m_);
68 owned_plugins_.clear();
69 }
70
71 } // namespace tensorrt
72 } // namespace tensorflow
73
74 #endif // GOOGLE_CUDA
75 #endif // GOOGLE_TENSORRT
76