From d4ecfe5fa4c1837056ef1da30f93c87b20e76a97 Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Fri, 5 May 2023 19:04:30 +0800 Subject: [PATCH] [Perf]Removed usless assign op in while_loop (#53452) (#53492) * [Perf]Removed usless assign op in while_loop * refine assign --- python/paddle/static/nn/control_flow.py | 4 ++++ python/paddle/tensor/creation.py | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/python/paddle/static/nn/control_flow.py b/python/paddle/static/nn/control_flow.py index c5d52654775..5e2e3add40c 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 3248aa30103..3533fbc7c23 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]) -- GitLab