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" 26 template <
class Context>
36 using EigenMatrixMap =
37 Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >;
40 Eigen::Map<Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic> >;
42 using EigenVectorMap = Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1> >;
44 using EigenVectorArrayMap = Eigen::Map<Eigen::Array<T, Eigen::Dynamic, 1> >;
46 using ConstEigenMatrixMap =
47 Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >;
49 using ConstEigenArrayMap =
50 Eigen::Map<const Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic> >;
52 using ConstEigenVectorMap =
53 Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1> >;
55 using ConstEigenVectorArrayMap =
56 Eigen::Map<const Eigen::Array<T, Eigen::Dynamic, 1> >;
61 template <
typename T,
class Context>
62 void Exp(
const int N,
const T* x, T* y, Context* context);
63 template <
typename T,
class Context>
64 void Log(
const int N,
const T* x, T* y, Context* context);
65 template <
typename T,
class Context>
66 void Cos(
const int N,
const T* x, T* y, Context* context);
67 template <
typename T,
class Context>
68 void Sin(
const int N,
const T* x, T* y, Context* context);
69 template <
typename T,
class Context>
70 void SinCos(
const int N,
const T* x, T* ys, T* yc, Context* context);
71 template <
typename T,
class Context>
72 void Abs(
const int N,
const T* x, T* y, Context* context);
73 template <
typename T,
class Context>
74 void Sqrt(
const int N,
const T* x, T* y, Context* context);
75 template <
typename T,
class Context>
76 void InvSqrt(
const int N,
const T* x, T* y, Context* context);
77 template <
typename T,
class Context>
78 void Sqr(
const int N,
const T* x, T* y, Context* context);
80 template <
typename T,
class Context>
81 void Not(
const int N,
const T* x, T* y, Context* context);
83 template <
typename T,
class Context>
84 void Powx(
const int N,
const T* a,
const T b, T* y, Context* context);
86 #define CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(name) \ 87 template <typename T, class Context> \ 88 void name(const int N, const T* a, const T* b, bool* y, Context* context); \ 89 template <typename T, class Context> \ 98 CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(LT);
99 CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(LE);
100 CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(GT);
101 CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(GE);
103 CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(And);
104 CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(Or);
105 CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT(Xor);
107 #undef CAFFE2_DECLARE_BINARY_OP_BINARY_RESULT 109 #define CAFFE2_DECLARE_BINARY_OP(name) \ 110 template <typename T, class Context> \ 111 void name(const int N, const T* a, const T* b, T* y, Context* context); \ 112 template <typename T, class Context> \ 120 template <typename T, class Context> \ 122 const int M, const int N, const T* x, T* y, Context* context); \ 123 template <typename T, class Context> \ 125 const int M, const int N, const T* x, T* y, Context* context); 127 CAFFE2_DECLARE_BINARY_OP(Add);
128 CAFFE2_DECLARE_BINARY_OP(Sub);
129 CAFFE2_DECLARE_BINARY_OP(Mul);
130 CAFFE2_DECLARE_BINARY_OP(Div);
132 #undef CAFFE2_DECLARE_BINARY_OP 134 template <
typename T,
class Context>
141 template <
typename T,
class Context>
151 template <
typename T,
class Context>
152 void AddStripedBatch(
162 template <
typename T,
class Context>
163 void RowwiseMax(
const int N,
const int D,
const T* x, T* y,
168 template <
typename T,
class Context>
169 void ColwiseMax(
const int N,
const int D,
const T* x, T* y,
173 template <
typename T,
class Context>
174 void ElemwiseMax(
const int N,
const T* x,
const T* y, T* z, Context* context);
177 template <
typename T,
class Context>
187 template <
typename T,
class Context>
200 template <
typename T,
class Context,
class Engine = DefaultEngine>
202 const CBLAS_TRANSPOSE TransA,
203 const CBLAS_TRANSPOSE TransB,
213 TensorProto::DataType math_type = TensorProto_DataType_FLOAT);
217 template <
typename T,
class Context,
class Engine = DefaultEngine>
219 const CBLAS_TRANSPOSE TransA,
220 const CBLAS_TRANSPOSE TransB,
235 template <
typename T,
class Context,
class Engine = DefaultEngine>
237 const CBLAS_TRANSPOSE TransA,
238 const CBLAS_TRANSPOSE TransB,
239 const int batch_size,
250 TensorProto::DataType math_type = TensorProto_DataType_FLOAT);
256 template <
typename T,
class Context,
class Engine = DefaultEngine>
258 const CBLAS_TRANSPOSE TransA,
267 TensorProto::DataType math_type = TensorProto_DataType_FLOAT);
269 template <
typename T,
class Context>
270 void Set(
const size_t N,
const T alpha, T* X, Context* context);
272 template <
typename T,
class Context>
273 void RandUniform(
const size_t n,
const T a,
const T b, T* r, Context* context);
275 template <
typename T,
class Context>
276 void RandUniformUnique(
285 template <
typename T,
class Context>
294 template <
typename T,
class Context>
295 void Dot(
const int N,
const T* a,
const T* b, T* y, Context* context);
298 template <
typename T,
class Context>
299 void Sum(
const int N,
const T* x, T* y, Context* context,
303 template <
typename T,
class Context>
313 template <
typename T,
class Context>
314 void Select(
const int N,
const int D,
const T* x,
const int* idx, T* y,
317 template <
typename T,
class Context>
318 void Scale(
const int N,
const float alpha,
const T* x, T* y, Context* context);
323 template <
typename T,
class Context>
324 void Scale(
const int N,
const float* alpha,
const T* x, T* y, Context* context);
326 template <
typename T,
class Context>
327 void Axpy(
const int N,
const float alpha,
const T* x, T* y, Context* context);
332 template <
typename T,
class Context>
333 void Axpy(
const int N,
const float* alpha,
const T* x, T* y, Context* context);
335 template <
typename T,
class Context>
344 template <
typename T,
class Context,
int order>
348 const int* col_shape,
351 const int* kernel_shape,
358 bool accumulate_output =
false);
360 template <
typename T,
class Context,
int order>
363 const int* img_shape,
364 const int* col_shape,
367 const int* kernel_shape,
375 template <
typename T,
class Context,
int order>
383 const int dilation_h,
384 const int dilation_w,
394 template <
typename T,
class Context,
int order>
402 const int dilation_h,
403 const int dilation_w,
415 template <
typename T,
class Context>
418 const int bias_channels,
419 const int image_size,
423 template <
class Context>
425 const size_t item_size,
433 TypeMeta::TypedCopy copy =
nullptr);
435 template <
typename T,
class Context>
436 void CopyVector(
const int N,
const T* A, T* B, Context* context);
446 inline bool is_a_ge_zero_and_a_lt_b(
int a,
int b) {
447 return static_cast<unsigned>(a) < static_cast<unsigned>(b);
452 template <
typename T>
453 constexpr T divUp(T a, T b) {
454 return (a + b - (T) 1) / b;
460 template <
typename T>
461 constexpr T roundUp(T a, T b) {
462 return divUp<T>(a, b) * b;
466 template <
typename T>
467 constexpr
int integerLog2(T n,
int p = 0) {
468 return (n <= 1) ? p : integerLog2(n / 2, p + 1);
472 template <
typename T>
473 constexpr T integerNextHighestPowerOf2(T v) {
474 return (integerIsPowerOf2(v) ? (T)2 * v : ((T)1 << (integerLog2(v) + 1)));
480 #include "caffe2/utils/math-detail.h" 481 #endif // CAFFE2_UTILS_MATH_H_
Tensor is the basic class in Caffe2 that stores a contiguous memory with its shape information...
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...