MKLDNNLayer.h 3.7 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserve.

Licensed 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. */

#pragma once

#include <vector>
#include "Layer.h"
19
#include "MKLDNNBase.h"
T
tensor-tang 已提交
20
#include "mkldnn.hpp"
T
tensor-tang 已提交
21
#include "paddle/math/MKLDNNMatrix.h"
T
tensor-tang 已提交
22

T
tensor-tang 已提交
23 24 25
DECLARE_bool(use_mkldnn);
DECLARE_bool(use_mkldnn_wgt);

T
tensor-tang 已提交
26 27
namespace paddle {

28 29
class MKLDNNLayer;
typedef std::shared_ptr<MKLDNNLayer> MKLDNNLayerPtr;
T
tensor-tang 已提交
30 31

/**
32
 * @brief Base class of MKLDNNlayer.
T
tensor-tang 已提交
33 34
 *
 */
35
class MKLDNNLayer : public Layer {
T
tensor-tang 已提交
36 37 38 39 40 41 42 43
protected:
  // batch size
  int bs_;
  // input image channel, height and width
  int ic_, ih_, iw_;
  // output image channel, height and width
  int oc_, oh_, ow_;

T
tensor-tang 已提交
44 45 46
  // backward also need reset after reset forward handle
  bool needResetBwd_;

T
tensor-tang 已提交
47 48
  // mkldnn engine, stream and primivtives
  mkldnn::engine engine_;
49
  std::shared_ptr<MKLDNNStream> stream_;
T
tensor-tang 已提交
50
  std::shared_ptr<mkldnn::primitive> fwd_;
T
tensor-tang 已提交
51 52
  std::shared_ptr<mkldnn::primitive> bwdWgt_;
  std::shared_ptr<mkldnn::primitive> bwdData_;
T
tensor-tang 已提交
53 54 55
  std::vector<mkldnn::primitive> pipelineFwd_;
  std::vector<mkldnn::primitive> pipelineBwd_;

56
  // TODO(TJ): change below memory as MKLDNNMatrixPtr type
T
tensor-tang 已提交
57 58 59 60 61 62 63 64 65
  std::shared_ptr<mkldnn::memory> inVal_;
  std::shared_ptr<mkldnn::memory> inGrad_;
  std::shared_ptr<mkldnn::memory> outVal_;
  std::shared_ptr<mkldnn::memory> outGrad_;
  std::shared_ptr<mkldnn::memory> wgtVal_;
  std::shared_ptr<mkldnn::memory> wgtGrad_;
  std::shared_ptr<mkldnn::memory> biasVal_;
  std::shared_ptr<mkldnn::memory> biasGrad_;

T
tensor-tang 已提交
66
public:
67
  explicit MKLDNNLayer(const LayerConfig& config)
T
tensor-tang 已提交
68 69 70 71 72 73 74 75
      : Layer(config),
        bs_(0),
        ic_(0),
        ih_(0),
        iw_(0),
        oc_(0),
        oh_(0),
        ow_(0),
T
tensor-tang 已提交
76
        needResetBwd_(true),
T
tensor-tang 已提交
77
        engine_(mkldnn::engine::cpu, 0),
T
tensor-tang 已提交
78 79 80 81
        stream_(nullptr),
        fwd_(nullptr),
        bwdWgt_(nullptr),
        bwdData_(nullptr) {}
T
tensor-tang 已提交
82

83
  ~MKLDNNLayer() {}
T
tensor-tang 已提交
84

T
tensor-tang 已提交
85 86 87 88 89 90 91 92 93
  virtual bool init(const LayerMap& layerMap,
                    const ParameterMap& parameterMap) {
    if (!Layer::init(layerMap, parameterMap)) {
      return false;
    }

    CHECK(FLAGS_use_mkldnn) << "MkldnnLayers only support use_mkldnn."
                            << "Please set WITH_MKLDNN=ON "
                            << "and set use_mkldnn=True";
94 95
    stream_.reset(new MKLDNNStream());
    engine_ = CPUEngine::Instance().getEngine();
T
tensor-tang 已提交
96

T
tensor-tang 已提交
97 98 99
    // TODO(TJ): deivecId
    return true;
  }
T
tensor-tang 已提交
100 101 102 103 104

  /**
   * convert weight from paddle format to mkldnn format
   * weight_ will be override
   */
105
  virtual void convertWeightsFromPaddle() {}
T
tensor-tang 已提交
106 107 108 109 110

  /**
   * convert mkldnn weight to paddle format
   * weight_ will be override
   */
111
  virtual void convertWeightsToPaddle() {}
T
tensor-tang 已提交
112

T
tensor-tang 已提交
113 114 115 116 117 118 119 120
  /**
   * print info about sizes
   */
  virtual void printSizeInfo() {
    VLOG(MKLDNN_SIZES) << getName() << ": bs: " << bs_ << ", ic: " << ic_
                       << ", ih: " << ih_ << ", iw: " << iw_ << ", oc: " << oc_
                       << ", oh: " << oh_ << ", ow: " << ow_;
  }
T
tensor-tang 已提交
121

T
tensor-tang 已提交
122 123 124 125 126
  // TODO(TJ): move to MkldnnMatrix
  // create memory desc
  inline mkldnn::memory::desc createMD(
      mkldnn::memory::dims dims,
      mkldnn::memory::format fmt,
T
tensor-tang 已提交
127 128 129 130
      mkldnn::memory::data_type type = mkldnn::memory::data_type::f32) {
    // TODO(TJ): isFmtSuppoted(fmt)
    return mkldnn::memory::desc(dims, type, fmt);
  }
T
tensor-tang 已提交
131 132 133
};

}  // namespace paddle