未验证 提交 5f443601 编写于 作者: H Huang Jiyi 提交者: GitHub

[phi decoupling] move sequence_padding from fluid to phi (#50639)

* move sequence_padding to phi

* fix bugs

* fix bugs

* fix bugs

* fix bugs

* fix bugs

* fix bugs

* fix bugs

* fix bugs

* fix buga

* fix bugs

* revert and update phi::XPUContext
上级 d79d5933
...@@ -28,7 +28,6 @@ math_library(sampler DEPS generator) ...@@ -28,7 +28,6 @@ math_library(sampler DEPS generator)
# math_library(math_function DEPS blas dense_tensor tensor) # math_library(math_function DEPS blas dense_tensor tensor)
math_library(sequence_padding DEPS lod_tensor)
math_library(sequence_pooling DEPS math_function jit_kernel_helper) math_library(sequence_pooling DEPS math_function jit_kernel_helper)
if(WITH_ASCEND_CL) if(WITH_ASCEND_CL)
math_library(beam_search DEPS math_function beam_search_npu) math_library(beam_search DEPS math_function beam_search_npu)
...@@ -55,10 +54,6 @@ cc_test( ...@@ -55,10 +54,6 @@ cc_test(
vol2col_test vol2col_test
SRCS vol2col_test.cc SRCS vol2col_test.cc
DEPS vol2col) DEPS vol2col)
cc_test(
sequence_padding_test
SRCS sequence_padding_test.cc
DEPS sequence_padding)
cc_test( cc_test(
sequence_pooling_test sequence_pooling_test
SRCS sequence_pooling_test.cc SRCS sequence_pooling_test.cc
......
...@@ -94,7 +94,7 @@ class SequencePadOp : public framework::OperatorWithKernel { ...@@ -94,7 +94,7 @@ class SequencePadOp : public framework::OperatorWithKernel {
static_cast<int64_t>(x_lod_0.back()))); static_cast<int64_t>(x_lod_0.back())));
int seq_num = x_lod_0.size() - 1; int seq_num = x_lod_0.size() - 1;
int max_seq_len = math::MaximumSequenceLength(x_lod_0); int max_seq_len = phi::funcs::MaximumSequenceLength(x_lod_0);
if (padded_length == -1) { if (padded_length == -1) {
padded_length = max_seq_len; padded_length = max_seq_len;
} }
......
...@@ -18,8 +18,8 @@ limitations under the License. */ ...@@ -18,8 +18,8 @@ limitations under the License. */
#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/memory/memcpy.h" #include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/operators/math/sequence_padding.h"
#include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/funcs/sequence_padding.h"
namespace paddle { namespace paddle {
namespace operators { namespace operators {
...@@ -45,7 +45,7 @@ class SequencePadOpKernel : public framework::OpKernel<T> { ...@@ -45,7 +45,7 @@ class SequencePadOpKernel : public framework::OpKernel<T> {
int padded_length = ctx.Attr<int>("padded_length"); int padded_length = ctx.Attr<int>("padded_length");
math::PaddingLoDTensorFunctor<DeviceContext, T>()( phi::funcs::PaddingLoDTensorFunctor<DeviceContext, T>()(
ctx.template device_context<DeviceContext>(), ctx.template device_context<DeviceContext>(),
*x, *x,
out, out,
...@@ -53,7 +53,7 @@ class SequencePadOpKernel : public framework::OpKernel<T> { ...@@ -53,7 +53,7 @@ class SequencePadOpKernel : public framework::OpKernel<T> {
padded_length, padded_length,
0, 0,
false, false,
math::kBatchLengthWidth); phi::funcs::kBatchLengthWidth);
phi::DenseTensor seq_len; phi::DenseTensor seq_len;
seq_len.Resize(len_t->dims()); seq_len.Resize(len_t->dims());
...@@ -80,14 +80,14 @@ class SequencePadGradOpKernel : public framework::OpKernel<T> { ...@@ -80,14 +80,14 @@ class SequencePadGradOpKernel : public framework::OpKernel<T> {
int padded_length = ctx.Attr<int>("padded_length"); int padded_length = ctx.Attr<int>("padded_length");
math::UnpaddingLoDTensorFunctor<DeviceContext, T>()( phi::funcs::UnpaddingLoDTensorFunctor<DeviceContext, T>()(
ctx.template device_context<DeviceContext>(), ctx.template device_context<DeviceContext>(),
*d_out, *d_out,
d_x, d_x,
padded_length, padded_length,
0, 0,
false, false,
math::kBatchLengthWidth); phi::funcs::kBatchLengthWidth);
} }
} }
}; };
......
...@@ -18,8 +18,8 @@ limitations under the License. */ ...@@ -18,8 +18,8 @@ limitations under the License. */
#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/memory/memcpy.h" #include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/operators/math/sequence_padding.h"
#include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/funcs/sequence_padding.h"
namespace paddle { namespace paddle {
namespace operators { namespace operators {
...@@ -70,8 +70,14 @@ class SequenceUnpadOpKernel : public framework::OpKernel<T> { ...@@ -70,8 +70,14 @@ class SequenceUnpadOpKernel : public framework::OpKernel<T> {
out_t->mutable_data<T>(ctx.GetPlace()); out_t->mutable_data<T>(ctx.GetPlace());
int64_t padded_length = x_t->dims()[1]; int64_t padded_length = x_t->dims()[1];
math::UnpaddingLoDTensorFunctor<DeviceContext, T>()( phi::funcs::UnpaddingLoDTensorFunctor<DeviceContext, T>()(
dev_ctx, *x_t, out_t, padded_length, 0, false, math::kBatchLengthWidth); dev_ctx,
*x_t,
out_t,
padded_length,
0,
false,
phi::funcs::kBatchLengthWidth);
} }
}; };
...@@ -93,7 +99,7 @@ class SequenceUnpadGradOpKernel : public framework::OpKernel<T> { ...@@ -93,7 +99,7 @@ class SequenceUnpadGradOpKernel : public framework::OpKernel<T> {
auto& dev_ctx = ctx.template device_context<DeviceContext>(); auto& dev_ctx = ctx.template device_context<DeviceContext>();
set_zero(dev_ctx, &zero_pads, static_cast<T>(0)); set_zero(dev_ctx, &zero_pads, static_cast<T>(0));
math::PaddingLoDTensorFunctor<DeviceContext, T>()( phi::funcs::PaddingLoDTensorFunctor<DeviceContext, T>()(
ctx.template device_context<DeviceContext>(), ctx.template device_context<DeviceContext>(),
*d_out, *d_out,
d_x, d_x,
...@@ -101,7 +107,7 @@ class SequenceUnpadGradOpKernel : public framework::OpKernel<T> { ...@@ -101,7 +107,7 @@ class SequenceUnpadGradOpKernel : public framework::OpKernel<T> {
padded_length, padded_length,
0, 0,
false, false,
math::kBatchLengthWidth); phi::funcs::kBatchLengthWidth);
} }
} }
}; };
......
...@@ -17,8 +17,7 @@ limitations under the License. */ ...@@ -17,8 +17,7 @@ limitations under the License. */
#include "paddle/fluid/operators/sequence_ops/sequence_unpad_op.h" #include "paddle/fluid/operators/sequence_ops/sequence_unpad_op.h"
namespace ops = paddle::operators; namespace ops = paddle::operators;
REGISTER_OP_XPU_KERNEL( REGISTER_OP_XPU_KERNEL(sequence_unpad,
sequence_unpad, ops::SequenceUnpadOpKernel<phi::XPUContext, float>);
ops::SequenceUnpadOpKernel<paddle::platform::XPUDeviceContext, float>);
#endif #endif
...@@ -23,6 +23,7 @@ math_library(softmax DEPS math_function) ...@@ -23,6 +23,7 @@ math_library(softmax DEPS math_function)
math_library(maxouting) math_library(maxouting)
math_library(matrix_bit_code) math_library(matrix_bit_code)
math_library(sequence_scale) math_library(sequence_scale)
math_library(sequence_padding DEPS lod_utils)
cc_library( cc_library(
phi_data_layout_transform phi_data_layout_transform
......
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. /* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
...@@ -12,18 +12,16 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ...@@ -12,18 +12,16 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "paddle/fluid/operators/math/sequence_padding.h" #include "paddle/phi/kernels/funcs/sequence_padding.h"
#include "paddle/fluid/platform/device/device_wrapper.h"
#include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/backends/cpu/cpu_context.h"
namespace phi { #ifdef PADDLE_WITH_XPU
class DenseTensor; #include "paddle/phi/backends/xpu/enforce_xpu.h"
} // namespace phi #endif
namespace paddle { namespace phi {
namespace operators { namespace funcs {
namespace math {
template <typename T> template <typename T>
void CopyValidData(phi::DenseTensor* dst_tensor, void CopyValidData(phi::DenseTensor* dst_tensor,
...@@ -46,7 +44,7 @@ void CopyValidData(phi::DenseTensor* dst_tensor, ...@@ -46,7 +44,7 @@ void CopyValidData(phi::DenseTensor* dst_tensor,
PADDLE_ENFORCE_GE( PADDLE_ENFORCE_GE(
pad_seq_len, pad_seq_len,
valid_seq_len, valid_seq_len,
platform::errors::InvalidArgument( phi::errors::InvalidArgument(
"The padded sequence length can not " "The padded sequence length can not "
"be less than its original length. Expected %ld >= %ld, but got " "be less than its original length. Expected %ld >= %ld, but got "
"%ld < %ld. Please check input value.", "%ld < %ld. Please check input value.",
...@@ -107,7 +105,7 @@ class PaddingLoDTensorFunctor<phi::CPUContext, T> { ...@@ -107,7 +105,7 @@ class PaddingLoDTensorFunctor<phi::CPUContext, T> {
bool norm_by_times = false, bool norm_by_times = false,
const PadLayout layout = kBatchLengthWidth) { const PadLayout layout = kBatchLengthWidth) {
auto seq_lod = seq_tensor.lod(); auto seq_lod = seq_tensor.lod();
const auto seq_offsets = framework::ToAbsOffset(seq_lod)[lod_level]; const auto seq_offsets = phi::ToAbsOffset(seq_lod)[lod_level];
const auto& seq_tensor_dims = seq_tensor.dims(); const auto& seq_tensor_dims = seq_tensor.dims();
const auto& pad_tensor_dims = pad_tensor->dims(); const auto& pad_tensor_dims = pad_tensor->dims();
if (pad_seq_len == -1) { if (pad_seq_len == -1) {
...@@ -125,7 +123,7 @@ class PaddingLoDTensorFunctor<phi::CPUContext, T> { ...@@ -125,7 +123,7 @@ class PaddingLoDTensorFunctor<phi::CPUContext, T> {
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(
pad_value.numel() == 1 || pad_value.numel() == step_width, pad_value.numel() == 1 || pad_value.numel() == step_width,
true, true,
platform::errors::InvalidArgument( phi::errors::InvalidArgument(
"The numel of 'pad_value' can only be 1 or be equal to the " "The numel of 'pad_value' can only be 1 or be equal to the "
"'step_width', but got %ld != 1 and %ld. Please check the input " "'step_width', but got %ld != 1 and %ld. Please check the input "
"value.", "value.",
...@@ -165,7 +163,7 @@ class UnpaddingLoDTensorFunctor<phi::CPUContext, T> { ...@@ -165,7 +163,7 @@ class UnpaddingLoDTensorFunctor<phi::CPUContext, T> {
int lod_level = 0, int lod_level = 0,
bool norm_by_times = false, bool norm_by_times = false,
const PadLayout layout = kBatchLengthWidth) { const PadLayout layout = kBatchLengthWidth) {
auto seq_offsets = framework::ToAbsOffset(seq_tensor->lod())[lod_level]; auto seq_offsets = phi::ToAbsOffset(seq_tensor->lod())[lod_level];
const auto& seq_tensor_dims = seq_tensor->dims(); const auto& seq_tensor_dims = seq_tensor->dims();
const auto& pad_tensor_dims = pad_tensor.dims(); const auto& pad_tensor_dims = pad_tensor.dims();
if (pad_seq_len == -1) { if (pad_seq_len == -1) {
...@@ -193,16 +191,16 @@ class UnpaddingLoDTensorFunctor<phi::CPUContext, T> { ...@@ -193,16 +191,16 @@ class UnpaddingLoDTensorFunctor<phi::CPUContext, T> {
#ifdef PADDLE_WITH_XPU #ifdef PADDLE_WITH_XPU
template <typename T> template <typename T>
class UnpaddingLoDTensorFunctor<platform::XPUDeviceContext, T> { class UnpaddingLoDTensorFunctor<phi::XPUContext, T> {
public: public:
void operator()(const platform::XPUDeviceContext& context, void operator()(const phi::XPUContext& context,
const phi::DenseTensor& pad_tensor, const phi::DenseTensor& pad_tensor,
phi::DenseTensor* seq_tensor, phi::DenseTensor* seq_tensor,
int pad_seq_len = -1, int pad_seq_len = -1,
int lod_level = 0, int lod_level = 0,
bool norm_by_times = false, bool norm_by_times = false,
const PadLayout layout = kBatchLengthWidth) { const PadLayout layout = kBatchLengthWidth) {
auto seq_offsets = framework::ToAbsOffset(seq_tensor->lod())[lod_level]; auto seq_offsets = phi::ToAbsOffset(seq_tensor->lod())[lod_level];
const auto& seq_tensor_dims = seq_tensor->dims(); const auto& seq_tensor_dims = seq_tensor->dims();
const auto& pad_tensor_dims = pad_tensor.dims(); const auto& pad_tensor_dims = pad_tensor.dims();
if (pad_seq_len == -1) { if (pad_seq_len == -1) {
...@@ -246,9 +244,8 @@ template class UnpaddingLoDTensorFunctor<phi::CPUContext, float>; ...@@ -246,9 +244,8 @@ template class UnpaddingLoDTensorFunctor<phi::CPUContext, float>;
template class UnpaddingLoDTensorFunctor<phi::CPUContext, double>; template class UnpaddingLoDTensorFunctor<phi::CPUContext, double>;
#ifdef PADDLE_WITH_XPU #ifdef PADDLE_WITH_XPU
template class UnpaddingLoDTensorFunctor<platform::XPUDeviceContext, float>; template class UnpaddingLoDTensorFunctor<phi::XPUContext, float>;
#endif #endif
} // namespace math } // namespace funcs
} // namespace operators } // namespace phi
} // namespace paddle
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. /* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
...@@ -14,12 +14,11 @@ limitations under the License. */ ...@@ -14,12 +14,11 @@ limitations under the License. */
#include <algorithm> #include <algorithm>
#include "paddle/fluid/operators/math/sequence_padding.h"
#include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/kernels/funcs/sequence_padding.h"
namespace paddle { namespace phi {
namespace operators { namespace funcs {
namespace math {
template <typename T, CopyType Type> template <typename T, CopyType Type>
__global__ void SequencePaddingKernel(T* dst, __global__ void SequencePaddingKernel(T* dst,
...@@ -69,7 +68,7 @@ class PaddingLoDTensorFunctor<phi::GPUContext, T> { ...@@ -69,7 +68,7 @@ class PaddingLoDTensorFunctor<phi::GPUContext, T> {
bool norm_by_times = false, bool norm_by_times = false,
const PadLayout layout = kBatchLengthWidth) { const PadLayout layout = kBatchLengthWidth) {
auto seq_lod = seq_tensor.lod(); auto seq_lod = seq_tensor.lod();
auto seq_offsets = framework::ToAbsOffset(seq_lod)[lod_level]; auto seq_offsets = phi::ToAbsOffset(seq_lod)[lod_level];
const auto& seq_tensor_dims = seq_tensor.dims(); const auto& seq_tensor_dims = seq_tensor.dims();
const auto& pad_tensor_dims = pad_tensor->dims(); const auto& pad_tensor_dims = pad_tensor->dims();
int max_seq_len = MaximumSequenceLength(seq_offsets); int max_seq_len = MaximumSequenceLength(seq_offsets);
...@@ -79,7 +78,7 @@ class PaddingLoDTensorFunctor<phi::GPUContext, T> { ...@@ -79,7 +78,7 @@ class PaddingLoDTensorFunctor<phi::GPUContext, T> {
PADDLE_ENFORCE_GE( PADDLE_ENFORCE_GE(
pad_seq_len, pad_seq_len,
max_seq_len, max_seq_len,
platform::errors::InvalidArgument( phi::errors::InvalidArgument(
"The pad_seq_len must be equal to or greater than the " "The pad_seq_len must be equal to or greater than the "
"original max sequence length. Expected %ld >= %ld, but got %ld < " "original max sequence length. Expected %ld >= %ld, but got %ld < "
"%ld. Please check the input value.", "%ld. Please check the input value.",
...@@ -99,7 +98,7 @@ class PaddingLoDTensorFunctor<phi::GPUContext, T> { ...@@ -99,7 +98,7 @@ class PaddingLoDTensorFunctor<phi::GPUContext, T> {
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(
pad_value.numel() == 1 || pad_value.numel() == step_width, pad_value.numel() == 1 || pad_value.numel() == step_width,
true, true,
platform::errors::InvalidArgument( phi::errors::InvalidArgument(
"The numel of 'pad_value' can only be 1 or be equal to " "The numel of 'pad_value' can only be 1 or be equal to "
"the 'step_width', but got %ld != 1 and %ld. Please check the " "the 'step_width', but got %ld != 1 and %ld. Please check the "
"input value.", "input value.",
...@@ -149,7 +148,7 @@ class UnpaddingLoDTensorFunctor<phi::GPUContext, T> { ...@@ -149,7 +148,7 @@ class UnpaddingLoDTensorFunctor<phi::GPUContext, T> {
int lod_level = 0, int lod_level = 0,
bool norm_by_times = false, bool norm_by_times = false,
const PadLayout layout = kBatchLengthWidth) { const PadLayout layout = kBatchLengthWidth) {
auto seq_offsets = framework::ToAbsOffset(seq_tensor->lod())[lod_level]; auto seq_offsets = phi::ToAbsOffset(seq_tensor->lod())[lod_level];
const auto& seq_tensor_dims = seq_tensor->dims(); const auto& seq_tensor_dims = seq_tensor->dims();
const auto& pad_tensor_dims = pad_tensor.dims(); const auto& pad_tensor_dims = pad_tensor.dims();
int max_seq_len = MaximumSequenceLength(seq_offsets); int max_seq_len = MaximumSequenceLength(seq_offsets);
...@@ -216,6 +215,5 @@ template class UnpaddingLoDTensorFunctor<phi::GPUContext, int64_t>; ...@@ -216,6 +215,5 @@ template class UnpaddingLoDTensorFunctor<phi::GPUContext, int64_t>;
template class UnpaddingLoDTensorFunctor<phi::GPUContext, float>; template class UnpaddingLoDTensorFunctor<phi::GPUContext, float>;
template class UnpaddingLoDTensorFunctor<phi::GPUContext, double>; template class UnpaddingLoDTensorFunctor<phi::GPUContext, double>;
} // namespace math } // namespace funcs
} // namespace operators } // namespace phi
} // namespace paddle
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. /* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
...@@ -17,12 +17,13 @@ limitations under the License. */ ...@@ -17,12 +17,13 @@ limitations under the License. */
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/platform/device_context.h" #include "paddle/fluid/platform/device_context.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/lod_utils.h"
#include "paddle/phi/core/mixed_vector.h"
namespace paddle { namespace phi {
namespace operators { namespace funcs {
namespace math {
enum PadLayout { kBatchLengthWidth = 0, kLengthBatchWidth }; enum PadLayout { kBatchLengthWidth = 0, kLengthBatchWidth };
...@@ -130,6 +131,5 @@ class UnpaddingLoDTensorFunctor { ...@@ -130,6 +131,5 @@ class UnpaddingLoDTensorFunctor {
const PadLayout layout = kBatchLengthWidth); const PadLayout layout = kBatchLengthWidth);
}; };
} // namespace math } // namespace funcs
} // namespace operators } // namespace phi
} // namespace paddle
...@@ -16,12 +16,12 @@ ...@@ -16,12 +16,12 @@
#include <vector> #include <vector>
#include "paddle/fluid/operators/math/sequence_padding.h"
#include "paddle/phi/backends/dynload/warpctc.h" #include "paddle/phi/backends/dynload/warpctc.h"
#include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/kernels/empty_kernel.h" #include "paddle/phi/kernels/empty_kernel.h"
#include "paddle/phi/kernels/funcs/eigen/common.h" #include "paddle/phi/kernels/funcs/eigen/common.h"
#include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/funcs/sequence_padding.h"
#include "paddle/phi/kernels/funcs/sequence_scale.h" #include "paddle/phi/kernels/funcs/sequence_scale.h"
#include "paddle/utils/optional.h" #include "paddle/utils/optional.h"
...@@ -69,14 +69,14 @@ void WarpctcGradKernel(const Context& dev_ctx, ...@@ -69,14 +69,14 @@ void WarpctcGradKernel(const Context& dev_ctx,
logits_grad_e.device(*place) = logits_g; logits_grad_e.device(*place) = logits_g;
} }
} else { } else {
paddle::operators::math::UnpaddingLoDTensorFunctor<Context, T>()( phi::funcs::UnpaddingLoDTensorFunctor<Context, T>()(
dev_ctx, dev_ctx,
warpctcgrad, warpctcgrad,
logits_grad, logits_grad,
-1, -1,
0, 0,
norm_by_times, norm_by_times,
paddle::operators::math::kLengthBatchWidth); phi::funcs::kLengthBatchWidth);
const T* loss_grad_data = loss_grad.data<T>(); const T* loss_grad_data = loss_grad.data<T>();
phi::funcs::ScaleLoDTensorFunctor<Context, T>()( phi::funcs::ScaleLoDTensorFunctor<Context, T>()(
......
...@@ -16,13 +16,13 @@ ...@@ -16,13 +16,13 @@
#include <vector> #include <vector>
#include "paddle/fluid/operators/math/sequence_padding.h"
#include "paddle/phi/backends/dynload/warpctc.h" #include "paddle/phi/backends/dynload/warpctc.h"
#include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/lod_utils.h" #include "paddle/phi/core/lod_utils.h"
#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/core/tensor_utils.h"
#include "paddle/phi/kernels/empty_kernel.h" #include "paddle/phi/kernels/empty_kernel.h"
#include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/funcs/sequence_padding.h"
#include "paddle/phi/kernels/funcs/sequence_scale.h" #include "paddle/phi/kernels/funcs/sequence_scale.h"
#include "paddle/utils/optional.h" #include "paddle/utils/optional.h"
...@@ -333,8 +333,7 @@ void WarpctcKernel(const Context& dev_ctx, ...@@ -333,8 +333,7 @@ void WarpctcKernel(const Context& dev_ctx,
num_sequences)); num_sequences));
sequence_width = logits.numel() / logits_dims[0]; sequence_width = logits.numel() / logits_dims[0];
max_sequence_length = max_sequence_length = phi::funcs::MaximumSequenceLength(logits_lod);
paddle::operators::math::MaximumSequenceLength(logits_lod);
} }
auto loss_dims = phi::make_ddim({static_cast<int64_t>(num_sequences), 1}); auto loss_dims = phi::make_ddim({static_cast<int64_t>(num_sequences), 1});
...@@ -361,7 +360,7 @@ void WarpctcKernel(const Context& dev_ctx, ...@@ -361,7 +360,7 @@ void WarpctcKernel(const Context& dev_ctx,
phi::Copy(dev_ctx, cpu_pad_value, dev_ctx.GetPlace(), true, &pad_value); phi::Copy(dev_ctx, cpu_pad_value, dev_ctx.GetPlace(), true, &pad_value);
} }
paddle::operators::math::PaddingLoDTensorFunctor<Context, T>()( phi::funcs::PaddingLoDTensorFunctor<Context, T>()(
dev_ctx, dev_ctx,
logits, logits,
&warpctc_logits, &warpctc_logits,
...@@ -369,7 +368,7 @@ void WarpctcKernel(const Context& dev_ctx, ...@@ -369,7 +368,7 @@ void WarpctcKernel(const Context& dev_ctx,
-1, -1,
0, 0,
false /* norm_by_times */, false /* norm_by_times */,
paddle::operators::math::kLengthBatchWidth); phi::funcs::kLengthBatchWidth);
} }
const T* warpctc_logits_data = warpctc_logits.data<T>(); const T* warpctc_logits_data = warpctc_logits.data<T>();
...@@ -394,39 +393,36 @@ void WarpctcKernel(const Context& dev_ctx, ...@@ -394,39 +393,36 @@ void WarpctcKernel(const Context& dev_ctx,
DenseTensor warpctc_label; DenseTensor warpctc_label;
if (logits_length.is_initialized()) { if (logits_length.is_initialized()) {
warpctc_label.Resize( warpctc_label.Resize(
{static_cast<int64_t>( {static_cast<int64_t>(phi::funcs::TotalSequenceLength(label_lod)), 1});
paddle::operators::math::TotalSequenceLength(label_lod)),
1});
dev_ctx.template HostAlloc<int>(&warpctc_label); dev_ctx.template HostAlloc<int>(&warpctc_label);
std::vector<phi::Vector<size_t>> lod; std::vector<phi::Vector<size_t>> lod;
lod.push_back(label_lod); lod.push_back(label_lod);
warpctc_label.set_lod(lod); warpctc_label.set_lod(lod);
if (dev_ctx.GetPlace() == phi::CPUPlace()) { if (dev_ctx.GetPlace() == phi::CPUPlace()) {
paddle::operators::math::UnpaddingLoDTensorFunctor<Context, int>()( phi::funcs::UnpaddingLoDTensorFunctor<Context, int>()(
dev_ctx, dev_ctx,
label, label,
&warpctc_label, &warpctc_label,
label.dims()[1] /*pad_seq_len*/, label.dims()[1] /*pad_seq_len*/,
0 /*lod_level*/, 0 /*lod_level*/,
false /*norm_by_times*/, false /*norm_by_times*/,
paddle::operators::math::kBatchLengthWidth); phi::funcs::kBatchLengthWidth);
} else { } else {
DenseTensor gpu_label; DenseTensor gpu_label;
gpu_label.Resize( gpu_label.Resize(
{static_cast<int64_t>( {static_cast<int64_t>(phi::funcs::TotalSequenceLength(label_lod)),
paddle::operators::math::TotalSequenceLength(label_lod)),
1}); 1});
dev_ctx.template Alloc<int>(&gpu_label); dev_ctx.template Alloc<int>(&gpu_label);
gpu_label.set_lod(lod); gpu_label.set_lod(lod);
paddle::operators::math::UnpaddingLoDTensorFunctor<Context, int>()( phi::funcs::UnpaddingLoDTensorFunctor<Context, int>()(
dev_ctx, dev_ctx,
label, label,
&gpu_label, &gpu_label,
label.dims()[1] /*pad_seq_len*/, label.dims()[1] /*pad_seq_len*/,
0 /*lod_level*/, 0 /*lod_level*/,
false /*norm_by_times*/, false /*norm_by_times*/,
paddle::operators::math::kBatchLengthWidth); phi::funcs::kBatchLengthWidth);
phi::Copy(dev_ctx, gpu_label, phi::CPUPlace(), true, &warpctc_label); phi::Copy(dev_ctx, gpu_label, phi::CPUPlace(), true, &warpctc_label);
} }
} else { } else {
......
...@@ -100,3 +100,8 @@ cc_test( ...@@ -100,3 +100,8 @@ cc_test(
strided_memcpy_test strided_memcpy_test
SRCS strided_memcpy_test.cc SRCS strided_memcpy_test.cc
DEPS device_context memory) DEPS device_context memory)
cc_test(
sequence_padding_test
SRCS sequence_padding_test.cc
DEPS sequence_padding)
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. /* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
...@@ -11,13 +11,15 @@ distributed under the License is distributed on an "AS IS" BASIS, ...@@ -11,13 +11,15 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include <gtest/gtest.h>
#include "paddle/fluid/operators/math/sequence_padding.h" #include "paddle/phi/kernels/funcs/sequence_padding.h"
#include "paddle/phi/core/tensor_utils.h"
#include <gtest/gtest.h>
template <typename DeviceContext, typename T> template <typename DeviceContext, typename T>
void TestSequencePadding(const DeviceContext &context, void TestSequencePadding(const DeviceContext &context,
const paddle::framework::LoD &lod, const phi::LoD &lod,
const size_t sequence_width) { const size_t sequence_width) {
phi::DenseTensor cpu_seq; phi::DenseTensor cpu_seq;
phi::DenseTensor cpu_seq_back; phi::DenseTensor cpu_seq_back;
...@@ -32,38 +34,43 @@ void TestSequencePadding(const DeviceContext &context, ...@@ -32,38 +34,43 @@ void TestSequencePadding(const DeviceContext &context,
static_cast<int64_t>(sequence_width)}); static_cast<int64_t>(sequence_width)});
cpu_seq.set_lod(lod); cpu_seq.set_lod(lod);
cpu_seq.mutable_data<T>(seq_dims, paddle::platform::CPUPlace()); auto *dev_ctx = static_cast<phi::CPUContext *>(
paddle::platform::DeviceContextPool::Instance().Get(phi::CPUPlace()));
cpu_seq.Resize(seq_dims);
dev_ctx->template Alloc<T>(&cpu_seq);
for (int64_t i = 0; i < cpu_seq.numel(); ++i) { for (int64_t i = 0; i < cpu_seq.numel(); ++i) {
cpu_seq.data<T>()[i] = static_cast<T>(i); cpu_seq.data<T>()[i] = static_cast<T>(i);
} }
auto place = context.GetPlace(); auto place = context.GetPlace();
if (paddle::platform::is_cpu_place(place)) { if (place.GetType() == phi::AllocationType::CPU) {
seq = cpu_seq; seq = cpu_seq;
} else { } else {
paddle::framework::TensorCopySync(cpu_seq, place, &seq); phi::Copy(context, cpu_seq, place, true, &seq);
seq.set_lod(lod); seq.set_lod(lod);
} }
const size_t max_sequence_length = const size_t max_sequence_length =
paddle::operators::math::MaximumSequenceLength(lod[level]); phi::funcs::MaximumSequenceLength(lod[level]);
const size_t num_sequences = lod[level].size() - 1; const size_t num_sequences = lod[level].size() - 1;
auto padding_dims = phi::make_ddim({static_cast<int64_t>(max_sequence_length), auto padding_dims = phi::make_ddim({static_cast<int64_t>(max_sequence_length),
static_cast<int64_t>(num_sequences), static_cast<int64_t>(num_sequences),
static_cast<int64_t>(sequence_width)}); static_cast<int64_t>(sequence_width)});
padding.mutable_data<T>(padding_dims, place); padding.Resize(padding_dims);
context.template Alloc<T>(&padding);
T *pad_value_data = cpu_pad_value.Resize({1});
cpu_pad_value.mutable_data<T>({1}, paddle::platform::CPUPlace()); T *pad_value_data = dev_ctx->template Alloc<T>(&cpu_pad_value);
*pad_value_data = static_cast<T>(0); *pad_value_data = static_cast<T>(0);
if (paddle::platform::is_cpu_place(place)) { if (place.GetType() == phi::AllocationType::CPU) {
pad_value = cpu_pad_value; pad_value = cpu_pad_value;
} else { } else {
paddle::framework::TensorCopySync(cpu_pad_value, place, &pad_value); phi::Copy(context, cpu_pad_value, place, true, &pad_value);
} }
paddle::operators::math::PaddingLoDTensorFunctor<DeviceContext, T>()( phi::funcs::PaddingLoDTensorFunctor<DeviceContext, T>()(
context, context,
seq, seq,
&padding, &padding,
...@@ -71,24 +78,18 @@ void TestSequencePadding(const DeviceContext &context, ...@@ -71,24 +78,18 @@ void TestSequencePadding(const DeviceContext &context,
-1, -1,
0, 0,
false, false,
paddle::operators::math::kLengthBatchWidth); phi::funcs::kLengthBatchWidth);
seq_back.set_lod(lod); seq_back.set_lod(lod);
seq_back.mutable_data<T>(seq_dims, place); seq_back.Resize(seq_dims);
paddle::operators::math::UnpaddingLoDTensorFunctor<DeviceContext, T>()( context.template Alloc<T>(&seq_back);
context, phi::funcs::UnpaddingLoDTensorFunctor<DeviceContext, T>()(
padding, context, padding, &seq_back, -1, 0, false, phi::funcs::kLengthBatchWidth);
&seq_back,
-1,
0,
false,
paddle::operators::math::kLengthBatchWidth);
if (paddle::platform::is_cpu_place(place)) { if (place.GetType() == phi::AllocationType::CPU) {
cpu_seq_back = seq_back; cpu_seq_back = seq_back;
} else { } else {
paddle::framework::TensorCopySync( phi::Copy(context, seq_back, phi::CPUPlace(), true, &cpu_seq_back);
seq_back, paddle::platform::CPUPlace(), &cpu_seq_back);
cpu_seq_back.set_lod(lod); cpu_seq_back.set_lod(lod);
} }
...@@ -100,15 +101,15 @@ void TestSequencePadding(const DeviceContext &context, ...@@ -100,15 +101,15 @@ void TestSequencePadding(const DeviceContext &context,
} }
TEST(Seq2BatchPadding, CPU) { TEST(Seq2BatchPadding, CPU) {
auto place = paddle::platform::CPUPlace(); auto place = phi::CPUPlace();
auto *context = static_cast<phi::CPUContext *>( auto *context = static_cast<phi::CPUContext *>(
paddle::platform::DeviceContextPool::Instance().Get(place)); paddle::platform::DeviceContextPool::Instance().Get(place));
paddle::framework::LoD lod1; phi::LoD lod1;
lod1.push_back(std::vector<size_t>{0, 10}); lod1.push_back(std::vector<size_t>{0, 10});
TestSequencePadding<phi::CPUContext, float>(*context, lod1, 16); TestSequencePadding<phi::CPUContext, float>(*context, lod1, 16);
paddle::framework::LoD lod2; phi::LoD lod2;
lod2.push_back(std::vector<size_t>{0, 2, 7, 10}); lod2.push_back(std::vector<size_t>{0, 2, 7, 10});
TestSequencePadding<phi::CPUContext, float>(*context, lod2, 128); TestSequencePadding<phi::CPUContext, float>(*context, lod2, 128);
} }
...@@ -119,11 +120,11 @@ TEST(SequencePadding, CUDA) { ...@@ -119,11 +120,11 @@ TEST(SequencePadding, CUDA) {
auto *context = static_cast<phi::GPUContext *>( auto *context = static_cast<phi::GPUContext *>(
paddle::platform::DeviceContextPool::Instance().Get(place)); paddle::platform::DeviceContextPool::Instance().Get(place));
paddle::framework::LoD lod1; phi::LoD lod1;
lod1.push_back(std::vector<size_t>{0, 10}); lod1.push_back(std::vector<size_t>{0, 10});
TestSequencePadding<phi::GPUContext, float>(*context, lod1, 16); TestSequencePadding<phi::GPUContext, float>(*context, lod1, 16);
paddle::framework::LoD lod2; phi::LoD lod2;
lod2.push_back(std::vector<size_t>{0, 2, 7, 10}); lod2.push_back(std::vector<size_t>{0, 2, 7, 10});
TestSequencePadding<phi::GPUContext, float>(*context, lod2, 128); TestSequencePadding<phi::GPUContext, float>(*context, lod2, 128);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册