1 #ifndef CAFFE2_UTILS_MATH_UTILS_H_ 2 #define CAFFE2_UTILS_MATH_UTILS_H_ 6 #include "caffe2/core/common.h" 8 #if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) || \ 10 #define MATH_UTILS_DECL inline __host__ __device__ 12 #define MATH_UTILS_DECL inline 21 MATH_UTILS_DECL
T Not(
const T x) {
26 MATH_UTILS_DECL
T Sign(
const T x) {
27 return x > 0 ?
T(1) : (x < 0 ? T(-1) : T(0));
31 MATH_UTILS_DECL
T Negate(
const T x) {
36 MATH_UTILS_DECL
T Inv(
const T x) {
41 MATH_UTILS_DECL
T Square(
const T x) {
46 MATH_UTILS_DECL
T Cube(
const T x) {
58 MATH_UTILS_DECL
bool IsAGeZeroAndALtB(
const int a,
const int b) {
59 return static_cast<unsigned int>(a) < static_cast<unsigned>(b);
63 template <
typename TIndex>
65 IncreaseIndexInDims(
int ndim,
const TIndex* dims, TIndex* index);
68 CAFFE2_API
int GetIndexFromDims(
const int n,
const int* dims,
const int* index);
71 CAFFE2_API
bool IsIdentityPermutation(
const int n,
const int* perm);
74 CheckReduceDims(
const int ndim,
const int* X_dims,
const int* Y_dims);
76 CAFFE2_API
bool IsRowwiseReduce(
83 CAFFE2_API
bool IsColwiseReduce(
90 CAFFE2_API
bool IsBothEndsReduce(
99 CAFFE2_API
void ComputeBroadcastBinaryOpDims(
104 int* A_broadcast_dims,
105 int* B_broadcast_dims,
106 int* C_broadcast_dims);
108 CAFFE2_API
bool IsRowwiseBroadcastBinaryOp(
114 bool* broadcast_1st);
116 CAFFE2_API
bool IsColwiseBroadcastBinaryOp(
122 bool* broadcast_1st);
124 CAFFE2_API
bool IsBothEndsBroadcastBinaryOp(
131 bool* broadcast_1st);
133 CAFFE2_API
bool IsBatchTranspose2D(
const int ndim,
const int* axes);
135 CAFFE2_API
void ComputeTransposeAxesForReduceOp(
137 const int num_reduce_axes,
138 const int* reduce_axes,
139 int* transpose_axes);
142 ComputeTransposeAxesForReduceOp(
const int ndim,
const int* dims,
int* axes);
144 template <
typename TIndex>
145 CAFFE2_API
void ComputeTransposedStrides(
155 template <
typename T>
156 constexpr
T DivUp(
const T a,
const T b) {
157 return (a + b -
T(1)) / b;
163 template <
typename T>
164 constexpr
T RoundUp(
const T a,
const T b) {
165 return DivUp<T>(a, b) * b;
169 template <
typename T>
170 constexpr
int IntegerLog2(
T n,
int p = 0) {
171 return (n <= 1) ? p : IntegerLog2(n / 2, p + 1);
175 template <
typename T>
176 constexpr
T IntegerNextHighestPowerOf2(
T v) {
177 return (IntegerIsPowerOf2(v) ?
T(2) * v : (
T(1) << (IntegerLog2(v) + 1)));
183 #endif // CAFFE2_UTILS_MATH_UTILS_H_
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...