-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66f1e82
commit 2c0a131
Showing
16 changed files
with
788 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) {} |
Oops, something went wrong.