Skip to content

Commit

Permalink
LinalgRefactor - specialpurpose (#3767)
Browse files Browse the repository at this point in the history
* Linalg special purpose - CPU
* Linalg special purpose - GPU
* Format check
* rm old special purpose
  • Loading branch information
OXPHOS committed Jun 27, 2017
1 parent a68fc6a commit 6b0fd29
Show file tree
Hide file tree
Showing 15 changed files with 1,341 additions and 1,281 deletions.
88 changes: 88 additions & 0 deletions src/shogun/mathematics/linalg/LinalgBackendBase.h
Expand Up @@ -180,6 +180,21 @@ namespace shogun
DEFINE_FOR_NON_INTEGER_PTYPE(BACKEND_GENERIC_CHOLESKY_SOLVER, SGMatrix)
#undef BACKEND_GENERIC_CHOLESKY_SOLVER

/**
* Wrapper method of cross entropy.
*
* @see linalg::cross_entropy
*/
#define BACKEND_GENERIC_CROSS_ENTROPY(Type, Container) \
virtual Type cross_entropy( \
const Container<Type>& P, const Container<Type>& Q) const \
{ \
SG_SNOTIMPLEMENTED; \
return 0; \
}
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_CROSS_ENTROPY, SGMatrix)
#undef BACKEND_GENERIC_CROSS_ENTROPY

/**
* Wrapper method of vector dot-product that works with generic vectors.
*
Expand Down Expand Up @@ -347,6 +362,37 @@ namespace shogun
BACKEND_GENERIC_COMPLEX_MEAN(SGMatrix)
#undef BACKEND_GENERIC_COMPLEX_MEAN

/**
* Wrapper method of multiply_by_logistic_derivative function f(x) =
* 1/(1+exp(-x))
*
* @see linalg::multiply_by_logistic_derivative
*/
#define BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV(Type, Container) \
virtual void multiply_by_logistic_derivative( \
Container<Type>& a, Container<Type>& result) const \
{ \
SG_SNOTIMPLEMENTED; \
}
DEFINE_FOR_ALL_PTYPE(
BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV, SGMatrix)
#undef BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV

/**
* Wrapper method of multiply_by_rectified_linear_derivative
*
* @see linalg::multiply_by_rectified_linear_derivative
*/
#define BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV(Type, Container) \
virtual void multiply_by_rectified_linear_derivative( \
Container<Type>& a, Container<Type>& result) const \
{ \
SG_SNOTIMPLEMENTED; \
}
DEFINE_FOR_ALL_PTYPE(
BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV, SGMatrix)
#undef BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV

/**
* Wrapper method that range fills a vector of matrix.
*
Expand All @@ -361,6 +407,20 @@ namespace shogun
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_RANGE_FILL, SGMatrix)
#undef BACKEND_GENERIC_RANGE_FILL

/**
* Wrapper method of rectified_linear method f(x) = max(0, x)
*
* @see linalg::rectified_linear
*/
#define BACKEND_GENERIC_RECTIFIED_LINEAR(Type, Container) \
virtual void rectified_linear(Container<Type>& a, Container<Type>& result) \
const \
{ \
SG_SNOTIMPLEMENTED; \
}
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_RECTIFIED_LINEAR, SGMatrix)
#undef BACKEND_GENERIC_RECTIFIED_LINEAR

/**
* Wrapper method that solves a system of linear equations
* using QR decomposition.
Expand Down Expand Up @@ -422,6 +482,34 @@ namespace shogun
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_SUM, SGMatrix)
#undef BACKEND_GENERIC_SUM

/**
* Wrapper method of softmax method.
*
* @see linalg::softmax
*/
#define BACKEND_GENERIC_SOFTMAX(Type, Container) \
virtual void softmax(Container<Type>& a) const \
{ \
SG_SNOTIMPLEMENTED; \
}
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_SOFTMAX, SGMatrix)
#undef BACKEND_GENERIC_SOFTMAX

/**
* Wrapper method of squared error method.
*
* @see linalg::squared_error
*/
#define BACKEND_GENERIC_SQUARED_ERROR(Type, Container) \
virtual Type squared_error( \
const Container<Type>& P, const Container<Type>& Q) const \
{ \
SG_SNOTIMPLEMENTED; \
return 0; \
}
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_SQUARED_ERROR, SGMatrix)
#undef BACKEND_GENERIC_SQUARED_ERROR

/**
* Wrapper method of sum that works with matrix blocks.
*
Expand Down
84 changes: 84 additions & 0 deletions src/shogun/mathematics/linalg/LinalgBackendEigen.h
Expand Up @@ -33,6 +33,7 @@
#ifndef LINALG_BACKEND_EIGEN_H__
#define LINALG_BACKEND_EIGEN_H__

#include <numeric>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/eigen3.h>
#include <shogun/mathematics/linalg/LinalgBackendBase.h>
Expand Down Expand Up @@ -91,6 +92,14 @@ namespace shogun
DEFINE_FOR_NON_INTEGER_PTYPE(BACKEND_GENERIC_CHOLESKY_SOLVER, SGMatrix)
#undef BACKEND_GENERIC_CHOLESKY_SOLVER

/** Implementation of @see linalg::cross_entropy */
#define BACKEND_GENERIC_CROSS_ENTROPY(Type, Container) \
virtual Type cross_entropy( \
const Container<Type>& P, const Container<Type>& Q) const;
DEFINE_FOR_NON_INTEGER_REAL_PTYPE(
BACKEND_GENERIC_CROSS_ENTROPY, SGMatrix)
#undef BACKEND_GENERIC_CROSS_ENTROPY

/** Implementation of @see LinalgBackendBase::dot */
#define BACKEND_GENERIC_DOT(Type, Container) \
virtual Type dot(const Container<Type>& a, const Container<Type>& b) const;
Expand Down Expand Up @@ -173,6 +182,22 @@ namespace shogun
BACKEND_GENERIC_COMPLEX_MEAN(SGMatrix)
#undef BACKEND_GENERIC_COMPLEX_MEAN

/** Implementation of @see linalg::multiply_by_logistic_derivative */
#define BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV(Type, Container) \
virtual void multiply_by_logistic_derivative( \
Container<Type>& a, Container<Type>& result) const;
DEFINE_FOR_NUMERIC_PTYPE(
BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV, SGMatrix)
#undef BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV

/** Implementation of @see linalg::multiply_by_rectified_linear_derivative */
#define BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV(Type, Container) \
virtual void multiply_by_rectified_linear_derivative( \
Container<Type>& a, Container<Type>& result) const;
DEFINE_FOR_NON_INTEGER_REAL_PTYPE(
BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV, SGMatrix)
#undef BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV

/** Implementation of @see LinalgBackendBase::qr_solver */
#define BACKEND_GENERIC_QR_SOLVER(Type, Container) \
virtual Container<Type> qr_solver( \
Expand All @@ -188,6 +213,13 @@ namespace shogun
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_RANGE_FILL, SGMatrix)
#undef BACKEND_GENERIC_RANGE_FILL

/** Implementation of @see linalg::rectified_linear */
#define BACKEND_GENERIC_RECTIFIED_LINEAR(Type, Container) \
virtual void rectified_linear(Container<Type>& a, Container<Type>& result) \
const;
DEFINE_FOR_REAL_PTYPE(BACKEND_GENERIC_RECTIFIED_LINEAR, SGMatrix)
#undef BACKEND_GENERIC_RECTIFIED_LINEAR

/** Implementation of @see linalg::scale */
#define BACKEND_GENERIC_IN_PLACE_SCALE(Type, Container) \
virtual void scale( \
Expand All @@ -203,6 +235,20 @@ namespace shogun
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_SET_CONST, SGMatrix)
#undef BACKEND_GENERIC_SET_CONST

/** Implementation of @see linalg::softmax */
#define BACKEND_GENERIC_SOFTMAX(Type, Container) \
virtual void softmax(Container<Type>& a) const;
DEFINE_FOR_NON_INTEGER_REAL_PTYPE(BACKEND_GENERIC_SOFTMAX, SGMatrix)
#undef BACKEND_GENERIC_SOFTMAX

/** Implementation of @see linalg::squared_error */
#define BACKEND_GENERIC_SQUARED_ERROR(Type, Container) \
virtual Type squared_error( \
const Container<Type>& P, const Container<Type>& Q) const;
DEFINE_FOR_NON_INTEGER_REAL_PTYPE(
BACKEND_GENERIC_SQUARED_ERROR, SGMatrix)
#undef BACKEND_GENERIC_SQUARED_ERROR

/** Implementation of @see LinalgBackendBase::sum */
#define BACKEND_GENERIC_SUM(Type, Container) \
virtual Type sum(const Container<Type>& a, bool no_diag) const;
Expand Down Expand Up @@ -348,6 +394,13 @@ namespace shogun
SGVector<T> cholesky_solver_impl(
const SGMatrix<T>& L, const SGVector<T>& b, const bool lower) const;

/** Eigen3 cross_entropy method
* The cross entropy is defined as \f$ H(P,Q) = - \sum_{ij}
* P[i,j]log(Q[i,j]) \f$
*/
template <typename T>
T cross_entropy_impl(const SGMatrix<T>& p, const SGMatrix<T>& q) const;

/** Eigen3 vector dot-product method */
template <typename T>
T dot_impl(const SGVector<T>& a, const SGVector<T>& b) const;
Expand Down Expand Up @@ -422,6 +475,22 @@ namespace shogun
template <template <typename> class Container>
complex128_t mean_impl(const Container<complex128_t>& a) const;

/** Eigen3 multiply_by_logistic_derivative method
* Performs the operation C(i,j) = C(i,j) * A(i,j) * (1.0-A(i,j)) for
* all i
* and j
*/
template <typename T>
void multiply_by_logistic_derivative_impl(
SGMatrix<T>& a, SGMatrix<T>& result) const;

/** Eigen3 multiply_by_rectified_linear_derivative method
* Performs the operation C(i,j) = C(i,j) * (A(i,j)!=0) for all i and j
*/
template <typename T>
void multiply_by_rectified_linear_derivative_impl(
SGMatrix<T>& a, SGMatrix<T>& result) const;

/** Eigen3 vector QR solver. */
template <typename T>
SGVector<T>
Expand All @@ -436,6 +505,10 @@ namespace shogun
template <typename T, template <typename> class Container>
void range_fill_impl(Container<T>& a, const T start) const;

/** Applies the elementwise rectified linear function f(x) = max(0,x) */
template <typename T>
void rectified_linear_impl(SGMatrix<T>& a, SGMatrix<T>& result) const;

/** Eigen3 vector inplace scale method: result = alpha * A */
template <typename T>
void scale_impl(SGVector<T>& a, T alpha, SGVector<T>& result) const;
Expand All @@ -448,6 +521,17 @@ namespace shogun
template <typename T, template <typename> class Container>
void set_const_impl(Container<T>& a, T value) const;

/** Eigen3 softmax method */
template <typename T, template <typename> class Container>
void softmax_impl(Container<T>& a) const;

/** Eigen3 squared error method
* The squared error is defined as \f$ E(P,Q) = \frac{1}{2} \sum_{ij}
* (P[i,j]-Q[i,j])^2 \f$
*/
template <typename T>
T squared_error_impl(const SGMatrix<T>& p, const SGMatrix<T>& q) const;

/** Eigen3 vector sum method */
template <typename T>
T sum_impl(const SGVector<T>& vec, bool no_diag = false) const;
Expand Down
62 changes: 32 additions & 30 deletions src/shogun/mathematics/linalg/LinalgBackendGPUBase.h
Expand Up @@ -49,41 +49,43 @@ namespace shogun
class LinalgBackendGPUBase : public LinalgBackendBase
{
public:
#define DEFINE_FOR_ALL_PTYPE(METHODNAME, Container) \
METHODNAME(char, Container); \
METHODNAME(uint8_t, Container); \
METHODNAME(int16_t, Container); \
METHODNAME(uint16_t, Container); \
METHODNAME(int32_t, Container); \
METHODNAME(uint32_t, Container); \
METHODNAME(float32_t, Container); \
METHODNAME(float64_t, Container);
// clang-format off
#define DEFINE_FOR_ALL_PTYPE(METHODNAME, Container) \
METHODNAME(char, Container); \
METHODNAME(uint8_t, Container); \
METHODNAME(int16_t, Container); \
METHODNAME(uint16_t, Container); \
METHODNAME(int32_t, Container); \
METHODNAME(uint32_t, Container); \
METHODNAME(float32_t, Container); \
METHODNAME(float64_t, Container); \

/**
* Wrapper method of Transferring data to GPU memory.
*
* @see LinalgBackendBase::to_gpu
*/
#define BACKEND_GENERIC_TO_GPU(Type, Container) \
virtual GPUMemoryBase<Type>* to_gpu(const Container<Type>&) const = 0;
/**
* Wrapper method of Transferring data to GPU memory.
*
* @see LinalgBackendBase::to_gpu
*/
#define BACKEND_GENERIC_TO_GPU(Type, Container) \
virtual GPUMemoryBase<Type>* to_gpu(const Container<Type>&) const = 0;\

DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_TO_GPU, SGVector)
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_TO_GPU, SGMatrix)
#undef BACKEND_GENERIC_TO_GPU
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_TO_GPU, SGVector)
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_TO_GPU, SGMatrix)
#undef BACKEND_GENERIC_TO_GPU

/**
* Wrapper method of fetching data from GPU memory.
*
* @see LinalgBackendBase::from_gpu
*/
#define BACKEND_GENERIC_FROM_GPU(Type, Container) \
virtual void from_gpu(const Container<Type>&, Type* data) const = 0;
/**
* Wrapper method of fetching data from GPU memory.
*
* @see LinalgBackendBase::from_gpu
*/
#define BACKEND_GENERIC_FROM_GPU(Type, Container) \
virtual void from_gpu(const Container<Type>&, Type* data) const = 0;\

DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_FROM_GPU, SGVector)
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_FROM_GPU, SGMatrix)
#undef BACKEND_GENERIC_FROM_GPU
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_FROM_GPU, SGVector)
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_FROM_GPU, SGMatrix)
#undef BACKEND_GENERIC_FROM_GPU

#undef DEFINE_FOR_ALL_PTYPE
#undef DEFINE_FOR_ALL_PTYPE
// clang-format on
};
}

Expand Down

0 comments on commit 6b0fd29

Please sign in to comment.