Skip to content

Commit b9ae1c4

Browse files
authored
Merge pull request #13994 from guoshengCS/add-reshape-reuse-input
[1.1] Make reshape_op reuse input.
2 parents dbe4df7 + cc0e239 commit b9ae1c4

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

paddle/fluid/API.spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ paddle.fluid.layers.softmax_with_cross_entropy ArgSpec(args=['logits', 'label',
107107
paddle.fluid.layers.smooth_l1 ArgSpec(args=['x', 'y', 'inside_weight', 'outside_weight', 'sigma'], varargs=None, keywords=None, defaults=(None, None, None))
108108
paddle.fluid.layers.one_hot ArgSpec(args=['input', 'depth'], varargs=None, keywords=None, defaults=None)
109109
paddle.fluid.layers.autoincreased_step_counter ArgSpec(args=['counter_name', 'begin', 'step'], varargs=None, keywords=None, defaults=(None, 1, 1))
110-
paddle.fluid.layers.reshape ArgSpec(args=['x', 'shape', 'actual_shape', 'act', 'inplace', 'name'], varargs=None, keywords=None, defaults=(None, None, True, None))
110+
paddle.fluid.layers.reshape ArgSpec(args=['x', 'shape', 'actual_shape', 'act', 'inplace', 'name'], varargs=None, keywords=None, defaults=(None, None, False, None))
111111
paddle.fluid.layers.squeeze ArgSpec(args=['input', 'axes', 'name'], varargs=None, keywords=None, defaults=(None,))
112112
paddle.fluid.layers.unsqueeze ArgSpec(args=['input', 'axes', 'name'], varargs=None, keywords=None, defaults=(None,))
113113
paddle.fluid.layers.lod_reset ArgSpec(args=['x', 'y', 'target_lod'], varargs=None, keywords=None, defaults=(None, None))

python/paddle/fluid/layers/nn.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4865,7 +4865,7 @@ def autoincreased_step_counter(counter_name=None, begin=1, step=1):
48654865
return counter
48664866

48674867

4868-
def reshape(x, shape, actual_shape=None, act=None, inplace=True, name=None):
4868+
def reshape(x, shape, actual_shape=None, act=None, inplace=False, name=None):
48694869
"""
48704870
Gives a new shape to the input Tensor without changing its data.
48714871
@@ -4913,15 +4913,22 @@ def reshape(x, shape, actual_shape=None, act=None, inplace=True, name=None):
49134913
:attr:`shape` specifying shape. That is to
49144914
say :attr:`actual_shape` has a higher priority
49154915
than :attr:`shape`.
4916-
act (str): The non-linear activation to be applied to output variable.
4917-
inplace(bool): If this flag is set true, the output
4918-
shares data with input without copying, otherwise
4919-
a new output tensor is created
4920-
whose data is copied from input x.
4916+
act (str): The non-linear activation to be applied to the reshaped tensor
4917+
variable.
4918+
inplace(bool): Must use :attr:`False` if :attr:`x` is used in multiple
4919+
operators. If this flag is set :attr:`True`, reuse input
4920+
:attr:`x` to reshape, which will change the shape of
4921+
tensor variable :attr:`x` and might cause errors when
4922+
:attr:`x` is used in multiple operators. If :attr:`False`,
4923+
preserve the shape :attr:`x` and create a new output tensor
4924+
variable whose data is copied from input x but reshaped.
49214925
name (str): The name of this layer. It is optional.
49224926
49234927
Returns:
4924-
Variable: The output tensor.
4928+
Variable: The reshaped tensor variable if :attr:`act` is None. It is a \
4929+
new tensor variable if :attr:`inplace` is :attr:`False`, \
4930+
otherwise it is :attr:`x`. If :attr:`act` is not None, return \
4931+
the activated tensor variable.
49254932
49264933
Raises:
49274934
TypeError: if actual_shape is neither Variable nor None.
@@ -4932,7 +4939,7 @@ def reshape(x, shape, actual_shape=None, act=None, inplace=True, name=None):
49324939
data = fluid.layers.data(
49334940
name='data', shape=[2, 4, 6], dtype='float32')
49344941
reshaped = fluid.layers.reshape(
4935-
x=data, shape=[-1, 0, 3, 2], act='tanh', inplace=True)
4942+
x=data, shape=[-1, 0, 3, 2], inplace=True)
49364943
"""
49374944

49384945
if not (isinstance(shape, list) or isinstance(shape, tuple)):
@@ -4959,7 +4966,8 @@ def reshape(x, shape, actual_shape=None, act=None, inplace=True, name=None):
49594966
"except one unknown dimension.")
49604967

49614968
helper = LayerHelper("reshape2", **locals())
4962-
out = helper.create_variable_for_type_inference(dtype=x.dtype)
4969+
out = x if inplace else helper.create_variable_for_type_inference(
4970+
dtype=x.dtype)
49634971
x_shape = helper.create_variable_for_type_inference(dtype=x.dtype)
49644972
helper.append_op(
49654973
type="reshape2",

0 commit comments

Comments
 (0)