CudnnBatchNormLayer.h 2.1 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 17

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 "BatchNormBaseLayer.h"
Y
Yu Yang 已提交
18 19
#include "Layer.h"
#include "paddle/utils/Stat.h"
Z
zhangjinchao01 已提交
20 21 22 23 24

namespace paddle {

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

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

  ~CudnnBatchNormLayer();

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

Y
Yu Yang 已提交
45 46
  void forward(PassType passType) override;
  void backward(const UpdateCallback& callback = nullptr) override;
Z
zhangjinchao01 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

protected:
  /**
   * Epsilon value used in the batch normalization formula.
   * Minimum allowed value is CUDNN_BN_MIN_EPSILON defined in cudnn.h.
   * Same epsilon value should be used in forward and backward functions.
   */
  static const double EPS;

  /// 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