prelu_op.h 4.2 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 48 49 50 51 52 53 54 55
      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") {
      for (i = 0; i < numel; i++) {
        o_ptr[i] = x_ptr[i] > 0 ? x_ptr[i] : alpha_ptr[i] * x_ptr[i];
      }
    } 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 已提交
56 57 58
  }
};

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

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

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

    // TODO(Guanzhong): add GPU kernels
Z
zchen0211 已提交
122 123 124 125 126
  }
};

}  // namespace operators
}  // namespace paddle