-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Seq expand op #4740
Seq expand op #4740
Changes from 15 commits
901b041
acd1aae
f984cba
23701ff
555ab3f
8de04be
31531ab
a5adffd
a94b3dd
74b283c
00ad751
d697b6a
4e8fccf
2961674
97f1b98
35e7944
fab6f30
9f32b61
8d4e2d4
84f471b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,5 +135,6 @@ class LoDTensor : public Tensor { | |
private: | ||
LoD lod_; | ||
}; | ||
|
||
} // namespace framework | ||
} // namespace paddle |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#include "paddle/operators/seq_expand_op.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
using framework::Tensor; | ||
|
||
class SeqExpandOp : public framework::OperatorWithKernel { | ||
public: | ||
using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
||
protected: | ||
void InferShape(framework::InferShapeContext* ctx) const override { | ||
PADDLE_ENFORCE(ctx->HasInput("X"), | ||
"Input(X) of SeqExpandOp should not be null."); | ||
PADDLE_ENFORCE(ctx->HasOutput("Out"), | ||
"Output(Out) of SeqExpandOp should not be null."); | ||
PADDLE_ENFORCE( | ||
ctx->HasInput("Y"), | ||
"Input(Y) of SeqExpandOp should not be null while repeat == 0."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while -> when? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
framework::DDim out_dim; | ||
out_dim = ctx->GetInputDim("Y"); | ||
ctx->ShareLoD("Y", "Out"); | ||
ctx->SetOutputDim("Out", out_dim); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whether the InferShape should set the LoD for output LoDTensor? Here, the LoD will be computed in the forward according the attr and input LoDs. I'm not sure wether the InferShape needs to infer all the shape info (dimension, LoD). @reyoung @jacquesqiao @QiJune |
||
} | ||
}; | ||
|
||
class SeqExpandOpMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
SeqExpandOpMaker(framework::OpProto* proto, | ||
framework::OpAttrChecker* op_checker) | ||
: OpProtoAndCheckerMaker(proto, op_checker) { | ||
AddInput("X", | ||
"(Tensor or LoDTensor) The input('X') of this operator can be a " | ||
"LoDTensor or a base Tensor."); | ||
AddInput("Y", | ||
"(LoDTensor)The reference input('Y') of seq_expand op." | ||
"It must be a LoDTensor with k-level(k>0)." | ||
"Input(X) will be expanded according to LOD of input(Y)." | ||
"The element numbers of last level in input('Y') " | ||
"must be equal to dims[0] of input('X')."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 有的地方用input('X'),用的地方用Input(X),是不是要统一下 |
||
AddOutput("Out", | ||
"The output of seq_expand op." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add type for the output: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
"The lod of output will be as same as input(Y)'s lod."); | ||
AddComment(R"DOC( | ||
Expand input(X) according to LOD of input(Y). | ||
|
||
Case 1: | ||
|
||
Given 2-level a LoDTensor input(X) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure this op support empty sequence, if it supports, add a case because this scenario is special. for example, Y's LoD is Some instance in X should be dropped when a corresponding LoD element is empty. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed by adding unitest case and comments. |
||
X.lod = [[0, 2, 3], | ||
[0, 1, 3, 4]] | ||
X.data = [a, b, c, d] | ||
X.dims = [4, 1] | ||
and input(Y) | ||
Y.lod = [[0, 2, 4], | ||
[0, 3, 6, 7, 8]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the necessary condition? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
then we get 2-level LoDTensor | ||
Out.lod = [[0, 2, 4], | ||
[0, 3, 6, 7, 8]] | ||
Out.data = [a, a, a, b, b, b, c, d] | ||
Out.dims = [8, 1] | ||
|
||
Case 2: | ||
|
||
Given a 0-level LoDTensor input(X) | ||
X.data = [a, b, c] | ||
X.lod = NULL | ||
X.dims = [3, 1] | ||
and input(Y) | ||
Y.lod = [[0, 2, 3, 6]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also add the necessary condition: len(Y.lod[0]) -1 == X.dims[0] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also add the necessary condition: len(Y.lod[0]) == X.dims[0] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
then we get 1-level LoDTensor | ||
Out.lod = [[0, 2, 3, 6]] | ||
Out.data = [a, a, b, c, c, c] | ||
Out.dims = [6, 1] | ||
|
||
Case 3: | ||
|
||
Given a 0-level LoDTensor input(X) | ||
X.data = [[a, b], [c, d], [e, f]] | ||
X.lod = NULL | ||
X.dims = [3, 2] | ||
and input(Y) | ||
Y.lod = [[0, 2, 3, 6]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
then we get 1-level LoDTensor | ||
Out.lod = [[0, 2, 3, 6]] | ||
Out.data = [[a,b], [a,b] [c,d], [e, f], [e, f], [e, f]] | ||
Out.dims = [6, 2] | ||
|
||
|
||
)DOC"); | ||
} | ||
}; | ||
|
||
class SeqExpandOpGrad : public framework::OperatorWithKernel { | ||
public: | ||
using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
||
protected: | ||
void InferShape(framework::InferShapeContext* ctx) const override { | ||
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null"); | ||
PADDLE_ENFORCE(ctx->HasInput("Out"), "Input(Out) should not be null"); | ||
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")), | ||
"Input(Out@GRAD) should not be null"); | ||
auto x_dims = ctx->GetInputDim("X"); | ||
auto x_grad_name = framework::GradVarName("X"); | ||
if (ctx->HasOutput(x_grad_name)) { | ||
ctx->SetOutputDim(x_grad_name, x_dims); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle | ||
|
||
namespace ops = paddle::operators; | ||
REGISTER_OP(seq_expand, ops::SeqExpandOp, ops::SeqExpandOpMaker, | ||
seq_expand_grad, ops::SeqExpandOpGrad); | ||
REGISTER_OP_CPU_KERNEL(seq_expand, | ||
ops::SeqExpandKernel<paddle::platform::CPUPlace, float>); | ||
REGISTER_OP_CPU_KERNEL( | ||
seq_expand_grad, | ||
ops::SeqExpandGradKernel<paddle::platform::CPUPlace, float>); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#define EIGEN_USE_GPU | ||
#include "paddle/operators/seq_expand_op.h" | ||
|
||
namespace ops = paddle::operators; | ||
REGISTER_OP_GPU_KERNEL(seq_expand, | ||
ops::SeqExpandKernel<paddle::platform::GPUPlace, float>); | ||
REGISTER_OP_GPU_KERNEL( | ||
seq_expand_grad, | ||
ops::SeqExpandGradKernel<paddle::platform::GPUPlace, float>); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#pragma once | ||
|
||
#include "paddle/framework/op_registry.h" | ||
#include "paddle/memory/memcpy.h" | ||
#include "unsupported/Eigen/CXX11/Tensor" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
using LoDTensor = framework::LoDTensor; | ||
|
||
template <typename Place, typename T> | ||
class SeqExpandKernel : public framework::OpKernel<T> { | ||
public: | ||
void Compute(const framework::ExecutionContext& context) const override { | ||
auto* x = context.Input<LoDTensor>("X"); | ||
auto* out = context.Output<LoDTensor>("Out"); | ||
const T* x_data = x->data<T>(); | ||
auto x_dims = x->dims(); | ||
auto* y = context.Input<LoDTensor>("Y"); | ||
PADDLE_ENFORCE_EQ(x_dims[0], y->lod().back().size() - 1, | ||
"The size of last lod level in Input(Y)" | ||
"must be equal to dims[0] of Input(X)."); | ||
out->set_lod(y->lod()); | ||
out->Resize(y->dims()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The dimension has been set in the InferShape, so this line can be removed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FIxed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The dimension has been set in the InferShape. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FIxed. |
||
auto place = context.GetEigenDevice<Place>(); | ||
size_t element_len = framework::product(x_dims) / x_dims[0]; | ||
T* out_data = out->mutable_data<T>(context.GetPlace()); | ||
auto out_starts = out->lod().back(); | ||
|
||
for (size_t i = 0; i < out_starts.size() - 1; i++) { | ||
int scale = out_starts[i + 1] - out_starts[i]; | ||
Eigen::TensorMap< | ||
Eigen::Tensor<const T, 2, Eigen::RowMajor, Eigen::DenseIndex>> | ||
x_t(x_data, 1, element_len); | ||
Eigen::TensorMap<Eigen::Tensor<T, 2, Eigen::RowMajor, Eigen::DenseIndex>> | ||
out_t(out_data, scale, element_len); | ||
Eigen::array<int, 2> cast({scale, 1}); | ||
out_t.device(place) = x_t.broadcast(cast); | ||
x_data += element_len; | ||
out_data += element_len * scale; | ||
} | ||
} | ||
}; | ||
|
||
template <typename Place, typename T> | ||
class SeqExpandGradKernel : public framework::OpKernel<T> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add more comments to describe the process because the code is long and hard to understand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FIxed. |
||
public: | ||
void Compute(const framework::ExecutionContext& context) const override { | ||
auto* d_out = context.Input<LoDTensor>(framework::GradVarName("Out")); | ||
auto* x = context.Input<LoDTensor>("X"); | ||
auto* out = context.Input<LoDTensor>("Out"); | ||
auto* d_x = context.Output<LoDTensor>(framework::GradVarName("X")); | ||
auto out_last_level = out->lod().back(); | ||
d_x->set_lod(x->lod()); | ||
const T* d_out_data = d_out->data<T>(); | ||
auto d_out_dims = d_out->dims(); | ||
T* d_x_data = d_x->mutable_data<T>(context.GetPlace()); | ||
size_t element_len = framework::product(d_out_dims) / d_out_dims[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove line 71: size_t element_len = d_out->numel() / d_out->dims()[0]; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
|
||
for (size_t i = 0; i < out_last_level.size() - 1; ++i) { | ||
size_t repeat = out_last_level[i + 1] - out_last_level[i]; | ||
Eigen::TensorMap< | ||
Eigen::Tensor<const T, 2, Eigen::RowMajor, Eigen::DenseIndex>> | ||
d_out_t(d_out_data, static_cast<int>(repeat), element_len); | ||
Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, Eigen::DenseIndex>> | ||
d_x_t(d_x_data, static_cast<int>(element_len)); | ||
auto place = context.GetEigenDevice<Place>(); | ||
d_x_t.device(place) = d_out_t.sum(Eigen::array<int, 1>({{0}})); | ||
d_out_data += (repeat * element_len); | ||
d_x_data += element_len; | ||
} | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import unittest | ||
import numpy as np | ||
from op_test import OpTest | ||
|
||
|
||
class TestSeqExpand(OpTest): | ||
def set_data(self): | ||
x_data = np.random.uniform(0.1, 1, [3, 1]).astype('float32') | ||
y_data = np.random.uniform(0.1, 1, [8, 1]).astype('float32') | ||
y_lod = [[0, 1, 4, 8]] | ||
self.inputs = {'X': x_data, 'Y': (y_data, y_lod)} | ||
|
||
def compute(self): | ||
x = self.inputs['X'] | ||
x_data, x_lod = x if type(x) == tuple else (x, None) | ||
n = 1 + x_data.shape[0] if not x_lod else len(x_lod[0]) | ||
y_data, y_lod = self.inputs['Y'] | ||
repeats = [((y_lod[-1][i + 1] - y_lod[-1][i])) | ||
for i in range(len(y_lod[-1]) - 1)] | ||
out = x_data.repeat(repeats, axis=0) | ||
self.outputs = {'Out': out} | ||
|
||
def setUp(self): | ||
self.op_type = 'seq_expand' | ||
self.set_data() | ||
self.compute() | ||
|
||
def test_check_output(self): | ||
self.check_output() | ||
|
||
def test_check_grad(self): | ||
self.check_grad(["X"], "Out") | ||
|
||
|
||
class TestSeqExpandCase1(TestSeqExpand): | ||
def set_data(self): | ||
x_data = np.random.uniform(0.1, 1, [5, 1]).astype('float32') | ||
x_lod = [[0, 2, 5]] | ||
y_data = np.random.uniform(0.1, 1, [13, 1]).astype('float32') | ||
y_lod = [[0, 2, 5], [0, 2, 4, 7, 10, 13]] | ||
self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)} | ||
|
||
|
||
class TestSeqExpandCase2(TestSeqExpand): | ||
def set_data(self): | ||
x_data = np.random.uniform(0.1, 1, [1, 2, 2]).astype('float32') | ||
x_lod = [[0, 1]] | ||
y_data = np.random.uniform(0.1, 1, [2, 2, 2]).astype('float32') | ||
y_lod = [[0, 2]] | ||
self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)} | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just PADDLE_ENFORCE_NOT_NULL(Input(X) ?
and this comment lack information, tells nothing more than the enforce code itself.
So enforce without comment is ok, or with a comment that really helps to find out the reason for its failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.