MKLDNNBatchNormLayer.h 4.2 KB
Newer Older
1
/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserved.
T
tensor-tang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

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 "MKLDNNLayer.h"
#include "mkldnn.hpp"

namespace paddle {
typedef mkldnn::batch_normalization_forward bn_fwd;
typedef mkldnn::batch_normalization_backward bn_bwd;

/**
 * @brief A subclass of MKLDNNLayer BatchNorm layer.
 *
 * The config file api is mkldnn_batch_norm
 */
class MKLDNNBatchNormLayer : public MKLDNNLayer {
W
Wu Yi 已提交
30
 protected:
T
tensor-tang 已提交
31 32 33 34
  // save forward primitive_desc, which can be used backward
  std::shared_ptr<bn_fwd::primitive_desc> fwdPD_;

  // Epsilon value used in the batch normalization formula.
35
  real epsilon_;
P
peterzhang2029 已提交
36

T
tensor-tang 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  // weight and bias in paddle
  std::unique_ptr<Weight> weight_;
  std::unique_ptr<Weight> biases_;
  // mkldnn use a large buffer store both scale and shift
  // which are weight and bias in paddle corresponding.
  MatrixPtr valueScaleShift_;
  MatrixPtr gradScaleShift_;
  // Moving average of mean.
  std::unique_ptr<Weight> movingMean_;
  // Moving average of variance.
  std::unique_ptr<Weight> movingVar_;

  // if useGlobalStats_ is true, will use the loaded mean and variance.
  // otherwise, calculate mean and variance in every mini-batch.
  bool useGlobalStats_;
  // used in MKLDNN primitive desc
  unsigned flags_;
  // use to compute moving mean and variance.
  real movingAvgFraction_;
  // whether the weight has been init
  bool hasInitedWgt_;

  // local mean and variance
T
tensor-tang 已提交
60 61 62 63
  // when useGlobalStats_ they are loaded from moving mean and variance
  // when do not useGlobalStats_ they are calculated from this mini-batch
  MKLDNNMatrixPtr mean_;
  MKLDNNMatrixPtr var_;
T
tensor-tang 已提交
64

W
Wu Yi 已提交
65
 public:
T
tensor-tang 已提交
66 67 68 69 70 71 72 73 74 75 76
  explicit MKLDNNBatchNormLayer(const LayerConfig& config)
      : MKLDNNLayer(config), useGlobalStats_(true), hasInitedWgt_(false) {}

  ~MKLDNNBatchNormLayer() {}

  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override;

  void forward(PassType passType) override;

  void reshape(
77
      int& bs, int& ic, int& ih, int& iw, int& oc, int& oh, int& ow) override;
T
tensor-tang 已提交
78 79

  void resetFwd(std::vector<mkldnn::primitive>& pipeline,
80
                std::vector<MKLDNNMatrixPtr>& inputs,
T
tensor-tang 已提交
81 82 83
                MKLDNNMatrixPtr& out) override;

  void resetBwd(std::vector<mkldnn::primitive>& pipeline,
84
                std::vector<MKLDNNMatrixPtr>& inputs,
T
tensor-tang 已提交
85 86 87 88 89 90
                MKLDNNMatrixPtr& out) override;

  void updateWeights(const UpdateCallback& callback) override;

  void convertWeightsFromPaddle() override;

W
Wu Yi 已提交
91
 protected:
T
tensor-tang 已提交
92 93 94 95 96 97
  void initWeight();
  /**
   * cal moving mean and variance.
   * moving = moving * AvgFraction + local * (1 - AvgFraction)
   */
  void calMovingMeanAndVar();
98

T
tensor-tang 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  void resetFwdBuffers(MKLDNNMatrixPtr& in,
                       MKLDNNMatrixPtr& wgt,
                       MKLDNNMatrixPtr& out);
  void resetFwdPD(std::shared_ptr<bn_fwd::primitive_desc>& pd,
                  MKLDNNMatrixPtr in,
                  MKLDNNMatrixPtr wgt,
                  MKLDNNMatrixPtr out);
  void resetFwdPipeline(std::vector<mkldnn::primitive>& pipeline,
                        std::shared_ptr<bn_fwd::primitive_desc>& pd,
                        MKLDNNMatrixPtr& in,
                        MKLDNNMatrixPtr& wgt,
                        MKLDNNMatrixPtr& out);
  void resetBwdBuffers(MKLDNNMatrixPtr& in,
                       MKLDNNMatrixPtr& wgt,
                       MKLDNNMatrixPtr& out);
  void resetBwdPD(std::shared_ptr<bn_bwd::primitive_desc>& pd,
                  MKLDNNMatrixPtr& in,
                  MKLDNNMatrixPtr& wgt,
                  MKLDNNMatrixPtr& out);
  void resetBwdPipeline(std::vector<mkldnn::primitive>& pipeline,
                        std::shared_ptr<bn_bwd::primitive_desc>& pd,
                        MKLDNNMatrixPtr& in,
                        MKLDNNMatrixPtr& wgt,
                        MKLDNNMatrixPtr& out);
};

}  // namespace paddle