/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #pragma once #include "paddle/phi/common/complex.h" #include "paddle/phi/common/float16.h" #include "paddle/phi/core/enforce.h" #include "paddle/phi/core/hostdevice.h" #if defined(__xpu__) #include #include "xpu/kernel/math_xpu2.h" // pow() #endif namespace phi { namespace funcs { // Define the binary functors used in elementwise ops. // Note: InverseXxxFunctor is needed when calling ElementwiseComputeEx on CPU. // Add template struct AddFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return a + b; } }; template struct InverseAddFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return b + a; } }; // Subtract template struct SubtractFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return a - b; } }; template struct InverseSubtractFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return b - a; } }; // Multiply template struct MultiplyFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return a * b; } }; template <> struct MultiplyFunctor { inline HOSTDEVICE bool operator()(const bool a, const bool b) const { return a && b; } }; template struct InverseMultiplyFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return b * a; } }; template <> struct InverseMultiplyFunctor { inline HOSTDEVICE bool operator()(const bool a, const bool b) const { return b && a; } }; template struct IsZeroFunctor { HOSTDEVICE bool operator()(T x) const { return x == static_cast(0); } }; // Divide #define DIV_ERROR_INFO \ "InvalidArgumentError: Integer division by zero encountered in " \ "(floor) divide. Please check the input value." template struct DivideFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return a / b; } }; template struct DivideFunctor< T, typename std::enable_if::value>::type> { inline HOSTDEVICE T operator()(const T a, const T b) const { // For int32/int64, need to check whether the divison is zero. PADDLE_ENFORCE(b != 0, DIV_ERROR_INFO); return a / b; } }; template struct InverseDivideFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return b / a; } }; template using ComplexType = phi::dtype::complex; template struct DivGradXYFunctor { inline HOSTDEVICE phi::Array operator()(const InT a, const InT b, const InT c) { // dx = dout / y // dy = - dout * out / y phi::Array outs; outs[0] = a / c; outs[1] = -a * b / c; return outs; } }; template struct DivGradXYFunctor, ComplexType> { inline HOSTDEVICE phi::Array, 2> operator()( const ComplexType a, const ComplexType b, const ComplexType c) { phi::Array, 2> outs; ComplexType c_conj(c.real, -c.imag); ComplexType out_div_c_conj((b / c).real, -(b / c).imag); outs[0] = a / c_conj; outs[1] = -a * out_div_c_conj; return outs; } }; // Float div grad template struct DivGradXFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return a / b; } }; // ComplexType div grad template struct DivGradXFunctor> { inline HOSTDEVICE ComplexType operator()(const ComplexType a, const ComplexType b) const { ComplexType b_conj(b.real, -b.imag); return a / b_conj; } }; // Float mul and div template struct DivGradYFunctor { inline HOSTDEVICE T operator()(const T a, const T b, const T c) const { return -a * b / c; } }; // ComplexType mul and div template struct DivGradYFunctor> { inline HOSTDEVICE ComplexType operator()(const ComplexType a, const ComplexType b, const ComplexType c) const { ComplexType out_div_c_conj((b / c).real, -(b / c).imag); return -a * out_div_c_conj; } }; // Fmin template struct FMinFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return std::fmin(a, b); } }; template <> struct FMinFunctor { inline HOSTDEVICE dtype::float16 operator()(const dtype::float16 a, const dtype::float16 b) const { float float_a = static_cast(a); float float_b = static_cast(b); auto result = std::fmin(float_a, float_b); return static_cast(result); } }; template <> struct FMinFunctor { inline HOSTDEVICE int operator()(const int a, const int b) const { float float_a = static_cast(a); float float_b = static_cast(b); auto result = std::fmin(float_a, float_b); return std::lrint(result); } }; template <> struct FMinFunctor { inline HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const { double double_a = static_cast(a); double double_b = static_cast(b); auto result = std::fmin(double_a, double_b); return std::llrint(result); } }; // Fmax template struct FMaxFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return std::fmax(a, b); } }; template <> struct FMaxFunctor { inline HOSTDEVICE dtype::float16 operator()(const dtype::float16 a, const dtype::float16 b) const { float float_a = static_cast(a); float float_b = static_cast(b); auto result = std::fmax(float_a, float_b); return static_cast(result); } }; template <> struct FMaxFunctor { inline HOSTDEVICE int operator()(const int a, const int b) const { float float_a = static_cast(a); float float_b = static_cast(b); auto result = std::fmax(float_a, float_b); return std::lrint(result); } }; template <> struct FMaxFunctor { inline HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const { double double_a = static_cast(a); double double_b = static_cast(b); auto result = std::fmax(double_a, double_b); return std::llrint(result); } }; template struct FMaxGradDx { HOSTDEVICE T operator()(T x, T y, T out, T dout) const { return dout * static_cast((x >= y) || isnan(y)); } }; template <> struct FMaxGradDx { HOSTDEVICE dtype::float16 operator()(dtype::float16 x, dtype::float16 y, dtype::float16 out, dtype::float16 dout) const { return dout * static_cast((x >= y) || dtype::isnan(y)); } }; template <> struct FMaxGradDx { HOSTDEVICE int operator()(int x, int y, int out, int dout) const { return dout * static_cast((x >= y)); } }; template <> struct FMaxGradDx { HOSTDEVICE int64_t operator()(int64_t x, int64_t y, int64_t out, int64_t dout) const { return dout * static_cast((x >= y)); } }; template struct FMaxGradDy { HOSTDEVICE T operator()(T x, T y, T out, T dout) const { return dout * static_cast(!((x >= y) || isnan(y))); } }; template <> struct FMaxGradDy { HOSTDEVICE dtype::float16 operator()(dtype::float16 x, dtype::float16 y, dtype::float16 out, dtype::float16 dout) const { return dout * static_cast(!((x >= y) || dtype::isnan(y))); } }; template <> struct FMaxGradDy { HOSTDEVICE int64_t operator()(int64_t x, int64_t y, int64_t out, int64_t dout) const { return dout * static_cast(!((x >= y))); } }; template <> struct FMaxGradDy { HOSTDEVICE int operator()(int x, int y, int out, int dout) const { return dout * static_cast(!((x >= y))); } }; template struct FMinGradDx { HOSTDEVICE T operator()(T x, T y, T out, T dout) const { return dout * static_cast((x <= y) || isnan(y)); } }; template <> struct FMinGradDx { HOSTDEVICE dtype::float16 operator()(dtype::float16 x, dtype::float16 y, dtype::float16 out, dtype::float16 dout) const { return dout * static_cast((x <= y) || dtype::isnan(y)); } }; template <> struct FMinGradDx { HOSTDEVICE int operator()(int x, int y, int out, int dout) const { return dout * static_cast((x <= y)); } }; template <> struct FMinGradDx { HOSTDEVICE int64_t operator()(int64_t x, int64_t y, int64_t out, int64_t dout) const { return dout * static_cast((x <= y)); } }; template struct FMinGradDy { HOSTDEVICE T operator()(T x, T y, T out, T dout) const { return dout * static_cast(!((x <= y) || isnan(y))); } }; template <> struct FMinGradDy { HOSTDEVICE dtype::float16 operator()(dtype::float16 x, dtype::float16 y, dtype::float16 out, dtype::float16 dout) const { return dout * static_cast(!((x <= y) || dtype::isnan(y))); } }; template <> struct FMinGradDy { HOSTDEVICE int operator()(int x, int y, int out, int dout) const { return dout * static_cast(!((x <= y))); } }; template <> struct FMinGradDy { HOSTDEVICE int64_t operator()(int64_t x, int64_t y, int64_t out, int64_t dout) const { return dout * static_cast(!((x <= y))); } }; template struct MultiplyGradFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return a * b; } }; template struct MultiplyGradFunctor> { inline HOSTDEVICE ComplexType operator()(const ComplexType a, const ComplexType b) const { ComplexType b_conj(b.real, -b.imag); return a * b_conj; } }; template struct MultiplyGradXYFunctor { inline HOSTDEVICE phi::Array operator()(const InT a, const InT b, const InT c) { phi::Array outs; // dx = dout * y outs[0] = a * b; // dy = dout * x outs[1] = a * c; return outs; } }; template struct MultiplyGradXYFunctor, ComplexType> { inline HOSTDEVICE phi::Array, 2> operator()( const ComplexType a, const ComplexType b, const ComplexType c) { phi::Array, 2> outs; // dx = dout * y ComplexType b_conj(b.real, -b.imag); outs[0] = a * b_conj; // dy = dout * x ComplexType c_conj(c.real, -c.imag); outs[1] = a * c_conj; return outs; } }; // Maximum template struct MaximumFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return a > b ? a : b; } }; template struct MaxGradXFunctor { inline HOSTDEVICE T operator()(const T x, const T y, const T dout) const { return dout * static_cast(x > y); } }; template struct MaxGradYFunctor { inline HOSTDEVICE T operator()(const T x, const T y, const T dout) const { return dout * static_cast(x <= y); } }; template struct MaxGradXYFunctor { inline HOSTDEVICE phi::Array operator()(const InT x, const InT y, const InT dout) { phi::Array outs; // dx = dout * (x > y) outs[0] = static_cast(dout * static_cast(x > y)); // dy = dout * (x <= y) outs[1] = static_cast(dout * static_cast(x <= y)); return outs; } }; // Minimum template struct MinimumFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return a < b ? a : b; } }; template struct MinGradXFunctor { inline HOSTDEVICE T operator()(const T x, const T y, const T dout) const { return dout * static_cast(x < y); } }; template struct MinGradYFunctor { inline HOSTDEVICE T operator()(const T x, const T y, const T dout) const { return dout * static_cast(x >= y); } }; template struct MinGradXYFunctor { inline HOSTDEVICE phi::Array operator()(const InT x, const InT y, const InT dout) { phi::Array outs; // dx = dout * (x < y) outs[0] = static_cast(dout * static_cast(x < y)); // dy = dout * (x >= y) outs[1] = static_cast(dout * static_cast(x >= y)); return outs; } }; // Modulo template struct RemainderFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { T res = a % b; // Accoding to #PR26732: in dividen % divsor // remainder shall have the same sign as divsor. if ((res != 0) && ((b ^ res) < 0)) res += b; return res; } }; template struct RemainderFunctor< T, typename std::enable_if_t::value>> { inline HOSTDEVICE T operator()(const T a, const T b) const { T res = fmod(a, b); // Accoding to #PR26732: in dividen % divsor // remainder shall have the same sign as divsor. if ((res != 0) && ((res < 0) != (b < 0))) res += b; return res; } }; template struct InverseRemainderFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { T res = b % a; if ((res != 0) && ((res < 0) != (a < 0))) res += a; return res; } }; template struct InverseRemainderFunctor< T, typename std::enable_if_t::value>> { inline HOSTDEVICE T operator()(const T a, const T b) const { T res = fmod(b, a); if ((res != 0) && ((a < 0) != (res < 0))) res += a; return res; } }; template struct ElementwiseHeavisideFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { return a == static_cast(0) ? b : static_cast(a > 0); } }; template struct FloorDivideFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { #ifndef PADDLE_WITH_XPU_KP PADDLE_ENFORCE(b != 0, DIV_ERROR_INFO); #endif return static_cast(std::trunc(a / b)); } }; template struct InverseFloorDivideFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { #ifndef PADDLE_WITH_XPU_KP PADDLE_ENFORCE(a != 0, DIV_ERROR_INFO); #endif return static_cast(std::trunc(b / a)); } }; template struct ElementwisePowFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { // TODO(wujionghao): A potential speed improvement is supporting different // types in C++. #if defined(__CUDA_ARCH__) || defined(__HIPCC__) // On CUDAPlace, std::pow(3, 1) calls pow(float, float), and // 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. if (std::is_integral::value) { return std::llrint( std::pow(static_cast(a), static_cast(b))); } #endif #ifdef PADDLE_WITH_XPU_KP return pow(a, b); #endif return std::pow(a, b); } }; } // namespace funcs } // namespace phi