chore: import upstream snapshot with attribution
Docker Image CI / build-ubuntu2004 (push) Has been cancelled
Docker Image CI / build-ubuntu2004 (push) Has been cancelled
This commit is contained in:
@@ -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(
|
||||
resizeNearestPlugin.cpp
|
||||
resizeNearestPlugin.h
|
||||
)
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
# ResizeNearest Plugin [DEPRECATED]
|
||||
|
||||
**This plugin is deprecated since TensorRT 10.12 and will be removed in a future release. `IResizeLayer` can be used as an alternative as appropriate to replace its functionality.**
|
||||
|
||||
**Table Of Contents**
|
||||
- [Description](#description)
|
||||
* [Structure](#structure)
|
||||
- [Parameters](#parameters)
|
||||
- [Additional resources](#additional-resources)
|
||||
- [License](#license)
|
||||
- [Changelog](#changelog)
|
||||
- [Known issues](#known-issues)
|
||||
|
||||
## Description
|
||||
|
||||
The `ResizeNearest` plugin performs nearest neighbor interpolation among feature map. It is used in sample MaskRCNN.
|
||||
|
||||
|
||||
### Structure
|
||||
|
||||
This plugin supports the NCHW format. It takes one input tensor `feature_map`
|
||||
|
||||
`feature_map` can be arbitrary feature map from convolution layer of shape `[N, C, H, W]`
|
||||
|
||||
`Resizenearest` generates the resized feature map according to scale factor. For example, if input feature is of `[N, C, H, W]`and`scale=2.0`, then the output feature will be of `[N, C, 2.0 * H, 2.0 * W]`
|
||||
|
||||
## Parameters
|
||||
|
||||
This plugin has the plugin creator class `ResizeNearestPluginCreator` and the plugin class `ResizeNearest`.
|
||||
|
||||
The following parameters were used to create `ResizeNearest` instance:
|
||||
|
||||
| Type | Parameter | Description
|
||||
|--------------------|--------------------------------|--------------------------------------------------------
|
||||
|`float` |`scale` | Scale factor for resize
|
||||
|
||||
|
||||
## Additional resources
|
||||
|
||||
The following resources provide a deeper understanding of the `ResizeNearest` plugin:
|
||||
|
||||
- [MaskRCNN](https://github.com/matterport/Mask_RCNN)
|
||||
|
||||
|
||||
## 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.
|
||||
|
||||
June 2019
|
||||
This is the first release of this `README.md` file.
|
||||
|
||||
|
||||
## Known issues
|
||||
|
||||
There are no known issues in this plugin.
|
||||
@@ -0,0 +1,34 @@
|
||||
#
|
||||
# SPDX-FileCopyrightText: Copyright (c) 2022-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.
|
||||
#
|
||||
---
|
||||
name: ResizeNearest_TRT
|
||||
interface: "IPluginV2Ext"
|
||||
versions:
|
||||
"1":
|
||||
attributes:
|
||||
- scale
|
||||
attribute_types:
|
||||
scale: float32
|
||||
attribute_length:
|
||||
scale: 1
|
||||
attribute_options:
|
||||
scale:
|
||||
min: "0"
|
||||
max: "=pinf"
|
||||
attributes_required:
|
||||
- scale
|
||||
...
|
||||
@@ -0,0 +1,278 @@
|
||||
/*
|
||||
* 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 "resizeNearestPlugin.h"
|
||||
#include "common/plugin.h"
|
||||
#include <algorithm>
|
||||
#include <cuda_runtime_api.h>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
|
||||
#define DEBUG 0
|
||||
|
||||
using namespace nvinfer1;
|
||||
using namespace plugin;
|
||||
using nvinfer1::plugin::ResizeNearest;
|
||||
using nvinfer1::plugin::ResizeNearestPluginCreator;
|
||||
|
||||
namespace
|
||||
{
|
||||
char const* const kRESIZE_PLUGIN_VERSION{"1"};
|
||||
char const* const kRESIZE_PLUGIN_NAME{"ResizeNearest_TRT"};
|
||||
} // namespace
|
||||
|
||||
ResizeNearestPluginCreator::ResizeNearestPluginCreator()
|
||||
{
|
||||
mPluginAttributes.clear();
|
||||
mPluginAttributes.emplace_back(PluginField("scale", nullptr, PluginFieldType::kFLOAT32, 1));
|
||||
|
||||
mFC.nbFields = mPluginAttributes.size();
|
||||
mFC.fields = mPluginAttributes.data();
|
||||
}
|
||||
|
||||
char const* ResizeNearestPluginCreator::getPluginName() const noexcept
|
||||
{
|
||||
return kRESIZE_PLUGIN_NAME;
|
||||
}
|
||||
|
||||
char const* ResizeNearestPluginCreator::getPluginVersion() const noexcept
|
||||
{
|
||||
return kRESIZE_PLUGIN_VERSION;
|
||||
}
|
||||
|
||||
PluginFieldCollection const* ResizeNearestPluginCreator::getFieldNames() noexcept
|
||||
{
|
||||
return &mFC;
|
||||
}
|
||||
|
||||
IPluginV2Ext* ResizeNearestPluginCreator::createPlugin(char const* name, PluginFieldCollection const* fc) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
using namespace std::string_view_literals;
|
||||
plugin::validateRequiredAttributesExist({"scale"}, fc);
|
||||
PluginField const* fields = fc->fields;
|
||||
for (int32_t i = 0; i < fc->nbFields; ++i)
|
||||
{
|
||||
std::string_view const attrName = fields[i].name;
|
||||
if (attrName == "scale"sv)
|
||||
{
|
||||
PLUGIN_VALIDATE(fields[i].type == PluginFieldType::kFLOAT32);
|
||||
mScale = *(static_cast<float const*>(fields[i].data));
|
||||
}
|
||||
}
|
||||
return new ResizeNearest(mScale);
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
IPluginV2Ext* ResizeNearestPluginCreator::deserializePlugin(char const* name, void const* data, size_t length) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
return new ResizeNearest(data, length);
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ResizeNearest::ResizeNearest(float scale)
|
||||
: mScale(scale)
|
||||
{
|
||||
PLUGIN_VALIDATE(mScale > 0);
|
||||
}
|
||||
|
||||
int32_t ResizeNearest::getNbOutputs() const noexcept
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
Dims ResizeNearest::getOutputDimensions(int32_t index, Dims const* inputDims, int32_t nbInputs) noexcept
|
||||
{
|
||||
PLUGIN_ASSERT(nbInputs == 1);
|
||||
nvinfer1::Dims const& input = inputDims[0];
|
||||
PLUGIN_ASSERT(index == 0);
|
||||
nvinfer1::Dims output{};
|
||||
output.nbDims = input.nbDims;
|
||||
for (int32_t d = 0; d < input.nbDims; ++d)
|
||||
{
|
||||
if (d == input.nbDims - 2 || d == input.nbDims - 1)
|
||||
{
|
||||
output.d[d] = int32_t(input.d[d] * mScale);
|
||||
}
|
||||
else
|
||||
{
|
||||
output.d[d] = input.d[d];
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
int32_t ResizeNearest::initialize() noexcept
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ResizeNearest::terminate() noexcept {}
|
||||
|
||||
void ResizeNearest::destroy() noexcept
|
||||
{
|
||||
delete this;
|
||||
}
|
||||
|
||||
size_t ResizeNearest::getWorkspaceSize(int32_t) const noexcept
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t ResizeNearest::getSerializationSize() const noexcept
|
||||
{
|
||||
// scale, dimensions: 3 * 2
|
||||
return sizeof(float) + sizeof(int32_t) * 3 * 2;
|
||||
}
|
||||
|
||||
void ResizeNearest::serialize(void* buffer) const noexcept
|
||||
{
|
||||
char *d = reinterpret_cast<char*>(buffer), *a = d;
|
||||
write(d, mScale);
|
||||
write(d, mInputDims.d[0]);
|
||||
write(d, mInputDims.d[1]);
|
||||
write(d, mInputDims.d[2]);
|
||||
write(d, mOutputDims.d[0]);
|
||||
write(d, mOutputDims.d[1]);
|
||||
write(d, mOutputDims.d[2]);
|
||||
PLUGIN_ASSERT(d == a + getSerializationSize());
|
||||
}
|
||||
|
||||
ResizeNearest::ResizeNearest(void const* data, size_t length)
|
||||
{
|
||||
deserialize(static_cast<int8_t const*>(data), length);
|
||||
}
|
||||
|
||||
void ResizeNearest::deserialize(int8_t const* data, size_t length)
|
||||
{
|
||||
auto const* d{data};
|
||||
mScale = read<float>(d);
|
||||
mInputDims = Dims3();
|
||||
mInputDims.d[0] = read<int32_t>(d);
|
||||
mInputDims.d[1] = read<int32_t>(d);
|
||||
mInputDims.d[2] = read<int32_t>(d);
|
||||
mOutputDims = Dims3();
|
||||
mOutputDims.d[0] = read<int32_t>(d);
|
||||
mOutputDims.d[1] = read<int32_t>(d);
|
||||
mOutputDims.d[2] = read<int32_t>(d);
|
||||
PLUGIN_VALIDATE(d == data + length);
|
||||
}
|
||||
|
||||
char const* ResizeNearest::getPluginType() const noexcept
|
||||
{
|
||||
return "ResizeNearest_TRT";
|
||||
}
|
||||
|
||||
char const* ResizeNearest::getPluginVersion() const noexcept
|
||||
{
|
||||
return "1";
|
||||
}
|
||||
|
||||
IPluginV2Ext* ResizeNearest::clone() const noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
auto plugin = std::make_unique<ResizeNearest>(*this);
|
||||
plugin->setPluginNamespace(mNameSpace.c_str());
|
||||
return plugin.release();
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void ResizeNearest::setPluginNamespace(char const* libNamespace) noexcept
|
||||
{
|
||||
mNameSpace = libNamespace;
|
||||
}
|
||||
|
||||
char const* ResizeNearest::getPluginNamespace() const noexcept
|
||||
{
|
||||
return mNameSpace.c_str();
|
||||
}
|
||||
|
||||
bool ResizeNearest::supportsFormat(DataType type, PluginFormat format) const noexcept
|
||||
{
|
||||
return (type == DataType::kFLOAT && format == PluginFormat::kLINEAR);
|
||||
}
|
||||
|
||||
int32_t ResizeNearest::enqueue(
|
||||
int32_t batch_size, void const* const* inputs, void* const* outputs, void* workspace, cudaStream_t stream) noexcept
|
||||
{
|
||||
|
||||
int32_t nchan = mOutputDims.d[0];
|
||||
float scale = mScale;
|
||||
int2 osize = {dimToInt32(mOutputDims.d[2]), dimToInt32(mOutputDims.d[1])};
|
||||
int32_t istride = mInputDims.d[2];
|
||||
int32_t ostride = mOutputDims.d[2];
|
||||
int32_t ibatchstride = mInputDims.d[1] * istride;
|
||||
int32_t obatchstride = mOutputDims.d[1] * ostride;
|
||||
dim3 block(32, 16);
|
||||
dim3 grid((osize.x - 1) / block.x + 1, (osize.y - 1) / block.y + 1, std::min(batch_size * nchan, 65535));
|
||||
|
||||
resizeNearest(grid, block, stream, batch_size * nchan, scale, osize, static_cast<float const*>(inputs[0]), istride,
|
||||
ibatchstride, static_cast<float*>(outputs[0]), ostride, obatchstride);
|
||||
|
||||
return cudaGetLastError() != cudaSuccess;
|
||||
}
|
||||
|
||||
// Return the DataType of the plugin output at the requested index
|
||||
DataType ResizeNearest::getOutputDataType(
|
||||
int32_t index, nvinfer1::DataType const* inputTypes, int32_t nbInputs) const noexcept
|
||||
{
|
||||
// Only 1 input and 1 output from the plugin layer
|
||||
PLUGIN_ASSERT(index == 0);
|
||||
|
||||
// Only DataType::kFLOAT is acceptable by the plugin layer
|
||||
return DataType::kFLOAT;
|
||||
}
|
||||
|
||||
// Configure the layer with input and output data types.
|
||||
void ResizeNearest::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(nbInputs == 1);
|
||||
mInputDims = inputDims[0];
|
||||
|
||||
PLUGIN_ASSERT(nbOutputs == 1);
|
||||
mOutputDims = outputDims[0];
|
||||
}
|
||||
|
||||
// Attach the plugin object to an execution context and grant the plugin the access to some context resource.
|
||||
void ResizeNearest::attachToContext(
|
||||
cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) noexcept
|
||||
{
|
||||
}
|
||||
|
||||
// Detach the plugin object from its execution context.
|
||||
void ResizeNearest::detachFromContext() noexcept {}
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* 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_RESIZENEAREST_PLUGIN_H
|
||||
#define TRT_RESIZENEAREST_PLUGIN_H
|
||||
|
||||
#include <cuda_runtime_api.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "NvInfer.h"
|
||||
#include "NvInferPlugin.h"
|
||||
#include "common/kernels/maskRCNNKernels.h"
|
||||
|
||||
namespace nvinfer1
|
||||
{
|
||||
namespace plugin
|
||||
{
|
||||
class ResizeNearest : public IPluginV2Ext
|
||||
{
|
||||
public:
|
||||
ResizeNearest(float scale);
|
||||
|
||||
ResizeNearest(void const* data, size_t length);
|
||||
|
||||
~ResizeNearest() 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;
|
||||
|
||||
void destroy() noexcept override;
|
||||
|
||||
size_t getWorkspaceSize(int32_t) const noexcept override;
|
||||
|
||||
int32_t enqueue(int32_t batch_size, 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;
|
||||
|
||||
IPluginV2Ext* clone() const noexcept override;
|
||||
|
||||
void setPluginNamespace(char const* libNamespace) 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(int8_t const* data, size_t length);
|
||||
float mScale{};
|
||||
Dims mInputDims{};
|
||||
Dims mOutputDims{};
|
||||
std::string mNameSpace;
|
||||
};
|
||||
|
||||
class ResizeNearestPluginCreator : public nvinfer1::pluginInternal::BaseCreator
|
||||
{
|
||||
public:
|
||||
ResizeNearestPluginCreator();
|
||||
|
||||
~ResizeNearestPluginCreator() 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* data, size_t length) noexcept override;
|
||||
|
||||
private:
|
||||
PluginFieldCollection mFC;
|
||||
float mScale;
|
||||
std::vector<PluginField> mPluginAttributes;
|
||||
};
|
||||
} // namespace plugin
|
||||
} // namespace nvinfer1
|
||||
#endif // TRT_RESIZENEAREST_PLUGIN_H
|
||||
Reference in New Issue
Block a user