From 69e9e9d5afd577f6786b43ac5bc18de9ff263cd0 Mon Sep 17 00:00:00 2001 From: zyfncg Date: Wed, 23 Feb 2022 00:41:48 +0800 Subject: [PATCH] [PHI] Remove fill_any_like kernel register in fluid (#39807) * remove fill_any_like kernel in fluid and fix data transform bug * support scalar in infershpe * recover infershape in fill_and_like --- paddle/fluid/framework/operator.cc | 3 + paddle/fluid/imperative/prepared_operator.h | 3 + paddle/fluid/operators/fill_any_like_op.cc | 14 +--- paddle/fluid/operators/fill_any_like_op.cu | 29 -------- paddle/fluid/operators/fill_any_like_op.h | 74 ------------------- .../fluid/operators/fill_any_like_op_npu.cc | 2 +- .../fluid/operators/fill_any_like_op_xpu.cc | 2 +- paddle/phi/api/lib/CMakeLists.txt | 5 +- paddle/phi/common/scalar.h | 5 +- paddle/phi/kernels/cpu/full_kernel.cc | 4 +- paddle/phi/kernels/gpu/full_kernel.cu | 4 +- paddle/phi/kernels/xpu/full_kernel.cc | 4 +- 12 files changed, 26 insertions(+), 123 deletions(-) delete mode 100644 paddle/fluid/operators/fill_any_like_op.cu delete mode 100644 paddle/fluid/operators/fill_any_like_op.h diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 8e614faa248..e589f059f52 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -1972,6 +1972,9 @@ Scope* OperatorWithKernel::PreparePtenData( continue; } + if (in_def.backend == phi::Backend::ALL_BACKEND) { + continue; + } auto expected_place = phi::TransToPtenPlace(in_def.backend); if (platform::is_same_place(tensor_in->place(), expected_place)) { continue; diff --git a/paddle/fluid/imperative/prepared_operator.h b/paddle/fluid/imperative/prepared_operator.h index 714e4297986..879b3ec3e68 100644 --- a/paddle/fluid/imperative/prepared_operator.h +++ b/paddle/fluid/imperative/prepared_operator.h @@ -479,6 +479,9 @@ void PreparePtenData(const phi::Kernel& pt_kernel, auto var = ins_vector[offset]; const auto* tensor_in = GetTensorFromVar(var->Var()); if (tensor_in && tensor_in->IsInitialized()) { + if (in_def.backend == phi::Backend::ALL_BACKEND) { + continue; + } auto expected_place = phi::TransToPtenPlace(in_def.backend); if (platform::is_same_place(tensor_in->place(), expected_place)) { continue; diff --git a/paddle/fluid/operators/fill_any_like_op.cc b/paddle/fluid/operators/fill_any_like_op.cc index ed68bd7b7c2..e6de430a78c 100644 --- a/paddle/fluid/operators/fill_any_like_op.cc +++ b/paddle/fluid/operators/fill_any_like_op.cc @@ -12,9 +12,10 @@ 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/fill_any_like_op.h" #include +#include "paddle/fluid/framework/op_registry.h" + namespace paddle { namespace operators { @@ -91,14 +92,3 @@ REGISTER_OPERATOR( ::paddle::framework::EmptyGradOpMaker, ::paddle::framework::EmptyGradOpMaker, ops::FillAnyLikeVarTypeInference) - -REGISTER_OP_CPU_KERNEL( - fill_any_like, - ops::FillAnyLikeKernel, - ops::FillAnyLikeKernel, - ops::FillAnyLikeKernel, - ops::FillAnyLikeKernel, - ops::FillAnyLikeKernel, - ops::FillAnyLikeKernel, - ops::FillAnyLikeKernel); diff --git a/paddle/fluid/operators/fill_any_like_op.cu b/paddle/fluid/operators/fill_any_like_op.cu deleted file mode 100644 index 3ebc0ad7c8e..00000000000 --- a/paddle/fluid/operators/fill_any_like_op.cu +++ /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. */ - -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/operators/fill_any_like_op.h" -#include "paddle/fluid/platform/float16.h" - -namespace ops = paddle::operators; -REGISTER_OP_CUDA_KERNEL( - fill_any_like, - ops::FillAnyLikeKernel, - ops::FillAnyLikeKernel, - ops::FillAnyLikeKernel, - ops::FillAnyLikeKernel, - ops::FillAnyLikeKernel, - ops::FillAnyLikeKernel, - ops::FillAnyLikeKernel); diff --git a/paddle/fluid/operators/fill_any_like_op.h b/paddle/fluid/operators/fill_any_like_op.h deleted file mode 100644 index 36b56394b6f..00000000000 --- a/paddle/fluid/operators/fill_any_like_op.h +++ /dev/null @@ -1,74 +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 -#include -#include -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/framework/pten_utils.h" - -#include "paddle/phi/kernels/full_kernel.h" - -namespace paddle { -namespace operators { - -template -class FillAnyLikeKernel : public framework::OpKernel { - public: - using CommonType = typename std::common_type< - float, - typename std::conditional::value, - float, T>::type>::type; - - void Compute(const framework::ExecutionContext& context) const override { - auto* x = context.Input("X"); - auto* out = context.Output("Out"); - out->mutable_data(context.GetPlace()); - - // TODO(fangzeyang): Once context.Attribute supports double dtype, this - // kernel should be updated to support double dtype, too. - float value = context.Attr("value"); - - auto common_type_value = static_cast(value); - - PADDLE_ENFORCE_EQ( - (common_type_value >= - static_cast(std::numeric_limits::lowest())) && - (common_type_value <= - static_cast(std::numeric_limits::max())), - true, - platform::errors::InvalidArgument( - "The filled value is out of range for target type, " - "current kernel type is %s, the range should between %f " - "and %f, but now value is %f.", - typeid(T).name(), - static_cast(std::numeric_limits::lowest()), - static_cast(std::numeric_limits::max()), value)); - - PADDLE_ENFORCE_EQ( - std::isnan(value), false, - platform::errors::InvalidArgument("The filled value is NaN.")); - - const auto& dev_ctx = context.template device_context(); - // call new kernel - phi::FullLikeKernel( - static_cast::TYPE&>(dev_ctx), - *x, value, phi::DataType::UNDEFINED, out); - } -}; - -} // namespace operators -} // namespace paddle diff --git a/paddle/fluid/operators/fill_any_like_op_npu.cc b/paddle/fluid/operators/fill_any_like_op_npu.cc index a584c1341dc..2a914ff2ebd 100644 --- a/paddle/fluid/operators/fill_any_like_op_npu.cc +++ b/paddle/fluid/operators/fill_any_like_op_npu.cc @@ -12,7 +12,7 @@ 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/fill_any_like_op.h" +#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/platform/device/npu/npu_op_runner.h" namespace paddle { diff --git a/paddle/fluid/operators/fill_any_like_op_xpu.cc b/paddle/fluid/operators/fill_any_like_op_xpu.cc index 693d4431b2e..896310cd091 100644 --- a/paddle/fluid/operators/fill_any_like_op_xpu.cc +++ b/paddle/fluid/operators/fill_any_like_op_xpu.cc @@ -14,7 +14,7 @@ limitations under the License. */ #ifdef PADDLE_WITH_XPU -#include "paddle/fluid/operators/fill_any_like_op.h" +#include "paddle/fluid/framework/op_registry.h" #include "paddle/phi/kernels/full_kernel.h" diff --git a/paddle/phi/api/lib/CMakeLists.txt b/paddle/phi/api/lib/CMakeLists.txt index 720c6f54bb0..1ebddc3d3cd 100644 --- a/paddle/phi/api/lib/CMakeLists.txt +++ b/paddle/phi/api/lib/CMakeLists.txt @@ -91,8 +91,9 @@ cc_library(pten_tensor SRCS tensor_method.cc DEPS pten_tensor_raw pten_function_ cc_library(op_meta_info SRCS op_meta_info.cc DEPS pten_tensor) +cc_library(wrapped_infermeta SRCS ${wrapped_infermeta_source_file} DEPS pten) + cc_library(sparse_api SRCS sparse_api.cc DEPS pten_tensor pten kernel_dispatch pten_data_transform) -cc_library(pten_function_api SRCS ${api_source_file} DEPS pten_tensor pten kernel_dispatch pten_data_transform) +cc_library(pten_function_api SRCS ${api_source_file} DEPS pten_tensor pten kernel_dispatch pten_data_transform wrapped_infermeta) cc_library(pten_dygraph_api SRCS ${dygraph_api_source_file} DEPS pten_tensor pten kernel_dispatch pten_data_transform) cc_library(pten_bw_function_api SRCS ${bw_api_source_file} DEPS pten_tensor pten kernel_dispatch backward_infermeta pten_data_transform pten_function_api) -cc_library(wrapped_infermeta SRCS ${wrapped_infermeta_source_file} DEPS pten) diff --git a/paddle/phi/common/scalar.h b/paddle/phi/common/scalar.h index 9a5a3fbf921..72cef89d300 100644 --- a/paddle/phi/common/scalar.h +++ b/paddle/phi/common/scalar.h @@ -25,7 +25,6 @@ namespace experimental { template class ScalarBase { public: - bool FromTensor() const { return is_from_tensor_; } // Constructor support implicit ScalarBase(double val) : dtype_(DataType::FLOAT64) { // NOLINT data_.f64 = val; @@ -157,6 +156,10 @@ class ScalarBase { CopyScalar(other, this); } + bool FromTensor() const { return is_from_tensor_; } + + void SetFromTensor(bool from_tensor) { is_from_tensor_ = from_tensor; } + template inline RT to() const { switch (dtype_) { diff --git a/paddle/phi/kernels/cpu/full_kernel.cc b/paddle/phi/kernels/cpu/full_kernel.cc index 84d7f56d336..6b0183d31c6 100644 --- a/paddle/phi/kernels/cpu/full_kernel.cc +++ b/paddle/phi/kernels/cpu/full_kernel.cc @@ -99,4 +99,6 @@ PD_REGISTER_KERNEL(full_like, int, int64_t, bool, - phi::dtype::float16) {} + phi::dtype::float16) { + kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND); +} diff --git a/paddle/phi/kernels/gpu/full_kernel.cu b/paddle/phi/kernels/gpu/full_kernel.cu index d5cb1575b71..48b26540331 100644 --- a/paddle/phi/kernels/gpu/full_kernel.cu +++ b/paddle/phi/kernels/gpu/full_kernel.cu @@ -123,4 +123,6 @@ PD_REGISTER_KERNEL(full_like, int, int64_t, bool, - phi::dtype::float16) {} + phi::dtype::float16) { + kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND); +} diff --git a/paddle/phi/kernels/xpu/full_kernel.cc b/paddle/phi/kernels/xpu/full_kernel.cc index b514425cc54..574f4e991a2 100644 --- a/paddle/phi/kernels/xpu/full_kernel.cc +++ b/paddle/phi/kernels/xpu/full_kernel.cc @@ -139,4 +139,6 @@ PD_REGISTER_KERNEL(full_like, float, int, int64_t, - phi::dtype::float16) {} + phi::dtype::float16) { + kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND); +} -- GitLab