You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by ta...@apache.org on 2018/12/14 02:54:19 UTC
[incubator-mxnet] branch master updated: support mkl log when dtype
is fp32 or fp64 (#13150)
This is an automated email from the ASF dual-hosted git repository.
taolv pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git
The following commit(s) were added to refs/heads/master by this push:
new 30655f9 support mkl log when dtype is fp32 or fp64 (#13150)
30655f9 is described below
commit 30655f9ef58daa778d6ea049940068f0ff9a21bf
Author: XiaotaoChen <ch...@gmail.com>
AuthorDate: Fri Dec 14 10:54:03 2018 +0800
support mkl log when dtype is fp32 or fp64 (#13150)
* support mkl log when dtype is fp32 or fp64
* remove macro
* ensure data size less than or equal MKL_INT_MAX
* code specification
* fix indent
* for retrigger
---
src/operator/tensor/elemwise_unary_op.h | 41 ++++++++++++++++++++++++++
src/operator/tensor/elemwise_unary_op_basic.cc | 3 +-
2 files changed, 43 insertions(+), 1 deletion(-)
diff --git a/src/operator/tensor/elemwise_unary_op.h b/src/operator/tensor/elemwise_unary_op.h
index 83b86bf..8d5ad05 100644
--- a/src/operator/tensor/elemwise_unary_op.h
+++ b/src/operator/tensor/elemwise_unary_op.h
@@ -29,11 +29,15 @@
#include <vector>
#include <utility>
#include <algorithm>
+#include <climits>
#include "./cast_storage-inl.h"
#include "../mshadow_op.h"
#include "../mxnet_op.h"
#include "../elemwise_op_common.h"
#include "../../ndarray/ndarray_function.h"
+#if MSHADOW_USE_MKL == 1
+#include "mkl.h"
+#endif
namespace mxnet {
namespace op {
@@ -348,6 +352,43 @@ class UnaryOp : public OpBase {
LogUnimplementedOp(attrs, ctx, inputs, req, outputs);
}
}
+
+#if MSHADOW_USE_MKL == 1
+ static inline void MKLLog(MKL_INT size, const float* pIn, float* pOut) {
+ vsLn(size, pIn, pOut);
+ }
+
+ static inline void MKLLog(MKL_INT size, const double* pIn, double* pOut) {
+ vdLn(size, pIn, pOut);
+ }
+#endif
+
+ template<typename xpu, typename OP>
+ static void LogCompute(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector<TBlob>& inputs,
+ const std::vector<OpReqType>& req,
+ const std::vector<TBlob>& outputs) {
+ if (req[0] == kNullOp) return;
+ // if defined MSHADOW_USE_MKL then call mkl log when req is KWriteTo, type_flag
+ // is mshadow::kFloat32 or mshadow::kFloat64 and data size less than or equal MKL_INT_MAX
+#if MSHADOW_USE_MKL == 1
+ auto type_flag = inputs[0].type_flag_;
+ const size_t MKL_INT_MAX = (sizeof(MKL_INT) == sizeof(int)) ? INT_MAX : LLONG_MAX;
+ size_t input_size = inputs[0].Size();
+ if (req[0] == kWriteTo &&
+ input_size <= MKL_INT_MAX &&
+ (type_flag == mshadow::kFloat32 || type_flag == mshadow::kFloat64)) {
+ MSHADOW_SGL_DBL_TYPE_SWITCH(type_flag, DType, {
+ MKLLog(input_size, inputs[0].dptr<DType>(), outputs[0].dptr<DType>());
+ });
+ } else {
+ Compute<xpu, OP>(attrs, ctx, inputs, req, outputs);
+ }
+#else
+ Compute<xpu, OP>(attrs, ctx, inputs, req, outputs);
+#endif
+ }
};
/*! \brief Map legacy unary_bwd to backward_grad */
diff --git a/src/operator/tensor/elemwise_unary_op_basic.cc b/src/operator/tensor/elemwise_unary_op_basic.cc
index 301fc48..9730d00 100644
--- a/src/operator/tensor/elemwise_unary_op_basic.cc
+++ b/src/operator/tensor/elemwise_unary_op_basic.cc
@@ -940,7 +940,7 @@ The storage type of ``exp`` output is always dense
.set_attr<nnvm::FGradient>("FGradient", ElemwiseGradUseOut{"_mul"});
// log
-MXNET_OPERATOR_REGISTER_UNARY_WITH_SPARSE_DR(log, cpu, mshadow_op::log)
+MXNET_OPERATOR_REGISTER_UNARY(log)
MXNET_ADD_SPARSE_OP_ALIAS(log)
.describe(R"code(Returns element-wise Natural logarithmic value of the input.
@@ -949,6 +949,7 @@ The natural logarithm is logarithm in base *e*, so that ``log(exp(x)) = x``
The storage type of ``log`` output is always dense
)code" ADD_FILELINE)
+.set_attr<FCompute>("FCompute<cpu>", UnaryOp::LogCompute<cpu, mshadow_op::log>)
.set_attr<nnvm::FGradient>("FGradient", ElemwiseGradUseIn{"_backward_log"});
// log10