Conversation
1. Add unitest 2. Add SeqExpandOpKernel
… seq_expand_op
… seq_expand_op
… seq_expand_op
1. Add more comments and exmples 2. Rename repeat_lod to expand_lod 3. Remove unused head file
… seq_expand_op
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."); |
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.
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
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.
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
qingqing01
left a comment
There was a problem hiding this comment.
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.
"(Tensor or LoDTensor) The input('X') of this operator can be a LoDTensor or a base Tensor."
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.
by - > according to the
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.
" (LoDTensor) The ... "
paddle/framework/lod_tensor.cc
Outdated
| } | ||
|
|
||
| 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.
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.
| } | ||
| 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.
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.
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.
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.
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.
|
|
||
| Case 1: | ||
|
|
||
| Given 2-level a LoDTensor input(X) |
There was a problem hiding this comment.
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.
Fixed by adding unitest case and comments.
| }; | ||
|
|
||
| template <typename Place, typename T> | ||
| class SeqExpandGradKernel : public framework::OpKernel<T> { |
There was a problem hiding this comment.
add more comments to describe the process because the code is long and hard to understand.
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.
Add type for the output: (LoDTensor) The ...
| X.dims = [4, 1] | ||
| and input(Y) | ||
| Y.lod = [[0, 2, 4], | ||
| [0, 3, 6, 7, 8]] |
There was a problem hiding this comment.
Add the necessary condition? Y.lod[0][-1] == X.dims[0]
| X.lod = NULL | ||
| X.dims = [3, 1] | ||
| and input(Y) | ||
| Y.lod = [[0, 2, 3, 6]] |
There was a problem hiding this comment.
Also add the necessary condition: len(Y.lod[0]) -1 == X.dims[0]
| X.lod = NULL | ||
| X.dims = [3, 1] | ||
| and input(Y) | ||
| Y.lod = [[0, 2, 3, 6]] |
There was a problem hiding this comment.
Also add the necessary condition: len(Y.lod[0]) == X.dims[0]
| X.lod = NULL | ||
| X.dims = [3, 2] | ||
| and input(Y) | ||
| Y.lod = [[0, 2, 3, 6]] |
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.
The dimension has been set in the InferShape, so this line can be removed.
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.
The dimension has been set in the InferShape.
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.
remove line 71:
size_t element_len = d_out->numel() / d_out->dims()[0];… seq_expand_op
… seq_expand_op
2. Fix comments and paddle enforce check
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.
有的地方用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.
while -> when?
repeat是从哪儿来呢?
fix #5000