diff --git a/python/paddle/static/nn/control_flow.py b/python/paddle/static/nn/control_flow.py index bc5f1d2d5d6f52fc38c6c91a5c0f87e948e784fe..79cc848804bdc7d005ef3e2a8232045b2fa00f91 100644 --- a/python/paddle/static/nn/control_flow.py +++ b/python/paddle/static/nn/control_flow.py @@ -395,6 +395,10 @@ def assign_skip_lod_tensor_array(input, output): input.shape, output.shape ) ) + # NOTE(dev): Avoid assign if input is output in Variable level which means + # input is not generated in While sub block and modified by in-place and only + # belong to inplace ops in constructing program process, because in-place pass + # is only available in Graph level. paddle.assign(input, output) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index c57fceeeb85252f1691416bccf25aa4b99a5ccbd..db1006b0b1af047126511c1226003254ab73ebd7 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -2029,6 +2029,10 @@ def assign(x, output=None): result2 = paddle.assign(data) # result2 = [[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]] result3 = paddle.assign(np.array([[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]], dtype='float32')) # result3 = [[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]] """ + # speed up + if x is output and isinstance(x, Variable): + return x + input = x helper = LayerHelper('assign', **locals()) check_type( @@ -2037,7 +2041,6 @@ def assign(x, output=None): (Variable, np.ndarray, list, tuple, float, int, bool), 'assign', ) - is_inplace = True if output is not None else False if np.isscalar(input) and not isinstance(input, str): input = np.array([input])