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
66 changes: 66 additions & 0 deletions paddle/phi/kernels/funcs/activation_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,41 @@ struct ReciprocalFunctor : public BaseActivationFunctor<T> {
}
};

template <typename T>
struct Reciprocal {
HOSTDEVICE ComplexType<T> operator()(const ComplexType<T>& val) const {
auto both_inf = [](T real, T imag) {
return (std::isinf(real) && std::isinf(imag));
};

auto either_inf = [](T real, T imag) {
return std::isinf(real) || std::isinf(imag);
};

auto either_nan = [](T real, T imag) {
return std::isnan(real) || std::isnan(imag);
};
if (either_nan(val.real, val.imag) || both_inf(val.real, val.imag)) {
// If either is Nan or both are infinite, return {nan, nan}
return ComplexType<T>(std::numeric_limits<T>::quiet_NaN(),
std::numeric_limits<T>::quiet_NaN());
} else if (either_inf(val.real, val.imag)) {
// If either is Inf, return {0, 0}
return ComplexType<T>{static_cast<T>(0), static_cast<T>(0)};
}
return static_cast<ComplexType<T>>(1.0) / val;
}
};

template <typename T>
struct ReciprocalFunctor<ComplexType<T>>
: public BaseActivationFunctor<ComplexType<T>> {
template <typename Device, typename X, typename Out>
void operator()(Device d, X x, Out out) const {
out.device(d) = x.unaryExpr(Reciprocal<T>());
}
};

template <typename T>
struct ReciprocalGradFunctor : public BaseActivationFunctor<T> {
template <typename Device,
Expand Down Expand Up @@ -3607,6 +3642,37 @@ struct CudaReciprocalFunctor : public BaseActivationFunctor<T> {
}
};

template <typename T>
struct CudaReciprocalFunctor<ComplexType<T>>
: public BaseActivationFunctor<ComplexType<T>> {
__device__ __forceinline__ ComplexType<T> operator()(
const ComplexType<T> x) const {
auto both_inf = [](T real, T imag) {
return (::isinf(real) && ::isinf(imag));
};

auto either_inf = [](T real, T imag) {
return ::isinf(real) || ::isinf(imag);
};

auto either_nan = [](T real, T imag) {
return ::isnan(real) || ::isnan(imag);
};
if (either_nan(x.real, x.imag) || both_inf(x.real, x.imag)) {
// If either is Nan or both are infinite, return {nan, nan}
if constexpr (std::is_same<T, float>::value) {
return ComplexType<T>(nanf(""), nanf(""));
} else if constexpr (std::is_same<T, double>::value) {
return ComplexType<T>(nan(""), nan(""));
}
} else if (either_inf(x.real, x.imag)) {
// If either is Inf, return {0, 0}
return ComplexType<T>(static_cast<T>(0), static_cast<T>(0));
}
return static_cast<ComplexType<T>>(1.0) / x;
}
};

template <typename T>
struct CudaReciprocalGradFunctor : public BaseActivationFunctor<T> {
using MPType = typename phi::dtype::MPTypeTrait<T>::Type;
Expand Down
20 changes: 20 additions & 0 deletions test/legacy_test/test_activation_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -3952,6 +3952,26 @@ def init_shape(self):
self.shape = []


class TestReciprocalComplex(unittest.TestCase):
def test_reciprocal_complex(self):
for place in get_places():
x_np = np.array(
[
complex(float('inf'), 0),
complex(0, float('inf')),
complex(float('inf'), float('inf')),
complex(0, float('nan')),
complex(0, 1),
],
dtype=np.complex64,
)
res_np = np.reciprocal(x_np)
with paddle.base.dygraph.guard(place):
x = paddle.to_tensor(x_np, dtype='complex64', place=place)
res = paddle.reciprocal(x)
np.testing.assert_allclose(res.numpy(), res_np)


class TestLog(TestActivation):
def setUp(self):
self.op_type = "log"
Expand Down