chore: import upstream snapshot with attribution
Docker Image CI / build-ubuntu2004 (push) Has been cancelled

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
+21
View File
@@ -0,0 +1,21 @@
#
# 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(
priorBoxPlugin.cpp
priorBoxPlugin.h
)
+103
View File
@@ -0,0 +1,103 @@
# priorBoxPlugin [DEPRECATED]
**This plugin is deprecated since TensorRT 10.12 and will be removed in a future release. No alternatives are planned to be provided.**
**Table Of Contents**
- [Description](#description)
* [Structure](#structure)
- [Parameters](#parameters)
- [Additional resources](#additional-resources)
- [License](#license)
- [Changelog](#changelog)
- [Known issues](#known-issues)
## Description
The `priorBoxPlugin` generates prior boxes (anchor boxes) from a feature map in object detection models such as SSD. This plugin is included in TensorRT.
This sample 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. The `priorBoxPlugin` uses a series of CUDA kernels in the `priorBoxLayer.cu` file to accelerate the process. The differences between `priorBoxPlugin` and `gridAnchorPlugin` is that `priorBoxPlugin` generates prior boxes for one feature map in the model at one time, while `gridAnchorPlugin` generates all prior boxes for all feature maps in the model at one time.
### Structure
Plugin `PriorBox` is created for each feature map. Plugin `PriorBox` takes no input (or one input to infer its shape information), and uses `PriorBoxParameters` to generate one output. The input is the feature map that needs to generate prior boxes and the output is the prior box data generated. The input has shape of `[N, C, H, W]` where `N` is the batch size, `C` is the number of channels, `H` is the height of the feature map input, and `W` is the width of the feature map input.
The output has shape `[2, H * W * numPriors * 4, 1]`. The first channel is for prior box coordinates. The second channel is for prior box scaling factors, which is simply a copy of the variance provided.
`H` and `W` are the height and width of the feature map the plugin is working on. `numPriors` is the number of prior boxes generated for one grid cell on the feature map. The value of `numPriors` is determined by the number of minimum sized box values, the number of maximum sized box values, the number of aspect ratios, and if we flip the aspect ratios or not. All the coordinates of prior boxes generated are in the format of `[x_min, y_min, x_max, y_max]`, and are scaled against image width and height in a range of `[0, 1]`.
A typical `PriorBox` layer in SSD300 implemented in Caffe looks similar to:
```
layer {
name: "conv6_2_mbox_priorbox"
type: "PriorBox"
bottom: "conv6_2"
bottom: "data"
top: "conv6_2_mbox_priorbox"
prior_box_param {
min_size: 111.0
max_size: 162.0
aspect_ratio: 2
aspect_ratio: 3
flip: true
clip: false
variance: 0.1
variance: 0.1
variance: 0.2
variance: 0.2
step: 32
offset: 0.5
}
}
```
## Parameters
This plugin has the plugin creator class `PriorBoxPluginCreator` and the plugin class `PriorBox`.
The `PriorBox` instance is created using `PriorBoxParameters`. The `PriorBoxParameters` is defined in [NvInferPlugin.h](https://docs.nvidia.com/deeplearning/sdk/tensorrt-api/c_api/structnvinfer1_1_1plugin_1_1_prior_box_parameters.html). It consists of the following parameters:
| Type | Parameter | Description
|-------------|--------------------------|--------------------------------------------------------
|`float *` |`minSize` |The minimum box size in pixels. Can not be `nullptr`. `minSize` points to a series of minimum box size values which are used to generate prior boxes with different aspect ratios. The width of prior box `w` is equal to one of the minimum box size values times the square root of one of the values from aspect ratios. The width of prior box `h` is equal to one of the minimum box size values divided by the square root of one of the values from aspect ratios. For example, to generate a prior box of min size = 30 with aspect ratio = 2, the width and height of the prior bounding box generated are 42 and 21 respectively. In the original SSD paper, only minimum box size value is provided for each feature map.
|`float *` |`maxSize` |The maximum box size in pixels. Can be `nullptr`. `maxSize` points to a series of maximum box size values which are used to generate additional prior boxes with aspect ratio `1`. The width of prior box is equal to the square root of one of the minimum box sizes values times the square root of its corresponding maximum box size value. For example, if min size = 30 and max size = 60, an additional prior box of width 42 and height 42 will be generated. In the original SSD paper, only one maximum box size value is provided for each feature map.
|`float *` |`aspectRatios` |The aspect ratios of the boxes. Can be `nullptr`. There is a built-in default aspect ratio of `1`. Therefore, it is not required to provide aspect ratio of `1` here. For example, if `aspectRatios = [2, 3]`, if `flip = true`, aspect ratios actually used is `[1, 2, 1/2, 3, 1/3]`; and if `flip = false`, aspect ratios actually used is `[1, 2, 3]`.
|`int` |`numMinSize` |The number of elements in `minSize`. Must be larger than `0`.
|`int` |`numMaxSize` |The number of elements in `maxSize`. Can be `0` or same as `numMinSize`.
|`int` |`numAspectRatios` |The number of elements in `aspectRatios`. Can be `0`.
|`bool` |`flip` |If `true`, will flip each aspect ratio. For example, if there is aspect ratio `r`, the aspect ratio `1.0/r` will be generated as well.
|`bool` |`clip` |If `true`, will clip the prior so that it is within `[0,1]`. Some prior boxes generated close to the border of the image will have coordinates larger than `1.0` or smaller than `0`. Setting `clip = true` will clip the out-of range coordinates so that all the coordinates fall into `[0, 1]`.
|`float` |`variance [4]` |The variances (scale factors) for adjusting the prior box coordinates encoding and decoding.
|`int` |`imgH` |The image height. If `0`, then the `H` dimension of the data tensor will be used. The height of the image input to the model. For example, for SSD300 model, `imgH = 300`.
|`int` |`imgW` |The image width. If `0`, then the `W` dimension of the data tensor will be used. The width of the image input to the model. For example, for SSD300 model, `imgW = 300`.
|`float` |`stepH` |The step in `H`. If `0`, then `(float)imgH/h` will be used where `h` is the `H` dimension of the first input tensor. For example, for SSD300 model, `imgH = 300` and the height of the first feature map is 38 x 38. Then, `stepH = 300 / 38 = 7.895`.
|`float` |`stepW` |The step in `W`. If `0`, then `(float)imgW/w` will be used where `w` is the `W` dimension of the first input tensor. For example, for SSD300 model, `imgW = 300` and the width of the first feature map is 38 x 38. Then, `stepW = 300 / 38 = 7.895`.
|`float` |`offset` |Offset to the top left corner of each cell. This value is usually set to `0.5` to make sure that the prior boxes generated have centroid located at the center of the grid in the feature map.
## Additional resources
The following resources provide a deeper understanding of the `priorBoxPlugin` plugin:
**Networks**
- [SSD: Single Shot MultiBox Detector](https://arxiv.org/abs/1512.02325)
## 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.
+525
View File
@@ -0,0 +1,525 @@
/*
* 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 "priorBoxPlugin.h"
#include <cmath>
#include <iostream>
#include <sstream>
#include <string_view>
#include <vector>
using namespace nvinfer1;
using namespace nvinfer1::plugin;
using nvinfer1::plugin::PriorBox;
using nvinfer1::plugin::PriorBoxPluginCreator;
namespace
{
char const* const kPRIOR_BOX_PLUGIN_VERSION{"1"};
char const* const kPRIOR_BOX_PLUGIN_NAME{"PriorBox_TRT"};
} // namespace
// Constructor
PriorBox::PriorBox(PriorBoxParameters param, int32_t H, int32_t W)
: mParam(param)
, mH(H)
, mW(W)
{
// Each object should manage its copy of param.
auto copyParamData = [](float*& dstPtr, std::vector<float>& dstVec, float const* src, int32_t size) {
PLUGIN_VALIDATE(size >= 0);
PLUGIN_VALIDATE(src != nullptr);
dstVec.resize(size);
dstPtr = dstVec.data();
std::copy_n(src, size, dstPtr);
};
copyParamData(mParam.minSize, mMinSizeCPU, param.minSize, param.numMinSize);
copyParamData(mParam.maxSize, mMaxSizeCPU, param.maxSize, param.numMaxSize);
copyParamData(mParam.aspectRatios, mAspectRatiosCPU, param.aspectRatios, param.numAspectRatios);
setupDeviceMemory();
}
void PriorBox::setupDeviceMemory() noexcept
{
auto copyToDevice = [](void const* hostData, int32_t count) -> Weights {
PLUGIN_VALIDATE(count >= 0);
void* deviceData = nullptr;
PLUGIN_CUASSERT(cudaMalloc(&deviceData, count * sizeof(float)));
PLUGIN_CUASSERT(cudaMemcpy(deviceData, hostData, count * sizeof(float), cudaMemcpyHostToDevice));
return Weights{DataType::kFLOAT, deviceData, static_cast<int64_t>(count)};
};
// minSize is required and needs to be positive.
PLUGIN_VALIDATE(mParam.numMinSize > 0);
PLUGIN_VALIDATE(mParam.minSize != nullptr);
for (int32_t i = 0; i < mParam.numMinSize; ++i)
{
PLUGIN_VALIDATE(mParam.minSize[i] > 0.F, "minSize must be positive");
}
mMinSizeGPU = copyToDevice(mParam.minSize, mParam.numMinSize);
PLUGIN_VALIDATE(mParam.numAspectRatios >= 0);
PLUGIN_VALIDATE(mParam.aspectRatios != nullptr);
// Aspect ratio of 1.0 is built in.
std::vector<float> tmpAR(1, 1);
for (int32_t i = 0; i < mParam.numAspectRatios; ++i)
{
float aspectRatio = mParam.aspectRatios[i];
bool alreadyExist = false;
// Prevent duplicated aspect ratios from input
for (size_t j = 0; j < tmpAR.size(); ++j)
{
if (std::fabs(aspectRatio - tmpAR[j]) < 1e-6)
{
alreadyExist = true;
break;
}
}
if (!alreadyExist)
{
PLUGIN_VALIDATE(aspectRatio > 0.F);
tmpAR.push_back(aspectRatio);
if (mParam.flip)
{
tmpAR.push_back(1.0F / aspectRatio);
}
}
}
//
// mAspectRatiosGPU is of type nvinfer1::Weights.
// https://docs.nvidia.com/deeplearning/sdk/tensorrt-api/c_api/classnvinfer1_1_1_weights.html
// mAspectRatiosGPU.count is different to mParam.numAspectRatios.
//
mAspectRatiosGPU = copyToDevice(tmpAR.data(), tmpAR.size());
// Number of prior boxes per grid cell on the feature map
// tmpAR already included an aspect ratio of 1.0
mNumPriors = tmpAR.size() * mParam.numMinSize;
//
// If we have maxSizes, as long as all the maxSizes meets assertion requirement, we add one bounding box per maxSize
// The final number of prior boxes per grid cell on feature map
// mNumPriors =
// tmpAR.size() * mParam.numMinSize If numMaxSize == 0
// (tmpAR.size() + 1) * mParam.numMinSize If mParam.numMinSize == mParam.numMaxSize
//
if (mParam.numMaxSize > 0)
{
PLUGIN_VALIDATE(mParam.numMinSize == mParam.numMaxSize);
PLUGIN_VALIDATE(mParam.maxSize != nullptr);
PLUGIN_VALIDATE(mParam.minSize != nullptr);
for (int32_t i = 0; i < mParam.numMaxSize; ++i)
{
// maxSize should be greater than minSize
// NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
PLUGIN_VALIDATE(mParam.maxSize[i] > mParam.minSize[i], "maxSize must be greater than minSize");
mNumPriors++;
}
mMaxSizeGPU = copyToDevice(mParam.maxSize, mParam.numMaxSize);
}
}
PriorBox::PriorBox(void const* data, size_t length)
{
deserialize(static_cast<uint8_t const*>(data), length);
}
void PriorBox::deserialize(uint8_t const* data, size_t length)
{
auto const* d{data};
mParam = read<PriorBoxParameters>(d);
auto readArray = [&d](int32_t size, std::vector<float>& dstVec, float*& dstPtr) {
PLUGIN_VALIDATE(size >= 0);
dstVec.resize(size);
for (int32_t i = 0; i < size; i++)
{
dstVec[i] = read<float>(d);
}
dstPtr = dstVec.data();
};
readArray(mParam.numMinSize, mMinSizeCPU, mParam.minSize);
readArray(mParam.numMaxSize, mMaxSizeCPU, mParam.maxSize);
readArray(mParam.numAspectRatios, mAspectRatiosCPU, mParam.aspectRatios);
mH = read<int32_t>(d);
mW = read<int32_t>(d);
PLUGIN_VALIDATE(d == data + length);
setupDeviceMemory();
}
// Returns the number of output from the plugin layer
int32_t PriorBox::getNbOutputs() const noexcept
{
// Number of outputs from the plugin layer is 1
return 1;
}
// Computes and returns the output dimensions
Dims PriorBox::getOutputDimensions(int32_t index, Dims const* inputs, int32_t nbInputDims) noexcept
{
PLUGIN_VALIDATE(nbInputDims == 2);
// Only one output from the plugin layer
PLUGIN_VALIDATE(index == 0);
// Particularity of the PriorBox layer: no batchSize dimension needed
mH = inputs[0].d[1];
mW = inputs[0].d[2];
// workaround for TRT
// The first channel is for prior box coordinates.
// The second channel is for prior box scaling factors, which is simply a copy of the variance provided.
return Dims3(2, mH * mW * mNumPriors * 4, 1);
}
int32_t PriorBox::initialize() noexcept
{
return STATUS_SUCCESS;
}
size_t PriorBox::getWorkspaceSize(int32_t /*maxBatchSize*/) const noexcept
{
return 0;
}
int32_t PriorBox::enqueue(int32_t /*batchSize*/, void const* const* /*inputs*/, void* const* outputs,
void* /*workspace*/, cudaStream_t stream) noexcept
{
void* outputData = outputs[0];
pluginStatus_t status = priorBoxInference(stream, mParam, mH, mW, mNumPriors, mAspectRatiosGPU.count,
mMinSizeGPU.values, mMaxSizeGPU.values, mAspectRatiosGPU.values, outputData);
return status;
}
// Returns the size of serialized parameters
size_t PriorBox::getSerializationSize() const noexcept
{
// PriorBoxParameters, minSize, maxSize, aspectRatios, mH, mW - the construct parameters
return sizeof(PriorBoxParameters) + sizeof(float) * (mParam.numMinSize + mParam.numMaxSize + mParam.numAspectRatios)
+ sizeof(int32_t) * 2;
}
void PriorBox::serialize(void* buffer) const noexcept
{
uint8_t* d = static_cast<uint8_t*>(buffer);
uint8_t* a = d;
write(d, mParam);
auto writeArray = [&d](int32_t const size, float const* srcPtr, std::vector<float> const& srcVec) {
// srcVec is only used here to check that the size and srcPtr are correct.
PLUGIN_VALIDATE(srcVec.data() == srcPtr);
PLUGIN_VALIDATE(srcVec.size() == static_cast<size_t>(size));
for (int32_t i = 0; i < size; i++)
{
write(d, srcPtr[i]);
}
};
writeArray(mParam.numMinSize, mParam.minSize, mMinSizeCPU);
writeArray(mParam.numMaxSize, mParam.maxSize, mMaxSizeCPU);
writeArray(mParam.numAspectRatios, mParam.aspectRatios, mAspectRatiosCPU);
write(d, mH);
write(d, mW);
PLUGIN_VALIDATE(d == a + getSerializationSize());
}
bool PriorBox::supportsFormat(DataType type, PluginFormat format) const noexcept
{
return (type == DataType::kFLOAT && format == PluginFormat::kLINEAR);
}
char const* PriorBox::getPluginType() const noexcept
{
return kPRIOR_BOX_PLUGIN_NAME;
}
char const* PriorBox::getPluginVersion() const noexcept
{
return kPRIOR_BOX_PLUGIN_VERSION;
}
void PriorBox::destroy() noexcept
{
PLUGIN_CUASSERT(cudaFree(const_cast<void*>(mMinSizeGPU.values)));
if (mParam.numMaxSize > 0)
{
PLUGIN_CUASSERT(cudaFree(const_cast<void*>(mMaxSizeGPU.values)));
}
if (mParam.numAspectRatios > 0)
{
PLUGIN_CUASSERT(cudaFree(const_cast<void*>(mAspectRatiosGPU.values)));
}
delete this;
}
IPluginV2Ext* PriorBox::clone() const noexcept
{
try
{
auto obj = std::make_unique<PriorBox>(mParam, mH, mW);
obj->setPluginNamespace(mPluginNamespace.c_str());
return obj.release();
}
catch (std::exception const& e)
{
caughtError(e);
}
return nullptr;
}
// Set plugin namespace
void PriorBox::setPluginNamespace(char const* pluginNamespace) noexcept
{
PLUGIN_VALIDATE(pluginNamespace != nullptr);
mPluginNamespace = pluginNamespace;
}
char const* PriorBox::getPluginNamespace() const noexcept
{
return mPluginNamespace.c_str();
}
// Return the DataType of the plugin output at the requested index.
DataType PriorBox::getOutputDataType(
int32_t index, nvinfer1::DataType const* /*inputTypes*/, int32_t /*nbInputs*/) const noexcept
{
// Two outputs
PLUGIN_VALIDATE(index == 0 || index == 1);
return DataType::kFLOAT;
}
// Configure the layer with input and output data types.
void PriorBox::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_VALIDATE(nbInputs == 2);
PLUGIN_VALIDATE(nbOutputs == 1);
PLUGIN_VALIDATE(inputDims && outputDims && inputTypes);
PLUGIN_VALIDATE(*inputTypes == DataType::kFLOAT && floatFormat == PluginFormat::kLINEAR);
PLUGIN_VALIDATE(inputDims[0].nbDims == 3);
PLUGIN_VALIDATE(inputDims[1].nbDims == 3);
PLUGIN_VALIDATE(outputDims[0].nbDims == 3);
mH = inputDims[0].d[1];
mW = inputDims[0].d[2];
// Prepare for the inference function.
if (mParam.imgH == 0 || mParam.imgW == 0)
{
mParam.imgH = inputDims[1].d[1];
mParam.imgW = inputDims[1].d[2];
}
if (mParam.stepH == 0 || mParam.stepW == 0)
{
mParam.stepH = static_cast<float>(mParam.imgH) / mH;
mParam.stepW = static_cast<float>(mParam.imgW) / mW;
}
}
// Attach the plugin object to an execution context and grant the plugin the access to some context resource.
void PriorBox::attachToContext(
cudnnContext* /*cudnnContext*/, cublasContext* /*cublasContext*/, IGpuAllocator* /*gpuAllocator*/) noexcept
{
}
// Detach the plugin object from its execution context.
void PriorBox::detachFromContext() noexcept {}
PriorBoxPluginCreator::PriorBoxPluginCreator()
{
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("flip", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("clip", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("variance", nullptr, PluginFieldType::kFLOAT32, 4));
mPluginAttributes.emplace_back(PluginField("imgH", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("imgW", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("stepH", nullptr, PluginFieldType::kFLOAT32, 1));
mPluginAttributes.emplace_back(PluginField("stepW", nullptr, PluginFieldType::kFLOAT32, 1));
mPluginAttributes.emplace_back(PluginField("offset", nullptr, PluginFieldType::kFLOAT32, 1));
mFC.nbFields = mPluginAttributes.size();
mFC.fields = mPluginAttributes.data();
}
PriorBoxPluginCreator::~PriorBoxPluginCreator()
{
// Free allocated memory (if any) here
}
char const* PriorBoxPluginCreator::getPluginName() const noexcept
{
return kPRIOR_BOX_PLUGIN_NAME;
}
char const* PriorBoxPluginCreator::getPluginVersion() const noexcept
{
return kPRIOR_BOX_PLUGIN_VERSION;
}
PluginFieldCollection const* PriorBoxPluginCreator::getFieldNames() noexcept
{
return &mFC;
}
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
IPluginV2Ext* PriorBoxPluginCreator::createPlugin(char const* /*name*/, PluginFieldCollection const* fc) noexcept
{
try
{
PluginField const* fields = fc->fields;
PriorBoxParameters params;
std::vector<float> minSize;
std::vector<float> maxSize;
std::vector<float> aspectRatios;
using namespace std::string_view_literals;
for (auto i = 0; i < fc->nbFields; ++i)
{
std::string_view const attrName = fields[i].name;
if (attrName == "minSize"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kFLOAT32);
int32_t const size = fields[i].length;
params.numMinSize = size;
if (size > 0)
{
minSize.resize(size);
params.minSize = minSize.data();
auto const* minS = static_cast<float const*>(fields[i].data);
std::copy_n(minS, size, params.minSize);
}
else
{
params.minSize = nullptr;
}
}
else if (attrName == "maxSize"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kFLOAT32);
int32_t const size = fields[i].length;
params.numMaxSize = size;
if (size > 0)
{
maxSize.resize(size);
params.maxSize = maxSize.data();
auto const* maxS = static_cast<float const*>(fields[i].data);
std::copy_n(maxS, size, params.maxSize);
}
else
{
params.maxSize = nullptr;
}
}
else if (attrName == "aspectRatios"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kFLOAT32);
int32_t const size = fields[i].length;
params.numAspectRatios = size;
if (size > 0)
{
aspectRatios.resize(size);
params.aspectRatios = aspectRatios.data();
auto const* aR = static_cast<float const*>(fields[i].data);
std::copy_n(aR, size, params.aspectRatios);
}
else
{
params.aspectRatios = nullptr;
}
}
else if (attrName == "variance"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kFLOAT32);
int32_t const size = fields[i].length;
PLUGIN_VALIDATE(size == 4);
auto const* lVar = static_cast<float const*>(fields[i].data);
for (auto j = 0; j < size; j++)
{
params.variance[j] = (*lVar);
lVar++;
}
}
else if (attrName == "flip"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kINT32);
params.flip = *(static_cast<int32_t const*>(fields[i].data));
}
else if (attrName == "clip"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kINT32);
params.clip = *(static_cast<int32_t const*>(fields[i].data));
}
else if (attrName == "imgH"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kINT32);
params.imgH = *(static_cast<int32_t const*>(fields[i].data));
}
else if (attrName == "imgW"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kINT32);
params.imgW = *(static_cast<int32_t const*>(fields[i].data));
}
else if (attrName == "stepH"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kFLOAT32);
params.stepH = *(static_cast<float const*>(fields[i].data));
}
else if (attrName == "stepW"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kFLOAT32);
params.stepW = *(static_cast<float const*>(fields[i].data));
}
else if (attrName == "offset"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kFLOAT32);
params.offset = *(static_cast<float const*>(fields[i].data));
}
}
auto obj = std::make_unique<PriorBox>(params);
obj->setPluginNamespace(mNamespace.c_str());
return obj.release();
}
catch (std::exception const& e)
{
caughtError(e);
}
return nullptr;
}
IPluginV2Ext* PriorBoxPluginCreator::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 PriorBox::destroy()
auto obj = std::make_unique<PriorBox>(serialData, serialLength);
obj->setPluginNamespace(mNamespace.c_str());
return obj.release();
}
catch (std::exception const& e)
{
caughtError(e);
}
return nullptr;
}
+129
View File
@@ -0,0 +1,129 @@
/*
* 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_PRIOR_BOX_PLUGIN_H
#define TRT_PRIOR_BOX_PLUGIN_H
#include "common/kernels/kernel.h"
#include "common/plugin.h"
#include <cstdlib>
#include <string>
#include <vector>
namespace nvinfer1
{
namespace plugin
{
class PriorBox : public IPluginV2Ext
{
public:
PriorBox(PriorBoxParameters param, int32_t H = 0, int32_t W = 0);
PriorBox(void const* buffer, size_t length);
~PriorBox() override = default;
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;
private:
void deserialize(uint8_t const* buffer, size_t length);
void setupDeviceMemory() noexcept;
PriorBoxParameters mParam{};
int32_t mNumPriors{};
int32_t mH{};
int32_t mW{};
// Arrays stored on the GPU.
Weights mMinSizeGPU{}; // not learnable weights
Weights mMaxSizeGPU{}; // not learnable weights
Weights mAspectRatiosGPU{}; // not learnable weights
// Arrays stored on the CPU.
// Data pointers in mParams point to these vectors.
std::vector<float> mMinSizeCPU;
std::vector<float> mMaxSizeCPU;
std::vector<float> mAspectRatiosCPU;
std::string mPluginNamespace;
};
class PriorBoxPluginCreator : public nvinfer1::pluginInternal::BaseCreator
{
public:
PriorBoxPluginCreator();
~PriorBoxPluginCreator() override;
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;
private:
PluginFieldCollection mFC;
std::vector<PluginField> mPluginAttributes;
};
} // namespace plugin
} // namespace nvinfer1
#endif // TRT_PRIOR_BOX_PLUGIN_H