From 2ad66a42282072b645ba0ae051435d971ef9d0ab Mon Sep 17 00:00:00 2001 From: RedContritio Date: Thu, 6 Apr 2023 10:42:21 +0800 Subject: [PATCH] support auto generate static for empty (#52524) --- paddle/fluid/operators/empty_op.cc | 100 ------------------ paddle/fluid/operators/unity_build_rule.cmake | 1 - paddle/phi/api/yaml/op_compat.yaml | 9 ++ paddle/phi/api/yaml/static_ops.yaml | 11 ++ paddle/phi/ops/compat/empty_sig.cc | 31 ------ 5 files changed, 20 insertions(+), 132 deletions(-) delete mode 100644 paddle/fluid/operators/empty_op.cc delete mode 100644 paddle/phi/ops/compat/empty_sig.cc diff --git a/paddle/fluid/operators/empty_op.cc b/paddle/fluid/operators/empty_op.cc deleted file mode 100644 index a5c707d460a..00000000000 --- a/paddle/fluid/operators/empty_op.cc +++ /dev/null @@ -1,100 +0,0 @@ -/* Copyright (c) 2020 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/nullary.h" - -namespace paddle { -namespace operators { - -class EmptyOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("ShapeTensor", - "(Tensor), optional). The shape of the output." - "It has a higher priority than Attr(shape).") - .AsDispensable(); - AddInput("ShapeTensorList", - "(vector>, optional). The shape of the output. " - "It has a higher priority than Attr(shape)." - "The shape of the element in vector must be [1].") - .AsDuplicable() - .AsDispensable(); - AddAttr>("shape", - "(vector) The shape of the output") - .SetDefault({}); - AddAttr("dtype", "The data type of output tensor, Default is float") - .SetDefault(framework::proto::VarType::FP32); - AddOutput("Out", "(Tensor) The output tensor."); - AddComment(R"DOC(empty operator -Returns a tensor filled with uninitialized data. The shape of the tensor is -defined by the variable argument shape. - - -The type of the tensor is specify by `dtype`. -)DOC"); - } -}; - -class EmptyOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - phi::KernelKey GetKernelTypeForVar( - const std::string& var_name, - const phi::DenseTensor& tensor, - const phi::KernelKey& expected_kernel_type) const override { - if (var_name == "ShapeTensor" || var_name == "ShapeTensorList") { - return phi::KernelKey(phi::Backend::ALL_BACKEND, - expected_kernel_type.layout(), - expected_kernel_type.dtype()); - } else { - return phi::KernelKey( - tensor.place(), tensor.layout(), expected_kernel_type.dtype()); - } - } - - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext& context) const override { - return phi::KernelKey( - framework::proto::VarType::Type(context.Attr("dtype")), - context.GetPlace()); - } -}; - -class EmptyOpVarTypeInference : public framework::VarTypeInference { - public: - void operator()(framework::InferVarTypeContext* context) const override { - auto data_type = static_cast( - PADDLE_GET_CONST(int, context->GetAttr("dtype"))); - context->SetOutputDataType("Out", data_type); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -namespace plat = paddle::platform; - -DECLARE_INFER_SHAPE_FUNCTOR(empty, - EmptyInferShapeFunctor, - PD_INFER_META(phi::CreateInferMeta)); -REGISTER_OP_WITHOUT_GRADIENT(empty, - ops::EmptyOp, - ops::EmptyOpMaker, - ops::EmptyOpVarTypeInference, - EmptyInferShapeFunctor); diff --git a/paddle/fluid/operators/unity_build_rule.cmake b/paddle/fluid/operators/unity_build_rule.cmake index 45c246b00c7..be64051ef27 100644 --- a/paddle/fluid/operators/unity_build_rule.cmake +++ b/paddle/fluid/operators/unity_build_rule.cmake @@ -82,7 +82,6 @@ register_unity_group( diag_v2_op.cc dot_op.cc edit_distance_op.cc - empty_op.cc enqueue_op.cc erf_op.cc py_func_op.cc diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 27fa51511ca..cacac7dfb9f 100644 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -602,6 +602,15 @@ int trainer_id = 0, int slot = 0, 'int64_t[] height_sections = {}', 'str[] epmap = {}', 'str[] table_names = {}'] +- op : empty + outputs : + out : Out + int_array: + shape : + data_type : int64_t + tensor_name : ShapeTensor + tensors_name : ShapeTensorList + - op : equal inputs : {x : X, y : Y} diff --git a/paddle/phi/api/yaml/static_ops.yaml b/paddle/phi/api/yaml/static_ops.yaml index 7ec37a8d063..8f9201d7ec4 100644 --- a/paddle/phi/api/yaml/static_ops.yaml +++ b/paddle/phi/api/yaml/static_ops.yaml @@ -70,6 +70,17 @@ data_type : weight backward : embedding_grad +- op : empty + args : (IntArray shape = {}, DataType dtype = DataType::FLOAT32) + output: Tensor(out) + infer_meta : + func : CreateInferMeta + param : [shape, dtype] + kernel : + func : empty + param : [shape, dtype] + data_type : dtype + - op : equal args : (Tensor x, Tensor y, int axis = -1, bool force_cpu=false) output : Tensor(out) diff --git a/paddle/phi/ops/compat/empty_sig.cc b/paddle/phi/ops/compat/empty_sig.cc deleted file mode 100644 index 42cd55bdc0c..00000000000 --- a/paddle/phi/ops/compat/empty_sig.cc +++ /dev/null @@ -1,31 +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 EmptyOpArgumentMapping(const ArgumentMappingContext& ctx) { - if (ctx.HasInput("ShapeTensor")) { - return KernelSignature("empty", {}, {"ShapeTensor", "dtype"}, {"Out"}); - } else if (ctx.InputSize("ShapeTensorList") > 0) { - return KernelSignature("empty", {}, {"ShapeTensorList", "dtype"}, {"Out"}); - } else { - return KernelSignature("empty", {}, {"shape", "dtype"}, {"Out"}); - } -} - -} // namespace phi - -PD_REGISTER_ARG_MAPPING_FN(empty, phi::EmptyOpArgumentMapping); -- GitLab