From 83d1fb385816b1321ed00749d4f6b9eaa8c80179 Mon Sep 17 00:00:00 2001 From: xiaoxiaohehe001 Date: Tue, 7 Sep 2021 11:18:08 +0000 Subject: [PATCH 1/5] add_roi_align --- .../inference/test_trt_convert_roi_align.py | 173 ++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py new file mode 100644 index 00000000000000..f2b5b73766e685 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py @@ -0,0 +1,173 @@ +# Copyright (c) 2021 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. + +from trt_layer_auto_scan_test import TrtLayerAutoScanTest, SkipReasons +from program_config import TensorConfig, ProgramConfig +import numpy as np +import paddle.inference as paddle_infer +from functools import partial +from typing import Optional, List, Callable, Dict, Any, Set + + +class TrtConvertRoiAlignTest(TrtLayerAutoScanTest): + def is_program_valid(self, program_config: ProgramConfig) -> bool: + # TODO: This is just the example to remove the wrong attrs. + inputs = program_config.inputs + weights = program_config.weights + outputs = program_config.outputs + + attrs = [ + program_config.ops[i].attrs + for i in range(len(program_config.ops)) + ] + # number between sections and outputs restriction. + return True + + def sample_program_configs(self): + def generate_input1(attrs: List[Dict[str, Any]], batch): + return np.ones([batch, 256, 32, 32]).astype(np.float32) + + def generate_input2(attrs: List[Dict[str, Any]], batch): + return np.random.random([3, 4]).astype(np.float32) + + def generate_input3(attrs: List[Dict[str, Any]], batch): + return np.random.random([batch]).astype(np.int32) + + for num_input in [0, 1]: + for batch in [1, 2, 4]: + for spatial_scale in [0.5, 0.6]: + for pooled_height in [7, 1]: + for pooled_width in [7, 1]: + for sampling_ratio in [-1, 4, 8]: + for aligned in [True, False]: + self.num_input = num_input + if num_input == 1: + batch = 1 + dics = [{ + "spatial_scale": spatial_scale, + "pooled_height": pooled_height, + "pooled_width": pooled_width, + "sampling_ratio": sampling_ratio, + "aligned": aligned + }, {}] + dics_input = [{ + "X": ["roi_align_input"], + "ROIs": ["ROIs"], + "RoisNum": ["RoisNum"] + }, { + "X": ["roi_align_input"], + "ROIs": ["ROIs"] + }] + program_input = [{ + "roi_align_input": TensorConfig( + data_gen=partial(generate_input1, + dics, batch)), + "ROIs": TensorConfig(data_gen=partial( + generate_input2, dics, batch)), + "RoisNum": TensorConfig( + data_gen=partial(generate_input3, + dics, batch)) + }, { + "roi_align_input": TensorConfig( + data_gen=partial(generate_input1, + dics, batch)), + "ROIs": TensorConfig( + data_gen=partial(generate_input2, + dics, batch), + lod=[[9988, 3]]) + }] + ops_config = [{ + "op_type": "roi_align", + "op_inputs": dics_input[num_input], + "op_outputs": { + "Out": ["roi_align_out"] + }, + "op_attrs": dics[0] + }] + ops = self.generate_op_config(ops_config) + program_config = ProgramConfig( + ops=ops, + weights={}, + inputs=program_input[num_input], + outputs=["roi_align_out"]) + + 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 = { + "roi_align_input": [1, 256, 32, 32], + "ROIs": [3, 4] + } + self.dynamic_shape.max_input_shape = { + "roi_align_input": [1, 256, 64, 64], + "ROIs": [3, 4] + } + self.dynamic_shape.opt_input_shape = { + "roi_align_input": [1, 256, 64, 64], + "ROIs": [3, 4] + } + + 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): + if self.num_input == 0: + if dynamic_shape == True: + return 0, 5 + else: + return 0, 5 + elif self.num_input == 1: + if dynamic_shape == True: + return 1, 3 + else: + return 0, 4 + + 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 add_skip_trt_case(self): + # TODO(wilber): This is just the example to illustrate the skip usage. + pass + + def test(self): + self.add_skip_trt_case() + self.run_test() + + +if __name__ == "__main__": + unittest.main() From 2a45b4e87d8a750aae542b409d09bac919daf853 Mon Sep 17 00:00:00 2001 From: xiaoxiaohehe001 Date: Thu, 9 Sep 2021 02:35:53 +0000 Subject: [PATCH 2/5] add_roi_align --- paddle/fluid/inference/tensorrt/op_teller.cc | 30 +++++++++ .../inference/test_trt_convert_roi_align.py | 67 ++++++++++++++----- 2 files changed, 79 insertions(+), 18 deletions(-) diff --git a/paddle/fluid/inference/tensorrt/op_teller.cc b/paddle/fluid/inference/tensorrt/op_teller.cc index 72f20790f35242..b071932053f2ba 100644 --- a/paddle/fluid/inference/tensorrt/op_teller.cc +++ b/paddle/fluid/inference/tensorrt/op_teller.cc @@ -661,6 +661,36 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8, "the roi_align will change the batch size."; return false; } + std::vector attrs{"pooled_height", "pooled_width", + "spatial_scale", "sampling_ratio"}; + for (auto const attr : attrs) { + if (!desc.HasAttr(attr)) return false; + } + + const auto pooled_height = + BOOST_GET_CONST(int, desc.GetAttr("pooled_height")); + if (pooled_height <= 0) return false; + + const auto pooled_width = + BOOST_GET_CONST(int, desc.GetAttr("pooled_width")); + if (pooled_width <= 0) return false; + + const auto spatial_scale = + BOOST_GET_CONST(float, desc.GetAttr("spatial_scale")); + if (spatial_scale <= 0.f) return false; + + const auto sampling_ratio = + BOOST_GET_CONST(int, desc.GetAttr("sampling_ratio")); + const auto aligned = BOOST_GET_CONST(bool, desc.GetAttr("aligned")); + + if (sampling_ratio == -1 && aligned == true) return false; + + auto roi_align_inputs = desc.Inputs(); + if (roi_align_inputs.find("RoisNum") != roi_align_inputs.end()) { + if (desc.Input("RoisNum").size() >= 1) { + return false; + } + } } if (op_type == "shuffle_channel") { diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py index f2b5b73766e685..66ace2a97a48cb 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py @@ -31,7 +31,6 @@ def is_program_valid(self, program_config: ProgramConfig) -> bool: program_config.ops[i].attrs for i in range(len(program_config.ops)) ] - # number between sections and outputs restriction. return True def sample_program_configs(self): @@ -85,7 +84,7 @@ def generate_input3(attrs: List[Dict[str, Any]], batch): "ROIs": TensorConfig( data_gen=partial(generate_input2, dics, batch), - lod=[[9988, 3]]) + lod=[[32, 3]]) }] ops_config = [{ "op_type": "roi_align", @@ -107,18 +106,35 @@ def generate_input3(attrs: List[Dict[str, Any]], batch): def sample_predictor_configs( self, program_config) -> (paddle_infer.Config, List[int], float): def generate_dynamic_shape(attrs): - self.dynamic_shape.min_input_shape = { - "roi_align_input": [1, 256, 32, 32], - "ROIs": [3, 4] - } - self.dynamic_shape.max_input_shape = { - "roi_align_input": [1, 256, 64, 64], - "ROIs": [3, 4] - } - self.dynamic_shape.opt_input_shape = { - "roi_align_input": [1, 256, 64, 64], - "ROIs": [3, 4] - } + if self.num_input == 0: + self.dynamic_shape.min_input_shape = { + "roi_align_input": [1, 256, 32, 32], + "ROIs": [3, 4], + "RoisNum": [1] + } + self.dynamic_shape.max_input_shape = { + "roi_align_input": [1, 256, 64, 64], + "ROIs": [3, 4], + "RoisNum": [1] + } + self.dynamic_shape.opt_input_shape = { + "roi_align_input": [1, 256, 64, 64], + "ROIs": [3, 4], + "RoisNum": [1] + } + elif self.num_input == 1: + self.dynamic_shape.min_input_shape = { + "roi_align_input": [1, 256, 32, 32], + "ROIs": [3, 4] + } + self.dynamic_shape.max_input_shape = { + "roi_align_input": [1, 256, 64, 64], + "ROIs": [3, 4] + } + self.dynamic_shape.opt_input_shape = { + "roi_align_input": [1, 256, 64, 64], + "ROIs": [3, 4] + } def clear_dynamic_shape(): self.dynamic_shape.min_input_shape = {} @@ -129,8 +145,6 @@ def generate_trt_nodes_num(attrs, dynamic_shape): if self.num_input == 0: if dynamic_shape == True: return 0, 5 - else: - return 0, 5 elif self.num_input == 1: if dynamic_shape == True: return 1, 3 @@ -161,8 +175,25 @@ def generate_trt_nodes_num(attrs, dynamic_shape): True), 1e-2 def add_skip_trt_case(self): - # TODO(wilber): This is just the example to illustrate the skip usage. - pass + def teller1(program_config, predictor_config): + if len(program_config.inputs) == 3: + return True + return False + + self.add_skip_case( + teller1, SkipReasons.TRT_NOT_SUPPORT, + "INPUT RoisNum NOT SUPPORT: we need to add support in the future") + + def teller2(program_config, predictor_config): + if (program_config.ops[0].attrs['sampling_ratio'] == -1 and + program_config.ops[0].attrs['aligned'] == True): + return True + return False + + self.add_skip_case( + teller2, SkipReasons.TRT_NOT_SUPPORT, + "SAMPLING_RATIO EQUAL TO - 1 WHEN ALIGNED IS TRUE IS NOT SUPPORT: we need to add support in the future" + ) def test(self): self.add_skip_trt_case() From 3ab851d710330aa9f28c310c3d45264a4557ef5a Mon Sep 17 00:00:00 2001 From: xiaoxiaohehe001 Date: Thu, 9 Sep 2021 03:22:53 +0000 Subject: [PATCH 3/5] add_roi_align_teller --- .../tests/unittests/ir/inference/test_trt_convert_roi_align.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py index 66ace2a97a48cb..3e58d0c47d214a 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py @@ -22,7 +22,6 @@ class TrtConvertRoiAlignTest(TrtLayerAutoScanTest): def is_program_valid(self, program_config: ProgramConfig) -> bool: - # TODO: This is just the example to remove the wrong attrs. inputs = program_config.inputs weights = program_config.weights outputs = program_config.outputs From beaecfcf6dfc03f0feecfabadce39403bca4bf64 Mon Sep 17 00:00:00 2001 From: xiaoxiaohehe001 Date: Thu, 9 Sep 2021 11:43:07 +0000 Subject: [PATCH 4/5] add_roi_align --- .../unittests/ir/inference/test_trt_convert_roi_align.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py index 3e58d0c47d214a..a8ebce2e5e1624 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py @@ -22,14 +22,6 @@ class TrtConvertRoiAlignTest(TrtLayerAutoScanTest): def is_program_valid(self, program_config: ProgramConfig) -> bool: - inputs = program_config.inputs - weights = program_config.weights - outputs = program_config.outputs - - attrs = [ - program_config.ops[i].attrs - for i in range(len(program_config.ops)) - ] return True def sample_program_configs(self): From fb83c48200f95e1e76a28676d2e98d431415f28b Mon Sep 17 00:00:00 2001 From: xiaoxiaohehe001 Date: Fri, 10 Sep 2021 04:43:03 +0000 Subject: [PATCH 5/5] add-roi_align --- .../ir/inference/test_trt_convert_roi_align.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py index a8ebce2e5e1624..265065c7b357eb 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_roi_align.py @@ -154,7 +154,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape): 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 + attrs, False), 1e-5 # for dynamic_shape generate_dynamic_shape(attrs) @@ -163,7 +163,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape): True), 1e-5 self.trt_param.precision = paddle_infer.PrecisionType.Half yield self.create_inference_config(), generate_trt_nodes_num(attrs, - True), 1e-2 + True), 1e-5 def add_skip_trt_case(self): def teller1(program_config, predictor_config): @@ -171,9 +171,8 @@ def teller1(program_config, predictor_config): return True return False - self.add_skip_case( - teller1, SkipReasons.TRT_NOT_SUPPORT, - "INPUT RoisNum NOT SUPPORT: we need to add support in the future") + self.add_skip_case(teller1, SkipReasons.TRT_NOT_SUPPORT, + "INPUT RoisNum NOT SUPPORT") def teller2(program_config, predictor_config): if (program_config.ops[0].attrs['sampling_ratio'] == -1 and @@ -183,8 +182,7 @@ def teller2(program_config, predictor_config): self.add_skip_case( teller2, SkipReasons.TRT_NOT_SUPPORT, - "SAMPLING_RATIO EQUAL TO - 1 WHEN ALIGNED IS TRUE IS NOT SUPPORT: we need to add support in the future" - ) + "SAMPLING_RATIO EQUAL TO - 1 WHEN ALIGNED IS TRUE IS NOT SUPPORT") def test(self): self.add_skip_trt_case()