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]Support for i in [x,y,z] statements in dy2stat #37259

Merged
merged 4 commits into from
Nov 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
3 changes: 2 additions & 1 deletion python/paddle/fluid/dygraph/dygraph_to_static/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,8 @@ def is_for_range_iter(self):
gast.Name) and self.node.iter.func.id == "range"

def is_for_iter(self):
if isinstance(self.node.iter, (gast.Name, gast.Attribute)):
if isinstance(self.node.iter,
(gast.Name, gast.Attribute, gast.List, gast.Tuple)):
return True
elif isinstance(self.node.iter, gast.Call) and isinstance(
self.node.iter.func,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,24 @@ def forward(self, x):
return z


# 21. for original list
@paddle.jit.to_static
def for_original_list():
z = fluid.layers.fill_constant([1], 'int32', 0)
for x in [1, 2, 3]:
z = z + x
return z


# 22. for original tuple
@paddle.jit.to_static
def for_original_tuple():
z = fluid.layers.fill_constant([1], 'int32', 0)
for x in (1, 2, 3):
z = z + x
return z


class TestTransformBase(unittest.TestCase):
def setUp(self):
self.place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda(
Expand Down Expand Up @@ -344,6 +362,13 @@ def transformed_result_compare(self):
self.assertTrue(np.allclose(x.numpy(), y.numpy()))


class TestTransformForOriginalList(TestTransform):
def _run(self, to_static):
program_translator.enable(to_static)
with fluid.dygraph.guard():
return self.dygraph_func()


class TestTransformError(TestTransformBase):
def transformed_error(self, etype):
with self.assertRaises(etype):
Expand Down Expand Up @@ -471,5 +496,21 @@ def set_test_func(self):
self.dygraph_func = ForwardContainsForLayer()


class TestForOriginalList(TestTransformForOriginalList):
def set_test_func(self):
self.dygraph_func = for_original_list

def test_transformed_result_compare(self):
self.transformed_result_compare()


class TestForOriginalTuple(TestTransformForOriginalList):
def set_test_func(self):
self.dygraph_func = for_original_tuple

def test_transformed_result_compare(self):
self.transformed_result_compare()


if __name__ == '__main__':
unittest.main()