From 877948fa8bc2ade25ba169438925c44b0c5157a9 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Fri, 2 Jun 2023 20:34:31 +0800 Subject: [PATCH] support auto generate for static op reduce_any (#54284) * decouple reduce_any_op.h and reduce_op.h from reduce_any_op.cc * support auto generate for static op reduce_any --- .../generator/get_expected_kernel_func.cc | 9 ++++ .../generator/get_expected_kernel_func.h | 4 ++ .../operators/reduce_ops/reduce_any_op.cc | 48 ------------------- .../operators/reduce_ops/reduce_any_op.h | 29 ----------- paddle/phi/api/yaml/op_compat.yaml | 21 ++++++-- paddle/phi/api/yaml/static_ops.yaml | 10 ++++ 6 files changed, 40 insertions(+), 81 deletions(-) delete mode 100644 paddle/fluid/operators/reduce_ops/reduce_any_op.cc delete mode 100644 paddle/fluid/operators/reduce_ops/reduce_any_op.h diff --git a/paddle/fluid/operators/generator/get_expected_kernel_func.cc b/paddle/fluid/operators/generator/get_expected_kernel_func.cc index 8f24bb525f6..96060b6de5b 100644 --- a/paddle/fluid/operators/generator/get_expected_kernel_func.cc +++ b/paddle/fluid/operators/generator/get_expected_kernel_func.cc @@ -109,6 +109,15 @@ phi::KernelKey GetReduceGradExpectedKernelType( return phi::KernelKey(input_data_type, ctx.GetPlace()); } +phi::KernelKey GetReduceOpUseInputPlaceExpectedKernelType( + const framework::ExecutionContext& ctx, + const framework::OperatorWithKernel* op_ptr) { + phi::KernelKey kt = op_ptr->OperatorWithKernel::GetExpectedKernelType(ctx); + kt.set_backend( + phi::TransToPhiBackend(ctx.Input("X")->place())); + return kt; +} + phi::KernelKey GetAssignExpectedKernelType( const framework::ExecutionContext& ctx, const framework::OperatorWithKernel* op_ptr) { diff --git a/paddle/fluid/operators/generator/get_expected_kernel_func.h b/paddle/fluid/operators/generator/get_expected_kernel_func.h index b7b14f2186d..37dfc7607da 100644 --- a/paddle/fluid/operators/generator/get_expected_kernel_func.h +++ b/paddle/fluid/operators/generator/get_expected_kernel_func.h @@ -32,6 +32,10 @@ phi::KernelKey GetReduceGradExpectedKernelType( const framework::ExecutionContext& ctx, const framework::OperatorWithKernel* op_ptr); +phi::KernelKey GetReduceOpUseInputPlaceExpectedKernelType( + const framework::ExecutionContext& ctx, + const framework::OperatorWithKernel* op_ptr); + phi::KernelKey GetAssignExpectedKernelType( const framework::ExecutionContext& ctx, const framework::OperatorWithKernel* op_ptr); diff --git a/paddle/fluid/operators/reduce_ops/reduce_any_op.cc b/paddle/fluid/operators/reduce_ops/reduce_any_op.cc deleted file mode 100644 index ac3ec355876..00000000000 --- a/paddle/fluid/operators/reduce_ops/reduce_any_op.cc +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2018 PaddlePaddle Authors. Any 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/operators/reduce_ops/reduce_any_op.h" - -#include "paddle/fluid/framework/infershape_utils.h" -#include "paddle/phi/core/infermeta_utils.h" -#include "paddle/phi/infermeta/unary.h" -namespace paddle { -namespace framework { -class OpDesc; -template -class EmptyGradOpMaker; -} // namespace framework -namespace imperative { -class OpBase; -} // namespace imperative -} // namespace paddle - -DECLARE_INFER_SHAPE_FUNCTOR(reduce_any, - ReduceAnyInferShapeFunctor, - PD_INFER_META(phi::ReduceInferMetaBase)); - -class ReduceAnyOpMaker : public ops::ReduceBaseOpMaker { - protected: - virtual std::string GetName() const { return "reduce_any"; } - virtual std::string GetOpType() const { return "Reduce reduce_any"; } -}; -// kernel's device type is decided by input tensor place, to be consistent with -// compare and logical ops -REGISTER_OPERATOR( - reduce_any, - ops::ReduceOpUseInputPlace, - ReduceAnyOpMaker, - paddle::framework::EmptyGradOpMaker, - paddle::framework::EmptyGradOpMaker, - ReduceAnyInferShapeFunctor); diff --git a/paddle/fluid/operators/reduce_ops/reduce_any_op.h b/paddle/fluid/operators/reduce_ops/reduce_any_op.h deleted file mode 100644 index b36bad9cada..00000000000 --- a/paddle/fluid/operators/reduce_ops/reduce_any_op.h +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2019 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. -#pragma once - -#include "paddle/fluid/operators/reduce_ops/reduce_op.h" - -namespace paddle { -namespace operators { - -struct AnyFunctor { - template - void operator()(const DeviceContext& place, X* x, Y* y, const Dim& dim) { - y->device(place) = x->any(dim); - } -}; - -} // namespace operators -} // namespace paddle diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index db86a1b5472..a27884bcf33 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -198,6 +198,23 @@ extra : attrs : [bool use_mkldnn = false] +- op : any (reduce_any) + inputs : + x : X + outputs : + out : Out + attrs: + { axis : dim, keepdim : keep_dim } + extra : + attrs : [bool use_mkldnn = false] + int_array: + axis : + data_type : int + support_tensor : true + get_expected_kernel_type : + any : GetReduceOpUseInputPlaceExpectedKernelType + manual_signature : [any] + - op : arange(range) inputs : {start : Start, end : End, step : Step} @@ -1940,10 +1957,6 @@ extra : attrs : [bool use_mkldnn = false, bool use_cudnn = false] -- op : reduce_any - extra : - attrs : [bool use_mkldnn = false] - - op : reduce_min backward : reduce_min_grad extra : diff --git a/paddle/phi/api/yaml/static_ops.yaml b/paddle/phi/api/yaml/static_ops.yaml index 1f9edaf0ddb..1d309fb4f1f 100755 --- a/paddle/phi/api/yaml/static_ops.yaml +++ b/paddle/phi/api/yaml/static_ops.yaml @@ -48,6 +48,16 @@ param : [x, axis, keepdim, reduce_all] backward : amin_grad +- op : any + args : (Tensor x, IntArray axis={0}, bool keepdim=false, bool reduce_all=false, int in_dtype=-1, int out_dtype=-1) + output : Tensor(out) + infer_meta : + func : ReduceInferMetaBase + param : [x, axis, keepdim, reduce_all] + kernel : + func : any_raw + param : [x, axis, keepdim, reduce_all] + - op : arange args : (Tensor start, Tensor end, Tensor step) output : Tensor(out) -- GitLab