Skip to content

Commit

Permalink
2022-04-28
Browse files Browse the repository at this point in the history
  • Loading branch information
yangguohao committed Apr 28, 2022
1 parent 66f1e82 commit 2c0a131
Show file tree
Hide file tree
Showing 16 changed files with 788 additions and 0 deletions.
146 changes: 146 additions & 0 deletions paddle/fluid/operators/soft_margin_loss_op.cc
@@ -0,0 +1,146 @@
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
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. */

#include <memory>
#include <string>
#include <vector>

#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/infermeta/binary.h"

namespace paddle {
namespace operators {

using framework::Tensor;

class SoftMarginLossOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;

protected:
framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
return framework::OpKernelType(
OperatorWithKernel::IndicateVarDataType(ctx, "X"),
ctx.device_context());
}
};

class SoftMarginLossGradOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;

void InferShape(framework::InferShapeContext* ctx) const override {
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "SoftMarginLossGrad");
OP_INOUT_CHECK(ctx->HasInput("Label"), "Input", "Label", "SoftMarginLossGrad");
OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Out")), "Input",
framework::GradVarName("Out"), "SoftMarginLossGrad");
OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("X")), "Output",
framework::GradVarName("X"), "SoftMarginLossGrad");

auto x_dims = ctx->GetInputDim("X");
auto labels_dims = ctx->GetInputDim("Label");
auto dout_dims = ctx->GetInputDim(framework::GradVarName("Out"));

bool check = true;
if ((!ctx->IsRuntime()) &&
(phi::product(x_dims) <= 0 || phi::product(labels_dims) <= 0)) {
check = false;
}

if (check) {
PADDLE_ENFORCE_EQ(x_dims, labels_dims,
platform::errors::InvalidArgument(
"Input(X) and Input(Label) shall have the same "
"shape. But received: the shape of Input(X) is "
"[%s], the shape of Input(Label) is [%s].",
x_dims, labels_dims));

PADDLE_ENFORCE_EQ(x_dims, dout_dims,
platform::errors::InvalidArgument(
"Input(X) and Input(Out@Grad) shall have the same "
"shape. But received: the shape of Input(X) is "
"[%s], the shape of Input(Out@Grad) is [%s].",
x_dims, dout_dims));
}

ctx->SetOutputDim(framework::GradVarName("X"), x_dims);
ctx->ShareLoD("X", framework::GradVarName("X"));
}

protected:
framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
return framework::OpKernelType(
OperatorWithKernel::IndicateVarDataType(ctx, "X"),
ctx.device_context());
}
};

class SoftMarginLossOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddInput("X",
"(Tensor, default Tensor<float>), the input is a tensor of logits"
"computed by the previous operator. ");
AddInput("Label",
"(Tensor, default Tensor<float>), have same shape with input"
"label should between in 0 and 1.");
AddOutput("Out",
"(Tensor, default Tensor<float>), have same shape with"
"input");
AddComment(R"DOC(
SoftMarginLoss operator.
This measures the element-wise probability error in classification tasks
in which each class is independent.
The logitstic loss is given as follows:
$$loss = log(1+exp(-Label * X))$$
)DOC");
}
};

template <typename T>
class SoftMarginLossGradOpMaker : public framework::SingleGradOpMaker<T> {
public:
using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

protected:
void Apply(GradOpPtr<T> op) const override {
op->SetType("soft_margin_loss_grad");
op->SetInput("X", this->Input("X"));
op->SetInput("Label", this->Input("Label"));
op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
op->SetAttrMap(this->Attrs());
}
};

DECLARE_INPLACE_OP_INFERER(SoftMarginLossInplaceInferer, {"X", "Out"});
DECLARE_INPLACE_OP_INFERER(SoftMarginLossGradInplaceInferer,
{framework::GradVarName("Out"),
framework::GradVarName("X")});

} // namespace operators
} // namespace paddle

namespace ops = paddle::operators;
DECLARE_INFER_SHAPE_FUNCTOR(soft_margin_loss,
SoftMarginLossInferShapeFunctor,
PD_INFER_META(phi::SoftMarginLossInferMeta));

REGISTER_OPERATOR(soft_margin_loss, ops::SoftMarginLossOp,
ops::SoftMarginLossOpMaker,
ops::SoftMarginLossGradOpMaker<paddle::framework::OpDesc>,
ops::SoftMarginLossGradOpMaker<paddle::imperative::OpBase>,
ops::SoftMarginLossInplaceInferer, SoftMarginLossInferShapeFunctor);
REGISTER_OPERATOR(soft_margin_loss_grad, ops::SoftMarginLossGradOp,
ops::SoftMarginLossGradInplaceInferer);
12 changes: 12 additions & 0 deletions paddle/phi/core/compat/soft_margin_loss_sig.cc
@@ -0,0 +1,12 @@
#include "paddle/phi/core/compat/op_utils.h"

namespace phi{
KernelSignature SoftMarginLossGradOpArgumentMapping(const ArgumentMappingContext& ctx){
return KernelSignature("soft_margin_loss_grad",
{GradVarName("Out"),"X","Label"},
{},
{GradVarName("X")});
}
}// namespace phi

PD_REGISTER_ARG_MAPPING_FN(soft_margin_loss_grad,phi::SoftMarginLossGradOpArgumentMapping);
39 changes: 39 additions & 0 deletions paddle/phi/infermeta/binary.cc
Expand Up @@ -1757,6 +1757,45 @@ void SigmoidCrossEntropyWithLogitsInferMeta(const MetaTensor& x,
out->share_lod(x);
}

void SoftMarginLossInferMeta(const MetaTensor& input,
const MetaTensor& label,
MetaTensor* out,
MetaConfig config) {
auto input_dims = input.dims();
auto label_dims = label.dims();

int rank = input_dims.size();
PADDLE_ENFORCE_EQ(rank,
label_dims.size(),
phi::errors::InvalidArgument(
"Input(X) and Input(Label) shall have the same rank."
"But received: the rank of Input(X) is [%d], "
"the rank of Input(Label) is [%d].",
rank,
label_dims.size()));

bool check = true;
if ((!config.is_runtime) &&
(phi::product(input_dims) <= 0 || phi::product(label_dims) <= 0)) {
check = false;
}

if (check) {
PADDLE_ENFORCE_EQ(input_dims,
label_dims,
phi::errors::InvalidArgument(
"Input(X) and Input(Label) shall have the same "
"shape. But received: the shape of Input(X) is "
"[%s], the shape of Input(Label) is [%s].",
input_dims,
label_dims));
}

out->set_dims(input_dims);
out->set_dtype(input.dtype());
out->share_lod(input);
}

void TakeAlongAxisInferMeta(const MetaTensor& x,
const MetaTensor& index,
int axis,
Expand Down
5 changes: 5 additions & 0 deletions paddle/phi/infermeta/binary.h
Expand Up @@ -253,6 +253,11 @@ void SigmoidCrossEntropyWithLogitsInferMeta(const MetaTensor& x,
MetaTensor* out,
MetaConfig config = MetaConfig());

void SoftMarginLossInferMeta(const MetaTensor& input,
const MetaTensor& label,
MetaTensor* out,
MetaConfig config = MetaConfig());

void TakeAlongAxisInferMeta(const MetaTensor& x,
const MetaTensor& index,
int axis,
Expand Down
48 changes: 48 additions & 0 deletions paddle/phi/kernels/cpu/soft_margin_loss_grad_kernel.cc
@@ -0,0 +1,48 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#include "paddle/phi/kernels/soft_margin_loss_grad_kernel.h"

#include <algorithm>
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"


namespace phi {

template <typename T, typename Context>
void SoftMarginLossGradKernel(const Context& dev_ctx,
const DenseTensor& input,
const DenseTensor& label,
const DenseTensor& out_grad,
DenseTensor* input_grad) {
auto dx_data = dev_ctx.template Alloc<T>(input_grad);
auto dout_data = out_grad.data<T>();
auto x_data = input.data<T>();
auto label_data = label.data<T>();

int x_numel = input.numel();

// dx = dout * (-label * exp(-label * x))/(1 + exp(-label * x ))
for (int i = 0; i < x_numel; ++i) {
dx_data[i] =
dout_data[i] * ((- label_data[i]*std::exp(-label_data[i]*x_data[i] )) /
std::max((static_cast<T>(1) + std::exp(-label_data[i]*x_data[i])),
static_cast<T>(1e-12)));
}
}
} // namespace phi

PD_REGISTER_KERNEL(
soft_margin_loss_grad, CPU, ALL_LAYOUT, phi::SoftMarginLossGradKernel, float, double) {}
40 changes: 40 additions & 0 deletions paddle/phi/kernels/cpu/soft_margin_loss_kernel.cc
@@ -0,0 +1,40 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#include "paddle/phi/kernels/soft_margin_loss_kernel.h"

#include <algorithm>
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"

namespace phi {

template <typename T, typename Context>
void SoftMarginLossKernel(const Context& dev_ctx,
const DenseTensor& input,
const DenseTensor& label,
DenseTensor* out) {
auto x_data = input.data<T>();
auto label_data = label.data<T>();
auto out_data = dev_ctx.template Alloc<T>(out);
auto x_numel = input.numel();

// out = ln(1+exp(-label * x)/(x_numel)
for (int64_t i = 0; i < x_numel; ++i) {
out_data[i] =std::log(static_cast<T>(1) + std::exp(-label_data[i]* x_data[i]));
}
}
} // namespace phi
PD_REGISTER_KERNEL(
soft_margin_loss, CPU, ALL_LAYOUT, phi::SoftMarginLossKernel, float, double) {}
59 changes: 59 additions & 0 deletions paddle/phi/kernels/gpu/soft_margin_loss_grad_kernel.cu
@@ -0,0 +1,59 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#include "paddle/phi/kernels/soft_margin_loss_grad_kernel.h"

#include <algorithm>
#include <vector>

#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/hostdevice.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/elementwise_base.h"

namespace phi {

template <typename T>
struct SoftMarginLossGradFunctor {
T one;
T eps;

HOSTDEVICE inline SoftMarginLossGradFunctor() {
one = static_cast<T>(1.0f);
eps = static_cast<T>(1e-12);
}

HOSTDEVICE inline T operator()(const T x, const T label, const T dout) const {
T term1 = max((one + std::exp(-label * x)), eps);
return (dout * (-label * std::exp(-label * x)) / term1);
}
};

template <typename T, typename Context>
void SoftMarginLossGradKernel(const Context& dev_ctx,
const DenseTensor& input,
const DenseTensor& label,
const DenseTensor& out_grad,
DenseTensor* input_grad) {
dev_ctx.template Alloc<T>(input_grad);
std::vector<const DenseTensor*> ins = {&input, &label, &out_grad};
std::vector<DenseTensor*> outs = {input_grad};
auto functor = SoftMarginLossGradFunctor<T>();
phi::funcs::ElementwiseKernel<T>(dev_ctx, ins, &outs, functor);
}

} // namespace phi

PD_REGISTER_KERNEL(
soft_margin_loss_grad, GPU, ALL_LAYOUT, phi::SoftMarginLossGradKernel, float, double) {}

0 comments on commit 2c0a131

Please sign in to comment.