chore: import upstream snapshot with attribution
Docker Image CI / build-ubuntu2004 (push) Waiting to run

This commit is contained in:
wehub-resource-sync
2026-07-13 13:36:55 +08:00
commit c8a779b1bb
1887 changed files with 3245738 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
#
# SPDX-FileCopyrightText: Copyright (c) 1993-2025 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.
#
add_plugin_source(
gridAnchorPlugin.cpp
gridAnchorPlugin.h
)
@@ -0,0 +1,100 @@
#
# SPDX-FileCopyrightText: Copyright (c) 2022-2025 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.
#
---
name: GridAnchor_TRT
interface: "IPluginV2Ext"
versions:
"1":
inputs: []
outputs:
- outputs
attributes:
- numLayers
- minSize
- maxSize
- variance
- aspectRatios
- featureMapShapes
attribute_types:
numLayers: int32
minSize: float32
maxSize: float32
variance: float32
aspectRatios: float32
featureMapShapes: int32
attribute_length:
numLayers: 1
minSize: 1
maxSize: 1
variance: 4
aspectRatios: -1
featureMapShapes: -1
attribute_options:
numLayers:
min: "0"
max: "=pinf"
minSize:
min: "0"
max: "1"
maxSize:
min: "0"
max: "1"
variance:
min: "=ninf"
max: "=pinf"
aspectRatios:
min: "0"
max: "=pinf"
featureMapShapes:
min: "1"
max: "=pinf"
attributes_required:
- numLayers
- minSize
- maxSize
- variance
- aspectRatios
- featureMapShapes
golden_io_path: "plugin/GridAnchor_TRT_PluginGoldenIO.json"
abs_tol: 1e-2
rel_tol: 1e-2
configs:
config1:
input_types:
input_layer: float32
attribute_options:
numLayers:
value: 6
shape: "1"
minSize:
value: 0.2
shape: "1"
maxSize:
value: 0.95
shape: "1"
variance:
value: [0.1, 0.1, 0.2, 0.2]
shape: "4"
aspectRatios:
value: [1.0, 2.0, 0.5, 3.0, 0.33]
shape: "5"
featureMapShapes:
value: [40, 20, 10, 5, 3, 1]
shape: "6"
output_types:
output: float32
...
+105
View File
@@ -0,0 +1,105 @@
# gridAnchorPlugin [DEPRECATED]
**This plugin is deprecated since TensorRT 10.12 and will be removed in a future release. No alternatives are planned to be provided. This applies to both GridAnchor_TRT and GridAnchorRect_TRT.**
**Table Of Contents**
- [Description](#description)
* [Structure](#structure)
- [Parameters](#parameters)
* [Example: Creating `GridAnchorGenerator` For An SSD Network](#example-creating-gridanchorgenerator-for-an-ssd-network)
- [Additional resources](#additional-resources)
- [License](#license)
- [Changelog](#changelog)
- [Known issues](#known-issues)
## Description
Some object detection neural networks such as Faster R-CNN and SSD use region proposal networks that require anchor boxes to generate predicted bounding boxes.
The `gridAnchorPlugin` generates anchor boxes (prior boxes) from the feature map in object detection models such as SSD. It generates anchor box coordinates `[x_min, y_min, x_max, y_max]` with variances (scaling factors) `[var_0, var_1, var_2, var_3]` for the downstream bounding box decoding steps. It uses a series of CUDA kernels in the `gridAnchorLayer.cu` file to accelerate the process in TensorRT.
If the feature maps are square, then the `GridAnchor_TRT` plugin should be used. If the feature maps
are rectangular but non-square, then the `GridAnchorRect_TRT` plugin should be used.
### Structure
The `GridAnchorGenerator` plugin takes no inputs. However, it uses the attributes from `GridAnchorParameters` array typed `mParam`, the number of the feature maps we are generating anchor boxes for, and generates `mNumLayers` outputs (one per each feature map).
Each output has shape of `[2, H x W x mNumPriors x 4, 1]`. The first dimension has two channels.
- The first channel is for the coordinates of the proposed anchor box. The position consists of four coordinates `[x_min, y_min, x_max, y_max]`.
- The second channel is for the variance pre-calculated for bounding box decoding. The variance was copied from the `GridAnchorParameters.variance` that you provided to create the plugin.
## Parameters
The `GridAnchor_TRT` plugin consists of the plugin creator class `GridAnchorPluginCreator` and the plugin class `GridAnchorGenerator`.
The `GridAnchorRect_TRT` plugin consists of the plugin creator class `GridAnchorRectPluginCreator` and the plugin class `GridAnchorGenerator`.
`GridAnchorPluginCreator` and `GridAnchorPluginCreator` both take the following parameters as user input:
| Type | Parameter | Description
|----------|--------------------------|--------------------------------------------------------
|`float` |`minSize` |Scale of anchors corresponding to finest resolution with respect to the height of input image. It corresponds to the `s_min` of the SSD paper. Default value is `0.2F`.
|`float` |`maxSize` |Scale of anchors corresponding to coarsest resolution with respect to the height of input image. It corresponds to the `s_max` of the SSD paper. Default value is `0.95F`.
|`float*` |`aspectRatios` |List of aspect ratios to place on each grid point.
|`int*` |`featureMapShapes` |Shapes of the feature maps. If creating a `GridAnchorRect_TRT` plugin, this must be a list of size `numLayers * 2` where the height and width of each feature map is listed in order. If creating a `GridAnchor_TRT` plugin, this must be list of size `numLayers` where the height (or width) of each feature map is listed in order.
|`float*` |`variance` |Variance for adjusting the prior boxes.
|`int` |`numLayers` |Number of feature maps. Default value is `6`.
`GridAnchorGenerator`'s constructor takes `numLayers` and an array of `GridAnchorParameters` typed parameters. The corresponding `GridAnchorParameters` are created internally by `GridAnchorPluginCreator` (not the plugin user's responsibility). `GridAnchorParameters` consists of the following parameters:
| Type | Parameter | Description
|----------|--------------------------|--------------------------------------------------------
|`float` |`minSize` |Scale of anchors corresponding to finest resolution with respect to the height of input image. It corresponds to the `s_min` of the SSD paper.
|`float` |`maxSize` |Scale of anchors corresponding to coarsest resolution with respect to the height of input image. It corresponds to the `s_max` of the SSD paper.
|`float*` |`aspectRatios` |List of aspect ratios to place on each grid point.
|`int` |`numAspectRatios` |Number of elements in aspectRatios.
|`int` |`H` |Height of feature map to generate anchors for.
|`int` |`W` |Width of feature map to generate anchors for.
|`float[4]`|`variance` |Variance for adjusting the prior boxes.
### Example: Creating `GridAnchorGenerator` For An SSD Network
If we were to create a `GridAnchorGenerator` for a SSD network consisting of 6 layers, then the following parameters would be passed to the plugin creator class `GridAnchorPluginCreator`:
```
numLayers=6,
minSize=0.2,
maxSize=0.95,
aspectRatios=[1.0, 2.0, 0.5, 3.0, 0.33],
variance=[0.1, 0.1, 0.2, 0.2],
featureMapShapes=[19, 10, 5, 3, 2, 1]
```
The `GridAnchorGenerator` uses distinct `GridAnchorParameters` for each feature map to generate anchor boxes, therefore, it takes an array of `GridAnchorParameters` with a length of the number of feature maps (`mNumLayers`) to create the plugin. In the above example, we have 6 layers, the plugin needs an array of 6 `GridAnchorParameters` to create the plugin. In this particular example, all the `GridAnchorParameters` except for the first one in the array are the same according to the SSD model settings. After the plugin is created, each feature map, except for the first feature map, will have 5 + 1 anchor boxes, where 5 is the number of elements in `aspectRatios` and 1 is an additional default anchor box with an aspect ratio of 1.0. The first layer, as described in the [SSD: Single Shot MultiBox Detector paper](https://arxiv.org/pdf/1512.02325.pdf), has fewer number of anchor boxes. In our case, it was set to 3, in our code, `int numFirstLayerARs = 3`, and there will be no additional default anchor box of aspect ratio 1.0. The feature map shapes also supports rectangular inputs. The height and width of the feature maps are put in order in the list above.
**Note:** The above settings are slightly different to the original published SSD paper.
## Additional resources
The following resources provide a deeper understanding of the `gridAnchorPlugin` plugin:
**Networks:**
- [SSD: Single Shot MultiBox Detector](https://arxiv.org/abs/1512.02325)
- [Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks](https://arxiv.org/abs/1506.01497)
**Documentation:**
- [GridAnchorParameters detailed descriptions](https://docs.nvidia.com/deeplearning/sdk/tensorrt-api/c_api/structnvinfer1_1_1plugin_1_1_grid_anchor_parameters.html)
## License
For terms and conditions for use, reproduction, and distribution, see the [TensorRT Software License Agreement](https://docs.nvidia.com/deeplearning/sdk/tensorrt-sla/index.html)
documentation.
## Changelog
May 2025
Add deprecation note.
May 2019
This is the first release of this `README.md` file.
## Known issues
There are no known issues in this plugin.
@@ -0,0 +1,528 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 1993-2026 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 "gridAnchorPlugin.h"
#include <cmath>
#include <iostream>
#include <memory>
#include <sstream>
#include <string_view>
#include <vector>
using namespace nvinfer1;
using namespace nvinfer1::pluginInternal;
namespace nvinfer1::plugin
{
namespace
{
std::string const kGRID_ANCHOR_PLUGIN_NAMES[] = {"GridAnchor_TRT", "GridAnchorRect_TRT"};
char const* const kGRID_ANCHOR_PLUGIN_VERSION = "1";
} // namespace
GridAnchorGenerator::GridAnchorGenerator(GridAnchorParameters const* paramIn, int32_t numLayers, char const* name)
: mPluginName(name)
, mNumLayers(numLayers)
{
PLUGIN_CUASSERT(cudaMallocHost((void**) &mNumPriors, mNumLayers * sizeof(int32_t)));
PLUGIN_CUASSERT(cudaMallocHost((void**) &mDeviceWidths, mNumLayers * sizeof(Weights)));
PLUGIN_CUASSERT(cudaMallocHost((void**) &mDeviceHeights, mNumLayers * sizeof(Weights)));
mParam.resize(mNumLayers);
for (int32_t id = 0; id < mNumLayers; id++)
{
mParam[id] = paramIn[id];
PLUGIN_VALIDATE(mParam[id].numAspectRatios >= 0 && mParam[id].aspectRatios != nullptr);
mParam[id].aspectRatios = (float*) malloc(sizeof(float) * mParam[id].numAspectRatios);
for (int32_t i = 0; i < paramIn[id].numAspectRatios; ++i)
{
mParam[id].aspectRatios[i] = paramIn[id].aspectRatios[i];
}
for (int32_t i = 0; i < 4; ++i)
{
mParam[id].variance[i] = paramIn[id].variance[i];
}
std::vector<float> tmpScales(mNumLayers + 1);
// Calculate the scales of SSD model for each layer
for (int32_t i = 0; i < mNumLayers; i++)
{
tmpScales[i] = (mParam[id].minSize + (mParam[id].maxSize - mParam[id].minSize) * id / (mNumLayers - 1));
}
// Add another 1.0f to tmpScales to prevent going out side of the vector in calculating the scale_next.
tmpScales.push_back(1.0F); // has 7 entries
// scale0 are for the first layer specifically
std::vector<float> scale0 = {0.1F, tmpScales[0], tmpScales[0]};
std::vector<float> aspect_ratios;
std::vector<float> scales;
// The first layer is different
if (id == 0)
{
for (int32_t i = 0; i < mParam[id].numAspectRatios; i++)
{
aspect_ratios.push_back(mParam[id].aspectRatios[i]);
scales.push_back(scale0[i]);
}
mNumPriors[id] = mParam[id].numAspectRatios;
}
else
{
for (int32_t i = 0; i < mParam[id].numAspectRatios; i++)
{
aspect_ratios.push_back(mParam[id].aspectRatios[i]);
}
// Additional aspect ratio of 1.0 as described in the paper
aspect_ratios.push_back(1.0);
// scales
for (int32_t i = 0; i < mParam[id].numAspectRatios; i++)
{
scales.push_back(tmpScales[id]);
}
auto scale_next = (id == mNumLayers - 1)
? 1.0
: (mParam[id].minSize + (mParam[id].maxSize - mParam[id].minSize) * (id + 1) / (mNumLayers - 1));
scales.push_back(std::sqrt(tmpScales[id] * scale_next));
mNumPriors[id] = mParam[id].numAspectRatios + 1;
}
std::vector<float> tmpWidths;
std::vector<float> tmpHeights;
// Calculate the width and height of the prior boxes
for (int32_t i = 0; i < mNumPriors[id]; i++)
{
float sqrt_AR = std::sqrt(aspect_ratios[i]);
tmpWidths.push_back(scales[i] * sqrt_AR);
tmpHeights.push_back(scales[i] / sqrt_AR);
}
mDeviceWidths[id] = copyToDevice(tmpWidths.data(), tmpWidths.size());
mDeviceHeights[id] = copyToDevice(tmpHeights.data(), tmpHeights.size());
}
}
GridAnchorGenerator::GridAnchorGenerator(void const* data, size_t length, char const* name)
: mPluginName(name)
{
char const *d = reinterpret_cast<char const*>(data), *a = d;
mNumLayers = read<int32_t>(d);
PLUGIN_CUASSERT(cudaMallocHost((void**) &mNumPriors, mNumLayers * sizeof(int32_t)));
PLUGIN_CUASSERT(cudaMallocHost((void**) &mDeviceWidths, mNumLayers * sizeof(Weights)));
PLUGIN_CUASSERT(cudaMallocHost((void**) &mDeviceHeights, mNumLayers * sizeof(Weights)));
mParam.resize(mNumLayers);
for (int32_t id = 0; id < mNumLayers; id++)
{
// we have to deserialize GridAnchorParameters by hand
mParam[id].minSize = read<float>(d);
mParam[id].maxSize = read<float>(d);
mParam[id].numAspectRatios = read<int32_t>(d);
mParam[id].aspectRatios = (float*) malloc(sizeof(float) * mParam[id].numAspectRatios);
for (int32_t i = 0; i < mParam[id].numAspectRatios; ++i)
{
mParam[id].aspectRatios[i] = read<float>(d);
}
mParam[id].H = read<int32_t>(d);
mParam[id].W = read<int32_t>(d);
for (int32_t i = 0; i < 4; ++i)
{
mParam[id].variance[i] = read<float>(d);
}
mNumPriors[id] = read<int32_t>(d);
mDeviceWidths[id] = deserializeToDevice(d, mNumPriors[id]);
mDeviceHeights[id] = deserializeToDevice(d, mNumPriors[id]);
}
PLUGIN_VALIDATE(d == a + length);
}
GridAnchorGenerator::~GridAnchorGenerator()
{
for (int32_t id = 0; id < mNumLayers; id++)
{
PLUGIN_CUERROR(cudaFree(const_cast<void*>(mDeviceWidths[id].values)));
PLUGIN_CUERROR(cudaFree(const_cast<void*>(mDeviceHeights[id].values)));
free(mParam[id].aspectRatios);
}
PLUGIN_CUERROR(cudaFreeHost(mNumPriors));
PLUGIN_CUERROR(cudaFreeHost(mDeviceWidths));
PLUGIN_CUERROR(cudaFreeHost(mDeviceHeights));
}
int32_t GridAnchorGenerator::getNbOutputs() const noexcept
{
return mNumLayers;
}
Dims GridAnchorGenerator::getOutputDimensions(int32_t index, Dims const* inputs, int32_t nbInputDims) noexcept
{
// Particularity of the PriorBox layer: no batchSize dimension needed
// 2 channels. First channel stores the mean of each prior coordinate.
// Second channel stores the variance of each prior coordinate.
return Dims3(2, mParam[index].H * mParam[index].W * mNumPriors[index] * 4, 1);
}
int32_t GridAnchorGenerator::initialize() noexcept
{
return STATUS_SUCCESS;
}
void GridAnchorGenerator::terminate() noexcept {}
size_t GridAnchorGenerator::getWorkspaceSize(int32_t maxBatchSize) const noexcept
{
return 0;
}
int32_t GridAnchorGenerator::enqueue(
int32_t batchSize, void const* const* inputs, void* const* outputs, void* workspace, cudaStream_t stream) noexcept
{
// Generate prior boxes for each layer
for (int32_t id = 0; id < mNumLayers; id++)
{
void* outputData = outputs[id];
pluginStatus_t status = anchorGridInference(
stream, mParam[id], mNumPriors[id], mDeviceWidths[id].values, mDeviceHeights[id].values, outputData);
if (status != STATUS_SUCCESS)
{
return status;
}
}
return STATUS_SUCCESS;
}
size_t GridAnchorGenerator::getSerializationSize() const noexcept
{
size_t sum = sizeof(int32_t); // mNumLayers
for (int32_t i = 0; i < mNumLayers; i++)
{
sum += 4 * sizeof(int32_t); // mNumPriors, mParam[i].{numAspectRatios, H, W}
sum += (6 + mParam[i].numAspectRatios)
* sizeof(float); // mParam[i].{minSize, maxSize, aspectRatios, variance[4]}
sum += mDeviceWidths[i].count * sizeof(float);
sum += mDeviceHeights[i].count * sizeof(float);
}
return sum;
}
void GridAnchorGenerator::serialize(void* buffer) const noexcept
{
char *d = reinterpret_cast<char*>(buffer), *a = d;
write(d, mNumLayers);
for (int32_t id = 0; id < mNumLayers; id++)
{
// we have to serialize GridAnchorParameters by hand
write(d, mParam[id].minSize);
write(d, mParam[id].maxSize);
write(d, mParam[id].numAspectRatios);
for (int32_t i = 0; i < mParam[id].numAspectRatios; ++i)
{
write(d, mParam[id].aspectRatios[i]);
}
write(d, mParam[id].H);
write(d, mParam[id].W);
for (int32_t i = 0; i < 4; ++i)
{
write(d, mParam[id].variance[i]);
}
write(d, mNumPriors[id]);
serializeFromDevice(d, mDeviceWidths[id]);
serializeFromDevice(d, mDeviceHeights[id]);
}
PLUGIN_ASSERT(d == a + getSerializationSize());
}
Weights GridAnchorGenerator::copyToDevice(void const* hostData, size_t count) noexcept
{
void* deviceData;
PLUGIN_CUASSERT(cudaMalloc(&deviceData, count * sizeof(float)));
PLUGIN_CUASSERT(cudaMemcpy(deviceData, hostData, count * sizeof(float), cudaMemcpyHostToDevice));
return Weights{DataType::kFLOAT, deviceData, int64_t(count)};
}
void GridAnchorGenerator::serializeFromDevice(char*& hostBuffer, Weights deviceWeights) const noexcept
{
PLUGIN_CUASSERT(
cudaMemcpy(hostBuffer, deviceWeights.values, deviceWeights.count * sizeof(float), cudaMemcpyDeviceToHost));
hostBuffer += deviceWeights.count * sizeof(float);
}
Weights GridAnchorGenerator::deserializeToDevice(char const*& hostBuffer, size_t count) noexcept
{
Weights w = copyToDevice(hostBuffer, count);
hostBuffer += count * sizeof(float);
return w;
}
bool GridAnchorGenerator::supportsFormat(DataType type, PluginFormat format) const noexcept
{
return (type == DataType::kFLOAT && format == PluginFormat::kLINEAR);
}
char const* GridAnchorGenerator::getPluginType() const noexcept
{
return mPluginName.c_str();
}
char const* GridAnchorGenerator::getPluginVersion() const noexcept
{
return kGRID_ANCHOR_PLUGIN_VERSION;
}
// Set plugin namespace
void GridAnchorGenerator::setPluginNamespace(char const* pluginNamespace) noexcept
{
mPluginNamespace = pluginNamespace;
}
char const* GridAnchorGenerator::getPluginNamespace() const noexcept
{
return mPluginNamespace.c_str();
}
#include <iostream>
// Return the DataType of the plugin output at the requested index
DataType GridAnchorGenerator::getOutputDataType(
int32_t index, nvinfer1::DataType const* inputTypes, int32_t nbInputs) const noexcept
{
PLUGIN_ASSERT(index < mNumLayers);
return DataType::kFLOAT;
}
// Configure the layer with input and output data types.
void GridAnchorGenerator::configurePlugin(Dims const* inputDims, int32_t nbInputs, Dims const* outputDims,
int32_t nbOutputs, DataType const* inputTypes, DataType const* outputTypes, bool const* inputIsBroadcast,
bool const* outputIsBroadcast, PluginFormat floatFormat, int32_t maxBatchSize) noexcept
{
PLUGIN_ASSERT(nbOutputs == mNumLayers);
PLUGIN_ASSERT(outputDims[0].nbDims == 3);
}
// Attach the plugin object to an execution context and grant the plugin the access to some context resource.
void GridAnchorGenerator::attachToContext(
cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) noexcept
{
}
// Detach the plugin object from its execution context.
void GridAnchorGenerator::detachFromContext() noexcept {}
void GridAnchorGenerator::destroy() noexcept
{
delete this;
}
IPluginV2Ext* GridAnchorGenerator::clone() const noexcept
{
try
{
auto plugin = std::make_unique<GridAnchorGenerator>(mParam.data(), mNumLayers, mPluginName.c_str());
plugin->setPluginNamespace(mPluginNamespace.c_str());
return plugin.release();
}
catch (std::exception const& e)
{
caughtError(e);
}
return nullptr;
}
GridAnchorBasePluginCreator::GridAnchorBasePluginCreator()
{
mPluginAttributes.clear();
mPluginAttributes.emplace_back(PluginField("minSize", nullptr, PluginFieldType::kFLOAT32, 1));
mPluginAttributes.emplace_back(PluginField("maxSize", nullptr, PluginFieldType::kFLOAT32, 1));
mPluginAttributes.emplace_back(PluginField("aspectRatios", nullptr, PluginFieldType::kFLOAT32, 1));
mPluginAttributes.emplace_back(PluginField("featureMapShapes", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("variance", nullptr, PluginFieldType::kFLOAT32, 4));
mPluginAttributes.emplace_back(PluginField("numLayers", nullptr, PluginFieldType::kINT32, 1));
mFC.nbFields = mPluginAttributes.size();
mFC.fields = mPluginAttributes.data();
}
char const* GridAnchorBasePluginCreator::getPluginName() const noexcept
{
return mPluginName.c_str();
}
char const* GridAnchorBasePluginCreator::getPluginVersion() const noexcept
{
return kGRID_ANCHOR_PLUGIN_VERSION;
}
PluginFieldCollection const* GridAnchorBasePluginCreator::getFieldNames() noexcept
{
return &mFC;
}
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
IPluginV2Ext* GridAnchorBasePluginCreator::createPlugin(char const* name, PluginFieldCollection const* fc) noexcept
{
try
{
using namespace std::string_view_literals;
float minScale = 0.2F, maxScale = 0.95F;
int32_t numLayers = 6;
std::vector<float> aspectRatios;
std::vector<int32_t> fMapShapes;
std::vector<float> layerVariances;
PluginField const* fields = fc->fields;
bool const isFMapRect = (kGRID_ANCHOR_PLUGIN_NAMES[1] == mPluginName);
for (int32_t i = 0; i < fc->nbFields; ++i)
{
std::string_view const attrName = fields[i].name;
if (attrName == "numLayers"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kINT32);
numLayers = static_cast<int32_t>(*(static_cast<int32_t const*>(fields[i].data)));
}
else if (attrName == "minSize"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kFLOAT32);
minScale = static_cast<float>(*(static_cast<float const*>(fields[i].data)));
}
else if (attrName == "maxSize"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kFLOAT32);
maxScale = static_cast<float>(*(static_cast<float const*>(fields[i].data)));
}
else if (attrName == "variance"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kFLOAT32);
int32_t size = fields[i].length;
layerVariances.reserve(size);
auto const* lVar = static_cast<float const*>(fields[i].data);
for (int32_t j = 0; j < size; j++)
{
layerVariances.push_back(*lVar);
lVar++;
}
}
else if (attrName == "aspectRatios"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kFLOAT32);
int32_t size = fields[i].length;
aspectRatios.reserve(size);
auto const* aR = static_cast<float const*>(fields[i].data);
for (int32_t j = 0; j < size; j++)
{
aspectRatios.push_back(*aR);
aR++;
}
}
else if (attrName == "featureMapShapes"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kINT32);
int32_t size = fields[i].length;
PLUGIN_VALIDATE(!isFMapRect || (size % 2 == 0));
fMapShapes.reserve(size);
int32_t const* fMap = static_cast<int32_t const*>(fields[i].data);
for (int32_t j = 0; j < size; j++)
{
fMapShapes.push_back(*fMap);
fMap++;
}
}
}
// Reducing the number of boxes predicted by the first layer.
// This is in accordance with the standard implementation.
std::vector<float> firstLayerAspectRatios;
PLUGIN_VALIDATE(numLayers > 0);
int32_t const numExpectedLayers = static_cast<int32_t>(fMapShapes.size()) >> (isFMapRect ? 1 : 0);
PLUGIN_VALIDATE(numExpectedLayers == numLayers);
int32_t numFirstLayerARs = 3;
// First layer only has the first 3 aspect ratios from aspectRatios
firstLayerAspectRatios.reserve(numFirstLayerARs);
for (int32_t i = 0; i < numFirstLayerARs; ++i)
{
firstLayerAspectRatios.push_back(aspectRatios[i]);
}
// A comprehensive list of box parameters that are required by anchor generator
std::vector<GridAnchorParameters> boxParams(numLayers);
// One set of box parameters for one layer
for (int32_t i = 0; i < numLayers; i++)
{
int32_t hOffset = (isFMapRect ? i * 2 : i);
int32_t wOffset = (isFMapRect ? i * 2 + 1 : i);
// Only the first layer is different
if (i == 0)
{
boxParams[i] = {minScale, maxScale, firstLayerAspectRatios.data(),
(int32_t) firstLayerAspectRatios.size(), fMapShapes[hOffset], fMapShapes[wOffset],
{layerVariances[0], layerVariances[1], layerVariances[2], layerVariances[3]}};
}
else
{
boxParams[i] = {minScale, maxScale, aspectRatios.data(), (int32_t) aspectRatios.size(),
fMapShapes[hOffset], fMapShapes[wOffset],
{layerVariances[0], layerVariances[1], layerVariances[2], layerVariances[3]}};
}
}
auto obj = std::make_unique<GridAnchorGenerator>(boxParams.data(), numLayers, mPluginName.c_str());
obj->setPluginNamespace(mNamespace.c_str());
return obj.release();
}
catch (std::exception const& e)
{
caughtError(e);
}
return nullptr;
}
IPluginV2Ext* GridAnchorBasePluginCreator::deserializePlugin(
char const* name, void const* serialData, size_t serialLength) noexcept
{
try
{
// This object will be deleted when the network is destroyed, which will
// call GridAnchor::destroy()
auto obj = std::make_unique<GridAnchorGenerator>(serialData, serialLength, mPluginName.c_str());
obj->setPluginNamespace(mNamespace.c_str());
return obj.release();
}
catch (std::exception const& e)
{
caughtError(e);
}
return nullptr;
}
GridAnchorPluginCreator::GridAnchorPluginCreator()
{
mPluginName = kGRID_ANCHOR_PLUGIN_NAMES[0];
}
GridAnchorRectPluginCreator::GridAnchorRectPluginCreator()
{
mPluginName = kGRID_ANCHOR_PLUGIN_NAMES[1];
}
} // namespace nvinfer1::plugin
+143
View File
@@ -0,0 +1,143 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 1993-2026 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.
*/
#ifndef TRT_GRID_ANCHOR_PLUGIN_H
#define TRT_GRID_ANCHOR_PLUGIN_H
#include "common/kernels/kernel.h"
#include "common/plugin.h"
#include <string>
#include <vector>
namespace nvinfer1
{
namespace plugin
{
class TRT_DEPRECATED_BECAUSE("Anchor-based detection models are obsolete.") GridAnchorGenerator : public IPluginV2Ext
{
public:
GridAnchorGenerator(GridAnchorParameters const* param, int32_t numLayers, char const* version);
GridAnchorGenerator(void const* data, size_t length, char const* version);
~GridAnchorGenerator() override;
int32_t getNbOutputs() const noexcept override;
Dims getOutputDimensions(int32_t index, Dims const* inputs, int32_t nbInputDims) noexcept override;
int32_t initialize() noexcept override;
void terminate() noexcept override;
size_t getWorkspaceSize(int32_t maxBatchSize) const noexcept override;
int32_t enqueue(int32_t batchSize, void const* const* inputs, void* const* outputs, void* workspace,
cudaStream_t stream) noexcept override;
size_t getSerializationSize() const noexcept override;
void serialize(void* buffer) const noexcept override;
bool supportsFormat(DataType type, PluginFormat format) const noexcept override;
char const* getPluginType() const noexcept override;
char const* getPluginVersion() const noexcept override;
void destroy() noexcept override;
IPluginV2Ext* clone() const noexcept override;
void setPluginNamespace(char const* pluginNamespace) noexcept override;
char const* getPluginNamespace() const noexcept override;
DataType getOutputDataType(
int32_t index, nvinfer1::DataType const* inputTypes, int32_t nbInputs) const noexcept override;
void attachToContext(
cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) noexcept override;
void configurePlugin(Dims const* inputDims, int32_t nbInputs, Dims const* outputDims, int32_t nbOutputs,
DataType const* inputTypes, DataType const* outputTypes, bool const* inputIsBroadcast,
bool const* outputIsBroadcast, PluginFormat floatFormat, int32_t maxBatchSize) noexcept override;
void detachFromContext() noexcept override;
protected:
std::string mPluginName;
private:
Weights copyToDevice(void const* hostData, size_t count) noexcept;
void serializeFromDevice(char*& hostBuffer, Weights deviceWeights) const noexcept;
Weights deserializeToDevice(char const*& hostBuffer, size_t count) noexcept;
int32_t mNumLayers;
std::vector<GridAnchorParameters> mParam;
int32_t* mNumPriors;
Weights *mDeviceWidths, *mDeviceHeights;
std::string mPluginNamespace;
};
class TRT_DEPRECATED_BECAUSE("Anchor-based detection models are obsolete.") GridAnchorBasePluginCreator
: public nvinfer1::pluginInternal::BaseCreator
{
public:
GridAnchorBasePluginCreator();
~GridAnchorBasePluginCreator() override = default;
char const* getPluginName() const noexcept override;
char const* getPluginVersion() const noexcept override;
PluginFieldCollection const* getFieldNames() noexcept override;
IPluginV2Ext* createPlugin(char const* name, PluginFieldCollection const* fc) noexcept override;
IPluginV2Ext* deserializePlugin(char const* name, void const* serialData, size_t serialLength) noexcept override;
protected:
std::string mPluginName;
private:
PluginFieldCollection mFC;
std::vector<PluginField> mPluginAttributes;
};
class TRT_DEPRECATED_BECAUSE("Anchor-based detection models are obsolete.") GridAnchorPluginCreator
: public GridAnchorBasePluginCreator
{
public:
GridAnchorPluginCreator();
~GridAnchorPluginCreator() override = default;
};
class TRT_DEPRECATED_BECAUSE("Anchor-based detection models are obsolete.") GridAnchorRectPluginCreator
: public GridAnchorBasePluginCreator
{
public:
GridAnchorRectPluginCreator();
~GridAnchorRectPluginCreator() override = default;
};
} // namespace plugin
} // namespace nvinfer1
#endif // TRT_GRID_ANCHOR_PLUGIN_H