Caffe2 - C++ API
A deep learning, cross platform ML framework
utils.h
1 #ifndef CAFFE2_UTILS_MATH_UTILS_H_
2 #define CAFFE2_UTILS_MATH_UTILS_H_
3 
4 #include <vector>
5 
6 #include "caffe2/core/common.h"
7 
8 #if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) || \
9  defined(__HIP__)
10 #define MATH_UTILS_DECL inline __host__ __device__
11 #else
12 #define MATH_UTILS_DECL inline
13 #endif
14 
15 namespace caffe2 {
16 namespace math {
17 
18 namespace utils {
19 
20 template <typename T>
21 MATH_UTILS_DECL T Not(const T x) {
22  return !x;
23 }
24 
25 template <typename T>
26 MATH_UTILS_DECL T Sign(const T x) {
27  return x > 0 ? T(1) : (x < 0 ? T(-1) : T(0));
28 }
29 
30 template <typename T>
31 MATH_UTILS_DECL T Negate(const T x) {
32  return -x;
33 }
34 
35 template <typename T>
36 MATH_UTILS_DECL T Inv(const T x) {
37  return T(1) / x;
38 }
39 
40 template <typename T>
41 MATH_UTILS_DECL T Square(const T x) {
42  return x * x;
43 }
44 
45 template <typename T>
46 MATH_UTILS_DECL T Cube(const T x) {
47  return x * x * x;
48 }
49 
50 // Function uses casting from int to unsigned to compare if value of
51 // parameter a is greater or equal to zero and lower than value of
52 // parameter b. The b parameter is of type signed and is always
53 // positive,
54 // therefore its value is always lower than 0x800... where casting
55 // negative value of a parameter converts it to value higher than
56 // 0x800...
57 // The casting allows to use one condition instead of two.
58 MATH_UTILS_DECL bool IsAGeZeroAndALtB(const int a, const int b) {
59  return static_cast<unsigned int>(a) < static_cast<unsigned>(b);
60 }
61 
62 // Increase the index digits by one based on dims.
63 template <typename TIndex>
64 CAFFE2_API void
65 IncreaseIndexInDims(int ndim, const TIndex* dims, TIndex* index);
66 
67 // Get index value from dims and index digits.
68 CAFFE2_API int GetIndexFromDims(const int n, const int* dims, const int* index);
69 
70 // Checks if the input permutation is an identity permutation;
71 CAFFE2_API bool IsIdentityPermutation(const int n, const int* perm);
72 
73 CAFFE2_API bool
74 CheckReduceDims(const int ndim, const int* X_dims, const int* Y_dims);
75 
76 CAFFE2_API bool IsRowwiseReduce(
77  const int ndim,
78  const int* X_dims,
79  const int* Y_dims,
80  int* rows,
81  int* cols);
82 
83 CAFFE2_API bool IsColwiseReduce(
84  const int ndim,
85  const int* X_dims,
86  const int* Y_dims,
87  int* rows,
88  int* cols);
89 
90 CAFFE2_API bool IsBothEndsReduce(
91  const int ndim,
92  const int* X_dims,
93  const int* Y_dims,
94  int* pre,
95  int* mid,
96  int* nxt);
97 
98 // Computest the broadcast binary operation dims.
99 CAFFE2_API void ComputeBroadcastBinaryOpDims(
100  const int A_ndim,
101  const int* A_dims,
102  const int B_ndim,
103  const int* B_dims,
104  int* A_broadcast_dims,
105  int* B_broadcast_dims,
106  int* C_broadcast_dims);
107 
108 CAFFE2_API bool IsRowwiseBroadcastBinaryOp(
109  const int ndim,
110  const int* A_dims,
111  const int* B_dims,
112  int* rows,
113  int* cols,
114  bool* broadcast_1st);
115 
116 CAFFE2_API bool IsColwiseBroadcastBinaryOp(
117  const int ndim,
118  const int* A_dims,
119  const int* B_dims,
120  int* rows,
121  int* cols,
122  bool* broadcast_1st);
123 
124 CAFFE2_API bool IsBothEndsBroadcastBinaryOp(
125  const int ndim,
126  const int* A_dims,
127  const int* B_dims,
128  int* pre,
129  int* mid,
130  int* nxt,
131  bool* broadcast_1st);
132 
133 CAFFE2_API bool IsBatchTranspose2D(const int ndim, const int* axes);
134 
135 CAFFE2_API void ComputeTransposeAxesForReduceOp(
136  const int num_dims,
137  const int num_reduce_axes,
138  const int* reduce_axes,
139  int* transpose_axes);
140 
141 CAFFE2_API void
142 ComputeTransposeAxesForReduceOp(const int ndim, const int* dims, int* axes);
143 
144 template <typename TIndex>
145 CAFFE2_API void ComputeTransposedStrides(
146  int ndim,
147  const TIndex* dims,
148  const int* axes,
149  TIndex* strides);
150 
151 } // namespace utils
152 
153 // Calculates ceil(a / b). User must be careful to ensure that there
154 // is no overflow or underflow in the calculation.
155 template <typename T>
156 constexpr T DivUp(const T a, const T b) {
157  return (a + b - T(1)) / b;
158 }
159 
160 // Rounds a up to the next highest multiple of b. User must be careful
161 // to ensure that there is no overflow or underflow in the calculation
162 // of divUp.
163 template <typename T>
164 constexpr T RoundUp(const T a, const T b) {
165  return DivUp<T>(a, b) * b;
166 }
167 
168 // Returns log2(n) for a positive integer type
169 template <typename T>
170 constexpr int IntegerLog2(T n, int p = 0) {
171  return (n <= 1) ? p : IntegerLog2(n / 2, p + 1);
172 }
173 
174 // Returns the next highest power-of-2 for an integer type
175 template <typename T>
176 constexpr T IntegerNextHighestPowerOf2(T v) {
177  return (IntegerIsPowerOf2(v) ? T(2) * v : (T(1) << (IntegerLog2(v) + 1)));
178 }
179 
180 } // namespace math
181 } // namespace caffe2
182 
183 #endif // CAFFE2_UTILS_MATH_UTILS_H_
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13