From fed2204517f4c6bc558ae2d3ff2cc69da4fb2ae2 Mon Sep 17 00:00:00 2001 From: guofei <52460041+gfwm2013@users.noreply.github.com> Date: Fri, 8 May 2020 14:19:10 +0800 Subject: [PATCH] Skip the process of copying LoDTensorArray in loop_vars in while_loop (#24271) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 目前在while_loop的执行过程中,loop_vars中的变量在每次的循环中都会进行拷贝,但是LoDTensorArray类型的变量在while循环体中已经完成了读/写的操作,即完成了更新,此时在进行拷贝属于冗余的操作,故该PR跳过每次循环中loop_vars中LoDTensorArray类型的变量的复制过程。 在PaddleCV/ocr_recognition/atention模型的预测过程中进行性能测试: |性能|with this PR|without this PR|提升| |---|---|---|---| |速度|4957.4ms|4978.47ms|0.4%| --- python/paddle/fluid/layers/control_flow.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py index 31430a1f321..e35576056e9 100755 --- a/python/paddle/fluid/layers/control_flow.py +++ b/python/paddle/fluid/layers/control_flow.py @@ -979,6 +979,14 @@ class While(object): "is_test": self.is_test}) +def assign_skip_lod_tensor_array(inputs, outputs): + """ + Skip the process of copying LoDTensorArray. + """ + if inputs.type != core.VarDesc.VarType.LOD_TENSOR_ARRAY: + assign(inputs, outputs) + + def while_loop(cond, body, loop_vars, is_test=False, name=None): """ while_loop is one of the control flows. Repeats while_loop `body` until `cond` returns False. @@ -1066,7 +1074,7 @@ def while_loop(cond, body, loop_vars, is_test=False, name=None): "body in while_loop should return the same arity " "(length and structure) and types as loop_vars") now_cond = cond(*output_vars).numpy()[0] - loop_vars = output_vars + map_structure(assign_skip_lod_tensor_array, output_vars, loop_vars) return loop_vars while_loop_block = While(pre_cond, is_test, name) @@ -1090,7 +1098,7 @@ def while_loop(cond, body, loop_vars, is_test=False, name=None): "(length and structure) as loop_vars: {0}".format( e)) now_cond = cond(*output_vars) - map_structure(assign, output_vars, loop_vars) + map_structure(assign_skip_lod_tensor_array, output_vars, loop_vars) assign(now_cond, pre_cond) return loop_vars -- GitLab