diff --git a/paddle/fluid/operators/identity_loss_op.cc b/paddle/fluid/operators/identity_loss_op.cc deleted file mode 100644 index 76e7f8a733e40..0000000000000 --- a/paddle/fluid/operators/identity_loss_op.cc +++ /dev/null @@ -1,107 +0,0 @@ -/* 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 - -#include "paddle/fluid/framework/infershape_utils.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/phi/core/infermeta_utils.h" -#include "paddle/phi/infermeta/unary.h" - -namespace paddle { -namespace operators { - -class IdentityLossOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext& ctx) const override { - return phi::KernelKey(OperatorWithKernel::IndicateVarDataType(ctx, "X"), - platform::CPUPlace()); - } -}; - -class IdentityLossOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("X", "(Tensor) The input of identity_loss op"); - AddOutput("Out", "(Tensor) The output of identity_loss op"); - AddAttr("reduction", "(int, default 1). The reduction.") - .SetDefault(1) - .InEnum({0, 1, 2}); - AddComment(R"DOC( -IdentityLoss Operator mark the Loss var. - -)DOC"); - } -}; - -class IdentityLossGradOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - void InferShape(framework::InferShapeContext* ctx) const override { - ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X")); - ctx->ShareLoD("X", framework::GradVarName("X")); - } - - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext& ctx) const override { - auto input_data_type = OperatorWithKernel::IndicateVarDataType( - ctx, framework::GradVarName("Out")); - return phi::KernelKey(input_data_type, platform::CPUPlace()); - } -}; - -template -class IdentityLossGradMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - - protected: - void Apply(GradOpPtr grad_op) const override { - grad_op->SetType("identity_loss_grad"); - grad_op->SetInput("X", this->Input("X")); - grad_op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out")); - grad_op->SetOutput(framework::GradVarName("X"), this->InputGrad("X")); - grad_op->SetAttrMap(this->Attrs()); - } -}; - -DECLARE_INPLACE_OP_INFERER(IdentityLossInplaceInferer, {"X", "Out"}); -DECLARE_INPLACE_OP_INFERER(IdentityLossGradInplaceInferer, - {framework::GradVarName("Out"), - framework::GradVarName("X")}); - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -DECLARE_INFER_SHAPE_FUNCTOR(identity_loss, - IdentityLossInferShapeFunctor, - PD_INFER_META(phi::IdentityLossInferMeta)); - -REGISTER_OPERATOR(identity_loss, - ops::IdentityLossOp, - ops::IdentityLossOpMaker, - ops::IdentityLossGradMaker, - ops::IdentityLossGradMaker, - ops::IdentityLossInplaceInferer, - IdentityLossInferShapeFunctor); - -REGISTER_OPERATOR(identity_loss_grad, - ops::IdentityLossGradOp, - ops::IdentityLossGradInplaceInferer); diff --git a/paddle/fluid/operators/ops_signature/identity_loss_sig.cc b/paddle/fluid/operators/ops_signature/identity_loss_sig.cc deleted file mode 100644 index 0b748396e9d05..0000000000000 --- a/paddle/fluid/operators/ops_signature/identity_loss_sig.cc +++ /dev/null @@ -1,34 +0,0 @@ -// 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/core/compat/op_utils.h" - -namespace phi { - -KernelSignature IdentityLossOpArgumentMapping( - const ArgumentMappingContext& ctx UNUSED) { - return KernelSignature("identity_loss", {"X"}, {"reduction"}, {"Out"}); -} - -KernelSignature IdentityLossGradOpArgumentMapping( - const ArgumentMappingContext& ctx UNUSED) { - return KernelSignature( - "identity_loss_grad", {"X", "Out@GRAD"}, {"reduction"}, {"X@GRAD"}); -} - -} // namespace phi - -PD_REGISTER_ARG_MAPPING_FN(identity_loss, phi::IdentityLossOpArgumentMapping); -PD_REGISTER_ARG_MAPPING_FN(identity_loss_grad, - phi::IdentityLossGradOpArgumentMapping); diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index 8ee3ca74c69a7..16393ecdd4f5c 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -1097,6 +1097,17 @@ kernel : func : i1e_grad +- backward_op : identity_loss_grad + forward : identity_loss (Tensor x, int reduction) -> Tensor(out) + args : (Tensor x, Tensor out_grad, int reduction = 1) + output : Tensor(x_grad) + infer_meta : + func : IdentityLossGradInferMeta + kernel : + func : identity_loss_grad + data_type : out_grad + inplace : (out_grad -> x_grad) + - backward_op : imag_grad forward : imag (Tensor x) -> Tensor(out) args : (Tensor out_grad) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 978858c0e6b19..04b1514b0449e 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -3439,6 +3439,12 @@ outputs : out : Out +- op: identity_loss + inputs : + x: X + outputs : + out : Out + - op: lod_array_length inputs : {x: X} diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 1f8a7d158325e..45f4e2e475bef 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -1228,6 +1228,18 @@ func : i1e backward : i1e_grad +- op : identity_loss + args : (Tensor x, int reduction = 1) + output : Tensor(out) + infer_meta : + func : IdentityLossInferMeta + kernel : + func : identity_loss + data_type : x + backend : x + inplace: (x -> out) + backward : identity_loss_grad + - op : imag args : (Tensor x) output : Tensor (out) diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index 006fd4d5437d1..4cd0f7f842303 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -3354,6 +3354,15 @@ void WeightDequantizeInferMeta(const MetaTensor& x, out->set_dtype(out_dtype); } +void IdentityLossGradInferMeta(const MetaTensor& x, + const MetaTensor& out_grad, + const int reduction, + MetaTensor* x_grad) { + x_grad->set_dims(x.dims()); + x_grad->share_lod(x); + x_grad->set_dtype(out_grad.dtype()); +} + } // namespace phi PD_REGISTER_INFER_META_FN(add_raw, phi::ElementwiseRawInferMeta); diff --git a/paddle/phi/infermeta/binary.h b/paddle/phi/infermeta/binary.h index 6ca56a5c98a17..7289d178e78b2 100644 --- a/paddle/phi/infermeta/binary.h +++ b/paddle/phi/infermeta/binary.h @@ -526,4 +526,9 @@ void WeightDequantizeInferMeta(const MetaTensor& x, DataType out_dtype, MetaTensor* out); +void IdentityLossGradInferMeta(const MetaTensor& x, + const MetaTensor& out_grad, + const int reduction, + MetaTensor* x_grad); + } // namespace phi diff --git a/python/paddle/incubate/nn/loss.py b/python/paddle/incubate/nn/loss.py index 0a8043b445b82..c6eb7df467a79 100644 --- a/python/paddle/incubate/nn/loss.py +++ b/python/paddle/incubate/nn/loss.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle import _legacy_C_ops +from paddle import _C_ops from paddle.base.data_feeder import check_variable_and_dtype from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode @@ -60,7 +60,7 @@ def identity_loss(x, reduction="none"): raise Exception("Unsupported reduction type.") if in_dynamic_mode(): - return _legacy_C_ops.identity_loss(x, "reduction", reduction) + return _C_ops.identity_loss(x, reduction) check_variable_and_dtype(x, 'x', ['float32', 'float64'], "identity_loss") attrs = {'reduction': reduction}