From 12346cdce77fb97af708d6b8f04ff9231a311229 Mon Sep 17 00:00:00 2001 From: zyfncg Date: Fri, 4 Mar 2022 20:07:16 +0800 Subject: [PATCH] [PHI] Move header of selected_rows kernel to selected_rows dir (#40128) * move selected_rows kernel head to selected_rows dir * update license * add sr namespace * refacter selected_rows kernel funciton name * fix bug --- paddle/phi/kernels/full_kernel.h | 8 --- paddle/phi/kernels/scale_kernel.h | 9 ---- .../phi/kernels/selected_rows/full_kernel.cc | 19 ++++--- .../phi/kernels/selected_rows/full_kernel.h | 32 ++++++++++++ .../phi/kernels/selected_rows/scale_kernel.cc | 21 ++++---- .../phi/kernels/selected_rows/scale_kernel.h | 32 ++++++++++++ .../selected_rows/uniform_random_kernel.cc | 50 +++++++++++-------- .../selected_rows/uniform_random_kernel.h | 45 +++++++++++++++++ paddle/phi/kernels/uniform_random_kernel.h | 22 -------- 9 files changed, 160 insertions(+), 78 deletions(-) create mode 100644 paddle/phi/kernels/selected_rows/full_kernel.h create mode 100644 paddle/phi/kernels/selected_rows/scale_kernel.h create mode 100644 paddle/phi/kernels/selected_rows/uniform_random_kernel.h diff --git a/paddle/phi/kernels/full_kernel.h b/paddle/phi/kernels/full_kernel.h index c7b1f9af0e3..05929ba83f3 100644 --- a/paddle/phi/kernels/full_kernel.h +++ b/paddle/phi/kernels/full_kernel.h @@ -17,7 +17,6 @@ #include "paddle/phi/common/scalar.h" #include "paddle/phi/common/scalar_array.h" #include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/selected_rows.h" #include "paddle/phi/infermeta/nullary.h" #include "paddle/phi/kernels/empty_kernel.h" @@ -31,13 +30,6 @@ void FullKernel(const Context& dev_ctx, DataType dtype, DenseTensor* out); -template -void FullSR(const Context& dev_ctx, - const ScalarArray& shape, - const Scalar& val, - DataType dtype, - SelectedRows* out); - template void FullLikeKernel(const Context& dev_ctx, const DenseTensor& x, diff --git a/paddle/phi/kernels/scale_kernel.h b/paddle/phi/kernels/scale_kernel.h index b3958343698..22e6efb03ac 100644 --- a/paddle/phi/kernels/scale_kernel.h +++ b/paddle/phi/kernels/scale_kernel.h @@ -16,7 +16,6 @@ limitations under the License. */ #include "paddle/phi/common/scalar.h" #include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/selected_rows.h" #include "paddle/phi/infermeta/unary.h" #include "paddle/phi/kernels/empty_kernel.h" namespace phi { @@ -29,14 +28,6 @@ void ScaleKernel(const Context& dev_ctx, bool bias_after_scale, DenseTensor* out); -template -void ScaleSR(const Context& dev_ctx, - const SelectedRows& x, - const Scalar& scale, - float bias, - bool bias_after_scale, - SelectedRows* out); - template DenseTensor Scale(const Context& dev_ctx, const DenseTensor& x, diff --git a/paddle/phi/kernels/selected_rows/full_kernel.cc b/paddle/phi/kernels/selected_rows/full_kernel.cc index 02231867fdd..39fd009cd65 100644 --- a/paddle/phi/kernels/selected_rows/full_kernel.cc +++ b/paddle/phi/kernels/selected_rows/full_kernel.cc @@ -12,34 +12,37 @@ 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/kernels/full_kernel.h" +#include "paddle/phi/kernels/selected_rows/full_kernel.h" #include "paddle/phi/backends/cpu/cpu_context.h" #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) #include "paddle/phi/backends/gpu/gpu_context.h" #endif #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/common/bfloat16.h" #include "paddle/phi/common/complex.h" namespace phi { +namespace sr { template -void FullSR(const Context& dev_ctx, - const ScalarArray& shape, - const Scalar& val, - DataType dtype, - SelectedRows* out) { +void FullKernel(const Context& dev_ctx, + const ScalarArray& shape, + const Scalar& val, + DataType dtype, + SelectedRows* out) { phi::FullKernel(dev_ctx, shape, val, dtype, out->mutable_value()); } +} // namespace sr } // namespace phi PD_REGISTER_KERNEL(full_sr, CPU, ALL_LAYOUT, - phi::FullSR, + phi::sr::FullKernel, float, double, uint8_t, @@ -56,7 +59,7 @@ PD_REGISTER_KERNEL(full_sr, PD_REGISTER_KERNEL(full_sr, GPU, ALL_LAYOUT, - phi::FullSR, + phi::sr::FullKernel, float, double, uint8_t, diff --git a/paddle/phi/kernels/selected_rows/full_kernel.h b/paddle/phi/kernels/selected_rows/full_kernel.h new file mode 100644 index 00000000000..d84ddcc0d3f --- /dev/null +++ b/paddle/phi/kernels/selected_rows/full_kernel.h @@ -0,0 +1,32 @@ +// 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. + +#pragma once + +#include "paddle/phi/common/scalar.h" +#include "paddle/phi/common/scalar_array.h" +#include "paddle/phi/core/selected_rows.h" + +namespace phi { +namespace sr { + +template +void FullKernel(const Context& dev_ctx, + const ScalarArray& shape, + const Scalar& val, + DataType dtype, + SelectedRows* out); + +} // namespace sr +} // namespace phi diff --git a/paddle/phi/kernels/selected_rows/scale_kernel.cc b/paddle/phi/kernels/selected_rows/scale_kernel.cc index 094b6f4d120..38a0cb75101 100644 --- a/paddle/phi/kernels/selected_rows/scale_kernel.cc +++ b/paddle/phi/kernels/selected_rows/scale_kernel.cc @@ -12,21 +12,23 @@ 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/kernels/scale_kernel.h" +#include "paddle/phi/kernels/selected_rows/scale_kernel.h" #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/common/bfloat16.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/scale_kernel.h" namespace phi { +namespace sr { template -void ScaleSR(const Context& dev_ctx, - const SelectedRows& x, - const Scalar& scale, - float bias, - bool bias_after_scale, - SelectedRows* out) { +void ScaleKernel(const Context& dev_ctx, + const SelectedRows& x, + const Scalar& scale, + float bias, + bool bias_after_scale, + SelectedRows* out) { if (x.value().Holder() != out->value().Holder() || x.value().data() != out->value().data()) { out->set_rows(x.rows()); @@ -36,12 +38,13 @@ void ScaleSR(const Context& dev_ctx, dev_ctx, x.value(), scale, bias, bias_after_scale, out->mutable_value()); } +} // namespace sr } // namespace phi PD_REGISTER_KERNEL(scale_sr, CPU, ALL_LAYOUT, - phi::ScaleSR, + phi::sr::ScaleKernel, float, double, phi::dtype::bfloat16, @@ -55,7 +58,7 @@ PD_REGISTER_KERNEL(scale_sr, PD_REGISTER_KERNEL(scale_sr, GPU, ALL_LAYOUT, - phi::ScaleSR, + phi::sr::ScaleKernel, float, double, phi::dtype::float16, diff --git a/paddle/phi/kernels/selected_rows/scale_kernel.h b/paddle/phi/kernels/selected_rows/scale_kernel.h new file mode 100644 index 00000000000..85c2c4ddff0 --- /dev/null +++ b/paddle/phi/kernels/selected_rows/scale_kernel.h @@ -0,0 +1,32 @@ +/* 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. */ + +#pragma once + +#include "paddle/phi/common/scalar.h" +#include "paddle/phi/core/selected_rows.h" + +namespace phi { +namespace sr { + +template +void ScaleKernel(const Context& dev_ctx, + const SelectedRows& x, + const Scalar& scale, + float bias, + bool bias_after_scale, + SelectedRows* out); + +} // namespace sr +} // namespace phi diff --git a/paddle/phi/kernels/selected_rows/uniform_random_kernel.cc b/paddle/phi/kernels/selected_rows/uniform_random_kernel.cc index 881180b71b1..b3dd1d1b7d2 100644 --- a/paddle/phi/kernels/selected_rows/uniform_random_kernel.cc +++ b/paddle/phi/kernels/selected_rows/uniform_random_kernel.cc @@ -12,22 +12,27 @@ 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/kernels/uniform_random_kernel.h" +#include "paddle/phi/kernels/selected_rows/uniform_random_kernel.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/uniform_random_kernel.h" namespace phi { +namespace sr { template -void UniformRandomRawSRKernel(const Context& dev_ctx, - const ScalarArray& shape, - DataType dtype, - float min, - float max, - int seed, - int diag_num, - int diag_step, - float diag_val, - SelectedRows* out) { +void UniformRandomRawKernel(const Context& dev_ctx, + const ScalarArray& shape, + DataType dtype, + float min, + float max, + int seed, + int diag_num, + int diag_step, + float diag_val, + SelectedRows* out) { phi::UniformRandomRawKernel(dev_ctx, shape, dtype, @@ -41,23 +46,24 @@ void UniformRandomRawSRKernel(const Context& dev_ctx, } template -void UniformRandomSRKernel(const Context& dev_ctx, - const ScalarArray& shape, - DataType dtype, - float min, - float max, - int seed, - SelectedRows* out) { +void UniformRandomKernel(const Context& dev_ctx, + const ScalarArray& shape, + DataType dtype, + float min, + float max, + int seed, + SelectedRows* out) { phi::UniformRandomKernel( dev_ctx, shape, dtype, min, max, seed, out->mutable_value()); } +} // namespace sr } // namespace phi PD_REGISTER_KERNEL(uniform_random_raw_sr, CPU, ALL_LAYOUT, - phi::UniformRandomRawSRKernel, + phi::sr::UniformRandomRawKernel, float, double, phi::dtype::bfloat16) {} @@ -65,7 +71,7 @@ PD_REGISTER_KERNEL(uniform_random_raw_sr, PD_REGISTER_KERNEL(uniform_random_sr, CPU, ALL_LAYOUT, - phi::UniformRandomSRKernel, + phi::sr::UniformRandomKernel, float, double, phi::dtype::bfloat16) {} @@ -75,14 +81,14 @@ PD_REGISTER_KERNEL(uniform_random_sr, PD_REGISTER_KERNEL(uniform_random_raw_sr, GPU, ALL_LAYOUT, - phi::UniformRandomRawSRKernel, + phi::sr::UniformRandomRawKernel, float, double) {} PD_REGISTER_KERNEL(uniform_random_sr, GPU, ALL_LAYOUT, - phi::UniformRandomSRKernel, + phi::sr::UniformRandomKernel, float, double) {} #endif diff --git a/paddle/phi/kernels/selected_rows/uniform_random_kernel.h b/paddle/phi/kernels/selected_rows/uniform_random_kernel.h new file mode 100644 index 00000000000..aee7a4c7aaf --- /dev/null +++ b/paddle/phi/kernels/selected_rows/uniform_random_kernel.h @@ -0,0 +1,45 @@ +// 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. + +#pragma once + +#include "paddle/phi/common/scalar_array.h" +#include "paddle/phi/core/selected_rows.h" + +namespace phi { +namespace sr { + +template +void UniformRandomRawKernel(const Context& dev_ctx, + const ScalarArray& shape, + DataType dtype, + float min, + float max, + int seed, + int diag_num, + int diag_step, + float diag_val, + SelectedRows* out); + +template +void UniformRandomKernel(const Context& dev_ctx, + const ScalarArray& shape, + DataType dtype, + float min, + float max, + int seed, + SelectedRows* out); + +} // namespace sr +} // namespace phi diff --git a/paddle/phi/kernels/uniform_random_kernel.h b/paddle/phi/kernels/uniform_random_kernel.h index 5bba1272785..36ce4c3f9ee 100644 --- a/paddle/phi/kernels/uniform_random_kernel.h +++ b/paddle/phi/kernels/uniform_random_kernel.h @@ -17,7 +17,6 @@ #include "paddle/phi/common/scalar_array.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/device_context.h" -#include "paddle/phi/core/selected_rows.h" namespace phi { @@ -42,25 +41,4 @@ void UniformRandomKernel(const Context& dev_ctx, int seed, DenseTensor* out); -template -void UniformRandomRawSRKernel(const Context& dev_ctx, - const ScalarArray& shape, - DataType dtype, - float min, - float max, - int seed, - int diag_num, - int diag_step, - float diag_val, - SelectedRows* out); - -template -void UniformRandomSRKernel(const Context& dev_ctx, - const ScalarArray& shape, - DataType dtype, - float min, - float max, - int seed, - SelectedRows* out); - } // namespace phi -- GitLab