forked from DanaHan/Yolov5-in-Deepstream-5.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardswish.cu
182 lines (148 loc) · 5.23 KB
/
hardswish.cu
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
#include <assert.h>
#include "hardswish.h"
#include "utils.h"
namespace nvinfer1
{
HardSwishPlugin::HardSwishPlugin()
{
}
HardSwishPlugin::~HardSwishPlugin()
{
}
// create the plugin at runtime from a byte stream
HardSwishPlugin::HardSwishPlugin(const void* data, size_t length)
{
const char *d = reinterpret_cast<const char *>(data), *a = d;
Tn::read(d, mInputSize);
assert(d == a + length);
}
void HardSwishPlugin::serialize(void* buffer) const
{
char* d = static_cast<char*>(buffer), *a = d;
Tn::write(d, mInputSize);
assert(d == a + getSerializationSize());
}
size_t HardSwishPlugin::getSerializationSize() const
{
return sizeof(mInputSize);
}
int HardSwishPlugin::initialize()
{
return 0;
}
Dims HardSwishPlugin::getOutputDimensions(int index, const Dims* inputs, int nbInputDims)
{
assert(nbInputDims == 1);
assert(index == 0);
return Dims3(inputs[0].d[0], inputs[0].d[1], inputs[0].d[2]);
}
// Set plugin namespace
void HardSwishPlugin::setPluginNamespace(const char* pluginNamespace)
{
mPluginNamespace = pluginNamespace;
}
const char* HardSwishPlugin::getPluginNamespace() const
{
return mPluginNamespace;
}
// Return the DataType of the plugin output at the requested index
DataType HardSwishPlugin::getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const
{
return DataType::kFLOAT;
}
// Return true if output tensor is broadcast across a batch.
bool HardSwishPlugin::isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const
{
return false;
}
// Return true if plugin can use input that is broadcast across batch without replication.
bool HardSwishPlugin::canBroadcastInputAcrossBatch(int inputIndex) const
{
return false;
}
void HardSwishPlugin::configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput)
{
mInputSize = in[0].dims.d[0] * in[0].dims.d[1] * in[0].dims.d[2];
}
// Attach the plugin object to an execution context and grant the plugin the access to some context resource.
void HardSwishPlugin::attachToContext(cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator)
{
}
// Detach the plugin object from its execution context.
void HardSwishPlugin::detachFromContext() {}
const char* HardSwishPlugin::getPluginType() const
{
return "HardSwishLayer_TRT";
}
const char* HardSwishPlugin::getPluginVersion() const
{
return "1";
}
void HardSwishPlugin::destroy()
{
delete this;
}
// Clone the plugin
IPluginV2IOExt* HardSwishPlugin::clone() const
{
HardSwishPlugin *p = new HardSwishPlugin();
p->setPluginNamespace(mPluginNamespace);
p->setInputSize(mInputSize);
return p;
}
__global__ void HardSwishKer(const float *in, float *out, int size) {
int idx = threadIdx.x + blockIdx.x * blockDim.x;
if (idx >= size)
return;
if (in[idx] >= 3.0f)
out[idx] = in[idx];
else if (in[idx] < -3.0f)
out[idx] = 0.0f;
else
out[idx] = in[idx] * (in[idx] + 3.0f) / 6.0f;
}
void HardSwishPlugin::forwardGpu(const float *const * inputs, float* output, cudaStream_t stream, int batchSize) {
int numElem = batchSize * mInputSize;
HardSwishKer<<<(numElem + mThreadCount - 1) / mThreadCount, mThreadCount, 0, stream>>>
(inputs[0], output, numElem);
}
int HardSwishPlugin::enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream)
{
forwardGpu((const float *const *)inputs, (float*)outputs[0], stream, batchSize);
return 0;
}
PluginFieldCollection HardSwishPluginCreator::mFC{};
std::vector<PluginField> HardSwishPluginCreator::mPluginAttributes;
HardSwishPluginCreator::HardSwishPluginCreator()
{
mPluginAttributes.clear();
mFC.nbFields = mPluginAttributes.size();
mFC.fields = mPluginAttributes.data();
}
const char* HardSwishPluginCreator::getPluginName() const
{
return "HardSwishLayer_TRT";
}
const char* HardSwishPluginCreator::getPluginVersion() const
{
return "1";
}
const PluginFieldCollection* HardSwishPluginCreator::getFieldNames()
{
return &mFC;
}
IPluginV2IOExt* HardSwishPluginCreator::createPlugin(const char* name, const PluginFieldCollection* fc)
{
HardSwishPlugin* obj = new HardSwishPlugin();
obj->setPluginNamespace(mNamespace.c_str());
return obj;
}
IPluginV2IOExt* HardSwishPluginCreator::deserializePlugin(const char* name, const void* serialData, size_t serialLength)
{
// This object will be deleted when the network is destroyed, which will
// call MishPlugin::destroy()
HardSwishPlugin* obj = new HardSwishPlugin(serialData, serialLength);
obj->setPluginNamespace(mNamespace.c_str());
return obj;
}
}