From 4dada844a57b567e095c68ebedf1172a7c691b8f Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Mon, 9 Jan 2023 20:44:54 +0800 Subject: [PATCH 1/6] try sequence_padding --- paddle/fluid/operators/math/CMakeLists.txt | 1 - paddle/phi/kernels/funcs/CMakeLists.txt | 1 + .../operators/math => phi/kernels/funcs}/sequence_scale.cc | 2 +- .../operators/math => phi/kernels/funcs}/sequence_scale.cu | 2 +- .../operators/math => phi/kernels/funcs}/sequence_scale.h | 0 paddle/phi/kernels/impl/warpctc_grad_kernel_impl.h | 2 +- paddle/phi/kernels/impl/warpctc_kernel_impl.h | 2 +- 7 files changed, 5 insertions(+), 5 deletions(-) rename paddle/{fluid/operators/math => phi/kernels/funcs}/sequence_scale.cc (96%) rename paddle/{fluid/operators/math => phi/kernels/funcs}/sequence_scale.cu (97%) rename paddle/{fluid/operators/math => phi/kernels/funcs}/sequence_scale.h (100%) diff --git a/paddle/fluid/operators/math/CMakeLists.txt b/paddle/fluid/operators/math/CMakeLists.txt index 9b1f3cb9b68f8..9751eec11d8f0 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 1b0e3a32f7e2f..e4f779c807570 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 96% rename from paddle/fluid/operators/math/sequence_scale.cc rename to paddle/phi/kernels/funcs/sequence_scale.cc index 9c17c8162b7f1..7db3d1b421703 100644 --- a/paddle/fluid/operators/math/sequence_scale.cc +++ b/paddle/phi/kernels/funcs/sequence_scale.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/math/sequence_scale.h" +#include "paddle/phi/kernels/funcs/sequence_scale.h" #include "paddle/phi/backends/cpu/cpu_context.h" diff --git a/paddle/fluid/operators/math/sequence_scale.cu b/paddle/phi/kernels/funcs/sequence_scale.cu similarity index 97% rename from paddle/fluid/operators/math/sequence_scale.cu rename to paddle/phi/kernels/funcs/sequence_scale.cu index 21010ca33148e..82cd4ee6ac153 100644 --- a/paddle/fluid/operators/math/sequence_scale.cu +++ b/paddle/phi/kernels/funcs/sequence_scale.cu @@ -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/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" diff --git a/paddle/fluid/operators/math/sequence_scale.h b/paddle/phi/kernels/funcs/sequence_scale.h similarity index 100% rename from paddle/fluid/operators/math/sequence_scale.h rename to paddle/phi/kernels/funcs/sequence_scale.h diff --git a/paddle/phi/kernels/impl/warpctc_grad_kernel_impl.h b/paddle/phi/kernels/impl/warpctc_grad_kernel_impl.h index 0c20fd1c2fe7e..b67b5743563cd 100644 --- a/paddle/phi/kernels/impl/warpctc_grad_kernel_impl.h +++ b/paddle/phi/kernels/impl/warpctc_grad_kernel_impl.h @@ -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()( + paddle::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 efa9bd1ae064d..8a92d223ec345 100644 --- a/paddle/phi/kernels/impl/warpctc_kernel_impl.h +++ b/paddle/phi/kernels/impl/warpctc_kernel_impl.h @@ -17,7 +17,7 @@ #include #include "paddle/fluid/operators/math/sequence_padding.h" -#include "paddle/fluid/operators/math/sequence_scale.h" +#include "paddle/phi/kernels/funcs/sequence_scale.h" #include "paddle/phi/backends/dynload/warpctc.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/tensor_utils.h" From c85af4894a9e55f1277603ca988fc31d1ef6bf57 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Mon, 9 Jan 2023 21:00:06 +0800 Subject: [PATCH 2/6] fix cant use mutable_data --- paddle/phi/kernels/funcs/sequence_scale.cc | 2 +- paddle/phi/kernels/funcs/sequence_scale.cu | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/paddle/phi/kernels/funcs/sequence_scale.cc b/paddle/phi/kernels/funcs/sequence_scale.cc index 7db3d1b421703..71fc1b2ce05c2 100644 --- a/paddle/phi/kernels/funcs/sequence_scale.cc +++ b/paddle/phi/kernels/funcs/sequence_scale.cc @@ -36,7 +36,7 @@ class ScaleLoDTensorFunctor { size_t seq_width = seq->dims()[1]; framework::LoD abs_offset_lod = 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; diff --git a/paddle/phi/kernels/funcs/sequence_scale.cu b/paddle/phi/kernels/funcs/sequence_scale.cu index 82cd4ee6ac153..2742fdd2f6d92 100644 --- a/paddle/phi/kernels/funcs/sequence_scale.cu +++ b/paddle/phi/kernels/funcs/sequence_scale.cu @@ -46,7 +46,7 @@ class ScaleLoDTensorFunctor { 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()); + T* seq_data = context.template Alloc(seq); paddle::framework::MixVector mix_vector(&(abs_offset_lod[level])); #ifdef PADDLE_WITH_HIP From 9fc297828d1d09cb0a25d197245fb113d98666a7 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Mon, 9 Jan 2023 22:00:11 +0800 Subject: [PATCH 3/6] fix mistake fluid_sequence_scale.hh/CMakeLists.t include --- paddle/phi/kernels/funcs/sequence_scale.cc | 1 - paddle/phi/kernels/impl/warpctc_grad_kernel_impl.h | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/paddle/phi/kernels/funcs/sequence_scale.cc b/paddle/phi/kernels/funcs/sequence_scale.cc index 71fc1b2ce05c2..a0fd9fbaa6c37 100644 --- a/paddle/phi/kernels/funcs/sequence_scale.cc +++ b/paddle/phi/kernels/funcs/sequence_scale.cc @@ -13,7 +13,6 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/phi/kernels/funcs/sequence_scale.h" - #include "paddle/phi/backends/cpu/cpu_context.h" 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 b67b5743563cd..347184d203e07 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 { From 28c94b9f2a4a28941306e8e72eaf60c8a82a4613 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Mon, 9 Jan 2023 22:10:08 +0800 Subject: [PATCH 4/6] fix namespace bug --- paddle/phi/kernels/funcs/sequence_scale.cc | 10 ++++------ paddle/phi/kernels/funcs/sequence_scale.cu | 10 ++++------ paddle/phi/kernels/funcs/sequence_scale.h | 10 ++++------ paddle/phi/kernels/impl/warpctc_grad_kernel_impl.h | 2 +- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/paddle/phi/kernels/funcs/sequence_scale.cc b/paddle/phi/kernels/funcs/sequence_scale.cc index a0fd9fbaa6c37..a13c820eb1091 100644 --- a/paddle/phi/kernels/funcs/sequence_scale.cc +++ b/paddle/phi/kernels/funcs/sequence_scale.cc @@ -19,9 +19,8 @@ namespace phi { class DenseTensor; } // namespace phi -namespace paddle { -namespace operators { -namespace math { +namespace phi { +namespace funcs { template class ScaleLoDTensorFunctor { @@ -49,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/phi/kernels/funcs/sequence_scale.cu b/paddle/phi/kernels/funcs/sequence_scale.cu index 2742fdd2f6d92..e8e7cdffe5444 100644 --- a/paddle/phi/kernels/funcs/sequence_scale.cu +++ b/paddle/phi/kernels/funcs/sequence_scale.cu @@ -16,9 +16,8 @@ limitations under the License. */ #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; @@ -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/phi/kernels/funcs/sequence_scale.h b/paddle/phi/kernels/funcs/sequence_scale.h index 7d7becbd51a96..5a11e8f6f4fec 100644 --- a/paddle/phi/kernels/funcs/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. @@ -53,6 +52,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 347184d203e07..6f122ad1cc04d 100644 --- a/paddle/phi/kernels/impl/warpctc_grad_kernel_impl.h +++ b/paddle/phi/kernels/impl/warpctc_grad_kernel_impl.h @@ -79,7 +79,7 @@ void WarpctcGradKernel(const Context& dev_ctx, paddle::operators::math::kLengthBatchWidth); const T* loss_grad_data = loss_grad.data(); - paddle::phi::funcs::ScaleLoDTensorFunctor()( + phi::funcs::ScaleLoDTensorFunctor()( dev_ctx, loss_grad_data, logits_grad); } } From e03ace0924c62317f7955bc7bac08a9bfd8b6fc6 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Mon, 9 Jan 2023 22:27:56 +0800 Subject: [PATCH 5/6] fix framework::ToAbsOffset not found --- paddle/phi/kernels/funcs/sequence_scale.cc | 2 +- paddle/phi/kernels/funcs/sequence_scale.cu | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/paddle/phi/kernels/funcs/sequence_scale.cc b/paddle/phi/kernels/funcs/sequence_scale.cc index a13c820eb1091..5a2873d7e326c 100644 --- a/paddle/phi/kernels/funcs/sequence_scale.cc +++ b/paddle/phi/kernels/funcs/sequence_scale.cc @@ -32,7 +32,7 @@ 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 = context.template Alloc(seq); for (size_t i = 0; i < num_seq; ++i) { diff --git a/paddle/phi/kernels/funcs/sequence_scale.cu b/paddle/phi/kernels/funcs/sequence_scale.cu index e8e7cdffe5444..06d8db04be6d4 100644 --- a/paddle/phi/kernels/funcs/sequence_scale.cu +++ b/paddle/phi/kernels/funcs/sequence_scale.cu @@ -44,7 +44,7 @@ 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); + auto abs_offset_lod = paddle::framework::ToAbsOffset(lod); T* seq_data = context.template Alloc(seq); paddle::framework::MixVector mix_vector(&(abs_offset_lod[level])); From ac2970d5da68a50cf5867bf36eb9e1d781828c02 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Mon, 9 Jan 2023 22:32:09 +0800 Subject: [PATCH 6/6] fix codestyle --- paddle/phi/kernels/funcs/sequence_scale.h | 1 + paddle/phi/kernels/impl/warpctc_kernel_impl.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/paddle/phi/kernels/funcs/sequence_scale.h b/paddle/phi/kernels/funcs/sequence_scale.h index 5a11e8f6f4fec..02a91d53e5827 100644 --- a/paddle/phi/kernels/funcs/sequence_scale.h +++ b/paddle/phi/kernels/funcs/sequence_scale.h @@ -44,6 +44,7 @@ namespace funcs { * \param num_seq Number of sequence * */ + template class ScaleLoDTensorFunctor { public: diff --git a/paddle/phi/kernels/impl/warpctc_kernel_impl.h b/paddle/phi/kernels/impl/warpctc_kernel_impl.h index 8a92d223ec345..d2fc934c71b52 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/phi/kernels/funcs/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 {