prelu_op.h 4.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zchen0211 已提交
2 3 4 5 6 7 8 9 10 11 12
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
J
jerrywgz 已提交
13
#include <string>
Y
Yi Wang 已提交
14 15 16
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/platform/transform.h"
Z
zchen0211 已提交
17 18 19 20
namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
Z
zchen0211 已提交
21
using platform::Transform;
Z
zchen0211 已提交
22

Q
QI JUN 已提交
23
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
24
class PReluKernel : public framework::OpKernel<T> {
Z
zchen0211 已提交
25 26
 public:
  void Compute(const framework::ExecutionContext& context) const override {
Z
prelu  
zchen0211 已提交
27 28 29
    auto* x = context.Input<Tensor>("X");
    auto* alpha = context.Input<Tensor>("Alpha");
    auto* out = context.Output<Tensor>("Out");
Z
zchen0211 已提交
30

Z
prelu  
zchen0211 已提交
31 32
    const T* x_ptr = x->data<T>();
    T* o_ptr = out->mutable_data<T>(context.GetPlace());
Z
zchen0211 已提交
33

J
jerrywgz 已提交
34
    const T* alpha_ptr = alpha->data<T>();
Y
Yu Yang 已提交
35
    auto& mode = context.Attr<std::string>("mode");
Z
zchen0211 已提交
36

Z
prelu  
zchen0211 已提交
37
    int numel = x->numel();
J
jerrywgz 已提交
38 39 40 41
    auto dim = x->dims();
    int index = 0;
    int i = 0;
    if (mode == "channel") {
T
tensor-tang 已提交
42
      int temp = numel / (dim[0] * dim[1]);
J
jerrywgz 已提交
43 44 45 46 47
      for (i = 0; i < numel; i++) {
        index = (i / temp) % dim[1];
        o_ptr[i] = x_ptr[i] > 0 ? x_ptr[i] : alpha_ptr[index] * x_ptr[i];
      }
    } else if (mode == "element") {
48
      int temp = numel / dim[0];
J
jerrywgz 已提交
49
      for (i = 0; i < numel; i++) {
50 51
        index = i % temp;
        o_ptr[i] = x_ptr[i] > 0 ? x_ptr[i] : alpha_ptr[index] * x_ptr[i];
J
jerrywgz 已提交
52 53 54 55 56 57
      }
    } else {
      for (i = 0; i < numel; i++) {
        o_ptr[i] = x_ptr[i] > 0 ? x_ptr[i] : alpha_ptr[0] * x_ptr[i];
      }
    }
Z
zchen0211 已提交
58 59 60
  }
};

Q
QI JUN 已提交
61
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
62
class PReluGradKernel : public framework::OpKernel<T> {
Z
zchen0211 已提交
63 64
 public:
  void Compute(const framework::ExecutionContext& context) const override {
J
jerrywgz 已提交
65
    auto* x = context.Input<Tensor>("X");
Z
prelu  
zchen0211 已提交
66 67
    auto* dx = context.Output<Tensor>(framework::GradVarName("X"));
    auto* dout = context.Input<Tensor>(framework::GradVarName("Out"));
J
jerrywgz 已提交
68
    auto* dalpha = context.Output<Tensor>(framework::GradVarName("Alpha"));
Z
prelu  
zchen0211 已提交
69
    auto* alpha = context.Input<Tensor>("Alpha");
J
jerrywgz 已提交
70 71
    const T* alpha_ptr = alpha->data<T>();
    const T* x_ptr = x->data<T>();
Z
prelu  
zchen0211 已提交
72
    const T* dout_ptr = dout->data<T>();
J
jerrywgz 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85
    std::string mode = context.Attr<std::string>("mode");
    int numel = x->numel();
    auto dim = x->dims();
    int index = 0;
    int i = 0;
    int temp = 0;
    if (dx) {
      T* dx_ptr = dx->mutable_data<T>(context.GetPlace());
      if (mode == "channel") {
        for (i = 0; i < numel; i++) {
          temp = numel / (dim[0] * dim[1]);
          index = (i / temp) % dim[1];
          dx_ptr[i] =
86
              x_ptr[i] > 0 ? dout_ptr[i] : alpha_ptr[index] * dout_ptr[i];
J
jerrywgz 已提交
87 88
        }
      } else if (mode == "element") {
89
        temp = numel / dim[0];
J
jerrywgz 已提交
90
        for (i = 0; i < numel; i++) {
91 92 93
          index = i % temp;
          dx_ptr[i] =
              x_ptr[i] > 0 ? dout_ptr[i] : alpha_ptr[index] * dout_ptr[i];
J
jerrywgz 已提交
94 95 96
        }
      } else {
        for (i = 0; i < numel; i++) {
97
          dx_ptr[i] = x_ptr[i] > 0 ? dout_ptr[i] : alpha_ptr[0] * dout_ptr[i];
J
jerrywgz 已提交
98 99 100 101 102 103 104
        }
      }
    }

    index = 0;
    if (dalpha) {
      T* dalpha_ptr = dalpha->mutable_data<T>(context.GetPlace());
Y
Yu Yang 已提交
105 106
      memset(dalpha_ptr, 0, sizeof(T) * dalpha->numel());

J
jerrywgz 已提交
107 108 109 110
      if (mode == "channel") {
        for (i = 0; i < numel; i++) {
          temp = numel / (dim[0] * dim[1]);
          index = (i / temp) % dim[1];
111
          dalpha_ptr[index] += x_ptr[i] > 0 ? 0 : x_ptr[i] * dout_ptr[i];
J
jerrywgz 已提交
112 113
        }
      } else if (mode == "element") {
114
        temp = numel / dim[0];
J
jerrywgz 已提交
115
        for (i = 0; i < numel; i++) {
116 117
          index = i % temp;
          dalpha_ptr[index] += x_ptr[i] > 0 ? 0 : x_ptr[i] * dout_ptr[i];
J
jerrywgz 已提交
118 119 120
        }
      } else {
        for (i = 0; i < numel; i++) {
121
          dalpha_ptr[0] += x_ptr[i] > 0 ? 0 : x_ptr[i] * dout_ptr[i];
J
jerrywgz 已提交
122 123 124 125 126
        }
      }
    }

    // TODO(Guanzhong): add GPU kernels
Z
zchen0211 已提交
127 128 129 130 131
  }
};

}  // namespace operators
}  // namespace paddle