lrn_op.h 5.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
G
gongweibao 已提交
2

L
Luo Tao 已提交
3 4 5
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
G
gongweibao 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
G
gongweibao 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
G
gongweibao 已提交
14 15 16

#pragma once

17 18
#include <string>
#include "paddle/fluid/framework/data_layout.h"
Y
Yi Wang 已提交
19 20 21
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/math_function.h"
G
gongweibao 已提交
22 23 24 25

namespace paddle {
namespace operators {

26 27
using DataLayout = framework::DataLayout;

28 29 30 31 32
template <typename place, typename T>
struct LRNFunctor {
  void operator()(const framework::ExecutionContext& ctx,
                  const framework::Tensor& input, framework::Tensor* out,
                  framework::Tensor* mid, int N, int C, int H, int W, int n,
33 34
                  T k, T alpha, T beta,
                  const DataLayout data_layout = DataLayout::kAnyLayout);
35 36
};

Q
QI JUN 已提交
37
template <typename DeviceContext, typename T>
G
gongweibao 已提交
38 39 40 41 42 43 44 45 46
class LRNKernel : public framework::OpKernel<T> {
 public:
  using Tensor = framework::Tensor;

  // f(x) = x * ( k + alpha * SUM((x)^2) )^(-beta)
  // x represents inputs
  // f(x) represents outputs
  void Compute(const framework::ExecutionContext& ctx) const override {
    // input
47 48
    const Tensor& x = *ctx.Input<Tensor>("X");
    auto x_dims = x.dims();
G
gongweibao 已提交
49

50 51 52
    const std::string data_layout_str = ctx.Attr<std::string>("data_format");
    const framework::DataLayout data_layout =
        framework::StringToDataLayout(data_layout_str);
G
gongweibao 已提交
53 54
    // NCHW
    int N = x_dims[0];
55 56 57
    int C = (data_layout != DataLayout::kNHWC ? x_dims[1] : x_dims[3]);
    int H = (data_layout != DataLayout::kNHWC ? x_dims[2] : x_dims[1]);
    int W = (data_layout != DataLayout::kNHWC ? x_dims[3] : x_dims[2]);
G
gongweibao 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    Tensor* out = ctx.Output<Tensor>("Out");
    out->mutable_data<T>(ctx.GetPlace());

    // MidOut save the intermediate result for backward
    Tensor* mid = ctx.Output<Tensor>("MidOut");
    mid->mutable_data<T>(ctx.GetPlace());

    int n = ctx.Attr<int>("n");
    T alpha = ctx.Attr<float>("alpha");
    T beta = ctx.Attr<float>("beta");
    T k = ctx.Attr<float>("k");

    PADDLE_ENFORCE(alpha >= 0.0, "alpha should >= 0.0");
    PADDLE_ENFORCE(beta >= 0.0, "beta should >= 0.0");
    PADDLE_ENFORCE(k >= 0.0, "k should >= 0.0");

Q
QI JUN 已提交
75
    LRNFunctor<DeviceContext, T> f;
76
    f(ctx, x, out, mid, N, C, H, W, n, k, alpha, beta, data_layout);
G
gongweibao 已提交
77 78 79
  }
};

Q
QI JUN 已提交
80
template <typename DeviceContext, typename T>
81 82 83 84 85
struct LRNGradFunctor {
  void operator()(const framework::ExecutionContext& ctx,
                  const framework::Tensor& x, const framework::Tensor& out,
                  const framework::Tensor& mid, framework::Tensor* x_g,
                  const framework::Tensor& out_g, int N, int C, int H, int W,
86 87
                  int n, T alpha, T beta,
                  const DataLayout data_layout = DataLayout::kAnyLayout);
88 89
};

G
gongweibao 已提交
90 91 92 93 94 95 96 97
/**
 * \brief Backward calculation for normalization with across maps.
 *
 * Function implementation:
 *
 * The implementation of this Function is derived from the
 * CrossMapNormalFunc implementation.
 *
98
 * InputGrad = OutputGrad * MidOut ^ (-beta)
G
gongweibao 已提交
99 100 101 102 103 104 105 106 107 108
 *    -- upper
 *  + > (OutputGrad * OutputValue * (-2 * alpha * beta) / MidOut) * InputValue
 *    -- lower
 *
 * The data of inputs/outputs format is the same as the forward interface
 * and is NCHW.
 *
 * The upper and lower is the same as forward. The logic of the sum
 * is also the same as forward.
 */
Q
QI JUN 已提交
109
template <typename DeviceContext, typename T>
G
gongweibao 已提交
110 111 112 113
class LRNGradKernel : public framework::OpKernel<T> {
 public:
  using Tensor = framework::Tensor;
  void Compute(const framework::ExecutionContext& ctx) const override {
114 115 116 117
    const Tensor& x = *ctx.Input<Tensor>("X");
    const Tensor& out = *ctx.Input<Tensor>("Out");
    const Tensor& out_g = *ctx.Input<Tensor>(framework::GradVarName("Out"));
    const Tensor& mid = *ctx.Input<Tensor>("MidOut");
118 119 120
    const std::string data_layout_str = ctx.Attr<std::string>("data_format");
    const framework::DataLayout data_layout =
        framework::StringToDataLayout(data_layout_str);
G
gongweibao 已提交
121 122 123 124

    auto x_g = ctx.Output<Tensor>(framework::GradVarName("X"));
    x_g->mutable_data<T>(ctx.GetPlace());

125
    auto x_dims = x.dims();
G
gongweibao 已提交
126
    int N = x_dims[0];
127 128 129
    int C = (data_layout != DataLayout::kNHWC ? x_dims[1] : x_dims[3]);
    int H = (data_layout != DataLayout::kNHWC ? x_dims[2] : x_dims[1]);
    int W = (data_layout != DataLayout::kNHWC ? x_dims[3] : x_dims[2]);
G
gongweibao 已提交
130 131 132 133

    int n = ctx.Attr<int>("n");
    T alpha = ctx.Attr<T>("alpha");
    T beta = ctx.Attr<T>("beta");
134

135 136 137 138
    PADDLE_ENFORCE(
        !ctx.Attr<bool>("is_test"),
        "is_test attribute should be set to False in training phase.");

Q
QI JUN 已提交
139
    LRNGradFunctor<DeviceContext, T> f;
140
    f(ctx, x, out, mid, x_g, out_g, N, C, H, W, n, alpha, beta, data_layout);
G
gongweibao 已提交
141 142 143 144 145
  }
};

}  // namespace operators
}  // namespace paddle