Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dy2Stat]BugFix StaticAanlysis with gast.Subscript #32969

Merged
merged 2 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -368,5 +368,8 @@ def _get_node_var_type(self, cur_wrapper):

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}
39 changes: 39 additions & 0 deletions python/paddle/fluid/tests/unittests/dygraph_to_static/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import unittest

import paddle
import numpy as np
import paddle.fluid as fluid
from paddle.fluid.dygraph.jit import declarative
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -261,5 +289,16 @@ def init_dygraph_func(self):
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()