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 reshape operator #3949

Merged
merged 10 commits into from
Sep 12, 2017
Merged

Add reshape operator #3949

merged 10 commits into from
Sep 12, 2017

Conversation

kuke
Copy link
Contributor

@kuke kuke commented Sep 7, 2017

Resolve #4009

void InferShape(const framework::InferShapeContext &ctx) const override {
auto *in = ctx.Input<framework::Tensor>("X");
auto shape = ctx.Attr<std::vector<int>>("shape");
PADDLE_ENFORCE_EQ((unsigned)shape.size(), in->dims().size(),
Copy link
Contributor

Choose a reason for hiding this comment

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

unsigned long please. the size returned by ddim is ssize_t, which is 8byte width.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this unreasonable line.

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
Copy link
Contributor

Choose a reason for hiding this comment

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

do not use using in the header file, according to the google style.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done



class ReshapeGradOpTest(GradientChecker):
def test_normal(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

now can use the function setUp to generate forward operator.
please take a look at this case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

out->mutable_data<T>(ctx.GetPlace());

auto shape = ctx.Attr<std::vector<int>>("shape");
std::vector<int64_t> tmp;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please do not mix use of int64_t and size_t, which is ugly for users who read our code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@qingqing01 qingqing01 requested a review from Superjomn September 9, 2017 02:28
Copy link
Contributor Author

@kuke kuke left a comment

Choose a reason for hiding this comment

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

Refine this PR by following the review comments. Please continue to review.

void InferShape(const framework::InferShapeContext &ctx) const override {
auto *in = ctx.Input<framework::Tensor>("X");
auto shape = ctx.Attr<std::vector<int>>("shape");
PADDLE_ENFORCE_EQ((unsigned)shape.size(), in->dims().size(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this unreasonable line.

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

out->mutable_data<T>(ctx.GetPlace());

auto shape = ctx.Attr<std::vector<int>>("shape");
std::vector<int64_t> tmp;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done



class ReshapeGradOpTest(GradientChecker):
def test_normal(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@kuke kuke requested review from qingqing01 and reyoung September 11, 2017 08:11
@kuke kuke changed the title [WIP]Add reshape operator Add reshape operator Sep 11, 2017
dzhwinter
dzhwinter previously approved these changes Sep 11, 2017
Copy link
Contributor

@dzhwinter dzhwinter left a comment

Choose a reason for hiding this comment

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

Seems LGTM.
Maybe @qingqing01 have more comment.


protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
auto *in = ctx.Input<framework::Tensor>("X");
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.

Done

} else {
capacity *= dim;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

int64_t capacity = 1;
for (auto dim : shape) {
  PADDLE_ENFORCE(dim > 0, "Each dimension of shape must be positive.");
  capacity *= dim;
}

or use std::accumulate :

int64_t  capacity = std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<int>());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

int64_t in_size = framework::product(in->dims());
PADDLE_ENFORCE_EQ(capacity, in_size,
"The size of Input(X) mismatches with Attr(shape).");
ctx.Output<framework::Tensor>("Out")->Resize(in->dims());
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not Resize(shape)? The dims of output is not shape ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified

AddComment(R"DOC(Reshape operator

Reshape Input(X) into the shape specified by Attr(shape).
)DOC");
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.

Done

: OperatorWithKernel(type, inputs, outputs, attrs) {}

protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to check nonempty for the inputs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor Author

@kuke kuke left a comment

Choose a reason for hiding this comment

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

updated, please continue to review @qingqing01

: OperatorWithKernel(type, inputs, outputs, attrs) {}

protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

int64_t in_size = framework::product(in->dims());
PADDLE_ENFORCE_EQ(capacity, in_size,
"The size of Input(X) mismatches with Attr(shape).");
ctx.Output<framework::Tensor>("Out")->Resize(in->dims());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified

} else {
capacity *= dim;
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
auto *in = ctx.Input<framework::Tensor>("X");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

AddComment(R"DOC(Reshape operator

Reshape Input(X) into the shape specified by Attr(shape).
)DOC");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@kuke kuke merged commit 4137cb0 into PaddlePaddle:develop Sep 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants