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(
regionPlugin.cpp
regionPlugin.h
)
+95
View File
@@ -0,0 +1,95 @@
# regionPlugin [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 `regionPlugin` is specifically used to generate encoded bounding box predictions, encoded bounding box objectness, and probabilities of bounding box being candidate objects for the YOLOv2 object detection model in TensorRT.
### Structure
The `regionPlugin` takes one input and generates one output.
The input has a shape of `[N, C, H, W]`, where:
- `N` is the batch size
- `C` is the number of channels in the input tensor. For example, `C = num * (coords + 1 + classes)`.
- `H` is the height of feature map
- `W` is the width of feature map
The information order of the channels are:
- `[t_x, t_y, t_w, t_h, t_o, d_1, d_2, ..., d_classes] for bbox_1`
- `[t_x, t_y, t_w, t_h, t_o, d_1, d_2, ..., d_classes] for bbox_2`, and so on
- `[t_x, t_y, t_w, t_h, t_o, d_1, d_2, ..., d_classes]` for `bbox_num`, totalling `num * (coords + 1 + classes)` channels.
Specifically:
- ` t_x, t_y, t_w, t_h` are the predicted offsets of bounding boxes before sigmoid activation
- `t_o` is the predicted objectness of the bounding box before sigmoid activation (see [YOLOv2 paper](https://arxiv.org/abs/1612.08242))
- `d_1, d_2, ..., d_classes` are the digits for each candidate class before the Softmax activation.
The output has the same shape as the input, in other words, `[N, C, H, W]`. The information order of the channels are:
- `[sigmoid(t_x), sigmoid(t_y), t_w, t_h, sigmoid(t_o), p_1, p_2, ..., p_classes]` for `bbox_1`
- `[sigmoid(t_x), sigmoid(t_y), t_w, t_h, sigmoid(t_o), p_1, p_2, ..., p_classes]` for `bbox_2`, and so on
- `[sigmoid(t_x), sigmoid(t_y), t_w, t_h, sigmoid(t_o), p_1, p_2, ..., p_classes]` for `bbox_num`, totalling `num * (coords + 1 + classes)` channels
Specifically:
- `sigmoid(t_x), sigmoid(t_y)`, and `sigmoid(t_o)` are sigmoid activated `t_x, t_y`, and `t_o` from the input
- `p_1, p_2, ..., p_classes` are the probability for each candidate class after the Softmax activation.
**Note:** `t_w` and `t_h` from the input remain unchanged.
## Parameters
The `regionPlugin` has a plugin creator class `RegionPluginCreator` and plugin class `Region`.
The following parameters were used to create the `Region` instance.
| Type | Parameter | Description
|----------|--------------------------|--------------------------------------------------------
|`int` |`num` |The number of predicted bounding box for each grid cell.
|`int` |`coords` |The number of coordinates for the bounding box. This value has to be `4`. Other values for `coords` are not supported currently.
|`int` |`classes` |The number of candidate classes to be predicted.
|`smTree` |`softmaxTree` |When performing yolo9000, `softmaxTree` is helping to perform Softmax on confidence scores, for example, to get the precise candidate classes through the word-tree structured candidate classes definition. `softmaxTree` is not required for non-hierarchical classification. The definition of `softmaxTree` can be found in [NvInferPlugin.h](https://docs.nvidia.com/deeplearning/sdk/tensorrt-api/c_api/_nv_infer_plugin_8h_source.html) and [here](https://docs.nvidia.com/deeplearning/sdk/tensorrt-api/c_api/structnvinfer1_1_1plugin_1_1softmax_tree.html).
|`bool` |`hasSoftmaxTree` |If `softmaxTree` is not `nullptr`, it is `true`; else it is `false`.
|`int` |`C` |The number of channels in the input tensor. `C = num * (coords + 1 + classes)` has to be satisfied.
|`int` |`H` |The height of the input tensor (feature map).
|`int` |`W` |The width of the input tensor (feature map).
## Additional resources
The following resources provide a deeper understanding of the `regionPlugin` plugin:
**Networks**
- [YOLOv2 paper](https://arxiv.org/abs/1612.08242)
## 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.
+551
View File
@@ -0,0 +1,551 @@
/*
* 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 "regionPlugin.h"
#include <memory>
#include <string_view>
namespace nvinfer1::plugin
{
namespace
{
char const* const kREGION_PLUGIN_VERSION{"1"};
char const* const kREGION_PLUGIN_NAME{"Region_TRT"};
template <typename T>
void safeFree(T* ptr)
{
if (ptr)
{
free(ptr);
ptr = nullptr;
}
}
template <typename T>
void allocateChunk(T*& ptr, int32_t count)
{
ptr = static_cast<T*>(malloc(count * sizeof(T)));
}
struct SoftmaxTreeDeleter
{
void operator()(softmaxTree* smTree) const
{
if (smTree)
{
// free individual elements first
safeFree(smTree->leaf);
safeFree(smTree->parent);
safeFree(smTree->child);
safeFree(smTree->group);
if (smTree->name)
{
for (int32_t i = 0; i < smTree->n; i++)
{
safeFree(smTree->name[i]);
}
safeFree(smTree->name);
}
safeFree(smTree->groupSize);
safeFree(smTree->groupOffset);
// free softmax tree
safeFree(smTree);
}
}
};
} // namespace
Region::Region(RegionParameters params)
: num(params.num)
, coords(params.coords)
, classes(params.classes)
, smTree(params.smTree, SoftmaxTreeDeleter())
{
}
Region::Region(RegionParameters params, int32_t C, int32_t H, int32_t W)
: num(params.num)
, coords(params.coords)
, classes(params.classes)
, smTree(params.smTree, SoftmaxTreeDeleter())
, C(C)
, H(H)
, W(W)
{
}
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
Region::Region(void const* buffer, size_t length)
{
char const *d = reinterpret_cast<char const*>(buffer), *a = d;
C = read<int32_t>(d);
H = read<int32_t>(d);
W = read<int32_t>(d);
num = read<int32_t>(d);
classes = read<int32_t>(d);
coords = read<int32_t>(d);
bool softmaxTreePresent = read<bool>(d);
bool leafPresent = read<bool>(d);
bool parentPresent = read<bool>(d);
bool childPresent = read<bool>(d);
bool groupPresent = read<bool>(d);
bool namePresent = read<bool>(d);
bool groupSizePresent = read<bool>(d);
bool groupOffsetPresent = read<bool>(d);
if (softmaxTreePresent)
{
softmaxTree* smTreeTemp;
// need to read each element individually
allocateChunk(smTreeTemp, 1);
smTreeTemp->n = read<int32_t>(d);
if (leafPresent)
{
allocateChunk(smTreeTemp->leaf, smTreeTemp->n);
}
else
{
smTreeTemp->leaf = nullptr;
}
if (parentPresent)
{
allocateChunk(smTreeTemp->parent, smTreeTemp->n);
}
else
{
smTreeTemp->parent = nullptr;
}
if (childPresent)
{
allocateChunk(smTreeTemp->child, smTreeTemp->n);
}
else
{
smTreeTemp->child = nullptr;
}
if (groupPresent)
{
allocateChunk(smTreeTemp->group, smTreeTemp->n);
}
else
{
smTreeTemp->group = nullptr;
}
for (int32_t i = 0; i < smTreeTemp->n; i++)
{
if (leafPresent)
{
smTreeTemp->leaf[i] = read<int32_t>(d);
}
if (parentPresent)
{
smTreeTemp->parent[i] = read<int32_t>(d);
}
if (childPresent)
{
smTreeTemp->child[i] = read<int32_t>(d);
}
if (groupPresent)
{
smTreeTemp->group[i] = read<int32_t>(d);
}
}
if (namePresent)
{
allocateChunk(smTreeTemp->name, smTreeTemp->n);
}
else
{
smTreeTemp->name = nullptr;
}
if (namePresent)
{
for (int32_t i = 0; i < smTreeTemp->n; i++)
{
allocateChunk(smTreeTemp->name[i], 256);
for (int32_t j = 0; j < 256; j++)
{
smTreeTemp->name[i][j] = read<char>(d);
}
}
}
smTreeTemp->groups = read<int32_t>(d);
if (groupSizePresent)
{
allocateChunk(smTreeTemp->groupSize, smTreeTemp->groups);
}
else
{
smTreeTemp->groupSize = nullptr;
}
if (groupOffsetPresent)
{
allocateChunk(smTreeTemp->groupOffset, smTreeTemp->groups);
}
else
{
smTreeTemp->groupOffset = nullptr;
}
for (int32_t i = 0; i < smTreeTemp->groups; i++)
{
if (groupSizePresent)
{
smTreeTemp->groupSize[i] = read<int32_t>(d);
}
if (groupOffsetPresent)
{
smTreeTemp->groupOffset[i] = read<int32_t>(d);
}
}
smTree = std::shared_ptr<softmaxTree>(smTreeTemp, SoftmaxTreeDeleter());
}
else
{
smTree.reset();
}
PLUGIN_VALIDATE(d == a + length);
}
int32_t Region::getNbOutputs() const noexcept
{
return 1;
}
Dims Region::getOutputDimensions(int32_t index, Dims const* inputs, int32_t nbInputDims) noexcept
{
PLUGIN_ASSERT(nbInputDims == 1);
PLUGIN_ASSERT(index == 0);
return inputs[0];
}
int32_t Region::enqueue(
int32_t batchSize, void const* const* inputs, void* const* outputs, void* workspace, cudaStream_t stream) noexcept
{
void const* inputData = inputs[0];
void* outputData = outputs[0];
if (smTree.get())
{
hasSoftmaxTree = true;
}
else
{
hasSoftmaxTree = false;
}
pluginStatus_t status = regionInference(
stream, batchSize, C, H, W, num, coords, classes, hasSoftmaxTree, smTree.get(), inputData, outputData);
return status;
}
size_t Region::getSerializationSize() const noexcept
{
// C, H, W, num, classes, coords, smTree !nullptr and other array members !nullptr, softmaxTree members
size_t count = 6 * sizeof(int32_t) + 8 * sizeof(bool);
if (smTree.get())
{
count += 2 * sizeof(int32_t);
if (smTree->leaf)
{
count += smTree->n * sizeof(int32_t);
}
if (smTree->parent)
{
count += smTree->n * sizeof(int32_t);
}
if (smTree->child)
{
count += smTree->n * sizeof(int32_t);
}
if (smTree->group)
{
count += smTree->n * sizeof(int32_t);
}
if (smTree->name)
{
count += smTree->n * 256 * sizeof(char);
}
if (smTree->groupSize)
{
count += smTree->groups * sizeof(int32_t);
}
if (smTree->groupOffset)
{
count += smTree->groups * sizeof(int32_t);
}
}
return count;
}
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
void Region::serialize(void* buffer) const noexcept
{
char *d = reinterpret_cast<char*>(buffer), *a = d;
write(d, C);
write(d, H);
write(d, W);
write(d, num);
write(d, classes);
write(d, coords);
write(d, smTree != nullptr);
write(d, smTree != nullptr && smTree->leaf != nullptr);
write(d, smTree != nullptr && smTree->parent != nullptr);
write(d, smTree != nullptr && smTree->child != nullptr);
write(d, smTree != nullptr && smTree->group != nullptr);
write(d, smTree != nullptr && smTree->name != nullptr);
write(d, smTree != nullptr && smTree->groupSize != nullptr);
write(d, smTree != nullptr && smTree->groupOffset != nullptr);
// need to do a deep copy
if (smTree)
{
write(d, smTree->n);
for (int32_t i = 0; i < smTree->n; i++)
{
if (smTree->leaf)
{
write(d, smTree->leaf[i]);
}
if (smTree->parent)
{
write(d, smTree->parent[i]);
}
if (smTree->child)
{
write(d, smTree->child[i]);
}
if (smTree->group)
{
write(d, smTree->group[i]);
}
}
if (smTree->name)
{
for (int32_t i = 0; i < smTree->n; i++)
{
char const* str = smTree->name[i];
for (int32_t j = 0; j < 256; j++)
{
write(d, str[j]);
}
}
}
write(d, smTree->groups);
for (int32_t i = 0; i < smTree->groups; i++)
{
if (smTree->groupSize)
{
write(d, smTree->groupSize[i]);
}
if (smTree->groupOffset)
{
write(d, smTree->groupOffset[i]);
}
}
}
PLUGIN_ASSERT(d == a + getSerializationSize());
}
bool Region::supportsFormat(DataType type, PluginFormat format) const noexcept
{
return (type == DataType::kFLOAT && format == PluginFormat::kLINEAR);
}
int32_t Region::initialize() noexcept
{
return STATUS_SUCCESS;
}
void Region::terminate() noexcept {}
char const* Region::getPluginType() const noexcept
{
return kREGION_PLUGIN_NAME;
}
char const* Region::getPluginVersion() const noexcept
{
return kREGION_PLUGIN_VERSION;
}
size_t Region::getWorkspaceSize(int32_t maxBatchSize) const noexcept
{
return 0;
}
void Region::destroy() noexcept
{
delete this;
}
IPluginV2Ext* Region::clone() const noexcept
{
try
{
RegionParameters params{num, coords, classes, nullptr};
auto plugin = std::make_unique<Region>(params, C, H, W);
plugin->setPluginNamespace(mPluginNamespace.c_str());
plugin->setSoftmaxTree(smTree);
return plugin.release();
}
catch (std::exception const& e)
{
caughtError(e);
}
return nullptr;
}
// Set plugin namespace
void Region::setPluginNamespace(char const* pluginNamespace) noexcept
{
mPluginNamespace = pluginNamespace;
}
char const* Region::getPluginNamespace() const noexcept
{
return mPluginNamespace.c_str();
}
// Return the DataType of the plugin output at the requested index
DataType Region::getOutputDataType(int32_t index, nvinfer1::DataType const* inputTypes, int32_t nbInputs) const noexcept
{
PLUGIN_ASSERT(index == 0);
return DataType::kFLOAT;
}
// Configure the layer with input and output data types.
void Region::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(*inputTypes == DataType::kFLOAT && floatFormat == PluginFormat::kLINEAR);
PLUGIN_ASSERT(nbInputs == 1);
PLUGIN_ASSERT(nbOutputs == 1);
C = inputDims[0].d[0];
H = inputDims[0].d[1];
W = inputDims[0].d[2];
/*
* In the below assertion, 1 stands for the objectness of the bounding box
* We should also
* PLUGIN_ASSERT(coords == 4);
*/
PLUGIN_ASSERT(C == num * (coords + 1 + classes));
}
// Attach the plugin object to an execution context and grant the plugin the access to some context resource.
void Region::attachToContext(
cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) noexcept
{
}
// Detach the plugin object from its execution context.
void Region::detachFromContext() noexcept {}
RegionPluginCreator::RegionPluginCreator()
{
mPluginAttributes.clear();
mPluginAttributes.emplace_back(PluginField("num", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("coords", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("classes", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("smTree", nullptr, PluginFieldType::kINT32, 1));
mFC.nbFields = mPluginAttributes.size();
mFC.fields = mPluginAttributes.data();
}
char const* RegionPluginCreator::getPluginName() const noexcept
{
return kREGION_PLUGIN_NAME;
}
char const* RegionPluginCreator::getPluginVersion() const noexcept
{
return kREGION_PLUGIN_VERSION;
}
PluginFieldCollection const* RegionPluginCreator::getFieldNames() noexcept
{
return &mFC;
}
IPluginV2Ext* RegionPluginCreator::createPlugin(char const* name, PluginFieldCollection const* fc) noexcept
{
try
{
using namespace std::string_view_literals;
PluginField const* fields = fc->fields;
for (int32_t i = 0; i < fc->nbFields; ++i)
{
std::string_view const attrName = fields[i].name;
if (attrName == "num"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kINT32);
params.num = *(static_cast<int32_t const*>(fields[i].data));
}
if (attrName == "coords"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kINT32);
params.coords = *(static_cast<int32_t const*>(fields[i].data));
}
if (attrName == "classes"sv)
{
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kINT32);
params.classes = *(static_cast<int32_t const*>(fields[i].data));
}
if (attrName == "smTree"sv)
{
// TODO not sure if this will work
void* tmpData = const_cast<void*>(fields[i].data);
params.smTree = static_cast<nvinfer1::plugin::softmaxTree*>(tmpData);
}
}
auto obj = std::make_unique<Region>(params);
obj->setPluginNamespace(mNamespace.c_str());
return obj.release();
}
catch (std::exception const& e)
{
caughtError(e);
}
return nullptr;
}
IPluginV2Ext* RegionPluginCreator::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 Region::destroy()
auto obj = std::make_unique<Region>(serialData, serialLength);
obj->setPluginNamespace(mNamespace.c_str());
return obj.release();
}
catch (std::exception const& e)
{
caughtError(e);
}
return nullptr;
}
} // namespace nvinfer1::plugin
+124
View File
@@ -0,0 +1,124 @@
/*
* 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_REGION_PLUGIN_H
#define TRT_REGION_PLUGIN_H
#include "common/kernels/kernel.h"
#include "common/plugin.h"
#include <iostream>
#include <memory>
#include <vector>
namespace nvinfer1
{
namespace plugin
{
class Region : public IPluginV2Ext
{
public:
Region(RegionParameters params);
Region(RegionParameters params, int32_t C, int32_t H, int32_t W);
Region(void const* buffer, size_t length);
~Region() 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;
void setSoftmaxTree(std::shared_ptr<softmaxTree> const& softmaxTree) noexcept
{
smTree = softmaxTree;
}
private:
int32_t num;
int32_t coords;
int32_t classes;
std::shared_ptr<softmaxTree> smTree;
int32_t C, H, W;
bool hasSoftmaxTree;
std::string mPluginNamespace;
};
class RegionPluginCreator : public nvinfer1::pluginInternal::BaseCreator
{
public:
RegionPluginCreator();
~RegionPluginCreator() 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;
private:
PluginFieldCollection mFC;
RegionParameters params;
std::vector<PluginField> mPluginAttributes;
};
} // namespace plugin
} // namespace nvinfer1
#endif // TRT_REGION_PLUGIN_H