From fca247d7db3ef8fc3e27dba030d6cb4d32d5fed0 Mon Sep 17 00:00:00 2001 From: wentingj Date: Thu, 7 Dec 2017 16:52:00 +0800 Subject: [PATCH 1/4] add mkldnn surport for concat --- src/operator/nn/concat.cc | 101 +++++++++++++++++++++++- src/operator/nn/mkldnn/mkldnn_concat.cc | 85 ++++++++++++++++++++ src/operator/nn/mkldnn/mkldnn_ops-inl.h | 8 ++ 3 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 src/operator/nn/mkldnn/mkldnn_concat.cc diff --git a/src/operator/nn/concat.cc b/src/operator/nn/concat.cc index 61b9f517eb56..d17bf8054238 100644 --- a/src/operator/nn/concat.cc +++ b/src/operator/nn/concat.cc @@ -25,6 +25,7 @@ */ #include "./concat-inl.h" +#include "./mkldnn/mkldnn_ops-inl.h" namespace mxnet { namespace op { @@ -103,12 +104,104 @@ static bool ConcatType(const nnvm::NodeAttrs& attrs, return true; } +inline static bool ConcatForwardInferStorageType(const nnvm::NodeAttrs& attrs, + const int dev_mask, + DispatchMode* dispatch_mode, + std::vector *in_attrs, + std::vector *out_attrs) { + CHECK(!in_attrs->empty()); + CHECK_EQ(out_attrs->size(), 1U); +#if MXNET_USE_MKLDNN == 1 + if (dev_mask == mshadow::cpu::kDevMask) { + *dispatch_mode = DispatchMode::kFComputeEx; + (*out_attrs)[0] = kMKLDNNStorage; + return true; + } +#endif + *dispatch_mode = DispatchMode::kFCompute; + (*out_attrs)[0] = kDefaultStorage; + return true; +} + +inline static bool backward_ConcatStorageType(const nnvm::NodeAttrs& attrs, + const int dev_mask, + DispatchMode* dispatch_mode, + std::vector *in_attrs, + std::vector *out_attrs) { +#if MXNET_USE_MKLDNN == 1 + CHECK_EQ(out_attrs->size(), in_attrs->size() - 1); + if (dev_mask == mshadow::cpu::kDevMask) { + *dispatch_mode = DispatchMode::kFComputeEx; + for (size_t i = 0; i < out_attrs->size(); i++) + (*out_attrs)[i] = kMKLDNNStorage; + return true; + } +#endif + *dispatch_mode = DispatchMode::kFCompute; + for (size_t i = 0; i < out_attrs->size(); i++) + (*out_attrs)[i] = kDefaultStorage; + return true; +} + +void ConcatComputeExCPU(const nnvm::NodeAttrs& attrs, + const OpContext& op_ctx, + const std::vector& inputs, + const std::vector& req, + const std::vector& outputs) { + CHECK(!inputs.empty()); + CHECK_EQ(outputs.size(), 1U); + CHECK_EQ(req.size(), 1U); + if (req[0] == kNullOp) return; +#if MXNET_USE_MKLDNN == 1 + //MKLDNN support 2D and 4D concat + if (inputs[0].shape().ndim() == 2 || inputs[0].shape().ndim() == 4) { + if(inputs[0].dtype() == mshadow::kFloat32) { + MKLDNNConcat_Forward(attrs, op_ctx, inputs, req, outputs); + } + } + else { + // TODO I need to convert format. + std::vector in_blobs(inputs.size()); + for (size_t i = 0; i < in_blobs.size(); i++) + in_blobs[i] = inputs[i].data(); + std::vector out_blobs(outputs.size()); + for (size_t i = 0; i < out_blobs.size(); i++) + out_blobs[i] = outputs[i].data(); + ConcatCompute(attrs, op_ctx, in_blobs, req, out_blobs); + } +#endif +} + +static void ConcatGradComputeExCPU(const nnvm::NodeAttrs& attrs, + const OpContext& ctx, const std::vector& inputs, + const std::vector& req, const std::vector& outputs) { +#if MXNET_USE_MKLDNN == 1 + if (inputs[0].shape().ndim() == 2 || inputs[0].shape().ndim() == 4) { + if(inputs[0].dtype() == mshadow::kFloat32) { + MKLDNNConcat_Backward(attrs, ctx, inputs, req, outputs); + } + } + else { + // TODO I need to convert format. + std::vector in_blobs(1); + in_blobs[0] = inputs[0].data(); + std::vector out_blobs(outputs.size()); + for (size_t i = 0; i < out_blobs.size(); i++) + out_blobs[i] = outputs[i].data(); + ConcatGradCompute(attrs, ctx, in_blobs, req, out_blobs); + } +#endif +} + struct ConcatGrad { const char *op_name; std::vector operator()(const nnvm::NodePtr& n, const std::vector& ograds) const { - const ConcatParam& param = nnvm::get(n->attrs.parsed); + CHECK_EQ(ograds.size(), 1); std::vector heads(ograds.begin(), ograds.end()); + for (size_t i = 0; i < n->inputs.size(); i++) { + heads.push_back(n->inputs[i]); + } return MakeGradNode(op_name, n, heads, n->attrs.dict); } }; @@ -165,7 +258,9 @@ Example:: }) .set_attr("FInferShape", ConcatShape) .set_attr("FInferType", ConcatType) +.set_attr("FInferStorageType", ConcatForwardInferStorageType) .set_attr("FCompute", ConcatCompute) +.set_attr("FComputeEx", ConcatComputeExCPU) .set_attr("FGradient", ConcatGrad{"_backward_Concat"}) .set_attr("key_var_num_args", "num_args") .add_argument("data", "NDArray-or-Symbol[]", "List of arrays to concatenate") @@ -180,7 +275,9 @@ NNVM_REGISTER_OP(_backward_Concat) }) .set_attr_parser(ParamParser) .set_attr("TIsBackward", true) -.set_attr("FCompute", ConcatGradCompute); +.set_attr("FInferStorageType", backward_ConcatStorageType) +.set_attr("FCompute", ConcatGradCompute) +.set_attr("FComputeEx", ConcatGradComputeExCPU); } // namespace op } // namespace mxnet diff --git a/src/operator/nn/mkldnn/mkldnn_concat.cc b/src/operator/nn/mkldnn/mkldnn_concat.cc new file mode 100644 index 000000000000..c3de8a5c4f4f --- /dev/null +++ b/src/operator/nn/mkldnn/mkldnn_concat.cc @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * \file mkldnn_concat.cc + * \brief + * \author Wenting Jiang +*/ +#include "../concat-inl.h" +#include "./mkldnn_ops-inl.h" +#include "./mkldnn_base-inl.h" + +#if MXNET_USE_MKLDNN == 1 +namespace mxnet { +namespace op { + +void MKLDNNConcat_Forward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, + const std::vector &in_data, const std::vector &req, + const std::vector &out_data) { + const ConcatParam& param = nnvm::get(attrs.parsed); + int num_in_data = param.num_args; + int concat_dim = param.dim; + std::vector data_md; + std::vector data_mem; + for(int i =0; i < num_in_data; i++) { + std::shared_ptr tmp_mem = in_data[i].GetMKLDNNData(); + auto tmp_pd = tmp_mem->get_primitive_desc(); + data_md.push_back(tmp_pd); + data_mem.push_back(*tmp_mem); + } + mkldnn::concat::primitive_desc fwd_pd(concat_dim, data_md); + auto engine = CpuEngine::Instance().get_engine(); + auto out_mem = CreateMKLDNNMem(out_data[concat_enum::kOut], + fwd_pd.dst_primitive_desc(), req[concat_enum::kOut]); + MKLDNNStream::Instance().RegisterPrim(mkldnn::concat(fwd_pd, data_mem, *out_mem.second)); + CommitOutput(out_data[concat_enum::kOut], out_mem); + MKLDNNStream::Instance().Submit(); +} + +void MKLDNNConcat_Backward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, + const std::vector& inputs, const std::vector& req, + const std::vector& outputs) { + const ConcatParam& param = nnvm::get(attrs.parsed); + int num_in_data = param.num_args; + int axis_ = param.dim; + auto engine = CpuEngine::Instance().get_engine(); + std::shared_ptrgz_mem = inputs[0].GetMKLDNNData(); + mkldnn::memory::primitive_desc gz_pd = gz_mem->get_primitive_desc(); + /* init the offset */ + mkldnn::memory::dims offsets = {0, 0, 0, 0}; + for (int i = 0; i < num_in_data; i++) { + mkldnn::memory::dims diff_src_tz = {inputs[i+1].shape()[0], inputs[i+1].shape()[1], inputs[i+1].shape()[2], inputs[i+1].shape()[3]}; + auto diff_src_mpd = inputs[i+1].GetMKLDNNData()->get_primitive_desc(); + auto gradi_mem_ = CreateMKLDNNMem(outputs[i], diff_src_mpd, req[i]); + // create view from gy to gxs[i] + std::shared_ptr view_pd; + view_pd.reset(new mkldnn::view::primitive_desc(gz_pd, diff_src_tz, offsets)); + // create reorder primitive from gy to gxs[i] + mkldnn::reorder::primitive_desc reorder_pd(view_pd.get()->dst_primitive_desc(), diff_src_mpd); + offsets[axis_] += diff_src_tz[axis_]; + MKLDNNStream::Instance().RegisterPrim(mkldnn::reorder(reorder_pd, *gz_mem, *gradi_mem_.second)); + CommitOutput(outputs[i], gradi_mem_); + } + MKLDNNStream::Instance().Submit(); +} + +}//op +}//mxnet +#endif diff --git a/src/operator/nn/mkldnn/mkldnn_ops-inl.h b/src/operator/nn/mkldnn/mkldnn_ops-inl.h index ffeaf67fa74a..f8dde505e938 100644 --- a/src/operator/nn/mkldnn/mkldnn_ops-inl.h +++ b/src/operator/nn/mkldnn/mkldnn_ops-inl.h @@ -76,6 +76,14 @@ void MKLDNNCopy(const nnvm::NodeAttrs& attrs, const OpContext &ctx, const NDArray &in_data, const OpReqType &req, const NDArray &out_data); +/* For concat */ +void MKLDNNConcat_Forward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, + const std::vector &in_data, const std::vector &req, + const std::vector &out_data); +void MKLDNNConcat_Backward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, + const std::vector& inputs, const std::vector& req, + const std::vector& outputs); + } } #endif // MXNET_USE_MKLDNN == 1 From ab2d9c6319c660359564ffab2039bb18d472f7c2 Mon Sep 17 00:00:00 2001 From: wentingj Date: Thu, 7 Dec 2017 16:52:00 +0800 Subject: [PATCH 2/4] add mkldnn surport for concat --- src/operator/nn/concat.cc | 101 +++++++++++++++++++++++- src/operator/nn/mkldnn/mkldnn_concat.cc | 85 ++++++++++++++++++++ src/operator/nn/mkldnn/mkldnn_ops-inl.h | 12 ++- 3 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 src/operator/nn/mkldnn/mkldnn_concat.cc diff --git a/src/operator/nn/concat.cc b/src/operator/nn/concat.cc index 61b9f517eb56..d17bf8054238 100644 --- a/src/operator/nn/concat.cc +++ b/src/operator/nn/concat.cc @@ -25,6 +25,7 @@ */ #include "./concat-inl.h" +#include "./mkldnn/mkldnn_ops-inl.h" namespace mxnet { namespace op { @@ -103,12 +104,104 @@ static bool ConcatType(const nnvm::NodeAttrs& attrs, return true; } +inline static bool ConcatForwardInferStorageType(const nnvm::NodeAttrs& attrs, + const int dev_mask, + DispatchMode* dispatch_mode, + std::vector *in_attrs, + std::vector *out_attrs) { + CHECK(!in_attrs->empty()); + CHECK_EQ(out_attrs->size(), 1U); +#if MXNET_USE_MKLDNN == 1 + if (dev_mask == mshadow::cpu::kDevMask) { + *dispatch_mode = DispatchMode::kFComputeEx; + (*out_attrs)[0] = kMKLDNNStorage; + return true; + } +#endif + *dispatch_mode = DispatchMode::kFCompute; + (*out_attrs)[0] = kDefaultStorage; + return true; +} + +inline static bool backward_ConcatStorageType(const nnvm::NodeAttrs& attrs, + const int dev_mask, + DispatchMode* dispatch_mode, + std::vector *in_attrs, + std::vector *out_attrs) { +#if MXNET_USE_MKLDNN == 1 + CHECK_EQ(out_attrs->size(), in_attrs->size() - 1); + if (dev_mask == mshadow::cpu::kDevMask) { + *dispatch_mode = DispatchMode::kFComputeEx; + for (size_t i = 0; i < out_attrs->size(); i++) + (*out_attrs)[i] = kMKLDNNStorage; + return true; + } +#endif + *dispatch_mode = DispatchMode::kFCompute; + for (size_t i = 0; i < out_attrs->size(); i++) + (*out_attrs)[i] = kDefaultStorage; + return true; +} + +void ConcatComputeExCPU(const nnvm::NodeAttrs& attrs, + const OpContext& op_ctx, + const std::vector& inputs, + const std::vector& req, + const std::vector& outputs) { + CHECK(!inputs.empty()); + CHECK_EQ(outputs.size(), 1U); + CHECK_EQ(req.size(), 1U); + if (req[0] == kNullOp) return; +#if MXNET_USE_MKLDNN == 1 + //MKLDNN support 2D and 4D concat + if (inputs[0].shape().ndim() == 2 || inputs[0].shape().ndim() == 4) { + if(inputs[0].dtype() == mshadow::kFloat32) { + MKLDNNConcat_Forward(attrs, op_ctx, inputs, req, outputs); + } + } + else { + // TODO I need to convert format. + std::vector in_blobs(inputs.size()); + for (size_t i = 0; i < in_blobs.size(); i++) + in_blobs[i] = inputs[i].data(); + std::vector out_blobs(outputs.size()); + for (size_t i = 0; i < out_blobs.size(); i++) + out_blobs[i] = outputs[i].data(); + ConcatCompute(attrs, op_ctx, in_blobs, req, out_blobs); + } +#endif +} + +static void ConcatGradComputeExCPU(const nnvm::NodeAttrs& attrs, + const OpContext& ctx, const std::vector& inputs, + const std::vector& req, const std::vector& outputs) { +#if MXNET_USE_MKLDNN == 1 + if (inputs[0].shape().ndim() == 2 || inputs[0].shape().ndim() == 4) { + if(inputs[0].dtype() == mshadow::kFloat32) { + MKLDNNConcat_Backward(attrs, ctx, inputs, req, outputs); + } + } + else { + // TODO I need to convert format. + std::vector in_blobs(1); + in_blobs[0] = inputs[0].data(); + std::vector out_blobs(outputs.size()); + for (size_t i = 0; i < out_blobs.size(); i++) + out_blobs[i] = outputs[i].data(); + ConcatGradCompute(attrs, ctx, in_blobs, req, out_blobs); + } +#endif +} + struct ConcatGrad { const char *op_name; std::vector operator()(const nnvm::NodePtr& n, const std::vector& ograds) const { - const ConcatParam& param = nnvm::get(n->attrs.parsed); + CHECK_EQ(ograds.size(), 1); std::vector heads(ograds.begin(), ograds.end()); + for (size_t i = 0; i < n->inputs.size(); i++) { + heads.push_back(n->inputs[i]); + } return MakeGradNode(op_name, n, heads, n->attrs.dict); } }; @@ -165,7 +258,9 @@ Example:: }) .set_attr("FInferShape", ConcatShape) .set_attr("FInferType", ConcatType) +.set_attr("FInferStorageType", ConcatForwardInferStorageType) .set_attr("FCompute", ConcatCompute) +.set_attr("FComputeEx", ConcatComputeExCPU) .set_attr("FGradient", ConcatGrad{"_backward_Concat"}) .set_attr("key_var_num_args", "num_args") .add_argument("data", "NDArray-or-Symbol[]", "List of arrays to concatenate") @@ -180,7 +275,9 @@ NNVM_REGISTER_OP(_backward_Concat) }) .set_attr_parser(ParamParser) .set_attr("TIsBackward", true) -.set_attr("FCompute", ConcatGradCompute); +.set_attr("FInferStorageType", backward_ConcatStorageType) +.set_attr("FCompute", ConcatGradCompute) +.set_attr("FComputeEx", ConcatGradComputeExCPU); } // namespace op } // namespace mxnet diff --git a/src/operator/nn/mkldnn/mkldnn_concat.cc b/src/operator/nn/mkldnn/mkldnn_concat.cc new file mode 100644 index 000000000000..c3de8a5c4f4f --- /dev/null +++ b/src/operator/nn/mkldnn/mkldnn_concat.cc @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * \file mkldnn_concat.cc + * \brief + * \author Wenting Jiang +*/ +#include "../concat-inl.h" +#include "./mkldnn_ops-inl.h" +#include "./mkldnn_base-inl.h" + +#if MXNET_USE_MKLDNN == 1 +namespace mxnet { +namespace op { + +void MKLDNNConcat_Forward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, + const std::vector &in_data, const std::vector &req, + const std::vector &out_data) { + const ConcatParam& param = nnvm::get(attrs.parsed); + int num_in_data = param.num_args; + int concat_dim = param.dim; + std::vector data_md; + std::vector data_mem; + for(int i =0; i < num_in_data; i++) { + std::shared_ptr tmp_mem = in_data[i].GetMKLDNNData(); + auto tmp_pd = tmp_mem->get_primitive_desc(); + data_md.push_back(tmp_pd); + data_mem.push_back(*tmp_mem); + } + mkldnn::concat::primitive_desc fwd_pd(concat_dim, data_md); + auto engine = CpuEngine::Instance().get_engine(); + auto out_mem = CreateMKLDNNMem(out_data[concat_enum::kOut], + fwd_pd.dst_primitive_desc(), req[concat_enum::kOut]); + MKLDNNStream::Instance().RegisterPrim(mkldnn::concat(fwd_pd, data_mem, *out_mem.second)); + CommitOutput(out_data[concat_enum::kOut], out_mem); + MKLDNNStream::Instance().Submit(); +} + +void MKLDNNConcat_Backward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, + const std::vector& inputs, const std::vector& req, + const std::vector& outputs) { + const ConcatParam& param = nnvm::get(attrs.parsed); + int num_in_data = param.num_args; + int axis_ = param.dim; + auto engine = CpuEngine::Instance().get_engine(); + std::shared_ptrgz_mem = inputs[0].GetMKLDNNData(); + mkldnn::memory::primitive_desc gz_pd = gz_mem->get_primitive_desc(); + /* init the offset */ + mkldnn::memory::dims offsets = {0, 0, 0, 0}; + for (int i = 0; i < num_in_data; i++) { + mkldnn::memory::dims diff_src_tz = {inputs[i+1].shape()[0], inputs[i+1].shape()[1], inputs[i+1].shape()[2], inputs[i+1].shape()[3]}; + auto diff_src_mpd = inputs[i+1].GetMKLDNNData()->get_primitive_desc(); + auto gradi_mem_ = CreateMKLDNNMem(outputs[i], diff_src_mpd, req[i]); + // create view from gy to gxs[i] + std::shared_ptr view_pd; + view_pd.reset(new mkldnn::view::primitive_desc(gz_pd, diff_src_tz, offsets)); + // create reorder primitive from gy to gxs[i] + mkldnn::reorder::primitive_desc reorder_pd(view_pd.get()->dst_primitive_desc(), diff_src_mpd); + offsets[axis_] += diff_src_tz[axis_]; + MKLDNNStream::Instance().RegisterPrim(mkldnn::reorder(reorder_pd, *gz_mem, *gradi_mem_.second)); + CommitOutput(outputs[i], gradi_mem_); + } + MKLDNNStream::Instance().Submit(); +} + +}//op +}//mxnet +#endif diff --git a/src/operator/nn/mkldnn/mkldnn_ops-inl.h b/src/operator/nn/mkldnn/mkldnn_ops-inl.h index 92ec12cf5e36..99da180e3cc5 100644 --- a/src/operator/nn/mkldnn/mkldnn_ops-inl.h +++ b/src/operator/nn/mkldnn/mkldnn_ops-inl.h @@ -77,8 +77,16 @@ void MKLDNNCopy(const nnvm::NodeAttrs& attrs, const OpContext &ctx, const NDArray &in_data, const OpReqType &req, const NDArray &out_data); -} // namespace op -} // namespace mxnet +/* For concat */ +void MKLDNNConcat_Forward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, + const std::vector &in_data, const std::vector &req, + const std::vector &out_data); +void MKLDNNConcat_Backward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, + const std::vector& inputs, const std::vector& req, + const std::vector& outputs); + +} +} #endif // MXNET_USE_MKLDNN == 1 #endif // MXNET_OPERATOR_NN_MKLDNN_MKLDNN_OPS_INL_H_ From 0ceaee0f519dae37fce0ec6356c812e7d703435f Mon Sep 17 00:00:00 2001 From: wentingj Date: Fri, 8 Dec 2017 17:39:00 +0800 Subject: [PATCH 3/4] fix the coding style for pr of mkldnn concat --- src/operator/nn/concat.cc | 14 +++++--------- src/operator/nn/mkldnn/mkldnn_concat.cc | 19 +++++++++++-------- src/operator/nn/mkldnn/mkldnn_ops-inl.h | 8 ++++---- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/operator/nn/concat.cc b/src/operator/nn/concat.cc index d17bf8054238..bbd41cb0153d 100644 --- a/src/operator/nn/concat.cc +++ b/src/operator/nn/concat.cc @@ -153,14 +153,12 @@ void ConcatComputeExCPU(const nnvm::NodeAttrs& attrs, CHECK_EQ(req.size(), 1U); if (req[0] == kNullOp) return; #if MXNET_USE_MKLDNN == 1 - //MKLDNN support 2D and 4D concat + // MKLDNN support 2D and 4D concat if (inputs[0].shape().ndim() == 2 || inputs[0].shape().ndim() == 4) { - if(inputs[0].dtype() == mshadow::kFloat32) { + if (inputs[0].dtype() == mshadow::kFloat32) { MKLDNNConcat_Forward(attrs, op_ctx, inputs, req, outputs); } - } - else { - // TODO I need to convert format. + } else { std::vector in_blobs(inputs.size()); for (size_t i = 0; i < in_blobs.size(); i++) in_blobs[i] = inputs[i].data(); @@ -177,12 +175,10 @@ static void ConcatGradComputeExCPU(const nnvm::NodeAttrs& attrs, const std::vector& req, const std::vector& outputs) { #if MXNET_USE_MKLDNN == 1 if (inputs[0].shape().ndim() == 2 || inputs[0].shape().ndim() == 4) { - if(inputs[0].dtype() == mshadow::kFloat32) { + if (inputs[0].dtype() == mshadow::kFloat32) { MKLDNNConcat_Backward(attrs, ctx, inputs, req, outputs); } - } - else { - // TODO I need to convert format. + } else { std::vector in_blobs(1); in_blobs[0] = inputs[0].data(); std::vector out_blobs(outputs.size()); diff --git a/src/operator/nn/mkldnn/mkldnn_concat.cc b/src/operator/nn/mkldnn/mkldnn_concat.cc index c3de8a5c4f4f..8171784d56cf 100644 --- a/src/operator/nn/mkldnn/mkldnn_concat.cc +++ b/src/operator/nn/mkldnn/mkldnn_concat.cc @@ -38,13 +38,13 @@ void MKLDNNConcat_Forward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, int concat_dim = param.dim; std::vector data_md; std::vector data_mem; - for(int i =0; i < num_in_data; i++) { + for (int i =0; i < num_in_data; i++) { std::shared_ptr tmp_mem = in_data[i].GetMKLDNNData(); auto tmp_pd = tmp_mem->get_primitive_desc(); data_md.push_back(tmp_pd); data_mem.push_back(*tmp_mem); } - mkldnn::concat::primitive_desc fwd_pd(concat_dim, data_md); + mkldnn::concat::primitive_desc fwd_pd(concat_dim, data_md); auto engine = CpuEngine::Instance().get_engine(); auto out_mem = CreateMKLDNNMem(out_data[concat_enum::kOut], fwd_pd.dst_primitive_desc(), req[concat_enum::kOut]); @@ -61,25 +61,28 @@ void MKLDNNConcat_Backward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, int axis_ = param.dim; auto engine = CpuEngine::Instance().get_engine(); std::shared_ptrgz_mem = inputs[0].GetMKLDNNData(); - mkldnn::memory::primitive_desc gz_pd = gz_mem->get_primitive_desc(); + mkldnn::memory::primitive_desc gz_pd = gz_mem->get_primitive_desc(); /* init the offset */ mkldnn::memory::dims offsets = {0, 0, 0, 0}; for (int i = 0; i < num_in_data; i++) { - mkldnn::memory::dims diff_src_tz = {inputs[i+1].shape()[0], inputs[i+1].shape()[1], inputs[i+1].shape()[2], inputs[i+1].shape()[3]}; + mkldnn::memory::dims diff_src_tz = {inputs[i+1].shape()[0], inputs[i+1].shape()[1], + inputs[i+1].shape()[2], inputs[i+1].shape()[3]}; auto diff_src_mpd = inputs[i+1].GetMKLDNNData()->get_primitive_desc(); auto gradi_mem_ = CreateMKLDNNMem(outputs[i], diff_src_mpd, req[i]); // create view from gy to gxs[i] std::shared_ptr view_pd; view_pd.reset(new mkldnn::view::primitive_desc(gz_pd, diff_src_tz, offsets)); // create reorder primitive from gy to gxs[i] - mkldnn::reorder::primitive_desc reorder_pd(view_pd.get()->dst_primitive_desc(), diff_src_mpd); + mkldnn::reorder::primitive_desc reorder_pd( + view_pd.get()->dst_primitive_desc(), diff_src_mpd); offsets[axis_] += diff_src_tz[axis_]; - MKLDNNStream::Instance().RegisterPrim(mkldnn::reorder(reorder_pd, *gz_mem, *gradi_mem_.second)); + MKLDNNStream::Instance().RegisterPrim(mkldnn::reorder( + reorder_pd, *gz_mem, *gradi_mem_.second)); CommitOutput(outputs[i], gradi_mem_); } MKLDNNStream::Instance().Submit(); } -}//op -}//mxnet +} // namespace op +} // namespace mxnet #endif diff --git a/src/operator/nn/mkldnn/mkldnn_ops-inl.h b/src/operator/nn/mkldnn/mkldnn_ops-inl.h index 99da180e3cc5..4d2543dc6f25 100644 --- a/src/operator/nn/mkldnn/mkldnn_ops-inl.h +++ b/src/operator/nn/mkldnn/mkldnn_ops-inl.h @@ -74,8 +74,8 @@ void MKLDNNSum_Forward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, /* For copy */ void MKLDNNCopy(const nnvm::NodeAttrs& attrs, const OpContext &ctx, - const NDArray &in_data, const OpReqType &req, - const NDArray &out_data); + const NDArray &in_data, const OpReqType &req, + const NDArray &out_data); /* For concat */ void MKLDNNConcat_Forward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, @@ -85,8 +85,8 @@ void MKLDNNConcat_Backward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, const std::vector& inputs, const std::vector& req, const std::vector& outputs); -} -} +} // namespace op +} // namespace mxnet #endif // MXNET_USE_MKLDNN == 1 #endif // MXNET_OPERATOR_NN_MKLDNN_MKLDNN_OPS_INL_H_ From b31823a426e9da9e054325155542cc36e9e4c0d2 Mon Sep 17 00:00:00 2001 From: wentingj Date: Fri, 8 Dec 2017 17:58:15 +0800 Subject: [PATCH 4/4] fix the coding style for pr of mkldnn concat (fix merge conficts) --- src/operator/nn/concat.cc | 20 ------------------ src/operator/nn/mkldnn/mkldnn_concat.cc | 27 ------------------------- src/operator/nn/mkldnn/mkldnn_ops-inl.h | 13 ------------ 3 files changed, 60 deletions(-) diff --git a/src/operator/nn/concat.cc b/src/operator/nn/concat.cc index fa7fc47767f9..bbd41cb0153d 100644 --- a/src/operator/nn/concat.cc +++ b/src/operator/nn/concat.cc @@ -153,23 +153,12 @@ void ConcatComputeExCPU(const nnvm::NodeAttrs& attrs, CHECK_EQ(req.size(), 1U); if (req[0] == kNullOp) return; #if MXNET_USE_MKLDNN == 1 -<<<<<<< HEAD // MKLDNN support 2D and 4D concat if (inputs[0].shape().ndim() == 2 || inputs[0].shape().ndim() == 4) { if (inputs[0].dtype() == mshadow::kFloat32) { MKLDNNConcat_Forward(attrs, op_ctx, inputs, req, outputs); } } else { -======= - //MKLDNN support 2D and 4D concat - if (inputs[0].shape().ndim() == 2 || inputs[0].shape().ndim() == 4) { - if(inputs[0].dtype() == mshadow::kFloat32) { - MKLDNNConcat_Forward(attrs, op_ctx, inputs, req, outputs); - } - } - else { - // TODO I need to convert format. ->>>>>>> fca247d7db3ef8fc3e27dba030d6cb4d32d5fed0 std::vector in_blobs(inputs.size()); for (size_t i = 0; i < in_blobs.size(); i++) in_blobs[i] = inputs[i].data(); @@ -186,19 +175,10 @@ static void ConcatGradComputeExCPU(const nnvm::NodeAttrs& attrs, const std::vector& req, const std::vector& outputs) { #if MXNET_USE_MKLDNN == 1 if (inputs[0].shape().ndim() == 2 || inputs[0].shape().ndim() == 4) { -<<<<<<< HEAD if (inputs[0].dtype() == mshadow::kFloat32) { MKLDNNConcat_Backward(attrs, ctx, inputs, req, outputs); } } else { -======= - if(inputs[0].dtype() == mshadow::kFloat32) { - MKLDNNConcat_Backward(attrs, ctx, inputs, req, outputs); - } - } - else { - // TODO I need to convert format. ->>>>>>> fca247d7db3ef8fc3e27dba030d6cb4d32d5fed0 std::vector in_blobs(1); in_blobs[0] = inputs[0].data(); std::vector out_blobs(outputs.size()); diff --git a/src/operator/nn/mkldnn/mkldnn_concat.cc b/src/operator/nn/mkldnn/mkldnn_concat.cc index 758487ef81dd..8171784d56cf 100644 --- a/src/operator/nn/mkldnn/mkldnn_concat.cc +++ b/src/operator/nn/mkldnn/mkldnn_concat.cc @@ -38,21 +38,13 @@ void MKLDNNConcat_Forward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, int concat_dim = param.dim; std::vector data_md; std::vector data_mem; -<<<<<<< HEAD for (int i =0; i < num_in_data; i++) { -======= - for(int i =0; i < num_in_data; i++) { ->>>>>>> fca247d7db3ef8fc3e27dba030d6cb4d32d5fed0 std::shared_ptr tmp_mem = in_data[i].GetMKLDNNData(); auto tmp_pd = tmp_mem->get_primitive_desc(); data_md.push_back(tmp_pd); data_mem.push_back(*tmp_mem); } -<<<<<<< HEAD mkldnn::concat::primitive_desc fwd_pd(concat_dim, data_md); -======= - mkldnn::concat::primitive_desc fwd_pd(concat_dim, data_md); ->>>>>>> fca247d7db3ef8fc3e27dba030d6cb4d32d5fed0 auto engine = CpuEngine::Instance().get_engine(); auto out_mem = CreateMKLDNNMem(out_data[concat_enum::kOut], fwd_pd.dst_primitive_desc(), req[concat_enum::kOut]); @@ -69,47 +61,28 @@ void MKLDNNConcat_Backward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, int axis_ = param.dim; auto engine = CpuEngine::Instance().get_engine(); std::shared_ptrgz_mem = inputs[0].GetMKLDNNData(); -<<<<<<< HEAD mkldnn::memory::primitive_desc gz_pd = gz_mem->get_primitive_desc(); /* init the offset */ mkldnn::memory::dims offsets = {0, 0, 0, 0}; for (int i = 0; i < num_in_data; i++) { mkldnn::memory::dims diff_src_tz = {inputs[i+1].shape()[0], inputs[i+1].shape()[1], inputs[i+1].shape()[2], inputs[i+1].shape()[3]}; -======= - mkldnn::memory::primitive_desc gz_pd = gz_mem->get_primitive_desc(); - /* init the offset */ - mkldnn::memory::dims offsets = {0, 0, 0, 0}; - for (int i = 0; i < num_in_data; i++) { - mkldnn::memory::dims diff_src_tz = {inputs[i+1].shape()[0], inputs[i+1].shape()[1], inputs[i+1].shape()[2], inputs[i+1].shape()[3]}; ->>>>>>> fca247d7db3ef8fc3e27dba030d6cb4d32d5fed0 auto diff_src_mpd = inputs[i+1].GetMKLDNNData()->get_primitive_desc(); auto gradi_mem_ = CreateMKLDNNMem(outputs[i], diff_src_mpd, req[i]); // create view from gy to gxs[i] std::shared_ptr view_pd; view_pd.reset(new mkldnn::view::primitive_desc(gz_pd, diff_src_tz, offsets)); // create reorder primitive from gy to gxs[i] -<<<<<<< HEAD mkldnn::reorder::primitive_desc reorder_pd( view_pd.get()->dst_primitive_desc(), diff_src_mpd); offsets[axis_] += diff_src_tz[axis_]; MKLDNNStream::Instance().RegisterPrim(mkldnn::reorder( reorder_pd, *gz_mem, *gradi_mem_.second)); -======= - mkldnn::reorder::primitive_desc reorder_pd(view_pd.get()->dst_primitive_desc(), diff_src_mpd); - offsets[axis_] += diff_src_tz[axis_]; - MKLDNNStream::Instance().RegisterPrim(mkldnn::reorder(reorder_pd, *gz_mem, *gradi_mem_.second)); ->>>>>>> fca247d7db3ef8fc3e27dba030d6cb4d32d5fed0 CommitOutput(outputs[i], gradi_mem_); } MKLDNNStream::Instance().Submit(); } -<<<<<<< HEAD } // namespace op } // namespace mxnet -======= -}//op -}//mxnet ->>>>>>> fca247d7db3ef8fc3e27dba030d6cb4d32d5fed0 #endif diff --git a/src/operator/nn/mkldnn/mkldnn_ops-inl.h b/src/operator/nn/mkldnn/mkldnn_ops-inl.h index b732db4a1a4b..4d2543dc6f25 100644 --- a/src/operator/nn/mkldnn/mkldnn_ops-inl.h +++ b/src/operator/nn/mkldnn/mkldnn_ops-inl.h @@ -85,21 +85,8 @@ void MKLDNNConcat_Backward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, const std::vector& inputs, const std::vector& req, const std::vector& outputs); -<<<<<<< HEAD } // namespace op } // namespace mxnet -======= -/* For concat */ -void MKLDNNConcat_Forward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, - const std::vector &in_data, const std::vector &req, - const std::vector &out_data); -void MKLDNNConcat_Backward(const nnvm::NodeAttrs& attrs, const OpContext &ctx, - const std::vector& inputs, const std::vector& req, - const std::vector& outputs); - -} -} ->>>>>>> fca247d7db3ef8fc3e27dba030d6cb4d32d5fed0 #endif // MXNET_USE_MKLDNN == 1 #endif // MXNET_OPERATOR_NN_MKLDNN_MKLDNN_OPS_INL_H_