-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathscatterElementsPluginLegacy.cpp
316 lines (271 loc) · 8.96 KB
/
scatterElementsPluginLegacy.cpp
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* SPDX-FileCopyrightText: Copyright (c) 1993-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include "common/serialize.hpp"
#include "scatterElementsPluginKernel.h"
#include "scatterElementsPluginLegacy.h"
namespace nvinfer1
{
namespace plugin
{
std::unordered_map<std::string, ReductionType> const kREDUCE_STR_TO_ENUM{
{"add", ReductionType::kSUM},
{"mean", ReductionType::kMEAN},
{"mul", ReductionType::kMUL},
{"min", ReductionType::kMIN},
{"max", ReductionType::kMAX},
};
// Static class fields initialization
PluginFieldCollection ScatterElementsPluginV2Creator::gFC{};
std::vector<PluginField> ScatterElementsPluginV2Creator::gPluginAttributes;
namespace
{
constexpr char const* kSCATTER_ELEMENTS_NAME{"ScatterElements"};
constexpr char const* kSCATTER_ELEMENTS_VERSION{"1"};
} // namespace
ScatterElementsPluginV2::ScatterElementsPluginV2(ReductionType reduction, int32_t dim)
: mReduction(reduction)
, mAxis(dim)
{
}
ScatterElementsPluginV2::ScatterElementsPluginV2(std::string const& reduction, int32_t dim)
: mReduction(kREDUCE_STR_TO_ENUM.at(reduction))
, mAxis(dim)
{
}
ScatterElementsPluginV2::ScatterElementsPluginV2(void const* serialData, size_t serialLength)
{
deserialize_value(&serialData, &serialLength, &mReduction);
deserialize_value(&serialData, &serialLength, &mAxis);
}
int32_t ScatterElementsPluginV2::getNbOutputs() const noexcept
{
return 1;
}
int32_t ScatterElementsPluginV2::initialize() noexcept
{
return 0;
}
char const* ScatterElementsPluginV2::getPluginType() const noexcept
{
return kSCATTER_ELEMENTS_NAME;
}
char const* ScatterElementsPluginV2::getPluginVersion() const noexcept
{
return kSCATTER_ELEMENTS_VERSION;
}
DimsExprs ScatterElementsPluginV2::getOutputDimensions(
int32_t index, DimsExprs const* inputs, int32_t nbInputs, IExprBuilder& exprBuilder) noexcept
{
try
{
PLUGIN_VALIDATE(nbInputs == 3);
PLUGIN_VALIDATE(inputs);
PLUGIN_VALIDATE(index <= kOUTPUT_TENSOR_IDX);
// both outputs are of the same size
DimsExprs out(inputs[kDATA_TENSOR_IDX]);
return out;
}
catch (std::exception const& e)
{
caughtError(e);
}
return DimsExprs();
}
int32_t ScatterElementsPluginV2::enqueue(PluginTensorDesc const* inputDesc, PluginTensorDesc const* outputDesc,
void const* const* inputs, void* const* outputs, void* workspace, cudaStream_t stream) noexcept
{
try
{
PLUGIN_VALIDATE(inputDesc[kINDICES_TENSOR_IDX].type == DataType::kINT64);
runScatterElementsKernel(outputs[kOUTPUT_TENSOR_IDX], inputs[kDATA_TENSOR_IDX], inputs[kUPDATES_TENSOR_IDX],
inputs[kINDICES_TENSOR_IDX], outputDesc[kOUTPUT_TENSOR_IDX], inputDesc[kDATA_TENSOR_IDX],
inputDesc[kUPDATES_TENSOR_IDX], inputDesc[kINDICES_TENSOR_IDX], mAxis, mReduction, stream);
return 0;
}
catch (std::exception const& e)
{
caughtError(e);
return -1;
}
}
size_t ScatterElementsPluginV2::getSerializationSize() const noexcept
{
auto ret = serialized_size(mReduction) + serialized_size(mAxis);
return ret;
}
void ScatterElementsPluginV2::serialize(void* buffer) const noexcept
{
serialize_value(&buffer, mReduction);
serialize_value(&buffer, mAxis);
}
bool ScatterElementsPluginV2::supportsFormatCombination(
int32_t pos, PluginTensorDesc const* inOut, int32_t nbInputs, int32_t nbOutputs) noexcept
{
try
{
PLUGIN_VALIDATE(inOut && pos < (nbInputs + nbOutputs));
if (inOut[pos].format != PluginFormat::kLINEAR)
{
return false;
}
auto mytype = inOut[pos].type;
auto firsttype = inOut[kDATA_TENSOR_IDX].type;
// Only INT64 is supported for indices
return pos == kINDICES_TENSOR_IDX ? (mytype == DataType::kINT64)
: (mytype == firsttype)
&& (mytype == DataType::kFLOAT || mytype == DataType::kHALF
|| (hasBfloat16AtomicAdd() && mytype == DataType::kBF16) || mytype == DataType::kINT32
|| mytype == DataType::kINT64);
}
catch (std::exception const& e)
{
caughtError(e);
return false;
}
}
void ScatterElementsPluginV2::terminate() noexcept {}
void ScatterElementsPluginV2::destroy() noexcept
{
// This gets called when the network containing plugin is destroyed
delete this;
}
IPluginV2DynamicExt* ScatterElementsPluginV2::clone() const noexcept
{
auto* plugin = new ScatterElementsPluginV2(mReduction, mAxis);
plugin->setPluginNamespace(mNamespace.c_str());
return plugin;
}
void ScatterElementsPluginV2::configurePlugin(
DynamicPluginTensorDesc const* in, int32_t nbInputs, DynamicPluginTensorDesc const* out, int32_t nbOutputs) noexcept
{
try
{
PLUGIN_VALIDATE(nbInputs == 3);
}
catch (std::exception const& e)
{
caughtError(e);
}
}
DataType ScatterElementsPluginV2::getOutputDataType(
int32_t index, DataType const* inputTypes, int32_t nbInputs) const noexcept
{
try
{
PLUGIN_VALIDATE(inputTypes && nbInputs == 3 && index == kOUTPUT_TENSOR_IDX);
}
catch (std::exception const& e)
{
caughtError(e);
}
return inputTypes[kDATA_TENSOR_IDX];
}
size_t ScatterElementsPluginV2::getWorkspaceSize(
PluginTensorDesc const* inputs, int32_t nbInputs, PluginTensorDesc const* outputs, int32_t nbOutputs) const noexcept
{
return 0;
}
void ScatterElementsPluginV2::setPluginNamespace(char const* libNamespace) noexcept
{
mNamespace = libNamespace;
}
char const* ScatterElementsPluginV2::getPluginNamespace() const noexcept
{
return mNamespace.c_str();
}
//
// ScatterElementsPluginV2Creator
//
ScatterElementsPluginV2Creator::ScatterElementsPluginV2Creator()
{
gPluginAttributes.clear();
gPluginAttributes.emplace_back(PluginField("reduction"));
gPluginAttributes.emplace_back(PluginField("axis"));
gFC.nbFields = gPluginAttributes.size();
gFC.fields = gPluginAttributes.data();
}
char const* ScatterElementsPluginV2Creator::getPluginName() const noexcept
{
return kSCATTER_ELEMENTS_NAME;
}
char const* ScatterElementsPluginV2Creator::getPluginVersion() const noexcept
{
return kSCATTER_ELEMENTS_VERSION;
}
PluginFieldCollection const* ScatterElementsPluginV2Creator::getFieldNames() noexcept
{
return &gFC;
}
char const* ScatterElementsPluginV2Creator::getPluginNamespace() const noexcept
{
return mNamespace.c_str();
}
void ScatterElementsPluginV2Creator::setPluginNamespace(char const* libNamespace) noexcept
{
mNamespace = libNamespace;
}
IPluginV2DynamicExt* ScatterElementsPluginV2Creator::createPlugin(
char const* name, PluginFieldCollection const* fc) noexcept
{
std::string reductionArg;
int32_t axisArg = 0;
ScatterElementsPluginV2* plugin = nullptr;
try
{
PLUGIN_VALIDATE(fc != nullptr);
auto fields = fc->fields;
std::set<std::string> requiredFields{"reduction"};
plugin::validateRequiredAttributesExist(requiredFields, fc);
for (int32_t i = 0; i < fc->nbFields; ++i)
{
PLUGIN_VALIDATE(fields[i].name != nullptr);
PLUGIN_VALIDATE(fields[i].data != nullptr);
if (strcmp(fields[i].name, "axis") == 0)
{
auto data = static_cast<int32_t const*>(fields[i].data);
axisArg = *data;
}
else if (strcmp(fields[i].name, "reduction") == 0)
{
auto data = static_cast<char const*>(fields[i].data);
reductionArg = std::string(data);
}
}
PLUGIN_VALIDATE(kREDUCE_STR_TO_ENUM.find(reductionArg) != kREDUCE_STR_TO_ENUM.end(),
(reductionArg + ": invalid value for 'reduction' plugin argument").c_str());
plugin = new ScatterElementsPluginV2(reductionArg, axisArg);
plugin->setPluginNamespace(mNamespace.c_str());
}
catch (std::exception& e)
{
caughtError(e);
}
return plugin;
}
IPluginV2DynamicExt* ScatterElementsPluginV2Creator::deserializePlugin(
char const* name, void const* serialData, size_t serialLength) noexcept
{
ScatterElementsPluginV2* plugin = new ScatterElementsPluginV2(serialData, serialLength);
plugin->setPluginNamespace(mNamespace.c_str());
return plugin;
}
} // namespace plugin
} // namespace nvinfer1