未验证 提交 6cdaa371 编写于 作者: 柠檬味~ 提交者: GitHub

DenseTensor (#48419)

上级 2a3ddce0
...@@ -145,30 +145,31 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -145,30 +145,31 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker {
void Make() override { void Make() override {
AddInput( AddInput(
"X", "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 " "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."); "total time steps in this mini-batch and N is the input_hidden_size.");
AddInput("PaddingData", AddInput(
"(Tensor, optional) the input(PaddingData) is an optional " "PaddingData",
"parameter, and it is learnable. " "(phi::DenseTensor, optional) the input(PaddingData) is an optional "
"This is a tensor with shape (P, N), where P is the " "parameter, and it is learnable. "
"top_pad + bottom_pad, N is the input_hidden_size. In order to " "This is a tensor with shape (P, N), where P is the "
"ensure the equal length of sequence before and after " "top_pad + bottom_pad, N is the input_hidden_size. In order to "
"convolution, it is necessary to fill the top and bottom of each " "ensure the equal length of sequence before and after "
"sequence according to context_length, context_stride and " "convolution, it is necessary to fill the top and bottom of each "
"context_start") "sequence according to context_length, context_stride and "
"context_start")
.AsDispensable(); .AsDispensable();
AddInput( AddInput(
"Filter", "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 " "This is a tensor with shape (K, M), where K is the "
"context_length * input_hidden_size, M is the output feature size."); "context_length * input_hidden_size, M is the output feature size.");
AddOutput( AddOutput(
"Out", "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 " "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."); "total time steps in this mini-batch, M is the output feature size.");
AddAttr<bool>("paddingTrainable", AddAttr<bool>("paddingTrainable",
......
...@@ -22,15 +22,12 @@ limitations under the License. */ ...@@ -22,15 +22,12 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using Tensor = phi::DenseTensor;
using LoDTensor = phi::DenseTensor;
template <typename DeviceContext, typename T> template <typename DeviceContext, typename T>
class SequenceConvKernel : public framework::OpKernel<T> { class SequenceConvKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& context) const override { void Compute(const framework::ExecutionContext& context) const override {
auto* in = context.Input<LoDTensor>("X"); auto* in = context.Input<phi::DenseTensor>("X");
auto* out = context.Output<LoDTensor>("Out"); auto* out = context.Output<phi::DenseTensor>("Out");
auto filter = *context.Input<phi::DenseTensor>("Filter"); auto filter = *context.Input<phi::DenseTensor>("Filter");
out->mutable_data<T>(context.GetPlace()); out->mutable_data<T>(context.GetPlace());
...@@ -40,11 +37,11 @@ class SequenceConvKernel : public framework::OpKernel<T> { ...@@ -40,11 +37,11 @@ class SequenceConvKernel : public framework::OpKernel<T> {
int context_stride = context.Attr<int>("contextStride"); int context_stride = context.Attr<int>("contextStride");
bool padding_trainable = context.Attr<bool>("paddingTrainable"); bool padding_trainable = context.Attr<bool>("paddingTrainable");
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(in->lod().empty(),
in->lod().empty(), false,
false, platform::errors::InvalidArgument(
platform::errors::InvalidArgument("Input(X) Tensor of SequenceConvOp " "Input(X) phi::DenseTensor of SequenceConvOp "
"does not contain LoD information.")); "does not contain LoD information."));
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(
in->lod().size(), in->lod().size(),
1UL, 1UL,
...@@ -64,7 +61,7 @@ class SequenceConvKernel : public framework::OpKernel<T> { ...@@ -64,7 +61,7 @@ class SequenceConvKernel : public framework::OpKernel<T> {
framework::DDim col_shape = {in->dims()[0], framework::DDim col_shape = {in->dims()[0],
context_length * sequence_width}; context_length * sequence_width};
Tensor col; phi::DenseTensor col;
col.mutable_data<T>(col_shape, context.GetPlace()); col.mutable_data<T>(col_shape, context.GetPlace());
// Because if padding_trainable is false, padding data should be zeros. // Because if padding_trainable is false, padding data should be zeros.
phi::funcs::SetConstant<DeviceContext, T> set_zero; phi::funcs::SetConstant<DeviceContext, T> set_zero;
...@@ -92,13 +89,14 @@ template <typename DeviceContext, typename T> ...@@ -92,13 +89,14 @@ template <typename DeviceContext, typename T>
class SequenceConvGradKernel : public framework::OpKernel<T> { class SequenceConvGradKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& context) const override { void Compute(const framework::ExecutionContext& context) const override {
auto* in_g = context.Output<LoDTensor>(framework::GradVarName("X")); auto* in_g = context.Output<phi::DenseTensor>(framework::GradVarName("X"));
auto* out_g = context.Input<LoDTensor>(framework::GradVarName("Out")); auto* out_g =
context.Input<phi::DenseTensor>(framework::GradVarName("Out"));
auto* filter_g = auto* filter_g =
context.Output<phi::DenseTensor>(framework::GradVarName("Filter")); context.Output<phi::DenseTensor>(framework::GradVarName("Filter"));
auto* padding_data_g = auto* padding_data_g =
context.Output<phi::DenseTensor>(framework::GradVarName("PaddingData")); 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"); auto* filter = context.Input<phi::DenseTensor>("Filter");
int context_start = context.Attr<int>("contextStart"); int context_start = context.Attr<int>("contextStart");
...@@ -125,7 +123,7 @@ class SequenceConvGradKernel : public framework::OpKernel<T> { ...@@ -125,7 +123,7 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
// use col_shape in the im2col calculation // use col_shape in the im2col calculation
framework::DDim col_shape = {in->dims()[0], framework::DDim col_shape = {in->dims()[0],
sequence_width * context_length}; sequence_width * context_length};
Tensor col; phi::DenseTensor col;
if (in_g || filter_g || (padding_trainable && padding_data_g)) { if (in_g || filter_g || (padding_trainable && padding_data_g)) {
col.mutable_data<T>(col_shape, context.GetPlace()); col.mutable_data<T>(col_shape, context.GetPlace());
...@@ -159,7 +157,7 @@ class SequenceConvGradKernel : public framework::OpKernel<T> { ...@@ -159,7 +157,7 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
padding_data_g->mutable_data<T>(context.GetPlace()); padding_data_g->mutable_data<T>(context.GetPlace());
set_zero(dev_ctx, padding_data_g, static_cast<T>(0)); 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, seq_project_grad_functor(dev_ctx,
*input, *input,
padding_trainable, padding_trainable,
...@@ -178,8 +176,8 @@ class SequenceConvGradKernel : public framework::OpKernel<T> { ...@@ -178,8 +176,8 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
filter_g->mutable_data<T>(context.GetPlace()); filter_g->mutable_data<T>(context.GetPlace());
set_zero(dev_ctx, filter_g, static_cast<T>(0)); set_zero(dev_ctx, filter_g, static_cast<T>(0));
Tensor filter_grad = *filter_g; phi::DenseTensor filter_grad = *filter_g;
LoDTensor out_grad = *out_g; phi::DenseTensor out_grad = *out_g;
const phi::DenseTensor* padding_data = nullptr; const phi::DenseTensor* padding_data = nullptr;
if (padding_trainable) { if (padding_trainable) {
......
...@@ -19,14 +19,13 @@ limitations under the License. */ ...@@ -19,14 +19,13 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using Tensor = phi::DenseTensor;
template <typename DeviceContext, typename T> template <typename DeviceContext, typename T>
class SequenceConvXPUKernel : public framework::OpKernel<T> { class SequenceConvXPUKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& context) const override { void Compute(const framework::ExecutionContext& context) const override {
auto* in = context.Input<LoDTensor>("X"); auto* in = context.Input<phi::DenseTensor>("X");
auto* out = context.Output<LoDTensor>("Out"); auto* out = context.Output<phi::DenseTensor>("Out");
auto filter = *context.Input<phi::DenseTensor>("Filter"); auto filter = *context.Input<phi::DenseTensor>("Filter");
out->mutable_data<T>(context.GetPlace()); out->mutable_data<T>(context.GetPlace());
...@@ -36,11 +35,11 @@ class SequenceConvXPUKernel : public framework::OpKernel<T> { ...@@ -36,11 +35,11 @@ class SequenceConvXPUKernel : public framework::OpKernel<T> {
int context_stride = context.Attr<int>("contextStride"); int context_stride = context.Attr<int>("contextStride");
bool padding_trainable = context.Attr<bool>("paddingTrainable"); bool padding_trainable = context.Attr<bool>("paddingTrainable");
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(in->lod().empty(),
in->lod().empty(), false,
false, platform::errors::InvalidArgument(
platform::errors::InvalidArgument("Input(X) Tensor of SequenceConvOp " "Input(X) phi::DenseTensor of SequenceConvOp "
"does not contain LoD information.")); "does not contain LoD information."));
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(
in->lod().size(), in->lod().size(),
1UL, 1UL,
...@@ -159,11 +158,12 @@ template <typename DeviceContext, typename T> ...@@ -159,11 +158,12 @@ template <typename DeviceContext, typename T>
class SequenceConvGradXPUKernel : public framework::OpKernel<T> { class SequenceConvGradXPUKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& context) const override { void Compute(const framework::ExecutionContext& context) const override {
auto* in_g = context.Output<LoDTensor>(framework::GradVarName("X")); auto* in_g = context.Output<phi::DenseTensor>(framework::GradVarName("X"));
auto* out_g = context.Input<LoDTensor>(framework::GradVarName("Out")); auto* out_g =
context.Input<phi::DenseTensor>(framework::GradVarName("Out"));
auto* filter_g = auto* filter_g =
context.Output<phi::DenseTensor>(framework::GradVarName("Filter")); 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"); auto* filter = context.Input<phi::DenseTensor>("Filter");
int context_start = context.Attr<int>("contextStart"); int context_start = context.Attr<int>("contextStart");
...@@ -171,11 +171,11 @@ class SequenceConvGradXPUKernel : public framework::OpKernel<T> { ...@@ -171,11 +171,11 @@ class SequenceConvGradXPUKernel : public framework::OpKernel<T> {
int context_stride = context.Attr<int>("contextStride"); int context_stride = context.Attr<int>("contextStride");
bool padding_trainable = context.Attr<bool>("paddingTrainable"); bool padding_trainable = context.Attr<bool>("paddingTrainable");
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(in->lod().empty(),
in->lod().empty(), false,
false, platform::errors::InvalidArgument(
platform::errors::InvalidArgument("Input(X) Tensor of SequenceConvOp " "Input(X) phi::DenseTensor of SequenceConvOp "
"does not contain LoD information.")); "does not contain LoD information."));
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(
in->lod().size(), in->lod().size(),
1UL, 1UL,
......
...@@ -36,11 +36,11 @@ class SequenceEnumerateOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -36,11 +36,11 @@ class SequenceEnumerateOpMaker : public framework::OpProtoAndCheckerMaker {
public: public:
void Make() override { void Make() override {
AddInput("X", AddInput("X",
"(2-D LoDTensor with the 2nd dimension equal to 1) " "(2-D phi::DenseTensor with the 2nd dimension equal to 1) "
"Input LoDTensor of SequenceEnumerate operator."); "Input phi::DenseTensor of SequenceEnumerate operator.");
AddOutput("Out", AddOutput("Out",
"(2-D LoDTensor with the 2nd dimension equal to win_size) " "(2-D phi::DenseTensor with the 2nd dimension equal to win_size) "
"Output LoDTensor of SequenceEnumerate operator."); "Output phi::DenseTensor of SequenceEnumerate operator.");
AddAttr<int>("win_size", "(int) The enumerate sequence window size.") AddAttr<int>("win_size", "(int) The enumerate sequence window size.")
.AddCustomChecker([](const int& win_size) { .AddCustomChecker([](const int& win_size) {
PADDLE_ENFORCE_GE(win_size, PADDLE_ENFORCE_GE(win_size,
......
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using phi::PADDLE_CUDA_NUM_THREADS; using phi::PADDLE_CUDA_NUM_THREADS;
using LoDTensor = phi::DenseTensor;
template <typename T> template <typename T>
__global__ void CalcOutPut(const T* in_data, __global__ void CalcOutPut(const T* in_data,
...@@ -52,8 +51,8 @@ template <typename T> ...@@ -52,8 +51,8 @@ template <typename T>
class SequenceEnumerateOpCUDAKernel : public framework::OpKernel<T> { class SequenceEnumerateOpCUDAKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& context) const override { void Compute(const framework::ExecutionContext& context) const override {
auto* in = context.Input<LoDTensor>("X"); auto* in = context.Input<phi::DenseTensor>("X");
auto* out = context.Output<LoDTensor>("Out"); auto* out = context.Output<phi::DenseTensor>("Out");
int win_size = context.Attr<int>("win_size"); int win_size = context.Attr<int>("win_size");
int pad_value = context.Attr<int>("pad_value"); int pad_value = context.Attr<int>("pad_value");
......
...@@ -18,14 +18,13 @@ ...@@ -18,14 +18,13 @@
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using LoDTensor = phi::DenseTensor;
template <typename DeviceContext, typename T> template <typename DeviceContext, typename T>
class SequenceEnumerateKernel : public framework::OpKernel<T> { class SequenceEnumerateKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& context) const override { void Compute(const framework::ExecutionContext& context) const override {
auto* in = context.Input<LoDTensor>("X"); auto* in = context.Input<phi::DenseTensor>("X");
auto* out = context.Output<LoDTensor>("Out"); auto* out = context.Output<phi::DenseTensor>("Out");
int win_size = context.Attr<int>("win_size"); int win_size = context.Attr<int>("win_size");
auto pad_value = static_cast<T>(context.Attr<int>("pad_value")); auto pad_value = static_cast<T>(context.Attr<int>("pad_value"));
...@@ -33,7 +32,7 @@ class SequenceEnumerateKernel : public framework::OpKernel<T> { ...@@ -33,7 +32,7 @@ class SequenceEnumerateKernel : public framework::OpKernel<T> {
in->lod().empty(), in->lod().empty(),
false, false,
platform::errors::InvalidArgument( platform::errors::InvalidArgument(
"Input(X) Tensor of SequenceEnumerateOp does not contain " "Input(X) phi::DenseTensor of SequenceEnumerateOp does not contain "
"LoD information.")); "LoD information."));
auto in_dims = in->dims(); auto in_dims = in->dims();
......
...@@ -27,20 +27,21 @@ class SequenceEraseOp : public framework::OperatorWithKernel { ...@@ -27,20 +27,21 @@ class SequenceEraseOp : public framework::OperatorWithKernel {
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "SequenceErase"); OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "SequenceErase");
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "SequenceErase"); OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "SequenceErase");
auto x_dims = ctx->GetInputDim("X"); auto x_dims = ctx->GetInputDim("X");
PADDLE_ENFORCE(x_dims.size() == 2 && x_dims[1] == 1, PADDLE_ENFORCE(
platform::errors::InvalidArgument( x_dims.size() == 2 && x_dims[1] == 1,
"Input(X) of SequenceEraseOp should be a 2-D LoDTensor " platform::errors::InvalidArgument(
"with the 2nd dimension equal to 1," "Input(X) of SequenceEraseOp should be a 2-D phi::DenseTensor "
"but received size %d with the 2nd dimension %d.", "with the 2nd dimension equal to 1,"
x_dims.size(), "but received size %d with the 2nd dimension %d.",
x_dims[1])); x_dims.size(),
x_dims[1]));
ctx->SetOutputDim("Out", x_dims); 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 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 // For runtime, output phi::DenseTensor's lod is determined by input X's lod
// the level specified by input RandTable. // and the level specified by input RandTable. We cannot get X's detail lod
// We cannot get X's detail lod and RankTable's level in this function, so // and RankTable's level in this function, so leave this work to the detail
// leave this work to the detail kernel implementation. // kernel implementation.
if (!ctx->IsRuntime()) { if (!ctx->IsRuntime()) {
ctx->SetLoDLevel("Out", ctx->GetLoDLevel("X")); ctx->SetLoDLevel("Out", ctx->GetLoDLevel("X"));
} }
...@@ -51,11 +52,11 @@ class SequenceEraseOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -51,11 +52,11 @@ class SequenceEraseOpMaker : public framework::OpProtoAndCheckerMaker {
public: public:
void Make() override { void Make() override {
AddInput("X", AddInput("X",
"(2-D LoDTensor with the 2nd dim. equal to 1) " "(2-D phi::DenseTensor with the 2nd dim. equal to 1) "
"Input LoDTensor of SequenceEraseOp."); "Input phi::DenseTensor of SequenceEraseOp.");
AddOutput("Out", AddOutput("Out",
"(2-D LoDTensor with the 2nd dim. equal to 1) " "(2-D phi::DenseTensor with the 2nd dim. equal to 1) "
"Output LoDTensor of SequenceEraseOp."); "Output phi::DenseTensor of SequenceEraseOp.");
AddAttr<std::vector<int>>("tokens", AddAttr<std::vector<int>>("tokens",
"(vector<int>) Tokens need to be erased from " "(vector<int>) Tokens need to be erased from "
"input sequences."); "input sequences.");
...@@ -64,7 +65,7 @@ Sequence Erase Operator. ...@@ -64,7 +65,7 @@ Sequence Erase Operator.
Sequence erase operator erases tokens specified by Attr(tokens) from the input Sequence erase operator erases tokens specified by Attr(tokens) from the input
sequences Input(X), and outputs the remaining data and modifies the LoD 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 X = [[2, 2, 6, 1, 3, 9, 6, 1, 0, 1]]^T
...@@ -77,7 +78,7 @@ operation, the three sequences become ...@@ -77,7 +78,7 @@ operation, the three sequences become
X1' = [[6]]^T, X2' = [[1, 9]]^T and X3' = [[6, 1, 0, 1]]^T. 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, Out = [[6, 1, 9, 6, 1, 0, 1]]^T,
......
...@@ -21,7 +21,6 @@ limitations under the License. */ ...@@ -21,7 +21,6 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using phi::PADDLE_CUDA_NUM_THREADS; using phi::PADDLE_CUDA_NUM_THREADS;
using LoDTensor = phi::DenseTensor;
template <typename T> template <typename T>
__global__ void LabelErasedIdx(const T* in_dat, __global__ void LabelErasedIdx(const T* in_dat,
...@@ -67,8 +66,8 @@ template <typename T> ...@@ -67,8 +66,8 @@ template <typename T>
class SequenceEraseOpCUDAKernel : public framework::OpKernel<T> { class SequenceEraseOpCUDAKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& ctx) const override { void Compute(const framework::ExecutionContext& ctx) const override {
auto* in = ctx.Input<LoDTensor>("X"); auto* in = ctx.Input<phi::DenseTensor>("X");
auto* out = ctx.Output<LoDTensor>("Out"); auto* out = ctx.Output<phi::DenseTensor>("Out");
auto lod = in->lod(); auto lod = in->lod();
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(
......
...@@ -20,8 +20,6 @@ limitations under the License. */ ...@@ -20,8 +20,6 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using LoDTensor = phi::DenseTensor;
class SequenceExpandAsOp : public framework::OperatorWithKernel { class SequenceExpandAsOp : public framework::OperatorWithKernel {
public: public:
using framework::OperatorWithKernel::OperatorWithKernel; using framework::OperatorWithKernel::OperatorWithKernel;
...@@ -49,8 +47,8 @@ class SequenceExpandAsOp : public framework::OperatorWithKernel { ...@@ -49,8 +47,8 @@ class SequenceExpandAsOp : public framework::OperatorWithKernel {
framework::Variable* y_var = framework::Variable* y_var =
PADDLE_GET(framework::Variable*, ctx->GetInputVarPtrs("Y")[0]); PADDLE_GET(framework::Variable*, ctx->GetInputVarPtrs("Y")[0]);
auto& x_dim = x_var->Get<LoDTensor>().dims(); auto& x_dim = x_var->Get<phi::DenseTensor>().dims();
auto& y_lod = y_var->Get<LoDTensor>().lod(); auto& y_lod = y_var->Get<phi::DenseTensor>().lod();
PADDLE_ENFORCE_EQ(y_lod.size(), PADDLE_ENFORCE_EQ(y_lod.size(),
1, 1,
...@@ -96,13 +94,16 @@ class SequenceExpandAsOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -96,13 +94,16 @@ class SequenceExpandAsOpMaker : public framework::OpProtoAndCheckerMaker {
public: public:
void Make() override { void Make() override {
AddInput("X", AddInput("X",
"(LoDTensor, default LoDTensor<float>) A 2-D LoDTensor whose lod " "(phi::DenseTensor, default phi::DenseTensor<float>) A 2-D "
"phi::DenseTensor whose lod "
"level is at most 1."); "level is at most 1.");
AddInput("Y", AddInput("Y",
"(LoDTensor, default LoDTensor<float>) Referred LoDTensor whose " "(phi::DenseTensor, default phi::DenseTensor<float>) Referred "
"phi::DenseTensor whose "
"lod (specified level) is referred by Input(X)."); "lod (specified level) is referred by Input(X).");
AddOutput("Out", AddOutput("Out",
"(LodTensor, default LoDTensor<float>) Output LoDTensor which is " "(phi::DenseTensor, default phi::DenseTensor<float>) Output "
"phi::DenseTensor which is "
"generated from Input(X) by referring lod of Input(Y)."); "generated from Input(X) by referring lod of Input(Y).");
AddComment(R"DOC( AddComment(R"DOC(
Sequence Expand As Operator. Sequence Expand As Operator.
...@@ -116,26 +117,26 @@ Following are cases to better explain how this works: ...@@ -116,26 +117,26 @@ Following are cases to better explain how this works:
Case 1: Case 1:
Given a 1-level LoDTensor input(X) Given a 1-level phi::DenseTensor input(X)
X.data = [[a], [b], [c], [d]] X.data = [[a], [b], [c], [d]]
X.dims = [4, 1] X.dims = [4, 1]
and input(Y) and input(Y)
Y.lod = [[0, 3, 6, 7, 8]] Y.lod = [[0, 3, 6, 7, 8]]
ref_level: 0 ref_level: 0
then we get 1-level LoDTensor then we get 1-level phi::DenseTensor
Out.lod = [[0, 3, 6, 7, 8]] Out.lod = [[0, 3, 6, 7, 8]]
Out.data = [[a], [a], [a], [b], [b], [b], [c], [d]] Out.data = [[a], [a], [a], [b], [b], [b], [c], [d]]
Out.dims = [8, 1] Out.dims = [8, 1]
Case 2: Case 2:
Given a common Tensor input(X) Given a common phi::DenseTensor input(X)
X.data = [[a, b], [c, d], [e, f]] X.data = [[a, b], [c, d], [e, f]]
X.dims = [3, 2] X.dims = [3, 2]
and input(Y) and input(Y)
Y.lod = [[0, 2, 3, 6]] Y.lod = [[0, 2, 3, 6]]
ref_level: 0 ref_level: 0
then we get a common LoDTensor then we get a common phi::DenseTensor
Out.lod = [[0, 2, 3, 6]] Out.lod = [[0, 2, 3, 6]]
Out.data = [[a, b], [a, b] [c, d], [e, f], [e, f], [e, f]] Out.data = [[a, b], [a, b] [c, d], [e, f], [e, f], [e, f]]
Out.dims = [6, 2] Out.dims = [6, 2]
......
...@@ -20,8 +20,6 @@ limitations under the License. */ ...@@ -20,8 +20,6 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using LoDTensor = phi::DenseTensor;
template <typename T> template <typename T>
static __global__ void sequence_expand_as_kernel(const T *in_data, static __global__ void sequence_expand_as_kernel(const T *in_data,
const size_t *expand_offset, const size_t *expand_offset,
...@@ -69,9 +67,9 @@ template <typename T> ...@@ -69,9 +67,9 @@ template <typename T>
struct SequenceExpandAsFunctor<phi::GPUContext, T> { struct SequenceExpandAsFunctor<phi::GPUContext, T> {
void operator()( void operator()(
const phi::GPUContext &context, const phi::GPUContext &context,
const LoDTensor &x, const phi::DenseTensor &x,
const framework::Vector<size_t> &ref_lod, /*expand referenced lod*/ const framework::Vector<size_t> &ref_lod, /*expand referenced lod*/
LoDTensor *out) { phi::DenseTensor *out) {
int height = x.dims()[0]; int height = x.dims()[0];
int width = phi::product(x.dims()) / height; int width = phi::product(x.dims()) / height;
...@@ -99,9 +97,9 @@ struct SequenceExpandAsFunctor<phi::GPUContext, T> { ...@@ -99,9 +97,9 @@ struct SequenceExpandAsFunctor<phi::GPUContext, T> {
template <typename T> template <typename T>
struct SequenceExpandAsGradFunctor<phi::GPUContext, T> { struct SequenceExpandAsGradFunctor<phi::GPUContext, T> {
void operator()(const phi::GPUContext &context, void operator()(const phi::GPUContext &context,
const LoDTensor &dout, const phi::DenseTensor &dout,
const framework::Vector<size_t> &ref_lod, /*expand based lod*/ const framework::Vector<size_t> &ref_lod, /*expand based lod*/
LoDTensor *dx) { phi::DenseTensor *dx) {
int height = dx->dims()[0]; int height = dx->dims()[0];
int width = phi::product(dx->dims()) / height; int width = phi::product(dx->dims()) / height;
......
...@@ -22,7 +22,6 @@ limitations under the License. */ ...@@ -22,7 +22,6 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using LoDTensor = phi::DenseTensor;
template <typename T, template <typename T,
int MajorType = Eigen::RowMajor, int MajorType = Eigen::RowMajor,
typename IndexType = Eigen::DenseIndex> typename IndexType = Eigen::DenseIndex>
...@@ -32,30 +31,30 @@ template <typename DeviceContext, typename T> ...@@ -32,30 +31,30 @@ template <typename DeviceContext, typename T>
struct SequenceExpandFunctor { struct SequenceExpandFunctor {
void operator()( void operator()(
const DeviceContext& ctx, const DeviceContext& ctx,
const LoDTensor& x, const phi::DenseTensor& x,
const framework::Vector<size_t>& x_lod, /*expand source lod*/ const framework::Vector<size_t>& x_lod, /*expand source lod*/
const framework::Vector<size_t>& ref_lod, /*expand referenced lod*/ const framework::Vector<size_t>& ref_lod, /*expand referenced lod*/
LoDTensor* out); phi::DenseTensor* out);
}; };
template <typename DeviceContext, typename T> template <typename DeviceContext, typename T>
struct SequenceExpandGradFunctor { struct SequenceExpandGradFunctor {
void operator()( void operator()(
const DeviceContext& ctx, const DeviceContext& ctx,
const LoDTensor& dout, const phi::DenseTensor& dout,
const framework::Vector<size_t>& x_lod, /*expand source lod*/ const framework::Vector<size_t>& x_lod, /*expand source lod*/
const framework::Vector<size_t>& ref_lod, /*expand referenced lod*/ const framework::Vector<size_t>& ref_lod, /*expand referenced lod*/
LoDTensor* dx); phi::DenseTensor* dx);
}; };
template <typename T> template <typename T>
struct SequenceExpandFunctor<phi::CPUContext, T> { struct SequenceExpandFunctor<phi::CPUContext, T> {
void operator()( void operator()(
const phi::CPUContext& context, const phi::CPUContext& context,
const LoDTensor& x, const phi::DenseTensor& x,
const framework::Vector<size_t>& x_lod, /*expand source lod*/ const framework::Vector<size_t>& x_lod, /*expand source lod*/
const framework::Vector<size_t>& ref_lod, /*expand referenced lod*/ const framework::Vector<size_t>& ref_lod, /*expand referenced lod*/
LoDTensor* out) { phi::DenseTensor* out) {
int out_offset = 0; int out_offset = 0;
int x_item_length = x.numel() / x.dims()[0]; int x_item_length = x.numel() / x.dims()[0];
auto out_data = out->data<T>(); auto out_data = out->data<T>();
...@@ -88,9 +87,9 @@ template <typename DeviceContext, typename T> ...@@ -88,9 +87,9 @@ template <typename DeviceContext, typename T>
class SequenceExpandKernel : public framework::OpKernel<T> { class SequenceExpandKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& context) const override { void Compute(const framework::ExecutionContext& context) const override {
auto* x = context.Input<LoDTensor>("X"); auto* x = context.Input<phi::DenseTensor>("X");
auto* y = context.Input<LoDTensor>("Y"); auto* y = context.Input<phi::DenseTensor>("Y");
auto* out = context.Output<LoDTensor>("Out"); auto* out = context.Output<phi::DenseTensor>("Out");
int ref_level = context.Attr<int>("ref_level"); int ref_level = context.Attr<int>("ref_level");
auto& x_lod = x->lod(); auto& x_lod = x->lod();
...@@ -100,7 +99,7 @@ class SequenceExpandKernel : public framework::OpKernel<T> { ...@@ -100,7 +99,7 @@ class SequenceExpandKernel : public framework::OpKernel<T> {
y_lod.empty(), y_lod.empty(),
false, false,
platform::errors::InvalidArgument( platform::errors::InvalidArgument(
"Input(Y) Tensor of SequenceExpandOp does not contain " "Input(Y) phi::DenseTensor of SequenceExpandOp does not contain "
"LoD information.")); "LoD information."));
if (ref_level == -1) ref_level = y_lod.size() - 1; if (ref_level == -1) ref_level = y_lod.size() - 1;
...@@ -164,10 +163,10 @@ template <typename T> ...@@ -164,10 +163,10 @@ template <typename T>
struct SequenceExpandGradFunctor<phi::CPUContext, T> { struct SequenceExpandGradFunctor<phi::CPUContext, T> {
void operator()( void operator()(
const phi::CPUContext& context, const phi::CPUContext& context,
const LoDTensor& dout, const phi::DenseTensor& dout,
const framework::Vector<size_t>& x_lod, /*expand source lod*/ const framework::Vector<size_t>& x_lod, /*expand source lod*/
const framework::Vector<size_t>& ref_lod, /*expand referenced lod*/ const framework::Vector<size_t>& ref_lod, /*expand referenced lod*/
LoDTensor* dx) { phi::DenseTensor* dx) {
int dout_offset = 0; int dout_offset = 0;
for (size_t i = 1; i < ref_lod.size(); ++i) { for (size_t i = 1; i < ref_lod.size(); ++i) {
int repeat_num = ref_lod[i] - ref_lod[i - 1]; int repeat_num = ref_lod[i] - ref_lod[i - 1];
...@@ -193,10 +192,11 @@ template <typename DeviceContext, typename T> ...@@ -193,10 +192,11 @@ template <typename DeviceContext, typename T>
class SequenceExpandGradKernel : public framework::OpKernel<T> { class SequenceExpandGradKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& context) const override { void Compute(const framework::ExecutionContext& context) const override {
auto* g_out = context.Input<LoDTensor>(framework::GradVarName("Out")); auto* g_out =
auto* x = context.Input<LoDTensor>("X"); context.Input<phi::DenseTensor>(framework::GradVarName("Out"));
auto* y = context.Input<LoDTensor>("Y"); auto* x = context.Input<phi::DenseTensor>("X");
auto* g_x = context.Output<LoDTensor>(framework::GradVarName("X")); auto* y = context.Input<phi::DenseTensor>("Y");
auto* g_x = context.Output<phi::DenseTensor>(framework::GradVarName("X"));
int ref_level = context.Attr<int>("ref_level"); int ref_level = context.Attr<int>("ref_level");
g_x->mutable_data<T>(context.GetPlace()); g_x->mutable_data<T>(context.GetPlace());
......
...@@ -82,7 +82,7 @@ class SequenceMaskOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -82,7 +82,7 @@ class SequenceMaskOpMaker : public framework::OpProtoAndCheckerMaker {
SequenceMask Operator SequenceMask Operator
This operator outputs a Mask according to Input(X) and Attr(maxlen). This operator outputs a Mask according to Input(X) and Attr(maxlen).
Supposing Input(X) is a Tensor with shape [d_1, d_2, ..., d_n], the Supposing Input(X) is a phi::DenseTensor with shape [d_1, d_2, ..., d_n], the
Output(Y) is a mask with shape [d_1, d_2, ..., d_n, maxlen], where: Output(Y) is a mask with shape [d_1, d_2, ..., d_n, maxlen], where:
Y(i_1, i_2, ..., i_n, j) = (j < X(i_1, i_2, ..., i_n)) Y(i_1, i_2, ..., i_n, j) = (j < X(i_1, i_2, ..., i_n))
......
...@@ -28,9 +28,6 @@ ...@@ -28,9 +28,6 @@
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using LoDTensor = phi::DenseTensor;
using Tensor = phi::DenseTensor;
template <typename Tx, typename Ty> template <typename Tx, typename Ty>
struct SequenceMaskForRangeFunctor { struct SequenceMaskForRangeFunctor {
HOSTDEVICE SequenceMaskForRangeFunctor(const Tx *x, Ty *y, int maxlen) HOSTDEVICE SequenceMaskForRangeFunctor(const Tx *x, Ty *y, int maxlen)
...@@ -50,8 +47,11 @@ struct SequenceMaskForRangeFunctor { ...@@ -50,8 +47,11 @@ struct SequenceMaskForRangeFunctor {
template <typename DeviceContext, typename Tx> template <typename DeviceContext, typename Tx>
struct SequenceMaskFunctor { struct SequenceMaskFunctor {
SequenceMaskFunctor( SequenceMaskFunctor(const DeviceContext &ctx,
const DeviceContext &ctx, const Tx *x, Tensor *y, int limits, int maxlen) const Tx *x,
phi::DenseTensor *y,
int limits,
int maxlen)
: ctx_(ctx), x_(x), y_(y), limits_(limits), maxlen_(maxlen) {} : ctx_(ctx), x_(x), y_(y), limits_(limits), maxlen_(maxlen) {}
template <typename Ty> template <typename Ty>
...@@ -64,15 +64,13 @@ struct SequenceMaskFunctor { ...@@ -64,15 +64,13 @@ struct SequenceMaskFunctor {
private: private:
const DeviceContext &ctx_; const DeviceContext &ctx_;
const Tx *x_; const Tx *x_;
Tensor *y_; phi::DenseTensor *y_;
int limits_; int limits_;
int maxlen_; int maxlen_;
}; };
template <typename DeviceContext, typename Tx> template <typename DeviceContext, typename Tx>
class SequenceMaskKernel : public framework::OpKernel<Tx> { class SequenceMaskKernel : public framework::OpKernel<Tx> {
using Tensor = phi::DenseTensor;
public: public:
void Compute(const framework::ExecutionContext &ctx) const override { void Compute(const framework::ExecutionContext &ctx) const override {
auto *x = ctx.Input<phi::DenseTensor>("X"); auto *x = ctx.Input<phi::DenseTensor>("X");
......
...@@ -18,8 +18,6 @@ limitations under the License. */ ...@@ -18,8 +18,6 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using Tensor = phi::DenseTensor;
template <typename DeviceContext, typename T> template <typename DeviceContext, typename T>
class SequenceMaskNPUKernel : public framework::OpKernel<T> { class SequenceMaskNPUKernel : public framework::OpKernel<T> {
public: public:
...@@ -58,7 +56,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> { ...@@ -58,7 +56,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
auto y_dim = phi::vectorize<int>(x->dims()); auto y_dim = phi::vectorize<int>(x->dims());
y_dim.push_back(maxlen); y_dim.push_back(maxlen);
Tensor cast_x; phi::DenseTensor cast_x;
cast_x.mutable_data<int32_t>(x->dims(), ctx.GetPlace()); cast_x.mutable_data<int32_t>(x->dims(), ctx.GetPlace());
const auto& cast1_runner = NpuOpRunner( const auto& cast1_runner = NpuOpRunner(
"Cast", "Cast",
...@@ -68,7 +66,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> { ...@@ -68,7 +66,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
ConvertToNpuDtype(framework::TransToProtoVarType(cast_x.dtype()))}}); ConvertToNpuDtype(framework::TransToProtoVarType(cast_x.dtype()))}});
cast1_runner.Run(dev_ctx.stream()); cast1_runner.Run(dev_ctx.stream());
Tensor tmp; phi::DenseTensor tmp;
tmp.mutable_data<int32_t>(phi::make_ddim({maxlen}), ctx.GetPlace()); tmp.mutable_data<int32_t>(phi::make_ddim({maxlen}), ctx.GetPlace());
NpuOpRunner range_runner; NpuOpRunner range_runner;
range_runner.SetType("Range"); range_runner.SetType("Range");
...@@ -78,7 +76,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> { ...@@ -78,7 +76,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
range_runner.AddOutput(tmp); range_runner.AddOutput(tmp);
range_runner.Run(dev_ctx.stream()); range_runner.Run(dev_ctx.stream());
Tensor expand_tmp; phi::DenseTensor expand_tmp;
expand_tmp.mutable_data<int32_t>(phi::make_ddim(y_dim), ctx.GetPlace()); expand_tmp.mutable_data<int32_t>(phi::make_ddim(y_dim), ctx.GetPlace());
const auto& expand_runner = const auto& expand_runner =
NpuOpRunner("ExpandD", {tmp}, {expand_tmp}, {{"shape", y_dim}}); NpuOpRunner("ExpandD", {tmp}, {expand_tmp}, {{"shape", y_dim}});
...@@ -87,7 +85,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> { ...@@ -87,7 +85,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
auto x_dims = phi::vectorize<int>(x->dims()); auto x_dims = phi::vectorize<int>(x->dims());
x_dims.push_back(1); x_dims.push_back(1);
cast_x.Resize(phi::make_ddim({x_dims})); cast_x.Resize(phi::make_ddim({x_dims}));
Tensor x_tmp; phi::DenseTensor x_tmp;
x_tmp.mutable_data<int32_t>(phi::make_ddim(y_dim), ctx.GetPlace()); x_tmp.mutable_data<int32_t>(phi::make_ddim(y_dim), ctx.GetPlace());
const auto& tile_runner = const auto& tile_runner =
NpuOpRunner("TileWithAxis", NpuOpRunner("TileWithAxis",
...@@ -96,7 +94,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> { ...@@ -96,7 +94,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
{{"axis", x->dims().size()}, {"tiles", maxlen}}); {{"axis", x->dims().size()}, {"tiles", maxlen}});
tile_runner.Run(dev_ctx.stream()); tile_runner.Run(dev_ctx.stream());
Tensor y_tmp; phi::DenseTensor y_tmp;
y_tmp.mutable_data<uint8_t>(phi::make_ddim(y_dim), ctx.GetPlace()); y_tmp.mutable_data<uint8_t>(phi::make_ddim(y_dim), ctx.GetPlace());
const auto& less_runner = const auto& less_runner =
NpuOpRunner("Less", {expand_tmp, x_tmp}, {y_tmp}, {}); NpuOpRunner("Less", {expand_tmp, x_tmp}, {y_tmp}, {});
......
...@@ -69,7 +69,7 @@ class SequencePadOp : public framework::OperatorWithKernel { ...@@ -69,7 +69,7 @@ class SequencePadOp : public framework::OperatorWithKernel {
// run time // run time
framework::Variable* x_var = framework::Variable* x_var =
PADDLE_GET(framework::Variable*, ctx->GetInputVarPtrs("X")[0]); PADDLE_GET(framework::Variable*, ctx->GetInputVarPtrs("X")[0]);
const auto& x_lod = x_var->Get<LoDTensor>().lod(); const auto& x_lod = x_var->Get<phi::DenseTensor>().lod();
PADDLE_ENFORCE_EQ(x_lod.empty(), PADDLE_ENFORCE_EQ(x_lod.empty(),
false, false,
platform::errors::NotFound( platform::errors::NotFound(
...@@ -145,20 +145,22 @@ class SequencePadOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -145,20 +145,22 @@ class SequencePadOpMaker : public framework::OpProtoAndCheckerMaker {
public: public:
void Make() override { void Make() override {
AddInput("X", AddInput("X",
"(LoDTensor, default LoDTensor<float>) Input variable which " "(phi::DenseTensor, default phi::DenseTensor<float>) Input "
"variable which "
"should contain lod information."); "should contain lod information.");
AddInput("PadValue", AddInput("PadValue",
"(LoDTensor), this Tensor holds values that will be fill into " "(phi::DenseTensor), this phi::DenseTensor holds values that will "
"be fill into "
"padded steps. It can be a scalar or a tensor whose shape equals " "padded steps. It can be a scalar or a tensor whose shape equals "
"to time steps in sequences. If it's a scalar, it will be " "to time steps in sequences. If it's a scalar, it will be "
"automatically broadcasted to the shape of time step."); "automatically broadcasted to the shape of time step.");
AddOutput( AddOutput("Out",
"Out", "(phi::DenseTensor) The output vairable, which contains padded "
"(LoDTensor) The output vairable, which contains padded sequences."); "sequences.");
AddOutput( AddOutput("Length",
"Length", "(phi::DenseTensor) The output vairable, which contains the "
"(LoDTensor) The output vairable, which contains the actual length of " "actual length of "
"sequences before padding."); "sequences before padding.");
AddAttr<int>( AddAttr<int>(
"padded_length", "padded_length",
"The length of padded sequences. It can be set to -1 or " "The length of padded sequences. It can be set to -1 or "
...@@ -179,41 +181,41 @@ class SequencePadOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -179,41 +181,41 @@ class SequencePadOpMaker : public framework::OpProtoAndCheckerMaker {
Case 1: Case 1:
Given a 1-level LoDTensor input(X): Given a 1-level phi::DenseTensor input(X):
X.lod = [[0, 2, 5]] X.lod = [[0, 2, 5]]
X.data = [a, b, c, d, e] X.data = [a, b, c, d, e]
and Input(PadValue): and Input(PadValue):
PadValue.data = [0] PadValue.data = [0]
and attribite 'padded_length' = 4, and attribite 'padded_length' = 4,
then we get LoDTensor: then we get phi::DenseTensor:
Out.data = [[a, b, 0, 0], Out.data = [[a, b, 0, 0],
[c, d, e, 0]] [c, d, e, 0]]
Length.data = [2, 3] Length.data = [2, 3]
Case 2: Case 2:
Given a 1-level LoDTensor input(X): Given a 1-level phi::DenseTensor input(X):
X.lod = [[0, 2, 5]] X.lod = [[0, 2, 5]]
X.data = [[a1, a2], [b1, b2], [c1, c2], [d1, d2], [e1, e2]] X.data = [[a1, a2], [b1, b2], [c1, c2], [d1, d2], [e1, e2]]
and Input(PadValue): and Input(PadValue):
PadValue.data = [0] PadValue.data = [0]
and attribite 'padded_length' = -1, which mean using the length and attribite 'padded_length' = -1, which mean using the length
of longest input sequence(3 in this case), of longest input sequence(3 in this case),
then we get LoDTensor: then we get phi::DenseTensor:
Out.data = [[[a1, a2], [b1, b2], [0, 0]], Out.data = [[[a1, a2], [b1, b2], [0, 0]],
[[c1, c2], [d1, d2], [e1, e2]]] [[c1, c2], [d1, d2], [e1, e2]]]
Length.data = [2, 3] Length.data = [2, 3]
Case 3: Case 3:
Given a 1-level LoDTensor input(X): Given a 1-level phi::DenseTensor input(X):
X.lod = [[0, 2, 5]] X.lod = [[0, 2, 5]]
X.data = [[a1, a2], [b1, b2], [c1, c2], [d1, d2], [e1, e2]] X.data = [[a1, a2], [b1, b2], [c1, c2], [d1, d2], [e1, e2]]
and Input(PadValue): and Input(PadValue):
PadValue.data = [p1, p2] PadValue.data = [p1, p2]
and attribite 'padded_length' = -1, which mean using the length and attribite 'padded_length' = -1, which mean using the length
of longest input sequence(3 in this case), of longest input sequence(3 in this case),
then we get LoDTensor: then we get phi::DenseTensor:
Out.data = [[[a1, a2], [b1, b2], [p1, p2]], Out.data = [[[a1, a2], [b1, b2], [p1, p2]],
[[c1, c2], [d1, d2], [e1, e2]]] [[c1, c2], [d1, d2], [e1, e2]]]
Length.data = [2, 3] Length.data = [2, 3]
......
...@@ -24,25 +24,24 @@ limitations under the License. */ ...@@ -24,25 +24,24 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using LoDTensor = phi::DenseTensor;
using LoD = framework::LoD; using LoD = framework::LoD;
template <typename DeviceContext, typename T> template <typename DeviceContext, typename T>
class SequencePadOpKernel : public framework::OpKernel<T> { class SequencePadOpKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& ctx) const override { void Compute(const framework::ExecutionContext& ctx) const override {
const auto* x = ctx.Input<LoDTensor>("X"); const auto* x = ctx.Input<phi::DenseTensor>("X");
auto* out = ctx.Output<LoDTensor>("Out"); auto* out = ctx.Output<phi::DenseTensor>("Out");
auto* len_t = ctx.Output<LoDTensor>("Length"); auto* len_t = ctx.Output<phi::DenseTensor>("Length");
out->mutable_data<T>(ctx.GetPlace()); out->mutable_data<T>(ctx.GetPlace());
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(x->lod().empty(),
x->lod().empty(), false,
false, platform::errors::NotFound(
platform::errors::NotFound("Input(X) Tensor of SequencePadOp does not " "Input(X) phi::DenseTensor of SequencePadOp does not "
"contain LoD information.")); "contain LoD information."));
const auto* pad_value = ctx.Input<LoDTensor>("PadValue"); const auto* pad_value = ctx.Input<phi::DenseTensor>("PadValue");
int padded_length = ctx.Attr<int>("padded_length"); int padded_length = ctx.Attr<int>("padded_length");
...@@ -56,7 +55,7 @@ class SequencePadOpKernel : public framework::OpKernel<T> { ...@@ -56,7 +55,7 @@ class SequencePadOpKernel : public framework::OpKernel<T> {
false, false,
math::kBatchLengthWidth); math::kBatchLengthWidth);
LoDTensor seq_len; phi::DenseTensor seq_len;
seq_len.Resize(len_t->dims()); seq_len.Resize(len_t->dims());
int64_t* len_data = seq_len.mutable_data<int64_t>(platform::CPUPlace()); int64_t* len_data = seq_len.mutable_data<int64_t>(platform::CPUPlace());
for (size_t i = 1; i < x->lod()[0].size(); ++i) { for (size_t i = 1; i < x->lod()[0].size(); ++i) {
...@@ -73,9 +72,10 @@ template <typename DeviceContext, typename T> ...@@ -73,9 +72,10 @@ template <typename DeviceContext, typename T>
class SequencePadGradOpKernel : public framework::OpKernel<T> { class SequencePadGradOpKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& ctx) const override { void Compute(const framework::ExecutionContext& ctx) const override {
auto* d_x = ctx.Output<LoDTensor>(framework::GradVarName("X")); auto* d_x = ctx.Output<phi::DenseTensor>(framework::GradVarName("X"));
if (d_x) { if (d_x) {
const auto* d_out = ctx.Input<LoDTensor>(framework::GradVarName("Out")); const auto* d_out =
ctx.Input<phi::DenseTensor>(framework::GradVarName("Out"));
d_x->mutable_data<T>(ctx.GetPlace()); d_x->mutable_data<T>(ctx.GetPlace());
int padded_length = ctx.Attr<int>("padded_length"); int padded_length = ctx.Attr<int>("padded_length");
......
...@@ -53,12 +53,15 @@ class SequencePoolOp : public framework::OperatorWithKernel { ...@@ -53,12 +53,15 @@ class SequencePoolOp : public framework::OperatorWithKernel {
class SequencePoolOpMaker : public framework::OpProtoAndCheckerMaker { class SequencePoolOpMaker : public framework::OpProtoAndCheckerMaker {
public: public:
void Make() override { void Make() override {
AddInput("X", "(LoDTensor) The variable-length input of SequencePoolOp"); AddInput("X",
AddOutput("Out", "(phi::DenseTensor) The variable-length input of SequencePoolOp");
"(Tensor) The output of SequencePoolOp does not contain LoD " AddOutput(
"information."); "Out",
"(phi::DenseTensor) The output of SequencePoolOp does not contain LoD "
"information.");
AddOutput("MaxIndex", AddOutput("MaxIndex",
"(Tensor<int>) This tensor is used for the sequence max-pooling " "(phi::DenseTensor<int>) This tensor is used for the sequence "
"max-pooling "
"to record the max indexes.") "to record the max indexes.")
.AsIntermediate(); .AsIntermediate();
AddAttr<bool>("is_test", AddAttr<bool>("is_test",
...@@ -92,11 +95,11 @@ The following example explains how this works: ...@@ -92,11 +95,11 @@ The following example explains how this works:
For a mini-batch of 3 variable-length sentences, For a mini-batch of 3 variable-length sentences,
containing 2, 3, and 2 time-steps: containing 2, 3, and 2 time-steps:
Assume X is a [7,M,N] LoDTensor, and X->lod()[0] = [0, 2, 5, 7], 7=2+3+2. Assume X is a [7,M,N] phi::DenseTensor, and X->lod()[0] = [0, 2, 5, 7], 7=2+3+2.
Besides, for the sake of simplicity, we assume M=1 and N=1, Besides, for the sake of simplicity, we assume M=1 and N=1,
and the value of X = [[1, 3], [2, 4, 6], [5, 1]]. and the value of X = [[1, 3], [2, 4, 6], [5, 1]].
Thus, Out is a [3,1,1] Tensor without LoD information. Thus, Out is a [3,1,1] phi::DenseTensor without LoD information.
And for different pooltype, the value of Out is as follows: And for different pooltype, the value of Out is as follows:
- AVERAGE: [2, 4, 3], where 2=(1+3)/2, 4=(2+4+6)/3, 3=(5+1)/2 - AVERAGE: [2, 4, 3], where 2=(1+3)/2, 4=(2+4+6)/3, 3=(5+1)/2
......
...@@ -23,15 +23,12 @@ limitations under the License. */ ...@@ -23,15 +23,12 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using Tensor = phi::DenseTensor;
using LoDTensor = phi::DenseTensor;
template <typename DeviceContext, typename T> template <typename DeviceContext, typename T>
class SequencePoolKernel : public framework::OpKernel<T> { class SequencePoolKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& context) const override { void Compute(const framework::ExecutionContext& context) const override {
auto* in = context.Input<LoDTensor>("X"); auto* in = context.Input<phi::DenseTensor>("X");
auto* out = context.Output<LoDTensor>("Out"); auto* out = context.Output<phi::DenseTensor>("Out");
std::string pooltype = context.Attr<std::string>("pooltype"); std::string pooltype = context.Attr<std::string>("pooltype");
T pad_value = static_cast<T>(context.Attr<float>("pad_value")); T pad_value = static_cast<T>(context.Attr<float>("pad_value"));
...@@ -39,11 +36,11 @@ class SequencePoolKernel : public framework::OpKernel<T> { ...@@ -39,11 +36,11 @@ class SequencePoolKernel : public framework::OpKernel<T> {
auto lod = in->lod(); auto lod = in->lod();
auto lod_level = lod.size(); auto lod_level = lod.size();
// InferShape by lod // InferShape by lod
PADDLE_ENFORCE_GT( PADDLE_ENFORCE_GT(lod_level,
lod_level, 0,
0, platform::errors::InvalidArgument(
platform::errors::InvalidArgument("Input(X) Tensor of SequencePoolOp " "Input(X) phi::DenseTensor of SequencePoolOp "
"does not contain LoD information.")); "does not contain LoD information."));
PADDLE_ENFORCE_LE(lod_level, PADDLE_ENFORCE_LE(lod_level,
2UL, 2UL,
platform::errors::InvalidArgument( platform::errors::InvalidArgument(
...@@ -100,8 +97,9 @@ template <typename DeviceContext, typename T> ...@@ -100,8 +97,9 @@ template <typename DeviceContext, typename T>
class SequencePoolGradKernel : public framework::OpKernel<T> { class SequencePoolGradKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& context) const override { void Compute(const framework::ExecutionContext& context) const override {
auto* out_g = context.Input<LoDTensor>(framework::GradVarName("Out")); auto* out_g =
auto* in_g = context.Output<LoDTensor>(framework::GradVarName("X")); context.Input<phi::DenseTensor>(framework::GradVarName("Out"));
auto* in_g = context.Output<phi::DenseTensor>(framework::GradVarName("X"));
std::string pooltype = context.Attr<std::string>("pooltype"); std::string pooltype = context.Attr<std::string>("pooltype");
const phi::DenseTensor* index = nullptr; const phi::DenseTensor* index = nullptr;
if (pooltype == "MAX") { if (pooltype == "MAX") {
......
...@@ -19,33 +19,30 @@ limitations under the License. */ ...@@ -19,33 +19,30 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using Tensor = phi::DenseTensor;
using LoDTensor = phi::DenseTensor;
template <typename DeviceContext, typename T> template <typename DeviceContext, typename T>
struct SequenceSoftmaxFunctor { struct SequenceSoftmaxFunctor {
void operator()( void operator()(
const DeviceContext &ctx, const DeviceContext &ctx,
const LoDTensor &x, const phi::DenseTensor &x,
const framework::Vector<size_t> &ref_lod, /*expand referenced lod*/ const framework::Vector<size_t> &ref_lod, /*expand referenced lod*/
LoDTensor *out); phi::DenseTensor *out);
}; };
template <typename DeviceContext, typename T> template <typename DeviceContext, typename T>
struct SequenceSoftmaxGradFunctor { struct SequenceSoftmaxGradFunctor {
void operator()(const DeviceContext &ctx, void operator()(const DeviceContext &ctx,
const LoDTensor &dout, const phi::DenseTensor &dout,
const LoDTensor &out, const phi::DenseTensor &out,
const framework::Vector<size_t> &ref_lod, /*referenced lod*/ const framework::Vector<size_t> &ref_lod, /*referenced lod*/
LoDTensor *dx); phi::DenseTensor *dx);
}; };
template <typename T> template <typename T>
struct SequenceSoftmaxFunctor<phi::CPUContext, T> { struct SequenceSoftmaxFunctor<phi::CPUContext, T> {
void operator()(const phi::CPUContext &ctx, void operator()(const phi::CPUContext &ctx,
const LoDTensor &x, const phi::DenseTensor &x,
const framework::Vector<size_t> &ref_lod, /*referenced lod*/ const framework::Vector<size_t> &ref_lod, /*referenced lod*/
LoDTensor *out) { phi::DenseTensor *out) {
size_t height = ref_lod.size() - 1; size_t height = ref_lod.size() - 1;
const T *in_data = x.data<T>(); const T *in_data = x.data<T>();
T *out_data = out->mutable_data<T>(ctx.GetPlace()); T *out_data = out->mutable_data<T>(ctx.GetPlace());
...@@ -65,10 +62,10 @@ struct SequenceSoftmaxFunctor<phi::CPUContext, T> { ...@@ -65,10 +62,10 @@ struct SequenceSoftmaxFunctor<phi::CPUContext, T> {
template <typename T> template <typename T>
struct SequenceSoftmaxGradFunctor<phi::CPUContext, T> { struct SequenceSoftmaxGradFunctor<phi::CPUContext, T> {
void operator()(const phi::CPUContext &ctx, void operator()(const phi::CPUContext &ctx,
const LoDTensor &dout, const phi::DenseTensor &dout,
const LoDTensor &out, const phi::DenseTensor &out,
const framework::Vector<size_t> &ref_lod, /*referenced lod*/ const framework::Vector<size_t> &ref_lod, /*referenced lod*/
LoDTensor *dx) { phi::DenseTensor *dx) {
size_t height = ref_lod.size() - 1; size_t height = ref_lod.size() - 1;
const T *softmax_grad_data = dout.data<T>(); const T *softmax_grad_data = dout.data<T>();
...@@ -94,17 +91,17 @@ template <typename DeviceContext, typename T> ...@@ -94,17 +91,17 @@ template <typename DeviceContext, typename T>
class SequenceSoftmaxKernel : public framework::OpKernel<T> { class SequenceSoftmaxKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext &ctx) const override { void Compute(const framework::ExecutionContext &ctx) const override {
auto *x = ctx.Input<LoDTensor>("X"); auto *x = ctx.Input<phi::DenseTensor>("X");
auto *out = ctx.Output<LoDTensor>("Out"); auto *out = ctx.Output<phi::DenseTensor>("Out");
auto lod = x->lod(); auto lod = x->lod();
auto dims = x->dims(); auto dims = x->dims();
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(lod.empty(),
lod.empty(), false,
false, platform::errors::InvalidArgument(
platform::errors::InvalidArgument( "Input(X) phi::DenseTensor of SequenceSoftmax "
"Input(X) Tensor of SequenceSoftmax operator does not contain " "operator does not contain "
"LoD information.")); "LoD information."));
const size_t level = lod.size() - 1; const size_t level = lod.size() - 1;
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(
...@@ -138,10 +135,10 @@ template <typename DeviceContext, typename T> ...@@ -138,10 +135,10 @@ template <typename DeviceContext, typename T>
class SequenceSoftmaxGradKernel : public framework::OpKernel<T> { class SequenceSoftmaxGradKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext &ctx) const override { void Compute(const framework::ExecutionContext &ctx) const override {
auto *out = ctx.Input<LoDTensor>("Out"); auto *out = ctx.Input<phi::DenseTensor>("Out");
auto *out_grad = ctx.Input<LoDTensor>(framework::GradVarName("Out")); auto *out_grad = ctx.Input<phi::DenseTensor>(framework::GradVarName("Out"));
auto *x = ctx.Input<LoDTensor>("X"); auto *x = ctx.Input<phi::DenseTensor>("X");
auto *x_grad = ctx.Output<LoDTensor>(framework::GradVarName("X")); auto *x_grad = ctx.Output<phi::DenseTensor>(framework::GradVarName("X"));
if (!x_grad) { if (!x_grad) {
return; return;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册