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

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 "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
Z
zchen0211 已提交
18
#include "paddle/platform/transform.h"
Z
zchen0211 已提交
19 20 21 22 23

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
Z
zchen0211 已提交
24
using platform::Transform;
Z
zchen0211 已提交
25

Z
zchen0211 已提交
26
template <typename T>
Z
zchen0211 已提交
27
class PReluFunctor {
Z
zchen0211 已提交
28
 public:
Z
zchen0211 已提交
29
  explicit PReluFunctor(const T* alpha) : alpha_(alpha) {}
Z
zchen0211 已提交
30

Z
prelu  
zchen0211 已提交
31 32 33
  HOSTDEVICE T operator()(const T& x) const {
    if (x > 0)
      return x;
Z
zchen0211 已提交
34
    else
Z
zchen0211 已提交
35
      return x * (*alpha_);
Z
zchen0211 已提交
36 37 38
  }

 private:
Z
zchen0211 已提交
39
  const T* alpha_;
Z
zchen0211 已提交
40 41
};

Z
prelu  
zchen0211 已提交
42
template <typename Place, typename T>
Z
fix  
zchen0211 已提交
43
class PReluKernel : public framework::OpKernel {
Z
zchen0211 已提交
44 45
 public:
  void Compute(const framework::ExecutionContext& context) const override {
Z
prelu  
zchen0211 已提交
46 47 48
    auto* x = context.Input<Tensor>("X");
    auto* alpha = context.Input<Tensor>("Alpha");
    auto* out = context.Output<Tensor>("Out");
Z
zchen0211 已提交
49

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

Z
zchen0211 已提交
53
    auto* alpha_ptr = alpha->data<T>();
Z
zchen0211 已提交
54

Z
prelu  
zchen0211 已提交
55
    int numel = x->numel();
Z
zchen0211 已提交
56

Z
zchen0211 已提交
57
    auto place = context.GetPlace();
Z
zchen0211 已提交
58
    Transform(place, x_ptr, x_ptr + numel, o_ptr, PReluFunctor<T>(alpha_ptr));
Z
zchen0211 已提交
59 60 61
  }
};

Z
zchen0211 已提交
62
template <typename T>
Z
zchen0211 已提交
63
class PReluGradFunctor {
Z
zchen0211 已提交
64
 public:
Z
zchen0211 已提交
65
  explicit PReluGradFunctor(const T* alpha) : alpha_(alpha) {}
Z
zchen0211 已提交
66

Z
prelu  
zchen0211 已提交
67 68 69
  HOSTDEVICE T operator()(const T& out, const T& dout) const {
    if (out > 0)
      return dout;
Z
zchen0211 已提交
70
    else
Z
zchen0211 已提交
71
      return dout * (*alpha_);
Z
zchen0211 已提交
72 73 74
  }

 private:
Z
zchen0211 已提交
75
  const T* alpha_;
Z
zchen0211 已提交
76 77
};

Z
prelu  
zchen0211 已提交
78
template <typename Place, typename T>
Z
fix  
zchen0211 已提交
79
class PReluGradKernel : public framework::OpKernel {
Z
zchen0211 已提交
80 81
 public:
  void Compute(const framework::ExecutionContext& context) const override {
Z
prelu  
zchen0211 已提交
82 83
    auto* dx = context.Output<Tensor>(framework::GradVarName("X"));
    auto* dout = context.Input<Tensor>(framework::GradVarName("Out"));
Z
zchen0211 已提交
84

Z
prelu  
zchen0211 已提交
85 86
    auto* out = context.Input<Tensor>("Out");
    auto* alpha = context.Input<Tensor>("Alpha");
Z
zchen0211 已提交
87
    auto* alpha_ptr = alpha->data<T>();
Z
zchen0211 已提交
88

Z
prelu  
zchen0211 已提交
89 90 91 92
    T* dx_ptr = dx->mutable_data<T>(context.GetPlace());
    const T* dout_ptr = dout->data<T>();
    const T* out_ptr = out->data<T>();
    int numel = dx->numel();
Z
zchen0211 已提交
93

Z
zchen0211 已提交
94
    auto place = context.GetPlace();
Z
prelu  
zchen0211 已提交
95
    Transform(place, out_ptr, out_ptr + numel, dout_ptr, dx_ptr,
Z
zchen0211 已提交
96 97 98
              PReluGradFunctor<T>(alpha_ptr));

    // TODO (Zhuoyuan): add dalpha upgrade when GPU kernels ready
Z
zchen0211 已提交
99 100 101 102 103
  }
};

}  // namespace operators
}  // namespace paddle