Skip to content
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

[inference][trt] bilinear support OutSize input #47495

Merged
merged 6 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions paddle/fluid/inference/tensorrt/convert/bilinear_interp_v2_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class BilinearInterpolateV2OpConverter : public OpConverter {

auto resize_inputs = op_desc.Inputs();
auto input_names = op_desc.Input("X");

auto out_h = PADDLE_GET_CONST(int, op_desc.GetAttr("out_h"));
auto out_w = PADDLE_GET_CONST(int, op_desc.GetAttr("out_w"));

Expand Down Expand Up @@ -94,6 +95,15 @@ class BilinearInterpolateV2OpConverter : public OpConverter {
out_w = static_cast<int>(in_dim.d[w_axis] * scale_w);
}

// Priority: Input(OutSize) > attr(out_h/out_w) > attr(scale)
nvinfer1::ITensor* outsize_tensor = nullptr;
if (engine_->with_dynamic_shape() &&
resize_inputs.find("OutSize") != resize_inputs.end()) {
if (op_desc.Input("OutSize").size() >= 1) {
outsize_tensor = engine_->GetITensor(op_desc.Input("OutSize")[0]);
}
}

if (out_h > 0 && out_w > 0) {
scale_h =
static_cast<float>(out_h) / static_cast<float>(in_dim.d[h_axis]);
Expand All @@ -102,11 +112,9 @@ class BilinearInterpolateV2OpConverter : public OpConverter {
}

std::vector<float> scales;

if (engine_->with_dynamic_shape()) {
scales.push_back(1.f);
}

if (data_layout == phi::DataLayout::kNCHW) {
scales.push_back(1.f);
scales.push_back(scale_h);
Expand All @@ -115,12 +123,29 @@ class BilinearInterpolateV2OpConverter : public OpConverter {
scales.push_back(scale_h);
scales.push_back(scale_w);
scales.push_back(1.f);
}

if (engine_->with_dynamic_shape()) {
if (outsize_tensor != nullptr) {
std::vector<nvinfer1::ITensor*> outsize_itensors;
auto* input_shape = Shape(input);
outsize_itensors.push_back(GetEleTensorOfShape(input_shape, 0));

if (data_layout == phi::DataLayout::kNCHW) {
outsize_itensors.push_back(GetEleTensorOfShape(input_shape, 1));
outsize_itensors.push_back(outsize_tensor);
} else if (data_layout == phi::DataLayout::kNHWC) {
outsize_itensors.push_back(outsize_tensor);
outsize_itensors.push_back(GetEleTensorOfShape(input_shape, 3));
}
layer->setInput(1, *Concat(outsize_itensors));
} else {
layer->setScales(scales.data(), scales.size());
}
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
"Data layout must be NCHW or NHWC."));
layer->setScales(scales.data(), scales.size());
}

layer->setScales(scales.data(), scales.size());
RreplenishLayerAndOutput(
layer, "bilinear_interp_v2", {output_name}, test_mode);
}
Expand Down
4 changes: 2 additions & 2 deletions paddle/fluid/inference/tensorrt/op_teller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,8 @@ struct SimpleOpTypeSetTeller : public Teller {
}

if (resize_inputs.find("OutSize") != resize_inputs.end()) {
if (desc.Input("OutSize").size() >= 1) {
VLOG(3) << "The Paddle-TRT doesn't support the OutSize for op_type "
if (!with_dynamic_shape) {
VLOG(3) << "Static shape don't support the OutSize for op_type "
<< op_type;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def generate_input2(attrs: List[Dict[str, Any]]):
)

for data_layout in ["NCHW", "NHWC"]:
for scale_y in [2.0, -1.0, 0.0]:
for scale_x in [2.0, -1.0, 0.0]:
for scale_y in [2.0, 1.0]:
for scale_x in [2.0, 1.0]:
scale = [scale_y, scale_x]
for out_h in [32, 64, 128, 192]:
for out_w in [32, 64]:
Expand Down