From afa3f9b3c76573b7bddc4ce9963e96a701a7ac45 Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Thu, 4 May 2023 09:41:19 +0800 Subject: [PATCH] [Perf]Removed usless assign op in while_loop (#53452) * [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 bc5f1d2d5d6..79cc848804b 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 c57fceeeb85..db1006b0b1a 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]) -- GitLab