elementwise_functor.h 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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

14
#include "paddle/fluid/platform/complex.h"
15 16
#include "paddle/phi/core/utils/array.h"
#include "paddle/phi/kernels/funcs/elementwise_functor.h"
17 18 19 20 21

namespace paddle {
namespace operators {

// Define the binary functors used in elementwise ops.
22
// Note: InverseXxxFunctor is needed when calling ElementwiseComputeEx on CPU.
23 24 25

// Add
template <typename T>
26
using AddFunctor = phi::funcs::AddFunctor<T>;
27

28
template <typename T>
29
using InverseAddFunctor = phi::funcs::InverseAddFunctor<T>;
30 31 32

// Subtract
template <typename T>
33
using SubFunctor = phi::funcs::SubtractFunctor<T>;
34

35
template <typename T>
36
using InverseSubFunctor = phi::funcs::InverseSubtractFunctor<T>;
37 38 39

// Multiply
template <typename T>
40
using MulFunctor = phi::funcs::MultiplyFunctor<T>;
41

42
template <typename T>
43
using InverseMulFunctor = phi::funcs::InverseMultiplyFunctor<T>;
44 45 46

// Divide
template <typename T>
47
using DivFunctor = phi::funcs::DivideFunctor<T>;
48

49
template <typename T>
50
using InverseDivFunctor = phi::funcs::InverseDivideFunctor<T>;
51 52 53 54

// Floor Divide
template <typename T>
struct FloorDivFunctor {
55
  inline HOSTDEVICE T operator()(const T a, const T b) const {
56 57 58 59 60 61 62
    PADDLE_ENFORCE(b != 0, DIV_ERROR_INFO);
    return static_cast<T>(std::trunc(a / b));
  }
};

template <typename T>
struct InverseFloorDivFunctor {
63
  inline HOSTDEVICE T operator()(const T a, const T b) const {
64 65 66 67 68 69 70 71 72
    PADDLE_ENFORCE(a != 0, DIV_ERROR_INFO);
    return static_cast<T>(std::trunc(b / a));
  }
};

#undef DIV_ERROR_INFO

// Maximum
template <typename T>
73
using MaxFunctor = phi::funcs::MaximumFunctor<T>;
74 75 76

// Minmum
template <typename T>
77
using MinFunctor = phi::funcs::MinimumFunctor<T>;
78

79 80 81
template <typename T>
using Complex = paddle::platform::complex<T>;

82
// Ternary compare
83
template <typename T>
84
using MaxGradXFunctor = phi::funcs::MaxGradXFunctor<T>;
85
template <typename T>
86
using MaxGradYFunctor = phi::funcs::MaxGradYFunctor<T>;
87
template <typename InT, typename OutT>
88
using MaxGradXYFunctor = phi::funcs::MaxGradXYFunctor<InT, OutT>;
89

90
template <typename T>
91
using MinGradXFunctor = phi::funcs::MinGradXFunctor<T>;
92
template <typename T>
93
using MinGradYFunctor = phi::funcs::MinGradYFunctor<T>;
94
template <typename InT, typename OutT>
95
using MinGradXYFunctor = phi::funcs::MinGradXYFunctor<InT, OutT>;
96

97 98
}  // namespace operators
}  // namespace paddle