-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Hackathon NO.71] 为 Paddle-TRT 添加 pad3d 算子 #50986
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
5e8a79e
update codes about pad3d
AndSonder 5d6bf44
add codes about Tensor type Padding
AndSonder 443f608
update
AndSonder 0c92e0e
更新单测文件
AndSonder 66a3c42
format code style
AndSonder 933e021
update and to &&'
AndSonder ed83ca0
rewrite codes about pad3d
AndSonder 01b6b4c
add codes about converting paddle pad format to tensorrt pad format
AndSonder f1563b6
pull Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle…
AndSonder 0deb315
fix some errors
AndSonder c246abb
指定trt版本范围
AndSonder 89f0957
修正dims初始化方式
AndSonder 90713f3
fix code style
AndSonder 8a72aff
update test pad values
AndSonder 924ce3f
指定pad3d trt版本
AndSonder 215472c
更新 单测 文件范围
AndSonder dfcd101
更新单测文件
AndSonder eac5f71
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AndSonder 5e50733
update pad3d paddings convert codes
AndSonder ac1918d
update pad3d
AndSonder 40afeb7
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AndSonder dcf4210
add static mode support
AndSonder b448e45
update test file
AndSonder 13a1c82
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AndSonder a3106bd
fix bugs about dynamic mode test codes
AndSonder 46a6218
fix bug and add limite in op_teller
AndSonder 458f538
use a new padding convert method[ITensor* padding with using Slice to…
AndSonder c90c556
fix PADDLE_THROW grammaly error
AndSonder af0d6f3
update test codes
AndSonder 61363ee
添加对于Tensor padding 的 size 判断
AndSonder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ list( | |
concat_op.cc | ||
dropout_op.cc | ||
group_norm_op.cc | ||
pad3d_op.cc | ||
pad_op.cc | ||
split_op.cc | ||
square_op.cc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. | ||
|
||
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 "paddle/fluid/inference/tensorrt/convert/op_converter.h" | ||
|
||
namespace paddle { | ||
namespace inference { | ||
namespace tensorrt { | ||
|
||
/* | ||
* Pad3dOp. | ||
*/ | ||
class Pad3dOpConverter : public OpConverter { | ||
public: | ||
void operator()(const framework::proto::OpDesc& op, | ||
const framework::Scope& scope, | ||
bool test_mode) override { | ||
#if IS_TRT_VERSION_GE(8200) | ||
VLOG(3) << "convert a pad3d op to tensorrt pad3d layer"; | ||
|
||
framework::OpDesc op_desc(op, nullptr); | ||
|
||
// Declare inputs | ||
auto* input = engine_->GetITensor(op_desc.Input("X")[0]); | ||
|
||
nvinfer1::ITensor* paddings; | ||
if (op_desc.HasInput("Paddings") && op_desc.Input("Paddings").size() > 0) { | ||
paddings = engine_->GetITensor(op_desc.Input("Paddings")[0]); | ||
} else { | ||
std::vector<int> paddings_v = | ||
PADDLE_GET_CONST(std::vector<int>, op_desc.GetAttr("paddings")); | ||
paddings = Add1DConstantLayer(paddings_v); | ||
} | ||
|
||
float value{0.F}; | ||
if (op_desc.HasAttr("value")) { | ||
value = PADDLE_GET_CONST(float, op_desc.GetAttr("value")); | ||
} | ||
|
||
std::string padding_mode = "constant"; | ||
if (op_desc.HasAttr("mode")) { | ||
padding_mode = PADDLE_GET_CONST(std::string, op_desc.GetAttr("mode")); | ||
} | ||
|
||
const int input_dim = input->getDimensions().nbDims; | ||
const int pad_size = paddings->getDimensions().d[0]; | ||
PADDLE_ENFORCE_EQ(input_dim * 2 - 4, | ||
pad_size, | ||
phi::errors::InvalidArgument( | ||
"Expected paddings size is %d, but received %d.", | ||
input_dim * 2 - 4, | ||
pad_size)); | ||
// convert paddle pad to tensorrt pad | ||
std::vector<int> shuffle_index{4, 2, 0, 5, 3, 1}; | ||
std::vector<nvinfer1::ITensor*> shuffle_inputs; | ||
for (int i = 0; i < pad_size; i++) { | ||
shuffle_inputs.push_back(GetEleTensorOfShape(paddings, shuffle_index[i])); | ||
} | ||
paddings = Concat(shuffle_inputs); | ||
auto* pre_zeros = Add1DConstantLayer(std::vector<int>(2, 0)); | ||
auto start_slice1 = nvinfer1::Dims{1, { 0 }}; | ||
auto start_slice2 = nvinfer1::Dims{1, { 3 }}; | ||
auto size_slice = nvinfer1::Dims{1, { 3 }}; | ||
auto stride_slice = nvinfer1::Dims{1, { 1 }}; | ||
|
||
auto* pre_pad = | ||
TRT_ENGINE_ADD_LAYER( | ||
engine_, Slice, *paddings, start_slice1, size_slice, stride_slice) | ||
->getOutput(0); | ||
pre_pad = Concat(std::vector<nvinfer1::ITensor*>{pre_zeros, pre_pad}); | ||
auto* post_pad = | ||
TRT_ENGINE_ADD_LAYER( | ||
engine_, Slice, *paddings, start_slice2, size_slice, stride_slice) | ||
->getOutput(0); | ||
post_pad = Concat(std::vector<nvinfer1::ITensor*>{pre_zeros, post_pad}); | ||
|
||
std::vector<int> zeros_v(input_dim, 0); | ||
auto const zeros = Add1DConstantLayer(zeros_v); | ||
|
||
nvinfer1::ITensor* start{}; | ||
nvinfer1::ITensor* size{}; | ||
// elementwise add zeros and pre_pad | ||
start = TRT_ENGINE_ADD_LAYER(engine_, | ||
ElementWise, | ||
*zeros, | ||
*pre_pad, | ||
nvinfer1::ElementWiseOperation::kSUB) | ||
->getOutput(0); | ||
|
||
auto const total_padding = | ||
TRT_ENGINE_ADD_LAYER(engine_, | ||
ElementWise, | ||
*pre_pad, | ||
*post_pad, | ||
nvinfer1::ElementWiseOperation::kSUM) | ||
->getOutput(0); | ||
|
||
auto* input_shape = Shape(input); | ||
size = TRT_ENGINE_ADD_LAYER(engine_, | ||
ElementWise, | ||
*input_shape, | ||
*total_padding, | ||
nvinfer1::ElementWiseOperation::kSUM) | ||
->getOutput(0); | ||
// add slice layer | ||
nvinfer1::Dims stride; | ||
stride.nbDims = input_dim; | ||
std::fill_n(stride.d, input_dim, 1); | ||
auto const& dummy = stride; | ||
auto* slice_layer = | ||
TRT_ENGINE_ADD_LAYER(engine_, | ||
Slice, | ||
*const_cast<nvinfer1::ITensor*>(input), | ||
dummy, | ||
dummy, | ||
stride); | ||
slice_layer->setInput(1, *start); | ||
slice_layer->setInput(2, *size); | ||
if (padding_mode == "constant") { | ||
#if IS_TRT_VERSION_GE(8500) | ||
slice_layer->setMode(nvinfer1::SampleMode::kFILL); | ||
#else | ||
slice_layer->setMode(nvinfer1::SliceMode::kFILL); | ||
#endif | ||
if (value != 0.F) { | ||
nvinfer1::ITensor* fill_value = nullptr; | ||
switch (input->getType()) { | ||
case nvinfer1::DataType::kFLOAT: | ||
case nvinfer1::DataType::kHALF: | ||
case nvinfer1::DataType::kINT8: { | ||
fill_value = Add1DConstantLayer(value); | ||
break; | ||
} | ||
default: { | ||
int value_int = static_cast<int>(value); | ||
fill_value = Add1DConstantLayer(value_int); | ||
break; | ||
} | ||
} | ||
slice_layer->setInput(4, *fill_value); | ||
} | ||
} else if (padding_mode == "reflect") { | ||
#if IS_TRT_VERSION_GE(8500) | ||
slice_layer->setMode(nvinfer1::SampleMode::kREFLECT); | ||
#else | ||
slice_layer->setMode(nvinfer1::SliceMode::kREFLECT); | ||
#endif | ||
} else if (padding_mode == "replicate") { | ||
#if IS_TRT_VERSION_GE(8500) | ||
slice_layer->setMode(nvinfer1::SampleMode::kCLAMP); | ||
#else | ||
slice_layer->setMode(nvinfer1::SliceMode::kCLAMP); | ||
#endif | ||
} else { | ||
PADDLE_THROW(paddle::platform::errors::Fatal("Unsupported mode: %s", | ||
padding_mode)); | ||
} | ||
|
||
auto output_name = op_desc.Output("Out")[0]; | ||
RreplenishLayerAndOutput(slice_layer, "pad3d", {output_name}, test_mode); | ||
|
||
#else | ||
VLOG(3) << "pad3d is not supported when TensorRT < 8.2"; | ||
#endif | ||
} | ||
}; | ||
|
||
} // namespace tensorrt | ||
} // namespace inference | ||
} // namespace paddle | ||
|
||
REGISTER_TRT_OP_CONVERTER(pad3d, Pad3dOpConverter); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里应该是或 ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改