Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add row conv operator #6013

Merged
merged 37 commits into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7183317
Add initial CPU version
sidgoyal78 Nov 22, 2017
9ecf3e6
Modify CPU version
sidgoyal78 Nov 28, 2017
838b161
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
sidgoyal78 Nov 28, 2017
65173bc
Add naive GPU version
sidgoyal78 Nov 29, 2017
5b43ad0
Remove type error for inp arguments
sidgoyal78 Nov 29, 2017
272a912
Remove update bug
sidgoyal78 Nov 29, 2017
543e2f3
Try size_t* for batch_indices
sidgoyal78 Nov 29, 2017
1b581f2
Fix variable name
sidgoyal78 Nov 29, 2017
ebc4f47
Fix minor <= bug
sidgoyal78 Nov 29, 2017
640b873
Add simplest dFilter implementation
sidgoyal78 Nov 29, 2017
261c972
Add simplest dX grad implementation
sidgoyal78 Nov 29, 2017
c425096
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
sidgoyal78 Nov 29, 2017
6ef6c0b
Check += error
sidgoyal78 Nov 29, 2017
dcb84af
Remove arg list error
sidgoyal78 Nov 29, 2017
9fed9d7
Add tid checks
sidgoyal78 Nov 29, 2017
0ac4c6f
Add single thread implementation
sidgoyal78 Nov 29, 2017
1d02152
Fix input
sidgoyal78 Nov 30, 2017
705cff7
Add better kernels for forward and dX computation
sidgoyal78 Nov 30, 2017
5b43f46
Modify blockDim and gridDim for dX kernel
sidgoyal78 Nov 30, 2017
99ae545
Add forward prop with shared memory
sidgoyal78 Nov 30, 2017
9cc579d
Fix variable name
sidgoyal78 Nov 30, 2017
d5a0040
Add pointer to sharedmem
sidgoyal78 Dec 1, 2017
84ed570
Add dX with shared memory
sidgoyal78 Dec 1, 2017
c9a1f96
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
sidgoyal78 Dec 1, 2017
0313069
Modify backprop for cpu version of dX
sidgoyal78 Dec 1, 2017
56cf035
Add better dW kernel
sidgoyal78 Dec 1, 2017
84926a0
Fix minor typo
sidgoyal78 Dec 1, 2017
a6d77cf
Fix index in dW
sidgoyal78 Dec 1, 2017
c5a793e
Try alt dX for cpu version
sidgoyal78 Dec 1, 2017
d6410e2
Add improved dW version
sidgoyal78 Dec 3, 2017
251ac0f
Fix errors
sidgoyal78 Dec 3, 2017
1974019
Fix variable dim for shared mem
sidgoyal78 Dec 3, 2017
29ab076
Fix dim indexing in dW improved gpu
sidgoyal78 Dec 3, 2017
025c32d
Fix documentation
sidgoyal78 Dec 3, 2017
2fbc0cb
Fix documentation
sidgoyal78 Dec 3, 2017
456cf13
Address review comments
sidgoyal78 Dec 5, 2017
2ae816a
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
sidgoyal78 Dec 5, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 246 additions & 0 deletions paddle/operators/row_conv_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
/* Copyright (c) 2017 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/row_conv_op.h"
#include "paddle/framework/eigen.h"

namespace paddle {
namespace operators {

using LoDTensor = framework::LoDTensor;
using framework::Tensor;

template <typename T, int MajorType = Eigen::RowMajor,
typename IndexType = Eigen::DenseIndex>
using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;

class RowConvOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;

void InferShape(framework::InferShapeContext *ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("X"),
"Input(X) of RowConvOp should not be null.");
PADDLE_ENFORCE(ctx->HasInput("Filter"),
"Input(Filter) of RowConvOp should not be null.");
PADDLE_ENFORCE(ctx->HasOutput("Out"),
"Output(Out) of RowConvOp should not be null.");

auto x_dims = ctx->GetInputDim("X");
auto filter_dims = ctx->GetInputDim("Filter");
PADDLE_ENFORCE_EQ(x_dims.size(), 2, "Input(X)'s rank should be 2.");
PADDLE_ENFORCE_EQ(filter_dims.size(), 2, "Input(Y)'s rank should be 2.");
PADDLE_ENFORCE_EQ(
x_dims[1], filter_dims[1],
"The 2nd dimension of Input(X) and Input(Filter) should be same.");
ctx->SetOutputDim("Out", x_dims);
ctx->ShareLoD("X", "Out");
}
};

class RowConvGradOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;

void InferShape(framework::InferShapeContext *ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null.");
PADDLE_ENFORCE(ctx->HasInput("Filter"),
"Input(Filter) should not be null.");
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
"Gradient of output(Out) should not be null.");

auto x_grad_name = framework::GradVarName("X");
if (ctx->HasOutput(x_grad_name)) {
auto x_dims = ctx->GetInputDim("X");
ctx->SetOutputDim(x_grad_name, x_dims);
}

auto filter_grad_name = framework::GradVarName("Filter");
if (ctx->HasOutput(filter_grad_name)) {
auto filter_dims = ctx->GetInputDim("Filter");
ctx->SetOutputDim(filter_grad_name, filter_dims);
}
}
};

class RowConvOpMaker : public framework::OpProtoAndCheckerMaker {
public:
RowConvOpMaker(framework::OpProto *proto,
framework::OpAttrChecker *op_checker)
: framework::OpProtoAndCheckerMaker(proto, op_checker) {
AddInput("X",
"(LoDTensor), the input(X) is a LodTensor, which supports "
"variable time-length input sequences. The underlying tensor "
"in this LoDTensor is a matrix with shape (T x N), where T "
"is the total time steps in this mini-batch and N is the input "
"data dimension.");
AddInput("Filter",
"(Tensor), the input(Filter) is a learnable parameter. It "
"is a 2-D tensor with shape (future_context x N), where, "
"future_context is the batch size and N is the data dimension.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your code, I think the name future_context is good :)

future_context is the batch size

future_context is the future context length.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks.

AddOutput("Out",
"(LoDTensor), the output(Out) is a LodTensor, which supports "
"variable time-length input sequences. The underlying tensor "
"in this LodTensor is a matrix with shape T x N, i.e., the "
"same shape as X.");
AddComment(R"DOC(
Row-convolution Operator.

This operator was introduced in the paper:
http://www.cs.cmu.edu/~dyogatam/papers/wang+etal.iclrworkshop2016.pdf
Given an input sequence $in$ of length $t$ and input dimension $d$,
and a filter ($W$) of size $context \times d$,
the output sequence is convolved in the following manner:

$$
out_{i, :} = \sum_{j=i}^{i + context} in_{j,:} \dot W_{i-j, :}
$$
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointer, i have included now.


)DOC");
}
};

template <typename T>
class RowConvKernel<platform::CPUPlace, T> : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext &context) const override {
auto *X = context.Input<LoDTensor>("X");
auto *Filter = context.Input<Tensor>("Filter");
auto *Out = context.Output<LoDTensor>("Out");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming style:
X->x
Filer->filter
Out->out

https://google.github.io/styleguide/cppguide.html#Variable_Names

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I need to fix for .cu code)


Out->mutable_data<T>(context.GetPlace());
context.ShareLoD("X", "Out");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is ctx->ShareLoD("X", "Out") in the InferShape, and the previous bug for ShareLoD in InferShape has been fixed, line 123 can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


auto batch_indices = X->lod()[0];
auto input_dim = X->dims()[1]; // 'in' is of size T x N
size_t num_sequence = batch_indices.size() - 1;

auto context_length = Filter->dims()[0];
auto weights = EigenMatrix<T>::From(*Filter);

for (size_t i = 0; i < num_sequence; i++) {
int start = static_cast<int>(batch_indices[i]);
int end = static_cast<int>(batch_indices[i + 1]);
int current_timesteps = end - start;
Tensor cur_input_sequence =
X->Slice(start, end); // Current input sequence
Tensor cur_output_sequence =
Out->Slice(start, end); // Current output sequence
auto cip_seq = EigenMatrix<T>::From(cur_input_sequence);
auto cot_seq = EigenMatrix<T>::From(cur_output_sequence);

for (int k = 0; k < current_timesteps;
k++) { // For different time steps in the same sequence
for (int w = 0; (w < context_length) && ((k + w) < current_timesteps);
w++) {
for (int d = 0; d < input_dim; d++) {
if (w == 0) {
cot_seq(k, d) = weights(w, d) * cip_seq(k + w, d);
} else {
cot_seq(k, d) += weights(w, d) * cip_seq(k + w, d);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use elementwise mul and col-wise sum to remove the for loop in line 145 and line 147. But the optimization can be done in the future. So in this PR, I think it is ok here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

}
}
}
}
}
};

template <typename T>
class RowConvGradKernel<platform::CPUPlace, T> : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext &context) const override {
auto *X = context.Input<LoDTensor>("X");
auto *Filter = context.Input<Tensor>("Filter");
auto *dOut = context.Input<LoDTensor>(framework::GradVarName("Out"));
auto *dX = context.Output<LoDTensor>(framework::GradVarName("X"));
auto *dFilter = context.Output<Tensor>(framework::GradVarName("Filter"));

auto input_dim = X->dims()[1]; // 'in' is of size T x N
auto batch_indices = X->lod()[0];
size_t num_sequence = batch_indices.size() - 1;
auto context_length = Filter->dims()[0];

if (dFilter) {
dFilter->mutable_data<T>(context.GetPlace());
auto dweights =
EigenMatrix<T>::From(*dFilter); // Gradient of weight matrix
dweights.setZero();

for (size_t i = 0; i < num_sequence; i++) { // For different sequences
int start = static_cast<int>(batch_indices[i]);
int end = static_cast<int>(batch_indices[i + 1]);

Tensor cur_input = X->Slice(start, end); // Current input sequence
Tensor cur_doutput =
dOut->Slice(start, end); // Current output grad sequence

auto cur_ip = EigenMatrix<T>::From(cur_input);
auto cur_dout = EigenMatrix<T>::From(cur_doutput);
int current_timesteps = end - start;

for (int k = 0; k < current_timesteps;
k++) { // For different time steps in the same sequence
for (int w = 0; (w < context_length) && ((k + w) < current_timesteps);
w++) {
// For dweights (Updating the gradient of weight matrix)
for (int d = 0; d < input_dim; d++) {
dweights(w, d) += cur_ip(k + w, d) * cur_dout(k, d);
}
}
}
}
}

if (dX) {
dX->mutable_data<T>(context.GetPlace());
auto weights = EigenMatrix<T>::From(*Filter);
for (size_t i = 0; i < num_sequence; i++) { // For different sequences
int start = static_cast<int>(batch_indices[i]);
int end = static_cast<int>(batch_indices[i + 1]);

Tensor cur_doutput =
dOut->Slice(start, end); // Current output grad sequence
Tensor cur_dinput =
dX->Slice(start, end); // Current input grad sequence

auto cur_dout = EigenMatrix<T>::From(cur_doutput);
auto cur_dip = EigenMatrix<T>::From(cur_dinput);
cur_dip.setZero();
int current_timesteps = end - start;

for (int k = 0; k < current_timesteps;
k++) { // For different time steps in the same sequence
for (int w = 0; (w < context_length) && ((k + w) < current_timesteps);
w++) {
// For dinput (Updating the gradient wrt input)
for (int d = 0; d < input_dim; d++) {
cur_dip(k + w, d) += weights(w, d) * cur_dout(k, d);
}
}
}
}
}
}
};
} // namespace operators
} // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP(row_conv, ops::RowConvOp, ops::RowConvOpMaker, row_conv_grad,
ops::RowConvGradOp);
REGISTER_OP_CPU_KERNEL(row_conv,
ops::RowConvKernel<paddle::platform::CPUPlace, float>);
REGISTER_OP_CPU_KERNEL(
row_conv_grad, ops::RowConvGradKernel<paddle::platform::CPUPlace, float>);
Loading