MkldnnLayer.h 4.2 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"
T
tensor-tang 已提交
19
#include "MkldnnBase.h"
T
tensor-tang 已提交
20 21 22 23 24 25 26 27 28 29 30 31
#include "mkldnn.hpp"

namespace paddle {

class MkldnnLayer;
typedef std::shared_ptr<MkldnnLayer> MkldnnLayerPtr;

/**
 * @brief Base class of Mkldnnlayer.
 *
 */
class MkldnnLayer : public Layer {
T
tensor-tang 已提交
32 33 34 35 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_;

  // mkldnn engine, stream and primivtives
  mkldnn::engine engine_;
  std::shared_ptr<MkldnnStream> stream_;
  std::shared_ptr<mkldnn::primitive> fwd_;
T
tensor-tang 已提交
44 45
  std::shared_ptr<mkldnn::primitive> bwdWgt_;
  std::shared_ptr<mkldnn::primitive> bwdData_;
T
tensor-tang 已提交
46 47 48
  std::vector<mkldnn::primitive> pipelineFwd_;
  std::vector<mkldnn::primitive> pipelineBwd_;

T
tensor-tang 已提交
49 50 51 52 53 54 55 56 57 58 59 60
  // TODO(TJ): change below memory as MkldnnMatrixPtr type
  // input == bottom, output == top
  // value == data, grad == diff
  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 已提交
61
public:
T
tensor-tang 已提交
62 63 64 65 66 67 68 69 70 71
  explicit MkldnnLayer(const LayerConfig& config)
      : Layer(config),
        bs_(0),
        ic_(0),
        ih_(0),
        iw_(0),
        oc_(0),
        oh_(0),
        ow_(0),
        engine_(mkldnn::engine::cpu, 0),
T
tensor-tang 已提交
72 73 74 75
        stream_(nullptr),
        fwd_(nullptr),
        bwdWgt_(nullptr),
        bwdData_(nullptr) {}
T
tensor-tang 已提交
76 77 78

  ~MkldnnLayer() {}

T
tensor-tang 已提交
79 80
  virtual bool init(const LayerMap& layerMap, const ParameterMap& parameterMap);

T
tensor-tang 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94
  virtual void printSizeInfo();

  /**
   * convert weight from paddle format to mkldnn format
   * weight_ will be override
   */
  virtual void cvtWgtFromPaddle() { ; }

  /**
   * convert mkldnn weight to paddle format
   * weight_ will be override
   */
  virtual void cvtWgtToPaddle() { ; }

T
tensor-tang 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  void resetForwardFC(int bs,
                      int ic,
                      int ih,
                      int iw,
                      real* botData,
                      int oc,
                      real* topData,
                      real* wgtData,
                      real* biasData);

  void mkldnnForwardFC(int bs,
                       int ic,
                       int ih,
                       int iw,
                       real* botData,
                       int oc,
                       real* topData,
                       real* wgtData,
                       real* biasData);
T
tensor-tang 已提交
114

T
tensor-tang 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  void resetBackwardFC(int bs,
                       int ic,
                       int ih,
                       int iw,
                       real* botDiff,
                       real* botData,
                       int oc,
                       real* topDiff,
                       real* wgtDiff,
                       real* wgtData,
                       real* biasDiff);

  void mkldnnBackwardFC(int bs,
                        int ic,
                        int ih,
                        int iw,
                        real* botDiff,
                        real* botData,
                        int oc,
                        real* topDiff,
                        real* wgtDiff,
                        real* wgtData,
                        real* biasDiff);

T
tensor-tang 已提交
139 140 141 142 143 144
  // TODO(TJ): move to MkldnnMatrix
  // create memory desc
  inline mkldnn::memory::desc createMD(
      mkldnn::memory::dims dims,
      mkldnn::memory::format fmt,
      mkldnn::memory::data_type type = mkldnn::memory::data_type::f32);
T
tensor-tang 已提交
145 146 147
};

}  // namespace paddle