From 6ab80b64033e8a784fea8e1da9a2e2f3c682a174 Mon Sep 17 00:00:00 2001 From: zyfncg Date: Fri, 26 Aug 2022 17:37:11 +0800 Subject: [PATCH] [Phi] Delete xpu kernel of fill_any_like and fill_constant in fluid (#45420) * delete fill xpu op in fluid * delete fill_constant header, test=kunlun * fix npu header, test=kunlun --- .../fluid/operators/fill_any_like_op_xpu.cc | 87 --------- .../fill_constant_batch_size_like_op_mlu.cc | 1 - .../fill_constant_batch_size_like_op_npu.cc | 3 +- paddle/fluid/operators/fill_constant_op.cc | 4 +- paddle/fluid/operators/fill_constant_op.h | 180 ------------------ .../fluid/operators/fill_constant_op_mlu.cc | 2 +- .../fluid/operators/fill_constant_op_npu.cc | 2 +- .../fluid/operators/fill_constant_op_xpu.cc | 30 --- paddle/fluid/operators/gaussian_random_op.cc | 1 - paddle/fluid/operators/gaussian_random_op.cu | 1 - .../fluid/operators/gaussian_random_op_npu.cc | 1 - paddle/fluid/operators/index_impl.cu.h | 1 - .../mkldnn/gaussian_random_mkldnn_op.cc | 3 +- 13 files changed, 8 insertions(+), 308 deletions(-) delete mode 100644 paddle/fluid/operators/fill_any_like_op_xpu.cc delete mode 100644 paddle/fluid/operators/fill_constant_op.h delete mode 100644 paddle/fluid/operators/fill_constant_op_xpu.cc diff --git a/paddle/fluid/operators/fill_any_like_op_xpu.cc b/paddle/fluid/operators/fill_any_like_op_xpu.cc deleted file mode 100644 index 166067db1b4..00000000000 --- a/paddle/fluid/operators/fill_any_like_op_xpu.cc +++ /dev/null @@ -1,87 +0,0 @@ -/* Copyright (c) 2021 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. */ - -#ifdef PADDLE_WITH_XPU - -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/phi/kernels/full_kernel.h" - -namespace paddle { -namespace operators { - -template -class FillAnyLikeXPUKernel : public framework::OpKernel { - public: - using CommonType = typename std::common_type< - float, - typename std::conditional::value, - float, - T>::type>::type; - using XPUInTDType = typename XPUTypeTrait::Type; - - void Compute(const framework::ExecutionContext& context) const override { - auto* x = context.Input("X"); - auto* out = context.Output("Out"); - out->mutable_data(context.GetPlace()); - - 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.")); - - auto& dev_ctx = - context.template device_context(); - - // call phi kernel - phi::FullLikeKernel( - static_cast::TYPE&>(dev_ctx), - *x, - value, - phi::DataType::UNDEFINED, - out); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; - -REGISTER_OP_XPU_KERNEL(fill_any_like, - ops::FillAnyLikeXPUKernel, - ops::FillAnyLikeXPUKernel, - ops::FillAnyLikeXPUKernel, - ops::FillAnyLikeXPUKernel); - -#endif diff --git a/paddle/fluid/operators/fill_constant_batch_size_like_op_mlu.cc b/paddle/fluid/operators/fill_constant_batch_size_like_op_mlu.cc index 32a19750f42..93fd3c5f3dd 100644 --- a/paddle/fluid/operators/fill_constant_batch_size_like_op_mlu.cc +++ b/paddle/fluid/operators/fill_constant_batch_size_like_op_mlu.cc @@ -13,7 +13,6 @@ 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_constant_op.h" #include "paddle/fluid/operators/mlu/mlu_baseop.h" #include "paddle/fluid/operators/utils.h" diff --git a/paddle/fluid/operators/fill_constant_batch_size_like_op_npu.cc b/paddle/fluid/operators/fill_constant_batch_size_like_op_npu.cc index 02f89cfdd26..479b2e19096 100644 --- a/paddle/fluid/operators/fill_constant_batch_size_like_op_npu.cc +++ b/paddle/fluid/operators/fill_constant_batch_size_like_op_npu.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_constant_op.h" +#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/utils.h" #include "paddle/fluid/platform/device/npu/npu_op_runner.h" +#include "paddle/phi/kernels/funcs/math_function.h" namespace paddle { namespace operators { diff --git a/paddle/fluid/operators/fill_constant_op.cc b/paddle/fluid/operators/fill_constant_op.cc index 68ea043d680..0dd0e1dcecf 100644 --- a/paddle/fluid/operators/fill_constant_op.cc +++ b/paddle/fluid/operators/fill_constant_op.cc @@ -12,11 +12,11 @@ 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_constant_op.h" - #include +#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_version_registry.h" + namespace paddle { namespace operators { diff --git a/paddle/fluid/operators/fill_constant_op.h b/paddle/fluid/operators/fill_constant_op.h deleted file mode 100644 index bd8303fe402..00000000000 --- a/paddle/fluid/operators/fill_constant_op.h +++ /dev/null @@ -1,180 +0,0 @@ -/* Copyright (c) 2018 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 - -#include "paddle/fluid/framework/convert_utils.h" -#include "paddle/fluid/framework/data_type.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/operators/utils.h" -#include "paddle/phi/kernels/funcs/math_function.h" - -namespace paddle { -namespace operators { - -using Tensor = framework::Tensor; - -template -class FillConstantKernel : public framework::OpKernel { - public: - void Compute(const paddle::framework::ExecutionContext &ctx) const override { - auto data_type = - static_cast(ctx.Attr("dtype")); - - auto str_value = ctx.Attr("str_value"); - auto float_value = ctx.Attr("value"); - auto force_cpu = ctx.Attr("force_cpu"); - auto place_type = ctx.Attr("place_type"); - framework::Tensor *tensor = nullptr; - - framework::Variable *out_var = ctx.OutputVar("Out"); - - T value; - if (str_value.empty()) { - value = static_cast(float_value); - } else { - // handle NaN/Inf first, which cannot be read from stream. - if (str_value == "inf") { - value = static_cast(std::numeric_limits::infinity()); - } else if (str_value == "-inf") { - value = static_cast(-std::numeric_limits::infinity()); - } else if (str_value == "nan") { - value = static_cast(std::numeric_limits::quiet_NaN()); - } else { - std::stringstream convert_stream(str_value); - if (std::is_same::value) { - int64_t tmp_value; - convert_stream >> tmp_value; - value = static_cast(tmp_value); - } else { - double tmp_value; - convert_stream >> tmp_value; - value = static_cast(tmp_value); - } - } - } - if (ctx.HasInput("ValueTensor")) { - auto *value_tensor = ctx.Input("ValueTensor"); - PADDLE_ENFORCE_EQ( - value_tensor->numel(), - 1, - platform::errors::InvalidArgument( - "When use Tensor as value to set Tensor value in fill_cosntant, " - "value input(ValueTensor) size must be 1, but get %d", - value_tensor->numel())); - const T *tensor_data = value_tensor->data(); - framework::Tensor cpu_tensor; - auto tmp_place = value_tensor->place(); - if (platform::is_gpu_place(tmp_place) || - platform::is_xpu_place(tmp_place)) { - paddle::framework::TensorCopySync( - *value_tensor, platform::CPUPlace(), &cpu_tensor); - tensor_data = cpu_tensor.data(); - } - value = tensor_data[0]; - } - auto shape = GetShape(ctx); - - if (out_var->IsType()) { - tensor = out_var->GetMutable(); - tensor->Resize(shape); - } else if (out_var->IsType()) { - tensor = out_var->GetMutable()->mutable_value(); - tensor->Resize(shape); - } else { - PADDLE_THROW(platform::errors::Unimplemented( - "In fill constant Op, the output only supports SelectedRows and " - "LoDTensor.")); - } - - platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance(); - int actual_place = place_type; - - if (actual_place == -1) { - bool cpu_place = (force_cpu || ctx.GetPlace() == platform::CPUPlace() || - data_type == framework::proto::VarType::BF16); - if (cpu_place) { - actual_place = 0; - } else if (platform::is_gpu_place(ctx.GetPlace())) { - actual_place = 1; - } else if (platform::is_xpu_place(ctx.GetPlace())) { - actual_place = 3; - } - } - - if (actual_place == 0) { - VLOG(4) << "[CPU] FillConstantKernel" - << ((data_type == framework::proto::VarType::BF16) ? "" - : ""); - tensor->mutable_data(platform::CPUPlace(), - framework::TransToPhiDataType(data_type)); - phi::funcs::SetConstant functor; - auto &dev_ctx = *pool.Get(platform::CPUPlace()); - functor(reinterpret_cast(dev_ctx), - tensor, - static_cast(value)); - } else if (actual_place == 1) { -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - tensor->mutable_data(ctx.GetPlace(), - framework::TransToPhiDataType(data_type)); - phi::funcs::SetConstant functor; - auto &dev_ctx = *pool.Get(ctx.GetPlace()); - functor(reinterpret_cast(dev_ctx), - tensor, - static_cast(value)); -#else - PADDLE_THROW(platform::errors::PreconditionNotMet( - "PaddlePaddle should compile with GPU.")); -#endif - } else if (actual_place == 2) { -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - tensor->mutable_data(platform::CUDAPinnedPlace(), - framework::TransToPhiDataType(data_type)); - phi::funcs::SetConstant functor; - auto &dev_ctx = *pool.Get(platform::CUDAPinnedPlace()); - functor( - reinterpret_cast(dev_ctx), - tensor, - static_cast(value)); -#else - PADDLE_THROW(platform::errors::PreconditionNotMet( - "PaddlePaddle should compile with GPU.")); -#endif - } else if (actual_place == 3) { -#ifdef PADDLE_WITH_XPU - tensor->mutable_data(ctx.GetPlace(), - framework::TransToPhiDataType(data_type)); - phi::funcs::SetConstant functor; - auto &dev_ctx = *pool.Get(ctx.GetPlace()); - functor(reinterpret_cast(dev_ctx), - tensor, - static_cast(value)); -#else - PADDLE_THROW(platform::errors::PreconditionNotMet( - "PaddlePaddle should compile with XPU.")); -#endif - } else { - PADDLE_THROW(platform::errors::Unimplemented( - "Could NOT determine the place of variable, place_type = %d .", - actual_place)); - } - } -}; -} // namespace operators -} // namespace paddle diff --git a/paddle/fluid/operators/fill_constant_op_mlu.cc b/paddle/fluid/operators/fill_constant_op_mlu.cc index 4256945777b..e2b4cd547d2 100644 --- a/paddle/fluid/operators/fill_constant_op_mlu.cc +++ b/paddle/fluid/operators/fill_constant_op_mlu.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_constant_op.h" +#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/mlu/mlu_baseop.h" namespace paddle { diff --git a/paddle/fluid/operators/fill_constant_op_npu.cc b/paddle/fluid/operators/fill_constant_op_npu.cc index 097423afe98..6e7f5e28914 100644 --- a/paddle/fluid/operators/fill_constant_op_npu.cc +++ b/paddle/fluid/operators/fill_constant_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_constant_op.h" +#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/utils.h" #include "paddle/fluid/platform/device/npu/npu_op_runner.h" diff --git a/paddle/fluid/operators/fill_constant_op_xpu.cc b/paddle/fluid/operators/fill_constant_op_xpu.cc deleted file mode 100644 index 62ca2e672f9..00000000000 --- a/paddle/fluid/operators/fill_constant_op_xpu.cc +++ /dev/null @@ -1,30 +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/operators/fill_constant_op.h" - -namespace ops = paddle::operators; -#ifdef PADDLE_WITH_XPU -REGISTER_OP_XPU_KERNEL( - fill_constant, - ops::FillConstantKernel, - ops::FillConstantKernel, - ops::FillConstantKernel, - ops::FillConstantKernel, - ops::FillConstantKernel, - ops::FillConstantKernel, - ops::FillConstantKernel, - ops::FillConstantKernel, - ops::FillConstantKernel>, - ops::FillConstantKernel>); -#endif diff --git a/paddle/fluid/operators/gaussian_random_op.cc b/paddle/fluid/operators/gaussian_random_op.cc index f4fea27aead..80964323e6b 100644 --- a/paddle/fluid/operators/gaussian_random_op.cc +++ b/paddle/fluid/operators/gaussian_random_op.cc @@ -18,7 +18,6 @@ limitations under the License. */ #include "paddle/fluid/framework/infershape_utils.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_version_registry.h" -#include "paddle/fluid/operators/fill_constant_op.h" #ifdef PADDLE_WITH_MKLDNN #include "paddle/fluid/platform/mkldnn_helper.h" #endif diff --git a/paddle/fluid/operators/gaussian_random_op.cu b/paddle/fluid/operators/gaussian_random_op.cu index 81b53c8b949..4df716f79f2 100644 --- a/paddle/fluid/operators/gaussian_random_op.cu +++ b/paddle/fluid/operators/gaussian_random_op.cu @@ -17,7 +17,6 @@ limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/operators/amp/fp16_type_traits.h" -#include "paddle/fluid/operators/fill_constant_op.h" #include "paddle/phi/kernels/funcs/index_impl.cu.h" namespace paddle { diff --git a/paddle/fluid/operators/gaussian_random_op_npu.cc b/paddle/fluid/operators/gaussian_random_op_npu.cc index a0200d623ec..8b3af57d923 100644 --- a/paddle/fluid/operators/gaussian_random_op_npu.cc +++ b/paddle/fluid/operators/gaussian_random_op_npu.cc @@ -18,7 +18,6 @@ limitations under the License. */ #include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_version_registry.h" -#include "paddle/fluid/operators/fill_constant_op.h" #ifdef PADDLE_WITH_MKLDNN #include "paddle/fluid/platform/mkldnn_helper.h" #endif diff --git a/paddle/fluid/operators/index_impl.cu.h b/paddle/fluid/operators/index_impl.cu.h index dfecd0b0ef2..ab411ad8ac6 100644 --- a/paddle/fluid/operators/index_impl.cu.h +++ b/paddle/fluid/operators/index_impl.cu.h @@ -20,7 +20,6 @@ limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/operators/amp/fp16_type_traits.h" -#include "paddle/fluid/operators/fill_constant_op.h" #include "paddle/phi/backends/gpu/gpu_launch_config.h" #include "paddle/phi/core/hostdevice.h" #include "paddle/phi/kernels/funcs/aligned_vector.h" diff --git a/paddle/fluid/operators/mkldnn/gaussian_random_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/gaussian_random_mkldnn_op.cc index d5c22b9a295..ad3289213f8 100644 --- a/paddle/fluid/operators/mkldnn/gaussian_random_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/gaussian_random_mkldnn_op.cc @@ -15,7 +15,8 @@ limitations under the License. */ #include #include "paddle/fluid/framework/generator.h" -#include "paddle/fluid/operators/fill_constant_op.h" +#include "paddle/fluid/framework/op_registry.h" +#include "paddle/fluid/operators/utils.h" #include "paddle/fluid/platform/mkldnn_reuse.h" namespace paddle { -- GitLab