Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

MKLdnn Integration Patch to improve issue #2986(call for cpu performance) #3438

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ ifeq ($(USE_CUDNN), 1)
LDFLAGS += -lcudnn
endif

ifeq ($(USE_MKLDNN),1)
CFLAGS += -DMXNET_USE_MKLDNN=1 -I$(MKLDNN_ROOT)/include
LDFLAGS += -L$(MKLDNN_ROOT)/lib/ -liomp5 -lmklml_gnu -lmklml_intel
else
CFLAGS += -DMXNET_USE_MKLDNN=0
endif

ifeq ($(USE_THREADED_ENGINE), 1)
CFLAGS += -DMXNET_USE_THREADED_ENGINE
endif
Expand Down
6 changes: 6 additions & 0 deletions make/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ USE_CUDNN = 0
# whether use cuda runtime compiling for writing kernels in native language (i.e. Python)
USE_NVRTC = 0

# whether use MKLDNN library
USE_MKLDNN = 0

# MKLDNN root library
MKLDNN_ROOT = NONE

# whether use opencv during compilation
# you can disable it, however, you will not able to use
# imbin iterator
Expand Down
2 changes: 1 addition & 1 deletion mshadow
Submodule mshadow updated 1 files
+13 −3 cmake/Cuda.cmake
26 changes: 25 additions & 1 deletion src/operator/activation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,35 @@
*/
#include "./activation-inl.h"
#include "./mshadow_op.h"

#if MXNET_USE_MKLDNN == 1
#include "./mkldnn/mkldnn_ReLU-inl.h"
#endif
namespace mxnet {
namespace op {
template<>
Operator *CreateOp<cpu>(ActivationParam param, int dtype) {
Operator *op = NULL;
#if MXNET_USE_MKLDNN == 1
MSHADOW_REAL_TYPE_SWITCH(dtype, DType, {
switch (param.act_type) {
case activation::kReLU:
// MKLDNN only supports ReLU
op = new MKLDNNReLUOp<DType>(param);
break;
case activation::kSigmoid:
op = new ActivationOp<cpu, mshadow_op::sigmoid, mshadow_op::sigmoid_grad, DType>();
break;
case activation::kTanh:
op = new ActivationOp<cpu, mshadow_op::tanh, mshadow_op::tanh_grad, DType>();
break;
case activation::kSoftReLU:
op = new ActivationOp<cpu, mshadow_op::softrelu, mshadow_op::softrelu_grad, DType>();
break;
default:
LOG(FATAL) << "unknown activation type";
}
})
#else
MSHADOW_REAL_TYPE_SWITCH(dtype, DType, {
switch (param.act_type) {
case activation::kReLU:
Expand All @@ -30,6 +53,7 @@ Operator *CreateOp<cpu>(ActivationParam param, int dtype) {
LOG(FATAL) << "unknown activation type";
}
})
#endif
return op;
}

Expand Down
7 changes: 7 additions & 0 deletions src/operator/batch_norm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
*/

#include "./batch_norm-inl.h"
#if MXNET_USE_MKLDNN == 1
#include "./mkldnn/mkldnn_batch_norm-inl.h"
#endif

namespace mxnet {
namespace op {
template<>
Operator *CreateOp<cpu>(BatchNormParam param) {
#if MXNET_USE_MKLDNN == 1
return new MKLBatchNormOp<float>(param);
#else
return new BatchNormOp<cpu>(param);
#endif
}

Operator *BatchNormProp::CreateOperator(Context ctx) const {
Expand Down
9 changes: 9 additions & 0 deletions src/operator/convolution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/

#include "./convolution-inl.h"
#if MXNET_USE_MKLDNN == 1
#include "./mkldnn/mkldnn_convolution-inl.h"
#endif

namespace mxnet {
namespace op {
Expand All @@ -15,9 +18,15 @@ Operator* CreateOp<cpu>(ConvolutionParam param, int dtype,
std::vector<TShape> *out_shape,
Context ctx) {
Operator *op = NULL;
#if MXNET_USE_MKLDNN == 1
MSHADOW_REAL_TYPE_SWITCH(dtype, DType, {
op = new MKLDNNConvolutionOp<DType>(param);
})
#else
MSHADOW_REAL_TYPE_SWITCH(dtype, DType, {
op = new ConvolutionOp<cpu, DType>(param);
})
#endif
return op;
}

Expand Down
8 changes: 7 additions & 1 deletion src/operator/fully_connected-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <utility>
#include "./operator_common.h"


namespace mxnet {
namespace op {

Expand Down Expand Up @@ -56,6 +55,9 @@ class FullyConnectedOp : public Operator {
const std::vector<TBlob> &aux_args) {
using namespace mshadow;
using namespace mshadow::expr;
#ifdef PRINT_LAYER_TIME
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will have a profiler soon. This should be removed, but it's ok if you want to leave it here until the profiler comes in

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your information. Remove it firstly.

double start_time = dmlc::GetTime();
#endif
if (req[fullc::kOut] == kNullOp) return;
CHECK_EQ(req[fullc::kOut], kWriteTo);
size_t expected = param_.no_bias ? 2 : 3;
Expand All @@ -82,6 +84,10 @@ class FullyConnectedOp : public Operator {
Tensor<xpu, 1, DType> bias = in_data[fullc::kBias].get<xpu, 1, DType>(s);
out += repmat(bias, data.size(0));
}
#ifdef PRINT_LAYER_TIME
double end_time = dmlc::GetTime();
LOG(INFO)<< "forward FC" << (end_time-start_time) << " s";
#endif
}

virtual void Backward(const OpContext &ctx,
Expand Down
20 changes: 20 additions & 0 deletions src/operator/fully_connected.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,30 @@
* \brief fully connect operator
*/
#include "./fully_connected-inl.h"
#if MXNET_USE_MKLDNN == 1
#include "./mkldnn/mkldnn_fully_connected-inl.h"
#endif
namespace mxnet {
namespace op {
template<>
Operator* CreateOp<cpu>(FullyConnectedParam param, int dtype) {
Operator *op = NULL;
#if MXNET_USE_MKLDNN == 1
switch (dtype) {
case mshadow::kFloat32:
op = new MKLDNNFullyConnectedOp<float>(param);
break;
case mshadow::kFloat64:
op = new MKLDNNFullyConnectedOp<double>(param);
break;
case mshadow::kFloat16:
LOG(FATAL) << "float16 fully connected layer is "
"not supported by MKLDNN.";
break;
default:
LOG(FATAL) << "Unsupported type " << dtype;
}
#else
switch (dtype) {
case mshadow::kFloat32:
op = new FullyConnectedOp<cpu, float>(param);
Expand All @@ -23,6 +42,7 @@ Operator* CreateOp<cpu>(FullyConnectedParam param, int dtype) {
default:
LOG(FATAL) << "Unsupported type " << dtype;
}
#endif
return op;
}

Expand Down
8 changes: 8 additions & 0 deletions src/operator/lrn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@
#if MXNET_USE_CUDNN == 1
#include "./cudnn_lrn-inl.h"
#endif
#if MXNET_USE_MKLDNN == 1
#include "./mkldnn/mkldnn_lrn-inl.h"
#endif


namespace mxnet {
namespace op {
template<>
Operator* CreateOp<cpu>(LRNParam param) {
#if MXNET_USE_MKLDNN == 1
return new MKLDNNLocalResponseNormOp<real_t>(param);
#else
return new LocalResponseNormOp<cpu>(param);
#endif
}

Operator* LocalResponseNormProp::CreateOperator(Context ctx) const {
Expand Down
31 changes: 31 additions & 0 deletions src/operator/mkldnn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# MKLDNN PLUGIN

MKLDNN is one INTEL released library to accelerate Deep Neural Network (DNN) applications on Intel architecture.
This README shows users how to setup mxnet with MKLDNN library.

## prepare MKLDNN Minimal Library
```
cd <MXNET ROOTDIR>
mkdir -p ./external/mkl
wget https://github.com/intel/caffe/releases/download/self_containted_MKLGOLD/mklml_lnx_2017.0.0.20160801.tgz
mv mklml_lnx_2017.0.0.20160801.tgz ./external/mkl
cd external/mkl
tar zxvf mklml_lnx_2017.0.0.20160801.tgz
cd <MXNET ROOTDIR>
```

## update config.mk
```
USE_MKLDNN = 1 # set USE_MKLDNN on
MKLDNN_ROOT = <MXNET ROOTDIR>/external/mkl/mklml_lnx_2017.0.0.20160801 # set MKLDNN ROOT PATH
```

## update LD_LIBRARY_PATH
```
export LD_LIBRARY_PATH=<MXNET ROOTDIR>/external/mkl/mklml_lnx_2017.0.0.20160801/lib:$LD_LIBRARY_PATH
```

## build mxnet
```
make -j8
```
Loading