-
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
Conversation
1. Add unitest 2. Add SeqExpandOpKernel
1. Add more comments and exmples 2. Rename repeat_lod to expand_lod 3. Remove unused head file
paddle/operators/seq_expand_op.cc
Outdated
out_dim[0] = out_dim[0] * repeat; | ||
} | ||
PADDLE_ENFORCE(ctx->HasOutput("Out"), | ||
"Output(Out) of PadOp should not be null."); |
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.
PadOp --> SeqExpandOp
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.
paddle/operators/seq_expand_op.h
Outdated
for (size_t i = 0; i < scales.size(); ++i) { | ||
count = element_len * (x_lod[0][i + 1] - x_lod[0][i]); | ||
for (size_t j = 0; j < scales[i]; ++j) { | ||
memory::Copy(place, out_data, place, x_data, sizeof(T) * count); |
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.
In GPU, we should set a CUDA stream for copy.
#ifdef PADDLE_WITH_CUDA
auto stream = reinterpret_cast<const platform::CUDADeviceContext&>(
context.device_context())
.stream();
memory::Copy(...... stream);
#else
memory::Copy(......);
#endif
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.
paddle/operators/seq_expand_op.h
Outdated
Eigen::TensorMap<Eigen::Tensor<T, 1>> d_x_t( | ||
d_x_data, static_cast<int>((ele_count * element_len) / repeat)); | ||
auto place = context.GetEigenDevice<Place>(); | ||
d_x_t.device(place) = d_out_t.sum(Eigen::array<int, 1>({0})); |
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.
Better change to
Eigen::array<int, 1>({{0}})
for clang compile.
https://stackoverflow.com/questions/31555584/why-is-clang-warning-suggest-braces-around-initialization-of-subobject-wmis
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.
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.
All the code is not read yet. Just part comments.
paddle/operators/seq_expand_op.cc
Outdated
: OpProtoAndCheckerMaker(proto, op_checker) { | ||
AddInput( | ||
"X", | ||
"The input('X') of seq_expand op. It can be LoDTensor or base Tensor."); |
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.
"(Tensor or LoDTensor) The input('X') of this operator can be a LoDTensor or a base Tensor."
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.
paddle/operators/seq_expand_op.cc
Outdated
"It must be a LoDTensor with k-level(k>0)." | ||
"This reference input is essential if 'repeat' attribute is not " | ||
"configured." | ||
"Input(X) will be expanded by LoD of input(Y) while repeat == 0."); |
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.
by - > according to the
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.
paddle/operators/seq_expand_op.cc
Outdated
"The input('X') of seq_expand op. It can be LoDTensor or base Tensor."); | ||
AddInput( | ||
"Y", | ||
"The reference input('Y') of seq_expand op." |
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.
" (LoDTensor) The ... "
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.
paddle/framework/lod_tensor.cc
Outdated
@@ -103,5 +103,34 @@ void LoDTensor::ShrinkInLevel(size_t level, size_t elem_begin, | |||
lod_ = new_lod; | |||
} | |||
|
|||
Vector<size_t> expand_lod(Vector<size_t> level, Vector<size_t> starts, | |||
Vector<size_t> scales, bool repeat) { |
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.
Functions names: "CamelCase"
https://google.github.io/styleguide/cppguide.html#Function_Names
Now, only the seq_expand needs this function, it may be removed to seq_expand_op.
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.
paddle/operators/seq_expand_op.cc
Outdated
} | ||
PADDLE_ENFORCE(ctx->HasOutput("Out"), | ||
"Output(Out) of SeqExpandOp should not be null."); | ||
ctx->SetOutputDim("Out", out_dim); |
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.
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
paddle/operators/seq_expand_op.cc
Outdated
repeat = 2 | ||
then we get 1-level LoDTensor | ||
Out.data = [1, 1, 2, 2, 3, 3, 4, 4] | ||
Out.lod = [[0, 2, 4, 6, 8]] |
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.
These examples are good, but still hard to understand. Need some more details, since this the changes for LoD are a bit complex. For example, explain the Repeatting
, it takes one instance (maybe other words) as a unit.
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.
paddle/operators/seq_expand_op.cc
Outdated
protected: | ||
void InferShape(framework::InferShapeContext* ctx) const override { | ||
PADDLE_ENFORCE(ctx->HasInput("X"), | ||
"Input(X) of SeqExpandOp should not be null."); |
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.
|
||
Case 1: | ||
|
||
Given 2-level a LoDTensor input(X) |
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.
Make sure this op support empty sequence, if it supports, add a case because this scenario is special.
for example, Y's LoD is 1 2 2 2
, that means there are 2 empty sequences.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed by adding unitest case and comments.
paddle/operators/seq_expand_op.h
Outdated
}; | ||
|
||
template <typename Place, typename T> | ||
class SeqExpandGradKernel : public framework::OpKernel<T> { |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
FIxed.
paddle/operators/seq_expand_op.cc
Outdated
"The element numbers of last level in input('Y') " | ||
"must be equal to dims[0] of input('X')."); | ||
AddOutput("Out", | ||
"The output of seq_expand op." |
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.
Add type for the output: (LoDTensor) The ...
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Add the necessary condition? Y.lod[0][-1] == X.dims[0]
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.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
paddle/operators/seq_expand_op.h
Outdated
"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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
FIxed.
paddle/operators/seq_expand_op.h
Outdated
"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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
FIxed.
paddle/operators/seq_expand_op.h
Outdated
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
2. Fix comments and paddle enforce check
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.
LGTM.
paddle/operators/seq_expand_op.cc
Outdated
"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 comment
The reason will be displayed to describe this comment to others. Learn more.
有的地方用input('X'),用的地方用Input(X),是不是要统一下
下面的注释里也是一样。
paddle/operators/seq_expand_op.cc
Outdated
PADDLE_ENFORCE(ctx->HasOutput("Out")); | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
while -> when?
repeat是从哪儿来呢?
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.
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.
LGTM
fix #5000