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

Modify the unsqueeze dimension of input data in conv1d NCL And NLC format #38425

Merged
merged 11 commits into from
Feb 10, 2022
15 changes: 15 additions & 0 deletions python/paddle/fluid/tests/unittests/test_conv1d_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,20 @@ def add_cases(suite):
suite.addTest(
Conv1DTestCase(
methodName='runTest', filter_size=3, padding='valid'))
suite.addTest(
Conv1DTestCase(
methodName='runTest', num_filters=512, padding='valid'))
suite.addTest(
Conv1DTestCase(
methodName='runTest', num_filters=512, padding=[1, 2]))
suite.addTest(
Conv1DTestCase(
methodName='runTest', padding=2, data_format='NLC'))
suite.addTest(Conv1DTestCase(methodName='runTest', padding=[1]))
suite.addTest(Conv1DTestCase(methodName='runTest', padding=[1, 2]))
suite.addTest(
Conv1DTestCase(
methodName='runTest', padding=[1, 2], data_format='NLC'))
suite.addTest(Conv1DTestCase(methodName='runTest', padding=2))
suite.addTest(Conv1DTestCase(methodName='runTest'))
suite.addTest(
Expand Down Expand Up @@ -204,6 +213,12 @@ def add_error_cases(suite):
suite.addTest(
Conv1DErrorTestCase(
methodName='runTest', padding=[1, 2, 3, 4, 5]))
suite.addTest(
Conv1DErrorTestCase(
methodName='runTest', padding=[1, 2, 3, 4, 5], data_format='NLC'))
suite.addTest(
Conv1DErrorTestCase(
methodName='runTest', num_filters=512, padding=[1, 2, 3, 4, 5]))
suite.addTest(Conv1DErrorTestCase(methodName='runTest', dilation=-10))


Expand Down
54 changes: 40 additions & 14 deletions python/paddle/nn/functional/conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,21 +326,44 @@ def conv1d(x,

# update attrs
padding, padding_algorithm = _update_padding_nd(padding, channel_last, 1)
if len(padding) == 2:
padding = padding + [0] * 2
elif len(padding) == 1:
padding = padding + [0]
else:
raise ValueError(
"The size of padding's dimension should be 1 or 2. But got padding={}".
format(padding))
use_dgrad_engine = False

#When num_filters == 512, use dgrad_engine<float, int=512, ..., bool=1> has better performance
if num_filters == 512:
Zjq9409 marked this conversation as resolved.
Show resolved Hide resolved
Zjq9409 marked this conversation as resolved.
Show resolved Hide resolved
use_dgrad_engine = True

#When use_dgrad_engine is true, the original dimension is expanded
if use_dgrad_engine:
if len(padding) == 2:
padding = padding + [0] * 2
elif len(padding) == 1:
padding = padding + [0]
else:
raise ValueError(
"The size of padding's dimension should be 1 or 2. But got padding={}".
format(padding))

stride = convert_to_list(stride, 1, 'stride') + [1]
dilation = convert_to_list(dilation, 1, 'dilation') + [1]
stride = convert_to_list(stride, 1, 'stride') + [1]
dilation = convert_to_list(dilation, 1, 'dilation') + [1]
weight = unsqueeze(weight, axis=[-1])
else:
if len(padding) == 2:
padding = [0] * 2 + padding
elif len(padding) == 1:
padding = [0] + padding
else:
raise ValueError(
"The size of padding's dimension should be 1 or 2. But got padding={}".
format(padding))
stride = [1] + convert_to_list(stride, 1, 'stride')
dilation = [1] + convert_to_list(dilation, 1, 'dilation')
weight = unsqueeze(weight, axis=[-2])

l_type = "conv2d"
if (num_channels == groups and num_channels != 1 and
num_filters % num_channels == 0 and not use_cudnn):

# When "groups==num_channels and num_filters% num_channels == 0" using depthwise_conv2d has better performance
if (core.is_compiled_with_cuda() and num_channels == groups and
num_channels != 1 and num_filters % num_channels == 0):
l_type = 'depthwise_conv2d'
use_cudnn = False

Expand All @@ -351,9 +374,12 @@ def conv1d(x,
else:
l_type = 'conv2d'

squeeze_aixs = -2 if channel_last else -1
squeeze_aixs = -3 if channel_last else -2
Zjq9409 marked this conversation as resolved.
Show resolved Hide resolved
if use_dgrad_engine:
squeeze_aixs += 1

x = unsqueeze(x, axis=[squeeze_aixs])
weight = unsqueeze(weight, axis=[-1])

if in_dygraph_mode():
attrs = ('strides', stride, 'paddings', padding, 'dilations', dilation,
'groups', groups, 'use_cudnn', use_cudnn, 'use_mkldnn', False,
Expand Down