Skip to content

Commit

Permalink
fix test_gammaincc_op fail (PaddlePaddle#61122)
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatV authored and eee4017 committed Jan 30, 2024
1 parent 0c96026 commit d53e700
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
9 changes: 7 additions & 2 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from paddle import _C_ops
from paddle.base.libpaddle import DataType
from paddle.common_ops_import import VarDesc, dygraph_utils
from paddle.pir import Value
from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only

from ..base.data_feeder import (
Expand Down Expand Up @@ -5128,11 +5129,15 @@ def gammaincc(x, y, name=None):
Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True,
[1. , 0.15729916, 0.00000774, 0. , 0. ])
"""
if not paddle.all(paddle.greater_equal(x, paddle.zeros_like(x))):
if not isinstance(x, Value) and not paddle.all(
paddle.greater_equal(x, paddle.zeros_like(x))
):
raise ValueError(
"The input argument x must be greater than or equal to 0."
)
if not paddle.all(paddle.greater_equal(y, paddle.zeros_like(y))):
if not isinstance(x, Value) and not paddle.all(
paddle.greater_equal(y, paddle.zeros_like(y))
):
raise ValueError(
"The input argument y must be greater than or equal to 0."
)
Expand Down
49 changes: 23 additions & 26 deletions test/legacy_test/test_gammaincc_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import numpy as np
from op_test import OpTest
from scipy import special
from utils import static_guard

import paddle
from paddle.base import core
Expand Down Expand Up @@ -69,54 +70,50 @@ def init_dtype_type(self):
self.dtype = "float64"

def test_static_api(self):
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data('x', self.x_np.shape, self.x_np.dtype)
y = paddle.static.data('y', self.y_np.shape, self.y_np.dtype)
out = paddle.gammaincc(x, y)
exe = paddle.static.Executor(self.place)
(res,) = exe.run(
feed={'x': self.x_np, 'y': self.y_np}, fetch_list=[out]
)
out_ref = ref_gammaincc(self.x_np, self.y_np)
np.testing.assert_allclose(out_ref, res, rtol=1e-6, atol=1e-6)
with static_guard():
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data('x', self.x_np.shape, self.x_np.dtype)
y = paddle.static.data('y', self.y_np.shape, self.y_np.dtype)
out = paddle.gammaincc(x, y)
exe = paddle.static.Executor(self.place)
(res,) = exe.run(
feed={'x': self.x_np, 'y': self.y_np}, fetch_list=[out]
)
out_ref = ref_gammaincc(self.x_np, self.y_np)
np.testing.assert_allclose(out_ref, res, rtol=1e-6, atol=1e-6)

def test_dygraph_api(self):
paddle.disable_static()
x = paddle.to_tensor(self.x_np)
y = paddle.to_tensor(self.y_np)
out = paddle.gammaincc(x, y)
out_ref = ref_gammaincc(self.x_np, self.y_np)
np.testing.assert_allclose(out_ref, out.numpy(), rtol=1e-6, atol=1e-6)

def test_x_le_zero_error(self):
paddle.disable_static()
x = paddle.to_tensor(self.x_np)
y = paddle.to_tensor(self.y_np)
x[0] = -1
self.assertRaises(ValueError, paddle.gammaincc, x, y)

def test_a_le_zero_error(self):
paddle.disable_static()
x = paddle.to_tensor(self.x_np)
y = paddle.to_tensor(self.y_np)
y[0] = -1
self.assertRaises(ValueError, paddle.gammaincc, x, y)

def test_dtype_error(self):
paddle.enable_static()
# in static graph mode
with self.assertRaises(TypeError):
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data(
name="x", shape=self.shape, dtype="int32"
)
y = paddle.static.data(
name="y", shape=self.shape, dtype="int32"
)
out = paddle.gammaincc(x, y)
with static_guard():
# in static graph mode
with self.assertRaises(TypeError):
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data(
name="x", shape=self.shape, dtype="int32"
)
y = paddle.static.data(
name="y", shape=self.shape, dtype="int32"
)
out = paddle.gammaincc(x, y)

paddle.disable_static()
# in dynamic mode
with self.assertRaises(RuntimeError):
with paddle.base.dygraph.guard():
Expand Down

0 comments on commit d53e700

Please sign in to comment.