Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DenseTensor #48419

Merged
merged 45 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
5b9415b
Update sequence_softmax_op.h
Lemon-er Nov 27, 2022
22cd783
Update sequence_softmax_op.h
Lemon-er Nov 27, 2022
6c837bf
Update sequence_conv_op.cc
Lemon-er Nov 27, 2022
369d58b
Update sequence_conv_op.h
Lemon-er Nov 27, 2022
faf1383
Update sequence_conv_op.h
Lemon-er Nov 27, 2022
a495a73
Update sequence_conv_op_xpu.cc
Lemon-er Nov 27, 2022
adbbeaa
Update sequence_enumerate_op.cc
Lemon-er Nov 27, 2022
e10ba9e
Update sequence_enumerate_op.cu
Lemon-er Nov 27, 2022
49f1eb8
Update sequence_enumerate_op.h
Lemon-er Nov 27, 2022
242c8b7
Update sequence_erase_op.cc
Lemon-er Nov 27, 2022
e302bd9
Update sequence_erase_op.cc
Lemon-er Nov 27, 2022
ae1e668
Update sequence_erase_op.cu
Lemon-er Nov 27, 2022
33674fb
Update sequence_expand_as_op.cc
Lemon-er Nov 27, 2022
9d933a0
Update sequence_expand_as_op.cc
Lemon-er Nov 27, 2022
c12e49b
Update sequence_expand_as_op.cu
Lemon-er Nov 27, 2022
ebecd2c
Update sequence_expand_op.h
Lemon-er Nov 27, 2022
8de2c79
Update sequence_softmax_op.h
Lemon-er Nov 27, 2022
1337a60
Update sequence_conv_op.cc
Lemon-er Nov 27, 2022
87e4124
Update sequence_conv_op.h
Lemon-er Nov 27, 2022
f9d83ca
Update sequence_conv_op_xpu.cc
Lemon-er Nov 27, 2022
98256ec
Update sequence_enumerate_op.cu
Lemon-er Nov 27, 2022
341abdb
Update sequence_enumerate_op.h
Lemon-er Nov 27, 2022
54f0885
Update sequence_expand_as_op.cc
Lemon-er Nov 27, 2022
ad227f7
Update sequence_expand_as_op.cu
Lemon-er Nov 27, 2022
a839b6b
Update sequence_expand_op.h
Lemon-er Nov 27, 2022
e448b9a
Update sequence_softmax_op.h
Lemon-er Nov 27, 2022
6e42a7a
DenseTensor
Lemon-er Nov 28, 2022
7613ac3
Update sequence_pad_op.cc
Lemon-er Nov 28, 2022
7fb5a8c
Update sequence_mask_op.cc
Lemon-er Nov 28, 2022
8a95239
Update sequence_mask_op.cc
Lemon-er Nov 29, 2022
4bbaffd
Update sequence_conv_op.cc
Lemon-er Nov 29, 2022
1f06485
Update sequence_conv_op.cc
Lemon-er Nov 29, 2022
382e6d2
Update sequence_enumerate_op.cu
Lemon-er Nov 29, 2022
a91d124
Update sequence_mask_op.cc
Lemon-er Nov 29, 2022
0cfa989
Update sequence_pad_op.cc
Lemon-er Nov 29, 2022
660c67c
Update sequence_pool_op.cc
Lemon-er Nov 29, 2022
f8011e1
Update sequence_erase_op.cc
Lemon-er Nov 29, 2022
da1841c
Update sequence_pad_op.h
Lemon-er Nov 29, 2022
ad24ae7
Update sequence_erase_op.cc
Lemon-er Nov 29, 2022
f5a6b65
Update sequence_erase_op.cc
Lemon-er Nov 29, 2022
23d5a5b
Update sequence_pad_op.h
Lemon-er Nov 29, 2022
4d38a5e
Update sequence_erase_op.cc
Lemon-er Dec 2, 2022
6e75ae2
Update sequence_softmax_op.h
Lemon-er Dec 2, 2022
f8bdd3e
Update sequence_mask_op_npu.cc
Lemon-er Dec 2, 2022
a6d86cd
for codestyle
Ligoml Dec 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions paddle/fluid/operators/sequence_ops/sequence_conv_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,30 +145,31 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker {
void Make() override {
AddInput(
"X",
"(LoDTensor) the input(X) is a LodTensor, which supports "
"(phi::DenseTensor) the input(X) is a LodTensor, which supports "
"variable-time length input sequence. The underlying tensor in "
"this LoDTensor is a matrix with shape (T, N), where T is the "
"this phi::DenseTensor is a matrix with shape (T, N), where T is the "
"total time steps in this mini-batch and N is the input_hidden_size.");
AddInput("PaddingData",
"(Tensor, optional) the input(PaddingData) is an optional "
"parameter, and it is learnable. "
"This is a tensor with shape (P, N), where P is the "
"top_pad + bottom_pad, N is the input_hidden_size. In order to "
"ensure the equal length of sequence before and after "
"convolution, it is necessary to fill the top and bottom of each "
"sequence according to context_length, context_stride and "
"context_start")
AddInput(
"PaddingData",
"(phi::DenseTensor, optional) the input(PaddingData) is an optional "
"parameter, and it is learnable. "
"This is a tensor with shape (P, N), where P is the "
"top_pad + bottom_pad, N is the input_hidden_size. In order to "
"ensure the equal length of sequence before and after "
"convolution, it is necessary to fill the top and bottom of each "
"sequence according to context_length, context_stride and "
"context_start")
.AsDispensable();
AddInput(
"Filter",
"(Tensor) the input(Filter) is an learnable parameter."
"(phi::DenseTensor) the input(Filter) is an learnable parameter."
"This is a tensor with shape (K, M), where K is the "
"context_length * input_hidden_size, M is the output feature size.");
AddOutput(
"Out",
"(LoDTensor) the output(Out) is a LodTensor, which support "
"(phi::DenseTensor) the output(Out) is a LodTensor, which support "
"variable-time length output sequence. The underlying tensor in "
"this LoDTensor is a matrix with shape (T, M), where, T is the "
"this phi::DenseTensor is a matrix with shape (T, M), where, T is the "
"total time steps in this mini-batch, M is the output feature size.");

AddAttr<bool>("paddingTrainable",
Expand Down
34 changes: 16 additions & 18 deletions paddle/fluid/operators/sequence_ops/sequence_conv_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ limitations under the License. */
namespace paddle {
namespace operators {

using Tensor = phi::DenseTensor;
using LoDTensor = phi::DenseTensor;

template <typename DeviceContext, typename T>
class SequenceConvKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
auto* in = context.Input<LoDTensor>("X");
auto* out = context.Output<LoDTensor>("Out");
auto* in = context.Input<phi::DenseTensor>("X");
auto* out = context.Output<phi::DenseTensor>("Out");
auto filter = *context.Input<phi::DenseTensor>("Filter");

out->mutable_data<T>(context.GetPlace());
Expand All @@ -40,11 +37,11 @@ class SequenceConvKernel : public framework::OpKernel<T> {
int context_stride = context.Attr<int>("contextStride");
bool padding_trainable = context.Attr<bool>("paddingTrainable");

PADDLE_ENFORCE_EQ(
in->lod().empty(),
false,
platform::errors::InvalidArgument("Input(X) Tensor of SequenceConvOp "
"does not contain LoD information."));
PADDLE_ENFORCE_EQ(in->lod().empty(),
false,
platform::errors::InvalidArgument(
"Input(X) phi::DenseTensor of SequenceConvOp "
"does not contain LoD information."));
PADDLE_ENFORCE_EQ(
in->lod().size(),
1UL,
Expand All @@ -64,7 +61,7 @@ class SequenceConvKernel : public framework::OpKernel<T> {

framework::DDim col_shape = {in->dims()[0],
context_length * sequence_width};
Tensor col;
phi::DenseTensor col;
col.mutable_data<T>(col_shape, context.GetPlace());
// Because if padding_trainable is false, padding data should be zeros.
phi::funcs::SetConstant<DeviceContext, T> set_zero;
Expand Down Expand Up @@ -92,13 +89,14 @@ template <typename DeviceContext, typename T>
class SequenceConvGradKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
auto* in_g = context.Output<LoDTensor>(framework::GradVarName("X"));
auto* out_g = context.Input<LoDTensor>(framework::GradVarName("Out"));
auto* in_g = context.Output<phi::DenseTensor>(framework::GradVarName("X"));
auto* out_g =
context.Input<phi::DenseTensor>(framework::GradVarName("Out"));
auto* filter_g =
context.Output<phi::DenseTensor>(framework::GradVarName("Filter"));
auto* padding_data_g =
context.Output<phi::DenseTensor>(framework::GradVarName("PaddingData"));
auto* in = context.Input<LoDTensor>("X");
auto* in = context.Input<phi::DenseTensor>("X");
auto* filter = context.Input<phi::DenseTensor>("Filter");

int context_start = context.Attr<int>("contextStart");
Expand All @@ -125,7 +123,7 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
// use col_shape in the im2col calculation
framework::DDim col_shape = {in->dims()[0],
sequence_width * context_length};
Tensor col;
phi::DenseTensor col;

if (in_g || filter_g || (padding_trainable && padding_data_g)) {
col.mutable_data<T>(col_shape, context.GetPlace());
Expand Down Expand Up @@ -159,7 +157,7 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
padding_data_g->mutable_data<T>(context.GetPlace());
set_zero(dev_ctx, padding_data_g, static_cast<T>(0));

LoDTensor* input = const_cast<LoDTensor*>(in);
phi::DenseTensor* input = const_cast<phi::DenseTensor*>(in);
seq_project_grad_functor(dev_ctx,
*input,
padding_trainable,
Expand All @@ -178,8 +176,8 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
filter_g->mutable_data<T>(context.GetPlace());
set_zero(dev_ctx, filter_g, static_cast<T>(0));

Tensor filter_grad = *filter_g;
LoDTensor out_grad = *out_g;
phi::DenseTensor filter_grad = *filter_g;
phi::DenseTensor out_grad = *out_g;

const phi::DenseTensor* padding_data = nullptr;
if (padding_trainable) {
Expand Down
32 changes: 16 additions & 16 deletions paddle/fluid/operators/sequence_ops/sequence_conv_op_xpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ limitations under the License. */

namespace paddle {
namespace operators {
using Tensor = phi::DenseTensor;

template <typename DeviceContext, typename T>
class SequenceConvXPUKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
auto* in = context.Input<LoDTensor>("X");
auto* out = context.Output<LoDTensor>("Out");
auto* in = context.Input<phi::DenseTensor>("X");
auto* out = context.Output<phi::DenseTensor>("Out");
auto filter = *context.Input<phi::DenseTensor>("Filter");

out->mutable_data<T>(context.GetPlace());
Expand All @@ -36,11 +35,11 @@ class SequenceConvXPUKernel : public framework::OpKernel<T> {
int context_stride = context.Attr<int>("contextStride");
bool padding_trainable = context.Attr<bool>("paddingTrainable");

PADDLE_ENFORCE_EQ(
in->lod().empty(),
false,
platform::errors::InvalidArgument("Input(X) Tensor of SequenceConvOp "
"does not contain LoD information."));
PADDLE_ENFORCE_EQ(in->lod().empty(),
false,
platform::errors::InvalidArgument(
"Input(X) phi::DenseTensor of SequenceConvOp "
"does not contain LoD information."));
PADDLE_ENFORCE_EQ(
in->lod().size(),
1UL,
Expand Down Expand Up @@ -159,23 +158,24 @@ template <typename DeviceContext, typename T>
class SequenceConvGradXPUKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
auto* in_g = context.Output<LoDTensor>(framework::GradVarName("X"));
auto* out_g = context.Input<LoDTensor>(framework::GradVarName("Out"));
auto* in_g = context.Output<phi::DenseTensor>(framework::GradVarName("X"));
auto* out_g =
context.Input<phi::DenseTensor>(framework::GradVarName("Out"));
auto* filter_g =
context.Output<phi::DenseTensor>(framework::GradVarName("Filter"));
auto* in = context.Input<LoDTensor>("X");
auto* in = context.Input<phi::DenseTensor>("X");
auto* filter = context.Input<phi::DenseTensor>("Filter");

int context_start = context.Attr<int>("contextStart");
int context_length = context.Attr<int>("contextLength");
int context_stride = context.Attr<int>("contextStride");
bool padding_trainable = context.Attr<bool>("paddingTrainable");

PADDLE_ENFORCE_EQ(
in->lod().empty(),
false,
platform::errors::InvalidArgument("Input(X) Tensor of SequenceConvOp "
"does not contain LoD information."));
PADDLE_ENFORCE_EQ(in->lod().empty(),
false,
platform::errors::InvalidArgument(
"Input(X) phi::DenseTensor of SequenceConvOp "
"does not contain LoD information."));
PADDLE_ENFORCE_EQ(
in->lod().size(),
1UL,
Expand Down
8 changes: 4 additions & 4 deletions paddle/fluid/operators/sequence_ops/sequence_enumerate_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ class SequenceEnumerateOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddInput("X",
"(2-D LoDTensor with the 2nd dimension equal to 1) "
"Input LoDTensor of SequenceEnumerate operator.");
"(2-D phi::DenseTensor with the 2nd dimension equal to 1) "
"Input phi::DenseTensor of SequenceEnumerate operator.");
AddOutput("Out",
"(2-D LoDTensor with the 2nd dimension equal to win_size) "
"Output LoDTensor of SequenceEnumerate operator.");
"(2-D phi::DenseTensor with the 2nd dimension equal to win_size) "
"Output phi::DenseTensor of SequenceEnumerate operator.");
AddAttr<int>("win_size", "(int) The enumerate sequence window size.")
.AddCustomChecker([](const int& win_size) {
PADDLE_ENFORCE_GE(win_size,
Expand Down
5 changes: 2 additions & 3 deletions paddle/fluid/operators/sequence_ops/sequence_enumerate_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
namespace paddle {
namespace operators {
using phi::PADDLE_CUDA_NUM_THREADS;
using LoDTensor = phi::DenseTensor;

template <typename T>
__global__ void CalcOutPut(const T* in_data,
Expand Down Expand Up @@ -52,8 +51,8 @@ template <typename T>
class SequenceEnumerateOpCUDAKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
auto* in = context.Input<LoDTensor>("X");
auto* out = context.Output<LoDTensor>("Out");
auto* in = context.Input<phi::DenseTensor>("X");
auto* out = context.Output<phi::DenseTensor>("Out");
int win_size = context.Attr<int>("win_size");
int pad_value = context.Attr<int>("pad_value");

Expand Down
7 changes: 3 additions & 4 deletions paddle/fluid/operators/sequence_ops/sequence_enumerate_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@

namespace paddle {
namespace operators {
using LoDTensor = phi::DenseTensor;

template <typename DeviceContext, typename T>
class SequenceEnumerateKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
auto* in = context.Input<LoDTensor>("X");
auto* out = context.Output<LoDTensor>("Out");
auto* in = context.Input<phi::DenseTensor>("X");
auto* out = context.Output<phi::DenseTensor>("Out");
int win_size = context.Attr<int>("win_size");
auto pad_value = static_cast<T>(context.Attr<int>("pad_value"));

PADDLE_ENFORCE_EQ(
in->lod().empty(),
false,
platform::errors::InvalidArgument(
"Input(X) Tensor of SequenceEnumerateOp does not contain "
"Input(X) phi::DenseTensor of SequenceEnumerateOp does not contain "
"LoD information."));

auto in_dims = in->dims();
Expand Down
37 changes: 19 additions & 18 deletions paddle/fluid/operators/sequence_ops/sequence_erase_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,21 @@ class SequenceEraseOp : public framework::OperatorWithKernel {
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "SequenceErase");
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "SequenceErase");
auto x_dims = ctx->GetInputDim("X");
PADDLE_ENFORCE(x_dims.size() == 2 && x_dims[1] == 1,
platform::errors::InvalidArgument(
"Input(X) of SequenceEraseOp should be a 2-D LoDTensor "
"with the 2nd dimension equal to 1,"
"but received size %d with the 2nd dimension %d.",
x_dims.size(),
x_dims[1]));
PADDLE_ENFORCE(
x_dims.size() == 2 && x_dims[1] == 1,
platform::errors::InvalidArgument(
"Input(X) of SequenceEraseOp should be a 2-D phi::DenseTensor "
"with the 2nd dimension equal to 1,"
"but received size %d with the 2nd dimension %d.",
x_dims.size(),
x_dims[1]));
ctx->SetOutputDim("Out", x_dims);
// The output LoDTensor's lod_level should be input X's lod_level.
// The output phi::DenseTensor's lod_level should be input X's lod_level.
// For compile-time, we call SetLoDLevel to set output's lod_level.
// For runtime, output LoDTensor's lod is determined by input X's lod and
// the level specified by input RandTable.
// We cannot get X's detail lod and RankTable's level in this function, so
// leave this work to the detail kernel implementation.
// For runtime, output phi::DenseTensor's lod is determined by input X's lod
// and the level specified by input RandTable. We cannot get X's detail lod
// and RankTable's level in this function, so leave this work to the detail
// kernel implementation.
if (!ctx->IsRuntime()) {
ctx->SetLoDLevel("Out", ctx->GetLoDLevel("X"));
}
Expand All @@ -51,11 +52,11 @@ class SequenceEraseOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddInput("X",
"(2-D LoDTensor with the 2nd dim. equal to 1) "
"Input LoDTensor of SequenceEraseOp.");
"(2-D phi::DenseTensor with the 2nd dim. equal to 1) "
"Input phi::DenseTensor of SequenceEraseOp.");
AddOutput("Out",
"(2-D LoDTensor with the 2nd dim. equal to 1) "
"Output LoDTensor of SequenceEraseOp.");
"(2-D phi::DenseTensor with the 2nd dim. equal to 1) "
"Output phi::DenseTensor of SequenceEraseOp.");
AddAttr<std::vector<int>>("tokens",
"(vector<int>) Tokens need to be erased from "
"input sequences.");
Expand All @@ -64,7 +65,7 @@ Sequence Erase Operator.

Sequence erase operator erases tokens specified by Attr(tokens) from the input
sequences Input(X), and outputs the remaining data and modifies the LoD
information at the same time. For example, given a 2-D LoDTensor
information at the same time. For example, given a 2-D phi::DenseTensor

X = [[2, 2, 6, 1, 3, 9, 6, 1, 0, 1]]^T

Expand All @@ -77,7 +78,7 @@ operation, the three sequences become

X1' = [[6]]^T, X2' = [[1, 9]]^T and X3' = [[6, 1, 0, 1]]^T.

Hence the LoDTensor Output(Out) should be
Hence the phi::DenseTensor Output(Out) should be

Out = [[6, 1, 9, 6, 1, 0, 1]]^T,

Expand Down
5 changes: 2 additions & 3 deletions paddle/fluid/operators/sequence_ops/sequence_erase_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ limitations under the License. */
namespace paddle {
namespace operators {
using phi::PADDLE_CUDA_NUM_THREADS;
using LoDTensor = phi::DenseTensor;

template <typename T>
__global__ void LabelErasedIdx(const T* in_dat,
Expand Down Expand Up @@ -67,8 +66,8 @@ template <typename T>
class SequenceEraseOpCUDAKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
auto* in = ctx.Input<LoDTensor>("X");
auto* out = ctx.Output<LoDTensor>("Out");
auto* in = ctx.Input<phi::DenseTensor>("X");
auto* out = ctx.Output<phi::DenseTensor>("Out");

auto lod = in->lod();
PADDLE_ENFORCE_EQ(
Expand Down
Loading