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
9 changes: 9 additions & 0 deletions paddle/phi/kernels/funcs/elementwise_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,10 @@ compute_pow(const T a, const T b) {
// it will return a float number like 2.99... , which floor to 2
// when cast to int by default and it is wrong.
// Use llrint to cast it to the nearest integer, which is 3.
T zero = static_cast<T>(0);
if (a == zero && b < zero) {
return zero;
}
return llrint(pow(static_cast<double>(a), static_cast<double>(b)));
}
template <typename T, typename MPType>
Expand All @@ -1057,6 +1061,11 @@ compute_pow(const T a, const T b) {
#else
template <typename T, typename MPType>
inline HOSTDEVICE T compute_pow(const T a, const T b) {
if constexpr (std::is_integral<T>::value) {
if (a == static_cast<T>(0) && b < static_cast<T>(0)) {
return static_cast<T>(0);
}
}
MPType a_val = static_cast<MPType>(a);
MPType b_val = static_cast<MPType>(b);
#ifdef PADDLE_WITH_XPU_KP
Expand Down
7 changes: 7 additions & 0 deletions paddle/phi/kernels/gpu/activation_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ void PowKernel(const Context& dev_ctx,
const DenseTensor& x,
const Scalar& factor,
DenseTensor* out) {
if constexpr (std::is_integral<T>::value) {
PADDLE_ENFORCE_GE(
factor.to<float>(),
0,
common::errors::InvalidArgument(
"Integers to negative integer powers are not allowed."));
}
if (factor.to<float>() == 0) {
std::vector<int64_t> vec_dims = common::vectorize(out->dims());
phi::Full<T, Context>(
Expand Down
51 changes: 51 additions & 0 deletions test/legacy_test/test_elementwise_pow_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,57 @@ def test_check_grad_normal(self):
)


class TestElementwisePowOp_ZeroBaseNumber1(TestElementwisePowOp):
def setUp(self):
self.op_type = "elementwise_pow"
self.python_api = paddle.pow
self.public_python_api = paddle.pow
self.prim_op_type = "prim"

self.inputs = {
'X': np.random.randint(-100, -1, size=[20, 5]).astype("int32"),
'Y': np.random.randint(-200, 0, size=[20, 5], dtype="int32"),
}
self.outputs = {'Out': np.zeros([20, 5]).astype("int32")}

def test_check_grad_normal(self):
pass


class TestElementwisePowOp_ZeroBaseNumber2(TestElementwisePowOp):
def setUp(self):
self.op_type = "elementwise_pow"
self.python_api = paddle.pow
self.public_python_api = paddle.pow
self.prim_op_type = "prim"

self.inputs = {
'X': np.random.randint(2, 100, size=[20, 5]).astype("int32"),
'Y': np.random.randint(-200, 0, size=[20, 5], dtype="int32"),
}
self.outputs = {'Out': np.zeros([20, 5]).astype("int32")}

def test_check_grad_normal(self):
pass


class TestElementwisePowOp_ZeroBaseNumber3(TestElementwisePowOp):
def setUp(self):
self.op_type = "elementwise_pow"
self.python_api = paddle.pow
self.public_python_api = paddle.pow
self.prim_op_type = "prim"

self.inputs = {
'X': np.asarray([-1, 0, 1]),
'Y': np.asarray([-1, -1, -1]),
}
self.outputs = {'Out': np.asarray([-1, 0, 1])}

def test_check_grad_normal(self):
pass


class TestElementwisePowOp_ZeroDim1(TestElementwisePowOp):
def setUp(self):
self.op_type = "elementwise_pow"
Expand Down