未验证 提交 fed22045 编写于 作者: G guofei 提交者: GitHub

Skip the process of copying LoDTensorArray in loop_vars in while_loop (#24271)

目前在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%|
上级 f62dfc62
...@@ -979,6 +979,14 @@ class While(object): ...@@ -979,6 +979,14 @@ class While(object):
"is_test": self.is_test}) "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): 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. 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): ...@@ -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 " "body in while_loop should return the same arity "
"(length and structure) and types as loop_vars") "(length and structure) and types as loop_vars")
now_cond = cond(*output_vars).numpy()[0] 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 return loop_vars
while_loop_block = While(pre_cond, is_test, name) 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): ...@@ -1090,7 +1098,7 @@ def while_loop(cond, body, loop_vars, is_test=False, name=None):
"(length and structure) as loop_vars: {0}".format( "(length and structure) as loop_vars: {0}".format(
e)) e))
now_cond = cond(*output_vars) 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) assign(now_cond, pre_cond)
return loop_vars return loop_vars
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册