Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix conv axis is not the default value -1 #51486

Merged
merged 4 commits into from
Mar 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 49 additions & 7 deletions python/paddle/nn/functional/conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -247,12 +248,30 @@ 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:
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 = reshape(
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
Expand Down Expand Up @@ -1335,7 +1354,30 @@ 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:
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 = reshape(
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

Expand Down