From d3435c393fd8dfd4dd11f8bc2f9a135d2d5faaef Mon Sep 17 00:00:00 2001 From: andsonder Date: Sun, 26 Feb 2023 20:56:39 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E4=B8=BA=20Paddle-TRT=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?elementwise=5Fmod=20=E7=AE=97=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fluid/inference/api/analysis_predictor.cc | 1 + .../tensorrt/convert/elementwise_op.cc | 28 ++++++++++++++++++- paddle/fluid/inference/tensorrt/op_teller.cc | 10 +++++-- .../inference/test_trt_convert_elementwise.py | 2 ++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc index e8888940a99ac9..44ef09a33d6619 100644 --- a/paddle/fluid/inference/api/analysis_predictor.cc +++ b/paddle/fluid/inference/api/analysis_predictor.cc @@ -2415,6 +2415,7 @@ USE_TRT_CONVERTER(elementwise_max_tensor); USE_TRT_CONVERTER(elementwise_min_tensor); USE_TRT_CONVERTER(elementwise_pow_tensor); USE_TRT_CONVERTER(elementwise_floordiv_tensor); +USE_TRT_CONVERTER(elementwise_mod_tensor); USE_TRT_CONVERTER(less_than); USE_TRT_CONVERTER(greater_than); USE_TRT_CONVERTER(logical_or); diff --git a/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc b/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc index 0280e418e804b9..24b7935a1aaa6f 100755 --- a/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc +++ b/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc @@ -163,7 +163,25 @@ class ElementwiseTensorOpConverter : public OpConverter { nvinfer1::ElementWiseOperation::kOR); RreplenishLayerAndOutput(layer, "elementwise", {output_name}, test_mode); - } else { + } else if (op_type_ == "mod"){ + audo* div_layer = TRT_ENGINE_ADD_LAYER(engine_, + ElementWise, + *X, + *reshape_y_tensor, + nvinfer1::ElementWiseOperation::kDIV); + auto* mul_layer = TRT_ENGINE_ADD_LAYER(engine_, + ElementWise, + *(div_layer->getOutput(0)), + *reshape_y_tensor, + nvinfer1::ElementWiseOperation::kPROD); + auto* layer = TRT_ENGINE_ADD_LAYER(engine_, + ElementWise, + *X, + *(mul_layer->getOutput(0)), + nvinfer1::ElementWiseOperation::kSUB); + RreplenishLayerAndOutput(layer, "elementwise", {output_name}, test_mode); + } + else { auto op_pair = ops.find(op_type_); PADDLE_ENFORCE_NE( op_pair, @@ -271,6 +289,11 @@ class ElementwiseTensorLessEqualOpConverter public: ElementwiseTensorLessEqualOpConverter() { op_type_ = "less_equal"; } }; +class ElementwiseTensorModOpConverter + : public ElementwiseTensorOpConverter { +public: + ElementwiseTensorModOpConverter() { op_type_ = "mod"; } +}; } // namespace tensorrt } // namespace inference } // namespace paddle @@ -291,6 +314,8 @@ REGISTER_TRT_OP_CONVERTER(elementwise_pow_weight, ElementwiseTensorPowOpConverter); REGISTER_TRT_OP_CONVERTER(elementwise_floordiv_weight, ElementwiseTensorFloorDivOpConverter); +REGISTER_TRT_OP_CONVERTER(elementwise_mod_weight, + ElementwiseTensorModOpConverter); REGISTER_TRT_OP_CONVERTER(elementwise_add_tensor, ElementwiseTensorAddOpConverter); @@ -315,3 +340,4 @@ REGISTER_TRT_OP_CONVERTER(logical_or, ElementwiseTensorLogicalOrOpConverter); REGISTER_TRT_OP_CONVERTER(logical_xor, ElementwiseTensorLogicalXorOpConverter); REGISTER_TRT_OP_CONVERTER(logical_and, ElementwiseTensorLogicalAndOpConverter); REGISTER_TRT_OP_CONVERTER(less_equal, ElementwiseTensorLessEqualOpConverter); +REGISTER_TRT_OP_CONVERTER(elementwise_add_tensor, ElementwiseTensorAddOpConverter); \ No newline at end of file diff --git a/paddle/fluid/inference/tensorrt/op_teller.cc b/paddle/fluid/inference/tensorrt/op_teller.cc index 029665bd111315..916b01acd5b67b 100644 --- a/paddle/fluid/inference/tensorrt/op_teller.cc +++ b/paddle/fluid/inference/tensorrt/op_teller.cc @@ -1418,7 +1418,8 @@ struct SimpleOpTypeSetTeller : public Teller { if (op_type == "elementwise_add" || op_type == "elementwise_mul" || op_type == "elementwise_sub" || op_type == "elementwise_div" || op_type == "elementwise_pow" || op_type == "elementwise_min" || - op_type == "elementwise_max" || op_type == "elementwise_floordiv") { + op_type == "elementwise_max" || op_type == "elementwise_floordiv" || + op_type == "elementwise_mod") { if (desc.Input("X").size() != 1) { VLOG(3) << "The input op's Input(\"X\").size() " "should equal to 1, but received Input(\"X\").size() = " @@ -1453,11 +1454,12 @@ struct SimpleOpTypeSetTeller : public Teller { if (op_type == "elementwise_add" || op_type == "elementwise_mul" || op_type == "elementwise_sub" || op_type == "elementwise_div" || op_type == "elementwise_pow" || op_type == "elementwise_min" || - op_type == "elementwise_max" || op_type == "elementwise_floordiv") { + op_type == "elementwise_max" || op_type == "elementwise_floordiv" || + op_type == "elementwise_mod") { if (x_var_desc->GetDataType() == paddle::framework::proto::VarType_Type::VarType_Type_BOOL) { VLOG(3) << "These operations " - "(elementwise_add/mul/sub/div/pow/min/max/floordiv) do " + "(elementwise_add/mul/sub/div/pow/min/max/floordiv/mod) do " "not support boolean datatype."; return false; } @@ -2606,6 +2608,7 @@ struct SimpleOpTypeSetTeller : public Teller { "elementwise_min", "elementwise_max", "elementwise_floordiv", + "elementwise_mod", "equal", "not_equal", "less_than", @@ -2758,6 +2761,7 @@ struct SimpleOpTypeSetTeller : public Teller { "elementwise_min", "elementwise_max", "elementwise_floordiv", + "elementwise_mod", "equal", "not_equal", "less_than", diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py index 4d4df30acb031c..5c85a0fa2ccae3 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py @@ -191,6 +191,7 @@ def generate_weight(op_type): "elementwise_min", "elementwise_max", "elementwise_floordiv", + "elementwise_mod", ]: for axis in [-1]: self.dims = len(shape) @@ -455,6 +456,7 @@ def generate_input(shape, op_type): "elementwise_min", "elementwise_max", "elementwise_floordiv", + "elementwise_mod", ]: for axis in [0, -1]: self.dims = len(shape) From 85b4a21619a7c3c1c99d0bd80219c88031350a49 Mon Sep 17 00:00:00 2001 From: andsonder Date: Mon, 27 Feb 2023 17:00:18 +0800 Subject: [PATCH 2/9] fix bugs about elementwise mod --- .../inference/tensorrt/convert/elementwise_op.cc | 11 ++++++----- .../fluid/inference/tensorrt/convert/op_converter.h | 4 ++-- .../ir/inference/test_trt_convert_elementwise.py | 4 ++++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc b/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc index 24b7935a1aaa6f..ca4ff811f0617b 100755 --- a/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc +++ b/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc @@ -164,11 +164,11 @@ class ElementwiseTensorOpConverter : public OpConverter { RreplenishLayerAndOutput(layer, "elementwise", {output_name}, test_mode); } else if (op_type_ == "mod"){ - audo* div_layer = TRT_ENGINE_ADD_LAYER(engine_, + auto* div_layer = TRT_ENGINE_ADD_LAYER(engine_, ElementWise, *X, *reshape_y_tensor, - nvinfer1::ElementWiseOperation::kDIV); + nvinfer1::ElementWiseOperation::kFLOOR_DIV); auto* mul_layer = TRT_ENGINE_ADD_LAYER(engine_, ElementWise, *(div_layer->getOutput(0)), @@ -315,7 +315,7 @@ REGISTER_TRT_OP_CONVERTER(elementwise_pow_weight, REGISTER_TRT_OP_CONVERTER(elementwise_floordiv_weight, ElementwiseTensorFloorDivOpConverter); REGISTER_TRT_OP_CONVERTER(elementwise_mod_weight, - ElementwiseTensorModOpConverter); + ElementwiseTensorModOpConverter); REGISTER_TRT_OP_CONVERTER(elementwise_add_tensor, ElementwiseTensorAddOpConverter); @@ -333,11 +333,12 @@ REGISTER_TRT_OP_CONVERTER(elementwise_pow_tensor, ElementwiseTensorPowOpConverter); REGISTER_TRT_OP_CONVERTER(elementwise_floordiv_tensor, ElementwiseTensorFloorDivOpConverter); +REGISTER_TRT_OP_CONVERTER(elementwise_mod_tensor, + ElementwiseTensorModOpConverter); REGISTER_TRT_OP_CONVERTER(less_than, ElementwiseTensorLessThanOpConverter); REGISTER_TRT_OP_CONVERTER(greater_than, ElementwiseTensorGreaterThanOpConverter); REGISTER_TRT_OP_CONVERTER(logical_or, ElementwiseTensorLogicalOrOpConverter); REGISTER_TRT_OP_CONVERTER(logical_xor, ElementwiseTensorLogicalXorOpConverter); REGISTER_TRT_OP_CONVERTER(logical_and, ElementwiseTensorLogicalAndOpConverter); -REGISTER_TRT_OP_CONVERTER(less_equal, ElementwiseTensorLessEqualOpConverter); -REGISTER_TRT_OP_CONVERTER(elementwise_add_tensor, ElementwiseTensorAddOpConverter); \ No newline at end of file +REGISTER_TRT_OP_CONVERTER(less_equal, ElementwiseTensorLessEqualOpConverter); \ No newline at end of file diff --git a/paddle/fluid/inference/tensorrt/convert/op_converter.h b/paddle/fluid/inference/tensorrt/convert/op_converter.h index 2e2a76745ca529..7d6a2772a5679b 100644 --- a/paddle/fluid/inference/tensorrt/convert/op_converter.h +++ b/paddle/fluid/inference/tensorrt/convert/op_converter.h @@ -74,9 +74,9 @@ class OpConverter { } if (op_desc.Type().find("elementwise") != std::string::npos) { static std::unordered_set add_tensor_op_set{ - "add", "mul", "sub", "div", "max", "min", "pow"}; + "add", "mul", "sub", "div", "max", "min", "pow", "mod"}; static std::unordered_set add_weight_op_set{ - "add", "mul", "sub", "div", "max", "min", "pow"}; + "add", "mul", "sub", "div", "max", "min", "pow", "mod"}; PADDLE_ENFORCE_EQ(op_desc.Input("Y").size(), 1UL, platform::errors::InvalidArgument( diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py index 5c85a0fa2ccae3..f8276589e1b43c 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py @@ -58,6 +58,7 @@ def generate_weight(op_type): "elementwise_min", "elementwise_max", "elementwise_floordiv", + "elementwise_mod", ]: for axis in [-1]: self.dims = len(shape) @@ -319,6 +320,7 @@ def generate_weight(op_type): "elementwise_min", "elementwise_max", "elementwise_floordiv", + "elementwise_mod", ]: for axis in [-1 if len(shape) == 1 else 1]: self.dims = len(shape) @@ -653,6 +655,7 @@ def generate_input(shape, op_type): "elementwise_min", "elementwise_max", "elementwise_floordiv", + "elementwise_mod", ]: for axis in axis_list[j][i]: self.shape1 = input1_shape @@ -793,6 +796,7 @@ def generate_weight(op_type): "elementwise_min", "elementwise_max", "elementwise_floordiv", + "elementwise_mod", ]: self.op_type = op_type for axis in [-1 if len(shape) == 1 else 1]: From 317cc387c1585033a8762108d91f0402d7a0c288 Mon Sep 17 00:00:00 2001 From: andsonder Date: Tue, 28 Feb 2023 09:28:11 +0800 Subject: [PATCH 3/9] update --- paddle/fluid/inference/api/analysis_predictor.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc index 44ef09a33d6619..fb18b3101cf141 100644 --- a/paddle/fluid/inference/api/analysis_predictor.cc +++ b/paddle/fluid/inference/api/analysis_predictor.cc @@ -2406,6 +2406,7 @@ USE_TRT_CONVERTER(elementwise_div_weight); USE_TRT_CONVERTER(elementwise_min_weight); USE_TRT_CONVERTER(elementwise_max_weight); USE_TRT_CONVERTER(elementwise_pow_weight); +USE_TRT_CONVERTER(elementwise_mod_weight); USE_TRT_CONVERTER(elementwise_floordiv_weight); USE_TRT_CONVERTER(elementwise_add_tensor); USE_TRT_CONVERTER(elementwise_sub_tensor); From ccc50071c56bba1c9c06942a9cce4b65981855f7 Mon Sep 17 00:00:00 2001 From: andsonder Date: Wed, 1 Mar 2023 17:11:28 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8D=95=E6=B5=8B?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=B2=BE=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unittests/ir/inference/test_trt_convert_elementwise.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py index f8276589e1b43c..67c1f628c05245 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py @@ -170,6 +170,8 @@ def generate_input(shape, op_type): return np.random.randint( low=1, high=10000, size=shape, dtype=np.int32 ) + if op_type == "elementwise_mod": + return np.random.uniform(low=0.1, high=1.0, size=shape).astype(np.float32) else: return np.random.random(shape).astype(np.float32) From 9cb533a74e573cead2b224f585760ce114f8633b Mon Sep 17 00:00:00 2001 From: andsonder Date: Thu, 2 Mar 2023 10:34:26 +0800 Subject: [PATCH 5/9] update code style --- .../tensorrt/convert/elementwise_op.cc | 39 ++++++++++--------- paddle/fluid/inference/tensorrt/op_teller.cc | 7 ++-- .../inference/test_trt_convert_elementwise.py | 4 +- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc b/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc index ca4ff811f0617b..59fafb97833c73 100755 --- a/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc +++ b/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc @@ -164,24 +164,26 @@ class ElementwiseTensorOpConverter : public OpConverter { RreplenishLayerAndOutput(layer, "elementwise", {output_name}, test_mode); } else if (op_type_ == "mod"){ - auto* div_layer = TRT_ENGINE_ADD_LAYER(engine_, - ElementWise, - *X, - *reshape_y_tensor, - nvinfer1::ElementWiseOperation::kFLOOR_DIV); - auto* mul_layer = TRT_ENGINE_ADD_LAYER(engine_, - ElementWise, - *(div_layer->getOutput(0)), - *reshape_y_tensor, - nvinfer1::ElementWiseOperation::kPROD); - auto* layer = TRT_ENGINE_ADD_LAYER(engine_, - ElementWise, - *X, - *(mul_layer->getOutput(0)), - nvinfer1::ElementWiseOperation::kSUB); + auto* div_layer = + TRT_ENGINE_ADD_LAYER(engine_, + ElementWise, + *X, + *reshape_y_tensor, + nvinfer1::ElementWiseOperation::kFLOOR_DIV); + auto* mul_layer = + TRT_ENGINE_ADD_LAYER(engine_, + ElementWise, + *(div_layer->getOutput(0)), + *reshape_y_tensor, + nvinfer1::ElementWiseOperation::kPROD); + auto* layer = + TRT_ENGINE_ADD_LAYER(engine_, + ElementWise, + *X, + *(mul_layer->getOutput(0)), + nvinfer1::ElementWiseOperation::kSUB); RreplenishLayerAndOutput(layer, "elementwise", {output_name}, test_mode); - } - else { + } else { auto op_pair = ops.find(op_type_); PADDLE_ENFORCE_NE( op_pair, @@ -289,8 +291,7 @@ class ElementwiseTensorLessEqualOpConverter public: ElementwiseTensorLessEqualOpConverter() { op_type_ = "less_equal"; } }; -class ElementwiseTensorModOpConverter - : public ElementwiseTensorOpConverter { +class ElementwiseTensorModOpConverter: public ElementwiseTensorOpConverter { public: ElementwiseTensorModOpConverter() { op_type_ = "mod"; } }; diff --git a/paddle/fluid/inference/tensorrt/op_teller.cc b/paddle/fluid/inference/tensorrt/op_teller.cc index 916b01acd5b67b..e79d2f78aad3d5 100644 --- a/paddle/fluid/inference/tensorrt/op_teller.cc +++ b/paddle/fluid/inference/tensorrt/op_teller.cc @@ -1458,9 +1458,10 @@ struct SimpleOpTypeSetTeller : public Teller { op_type == "elementwise_mod") { if (x_var_desc->GetDataType() == paddle::framework::proto::VarType_Type::VarType_Type_BOOL) { - VLOG(3) << "These operations " - "(elementwise_add/mul/sub/div/pow/min/max/floordiv/mod) do " - "not support boolean datatype."; + VLOG(3) + << "These operations " + "(elementwise_add/mul/sub/div/pow/min/max/floordiv/mod) do " + "not support boolean datatype."; return false; } } diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py index 67c1f628c05245..b38d9f57eb769e 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py @@ -171,7 +171,9 @@ def generate_input(shape, op_type): low=1, high=10000, size=shape, dtype=np.int32 ) if op_type == "elementwise_mod": - return np.random.uniform(low=0.1, high=1.0, size=shape).astype(np.float32) + return np.random.uniform(low=0.1, high=1.0, size=shape).astype( + np.float32 + ) else: return np.random.random(shape).astype(np.float32) From 17ebc4774be04459458c63c851326b68d4ee8f49 Mon Sep 17 00:00:00 2001 From: andsonder Date: Thu, 2 Mar 2023 10:55:04 +0800 Subject: [PATCH 6/9] update code style --- .../tensorrt/convert/elementwise_op.cc | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc b/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc index 59fafb97833c73..7ce6d930a83648 100755 --- a/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc +++ b/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc @@ -164,25 +164,25 @@ class ElementwiseTensorOpConverter : public OpConverter { RreplenishLayerAndOutput(layer, "elementwise", {output_name}, test_mode); } else if (op_type_ == "mod"){ - auto* div_layer = - TRT_ENGINE_ADD_LAYER(engine_, - ElementWise, - *X, - *reshape_y_tensor, - nvinfer1::ElementWiseOperation::kFLOOR_DIV); - auto* mul_layer = - TRT_ENGINE_ADD_LAYER(engine_, - ElementWise, - *(div_layer->getOutput(0)), - *reshape_y_tensor, - nvinfer1::ElementWiseOperation::kPROD); - auto* layer = - TRT_ENGINE_ADD_LAYER(engine_, - ElementWise, - *X, - *(mul_layer->getOutput(0)), - nvinfer1::ElementWiseOperation::kSUB); - RreplenishLayerAndOutput(layer, "elementwise", {output_name}, test_mode); + auto* div_layer = + TRT_ENGINE_ADD_LAYER(engine_, + ElementWise, + *X, + *reshape_y_tensor, + nvinfer1::ElementWiseOperation::kFLOOR_DIV); + auto* mul_layer = + TRT_ENGINE_ADD_LAYER(engine_, + ElementWise, + *(div_layer->getOutput(0)), + *reshape_y_tensor, + nvinfer1::ElementWiseOperation::kPROD); + auto* layer = + TRT_ENGINE_ADD_LAYER(engine_, + ElementWise, + *X, + *(mul_layer->getOutput(0)), + nvinfer1::ElementWiseOperation::kSUB); + RreplenishLayerAndOutput(layer, "elementwise", {output_name}, test_mode); } else { auto op_pair = ops.find(op_type_); PADDLE_ENFORCE_NE( @@ -292,8 +292,8 @@ class ElementwiseTensorLessEqualOpConverter ElementwiseTensorLessEqualOpConverter() { op_type_ = "less_equal"; } }; class ElementwiseTensorModOpConverter: public ElementwiseTensorOpConverter { -public: - ElementwiseTensorModOpConverter() { op_type_ = "mod"; } + public: + ElementwiseTensorModOpConverter() { op_type_ = "mod"; } }; } // namespace tensorrt } // namespace inference From 1e884429f5dc508ebf3d4ea1d7a6d8aa8ee76607 Mon Sep 17 00:00:00 2001 From: andsonder Date: Thu, 2 Mar 2023 11:24:05 +0800 Subject: [PATCH 7/9] fix all code style error --- .../inference/tensorrt/convert/elementwise_op.cc | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc b/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc index 7ce6d930a83648..ee309a37e372f4 100755 --- a/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc +++ b/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc @@ -163,7 +163,7 @@ class ElementwiseTensorOpConverter : public OpConverter { nvinfer1::ElementWiseOperation::kOR); RreplenishLayerAndOutput(layer, "elementwise", {output_name}, test_mode); - } else if (op_type_ == "mod"){ + } else if (op_type_ == "mod") { auto* div_layer = TRT_ENGINE_ADD_LAYER(engine_, ElementWise, @@ -176,12 +176,11 @@ class ElementwiseTensorOpConverter : public OpConverter { *(div_layer->getOutput(0)), *reshape_y_tensor, nvinfer1::ElementWiseOperation::kPROD); - auto* layer = - TRT_ENGINE_ADD_LAYER(engine_, - ElementWise, - *X, - *(mul_layer->getOutput(0)), - nvinfer1::ElementWiseOperation::kSUB); + auto* layer = TRT_ENGINE_ADD_LAYER(engine_, + ElementWise, + *X, + *(mul_layer->getOutput(0)), + nvinfer1::ElementWiseOperation::kSUB); RreplenishLayerAndOutput(layer, "elementwise", {output_name}, test_mode); } else { auto op_pair = ops.find(op_type_); @@ -291,7 +290,7 @@ class ElementwiseTensorLessEqualOpConverter public: ElementwiseTensorLessEqualOpConverter() { op_type_ = "less_equal"; } }; -class ElementwiseTensorModOpConverter: public ElementwiseTensorOpConverter { +class ElementwiseTensorModOpConverter : public ElementwiseTensorOpConverter { public: ElementwiseTensorModOpConverter() { op_type_ = "mod"; } }; From 4ed86971f2f992f33a74b4a06c398290c7a548f6 Mon Sep 17 00:00:00 2001 From: andsonder Date: Thu, 2 Mar 2023 18:20:16 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8D=95=E6=B5=8B?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=B2=BE=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tensorrt/convert/elementwise_op.cc | 2 +- .../inference/test_trt_convert_elementwise.py | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc b/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc index ee309a37e372f4..e3df8337cb8cd2 100755 --- a/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc +++ b/paddle/fluid/inference/tensorrt/convert/elementwise_op.cc @@ -341,4 +341,4 @@ REGISTER_TRT_OP_CONVERTER(greater_than, REGISTER_TRT_OP_CONVERTER(logical_or, ElementwiseTensorLogicalOrOpConverter); REGISTER_TRT_OP_CONVERTER(logical_xor, ElementwiseTensorLogicalXorOpConverter); REGISTER_TRT_OP_CONVERTER(logical_and, ElementwiseTensorLogicalAndOpConverter); -REGISTER_TRT_OP_CONVERTER(less_equal, ElementwiseTensorLessEqualOpConverter); \ No newline at end of file +REGISTER_TRT_OP_CONVERTER(less_equal, ElementwiseTensorLessEqualOpConverter); diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py index b38d9f57eb769e..4a87f9ad9b81da 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py @@ -36,6 +36,10 @@ def generate_input(shape, op_type): return np.random.randint( low=1, high=10000, size=shape, dtype=np.int32 ) + elif op_type == "elementwise_mod": + return np.random.uniform(low=0.1, high=1.0, size=shape).astype( + np.float32 + ) else: return np.random.random(shape).astype(np.float32) @@ -44,6 +48,10 @@ def generate_weight(op_type): return np.random.randint( low=1, high=10000, size=[1, 32, 1, 1], dtype=np.int32 ) + elif op_type == "elementwise_mod": + return np.random.uniform( + low=0.1, high=1.0, size=[1, 32, 1, 1] + ).astype(np.float32) else: return np.random.randn(1, 32, 1, 1).astype(np.float32) @@ -183,6 +191,10 @@ def generate_weight(op_type): return np.random.randint( low=1, high=10000, size=[1], dtype=np.int32 ) + elif op_type == "elementwise_mod": + return np.random.uniform(low=0.1, high=1.0, size=[1]).astype( + np.float32 + ) else: return np.random.randn(1).astype(np.float32) @@ -296,6 +308,10 @@ def generate_input(shape, op_type): return np.random.randint( low=1, high=10000, size=shape, dtype=np.int32 ) + elif op_type == "elementwise_mod": + return np.random.uniform(low=0.1, high=1.0, size=shape).astype( + np.float32 + ) else: return np.random.random(shape).astype(np.float32) @@ -305,6 +321,10 @@ def generate_weight(op_type): return np.random.randint( low=1, high=10000, size=[32], dtype=np.int32 ) + elif op_type == "elementwise_mod": + return np.random.uniform(low=0.1, high=1.0, size=[32]).astype( + np.float32 + ) else: return np.random.randn(32).astype(np.float32) @@ -449,6 +469,10 @@ def generate_input(shape, op_type): return np.random.randint( low=1, high=10000, size=shape, dtype=np.int32 ) + elif op_type == "elementwise_mod": + return np.random.uniform(low=0.1, high=1.0, size=shape).astype( + np.float32 + ) else: return np.random.random(shape).astype(np.float32) @@ -613,6 +637,10 @@ def generate_input(shape, op_type): return np.random.randint( low=1, high=10000, size=shape, dtype=np.int32 ) + elif op_type == "elementwise_mod": + return np.random.uniform(low=0.1, high=1.0, size=shape).astype( + np.float32 + ) else: return np.random.random(shape).astype(np.float32) @@ -772,6 +800,10 @@ def generate_input(shape, op_type): return np.random.randint( low=1, high=10000, size=shape, dtype=np.int32 ) + elif op_type == "elementwise_mod": + return np.random.uniform(low=0.1, high=1.0, size=shape).astype( + np.float32 + ) else: return np.random.random(shape).astype(np.float32) @@ -781,6 +813,10 @@ def generate_weight(op_type): return np.random.randint( low=1, high=10000, size=[32], dtype=np.int32 ) + elif op_type == "elementwise_mod": + return np.random.uniform(low=0.1, high=1.0, size=[32]).astype( + np.float32 + ) else: return np.random.rand(32).astype(np.float32) From 08f49923406a4added5d1d60282e57b9ebe8011f Mon Sep 17 00:00:00 2001 From: andsonder Date: Mon, 6 Mar 2023 09:30:11 +0800 Subject: [PATCH 9/9] =?UTF-8?q?update=20=E5=8D=95=E6=B5=8B=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20dynamic=20shape?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unittests/ir/inference/test_trt_convert_elementwise.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py index 4a87f9ad9b81da..ff7cc6e2dd1a21 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_elementwise.py @@ -886,8 +886,8 @@ def generate_dynamic_shape(attrs): # The input.dims[1] must be equal to the weight's length. if self.dims == 1: self.dynamic_shape.min_input_shape = {"input_data": [4]} - self.dynamic_shape.max_input_shape = {"input_data": [256]} - self.dynamic_shape.opt_input_shape = {"input_data": [16]} + self.dynamic_shape.max_input_shape = {"input_data": [64]} + self.dynamic_shape.opt_input_shape = {"input_data": [32]} elif self.dims == 2: self.dynamic_shape.min_input_shape = {"input_data": [1, 32]} self.dynamic_shape.max_input_shape = {"input_data": [4, 32]}