forked from wdhao/tensorrtCV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpsamplePlugin.cpp
More file actions
235 lines (191 loc) · 6.32 KB
/
Copy pathUpsamplePlugin.cpp
File metadata and controls
235 lines (191 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include "UpsmapleKernel.h"
#include "UpsamplePlugin.h"
#include<cassert>
#include <cstring>
using namespace nvinfer1;
// Upsample plugin specific constants
namespace {
static const char* UPSAMPLE_PLUGIN_VERSION{"1"};
static const char* UPSAMPLE_PLUGIN_NAME{"UpsamplePlugin"};
}
// Static class fields initialization
PluginFieldCollection UpsamplePluginCreator::mFC{};
std::vector<PluginField> UpsamplePluginCreator::mPluginAttributes;
REGISTER_TENSORRT_PLUGIN(UpsamplePluginCreator);
template<typename T>
void writeToBuffer(char*& buffer, const T& val)
{
*reinterpret_cast<T*>(buffer) = val;
buffer += sizeof(T);
}
// Helper function for deserializing plugin
template<typename T>
T readFromBuffer(const char*& buffer)
{
T val = *reinterpret_cast<const T*>(buffer);
buffer += sizeof(T);
return val;
}
UpsamplePlugin::UpsamplePlugin(const std::string name, float scale_h, float scale_w)
: mLayerName(name)
, mScaleFactor_h(scale_h)
, mScaleFactor_w(scale_w)
{
mInputShape.c() = -1;
mInputShape.h() = -1;
mInputShape.w() = -1;
mInputVolume = 0;
}
UpsamplePlugin::UpsamplePlugin(const std::string name, const void* data, size_t length)
: mLayerName(name)
{
const char *d = static_cast<const char *>(data);
const char *a = d;
mScaleFactor_h = readFromBuffer<float>(d);
mScaleFactor_w = readFromBuffer<float>(d);
mInputVolume = readFromBuffer<size_t>(d);
mInputShape.c() = readFromBuffer<int>(d);
mInputShape.h() = readFromBuffer<int>(d);
mInputShape.w() = readFromBuffer<int>(d);
assert(d == (a + length));
}
const char* UpsamplePlugin::getPluginType() const
{
return UPSAMPLE_PLUGIN_NAME;
}
const char* UpsamplePlugin::getPluginVersion() const
{
return UPSAMPLE_PLUGIN_VERSION;
}
int UpsamplePlugin::getNbOutputs() const
{
return 1;
}
Dims UpsamplePlugin::getOutputDimensions(int index, const Dims* inputs, int nbInputDims)
{
assert(index == 0);
assert(nbInputDims == 1);
assert(inputs[0].nbDims == 3);
return nvinfer1::DimsCHW{inputs[0].d[0],int(inputs[0].d[1]*mScaleFactor_h), int(inputs[0].d[2]*mScaleFactor_w)};
}
int UpsamplePlugin::initialize()
{
//printf("UpsamplePlugin::initialize\n");
return 0;
}
int UpsamplePlugin::enqueue(int batchSize, const void* const* inputs, void** outputs, void*, cudaStream_t stream)
{
//printf("UpsamplePlugin::enqueue\n");
int status = -1;
// Our plugin outputs only one tensor
void* output = outputs[0];
// Launch CUDA kernel wrapper and save its return value
status = UpsampleInference(stream, mInputVolume,
batchSize, mInputShape.c(), mInputShape.h(), mInputShape.w(),
mScaleFactor_h,mScaleFactor_w,
inputs[0], output);
return status;
}
size_t UpsamplePlugin::getSerializationSize() const
{
//printf("UpsamplePlugin::getSerializationSize\n");
return sizeof(mScaleFactor_h) + sizeof(mScaleFactor_w) +
sizeof(mInputVolume) + sizeof(mInputShape.c()) +
sizeof(mInputShape.h()) + sizeof(mInputShape.w());
}
void UpsamplePlugin::serialize(void* buffer) const
{
//printf("UpsamplePlugin::serialize\n");
char *d = static_cast<char *>(buffer);
const char *a = d;
writeToBuffer(d, mScaleFactor_h);
writeToBuffer(d, mScaleFactor_w);
writeToBuffer(d, mInputVolume);
writeToBuffer(d, mInputShape.c());
writeToBuffer(d, mInputShape.h());
writeToBuffer(d, mInputShape.w());
assert(d == a + getSerializationSize());
}
void UpsamplePlugin::configureWithFormat(const Dims* inputs, int nbInputs, const Dims* outputs, int nbOutputs, DataType type, PluginFormat format, int)
{
assert(nbOutputs == 1);
assert(type == DataType::kFLOAT);
assert(format == PluginFormat::kNCHW);
assert(inputs[0].nbDims == 3);
size_t volume = int(inputs[0].d[1]*mScaleFactor_h) * int(inputs[0].d[2]*mScaleFactor_w);
mInputVolume = volume;
mInputShape.c() = inputs[0].d[0];
mInputShape.h() = inputs[0].d[1];
mInputShape.w() = inputs[0].d[2];
}
bool UpsamplePlugin::supportsFormat(DataType type, PluginFormat format) const
{
if (type == DataType::kFLOAT && format == PluginFormat::kNCHW)
return true;
else
return false;
}
void UpsamplePlugin::terminate() {}
void UpsamplePlugin::destroy() {
// This gets called when the network containing plugin is destroyed
delete this;
}
IPluginV2* UpsamplePlugin::clone() const
{
return new UpsamplePlugin(mLayerName, mScaleFactor_h, mScaleFactor_w);
}
void UpsamplePlugin::setPluginNamespace(const char* libNamespace)
{
mNamespace = libNamespace;
}
const char* UpsamplePlugin::getPluginNamespace() const
{
return mNamespace.c_str();
}
UpsamplePluginCreator::UpsamplePluginCreator()
{
mPluginAttributes.emplace_back(PluginField("scaleFactor", nullptr, PluginFieldType::kFLOAT32, 1));
mFC.nbFields = mPluginAttributes.size();
mFC.fields = mPluginAttributes.data();
}
const char* UpsamplePluginCreator::getPluginName() const
{
return UPSAMPLE_PLUGIN_NAME;
}
const char* UpsamplePluginCreator::getPluginVersion() const
{
return UPSAMPLE_PLUGIN_VERSION;
}
const PluginFieldCollection* UpsamplePluginCreator::getFieldNames()
{
return &mFC;
}
IPluginV2* UpsamplePluginCreator::createPlugin(const char* name, const PluginFieldCollection* fc)
{
float scaleFactor_h = 0.f;
float scaleFactor_w = 0.f;
const PluginField* fields = fc->fields;
assert(fc->nbFields == 1);
for (int i = 0; i < fc->nbFields; i++){
if (strcmp(fields[i].name, "scaleFactor") == 0) {
assert(fields[i].type == PluginFieldType::kFLOAT32);
scaleFactor_h = *(static_cast<const float*>(fields[i].data));
scaleFactor_w = *(static_cast<const float*>(fields[i].data)+1);
//std::cout<<scaleFactor_h<< " , "<<scaleFactor_w<<std::endl;
}
}
return new UpsamplePlugin(name, scaleFactor_h, scaleFactor_w);
}
IPluginV2* UpsamplePluginCreator::deserializePlugin(const char* name, const void* serialData, size_t serialLength)
{
return new UpsamplePlugin(name, serialData, serialLength);
}
void UpsamplePluginCreator::setPluginNamespace(const char* libNamespace)
{
mNamespace = libNamespace;
}
const char* UpsamplePluginCreator::getPluginNamespace() const
{
return mNamespace.c_str();
}