From 13992de74ffb2eade5ab63471f43f22e343ab098 Mon Sep 17 00:00:00 2001 From: Sanbu <96160062+sanbuphy@users.noreply.github.com> Date: Tue, 10 Jan 2023 10:49:29 +0800 Subject: [PATCH] Add reduce_min prod trt converter (#49615) --- .../fluid/inference/api/analysis_predictor.cc | 2 + .../inference/tensorrt/convert/reduce_op.cc | 16 ++++- paddle/fluid/inference/tensorrt/op_teller.cc | 3 +- .../ir/inference/test_trt_convert_reduce.py | 61 +++++++++++-------- 4 files changed, 54 insertions(+), 28 deletions(-) diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc index 81b6f9206e4..2c1e6ba95dd 100644 --- a/paddle/fluid/inference/api/analysis_predictor.cc +++ b/paddle/fluid/inference/api/analysis_predictor.cc @@ -2350,7 +2350,9 @@ USE_TRT_CONVERTER(reshape2); USE_TRT_CONVERTER(gather_nd); USE_TRT_CONVERTER(reduce_mean); USE_TRT_CONVERTER(reduce_max); +USE_TRT_CONVERTER(reduce_min); USE_TRT_CONVERTER(reduce_sum); +USE_TRT_CONVERTER(reduce_prod); USE_TRT_CONVERTER(tile); USE_TRT_CONVERTER(conv3d); USE_TRT_CONVERTER(conv3d_transpose); diff --git a/paddle/fluid/inference/tensorrt/convert/reduce_op.cc b/paddle/fluid/inference/tensorrt/convert/reduce_op.cc index e6eed80e905..637b0a662f7 100644 --- a/paddle/fluid/inference/tensorrt/convert/reduce_op.cc +++ b/paddle/fluid/inference/tensorrt/convert/reduce_op.cc @@ -43,7 +43,6 @@ class ReduceOpConverter : public OpConverter { VLOG(4) << "convert a paddle " << op_type << " op to tensorrt reduce layer"; framework::OpDesc op_desc(op, nullptr); auto reduce_type = ops_.find(op_type); - auto* x = engine_->GetITensor(op_desc.Input("X").front()); nvinfer1::Dims input_shape = x->getDimensions(); int input_dims = input_shape.nbDims; @@ -104,6 +103,8 @@ const std::unordered_map> {"reduce_mean", {nvinfer1::ReduceOperation::kAVG}}, {"reduce_sum", {nvinfer1::ReduceOperation::kSUM}}, {"reduce_max", {nvinfer1::ReduceOperation::kMAX}}, + {"reduce_min", {nvinfer1::ReduceOperation::kMIN}}, + {"reduce_prod", {nvinfer1::ReduceOperation::kPROD}}, }; class ReduceSumOpConverter : public ReduceOpConverter { @@ -120,6 +121,17 @@ class ReduceMaxOpConverter : public ReduceOpConverter { public: ReduceMaxOpConverter() { op_type = "reduce_max"; } }; + +class ReduceMinOpConverter : public ReduceOpConverter { + public: + ReduceMinOpConverter() { op_type = "reduce_min"; } +}; + +class ReduceProdOpConverter : public ReduceOpConverter { + public: + ReduceProdOpConverter() { op_type = "reduce_prod"; } +}; + } // namespace tensorrt } // namespace inference } // namespace paddle @@ -127,3 +139,5 @@ class ReduceMaxOpConverter : public ReduceOpConverter { REGISTER_TRT_OP_CONVERTER(reduce_sum, ReduceSumOpConverter); REGISTER_TRT_OP_CONVERTER(reduce_mean, ReduceMeanOpConverter); REGISTER_TRT_OP_CONVERTER(reduce_max, ReduceMaxOpConverter); +REGISTER_TRT_OP_CONVERTER(reduce_min, ReduceMinOpConverter); +REGISTER_TRT_OP_CONVERTER(reduce_prod, ReduceProdOpConverter); diff --git a/paddle/fluid/inference/tensorrt/op_teller.cc b/paddle/fluid/inference/tensorrt/op_teller.cc index b8fa355c66f..04ed688c27c 100644 --- a/paddle/fluid/inference/tensorrt/op_teller.cc +++ b/paddle/fluid/inference/tensorrt/op_teller.cc @@ -2088,7 +2088,8 @@ struct SimpleOpTypeSetTeller : public Teller { } if (op_type == "reduce_sum" || op_type == "reduce_mean" || - op_type == "reduce_max") { + op_type == "reduce_max" || op_type == "reduce_min" || + op_type == "reduce_prod") { if (!desc.HasAttr("dim", /*with_attr_var=*/false)) { VLOG(3) << "Skip to convert into TRT while found Attribute('dim') is " "Variable type in " diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_reduce.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_reduce.py index ba94e2fcd49..67580b6d7c3 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_reduce.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_reduce.py @@ -68,10 +68,12 @@ class TrtConvertReduceTest(TrtLayerAutoScanTest): for out_dtype in [-1, 2, 5]: for op_type in [ "reduce_max", + "reduce_min", "reduce_mean", "reduce_sum", + "reduce_prod", ]: - dics = [ + dics1 = [ { "keep_dim": keep_dim, "dim": dim, @@ -81,36 +83,43 @@ class TrtConvertReduceTest(TrtLayerAutoScanTest): }, {}, ] - - ops_config = [ + dics2 = [ { - "op_type": op_type, - "op_inputs": {"X": ["input_data"]}, - "op_outputs": { - "Out": ["reduce_output_data"] - }, - "op_attrs": dics[0], - } + "out_dtype": out_dtype, + "in_dtype": out_dtype, + }, + {}, ] - ops = self.generate_op_config(ops_config) - - program_config = ProgramConfig( - ops=ops, - weights={}, - inputs={ - "input_data": TensorConfig( - data_gen=partial( - generate_input1, out_dtype, dics + for dics in [dics1, dics2]: + ops_config = [ + { + "op_type": op_type, + "op_inputs": {"X": ["input_data"]}, + "op_outputs": { + "Out": ["reduce_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, out_dtype, dics + ) ) - ) - }, - outputs=["reduce_output_data"], - ) + }, + outputs=["reduce_output_data"], + ) - if not self.is_program_valid(program_config): - continue + if not self.is_program_valid(program_config): + continue - yield program_config + yield program_config def sample_predictor_configs( self, program_config -- GitLab