From 8f52402124f6e02816728ad3ccf18cd62298081d Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Fri, 10 Mar 2023 08:37:42 +0000 Subject: [PATCH 1/4] fix add axis is not default -1 --- python/paddle/nn/functional/conv.py | 61 +++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/python/paddle/nn/functional/conv.py b/python/paddle/nn/functional/conv.py index 23eff58805ee95..4a72fae4799e5f 100644 --- a/python/paddle/nn/functional/conv.py +++ b/python/paddle/nn/functional/conv.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import paddle from paddle import _C_ops, _legacy_C_ops, get_flags, in_dynamic_mode from paddle.device import ( get_all_custom_device_type, @@ -247,12 +248,32 @@ def _conv_nd( ) if bias is not None: out = helper.create_variable_for_type_inference(dtype) - helper.append_op( - type='elementwise_add', - inputs={'X': [pre_bias], 'Y': [bias]}, - outputs={'Out': [out]}, - attrs={'axis': channel_dim, 'use_mkldnn': use_mkldnn}, - ) + x_shape = list(pre_bias.shape) + y_shape = list(bias.shape) + if channel_dim == -1 or len(x_shape) == len(y_shape): + helper.append_op( + type='elementwise_add', + inputs={'X': [pre_bias], 'Y': [bias]}, + outputs={'Out': [out]}, + attrs={'axis': -1, 'use_mkldnn': use_mkldnn}, + ) + else: + if len(x_shape) > len(y_shape): + padding = len(x_shape) - len(y_shape) - channel_dim + bias = paddle.reshape( + bias, [1] * channel_dim + y_shape + [1] * padding + ) + else: + padding = len(y_shape) - len(x_shape) - channel_dim + pre_bias = paddle.reshape( + pre_bias, [1] * channel_dim + y_shape + [1] * padding + ) + helper.append_op( + type='elementwise_add', + inputs={'X': [pre_bias], 'Y': [bias]}, + outputs={'Out': [out]}, + attrs={'axis': -1, 'use_mkldnn': use_mkldnn}, + ) else: out = pre_bias return out @@ -1335,7 +1356,33 @@ def conv2d_transpose( ) if bias is not None: - out = _add_with_axis(pre_bias, bias, axis=channel_dim) + out = helper.create_variable_for_type_inference(x.dtype) + x_shape = list(pre_bias.shape) + y_shape = list(bias.shape) + if channel_dim == -1 or len(x_shape) == len(y_shape): + helper.append_op( + type='elementwise_add', + inputs={'X': [pre_bias], 'Y': [bias]}, + outputs={'Out': [out]}, + attrs={'axis': -1, 'use_mkldnn': False}, + ) + else: + if len(x_shape) > len(y_shape): + padding = len(x_shape) - len(y_shape) - channel_dim + bias = paddle.reshape( + bias, [1] * channel_dim + y_shape + [1] * padding + ) + else: + padding = len(y_shape) - len(x_shape) - channel_dim + pre_bias = paddle.reshape( + pre_bias, [1] * channel_dim + y_shape + [1] * padding + ) + helper.append_op( + type='elementwise_add', + inputs={'X': [pre_bias], 'Y': [bias]}, + outputs={'Out': [out]}, + attrs={'axis': -1, 'use_mkldnn': False}, + ) else: out = pre_bias From b5a09af2420b716259670eb3009b05391eaf91ea Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Thu, 16 Mar 2023 13:59:26 +0000 Subject: [PATCH 2/4] polish conv logic --- python/paddle/nn/functional/conv.py | 35 +++++++++++++---------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/python/paddle/nn/functional/conv.py b/python/paddle/nn/functional/conv.py index 4a72fae4799e5f..691e2d1b0642e8 100644 --- a/python/paddle/nn/functional/conv.py +++ b/python/paddle/nn/functional/conv.py @@ -258,16 +258,14 @@ def _conv_nd( attrs={'axis': -1, 'use_mkldnn': use_mkldnn}, ) else: - if len(x_shape) > len(y_shape): - padding = len(x_shape) - len(y_shape) - channel_dim - bias = paddle.reshape( - bias, [1] * channel_dim + y_shape + [1] * padding - ) - else: - padding = len(y_shape) - len(x_shape) - channel_dim - pre_bias = paddle.reshape( - pre_bias, [1] * channel_dim + y_shape + [1] * padding - ) + assert len(x_shape) > len( + y_shape + ), 'The length of pre_bias must greater than the length of bias' + padding = len(x_shape) - len(y_shape) - channel_dim + bias = paddle.reshape( + bias, [1] * channel_dim + y_shape + [1] * padding + ) + helper.append_op( type='elementwise_add', inputs={'X': [pre_bias], 'Y': [bias]}, @@ -1367,16 +1365,13 @@ def conv2d_transpose( attrs={'axis': -1, 'use_mkldnn': False}, ) else: - if len(x_shape) > len(y_shape): - padding = len(x_shape) - len(y_shape) - channel_dim - bias = paddle.reshape( - bias, [1] * channel_dim + y_shape + [1] * padding - ) - else: - padding = len(y_shape) - len(x_shape) - channel_dim - pre_bias = paddle.reshape( - pre_bias, [1] * channel_dim + y_shape + [1] * padding - ) + assert len(x_shape) > len( + y_shape + ), 'The length of pre_bias must greater than the length of bias' + padding = len(x_shape) - len(y_shape) - channel_dim + bias = paddle.reshape( + bias, [1] * channel_dim + y_shape + [1] * padding + ) helper.append_op( type='elementwise_add', inputs={'X': [pre_bias], 'Y': [bias]}, From dc00e5e742fbcec1881ab613b4e79e38534fe4fa Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Mon, 20 Mar 2023 02:34:28 +0000 Subject: [PATCH 3/4] Don't import paddle --- python/paddle/nn/functional/conv.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/python/paddle/nn/functional/conv.py b/python/paddle/nn/functional/conv.py index 691e2d1b0642e8..dfdda6d7e59344 100644 --- a/python/paddle/nn/functional/conv.py +++ b/python/paddle/nn/functional/conv.py @@ -12,8 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import paddle -from paddle import _C_ops, _legacy_C_ops, get_flags, in_dynamic_mode +from paddle import _C_ops, _legacy_C_ops, get_flags, in_dynamic_mode, reshape from paddle.device import ( get_all_custom_device_type, is_compiled_with_cuda, @@ -262,7 +261,7 @@ def _conv_nd( y_shape ), 'The length of pre_bias must greater than the length of bias' padding = len(x_shape) - len(y_shape) - channel_dim - bias = paddle.reshape( + bias = reshape( bias, [1] * channel_dim + y_shape + [1] * padding ) @@ -1369,7 +1368,7 @@ def conv2d_transpose( y_shape ), 'The length of pre_bias must greater than the length of bias' padding = len(x_shape) - len(y_shape) - channel_dim - bias = paddle.reshape( + bias = reshape( bias, [1] * channel_dim + y_shape + [1] * padding ) helper.append_op( From 36fa00d34fa68a0be6ec595a5cd98afb9f2fae4b Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Mon, 20 Mar 2023 03:28:48 +0000 Subject: [PATCH 4/4] fix error --- python/paddle/nn/functional/conv.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/paddle/nn/functional/conv.py b/python/paddle/nn/functional/conv.py index dfdda6d7e59344..816fd3266f184f 100644 --- a/python/paddle/nn/functional/conv.py +++ b/python/paddle/nn/functional/conv.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle import _C_ops, _legacy_C_ops, get_flags, in_dynamic_mode, reshape +from paddle import _C_ops, _legacy_C_ops, get_flags, in_dynamic_mode from paddle.device import ( get_all_custom_device_type, is_compiled_with_cuda, @@ -20,6 +20,7 @@ is_compiled_with_rocm, ) from paddle.fluid.framework import _global_flags, in_dygraph_mode +from paddle.tensor.manipulation import reshape from paddle.tensor.math import _add_with_axis from ...common_ops_import import Variable