Skip to content
Closed
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
23 changes: 21 additions & 2 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@
VarDesc.VarType.FP64,
]

_supported_int_like_types_lt_int64 = frozenset(
{
DataType.BOOL,
DataType.INT8,
DataType.INT16,
DataType.INT32,
DataType.UINT8,
}
)


def _get_reduce_axis(axis, x):
"""
Expand Down Expand Up @@ -4722,8 +4732,17 @@ def cumprod(

"""

if dtype is not None and x.dtype != convert_np_dtype_to_dtype_(dtype):
x = cast(x, dtype)
origin_dtype = x.dtype
if dtype is None:
if origin_dtype in _supported_int_like_types_lt_int64:
# use the default platform integer when integer dtype with a precision less than that of the default platform integer
return _C_ops.cumprod(
_C_ops.cast(x, DataType.INT64), dim, False, False
)
else:
target_dtype = convert_np_dtype_to_dtype_(dtype)
if origin_dtype != target_dtype:
x = cast(x, target_dtype)

if in_dynamic_or_pir_mode():
return _C_ops.cumprod(x, dim, False, False)
Expand Down
34 changes: 30 additions & 4 deletions test/legacy_test/test_cumprod_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ def init_dtype(self):

def setUp(self):
paddle.enable_static()
self.target_dtype = None
self.init_dtype()
self.x = (np.random.rand(2, 3, 10, 10) + 0.5).astype(self.dtype)
self.place = []
Expand All @@ -281,10 +282,10 @@ def test_static_api(self):
def run(place):
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data('X', self.shape, dtype=self.dtype)
out = paddle.cumprod(x, -2)
out = paddle.cumprod(x, -2, self.target_dtype)
exe = paddle.static.Executor(place)
res = exe.run(feed={'X': self.x}, fetch_list=[out])
out_ref = np.cumprod(self.x, -2)
out_ref = np.cumprod(self.x, -2, self.target_dtype)

for r in res:
np.testing.assert_allclose(out_ref, r, rtol=1e-05)
Expand All @@ -297,15 +298,40 @@ def test_dygraph_api(self):
def run(place):
paddle.disable_static(place)
x = paddle.to_tensor(self.x)
out = paddle.cumprod(x, 1)
out_ref = np.cumprod(self.x, 1)
out = paddle.cumprod(x, 1, self.target_dtype)
out_ref = np.cumprod(self.x, 1, self.target_dtype)
np.testing.assert_allclose(out_ref, out.numpy(), rtol=1e-05)
paddle.enable_static()

for place in self.place:
run(place)


class TestCumprodAPICase1(TestCumprodAPI):
def init_dtype(self):
self.dtype = 'int32'
self.shape = [2, 3, 10, 10]


class TestCumprodAPICase2(TestCumprodAPI):
def init_dtype(self):
self.dtype = 'bool'
self.shape = [2, 3, 10, 10]


class TestCumprodAPICase3(TestCumprodAPI):
def init_dtype(self):
self.dtype = 'int64'
self.shape = [2, 3, 10, 10]


class TestCumprodAPICase4(TestCumprodAPI):
def init_dtype(self):
self.dtype = 'float32'
self.shape = [2, 3, 10, 10]
self.target_dtype = 'float64'


# test function.
class TestCumprodReverse(TestCumprod):
def init_dtype(self):
Expand Down
Loading