Skip to content
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
4 changes: 4 additions & 0 deletions paddle/phi/kernels/cpu/cum_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ void ScanKernel(const Context& dev_ctx,
bool reverse,
Reducer reducer,
DenseTensor* out) {
if (out && out->numel() == 0) {
dev_ctx.template Alloc<T>(out);
return;
}
dev_ctx.template Alloc<T>(out);

if (x.numel() == 1) {
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/gpu/cum_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ void ScanKernel(const Context& dev_ctx,
bool reverse,
Op op,
DenseTensor* out) {
if (out && out->numel() == 0) {
dev_ctx.template Alloc<T>(out);
return;
}
T* out_data = dev_ctx.template Alloc<T>(out);

// For 0D Tensor
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/impl/logcumsumexp_grad_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ void LogcumsumexpGradKernel(const Context& dev_ctx,
bool exclusive,
bool reverse,
DenseTensor* d_x) {
if (d_x && d_x->numel() == 0) {
dev_ctx.template Alloc<T>(d_x);
return;
}
reverse = !reverse;
dev_ctx.template Alloc<T>(d_x);

Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/xpu/cum_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ void CumsumKernel(const Context& dev_ctx,
bool reverse,
DenseTensor* out) {
using XPUType = typename XPUTypeTrait<T>::Type;
if (out && out->numel() == 0) {
dev_ctx.template Alloc<T>(out);
return;
}
dev_ctx.template Alloc<T>(out);

if (x.numel() == 1) {
Expand Down
34 changes: 34 additions & 0 deletions test/legacy_test/test_cumsum_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,5 +747,39 @@ def test_fp16(self):
paddle.disable_static()


def create_test_class(op_type, dtype, shape, axis):
class Cls(unittest.TestCase):
def test_zero_size(self):
paddle.disable_static()
numpy_tensor_1 = np.random.rand(*shape).astype(dtype)
paddle_x = paddle.to_tensor(numpy_tensor_1)
paddle_x.stop_gradient = False

paddle_api = eval(f"paddle.{op_type}")
paddle_out = paddle_api(paddle_x, axis=axis)
numpy_api = eval(f"np.{op_type}")
numpy_out = numpy_api(numpy_tensor_1, axis=axis)

np.testing.assert_allclose(
paddle_out.numpy(),
numpy_out,
1e-2,
1e-2,
)
np.testing.assert_allclose(
paddle_out.shape,
numpy_out.shape,
)

cls_name = f"{op_type}{dtype}_0SizeTest"
Cls.__name__ = cls_name
globals()[cls_name] = Cls


create_test_class("cumsum", "float32", [3, 4, 0], 0)
create_test_class("cumsum", "float64", [3, 4, 0, 3, 4], -2)
create_test_class("cumsum", "int32", [3, 4, 0], 0)
create_test_class("cumsum", "int64", [3, 4, 0, 3, 4], -1)

if __name__ == '__main__':
unittest.main()
35 changes: 35 additions & 0 deletions test/legacy_test/test_logcumsumexp_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,5 +375,40 @@ def test_check_grad(self):
)


def create_test_class(op_type, dtype, shape, axis):
class Cls(unittest.TestCase):
def test_zero_size(self):
paddle.disable_static()
numpy_tensor_1 = np.random.rand(*shape).astype(dtype)
paddle_x = paddle.to_tensor(numpy_tensor_1)
paddle_x.stop_gradient = False

paddle_api = eval(f"paddle.{op_type}")
paddle_out = paddle_api(paddle_x, axis=axis)
numpy_out = np.log(
np.cumsum(np.exp(numpy_tensor_1), axis=axis)
) # Numpy does not have logcumsumexp

np.testing.assert_allclose(
paddle_out.numpy(),
numpy_out,
1e-2,
1e-2,
)
np.testing.assert_allclose(
paddle_out.shape,
numpy_out.shape,
)

cls_name = f"{op_type}{dtype}_0SizeTest"
Cls.__name__ = cls_name
globals()[cls_name] = Cls


create_test_class("logcumsumexp", "float32", [3, 4, 0], 0)
create_test_class("logcumsumexp", "float64", [3, 4, 0, 3, 4], -2)
create_test_class("logcumsumexp", "int32", [3, 4, 0], 0)
create_test_class("logcumsumexp", "int64", [3, 4, 0, 3, 4], -1)

if __name__ == '__main__':
unittest.main()