diff --git a/paddle/fluid/operators/fill_any_like_op.cc b/paddle/fluid/operators/fill_any_like_op.cc deleted file mode 100644 index 2efe0eeb720b0f95e8a8b75a0e8e8c63ae402135..0000000000000000000000000000000000000000 --- a/paddle/fluid/operators/fill_any_like_op.cc +++ /dev/null @@ -1,98 +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. */ - -#include - -#include "paddle/fluid/framework/op_registry.h" - -namespace paddle { -namespace operators { - -class FillAnyLikeOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - void InferShape(framework::InferShapeContext *ctx) const override { - OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "fill_any_like"); - OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "fill_any_like"); - ctx->SetOutputDim("Out", ctx->GetInputDim("X")); - ctx->ShareLoD("X", /*->*/ "Out"); - } - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext &ctx) const override { - phi::KernelKey kt = OperatorWithKernel::GetExpectedKernelType(ctx); - const auto &data_type = ctx.Attr("dtype"); - if (data_type >= 0) { - kt.set_dtype(phi::TransToPhiDataType( - static_cast(data_type))); - } - return kt; - } - - phi::KernelKey GetKernelTypeForVar( - const std::string &var_name, - const phi::DenseTensor &tensor, - const phi::KernelKey &expected_kernel_type) const override { - return phi::KernelKey(phi::Backend::ALL_BACKEND, - tensor.layout(), - expected_kernel_type.dtype()); - } -}; - -class FillAnyLikeOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("X", "The input of fill-zeros-like op."); - AddOutput("Out", "The variable will be filled up with specified value."); - AddAttr("value", "The filled value").SetDefault(0.0); - AddAttr("dtype", - "Output tensor data type. default value is -1," - "according to the input dtype.") - .SetDefault(-1); - AddComment(R"DOC( -FillAnyLike Operator. - -Fill up a variable with Attr(value). -The output will have the same shape and dtype as the input. - -)DOC"); - } -}; - -class FillAnyLikeVarTypeInference : public framework::VarTypeInference { - public: - void operator()(framework::InferVarTypeContext *ctx) const override { - auto var_data_type = static_cast( - PADDLE_GET_CONST(int, ctx->GetAttr("dtype"))); - if (var_data_type < 0) { - ctx->SetOutputDataType("Out", ctx->GetInputDataType("X")); - } else { - ctx->SetOutputDataType("Out", var_data_type); - } - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -REGISTER_OPERATOR( - fill_any_like, - ops::FillAnyLikeOp, - ops::FillAnyLikeOpMaker, - ::paddle::framework::EmptyGradOpMaker, - ::paddle::framework::EmptyGradOpMaker, - ops::FillAnyLikeVarTypeInference) diff --git a/paddle/fluid/operators/generator/templates/operator_utils.c.j2 b/paddle/fluid/operators/generator/templates/operator_utils.c.j2 index 0253697ba681441ee253b4ae3971803e8fb3bb08..bebd2a1513181f5e4b3e17a6ad4f0e10f6490522 100644 --- a/paddle/fluid/operators/generator/templates/operator_utils.c.j2 +++ b/paddle/fluid/operators/generator/templates/operator_utils.c.j2 @@ -335,8 +335,8 @@ phi::KernelKey GetExpectedKernelType( {% endif %} {% elif kernel["data_type"]["candidates"] | length == 2 %} {% set data_type_args = kernel["data_type"]["candidates"] %} - auto data_type = framework::proto::VarType::Type(ctx.Attr("{{data_type_args[0]}}"); - if (data_type == static_cast(-1)) { + auto data_type = framework::proto::VarType::Type(ctx.Attr("{{data_type_args[0]}}")); + if (data_type == static_cast(-1)) { data_type = framework::OperatorWithKernel::IndicateVarDataType(ctx, {{data_type_args[1] | to_opmaker_name}}); } {% endif %} diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 04b372d391726b063063e43058b73e6b382f0d5f..4d1d29f5f68fee31a7e8d367236cf9eab22d2a14 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -1095,8 +1095,10 @@ x : X outputs : out : Out - attrs : - {value: value, dtype: dtype} + scalar : + value : + data_type : float + support_tensor : true - op : fused_conv2d extra : diff --git a/paddle/phi/api/yaml/static_ops.yaml b/paddle/phi/api/yaml/static_ops.yaml index ec6dfc0ef486dfb8c014031519f2d7768e796d7c..abacbe9f716461548f34b81679b09d17e0873f8a 100755 --- a/paddle/phi/api/yaml/static_ops.yaml +++ b/paddle/phi/api/yaml/static_ops.yaml @@ -203,6 +203,16 @@ param : [x, axis, keepdim, reduce_all] backward : frobenius_norm_grad +- op : full_like + args : (Tensor x, Scalar value = 0.0, DataType dtype = DataType::UNDEFINED) + output: Tensor(out) + infer_meta : + func : FillAnyLikeInferMeta + kernel : + func : full_like + param : [x, value, dtype] + data_type : dtype > x + - op : gaussian args : (IntArray shape = {}, float mean = .0f, float std = 1.0f, int seed = 0, DataType dtype = DataType::FLOAT32) output: Tensor(out) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index ddc120397fe20f9de5fb313d7bda717f38e7fa4c..5bfa6108cee922ec202b613e10d7fba5bb697e29 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -1111,6 +1111,15 @@ void ExpandInferMeta(const MetaTensor& x, } } +void FillAnyLikeInferMeta(const MetaTensor& x, + const Scalar& value, + DataType dtype, + MetaTensor* out) { + out->set_dims(x.dims()); + out->set_dtype(dtype == DataType::UNDEFINED ? x.dtype() : dtype); + out->share_lod(x); +} + void FillDiagonalInferMeta( const MetaTensor& x, float value, int offset, bool wrap, MetaTensor* out) { PADDLE_ENFORCE_NE( diff --git a/paddle/phi/infermeta/unary.h b/paddle/phi/infermeta/unary.h index 512e27086eea9271bb66dee7bd7b7abaa0e513f8..2fee9bc479aa966fddbcc979af7c0d60eb91d386 100644 --- a/paddle/phi/infermeta/unary.h +++ b/paddle/phi/infermeta/unary.h @@ -178,6 +178,11 @@ void ExpandInferMeta(const MetaTensor& x, const IntArray& shape, MetaTensor* out); +void FillAnyLikeInferMeta(const MetaTensor& x, + const Scalar& value, + DataType dtype, + MetaTensor* out); + void FillDiagonalInferMeta( const MetaTensor& x, float value, int offset, bool wrap, MetaTensor* out); diff --git a/paddle/phi/ops/compat/fill_any_like_sig.cc b/paddle/phi/ops/compat/fill_any_like_sig.cc deleted file mode 100644 index e49bb2a4de3ce0a6804ef449d7755deee411e46a..0000000000000000000000000000000000000000 --- a/paddle/phi/ops/compat/fill_any_like_sig.cc +++ /dev/null @@ -1,28 +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 FillAnyLikeOpArgumentMapping( - const ArgumentMappingContext& ctx UNUSED) { - return KernelSignature("full_like", {"X"}, {"value", "dtype"}, {"Out"}); -} - -} // namespace phi - -PD_REGISTER_BASE_KERNEL_NAME(fill_any_like, full_like); - -PD_REGISTER_ARG_MAPPING_FN(fill_any_like, phi::FillAnyLikeOpArgumentMapping);