|
|
@ -15,33 +15,72 @@ limitations under the License. */
|
|
|
|
#pragma once
|
|
|
|
#pragma once
|
|
|
|
#include "paddle/fluid/framework/eigen.h"
|
|
|
|
#include "paddle/fluid/framework/eigen.h"
|
|
|
|
#include "paddle/fluid/framework/op_registry.h"
|
|
|
|
#include "paddle/fluid/framework/op_registry.h"
|
|
|
|
|
|
|
|
#include "paddle/fluid/platform/hostdevice.h"
|
|
|
|
|
|
|
|
#include "paddle/legacy/utils/Logging.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
namespace paddle {
|
|
|
|
namespace operators {
|
|
|
|
namespace operators {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using Tensor = framework::Tensor;
|
|
|
|
|
|
|
|
template <typename T, int MajorType = Eigen::RowMajor,
|
|
|
|
|
|
|
|
typename IndexType = Eigen::DenseIndex>
|
|
|
|
|
|
|
|
using EigenVector = framework::EigenVector<T, MajorType, IndexType>;
|
|
|
|
|
|
|
|
template <typename T, int MajorType = Eigen::RowMajor,
|
|
|
|
|
|
|
|
typename IndexType = Eigen::DenseIndex>
|
|
|
|
|
|
|
|
using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
|
|
|
struct SigmoidCrossEntropyWithLogitsForward {
|
|
|
|
|
|
|
|
HOSTDEVICE SigmoidCrossEntropyWithLogitsForward(const int &ignore_index)
|
|
|
|
|
|
|
|
: ignore_index(ignore_index) {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HOSTDEVICE T operator()(const T &x, const T &label) const {
|
|
|
|
|
|
|
|
if (static_cast<int>(label) == ignore_index) {
|
|
|
|
|
|
|
|
return static_cast<T>(0.);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
T term1 = (x > 0) ? x : 0;
|
|
|
|
|
|
|
|
T term2 = x * label;
|
|
|
|
|
|
|
|
T term3 = std::log(static_cast<T>(1) + std::exp(-(std::abs(x))));
|
|
|
|
|
|
|
|
return term1 - term2 + term3;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ignore_index;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
|
|
|
struct SigmoidCrossEntropyWithLogitsBackward {
|
|
|
|
|
|
|
|
HOSTDEVICE SigmoidCrossEntropyWithLogitsBackward(const int &ignore_index)
|
|
|
|
|
|
|
|
: ignore_index(ignore_index) {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HOSTDEVICE T operator()(const T &x, const T &label) const {
|
|
|
|
|
|
|
|
if (static_cast<int>(label) == ignore_index) {
|
|
|
|
|
|
|
|
return static_cast<T>(0.);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
T simoid_x = static_cast<T>(1) / (static_cast<T>(1) + std::exp(-x));
|
|
|
|
|
|
|
|
return simoid_x - label;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ignore_index;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Out = max(X, 0) - X * Labels + log(1 + exp(-abs(X)))
|
|
|
|
// Out = max(X, 0) - X * Labels + log(1 + exp(-abs(X)))
|
|
|
|
template <typename DeviceContext, typename T>
|
|
|
|
template <typename DeviceContext, typename T>
|
|
|
|
class SigmoidCrossEntropyWithLogitsKernel : public framework::OpKernel<T> {
|
|
|
|
class SigmoidCrossEntropyWithLogitsKernel : public framework::OpKernel<T> {
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
void Compute(const framework::ExecutionContext &context) const override {
|
|
|
|
void Compute(const framework::ExecutionContext &context) const override {
|
|
|
|
const framework::Tensor *X = context.Input<framework::Tensor>("X");
|
|
|
|
const Tensor *X = context.Input<Tensor>("X");
|
|
|
|
const framework::Tensor *Labels = context.Input<framework::Tensor>("Label");
|
|
|
|
const Tensor *Labels = context.Input<Tensor>("Label");
|
|
|
|
framework::Tensor *Out = context.Output<framework::Tensor>("Out");
|
|
|
|
Tensor *Out = context.Output<Tensor>("Out");
|
|
|
|
Out->mutable_data<T>(context.GetPlace());
|
|
|
|
Out->mutable_data<T>(context.GetPlace());
|
|
|
|
|
|
|
|
int ignore_index = context.Attr<int>("ignore_index");
|
|
|
|
|
|
|
|
|
|
|
|
auto x = framework::EigenVector<T>::Flatten(*X);
|
|
|
|
auto x = EigenVector<T>::Flatten(*X);
|
|
|
|
auto labels = framework::EigenVector<T>::Flatten(*Labels);
|
|
|
|
auto labels = EigenVector<T>::Flatten(*Labels);
|
|
|
|
auto out = framework::EigenVector<T>::Flatten(*Out);
|
|
|
|
auto out = EigenVector<T>::Flatten(*Out);
|
|
|
|
auto &place = *context.device_context<DeviceContext>().eigen_device();
|
|
|
|
auto &place = *context.device_context<DeviceContext>().eigen_device();
|
|
|
|
|
|
|
|
|
|
|
|
// term1 = max(x, 0)
|
|
|
|
out.device(place) = x.binaryExpr(
|
|
|
|
auto term1 = x.cwiseMax(static_cast<T>(0));
|
|
|
|
labels, SigmoidCrossEntropyWithLogitsForward<T>(ignore_index));
|
|
|
|
// term2 = x * labels
|
|
|
|
|
|
|
|
auto term2 = x * labels;
|
|
|
|
|
|
|
|
// term3 = log(1 + exp(-abs(x)))
|
|
|
|
|
|
|
|
auto term3 = (static_cast<T>(1) + (-(x.abs())).exp()).log();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out.device(place) = term1 - term2 + term3;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -50,23 +89,23 @@ template <typename DeviceContext, typename T>
|
|
|
|
class SigmoidCrossEntropyWithLogitsGradKernel : public framework::OpKernel<T> {
|
|
|
|
class SigmoidCrossEntropyWithLogitsGradKernel : public framework::OpKernel<T> {
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
void Compute(const framework::ExecutionContext &context) const override {
|
|
|
|
void Compute(const framework::ExecutionContext &context) const override {
|
|
|
|
const framework::Tensor *X = context.Input<framework::Tensor>("X");
|
|
|
|
const Tensor *X = context.Input<Tensor>("X");
|
|
|
|
const framework::Tensor *Labels = context.Input<framework::Tensor>("Label");
|
|
|
|
const Tensor *Labels = context.Input<Tensor>("Label");
|
|
|
|
const framework::Tensor *dOut =
|
|
|
|
const Tensor *dOut = context.Input<Tensor>(framework::GradVarName("Out"));
|
|
|
|
context.Input<framework::Tensor>(framework::GradVarName("Out"));
|
|
|
|
Tensor *dX = context.Output<Tensor>(framework::GradVarName("X"));
|
|
|
|
framework::Tensor *dX =
|
|
|
|
|
|
|
|
context.Output<framework::Tensor>(framework::GradVarName("X"));
|
|
|
|
|
|
|
|
dX->mutable_data<T>(context.GetPlace());
|
|
|
|
dX->mutable_data<T>(context.GetPlace());
|
|
|
|
|
|
|
|
|
|
|
|
auto x = framework::EigenVector<T>::Flatten(*X);
|
|
|
|
auto ignore_index = context.Attr<int>("ignore_index");
|
|
|
|
auto labels = framework::EigenVector<T>::Flatten(*Labels);
|
|
|
|
auto x = EigenVector<T>::Flatten(*X);
|
|
|
|
auto dout = framework::EigenVector<T>::Flatten(*dOut);
|
|
|
|
auto labels = EigenVector<T>::Flatten(*Labels);
|
|
|
|
auto dx = framework::EigenVector<T>::Flatten(*dX);
|
|
|
|
auto dout = EigenVector<T>::Flatten(*dOut);
|
|
|
|
|
|
|
|
auto dx = EigenVector<T>::Flatten(*dX);
|
|
|
|
auto &place =
|
|
|
|
auto &place =
|
|
|
|
*context.template device_context<DeviceContext>().eigen_device();
|
|
|
|
*context.template device_context<DeviceContext>().eigen_device();
|
|
|
|
|
|
|
|
|
|
|
|
auto sigmoid_x = static_cast<T>(1) / (static_cast<T>(1) + (-x).exp());
|
|
|
|
auto diff = x.binaryExpr(labels, SigmoidCrossEntropyWithLogitsBackward<T>(
|
|
|
|
dx.device(place) = dout * (sigmoid_x - labels);
|
|
|
|
static_cast<int>(ignore_index)));
|
|
|
|
|
|
|
|
dx.device(place) = dout * diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|