CudnnBatchNormLayer.h 2.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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

P
peterzhang2029 已提交
17
#include <cudnn.h>
Z
zhangjinchao01 已提交
18
#include "BatchNormBaseLayer.h"
Y
Yu Yang 已提交
19 20
#include "Layer.h"
#include "paddle/utils/Stat.h"
Z
zhangjinchao01 已提交
21 22 23 24 25

namespace paddle {

/**
 * @brief Cudnn Batch normalization layer use to cuDNN lib to implentment.
26 27
 * @note Cudnn version must >= v4.0, and better to use the latest version
 * (v5.1).
Z
zhangjinchao01 已提交
28 29 30 31 32 33 34 35 36 37 38
 *
 * The config file api is batch_norm_layer.
 */

class CudnnBatchNormLayer : public BatchNormBaseLayer {
public:
  explicit CudnnBatchNormLayer(const LayerConfig& config)
      : BatchNormBaseLayer(config) {}

  ~CudnnBatchNormLayer();

Y
Yu Yang 已提交
39 40
  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override;
Z
zhangjinchao01 已提交
41 42 43 44 45
  /**
   * reshape tensor of ioDesc_.
   */
  void reshape(int batchSize);

Y
Yu Yang 已提交
46 47
  void forward(PassType passType) override;
  void backward(const UpdateCallback& callback = nullptr) override;
Z
zhangjinchao01 已提交
48 49

protected:
P
peterzhang2029 已提交
50
  /// Epsilon value used in the batch normalization formula.
51 52
  /// Same epsilon value should be used in forward and backward functions.
  double eps_;
Z
zhangjinchao01 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

  /// Input/output tensor descriptor desc
  hl_tensor_descriptor ioDesc_;
  /// Shared tensor descriptor desc for the 6 tenros:
  /// bnScale, bnBias, running mean/var, save_mean/var
  hl_tensor_descriptor bnParamDesc_;

  /**
   * @brief The gradient of weight and bias in cudnn api can not be empty.
   * If set is_static for weight or bias, it will not allocate memory for them,
   * and the gradient is NULL. In this case, will use two matrix.
   */
  MatrixPtr tmpWGrad_, tmpBiasGrad_;
};

}  // namespace paddle