1 #ifndef CAFFE2_UTILS_MATH_H_ 2 #define CAFFE2_UTILS_MATH_H_ 9 #include "caffe2/utils/cblas.h" 12 #ifdef CAFFE2_USE_ACCELERATE 13 #include <Accelerate/Accelerate.h> 14 #endif // CAFFE2_USE_ACCELERATE 16 #include "caffe2/core/common.h" 17 #include "caffe2/core/types.h" 21 #include "Eigen/Dense" 33 using EigenMatrixMap =
34 Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >;
37 Eigen::Map<Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic> >;
39 using EigenVectorMap = Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1> >;
41 using EigenVectorArrayMap = Eigen::Map<Eigen::Array<T, Eigen::Dynamic, 1> >;
43 using ConstEigenMatrixMap =
44 Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >;
46 using ConstEigenArrayMap =
47 Eigen::Map<const Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic> >;
49 using ConstEigenVectorMap =
50 Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1> >;
52 using ConstEigenVectorArrayMap =
53 Eigen::Map<const Eigen::Array<T, Eigen::Dynamic, 1> >;
58 template <
typename T,
class Context>
59 void Exp(
const int N,
const T* x, T* y, Context* context);
60 template <
typename T,
class Context>
61 void Log(
const int N,
const T* x, T* y, Context* context);
62 template <
typename T,
class Context>
63 void Sqr(
const int N,
const T* x, T* y, Context* context);
65 template <
typename T,
class Context>
66 void Not(
const int N,
const T* x, T* y, Context* context);
68 template <
typename T,
class Context>
69 void Powx(
const int N,
const T* a,
const T b, T* y, Context* context);
71 #define CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(name) \ 72 template <typename T, class Context> \ 73 void name(const int N, const T* a, const T* b, bool* y, Context* context); \ 74 template <typename T, class Context> \ 83 CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(LT);
84 CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(LE);
85 CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(GT);
86 CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(GE);
88 CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(And);
89 CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(Or);
90 CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(Xor);
92 #undef CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT 94 #define CAFFE2_DECLARE_BINARY_OP(name) \ 95 template <typename T, class Context> \ 96 void name(const int N, const T* a, const T* b, T* y, Context* context); \ 97 template <typename T, class Context> \ 105 template <typename T, class Context> \ 107 const int M, const int N, const T* x, T* y, Context* context); \ 108 template <typename T, class Context> \ 110 const int M, const int N, const T* x, T* y, Context* context); 112 CAFFE2_DECLARE_BINARY_OP(Add);
113 CAFFE2_DECLARE_BINARY_OP(Sub);
114 CAFFE2_DECLARE_BINARY_OP(Mul);
115 CAFFE2_DECLARE_BINARY_OP(Div);
117 #undef CAFFE2_DECLARE_BINARY_OP 121 template <
typename T,
class Context>
122 void AddStripedBatch(
132 template <
typename T,
class Context>
133 void RowwiseMax(
const int N,
const int D,
const T* x, T* y,
138 template <
typename T,
class Context>
139 void ColwiseMax(
const int N,
const int D,
const T* x, T* y,
144 template <
typename T,
class Context,
class Engine=DefaultEngine>
145 void Gemm(
const CBLAS_TRANSPOSE TransA,
const CBLAS_TRANSPOSE TransB,
146 const int M,
const int N,
const int K,
const T alpha,
const T* A,
147 const T* B,
const T beta, T* C, Context* context);
151 template <
typename T,
class Context,
class Engine = DefaultEngine>
153 const CBLAS_TRANSPOSE TransA,
154 const CBLAS_TRANSPOSE TransB,
172 template <
typename T,
class Context,
class Engine=DefaultEngine>
173 void Gemv(
const CBLAS_TRANSPOSE TransA,
const int M,
const int N,
174 const T alpha,
const T* A,
const T* x,
const T beta,
175 T* y, Context* context);
177 template <
typename T,
class Context>
178 void Set(
const TIndex N,
const T alpha, T* X, Context* context);
180 template <
typename T,
class Context>
181 void RandUniform(
const int n,
const T a,
const T b, T* r,
184 template <
typename T,
class Context>
185 void RandUniformUnique(
194 template <
typename T,
class Context>
203 template <
typename T,
class Context>
204 void Dot(
const int N,
const T* a,
const T* b, T* y, Context* context);
207 template <
typename T,
class Context>
208 void Sum(
const int N,
const T* x, T* y, Context* context);
211 template <
typename T,
class Context>
212 void SumSqr(
const int N,
const T* x, T* y, Context* context);
216 template <
typename T,
class Context>
217 void Select(
const int N,
const int D,
const T* x,
const int* idx, T* y,
220 template <
typename T,
class Context>
221 void Scale(
const int N,
const T alpha,
const T* x, T* y, Context* context);
226 template <
typename T,
class Context>
227 void Scale(
const int N,
const T* alpha,
const T* x, T* y,
230 template <
typename T,
class Context>
231 void Axpy(
const int N,
const T alpha,
const T* x, T* y, Context* context);
236 template <
typename T,
class Context>
237 void Axpy(
const int N,
const T* alpha,
const T* x, T* y,
240 template <
typename T,
class Context>
241 void Axpby(
const int N,
const T alpha,
const T* x,
const T b, T* y,
244 template <
typename T,
class Context,
int order>
248 const int* col_shape,
251 const int* kernel_shape,
258 bool accumulate_output =
false);
260 template <
typename T,
class Context,
int order>
263 const int* img_shape,
264 const int* col_shape,
267 const int* kernel_shape,
275 template <
typename T,
class Context,
int order>
283 const int dilation_h,
284 const int dilation_w,
294 template <
typename T,
class Context,
int order>
302 const int dilation_h,
303 const int dilation_w,
315 template <
typename T,
class Context>
318 const int bias_channels,
319 const int image_size,
323 template <
class Context>
324 void CopyMatrix(
const size_t item_size,
const int M,
const int N,
const void* A,
325 const int lda,
void* B,
const int ldb, Context* context);
328 uint32_t randomNumberSeed();
338 inline bool is_a_ge_zero_and_a_lt_b(
int a,
int b) {
339 return static_cast<unsigned>(a) < static_cast<unsigned>(b);
344 template <
typename T>
345 inline T divUp(T a, T b) {
346 return (a + b - (T) 1) / b;
352 template <
typename T>
353 inline T roundUp(T a, T b) {
354 return divUp<T>(a, b) * b;
361 #include "caffe2/utils/math-detail.h" 362 #endif // CAFFE2_UTILS_MATH_H_
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...