You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Paddle/paddle/operators/prelu_op.h

105 lines
2.8 KiB

8 years ago
/* 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"
#include "paddle/platform/transform.h"
8 years ago
namespace paddle {
namespace operators {
using Tensor = framework::Tensor;
using platform::Transform;
8 years ago
template <typename T>
8 years ago
class PReluFunctor {
public:
8 years ago
explicit PReluFunctor(const T* alpha) : alpha_(alpha) {}
8 years ago
HOSTDEVICE T operator()(const T& x) const {
if (x > 0)
return x;
else
8 years ago
return x * (*alpha_);
}
private:
8 years ago
const T* alpha_;
};
8 years ago
template <typename Place, typename T>
class PReluKernel : public framework::OpKernel<T> {
8 years ago
public:
void Compute(const framework::ExecutionContext& context) const override {
8 years ago
auto* x = context.Input<Tensor>("X");
auto* alpha = context.Input<Tensor>("Alpha");
auto* out = context.Output<Tensor>("Out");
8 years ago
8 years ago
const T* x_ptr = x->data<T>();
T* o_ptr = out->mutable_data<T>(context.GetPlace());
8 years ago
8 years ago
auto* alpha_ptr = alpha->data<T>();
8 years ago
8 years ago
int numel = x->numel();
8 years ago
Transform<Place> trans;
trans(context.device_context(), x_ptr, x_ptr + numel, o_ptr,
PReluFunctor<T>(alpha_ptr));
8 years ago
}
};
template <typename T>
8 years ago
class PReluGradFunctor {
public:
8 years ago
explicit PReluGradFunctor(const T* alpha) : alpha_(alpha) {}
8 years ago
HOSTDEVICE T operator()(const T& out, const T& dout) const {
if (out > 0)
return dout;
else
8 years ago
return dout * (*alpha_);
}
private:
8 years ago
const T* alpha_;
};
8 years ago
template <typename Place, typename T>
class PReluGradKernel : public framework::OpKernel<T> {
8 years ago
public:
void Compute(const framework::ExecutionContext& context) const override {
8 years ago
auto* dx = context.Output<Tensor>(framework::GradVarName("X"));
auto* dout = context.Input<Tensor>(framework::GradVarName("Out"));
8 years ago
8 years ago
auto* out = context.Input<Tensor>("Out");
auto* alpha = context.Input<Tensor>("Alpha");
8 years ago
auto* alpha_ptr = alpha->data<T>();
8 years ago
8 years ago
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();
8 years ago
Transform<Place> trans;
trans(context.device_context(), out_ptr, out_ptr + numel, dout_ptr, dx_ptr,
PReluGradFunctor<T>(alpha_ptr));
8 years ago
// TODO(Zhuoyuan): add dalpha upgrade when GPU kernels ready
8 years ago
}
};
} // namespace operators
} // namespace paddle