From be0ec9043035afd7058d777b030a6793df5d3d0f Mon Sep 17 00:00:00 2001 From: xiongkun Date: Tue, 2 Aug 2022 19:55:25 +0800 Subject: [PATCH] [ Dy2Static ] Remove assign split (#44769) * just a test * remove split assign test * remove other useless code related to split assign --- .../dygraph_to_static/base_transformer.py | 82 ------------------- .../dygraph_to_static/list_transformer.py | 2 - .../unittests/dygraph_to_static/test_utils.py | 9 -- 3 files changed, 93 deletions(-) diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/base_transformer.py b/python/paddle/fluid/dygraph/dygraph_to_static/base_transformer.py index 9df7e8d9b4f..ff5b5220952 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/base_transformer.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/base_transformer.py @@ -186,88 +186,6 @@ class ForLoopTuplePreTransformer(BaseTransformer): return [assign_node] -class SplitAssignTransformer(BaseTransformer): - """ - This class transforms sequence assignments and multi-target assignments to normal assignments. - """ - - def __init__(self, ast_node): - assert isinstance(ast_node, gast.AST) - self.ast_root = ast_node - - def transform(self): - self.visit(self.ast_root) - - def visit_Assign(self, node): - target_nodes = node.targets - if len(target_nodes) == 1: - node = self._parse_sequence_assign(node) - else: - node = self._parse_multi_target_assign(node) - return node - - def _parse_sequence_assign(self, node): - """ - a, b = c, d - -> - a = c - b = d - """ - assert isinstance(node, gast.Assign) - - target_nodes = node.targets - value_node = node.value - if not isinstance(target_nodes[0], (gast.List, gast.Tuple)): - return node - if not isinstance(value_node, (gast.List, gast.Tuple)): - return node - - targets = node.targets[0].elts - values = node.value.elts - if len(targets) != len(values): - return node - - new_nodes = [] - for target, value in zip(targets, values): - assign_node = gast.Assign(targets=[target], value=value) - new_nodes.append(assign_node) - - return new_nodes - - def _parse_multi_target_assign(self, node): - """ - Example 1: - a = b = c - -> - b = c - a = b - - Example 2: - a, b = c, d = x - -> - c,d = x - a = c - b = d - """ - assert isinstance(node, gast.Assign) - - target_nodes = node.targets - value_node = node.value - new_nodes = [] - for target in reversed(target_nodes): - assign_node = gast.Assign(targets=[target], value=value_node) - # NOTE: Because assign_node can be sequence assign statement like `a,b = c,d`, - # it's necessary to visit this new assign_node - parsed_node = self.visit_Assign(assign_node) - if not isinstance(parsed_node, list): - parsed_node = [parsed_node] - - new_nodes.extend(parsed_node) - value_node = target - - return new_nodes - - class ForNodeVisitor(object): """ This class parses python for statement, get transformed 3 statement components of for node diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/list_transformer.py b/python/paddle/fluid/dygraph/dygraph_to_static/list_transformer.py index 29e3ed52968..33ec9b9be73 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/list_transformer.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/list_transformer.py @@ -22,7 +22,6 @@ from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_source_code from paddle.fluid.dygraph.dygraph_to_static.utils import slice_is_num from paddle.fluid.dygraph.dygraph_to_static.utils import is_control_flow_to_transform from paddle.fluid.dygraph.dygraph_to_static.base_transformer import BaseTransformer -from paddle.fluid.dygraph.dygraph_to_static.base_transformer import SplitAssignTransformer class ListTransformer(BaseTransformer): @@ -47,7 +46,6 @@ class ListTransformer(BaseTransformer): self.scope_var_type_dict = var_env.get_scope_var_type() def transform(self): - SplitAssignTransformer(self.root).transform() self.visit(self.root) self.replace_list_with_tensor_array(self.root) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_utils.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_utils.py index 6f4fe613db7..b2943c779ca 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_utils.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_utils.py @@ -57,15 +57,6 @@ class StaticCode(): y = n -class TestSplitAssignTransformer(unittest.TestCase): - - def test_code(self): - answer = get_source_code(StaticCode.dyfunc_assign) - program_translator = ProgramTranslator() - code = program_translator.get_code(dyfunc_assign) - self.assertEqual(answer, code) - - class TestIsPaddle(unittest.TestCase): def fake_module(self): -- GitLab