From a36c5490ea0ae6a5fd05b3689a78ddc1981fb896 Mon Sep 17 00:00:00 2001 From: Ryan <44900829+DrRyanHuang@users.noreply.github.com> Date: Tue, 10 Jan 2023 14:12:15 +0800 Subject: [PATCH] [PHI Decoupling] move sequence_scale from fluid to phi (#49668) * try sequence_padding * fix cant use mutable_data * fix mistake fluid_sequence_scale.hh/CMakeLists.t include * fix namespace bug * fix framework::ToAbsOffset not found * fix codestyle --- paddle/fluid/operators/math/CMakeLists.txt | 1 - paddle/phi/kernels/funcs/CMakeLists.txt | 1 + .../kernels/funcs}/sequence_scale.cc | 17 +++++++---------- .../kernels/funcs}/sequence_scale.cu | 16 +++++++--------- .../math => phi/kernels/funcs}/sequence_scale.h | 11 +++++------ .../phi/kernels/impl/warpctc_grad_kernel_impl.h | 4 ++-- paddle/phi/kernels/impl/warpctc_kernel_impl.h | 2 +- 7 files changed, 23 insertions(+), 29 deletions(-) rename paddle/{fluid/operators/math => phi/kernels/funcs}/sequence_scale.cc (81%) rename paddle/{fluid/operators/math => phi/kernels/funcs}/sequence_scale.cu (88%) rename paddle/{fluid/operators/math => phi/kernels/funcs}/sequence_scale.h (93%) diff --git a/paddle/fluid/operators/math/CMakeLists.txt b/paddle/fluid/operators/math/CMakeLists.txt index 9b1f3cb9b6..9751eec11d 100644 --- a/paddle/fluid/operators/math/CMakeLists.txt +++ b/paddle/fluid/operators/math/CMakeLists.txt @@ -30,7 +30,6 @@ math_library(sampler DEPS generator) math_library(sequence_padding) math_library(sequence_pooling DEPS math_function jit_kernel_helper) -math_library(sequence_scale) if(WITH_ASCEND_CL) math_library(beam_search DEPS math_function beam_search_npu) elseif(WITH_XPU) diff --git a/paddle/phi/kernels/funcs/CMakeLists.txt b/paddle/phi/kernels/funcs/CMakeLists.txt index 1b0e3a32f7..e4f779c807 100644 --- a/paddle/phi/kernels/funcs/CMakeLists.txt +++ b/paddle/phi/kernels/funcs/CMakeLists.txt @@ -22,6 +22,7 @@ math_library(vol2col) math_library(softmax DEPS math_function) math_library(maxouting) math_library(matrix_bit_code) +math_library(sequence_scale) cc_library( phi_data_layout_transform diff --git a/paddle/fluid/operators/math/sequence_scale.cc b/paddle/phi/kernels/funcs/sequence_scale.cc similarity index 81% rename from paddle/fluid/operators/math/sequence_scale.cc rename to paddle/phi/kernels/funcs/sequence_scale.cc index 9c17c8162b..5a2873d7e3 100644 --- a/paddle/fluid/operators/math/sequence_scale.cc +++ b/paddle/phi/kernels/funcs/sequence_scale.cc @@ -12,17 +12,15 @@ 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/math/sequence_scale.h" - +#include "paddle/phi/kernels/funcs/sequence_scale.h" #include "paddle/phi/backends/cpu/cpu_context.h" namespace phi { class DenseTensor; } // namespace phi -namespace paddle { -namespace operators { -namespace math { +namespace phi { +namespace funcs { template class ScaleLoDTensorFunctor { @@ -34,9 +32,9 @@ class ScaleLoDTensorFunctor { auto lod = seq->lod(); const size_t num_seq = lod[level].size() - 1; size_t seq_width = seq->dims()[1]; - framework::LoD abs_offset_lod = framework::ToAbsOffset(lod); + paddle::framework::LoD abs_offset_lod = paddle::framework::ToAbsOffset(lod); - T* seq_data = seq->mutable_data(context.GetPlace()); + T* seq_data = context.template Alloc(seq); for (size_t i = 0; i < num_seq; ++i) { for (size_t j = lod[level][i] * seq_width; j < lod[level][i + 1] * seq_width; @@ -50,6 +48,5 @@ class ScaleLoDTensorFunctor { template class ScaleLoDTensorFunctor; template class ScaleLoDTensorFunctor; -} // namespace math -} // namespace operators -} // namespace paddle +} // namespace funcs +} // namespace phi diff --git a/paddle/fluid/operators/math/sequence_scale.cu b/paddle/phi/kernels/funcs/sequence_scale.cu similarity index 88% rename from paddle/fluid/operators/math/sequence_scale.cu rename to paddle/phi/kernels/funcs/sequence_scale.cu index 21010ca331..06d8db04be 100644 --- a/paddle/fluid/operators/math/sequence_scale.cu +++ b/paddle/phi/kernels/funcs/sequence_scale.cu @@ -12,13 +12,12 @@ 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/math/sequence_scale.h" +#include "paddle/phi/kernels/funcs/sequence_scale.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/backends/gpu/gpu_primitives.h" -namespace paddle { -namespace operators { -namespace math { +namespace phi { +namespace funcs { using phi::PADDLE_CUDA_NUM_THREADS; @@ -45,8 +44,8 @@ class ScaleLoDTensorFunctor { auto lod = seq->lod(); const size_t num_seq = lod[level].size() - 1; const size_t seq_width = seq->numel() / seq->dims()[0]; - auto abs_offset_lod = framework::ToAbsOffset(lod); - T* seq_data = seq->mutable_data(context.GetPlace()); + auto abs_offset_lod = paddle::framework::ToAbsOffset(lod); + T* seq_data = context.template Alloc(seq); paddle::framework::MixVector mix_vector(&(abs_offset_lod[level])); #ifdef PADDLE_WITH_HIP @@ -75,6 +74,5 @@ class ScaleLoDTensorFunctor { template class ScaleLoDTensorFunctor; template class ScaleLoDTensorFunctor; -} // namespace math -} // namespace operators -} // namespace paddle +} // namespace funcs +} // namespace phi diff --git a/paddle/fluid/operators/math/sequence_scale.h b/paddle/phi/kernels/funcs/sequence_scale.h similarity index 93% rename from paddle/fluid/operators/math/sequence_scale.h rename to paddle/phi/kernels/funcs/sequence_scale.h index 7d7becbd51..02a91d53e5 100644 --- a/paddle/fluid/operators/math/sequence_scale.h +++ b/paddle/phi/kernels/funcs/sequence_scale.h @@ -17,9 +17,8 @@ limitations under the License. */ #include "paddle/fluid/framework/lod_tensor.h" #include "paddle/fluid/platform/device_context.h" -namespace paddle { -namespace operators { -namespace math { +namespace phi { +namespace funcs { /* * \brief Scale a sequence. @@ -45,6 +44,7 @@ namespace math { * \param num_seq Number of sequence * */ + template class ScaleLoDTensorFunctor { public: @@ -53,6 +53,5 @@ class ScaleLoDTensorFunctor { phi::DenseTensor* seq); }; -} // namespace math -} // namespace operators -} // namespace paddle +} // namespace funcs +} // namespace phi diff --git a/paddle/phi/kernels/impl/warpctc_grad_kernel_impl.h b/paddle/phi/kernels/impl/warpctc_grad_kernel_impl.h index 0c20fd1c2f..6f122ad1cc 100644 --- a/paddle/phi/kernels/impl/warpctc_grad_kernel_impl.h +++ b/paddle/phi/kernels/impl/warpctc_grad_kernel_impl.h @@ -17,12 +17,12 @@ #include #include "paddle/fluid/operators/math/sequence_padding.h" -#include "paddle/fluid/operators/math/sequence_scale.h" #include "paddle/phi/backends/dynload/warpctc.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/kernels/empty_kernel.h" #include "paddle/phi/kernels/funcs/eigen/common.h" #include "paddle/phi/kernels/funcs/math_function.h" +#include "paddle/phi/kernels/funcs/sequence_scale.h" #include "paddle/utils/optional.h" namespace phi { @@ -79,7 +79,7 @@ void WarpctcGradKernel(const Context& dev_ctx, paddle::operators::math::kLengthBatchWidth); const T* loss_grad_data = loss_grad.data(); - paddle::operators::math::ScaleLoDTensorFunctor()( + phi::funcs::ScaleLoDTensorFunctor()( dev_ctx, loss_grad_data, logits_grad); } } diff --git a/paddle/phi/kernels/impl/warpctc_kernel_impl.h b/paddle/phi/kernels/impl/warpctc_kernel_impl.h index efa9bd1ae0..d2fc934c71 100644 --- a/paddle/phi/kernels/impl/warpctc_kernel_impl.h +++ b/paddle/phi/kernels/impl/warpctc_kernel_impl.h @@ -17,12 +17,12 @@ #include #include "paddle/fluid/operators/math/sequence_padding.h" -#include "paddle/fluid/operators/math/sequence_scale.h" #include "paddle/phi/backends/dynload/warpctc.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/empty_kernel.h" #include "paddle/phi/kernels/funcs/math_function.h" +#include "paddle/phi/kernels/funcs/sequence_scale.h" #include "paddle/utils/optional.h" namespace phi { -- GitLab