From 66fdbd0ceea6711183353b7f4af168d420166a2f Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Wed, 13 Sep 2017 13:09:07 +0800 Subject: [PATCH] add some comment and simplify some code --- paddle/gserver/layers/MKLDNNConvLayer.cpp | 4 ++-- paddle/gserver/layers/MKLDNNConvLayer.h | 24 ++++++++--------------- paddle/math/MKLDNNMatrix.cpp | 6 +----- paddle/math/MKLDNNMatrix.h | 6 ++++++ python/paddle/trainer/config_parser.py | 5 +---- 5 files changed, 18 insertions(+), 27 deletions(-) diff --git a/paddle/gserver/layers/MKLDNNConvLayer.cpp b/paddle/gserver/layers/MKLDNNConvLayer.cpp index 617874def..19891043a 100644 --- a/paddle/gserver/layers/MKLDNNConvLayer.cpp +++ b/paddle/gserver/layers/MKLDNNConvLayer.cpp @@ -47,9 +47,9 @@ bool MKLDNNConvLayer::init(const LayerMap& layerMap, sw_ = conf.stride(); sh_ = conf.stride_y(); gp_ = conf.groups(); - oh_ = conf.has_output_y() ? conf.output_y() : conf.output_x(); + oh_ = conf.output_y(); ow_ = conf.output_x(); - ih_ = conf.has_img_size_y() ? conf.img_size_y() : conf.img_size(); + ih_ = conf.img_size_y(); iw_ = conf.img_size(); caffeMode_ = conf.caffe_mode(); CHECK(caffeMode_) << "Only support caffe mode yet"; diff --git a/paddle/gserver/layers/MKLDNNConvLayer.h b/paddle/gserver/layers/MKLDNNConvLayer.h index 58891ff5e..d1a78ac1c 100644 --- a/paddle/gserver/layers/MKLDNNConvLayer.h +++ b/paddle/gserver/layers/MKLDNNConvLayer.h @@ -37,38 +37,30 @@ protected: // group number int gp_; - // in backward data the format is different with wgtVal_ + // in resetBwdData, the format of wgtValBwdData_ is different with wgtVal_ MKLDNNMatrixPtr wgtValBwdData_; + // convert handle from wgtVal_ to wgtValBwdData_ std::shared_ptr cvtWgtVal_; - // save forward primitive_desc use for backward + // save forward primitive_desc, which can be used backward std::shared_ptr fwdPD_; - // MKLDNNMatrixPtr with cpu device for conversion between MKLDNN device + // MKLDNNMatrixPtr which should be created from CPU Device MKLDNNMatrixPtr cpuInVal_; MKLDNNMatrixPtr cpuInGrad_; MKLDNNMatrixPtr cpuOutVal_; MKLDNNMatrixPtr cpuOutGrad_; + // convert handle between CPU device and MKLDNN device std::shared_ptr cvtInVal_; std::shared_ptr cvtInGrad_; std::shared_ptr cvtOutVal_; std::shared_ptr cvtOutGrad_; - // if has already init the weight + // whether the weight has been init bool hasInitedWgt_; - // True by default. This impact the calculation of output size. - // For example: - // - input(+padding): 0123456789 - // - imageSize(+padding) = 10; - // - filterSize = 3; - // - stride = 2; - // - caffeMode_ is true: - // - output: (012), (234), (456), (678) - // - outputSize = 4; - // - caffeMode_ is false: - // - output: (012), (234), (456), (678), (9) - // - outputSize = 5; + // true by default, which impact the calculation of output image size. + // details can refer to mathUtil.h bool caffeMode_; // weight and bias diff --git a/paddle/math/MKLDNNMatrix.cpp b/paddle/math/MKLDNNMatrix.cpp index a71ac12af..0778bb63b 100644 --- a/paddle/math/MKLDNNMatrix.cpp +++ b/paddle/math/MKLDNNMatrix.cpp @@ -52,11 +52,7 @@ MKLDNNMatrixPtr MKLDNNMatrix::create(MatrixPtr m, std::shared_ptr MKLDNNMatrix::createReorder(const MKLDNNMatrixPtr& src, const MKLDNNMatrixPtr& dst, bool checkData) { - if (src == dst) { - return nullptr; - } - - if (src->getPrimitiveDesc() == dst->getPrimitiveDesc()) { + if (src == dst || src->getPrimitiveDesc() == dst->getPrimitiveDesc()) { return nullptr; } diff --git a/paddle/math/MKLDNNMatrix.h b/paddle/math/MKLDNNMatrix.h index c7765369c..0aa130b4a 100644 --- a/paddle/math/MKLDNNMatrix.h +++ b/paddle/math/MKLDNNMatrix.h @@ -65,6 +65,12 @@ public: /** * Create reorder primitive. + * Create a mkldnn::reorder handle for converting src MKLDNNMatrix to dst. + * checkData: for whether to check the data handle of src and dst is the same. + * if true, means check it and do not want support inplace reorder; + * otherwise do not check data which means the created reorder + * maybe inplace buffer and do not guarantee the logical is correct + * since not all format or conversion support inplace. */ static std::shared_ptr createReorder( const MKLDNNMatrixPtr& src, diff --git a/python/paddle/trainer/config_parser.py b/python/paddle/trainer/config_parser.py index d633cae4a..58ebcd1d8 100644 --- a/python/paddle/trainer/config_parser.py +++ b/python/paddle/trainer/config_parser.py @@ -2073,10 +2073,7 @@ class ConvLayerBase(LayerBase): (parallel_nn == 0 or self.config.device > -1)): self.layer_type = "cudnn_conv" else: - if (use_mkldnn == 1): - self.layer_type = "mkldnn_conv" - else: - self.layer_type = "exconv" + self.layer_type = "mkldnn_conv" if use_mkldnn else "exconv" # need to specify layer in config self.config.type = self.layer_type -- GitLab