From ef2ee5e52ebf2ed8639b4279522676f6fff77929 Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Thu, 20 May 2021 19:44:25 +0800 Subject: [PATCH] [cherry-pick] BugFix StaticAanlysis with gast.Subscript (#32969) (#32903) (#32986) * [Custom Op]Remove PADDLE_WITH_MKLDNN in custom_op (#32903) * [Dy2Stat]BugFix StaticAanlysis with gast.Subscript (#32969) * BugFix StaticAanlysis with gast.Subscript * remove codes --- .../dygraph_to_static/static_analysis.py | 3 ++ .../unittests/dygraph_to_static/test_list.py | 39 +++++++++++++++++++ .../utils/cpp_extension/extension_utils.py | 4 -- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/static_analysis.py b/python/paddle/fluid/dygraph/dygraph_to_static/static_analysis.py index 4b3b9fcf298..cbe6b8a0ff9 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/static_analysis.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/static_analysis.py @@ -368,5 +368,8 @@ class StaticAnalysisVisitor(object): if isinstance(node.func, gast.Name): return self.var_env.get_var_type(node.func.id) + if isinstance(node, gast.Subscript): + if self.is_tensor_node(node.value): + return {NodeVarType.TENSOR} return {NodeVarType.STATEMENT} diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_list.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_list.py index 0243ef3a6dd..e630c2b9c6f 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_list.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_list.py @@ -16,6 +16,7 @@ from __future__ import print_function import unittest +import paddle import numpy as np import paddle.fluid as fluid from paddle.fluid.dygraph.jit import declarative @@ -61,6 +62,33 @@ def test_list_append_in_for_loop(x, iter_num): return a[0] +paddle.jit.set_code_level(100) + + +def test_list_append_in_for_subscript(x): + x = fluid.dygraph.to_variable(x) + iter_num = paddle.shape(x)[0] + a = [] + for i in range(iter_num): + x = x + 1 + a.append(x) + out = paddle.concat(a) + return out[0] + + +def test_list_append_in_while_loop_subscript(x): + x = fluid.dygraph.to_variable(x) + iter_num = paddle.shape(x)[0] + a = [] + i = 0 + while i < iter_num: + x = x + 1 + a.append(x) + i += 1 + out = paddle.concat(a) + return out[0] + + def test_list_append_in_for_loop_with_concat(x, iter_num): x = fluid.dygraph.to_variable(x) a = [] @@ -261,5 +289,16 @@ class TestListInForLoopWithConcat(TestListInWhileLoopWithStack): self.all_dygraph_funcs = [test_list_append_in_for_loop_with_concat, ] +class TestListInForLoopWithSubscript(TestListWithoutControlFlow): + def init_dygraph_func(self): + self.all_dygraph_funcs = [ + test_list_append_in_for_subscript, + test_list_append_in_while_loop_subscript + ] + + def init_data(self): + self.input = np.random.random((3, 4)).astype('float32') + + if __name__ == '__main__': unittest.main() diff --git a/python/paddle/utils/cpp_extension/extension_utils.py b/python/paddle/utils/cpp_extension/extension_utils.py index ea46ea8b391..104d979ef67 100644 --- a/python/paddle/utils/cpp_extension/extension_utils.py +++ b/python/paddle/utils/cpp_extension/extension_utils.py @@ -469,10 +469,6 @@ def normalize_extension_kwargs(kwargs, use_cuda=False): ########################### -- END -- ########################### add_compile_flag(extra_compile_args, ['-w']) # disable warning - # Note(Aurelius84): This marco will impact memory layout of `Tensor`. - # We align it automatically with pre-installed Paddle. - if core.is_compiled_with_mkldnn(): - add_compile_flag(extra_compile_args, ['-DPADDLE_WITH_MKLDNN']) if use_cuda: extra_link_args.append('-lcudart') -- GitLab