logical_transformer.py 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
from paddle.utils import gast
16
from .utils import ast_to_source_code
17
from .base_transformer import (
18 19
    BaseTransformer,
)
20

21 22
__all__ = []

23 24 25 26 27 28 29 30 31 32
cmpop_type_to_str = {
    gast.Eq: "==",
    gast.NotEq: "!=",
    gast.Lt: "<",
    gast.LtE: "<=",
    gast.Gt: ">",
    gast.GtE: ">=",
    gast.Is: "is",
    gast.IsNot: "is not",
    gast.In: "in",
33
    gast.NotIn: "not in",
34 35 36 37 38 39
}


def cmpop_node_to_str(node):
    return cmpop_type_to_str[type(node)]

40

41
class LogicalTransformer(BaseTransformer):
42
    """
43 44 45 46 47 48
    Transform python boolean op into Paddle logical op.

    For example:
        a = x > 1 and y < 1

    Transformed code:
49
        a = _jst.And(lambda:x>1, lambda:y<1)
50 51 52 53 54 55 56 57 58 59 60 61 62
    """

    def __init__(self, wrapper_root):
        self.wrapper_root = wrapper_root
        self.root = wrapper_root.node

    def transform(self):
        return self.visit(self.root)

    def visit_UnaryOp(self, node):
        self.generic_visit(node)
        if isinstance(node.op, gast.Not):
            arg = ast_to_source_code(node.operand)
63
            new_node_str = "_jst.Not({})".format(arg)
64 65 66 67 68 69 70 71
            # NOTE: gast.parse returns Module(body=[expr(value=...)])
            new_node = gast.parse(new_node_str).body[0].value
            return new_node
        return node

    def visit_BoolOp(self, node):
        self.generic_visit(node)
        if isinstance(node.op, gast.And):
72
            new_node = self._create_bool_op_node(node.values, 'And')
73
        elif isinstance(node.op, gast.Or):
74
            new_node = self._create_bool_op_node(node.values, 'Or')
75 76
        else:
            raise TypeError(
77 78
                "Only supports and/or syntax in control flow if statement."
            )
79 80 81
        return new_node

    def _create_bool_op_node(self, nodes, api_type):
82 83 84 85 86 87
        '''
        NOTE(liym27):
           The arguments of function convert_logical_XX should be callable so that they can be run
          according to the actual order. In `convert_logical_and(lambda:x>1, lambda:y<1)`, `lambda:y<1`
          must be run after `lambda:x>1`, If `x>1` is False, `y<1` should NOT be run.
        '''
88 89 90 91 92
        assert (
            len(nodes) > 1
        ), "The length of BoolOp should be at least 2, but received {}.".format(
            len(nodes)
        )
93 94 95 96 97 98 99 100 101 102
        if len(nodes) > 2:
            # Creates logic_and/logic_or node recursively.
            pre_logic_node = self._create_bool_op_node(nodes[:2], api_type)
            if len(nodes[2:]) == 1:
                post_logic_node = nodes[2]
            else:
                post_logic_node = self._create_bool_op_node(nodes[2:], api_type)
            nodes = [pre_logic_node] + [post_logic_node]

        args = [ast_to_source_code(child) for child in nodes]
103
        new_node_str = "_jst.{}(lambda:{}, lambda:{})".format(
104 105
            api_type, args[0], args[1]
        )
106 107 108
        # NOTE: gast.parse return Module(body=[expr(...)])
        new_node = gast.parse(new_node_str).body[0].value
        return new_node