|
|
@ -15,6 +15,7 @@ limitations under the License. */
|
|
|
|
#pragma once
|
|
|
|
#pragma once
|
|
|
|
#include "paddle/framework/eigen.h"
|
|
|
|
#include "paddle/framework/eigen.h"
|
|
|
|
#include "paddle/framework/op_registry.h"
|
|
|
|
#include "paddle/framework/op_registry.h"
|
|
|
|
|
|
|
|
#include "paddle/platform/transform.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
namespace paddle {
|
|
|
|
namespace operators {
|
|
|
|
namespace operators {
|
|
|
@ -23,28 +24,60 @@ using Tensor = framework::Tensor;
|
|
|
|
template <typename T, int MajorType = Eigen::RowMajor,
|
|
|
|
template <typename T, int MajorType = Eigen::RowMajor,
|
|
|
|
typename IndexType = Eigen::DenseIndex>
|
|
|
|
typename IndexType = Eigen::DenseIndex>
|
|
|
|
using EigenVector = framework::EigenVector<T, MajorType, IndexType>;
|
|
|
|
using EigenVector = framework::EigenVector<T, MajorType, IndexType>;
|
|
|
|
|
|
|
|
using platform::Transform;
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Place, typename T>
|
|
|
|
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>
|
|
|
|
class PReluKernel : public framework::OpKernel {
|
|
|
|
class PReluKernel : public framework::OpKernel {
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
void Compute(const framework::ExecutionContext& context) const override {
|
|
|
|
void Compute(const framework::ExecutionContext& context) const override {
|
|
|
|
auto* X = context.Input<Tensor>("X");
|
|
|
|
auto* X = context.Input<Tensor>("X");
|
|
|
|
auto* Out = context.Output<Tensor>("Out");
|
|
|
|
auto* Out = context.Output<Tensor>("Out");
|
|
|
|
|
|
|
|
|
|
|
|
Out->mutable_data<T>(context.GetPlace());
|
|
|
|
const T* X_ptr = X->data<T>();
|
|
|
|
|
|
|
|
T* O_ptr = Out->mutable_data<T>(context.GetPlace());
|
|
|
|
|
|
|
|
|
|
|
|
auto alpha = static_cast<T>(context.Attr<float>("alpha"));
|
|
|
|
auto alpha = static_cast<T>(context.Attr<AttrType>("alpha"));
|
|
|
|
|
|
|
|
|
|
|
|
auto X_vec = EigenVector<T>::Flatten(*X);
|
|
|
|
int numel = X->numel();
|
|
|
|
auto Out_vec = EigenVector<T>::Flatten(*Out);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// auto place = context.GetEigenDevice<Place>();
|
|
|
|
auto place = context.GetPlace();
|
|
|
|
// Out_vec.device(place)
|
|
|
|
Transform(place, X_ptr, X_ptr + numel, O_ptr, Prelu_functor<T>(alpha));
|
|
|
|
Out_vec = X_vec.cwiseMax(0.f) + X_vec.cwiseMin(0.f) * alpha;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Place, typename T>
|
|
|
|
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>
|
|
|
|
class PReluGradKernel : public framework::OpKernel {
|
|
|
|
class PReluGradKernel : public framework::OpKernel {
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
void Compute(const framework::ExecutionContext& context) const override {
|
|
|
|
void Compute(const framework::ExecutionContext& context) const override {
|
|
|
@ -53,16 +86,16 @@ class PReluGradKernel : public framework::OpKernel {
|
|
|
|
|
|
|
|
|
|
|
|
auto* Out = context.Input<Tensor>("Out");
|
|
|
|
auto* Out = context.Input<Tensor>("Out");
|
|
|
|
|
|
|
|
|
|
|
|
auto alpha = static_cast<T>(context.Attr<float>("alpha"));
|
|
|
|
auto alpha = static_cast<T>(context.Attr<AttrType>("alpha"));
|
|
|
|
|
|
|
|
|
|
|
|
dX->mutable_data<T>(context.GetPlace());
|
|
|
|
T* dX_ptr = dX->mutable_data<T>(context.GetPlace());
|
|
|
|
for (int i = 0; i < dX->numel(); ++i) {
|
|
|
|
const T* dO_ptr = dO->data<T>();
|
|
|
|
if (Out->data<T>()[i] > 0) {
|
|
|
|
const T* O_ptr = Out->data<T>();
|
|
|
|
dX->data<T>()[i] = dO->data<T>()[i];
|
|
|
|
int numel = dX->numel();
|
|
|
|
} else {
|
|
|
|
|
|
|
|
dX->data<T>()[i] = dO->data<T>()[i] * alpha;
|
|
|
|
auto place = context.GetPlace();
|
|
|
|
}
|
|
|
|
Transform(place, O_ptr, O_ptr + numel, dO_ptr, dX_ptr,
|
|
|
|
}
|
|
|
|
Prelu_Grad_functor<T>(alpha));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|