From 1f3b9ef5bc3d954cbdefce334b8b436d48dcae71 Mon Sep 17 00:00:00 2001 From: gouzil <66515297+gouzil@users.noreply.github.com> Date: Mon, 3 Apr 2023 12:07:26 +0800 Subject: [PATCH] add autogen code support for accuracy (#52424) * add autogen code support for accuracy * fix input --- paddle/fluid/operators/metrics/accuracy_op.cc | 77 ------------------- paddle/phi/api/yaml/legacy_ops.yaml | 9 --- paddle/phi/api/yaml/op_compat.yaml | 6 ++ paddle/phi/api/yaml/ops.yaml | 9 +++ 4 files changed, 15 insertions(+), 86 deletions(-) delete mode 100644 paddle/fluid/operators/metrics/accuracy_op.cc diff --git a/paddle/fluid/operators/metrics/accuracy_op.cc b/paddle/fluid/operators/metrics/accuracy_op.cc deleted file mode 100644 index 25e32b51978..00000000000 --- a/paddle/fluid/operators/metrics/accuracy_op.cc +++ /dev/null @@ -1,77 +0,0 @@ -/* Copyright (c) 2016 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/fluid/framework/infershape_utils.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/phi/infermeta/ternary.h" - -namespace paddle { -namespace operators { - -class AccuracyOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext &ctx) const override { - return phi::KernelKey(OperatorWithKernel::IndicateVarDataType(ctx, "Out"), - ctx.GetPlace()); - } -}; - -class AccuracyOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - // TODO(typhoonzero): support both inference value and indices. - AddInput("Out", "The network output of topk (inferences)"); - AddInput("Indices", "The network output of topk (indices)"); - AddInput("Label", "Label of the training data"); - // TODO(typhoonzero): AddInput("Weight", ... - AddOutput("Accuracy", "The accuracy of current batch"); - AddOutput("Correct", "The correct samples count of current batch"); - AddOutput("Total", "The samples count of current batch"); - - AddComment(R"DOC( -Accuracy Operator. - -It will print accuracy rate for classification. -The accuracy is calculated as follows: - -$$accuracy = \frac{NumOfCorrectPredicts}{NumOfAllSamples}$$ - -Both the input Out and Label can carry the LoD (Level of Details) -information, or not. But the output only shares the LoD information -with the input Out(Inference). - -)DOC"); - } -}; - -} // namespace operators -} // namespace paddle - -// FIXME(typhoonzero): types of T is for infernece data. -// label data is always int. -DECLARE_INFER_SHAPE_FUNCTOR(accuracy, - AccuracyInferShapeFunctor, - PD_INFER_META(phi::AccuracyInferMeta)); -namespace ops = paddle::operators; -REGISTER_OPERATOR( - accuracy, - ops::AccuracyOp, - ops::AccuracyOpMaker, - paddle::framework::EmptyGradOpMaker, - paddle::framework::EmptyGradOpMaker, - AccuracyInferShapeFunctor); diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index 7484bb50298..92cc67bc9da 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -10,15 +10,6 @@ func : abs backward : abs_grad -- op : accuracy - args : (Tensor x, Tensor indices, Tensor label) - output : Tensor(accuracy), Tensor(correct), Tensor(total) - infer_meta : - func : AccuracyInferMeta - kernel : - func : accuracy - data_type : x - - op : adadelta_ args : (Tensor param, Tensor grad, Tensor avg_squared_grad, Tensor avg_squared_update, Tensor master_param, float rho, float epsilon, bool multi_precision) output : Tensor(param_out), Tensor(moment_out), Tensor(inf_norm_out), Tensor(master_param_out) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 51b03693f30..45c87aaca57 100644 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -23,6 +23,12 @@ extra : attrs : [bool use_mkldnn = false] +- op : accuracy + inputs : + {x : Out , indices : Indices, label: Label} + outputs : + {accuracy : Accuracy, correct : Correct, total : Total} + - op : acos inputs : x : X diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index cf126e8d4d8..473b18dbcc4 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -5,6 +5,15 @@ # are consistent and correspond one-to-one. It's forbidden that the # operator configured in this yaml file does not have Python API. +- op : accuracy + args : (Tensor x, Tensor indices, Tensor label) + output : Tensor(accuracy), Tensor(correct), Tensor(total) + infer_meta : + func : AccuracyInferMeta + kernel : + func : accuracy + data_type : x + - op : acos args : (Tensor x) output : Tensor -- GitLab