diff --git a/python/paddle/static/nn/control_flow.py b/python/paddle/static/nn/control_flow.py index c5d52654775418b822d767a18859dabb1c5eb63f..5e2e3add40cda182c9f470861e1130d85a4ed6b8 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 3248aa30103f7d1ffae9eb586b1e825f91280c0e..3533fbc7c237ba5e1d738dd329ce014f4f5d403f 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -2030,6 +2030,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( @@ -2038,7 +2042,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])