Apollo 11.0
自动驾驶开放平台
leakyReLU_plugin.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#pragma once
18
19#include <algorithm>
20#include <vector>
21#include <NvInferVersion.h>
22
24
25namespace apollo {
26namespace perception {
27namespace inference {
28
29#ifdef NV_TENSORRT_MAJOR
30#if NV_TENSORRT_MAJOR != 8
31class ReLUPlugin : public nvinfer1::IPlugin {
32 public:
33 ReLUPlugin(const ReLUParameter &param, const nvinfer1::Dims &in_dims) {
34 input_dims_.nbDims = in_dims.nbDims;
35 CHECK_GT(input_dims_.nbDims, 0);
36 for (int i = 0; i < in_dims.nbDims; i++) {
37 input_dims_.d[i] = in_dims.d[i];
38 input_dims_.type[i] = in_dims.type[i];
39 }
40 negative_slope_ = param.negative_slope();
41 }
42
43 ReLUPlugin() {}
44 ~ReLUPlugin() {}
45 virtual int initialize() { return 0; }
46 virtual void terminate() {}
47 int getNbOutputs() const override { return 1; }
48
49 nvinfer1::Dims getOutputDimensions(int index, const nvinfer1::Dims *inputs,
50 int nbInputDims) override {
51 nvinfer1::Dims out_dims = inputs[0];
52 return out_dims;
53 }
54
55 void configure(const nvinfer1::Dims *inputDims, int nbInputs,
56 const nvinfer1::Dims *outputDims, int nbOutputs,
57 int maxBatchSize) override {
58 input_dims_ = inputDims[0];
59 }
60
61 size_t getWorkspaceSize(int maxBatchSize) const override { return 0; }
62
63 virtual int enqueue(int batchSize, const void *const *inputs, void **outputs,
64 void *workspace, cudaStream_t stream);
65
66 size_t getSerializationSize() override { return 0; }
67
68 void serialize(void *buffer) override {
69 char *d = reinterpret_cast<char *>(buffer), *a = d;
70 size_t size = getSerializationSize();
71 CHECK_EQ(d, a + size);
72 }
73
74 private:
75 float negative_slope_;
76 nvinfer1::Dims input_dims_;
77};
78
79#else
80class ReLUPlugin : public nvinfer1::IPluginV2Ext {
81 public:
82 ReLUPlugin(const ReLUParameter &param, const nvinfer1::Dims &in_dims) {
83 input_dims_.nbDims = in_dims.nbDims;
84 CHECK_GT(input_dims_.nbDims, 0);
85 for (int i = 0; i < in_dims.nbDims; i++) {
86 input_dims_.d[i] = in_dims.d[i];
87 }
88 negative_slope_ = param.negative_slope();
89 }
90
91 ReLUPlugin() {}
92 ~ReLUPlugin() {}
93 virtual int initialize() noexcept { return 0; }
94 virtual void terminate() noexcept {}
95 int32_t getNbOutputs() const noexcept override { return 1; }
96
97 nvinfer1::Dims getOutputDimensions(int32_t index,
98 const nvinfer1::Dims *inputs,
99 int32_t nbInputDims) noexcept override {
100 nvinfer1::Dims out_dims = inputs[0];
101 return out_dims;
102 }
103
104 void configureWithFormat(const nvinfer1::Dims *inputDims, int32_t nbInputs,
105 const nvinfer1::Dims *outputDims, int32_t nbOutputs,
106 nvinfer1::DataType type, nvinfer1::PluginFormat format,
107 int32_t maxBatchSize) noexcept override {
108 input_dims_ = inputDims[0];
109 }
110
111 size_t getWorkspaceSize(int32_t maxBatchSize)
112 const noexcept override { return 0; }
113
114 int32_t enqueue(int32_t batchSize, const void *const *inputs,
115 void *const *outputs, void *workspace,
116 cudaStream_t stream)
117 noexcept override;
118
119 size_t getSerializationSize() const noexcept override { return 0; }
120
121 void serialize(void *buffer) const noexcept override {
122 char *d = reinterpret_cast<char *>(buffer), *a = d;
123 size_t size = getSerializationSize();
124 CHECK_EQ(d, a + size);
125 }
126
127 nvinfer1::AsciiChar const* getPluginType()
128 const noexcept override {
129 return plugin_type;
130 }
131
132 nvinfer1::AsciiChar const* getPluginVersion()
133 const noexcept override {
134 return plugin_version;
135 }
136
137 void setPluginNamespace(const nvinfer1::AsciiChar* libNamespace)
138 noexcept override {
139 plugin_namespace = const_cast<nvinfer1::AsciiChar*>(libNamespace);
140 }
141
142 nvinfer1::AsciiChar const* getPluginNamespace()
143 const noexcept override {
144 return const_cast<nvinfer1::AsciiChar*>(plugin_namespace);
145 }
146
147 bool supportsFormat(nvinfer1::DataType type,
148 nvinfer1::PluginFormat format) const noexcept override {
149 return true;
150 }
151
152 void destroy() noexcept override {
153 delete this;
154 }
155
156 nvinfer1::IPluginV2Ext* clone() const noexcept override {
157 ReLUPlugin* p = new ReLUPlugin();
158 p->negative_slope_ = negative_slope_;
159 (p->input_dims_).nbDims = input_dims_.nbDims;
160 for (int i = 0; i < input_dims_.nbDims; i++) {
161 (p->input_dims_).d[i] = input_dims_.d[i];
162 }
163
164 p->plugin_namespace = plugin_namespace;
165 return p;
166 }
167
168 bool isOutputBroadcastAcrossBatch(int32_t outputIndex,
169 bool const *inputIsBroadcasted, int32_t nbInputs)
170 const noexcept override {
171 return false;
172 }
173
174 bool canBroadcastInputAcrossBatch(int32_t inputIndex)
175 const noexcept override {
176 return false;
177 }
178
179 nvinfer1::DataType getOutputDataType(int32_t index,
180 nvinfer1::DataType const *inputTypes, int32_t nbInputs)
181 const noexcept {
182 return nvinfer1::DataType::kFLOAT;
183 }
184
185 void configurePlugin(
186 nvinfer1::Dims const *inputDims, int32_t nbInputs,
187 nvinfer1::Dims const *outputDims, int32_t nbOutputs,
188 nvinfer1::DataType const *inputTypes, nvinfer1::DataType const *outputTypes,
189 bool const *inputIsBroadcast, bool const *outputIsBroadcast,
190 nvinfer1::PluginFormat floatFormat, int32_t maxBatchSize) noexcept {}
191
192 private:
193 float negative_slope_;
194 nvinfer1::Dims input_dims_;
195
196 nvinfer1::AsciiChar* plugin_namespace;
197 const nvinfer1::AsciiChar* plugin_type = "";
198 const nvinfer1::AsciiChar* plugin_version = "";
199};
200
201#endif
202#endif
203
204} // namespace inference
205} // namespace perception
206} // namespace apollo
class register implement
Definition arena_queue.h:37