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 166067db1b463a73a76f84b2cd2672775160108e..0000000000000000000000000000000000000000 --- 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 32a19750f420a9c06fe4a2734e24048e0db932c5..93fd3c5f3ddbd7e43c7c90d3086ca98164ea7bd9 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 02f89cfdd2691231a60e8e6076911f2eda36a483..479b2e19096e5e2ab53bc114fbb3f23a99a62bf0 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 68ea043d680cb65d745d9ddca37b9a56118ab0b2..0dd0e1dcecf6bfdcdf08e321def0685f9c76d608 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 bd8303fe402f41da0cf22e9170d90d1f06d062a3..0000000000000000000000000000000000000000 --- 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 4256945777bb1ad7fb26ec96bb54b9fba9496d4c..e2b4cd547d24b5ebdc84a256fe9ea47ac4e9a624 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 097423afe98527066e17735b295d24b522cf5889..6e7f5e289142a58939e51fc2cdb89e09a2ab1b9b 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 62ca2e672f998c41fb5e7404063d34585c5ed375..0000000000000000000000000000000000000000 --- 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 f4fea27aeadace57b3411b6689e1e89620cc2dfd..80964323e6b012cfbfa6b27529724813e73e8e01 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 81b53c8b949763840ed71d4107e42f1ebe8ee5b9..4df716f79f2afd96cfff10605cc13075ce3ef3ff 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 a0200d623ec76a8bae3ee5cf1ab09813249c76f3..8b3af57d923fe5c6c80de321901f46a49c266100 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 dfecd0b0ef2b7bcd58cac8789f6138cfa475c04c..ab411ad8ac625abf38306815787976b34b404711 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 d5c22b9a2952e6253b662433508de328385bef67..ad3289213f87c43f48444c00506fe234b9d3d5d4 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 {