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

Several ops support zero dim on GPU and CPU #49959

Merged
merged 2 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
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
19 changes: 14 additions & 5 deletions paddle/phi/kernels/gpu/prelu_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,20 @@ class PreluOpGradFunctor {
for (size_t i = 0; i < input_dims.size(); ++i) {
numel *= input_dims[i];
}
size_t plane_size = numel / input_dims[0] / input_dims[1];
size_t spatial_size = numel / input_dims[0];
size_t channel =
mode == ChannelLast ? input_dims[input_dims.size() - 1] : input_dims[1];

size_t plane_size;
size_t spatial_size;
size_t channel;
if (mode == PRELU_Scalar) {
juncaipeng marked this conversation as resolved.
Show resolved Hide resolved
plane_size = 1;
spatial_size = 1;
channel = 1;
} else {
plane_size = numel / input_dims[0] / input_dims[1];
spatial_size = numel / input_dims[0];
channel = mode == ChannelLast ? input_dims[input_dims.size() - 1]
: input_dims[1];
}

PReluOpGradKernel<T>
<<<PADDLE_GET_BLOCKS(numel), CUDA_NUM_THREADS, 0, stream>>>(
Expand Down Expand Up @@ -120,7 +130,6 @@ void PReluGradKernel(const Context& dev_ctx,
int numel = x.numel();
auto dim = x.dims();
auto x_rank = dim.size();
std::vector<int> input_shape = phi::vectorize<int>(dim);
auto stream = dev_ctx.stream();

T* alpha_grad_tmp_ptr;
Expand Down
55 changes: 55 additions & 0 deletions python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
paddle.poisson,
paddle.bernoulli,
paddle.median,
paddle.nn.functional.softmax,
juncaipeng marked this conversation as resolved.
Show resolved Hide resolved
paddle.nn.functional.log_softmax,
paddle.nn.functional.gumbel_softmax,
]

inplace_api_list = [
Expand Down Expand Up @@ -1430,6 +1433,26 @@ def test_t(self):
self.assertEqual(out.grad.shape, [])
self.assertEqual(x.grad.shape, [])

def test_prelu(self):
x = paddle.full([], 1.0, 'float32')
x.stop_gradient = False

w1 = paddle.to_tensor([0.25], dtype='float32')
out1 = paddle.nn.functional.prelu(x, w1)
out1.retain_grads()
out1.backward()
self.assertEqual(out1.shape, [])
self.assertEqual(out1.grad.shape, [])
self.assertEqual(x.grad.shape, [])

w2 = paddle.full([], 0.25, dtype='float32')
out2 = paddle.nn.functional.prelu(x, w2)
out2.retain_grads()
out2.backward()
self.assertEqual(out2.shape, [])
juncaipeng marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(out2.grad.shape, [])
self.assertEqual(x.grad.shape, [])


class TestSundryAPIStatic(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -2297,6 +2320,38 @@ def test_sequence_pad(self):
res = self.exe.run(prog, feed={"x": x_tensor}, fetch_list=[out])
self.assertEqual(res[0].shape, (3, 4, 2))

def test_prelu(self):
x1 = paddle.full([], 1.0, 'float32')
x1.stop_gradient = False
w1 = paddle.to_tensor([0.25], dtype='float32')
out1 = paddle.nn.functional.prelu(x1, w1)
paddle.static.append_backward(out1.sum())

x2 = paddle.full([], 1.0, 'float32')
x2.stop_gradient = False
w2 = paddle.full([], 0.25, dtype='float32')
out2 = paddle.nn.functional.prelu(x2, w2)
paddle.static.append_backward(out2.sum())

prog = paddle.static.default_main_program()
res = self.exe.run(
prog,
fetch_list=[
out1,
out2,
x1.grad_name,
x2.grad_name,
out1.grad_name,
out2.grad_name,
],
)
self.assertEqual(res[0].shape, ())
self.assertEqual(res[1].shape, ())
self.assertEqual(res[2].shape, ())
self.assertEqual(res[3].shape, ())
self.assertEqual(res[4].shape, ())
self.assertEqual(res[5].shape, ())


# Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest.
class TestNoBackwardAPI(unittest.TestCase):
Expand Down
9 changes: 4 additions & 5 deletions python/paddle/nn/functional/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def prelu(x, weight, data_format="NCHW", name=None):
Parameters:
x (Tensor): The input Tensor with data type float32, float64.
weight (Tensor): The learnable parameter with data type same as ``x``.
The weight shape is [1] or [in], where `in` is the input channel of ``x``.
The weight shape is [], [1] or [in], where `in` is the input channel of ``x``.
name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
data_format(str, optional): Data format that specifies the layout of input.
It may be "NC", "NCL", "NCHW", "NCDHW", "NLC", "NHWC" or "NDHWC". Default: "NCHW".
Expand Down Expand Up @@ -495,12 +495,11 @@ def prelu(x, weight, data_format="NCHW", name=None):
# [ 6. , 7. , 8. , 9. ]]]]
"""
assert (
len(weight.shape) == 1
), "The dim count of weight shape should be 1 in prelu()."
len(weight.shape) == 0 or len(weight.shape) == 1
juncaipeng marked this conversation as resolved.
Show resolved Hide resolved
), "The dim count of weight shape should be 0 or 1 in prelu()."

mode = 'all'
if weight.shape[0] > 1:

if len(weight.shape) == 1 and weight.shape[0] > 1:
true_data_format = [
'NC',
'NCL',
Expand Down