prelu_op.h 2.7 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
template <typename T>
class Prelu_functor {
 public:
  explicit Prelu_functor(const T& alpha) : alpha_(alpha) {}

  HOSTDEVICE T operator()(const T& X) const {
    if (X > 0)
      return X;
    else
      return X * alpha_;
  }

 private:
  T alpha_;
};

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

Z
zchen0211 已提交
49 50
    const T* X_ptr = X->data<T>();
    T* O_ptr = Out->mutable_data<T>(context.GetPlace());
Z
zchen0211 已提交
51

Z
zchen0211 已提交
52
    auto alpha = static_cast<T>(context.Attr<AttrType>("alpha"));
Z
zchen0211 已提交
53

Z
zchen0211 已提交
54
    int numel = X->numel();
Z
zchen0211 已提交
55

Z
zchen0211 已提交
56 57
    auto place = context.GetPlace();
    Transform(place, X_ptr, X_ptr + numel, O_ptr, Prelu_functor<T>(alpha));
Z
zchen0211 已提交
58 59 60
  }
};

Z
zchen0211 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
template <typename T>
class Prelu_Grad_functor {
 public:
  explicit Prelu_Grad_functor(const T& alpha) : alpha_(alpha) {}

  HOSTDEVICE T operator()(const T& Out, const T& dOut) const {
    if (Out > 0)
      return dOut;
    else
      return dOut * alpha_;
  }

 private:
  T alpha_;
};

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

Z
zchen0211 已提交
84
    auto* Out = context.Input<Tensor>("Out");
Z
zchen0211 已提交
85

Z
zchen0211 已提交
86 87 88 89 90 91
    auto alpha = static_cast<T>(context.Attr<AttrType>("alpha"));

    T* dX_ptr = dX->mutable_data<T>(context.GetPlace());
    const T* dO_ptr = dO->data<T>();
    const T* O_ptr = Out->data<T>();
    int numel = dX->numel();
Z
zchen0211 已提交
92

Z
zchen0211 已提交
93 94 95
    auto place = context.GetPlace();
    Transform(place, O_ptr, O_ptr + numel, dO_ptr, dX_ptr,
              Prelu_Grad_functor<T>(alpha));
Z
zchen0211 已提交
96 97 98 99 100
  }
};

}  // namespace operators
}  // namespace paddle