-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Enhance reshape #9008
Conversation
4caf848
to
8bfaf07
Compare
8bfaf07
to
cf08185
Compare
Is there some problem in CI? |
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.
BTW, any specially consideration to move this operator from ops
to nn
?
paddle/fluid/operators/reshape_op.cc
Outdated
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 |
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.
canbe
-> can be
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.
Done.
paddle/fluid/operators/reshape_op.cc
Outdated
} | ||
} | ||
PADDLE_ENFORCE_LE( | ||
neg_dims_idx.size(), 1, | ||
"Only one input dimension of Attr(shape) may be unknown."); |
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.
may
-> can
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.
Done.
paddle/fluid/operators/reshape_op.cc
Outdated
@@ -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"); |
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.
Need to explain when the dimension can be set to 0 (necessary) and in-place reshape (optional) in the doc.
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.
Done.
paddle/fluid/operators/reshape_op.cc
Outdated
"(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.") |
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.
copy
-> copying
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.
Done.
python/paddle/fluid/layers/nn.py
Outdated
|
||
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. |
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 need to refine this doc
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.
Done.
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.
follow comments.
paddle/fluid/operators/reshape_op.cc
Outdated
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 |
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.
Done.
paddle/fluid/operators/reshape_op.cc
Outdated
} | ||
} | ||
PADDLE_ENFORCE_LE( | ||
neg_dims_idx.size(), 1, | ||
"Only one input dimension of Attr(shape) may be unknown."); |
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.
Done.
paddle/fluid/operators/reshape_op.cc
Outdated
"(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.") |
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.
Done.
python/paddle/fluid/layers/nn.py
Outdated
|
||
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. |
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.
Done.
paddle/fluid/operators/reshape_op.cc
Outdated
@@ -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"); |
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.
Done.
46f2a03
to
abae95c
Compare
@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. |
abae95c
to
a6e6424
Compare
This update expects to make
|
python/paddle/fluid/layers/nn.py
Outdated
|
||
Examples: | ||
.. code-block:: python | ||
data = fluid.layers.data(name='data', shape=[2, 4, 6], dtype='float32') |
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.
Exceed 80 columns.
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.
Done.
@lcy-seso Thx! I get the point. Please let me know if this pr is ready. |
Refine by following comments and ready to be reviewed again. Thanks. |
This update adds an optional input |
paddle/fluid/operators/reshape_op.cc
Outdated
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 |
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.
1 --> 2 --> 3 ... Please also modify the doc the python API
paddle/fluid/operators/reshape_op.cc
Outdated
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 |
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.
1 --> 2 --> 3
paddle/fluid/operators/reshape_op.h
Outdated
int64_t capacity = 1; | ||
int unk_dim_idx = -1; | ||
for (size_t i = 0; i < shape.size(); ++i) { | ||
// std::cout<< shape[i] << "haha"; |
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 this line
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
fixes #8781