Skip to content

Commit 8fb05df

Browse files
committed
Fix
1 parent 58b942a commit 8fb05df

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

paddle/phi/kernels/gpudnn/softmax_grad_kernel.cu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void SoftmaxGradGPUDNNKernel(const Context& dev_ctx,
2828
int axis,
2929
DenseTensor* x_grad) {
3030
dev_ctx.template Alloc<T>(x_grad);
31+
if (x_grad->numel() == 0) return;
3132

3233
const int rank = out.dims().size();
3334
// For 0D Tensor

paddle/phi/kernels/gpudnn/softmax_kernel.cu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void SoftmaxGPUDNNKernel(const Context& dev_ctx,
2727
int axis,
2828
DenseTensor* out) {
2929
dev_ctx.template Alloc<T>(out);
30+
if (x.numel() == 0) return;
3031

3132
const int rank = x.dims().size();
3233
// For 0D Tensor

test/legacy_test/test_softmax_op.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import unittest
1616

1717
import numpy as np
18-
from op_test import OpTest, convert_float_to_uint16
18+
from op_test import OpTest, convert_float_to_uint16, get_places
1919
from utils import static_guard
2020

2121
import paddle
@@ -646,5 +646,20 @@ def executed_api(self):
646646
self.softmax = F.softmax_
647647

648648

649+
class TestSoftmaxAPI_ZeroSize(unittest.TestCase):
650+
def test_dygraph(self):
651+
for place in get_places():
652+
paddle.disable_static(place)
653+
x = paddle.rand([0, 2, 3])
654+
x.stop_gradient = False
655+
x.retain_grads()
656+
out = paddle.nn.functional.softmax(x)
657+
out.retain_grads()
658+
out.backward()
659+
np.testing.assert_allclose(out.numpy(), np.random.random([0, 2, 3]))
660+
np.testing.assert_allclose(x.grad.shape, x.shape)
661+
paddle.enable_static()
662+
663+
649664
if __name__ == "__main__":
650665
unittest.main()

0 commit comments

Comments
 (0)