-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
1,174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef CAFFE2_UTILS_MATH_BROADCAST_H_ | ||
#define CAFFE2_UTILS_MATH_BROADCAST_H_ | ||
|
||
#include "caffe2/core/common.h" | ||
#include "caffe2/core/types.h" | ||
|
||
namespace caffe2 { | ||
namespace math { | ||
|
||
template <typename T, class Context, StorageOrder kOrder> | ||
CAFFE2_API void AffineChannel( | ||
const int N, | ||
const int C, | ||
const int HxW, | ||
const T* X, | ||
const T* scale, | ||
const T* bias, | ||
T* Y, | ||
Context* context); | ||
|
||
} // namespace math | ||
} // namespace caffe2 | ||
|
||
#endif // CAFFE2_UTILS_MATH_BROADCAST_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
#ifndef CAFFE2_UTILS_MATH_ELEMENTWISE_H_ | ||
#define CAFFE2_UTILS_MATH_ELEMENTWISE_H_ | ||
|
||
#include "caffe2/core/common.h" | ||
#include "caffe2/core/types.h" | ||
|
||
namespace caffe2 { | ||
namespace math { | ||
|
||
template <typename T, class Context> | ||
CAFFE2_API void Exp(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Log(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Sin(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Asin(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Cos(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Acos(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Tan(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Atan(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Sinh(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Cosh(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void SinCos(int N, const T* X, T* S, T* C, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Tanh(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Abs(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Sqr(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Sqrt(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Rsqrt(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Cube(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Cbrt(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Neg(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Sign(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Not(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Powx(int N, const T* A, const T b, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Inv(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Erf(int N, const T* X, T* Y, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void CdfNorm(int N, const T* X, T* Y, Context* context); | ||
|
||
template <typename T, class Context> | ||
CAFFE2_API void Set(std::int64_t N, T alpha, T* X, Context* context); | ||
|
||
template <typename TAlpha, typename TData, class Context> | ||
CAFFE2_API void | ||
Scale(int N, TAlpha alpha, const TData* X, TData* Y, Context* context); | ||
|
||
// Different from the Scale function above, if alpha is passed in as a pointer, | ||
// we will assume that it lives on the Context device, for example on GPU. | ||
template <typename TAlpha, typename TData, class Context> | ||
CAFFE2_API void | ||
Scale(int N, const TAlpha* alpha, const TData* X, TData* Y, Context* context); | ||
|
||
template <typename T, class Context> | ||
CAFFE2_API void Add(int N, const T* A, const T* B, T* C, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Sub(int N, const T* A, const T* B, T* C, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Mul(int N, const T* A, const T* B, T* C, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Div(int N, const T* A, const T* B, T* C, Context* context); | ||
|
||
template <typename T, class Context> | ||
CAFFE2_API void Min(int N, const T* A, const T* B, T* C, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Max(int N, const T* A, const T* B, T* C, Context* context); | ||
|
||
template <typename T, class Context> | ||
CAFFE2_API void And(int N, const T* A, const T* B, T* C, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Or(int N, const T* A, const T* B, T* C, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void Xor(int N, const T* A, const T* B, T* C, Context* context); | ||
|
||
template <typename T, class Context> | ||
CAFFE2_API void | ||
BitwiseAnd(int N, const T* A, const T* B, T* C, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void | ||
BitwiseOr(int N, const T* A, const T* B, T* C, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void | ||
BitwiseXor(int N, const T* A, const T* B, T* C, Context* context); | ||
|
||
template <typename T, class Context> | ||
CAFFE2_API void EQ(int N, const T* A, const T* B, bool* C, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void NE(int N, const T* A, const T* B, bool* C, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void LT(int N, const T* A, const T* B, bool* C, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void LE(int N, const T* A, const T* B, bool* C, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void GT(int N, const T* A, const T* B, bool* C, Context* context); | ||
template <typename T, class Context> | ||
CAFFE2_API void GE(int N, const T* A, const T* B, bool* C, Context* context); | ||
|
||
template <typename TAlpha, typename TData, class Context> | ||
CAFFE2_API void | ||
Axpy(std::int64_t N, TAlpha alpha, const TData* X, TData* Y, Context* context); | ||
|
||
// Different from the Axpy function above, if alpha is passed in | ||
// as a pointer, we will assume that it lives on the Context device, | ||
// for example on GPU. | ||
template <typename TAlpha, typename TData, class Context> | ||
CAFFE2_API void Axpy( | ||
std::int64_t N, | ||
const TAlpha* alpha, | ||
const TData* X, | ||
TData* Y, | ||
Context* context); | ||
|
||
template <typename TAlpha, typename TData, class Context> | ||
CAFFE2_API void Axpby( | ||
std::int64_t N, | ||
TAlpha alpha, | ||
const TData* X, | ||
TAlpha beta, | ||
TData* Y, | ||
Context* context); | ||
|
||
template <typename TAlpha, typename TData, class Context> | ||
CAFFE2_API void Axpby( | ||
std::int64_t N, | ||
const TAlpha* alpha, | ||
const TData* X, | ||
const TAlpha* beta, | ||
TData* Y, | ||
Context* context); | ||
|
||
} // namespace math | ||
} // namespace caffe2 | ||
|
||
#endif // CAFFE2_UTILS_MATH_ELEMENTWISE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#ifndef CAFFE2_UTILS_MATH_HALF_UTILS_H_ | ||
#define CAFFE2_UTILS_MATH_HALF_UTILS_H_ | ||
|
||
#include "caffe2/core/common.h" | ||
#include "caffe2/core/types.h" | ||
#include "caffe2/utils/conversions.h" | ||
#include "caffe2/utils/math/utils.h" | ||
|
||
namespace caffe2 { | ||
namespace math { | ||
namespace utils { | ||
|
||
struct HalfAddFunctor { | ||
MATH_UTILS_DECL at::Half operator()(const at::Half a, const at::Half b) | ||
const { | ||
return convert::To<float, at::Half>( | ||
convert::To<at::Half, float>(a) + convert::To<at::Half, float>(b)); | ||
} | ||
}; | ||
|
||
struct HalfSubFunctor { | ||
MATH_UTILS_DECL at::Half operator()(const at::Half a, const at::Half b) | ||
const { | ||
return convert::To<float, at::Half>( | ||
convert::To<at::Half, float>(a) - convert::To<at::Half, float>(b)); | ||
} | ||
}; | ||
|
||
struct HalfMulFunctor { | ||
MATH_UTILS_DECL at::Half operator()(const at::Half a, const at::Half b) | ||
const { | ||
return convert::To<float, at::Half>( | ||
convert::To<at::Half, float>(a) * convert::To<at::Half, float>(b)); | ||
} | ||
}; | ||
|
||
struct HalfDivFunctor { | ||
MATH_UTILS_DECL at::Half operator()(const at::Half a, const at::Half b) | ||
const { | ||
return convert::To<float, at::Half>( | ||
convert::To<at::Half, float>(a) / convert::To<at::Half, float>(b)); | ||
} | ||
}; | ||
|
||
} // namespace utils | ||
} // namespace math | ||
} // namespace caffe2 | ||
|
||
#endif // CAFFE2_UTILS_MATH_HALF_UTILS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#ifndef CAFFE2_UTILS_MATH_REDUCE_H_ | ||
#define CAFFE2_UTILS_MATH_REDUCE_H_ | ||
|
||
#include "caffe2/core/common.h" | ||
#include "caffe2/core/types.h" | ||
|
||
namespace caffe2 { | ||
|
||
class Tensor; | ||
|
||
namespace math { | ||
|
||
template <typename T, class Context> | ||
CAFFE2_API void | ||
ReduceMin(const int N, const T* X, T* y, Tensor* scratch_ptr, Context* context); | ||
|
||
template <typename T, class Context> | ||
CAFFE2_API void | ||
ReduceMax(const int N, const T* X, T* y, Tensor* scratch_ptr, Context* context); | ||
|
||
// In all of the reduce functions, X_dims and Y_dims should have ndim elements. | ||
// Each dimension of Y_dims must match the corresponding dimension of X_dims or | ||
// must be equal to 1. The dimensions equal to 1 indicate the dimensions of X to | ||
// be reduced. | ||
|
||
// Y = alpha * ReduceMin(X) | ||
template <typename T, class Context> | ||
CAFFE2_API void ReduceMin( | ||
const int ndim, | ||
const int* X_dims, | ||
const int* Y_dims, | ||
const T alpha, | ||
const T* X, | ||
T* Y, | ||
Context* context); | ||
|
||
// Y = alpha * ReduceMax(X) | ||
template <typename T, class Context> | ||
CAFFE2_API void ReduceMax( | ||
const int ndim, | ||
const int* X_dims, | ||
const int* Y_dims, | ||
const T alpha, | ||
const T* X, | ||
T* Y, | ||
Context* context); | ||
|
||
// Y = alpha * ReduceSum(X) | ||
template <typename T, class Context> | ||
CAFFE2_API void ReduceSum( | ||
const int ndim, | ||
const int* X_dims, | ||
const int* Y_dims, | ||
const T alpha, | ||
const T* X, | ||
T* Y, | ||
Context* context); | ||
|
||
// Y = alpha * ReduceMean(X) | ||
template <typename T, class Context> | ||
CAFFE2_API void ReduceMean( | ||
const int ndim, | ||
const int* X_dims, | ||
const int* Y_dims, | ||
const T alpha, | ||
const T* X, | ||
T* Y, | ||
Context* context); | ||
|
||
// Y = alpha * ReduceL1(X) | ||
template <typename T, class Context> | ||
CAFFE2_API void ReduceL1( | ||
const int ndim, | ||
const int* X_dims, | ||
const int* Y_dims, | ||
const T alpha, | ||
const T* X, | ||
T* Y, | ||
Context* context); | ||
|
||
// Y = alpha * ReduceL2(X) | ||
template <typename T, class Context> | ||
CAFFE2_API void ReduceL2( | ||
const int ndim, | ||
const int* X_dims, | ||
const int* Y_dims, | ||
const T alpha, | ||
const T* X, | ||
T* Y, | ||
Context* context); | ||
|
||
// Computes mean and variance over axes. | ||
template <typename T, class Context> | ||
CAFFE2_API void Moments( | ||
const int ndims, | ||
const int* X_dims, | ||
const int* Y_dims, | ||
const T* X, | ||
T* mean, | ||
T* var, | ||
Context* context); | ||
|
||
} // namespace math | ||
|
||
} // namespace caffe2 | ||
|
||
#endif // CAFFE2_UTILS_MATH_REDUCE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef CAFFE2_UTILS_MATH_TRANSPOSE_H_ | ||
#define CAFFE2_UTILS_MATH_TRANSPOSE_H_ | ||
|
||
#include "caffe2/core/common.h" | ||
#include "caffe2/core/types.h" | ||
|
||
namespace caffe2 { | ||
namespace math { | ||
|
||
// Transpose tensor X with dims by axes and write the result to tensor Y. | ||
template <typename TIndex, typename TData, class Context> | ||
CAFFE2_API void Transpose( | ||
int ndim, | ||
const TIndex* dims, | ||
const int* axes, | ||
const TData* X, | ||
TData* Y, | ||
Context* context); | ||
|
||
template <typename T, class Context> | ||
CAFFE2_API void | ||
NCHW2NHWC(int N, int C, int HxW, const T* X, T* Y, Context* context); | ||
|
||
template <typename T, class Context> | ||
CAFFE2_API void | ||
NHWC2NCHW(int N, int C, int HxW, const T* X, T* Y, Context* context); | ||
|
||
} // namespace math | ||
} // namespace caffe2 | ||
|
||
#endif // CAFFE2_UTILS_MATH_TRANSPOSE_H_ |
Oops, something went wrong.