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

Enhance reshape #9008

Merged
merged 15 commits into from
Apr 2, 2018
Merged

Enhance reshape #9008

merged 15 commits into from
Apr 2, 2018

Conversation

lcy-seso
Copy link
Contributor

@lcy-seso lcy-seso commented Mar 12, 2018

fixes #8781

@lcy-seso lcy-seso force-pushed the enhance_reshape branch 7 times, most recently from 4caf848 to 8bfaf07 Compare March 12, 2018 16:52
@kuke
Copy link
Contributor

kuke commented Mar 14, 2018

Is there some problem in CI?

Copy link
Contributor

@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.

BTW, any specially consideration to move this operator from ops to nn?

bool ValidateShape(const std::vector<int> &shape,
const framework::DDim &input_dim,
std::vector<int64_t> &output_shape) const {
// only one dimension canbe set to -1, whose size will be automatically
Copy link
Contributor

Choose a reason for hiding this comment

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

canbe -> can be

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.

}
}
PADDLE_ENFORCE_LE(
neg_dims_idx.size(), 1,
"Only one input dimension of Attr(shape) may be unknown.");
Copy link
Contributor

Choose a reason for hiding this comment

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

may -> can

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.

@@ -99,7 +127,7 @@ and target shape = [1, 4], the reshape operator will transform
the tensor X into a 2-D tensor: [[1, 2, 3, 4]]

One dimension in the target shape can be set -1, representing that its
size is unknown. In this case, the real dimension will be infered from
size is unknown. In this case, the real dimension will be infered from
the original shape of Input(X) and other dimensions in the target 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.

Need to explain when the dimension can be set to 0 (necessary) and in-place reshape (optional) in the 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.

"(vector<int>) "
"Target shape of reshape operator.");
AddAttr<std::vector<int>>(
"shape", "(std::vector<int>) Target shape of reshape operator.");
AddAttr<bool>("inplace",
"Change the source tensor's shape without copy memory.")
Copy link
Contributor

Choose a reason for hiding this comment

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

copy -> copying

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.


Given a 3-D tensor x with shape [2, 3, 8] and the new shape:
[-1, 0, 2, 2]. The reshape layer will change tensor X into a 4-D tensor
with shape [4, 3, 2, 2] with its data unchanged.
Copy link
Contributor

Choose a reason for hiding this comment

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

Also need to refine this 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.

Copy link
Contributor Author

@lcy-seso lcy-seso left a comment

Choose a reason for hiding this comment

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

follow comments.

bool ValidateShape(const std::vector<int> &shape,
const framework::DDim &input_dim,
std::vector<int64_t> &output_shape) const {
// only one dimension canbe set to -1, whose size will be automatically
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.

}
}
PADDLE_ENFORCE_LE(
neg_dims_idx.size(), 1,
"Only one input dimension of Attr(shape) may be unknown.");
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.

"(vector<int>) "
"Target shape of reshape operator.");
AddAttr<std::vector<int>>(
"shape", "(std::vector<int>) Target shape of reshape operator.");
AddAttr<bool>("inplace",
"Change the source tensor's shape without copy memory.")
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.


Given a 3-D tensor x with shape [2, 3, 8] and the new shape:
[-1, 0, 2, 2]. The reshape layer will change tensor X into a 4-D tensor
with shape [4, 3, 2, 2] with its data unchanged.
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.

@@ -99,7 +127,7 @@ and target shape = [1, 4], the reshape operator will transform
the tensor X into a 2-D tensor: [[1, 2, 3, 4]]

One dimension in the target shape can be set -1, representing that its
size is unknown. In this case, the real dimension will be infered from
size is unknown. In this case, the real dimension will be infered from
the original shape of Input(X) and other dimensions in the target 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.

@lcy-seso lcy-seso force-pushed the enhance_reshape branch 4 times, most recently from 46f2a03 to abae95c Compare March 19, 2018 05:06
@lcy-seso
Copy link
Contributor Author

lcy-seso commented Mar 19, 2018

@kuke Reply to the previous comment about "why remove this operator from ops into nn".

Actually, my only consideration is to generate a more accurate documentation for users. Currently, our API documents are generated by Python API helpers. Helpers for layers in ops.py are automatically generated, and their documentation is from the C++ codes (including comments for inputs, outputs, attributes and op comments written in C++ codes). The display format is hard to control, and parameter's data type is not correct for users who use python APIs.

@guoshengCS
Copy link
Contributor

This update expects to make InferShape suitable to both compile-time and run-time. Specifically, InferShape will act as follows:

  1. For each dimension size of the output, copy the corresponding dimension size of input if the given value of that dimension is 0, otherwise, use the given value as that dimension size. And multiply the dimension sizes to compute capacity.
  2. If unknown dimension exists, divide the product of input dimension sizes by the capacity computed before to infer the unknown dimension size.


Examples:
.. code-block:: python
data = fluid.layers.data(name='data', shape=[2, 4, 6], dtype='float32')
Copy link
Contributor

Choose a reason for hiding this comment

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

Exceed 80 columns.

Copy link
Contributor

Choose a reason for hiding this comment

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

Done.

@kuke
Copy link
Contributor

kuke commented Mar 22, 2018

@lcy-seso Thx! I get the point. Please let me know if this pr is ready.

@guoshengCS
Copy link
Contributor

Refine by following comments and ready to be reviewed again. Thanks.

@guoshengCS
Copy link
Contributor

This update adds an optional input Shape acting as the target shape at runtime. If Shape is provided, the actual output shape will be this rather than the attribute specifying shape. The rules of copying dimension sizes and unknown dimension inference is same for Input(Shape) and Attr(shape).

specified by Attr(shape) is [6, 8], the reshape operator will transform Input(X)
into a 2-D tensor with shape [6, 8] and leaving Input(X)'s data unchanged.

1. Given a 3-D tensor Input(X) with a shape [2, 4, 6], and the target shape
Copy link
Contributor

@kuke kuke Mar 29, 2018

Choose a reason for hiding this comment

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

1 --> 2 --> 3 ... Please also modify the doc the python API

1. One and only one dimension in Attr(shape) can be set -1. In this case,
the actual dimension value will be infered from the total element number of
Input(X) and remaining dimensions.
1. More than one dimensions in Attr(shape) can be set to 0, which means the real
Copy link
Contributor

Choose a reason for hiding this comment

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

1 --> 2 --> 3

int64_t capacity = 1;
int unk_dim_idx = -1;
for (size_t i = 0; i < shape.size(); ++i) {
// std::cout<< shape[i] << "haha";
Copy link
Contributor

Choose a reason for hiding this comment

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

remove this line

Copy link
Contributor

@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.

LGTM

@guoshengCS guoshengCS merged commit d908c3b into PaddlePaddle:develop Apr 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

enhance reshape operator to determine shape of its output at runtime.
4 participants