From 1f1e77715c2a656fdb32183b754e31fd7f0513b4 Mon Sep 17 00:00:00 2001 From: zhangjun Date: Sun, 30 Oct 2022 09:25:25 +0000 Subject: [PATCH 1/6] add bilinear OutSize --- .../tensorrt/convert/bilinear_interp_v2_op.cc | 56 ++++++--- paddle/fluid/inference/tensorrt/op_teller.cc | 8 -- .../test_trt_convert_bilinear_interp_v2.py | 117 ++++++++++++++++++ 3 files changed, 157 insertions(+), 24 deletions(-) diff --git a/paddle/fluid/inference/tensorrt/convert/bilinear_interp_v2_op.cc b/paddle/fluid/inference/tensorrt/convert/bilinear_interp_v2_op.cc index fca5424875d2b..75553fb5a9c22 100644 --- a/paddle/fluid/inference/tensorrt/convert/bilinear_interp_v2_op.cc +++ b/paddle/fluid/inference/tensorrt/convert/bilinear_interp_v2_op.cc @@ -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")); @@ -88,39 +89,62 @@ class BilinearInterpolateV2OpConverter : public OpConverter { bool with_dynamic = engine_->with_dynamic_shape(); int h_axis = (data_layout == phi::DataLayout::kNCHW) + with_dynamic; int w_axis = (data_layout == phi::DataLayout::kNCHW) + 1 + with_dynamic; + int c_axis = + ((data_layout == phi::DataLayout::kNCHW) ? 0 : 2) + with_dynamic; if (scale_w > 0. && scale_h > 0.) { + std::cout << scale_w << ", " << scale_h << std::endl; out_h = static_cast(in_dim.d[h_axis] * scale_h); out_w = static_cast(in_dim.d[w_axis] * scale_w); } + // Priority: Input(OutSize) > attr(out_h/out_w) > attr(scale) + nvinfer1::ITensor* outsize_tensor = nullptr; + if (resize_inputs.find("OutSize") != resize_inputs.end()) { + if (op_desc.Input("OutSize").size() >= 1) { + outsize_tensor = engine_->GetITensor(op_desc.Input("OutSize")[0]); + // out_h = outsize_tensor->getDimensions().d[0]; + // out_w = outsize_tensor->getDimensions().d[1]; + } + } + if (out_h > 0 && out_w > 0) { scale_h = static_cast(out_h) / static_cast(in_dim.d[h_axis]); scale_w = static_cast(out_w) / static_cast(in_dim.d[w_axis]); } - - std::vector scales; + std::cout << scale_w << ", " << scale_h << std::endl; + std::cout << out_w << ", " << out_h << std::endl; 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); - scales.push_back(scale_w); - } else if (data_layout == phi::DataLayout::kNHWC) { - scales.push_back(scale_h); - scales.push_back(scale_w); - scales.push_back(1.f); + std::vector outsize_itensors; + auto* input_shape = Shape(input); + outsize_itensors.push_back(GetEleTensorOfShape(Shape(input), 0)); + if (data_layout == phi::DataLayout::kNCHW) { + outsize_itensors.push_back(GetEleTensorOfShape(input_shape, c_axis)); + outsize_itensors.push_back(GetEleTensorOfShape(outsize_tensor, 0)); + outsize_itensors.push_back(GetEleTensorOfShape(outsize_tensor, 1)); + } else if (data_layout == phi::DataLayout::kNHWC) { + outsize_itensors.push_back(GetEleTensorOfShape(outsize_tensor, 0)); + outsize_itensors.push_back(GetEleTensorOfShape(outsize_tensor, 1)); + outsize_itensors.push_back(GetEleTensorOfShape(input_shape, c_axis)); + } + layer->setInput(1, *Concat(outsize_itensors)); } else { - PADDLE_THROW(platform::errors::InvalidArgument( - "Data layout must be NCHW or NHWC.")); + std::vector scales; + if (data_layout == phi::DataLayout::kNCHW) { + scales.push_back(1.f); + scales.push_back(scale_h); + scales.push_back(scale_w); + } else if (data_layout == phi::DataLayout::kNHWC) { + scales.push_back(scale_h); + scales.push_back(scale_w); + scales.push_back(1.f); + } + layer->setScales(scales.data(), scales.size()); } - layer->setScales(scales.data(), scales.size()); RreplenishLayerAndOutput( layer, "bilinear_interp_v2", {output_name}, test_mode); } diff --git a/paddle/fluid/inference/tensorrt/op_teller.cc b/paddle/fluid/inference/tensorrt/op_teller.cc index 3e6f5779c6fa8..48af3e1155cdb 100644 --- a/paddle/fluid/inference/tensorrt/op_teller.cc +++ b/paddle/fluid/inference/tensorrt/op_teller.cc @@ -801,14 +801,6 @@ 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 " - << op_type; - return false; - } - } - auto data_layout = phi::StringToDataLayout( PADDLE_GET_CONST(std::string, desc.GetAttr("data_layout"))); if (data_layout != phi::DataLayout::kNCHW && diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_bilinear_interp_v2.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_bilinear_interp_v2.py index 5015e7e36be06..28efb7c1da6ad 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_bilinear_interp_v2.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_bilinear_interp_v2.py @@ -138,5 +138,122 @@ def test(self): self.run_test() +class TrtConvertBilinearInterpV2Test1(TrtLayerAutoScanTest): + def is_program_valid(self, program_config: ProgramConfig) -> bool: + inputs = program_config.inputs + weights = program_config.weights + attrs = [ + program_config.ops[i].attrs for i in range(len(program_config.ops)) + ] + + return True + + def sample_program_configs(self): + def generate_input1(attrs: List[Dict[str, Any]]): + return np.ones([1, 3, 64, 64]).astype(np.float32) + + def generate_input2(attrs: List[Dict[str, Any]]): + return np.array([attrs[0]["out_h"], attrs[0]["out_w"]]).astype( + "int32" + ) + + 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]: + scale = [scale_y, scale_x] + for out_h in [32, 64, 128, 192]: + for out_w in [32, 64]: + dics = [ + { + "data_layout": data_layout, + "interp_method": "bilinear", + "align_corners": False, + "align_mode": 0, + "scale": scale, + "out_h": out_h, + "out_w": out_w, + } + ] + + ops_config = [ + { + "op_type": "bilinear_interp_v2", + "op_inputs": { + "X": ["input_data"], + "OutSize": ["out_size"], + }, + "op_outputs": { + "Out": [ + "bilinear_interp_v2_output_data" + ] + }, + "op_attrs": dics[0], + } + ] + ops = self.generate_op_config(ops_config) + + program_config = ProgramConfig( + ops=ops, + weights={ + "out_size": TensorConfig( + data_gen=partial(generate_input2, dics) + ) + }, + inputs={ + "input_data": TensorConfig( + data_gen=partial(generate_input1, dics) + ) + }, + outputs=["bilinear_interp_v2_output_data"], + ) + + yield program_config + + def sample_predictor_configs( + self, program_config + ) -> (paddle_infer.Config, List[int], float): + def generate_dynamic_shape(attrs): + self.dynamic_shape.min_input_shape = {"input_data": [1, 3, 64, 64]} + self.dynamic_shape.max_input_shape = {"input_data": [4, 3, 64, 64]} + self.dynamic_shape.opt_input_shape = {"input_data": [1, 3, 64, 64]} + + def clear_dynamic_shape(): + self.dynamic_shape.min_input_shape = {} + self.dynamic_shape.max_input_shape = {} + self.dynamic_shape.opt_input_shape = {} + + def generate_trt_nodes_num(attrs, dynamic_shape): + return 1, 2 + + attrs = [ + program_config.ops[i].attrs for i in range(len(program_config.ops)) + ] + + # for static_shape + clear_dynamic_shape() + self.trt_param.precision = paddle_infer.PrecisionType.Float32 + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, False + ), 1e-5 + self.trt_param.precision = paddle_infer.PrecisionType.Half + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, False + ), 1e-2 + + # # for dynamic_shape + generate_dynamic_shape(attrs) + self.trt_param.precision = paddle_infer.PrecisionType.Float32 + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, True + ), 1e-5 + self.trt_param.precision = paddle_infer.PrecisionType.Half + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, True + ), 1e-2 + + def test(self): + self.run_test() + + if __name__ == "__main__": unittest.main() From fe19069e60647768d578bed5240459fa5a2636c7 Mon Sep 17 00:00:00 2001 From: zhangjun Date: Sun, 30 Oct 2022 17:31:38 +0000 Subject: [PATCH 2/6] update --- .../tensorrt/convert/bilinear_interp_v2_op.cc | 33 +++++++++---------- paddle/fluid/inference/tensorrt/op_teller.cc | 5 +++ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/paddle/fluid/inference/tensorrt/convert/bilinear_interp_v2_op.cc b/paddle/fluid/inference/tensorrt/convert/bilinear_interp_v2_op.cc index 75553fb5a9c22..167924bc6d67c 100644 --- a/paddle/fluid/inference/tensorrt/convert/bilinear_interp_v2_op.cc +++ b/paddle/fluid/inference/tensorrt/convert/bilinear_interp_v2_op.cc @@ -89,22 +89,18 @@ class BilinearInterpolateV2OpConverter : public OpConverter { bool with_dynamic = engine_->with_dynamic_shape(); int h_axis = (data_layout == phi::DataLayout::kNCHW) + with_dynamic; int w_axis = (data_layout == phi::DataLayout::kNCHW) + 1 + with_dynamic; - int c_axis = - ((data_layout == phi::DataLayout::kNCHW) ? 0 : 2) + with_dynamic; if (scale_w > 0. && scale_h > 0.) { - std::cout << scale_w << ", " << scale_h << std::endl; out_h = static_cast(in_dim.d[h_axis] * scale_h); out_w = static_cast(in_dim.d[w_axis] * scale_w); } // Priority: Input(OutSize) > attr(out_h/out_w) > attr(scale) nvinfer1::ITensor* outsize_tensor = nullptr; - if (resize_inputs.find("OutSize") != resize_inputs.end()) { + 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]); - // out_h = outsize_tensor->getDimensions().d[0]; - // out_w = outsize_tensor->getDimensions().d[1]; } } @@ -114,23 +110,24 @@ class BilinearInterpolateV2OpConverter : public OpConverter { scale_w = static_cast(out_w) / static_cast(in_dim.d[w_axis]); } - std::cout << scale_w << ", " << scale_h << std::endl; - std::cout << out_w << ", " << out_h << std::endl; if (engine_->with_dynamic_shape()) { std::vector outsize_itensors; auto* input_shape = Shape(input); - outsize_itensors.push_back(GetEleTensorOfShape(Shape(input), 0)); - if (data_layout == phi::DataLayout::kNCHW) { - outsize_itensors.push_back(GetEleTensorOfShape(input_shape, c_axis)); - outsize_itensors.push_back(GetEleTensorOfShape(outsize_tensor, 0)); - outsize_itensors.push_back(GetEleTensorOfShape(outsize_tensor, 1)); - } else if (data_layout == phi::DataLayout::kNHWC) { - outsize_itensors.push_back(GetEleTensorOfShape(outsize_tensor, 0)); - outsize_itensors.push_back(GetEleTensorOfShape(outsize_tensor, 1)); - outsize_itensors.push_back(GetEleTensorOfShape(input_shape, c_axis)); + std::vector nc_mask{1, 1, 0, 0}; + auto* nc_mask_tensor = Add1DConstantLayer(nc_mask); + auto* out_mask_tensor = + Sum(Prod(input_shape, nc_mask_tensor), + Concat(std::vector{Add1DConstantLayer({0, 0}), + outsize_tensor})); + // nchw order + auto* outsize_full = + TRT_ENGINE_ADD_LAYER(engine_, Shuffle, *(out_mask_tensor)); + if (data_layout == phi::DataLayout::kNHWC) { + nvinfer1::Permutation transpose{0, 2, 3, 1}; + outsize_full->setSecondTranspose(transpose); } - layer->setInput(1, *Concat(outsize_itensors)); + layer->setInput(1, *(outsize_full->getOutput(0))); } else { std::vector scales; if (data_layout == phi::DataLayout::kNCHW) { diff --git a/paddle/fluid/inference/tensorrt/op_teller.cc b/paddle/fluid/inference/tensorrt/op_teller.cc index 48af3e1155cdb..d463338f4f37f 100644 --- a/paddle/fluid/inference/tensorrt/op_teller.cc +++ b/paddle/fluid/inference/tensorrt/op_teller.cc @@ -793,6 +793,11 @@ struct SimpleOpTypeSetTeller : public Teller { auto resize_inputs = desc.Inputs(); if (resize_inputs.find("SizeTensor") != resize_inputs.end()) { + if (!with_dynamic_shape) { + VLOG(3) << "Static shape don't support the SizeTensor for op_type " + << op_type; + return false; + } if (desc.Input("SizeTensor").size() >= 1) { VLOG(3) << "The Paddle-TRT doesn't support the SizeTensor for op_type " From 4b8d22dc468001e28a5a4628f923c478407d4844 Mon Sep 17 00:00:00 2001 From: zhangjun Date: Mon, 31 Oct 2022 08:17:27 +0000 Subject: [PATCH 3/6] update --- .../tensorrt/convert/bilinear_interp_v2_op.cc | 54 ++++++++++--------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/paddle/fluid/inference/tensorrt/convert/bilinear_interp_v2_op.cc b/paddle/fluid/inference/tensorrt/convert/bilinear_interp_v2_op.cc index 167924bc6d67c..bcd8462c99557 100644 --- a/paddle/fluid/inference/tensorrt/convert/bilinear_interp_v2_op.cc +++ b/paddle/fluid/inference/tensorrt/convert/bilinear_interp_v2_op.cc @@ -111,34 +111,38 @@ class BilinearInterpolateV2OpConverter : public OpConverter { static_cast(out_w) / static_cast(in_dim.d[w_axis]); } + std::vector scales; if (engine_->with_dynamic_shape()) { - std::vector outsize_itensors; - auto* input_shape = Shape(input); - std::vector nc_mask{1, 1, 0, 0}; - auto* nc_mask_tensor = Add1DConstantLayer(nc_mask); - auto* out_mask_tensor = - Sum(Prod(input_shape, nc_mask_tensor), - Concat(std::vector{Add1DConstantLayer({0, 0}), - outsize_tensor})); - // nchw order - auto* outsize_full = - TRT_ENGINE_ADD_LAYER(engine_, Shuffle, *(out_mask_tensor)); - if (data_layout == phi::DataLayout::kNHWC) { - nvinfer1::Permutation transpose{0, 2, 3, 1}; - outsize_full->setSecondTranspose(transpose); + scales.push_back(1.f); + } + if (data_layout == phi::DataLayout::kNCHW) { + scales.push_back(1.f); + scales.push_back(scale_h); + scales.push_back(scale_w); + } else if (data_layout == phi::DataLayout::kNHWC) { + 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 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()); } - layer->setInput(1, *(outsize_full->getOutput(0))); } else { - std::vector scales; - if (data_layout == phi::DataLayout::kNCHW) { - scales.push_back(1.f); - scales.push_back(scale_h); - scales.push_back(scale_w); - } else if (data_layout == phi::DataLayout::kNHWC) { - scales.push_back(scale_h); - scales.push_back(scale_w); - scales.push_back(1.f); - } layer->setScales(scales.data(), scales.size()); } From 3fa454262b64d444077a7c9cfe306d245c998ea3 Mon Sep 17 00:00:00 2001 From: zhangjun Date: Mon, 31 Oct 2022 08:23:13 +0000 Subject: [PATCH 4/6] update --- paddle/fluid/inference/tensorrt/op_teller.cc | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/inference/tensorrt/op_teller.cc b/paddle/fluid/inference/tensorrt/op_teller.cc index d463338f4f37f..1a210c87371f9 100644 --- a/paddle/fluid/inference/tensorrt/op_teller.cc +++ b/paddle/fluid/inference/tensorrt/op_teller.cc @@ -793,11 +793,6 @@ struct SimpleOpTypeSetTeller : public Teller { auto resize_inputs = desc.Inputs(); if (resize_inputs.find("SizeTensor") != resize_inputs.end()) { - if (!with_dynamic_shape) { - VLOG(3) << "Static shape don't support the SizeTensor for op_type " - << op_type; - return false; - } if (desc.Input("SizeTensor").size() >= 1) { VLOG(3) << "The Paddle-TRT doesn't support the SizeTensor for op_type " @@ -806,6 +801,14 @@ struct SimpleOpTypeSetTeller : public Teller { } } + if (resize_inputs.find("OutSize") != resize_inputs.end()) { + if (!with_dynamic_shape) { + VLOG(3) << "Static shape don't support the OutSize for op_type " + << op_type; + return false; + } + } + auto data_layout = phi::StringToDataLayout( PADDLE_GET_CONST(std::string, desc.GetAttr("data_layout"))); if (data_layout != phi::DataLayout::kNCHW && From c4c619475fc54b20e5e4293eef9b83dfac391094 Mon Sep 17 00:00:00 2001 From: zhangjun Date: Mon, 31 Oct 2022 08:32:49 +0000 Subject: [PATCH 5/6] update --- .../test_trt_convert_bilinear_interp_v2.py | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_bilinear_interp_v2.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_bilinear_interp_v2.py index 28efb7c1da6ad..e68ec53a2768d 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_bilinear_interp_v2.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_bilinear_interp_v2.py @@ -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]: @@ -140,8 +140,6 @@ def test(self): class TrtConvertBilinearInterpV2Test1(TrtLayerAutoScanTest): def is_program_valid(self, program_config: ProgramConfig) -> bool: - inputs = program_config.inputs - weights = program_config.weights attrs = [ program_config.ops[i].attrs for i in range(len(program_config.ops)) ] @@ -158,8 +156,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]: @@ -194,15 +192,14 @@ def generate_input2(attrs: List[Dict[str, Any]]): program_config = ProgramConfig( ops=ops, - weights={ - "out_size": TensorConfig( - data_gen=partial(generate_input2, dics) - ) - }, + weights={}, inputs={ "input_data": TensorConfig( data_gen=partial(generate_input1, dics) - ) + ), + "out_size": TensorConfig( + data_gen=partial(generate_input2, dics) + ), }, outputs=["bilinear_interp_v2_output_data"], ) @@ -213,9 +210,18 @@ def sample_predictor_configs( self, program_config ) -> (paddle_infer.Config, List[int], float): def generate_dynamic_shape(attrs): - self.dynamic_shape.min_input_shape = {"input_data": [1, 3, 64, 64]} - self.dynamic_shape.max_input_shape = {"input_data": [4, 3, 64, 64]} - self.dynamic_shape.opt_input_shape = {"input_data": [1, 3, 64, 64]} + self.dynamic_shape.min_input_shape = { + "input_data": [1, 3, 64, 64], + "out_size": [2], + } + self.dynamic_shape.max_input_shape = { + "input_data": [4, 3, 64, 64], + "out_size": [2], + } + self.dynamic_shape.opt_input_shape = { + "input_data": [1, 3, 64, 64], + "out_size": [2], + } def clear_dynamic_shape(): self.dynamic_shape.min_input_shape = {} @@ -240,7 +246,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape): attrs, False ), 1e-2 - # # for dynamic_shape + # for dynamic_shape generate_dynamic_shape(attrs) self.trt_param.precision = paddle_infer.PrecisionType.Float32 yield self.create_inference_config(), generate_trt_nodes_num( From c559d1ebfa21c3e18e6c1a33f0d243ee3189a95e Mon Sep 17 00:00:00 2001 From: zhangjun Date: Mon, 31 Oct 2022 08:33:38 +0000 Subject: [PATCH 6/6] remove OutSize unittest --- .../test_trt_convert_bilinear_interp_v2.py | 123 ------------------ 1 file changed, 123 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_bilinear_interp_v2.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_bilinear_interp_v2.py index e68ec53a2768d..3854d0e86bf6e 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_bilinear_interp_v2.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_bilinear_interp_v2.py @@ -138,128 +138,5 @@ def test(self): self.run_test() -class TrtConvertBilinearInterpV2Test1(TrtLayerAutoScanTest): - def is_program_valid(self, program_config: ProgramConfig) -> bool: - attrs = [ - program_config.ops[i].attrs for i in range(len(program_config.ops)) - ] - - return True - - def sample_program_configs(self): - def generate_input1(attrs: List[Dict[str, Any]]): - return np.ones([1, 3, 64, 64]).astype(np.float32) - - def generate_input2(attrs: List[Dict[str, Any]]): - return np.array([attrs[0]["out_h"], attrs[0]["out_w"]]).astype( - "int32" - ) - - for data_layout in ["NCHW", "NHWC"]: - 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]: - dics = [ - { - "data_layout": data_layout, - "interp_method": "bilinear", - "align_corners": False, - "align_mode": 0, - "scale": scale, - "out_h": out_h, - "out_w": out_w, - } - ] - - ops_config = [ - { - "op_type": "bilinear_interp_v2", - "op_inputs": { - "X": ["input_data"], - "OutSize": ["out_size"], - }, - "op_outputs": { - "Out": [ - "bilinear_interp_v2_output_data" - ] - }, - "op_attrs": dics[0], - } - ] - ops = self.generate_op_config(ops_config) - - program_config = ProgramConfig( - ops=ops, - weights={}, - inputs={ - "input_data": TensorConfig( - data_gen=partial(generate_input1, dics) - ), - "out_size": TensorConfig( - data_gen=partial(generate_input2, dics) - ), - }, - outputs=["bilinear_interp_v2_output_data"], - ) - - yield program_config - - def sample_predictor_configs( - self, program_config - ) -> (paddle_infer.Config, List[int], float): - def generate_dynamic_shape(attrs): - self.dynamic_shape.min_input_shape = { - "input_data": [1, 3, 64, 64], - "out_size": [2], - } - self.dynamic_shape.max_input_shape = { - "input_data": [4, 3, 64, 64], - "out_size": [2], - } - self.dynamic_shape.opt_input_shape = { - "input_data": [1, 3, 64, 64], - "out_size": [2], - } - - def clear_dynamic_shape(): - self.dynamic_shape.min_input_shape = {} - self.dynamic_shape.max_input_shape = {} - self.dynamic_shape.opt_input_shape = {} - - def generate_trt_nodes_num(attrs, dynamic_shape): - return 1, 2 - - attrs = [ - program_config.ops[i].attrs for i in range(len(program_config.ops)) - ] - - # for static_shape - clear_dynamic_shape() - self.trt_param.precision = paddle_infer.PrecisionType.Float32 - yield self.create_inference_config(), generate_trt_nodes_num( - attrs, False - ), 1e-5 - self.trt_param.precision = paddle_infer.PrecisionType.Half - yield self.create_inference_config(), generate_trt_nodes_num( - attrs, False - ), 1e-2 - - # for dynamic_shape - generate_dynamic_shape(attrs) - self.trt_param.precision = paddle_infer.PrecisionType.Float32 - yield self.create_inference_config(), generate_trt_nodes_num( - attrs, True - ), 1e-5 - self.trt_param.precision = paddle_infer.PrecisionType.Half - yield self.create_inference_config(), generate_trt_nodes_num( - attrs, True - ), 1e-2 - - def test(self): - self.run_test() - - if __name__ == "__main__": unittest.main()