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

Refactor operators and add MKLDNN #9677

Merged
merged 72 commits into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
4e9db27
Use NNVM interface.
zheng-da Nov 27, 2017
9e071f8
Remove MKL code.
zheng-da Oct 20, 2017
f668b68
Integrate MKLDNN.
zheng-da Oct 21, 2017
6ca9354
revert modification in test_executor.
zheng-da Jan 17, 2018
56e8c2c
Fix a bug in FlattenStorageType.
zheng-da Jan 17, 2018
c3d1683
Remove BN debug.
zheng-da Jan 17, 2018
27caf85
Remove remaining MXNET_USE_MKL2017
zheng-da Jan 17, 2018
585d837
Remove unused code in pooling.
zheng-da Jan 18, 2018
6a54f28
Fixing bugs in gtests.
zheng-da Jan 18, 2018
ffdd860
Fix lint errors.
zheng-da Jan 18, 2018
3ca549d
a lot of minor updates to address comments.
zheng-da Jan 19, 2018
8abbcc1
Fix coding style in MKLDNN Pooling (#22)
TaoLv Jan 19, 2018
9203748
revert the code change in the previous code refactor.
zheng-da Jan 19, 2018
407a972
Fix a bug in pooling.
zheng-da Jan 19, 2018
d4e7c58
LRN coding style changes (#21)
Jan 19, 2018
856aec9
cast storage with MKLDNN properly.
zheng-da Jan 19, 2018
b8deaab
Minor updates to address comments.
zheng-da Jan 19, 2018
bc1d164
some minor updates.
zheng-da Jan 19, 2018
b77ae3e
Switch to the master branch of MKLDNN.
zheng-da Jan 19, 2018
1766dc2
Minor updates to address comments.
zheng-da Jan 19, 2018
d0ae271
Update activation.cc
zheng-da Jan 20, 2018
ff78b02
Fix a bug in convert NDArray.
zheng-da Jan 23, 2018
5d6d604
Add gluon model zoo tests.
zheng-da Jan 23, 2018
b1e2983
Update GPU tests on model zoo.
zheng-da Jan 23, 2018
67359d9
Avoid using mobilenet for GPU tests with gluon models.
zheng-da Jan 23, 2018
e02de9d
Update GPU tests on gluon.
zheng-da Jan 24, 2018
01a1f54
change cmake to compile MKLDNN.
zheng-da Jan 25, 2018
69cb72c
update cmake for MKLDNN.
zheng-da Jan 25, 2018
f16fdfd
Implement align myself.
zheng-da Jan 26, 2018
4fb0c9f
Switch to intel/mkl-dnn.
zheng-da Jan 26, 2018
35c2665
Fix errors in align unittest.
zheng-da Jan 26, 2018
af9e811
Add unit test for LRN.
zheng-da Jan 26, 2018
8c15676
fix a compilation error.
zheng-da Jan 26, 2018
65c0242
use storage_type_assign to determine storage type.
zheng-da Jan 26, 2018
cb2af66
avoid global pooling in mkldnn.
zheng-da Jan 29, 2018
a1b2a06
compare all MKLDNN ops with native impls.
zheng-da Jan 30, 2018
2e2f55b
Fix a bug in testing correctness.
zheng-da Jan 30, 2018
a027011
print the name of buggy operator.
zheng-da Jan 30, 2018
6d7880a
undo some modifications.
zheng-da Jan 30, 2018
1451790
Fix a bug on reshaped array.
zheng-da Jan 30, 2018
eef3114
avoid testing outputs with NullOp.
zheng-da Jan 30, 2018
e59bdee
turn on MKLDNN tests in Jenkins.
zheng-da Jan 30, 2018
5e74b76
print each operator in MKLDNN tests.
zheng-da Jan 30, 2018
6ff193e
rename test_gluon_model_zoo.py
zheng-da Jan 31, 2018
ea1dd46
Create hashcode for operator parameters properly.
zheng-da Jan 31, 2018
c0f14c5
Add USE_MKL2017 back.
zheng-da Jan 31, 2018
4795960
Print warning messages.
zheng-da Feb 1, 2018
ac88417
move batchnorm tests to nnvm interface.
zheng-da Feb 1, 2018
3a6d8ae
Delete batchnorm v1 tests.
zheng-da Feb 1, 2018
6a25a20
Get inputs and outputs in batchnorm tests.
zheng-da Feb 2, 2018
2de47bd
disable batchnorm tests for now.
zheng-da Feb 2, 2018
cd46ddd
Remove warnings.
zheng-da Feb 5, 2018
d1e21ce
Fix GPU tests on gluon model zoo.
zheng-da Feb 2, 2018
2b69798
Fix lint complains in tests.
zheng-da Feb 2, 2018
42c3f24
Remove simd from openmp instructions in BatchNorm (#24)
TaoLv Feb 3, 2018
27a0227
Fix compilation errors.
zheng-da Feb 5, 2018
8be6810
Fix MKLDNN 1st compile failure issue (#23)
jinhuang415 Feb 5, 2018
f7f5563
Remove ARCH_OPT in Jenkins.
zheng-da Feb 5, 2018
d3d3c46
Revert "avoid global pooling in mkldnn."
zheng-da Feb 6, 2018
84920f2
Move to the latest MKLDNN.
zheng-da Feb 6, 2018
0ed1f94
WIP unit tests (#25)
cjolivier01 Feb 7, 2018
ecfd2b5
Make more C++ unit tests work for batch norm (#28)
cjolivier01 Feb 12, 2018
7417fdf
Fix lint complains.
zheng-da Feb 13, 2018
e8d1f8b
Fix a minor problem in Makefile.
zheng-da Feb 13, 2018
952a23d
fix GPU pooling.
zheng-da Feb 14, 2018
40a52b5
Disable modelzoo inference tests.
zheng-da Feb 14, 2018
40505b5
update accuracy checks for MKLDNN.
zheng-da Feb 14, 2018
466cea7
Fix MKLDNN pooling for global pooling.
zheng-da Feb 14, 2018
30b6866
Fix Jenkins.
zheng-da Feb 14, 2018
39fef04
Merge branch 'mkldnn' of https://github.com/zheng-da/incubator-mxnet …
zheng-da Feb 14, 2018
a6dec46
Fix a bug in Jenkins.
zheng-da Feb 14, 2018
3f78c29
Fix Jenkins
zheng-da Feb 15, 2018
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
Prev Previous commit
Next Next commit
LRN coding style changes (#21)
* LRN coding style change

* Add const for local variables

* Add req for LRN forward

* rebase code

* align API interface

* revert modification in test_executor.
  • Loading branch information
PatricZhao authored and zheng-da committed Feb 13, 2018
commit d4e7c587d77b8e248e6b94db9d5eb75377589655
59 changes: 31 additions & 28 deletions src/operator/nn/lrn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@
* Copyright (c) 2015 by Contributors
* \file lrn.cc
* \brief
* \author Bing Xu
* \author Bing Xu, Patric Zhao (patric.zhao@intel.com)
*/

#include "./lrn-inl.h"
#include "../operator_common.h"
#if MXNET_USE_MKLDNN == 1
#include "./mkldnn/mkldnn_lrn-inl.h"
#endif

namespace mxnet {
namespace op {

static bool LRNShape(const nnvm::NodeAttrs& attrs,
std::vector<TShape> *in_shape,
std::vector<TShape> *out_shape) {
bool LRNShape(const nnvm::NodeAttrs& attrs,
std::vector<TShape> *in_shape,
std::vector<TShape> *out_shape) {
using namespace mshadow;
CHECK_EQ(in_shape->size(), 1U) << "Input:[data]";
const TShape &dshape = in_shape->at(0);
Expand All @@ -45,13 +46,13 @@ static bool LRNShape(const nnvm::NodeAttrs& attrs,
return true;
}

static inline std::vector<std::string> ListArguments() {
inline std::vector<std::string> ListArguments() {
return {"data"};
}

static bool LRNType(const nnvm::NodeAttrs& attrs,
std::vector<int> *in_type,
std::vector<int> *out_type) {
bool LRNType(const nnvm::NodeAttrs& attrs,
std::vector<int> *in_type,
std::vector<int> *out_type) {
CHECK_GE(in_type->size(), 1U);
int dtype = (*in_type)[0];
CHECK_NE(dtype, -1) << "First input must have specified type";
Expand Down Expand Up @@ -80,37 +81,39 @@ struct LRNGrad {
}
};

inline static bool LRNForwardInferStorageType(const nnvm::NodeAttrs& attrs,
const int dev_mask,
DispatchMode* dispatch_mode,
std::vector<int> *in_attrs,
std::vector<int> *out_attrs) {
*dispatch_mode = DispatchMode::kFCompute;
#if MXNET_USE_MKLDNN == 1
bool LRNForwardInferStorageType(const nnvm::NodeAttrs& attrs,
const int dev_mask,
DispatchMode* dispatch_mode,
std::vector<int> *in_attrs,
std::vector<int> *out_attrs) {
CHECK(!in_attrs->empty());
#if MXNET_USE_MKLDNN == 1
if (dev_mask == mshadow::cpu::kDevMask) {
*dispatch_mode = DispatchMode::kFComputeEx;
storage_type_assign(out_attrs, mxnet::kDefaultStorage,
dispatch_mode, DispatchMode::kFComputeEx);
return true;
}
#endif
for (size_t i = 0; i < out_attrs->size(); i++)
(*out_attrs)[i] = kDefaultStorage;
storage_type_assign(out_attrs, mxnet::kDefaultStorage,
dispatch_mode, DispatchMode::kFCompute);
return true;
}

inline static bool LRNBackwardInferStorageType(const nnvm::NodeAttrs& attrs,
const int dev_mask,
DispatchMode* dispatch_mode,
std::vector<int> *in_attrs,
std::vector<int> *out_attrs) {
*dispatch_mode = DispatchMode::kFCompute;
#if MXNET_USE_MKLDNN == 1
bool LRNBackwardInferStorageType(const nnvm::NodeAttrs& attrs,
const int dev_mask,
DispatchMode* dispatch_mode,
std::vector<int> *in_attrs,
std::vector<int> *out_attrs) {
CHECK(!in_attrs->empty());
#if MXNET_USE_MKLDNN == 1
if (dev_mask == mshadow::cpu::kDevMask) {
*dispatch_mode = DispatchMode::kFComputeEx;
storage_type_assign(out_attrs, mxnet::kDefaultStorage,
dispatch_mode, DispatchMode::kFComputeEx);
return true;
}
#endif
for (size_t i = 0; i < out_attrs->size(); i++)
(*out_attrs)[i] = kDefaultStorage;
storage_type_assign(out_attrs, mxnet::kDefaultStorage,
dispatch_mode, DispatchMode::kFCompute);
return true;
}

Expand Down
84 changes: 44 additions & 40 deletions src/operator/nn/mkldnn/mkldnn_lrn-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,54 +33,58 @@
namespace mxnet {
namespace op {

static inline algorithm GetMKLDNNLRNAlgo(const LRNParam &param) {
inline algorithm GetMKLDNNLRNAlgo(const LRNParam &param) {
// TODO(Patric): lrn_within_channel will cause core dump in MKLDNN backward
// Need to confirm with MKLDNN team and fix later
return algorithm::lrn_across_channels;
}

inline static lrn_forward::primitive_desc GetLRNFwd(
const LRNParam &param, bool is_train, const memory::desc &src_md) {
auto engine = CpuEngine::Get()->get_engine();
auto alg_ = GetMKLDNNLRNAlgo(param);
auto alpha_ = param.alpha;
auto beta_ = param.beta;
auto nsize_ = param.nsize;
auto k_ = param.knorm;
auto kind_ = prop_kind::forward_training;
inline lrn_forward::primitive_desc GetLRNFwd(const LRNParam &param,
const bool is_train,
const memory::desc &src_md) {
const auto engine = CpuEngine::Get()->get_engine();
const auto alg = GetMKLDNNLRNAlgo(param);
const float alpha = param.alpha;
const float beta = param.beta;
const int nsize = param.nsize;
const float k = param.knorm;
auto kind = prop_kind::forward_training;
if (is_train) {
kind_ = prop_kind::forward_training;
kind = prop_kind::forward_training;
} else {
kind_ = prop_kind::forward_scoring;
kind = prop_kind::forward_scoring;
}
lrn_forward::desc fwd_desc_(kind_, alg_, src_md, nsize_, alpha_, beta_, k_);
return mkldnn::lrn_forward::primitive_desc(fwd_desc_, engine);
lrn_forward::desc fwd_desc(kind, alg, src_md, nsize, alpha, beta, k);
return mkldnn::lrn_forward::primitive_desc(fwd_desc, engine);
}

inline static mkldnn::lrn_backward::primitive_desc GetLRNBwd(
const LRNParam &param, const mkldnn::memory::desc &diff_in_md,
const mkldnn::memory::desc &diff_md,
const lrn_forward::primitive_desc &lrnFwd_desc) {
auto engine = CpuEngine::Get()->get_engine();
auto alg_ = GetMKLDNNLRNAlgo(param);
auto alpha_ = param.alpha;
auto beta_ = param.beta;
int nsize_ = param.nsize;
auto k_ = param.knorm;
inline mkldnn::lrn_backward::primitive_desc
GetLRNBwd(const LRNParam &param,
const mkldnn::memory::desc &diff_in_md,
const mkldnn::memory::desc &diff_md,
const lrn_forward::primitive_desc &lrnFwd_desc) {
const auto engine = CpuEngine::Get()->get_engine();
const auto alg = GetMKLDNNLRNAlgo(param);
const float alpha = param.alpha;
const float beta = param.beta;
const int nsize = param.nsize;
const float k = param.knorm;

lrn_backward::desc lrnBwd_desc(alg_, diff_in_md,
diff_md, nsize_, alpha_, beta_, k_);
lrn_backward::desc lrnBwd_desc(alg, diff_in_md,
diff_md, nsize, alpha, beta, k);
return mkldnn::lrn_backward::primitive_desc(lrnBwd_desc,
engine, lrnFwd_desc);
}

void MKLDNNLRN_Forward(const OpContext &ctx, const LRNParam &param,
const NDArray &in_data, const OpReqType &req,
void MKLDNNLRN_Forward(const OpContext &ctx,
const LRNParam &param,
const NDArray &in_data,
const OpReqType req,
const NDArray &out_data) {
auto src_mem = in_data.GetMKLDNNData();
auto src_md = src_mem->get_primitive_desc().desc();
auto pdesc = GetLRNFwd(param, ctx.is_train, src_md);
auto dst_mem = const_cast<NDArray &>(out_data).CreateMKLDNNData(
const auto src_md = src_mem->get_primitive_desc().desc();
const auto pdesc = GetLRNFwd(param, ctx.is_train, src_md);
auto dst_mem = const_cast<NDArray &>(out_data).CreateMKLDNNData(
pdesc.dst_primitive_desc());
if (ctx.is_train) {
std::shared_ptr<const mkldnn::memory> ws_mem(
Expand All @@ -97,17 +101,17 @@ void MKLDNNLRN_Forward(const OpContext &ctx, const LRNParam &param,
}

void MKLDNNLRN_Backward(const OpContext &ctx, const LRNParam &param,
const NDArray &out_grad,
const NDArray &in_data,
const OpReqType &req,
const NDArray &in_grad) {
const NDArray &out_grad,
const NDArray &in_data,
const OpReqType req,
const NDArray &in_grad) {
if (req == kNullOp) {
return;
}
// Repeat FW for getting workspace
auto data_mem = in_data.GetMKLDNNData();
auto data_md = data_mem->get_primitive_desc().desc();
auto pdesc_fwd = GetLRNFwd(param, ctx.is_train, data_md);
const auto data_md = data_mem->get_primitive_desc().desc();
const auto pdesc_fwd = GetLRNFwd(param, ctx.is_train, data_md);

// TODO(Patric): To keep the function stateless, we can't pass workspace
// from LRN forward to backward. We have to re-compute
Expand All @@ -121,10 +125,10 @@ void MKLDNNLRN_Backward(const OpContext &ctx, const LRNParam &param,
lrn_forward(pdesc_fwd, mkldnn::primitive::at(*data_mem),
*ws_mem, *dst_temp));

auto data_in_md = pdesc_fwd.src_primitive_desc().desc();
const auto data_in_md = pdesc_fwd.src_primitive_desc().desc();
auto diff_mem = out_grad.GetMKLDNNData();
auto diff_md = diff_mem->get_primitive_desc().desc();
auto pdesc_bwd = GetLRNBwd(param, data_in_md, diff_md, pdesc_fwd);
const auto diff_md = diff_mem->get_primitive_desc().desc();
const auto pdesc_bwd = GetLRNBwd(param, data_in_md, diff_md, pdesc_fwd);
auto diff_src_mem = CreateMKLDNNMem(in_grad,
pdesc_bwd.diff_src_primitive_desc(), req);

Expand Down