static_analysis.py 16.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2019 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 .logging_utils import warn
17 18 19 20 21 22 23
from .utils import (
    is_paddle_api,
    is_dygraph_api,
    is_numpy_api,
    index_in_list,
    ast_to_source_code,
)
24

25
__all__ = []
26 27


28
class NodeVarType:
29 30 31 32 33 34
    """
    Enum class of python variable types. We have to know some variable types
    during compile time to transfer AST. For example, a string variable and a
    tensor variable in if clause may lead to different conversion from dygraph
    to static graph.
    """
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    ERROR = -1  # Returns when static analysis gets error
    UNKNOWN = 0  # Reserve for AST nodes have not known the type
    STATEMENT = 1  # For nodes representing statement (non-variable type)
    CALLABLE = 2

    # python data types
    NONE = 100
    BOOLEAN = 101
    INT = 102
    FLOAT = 103
    STRING = 104
    TENSOR = 105
    NUMPY_NDARRAY = 106

    # python collections
    LIST = 200
    SET = 201
    DICT = 202

    PADDLE_DYGRAPH_API = 300
    PADDLE_CONTROL_IF = 301
    PADDLE_CONTROL_WHILE = 302
    PADDLE_CONTROL_FOR = 303
59 60 61
    # Paddle API may not be visible to get source code.
    # We use this enum value to denote the type return by a Paddle API
    PADDLE_RETURN_TYPES = 304
62

63 64 65
    # If node.node_var_type in TENSOR_TYPES, it can be considered as tensor-dependent.
    TENSOR_TYPES = {TENSOR, PADDLE_RETURN_TYPES}

66 67 68 69 70 71
    Annotation_map = {
        "Tensor": TENSOR,
        "paddle.Tensor": TENSOR,
        "int": INT,
        "float": FLOAT,
        "bool": BOOLEAN,
72
        "str": STRING,
73 74
    }

75 76 77 78 79 80 81 82 83 84 85
    @staticmethod
    def binary_op_output_type(in_type1, in_type2):
        if in_type1 == in_type2:
            return in_type1

        if in_type1 == NodeVarType.UNKNOWN:
            return in_type2
        if in_type2 == NodeVarType.UNKNOWN:
            return in_type1

        supported_types = [
86 87 88 89 90 91
            NodeVarType.BOOLEAN,
            NodeVarType.INT,
            NodeVarType.FLOAT,
            NodeVarType.NUMPY_NDARRAY,
            NodeVarType.TENSOR,
            NodeVarType.PADDLE_RETURN_TYPES,
92 93 94 95 96 97 98 99 100 101 102 103
        ]

        if in_type1 not in supported_types:
            return NodeVarType.UNKNOWN
        if in_type2 not in supported_types:
            return NodeVarType.UNKNOWN

        forbidden_types = [NodeVarType.NUMPY_NDARRAY, NodeVarType.TENSOR]
        if in_type1 in forbidden_types and in_type2 in forbidden_types:
            return NodeVarType.UNKNOWN
        return max(in_type1, in_type2)

104 105 106 107 108 109 110 111 112 113
    @staticmethod
    def type_from_annotation(annotation):
        annotation_str = ast_to_source_code(annotation).strip()
        if annotation_str in NodeVarType.Annotation_map:
            return NodeVarType.Annotation_map[annotation_str]

        # raise warning if not found
        warn("Currently we don't support annotation: %s" % annotation_str)
        return NodeVarType.UNKNOWN

114

115
class AstNodeWrapper:
116 117 118 119 120 121 122 123 124 125 126
    """
    Wrapper for python gast.node. We need a node wrapper because gast.node
    doesn't store all required information when we are transforming AST.
    We should collect additional information which the actual transformation
    needs.
    """

    def __init__(self, node):
        self.node = node
        self.parent = None
        self.children = []
127
        self.node_var_type = {NodeVarType.UNKNOWN}
128 129


130
class AstVarScope:
131 132
    """
    AstVarScope is a class holding the map from current scope variable to its
133
    type.
134
    """
135

136 137 138 139
    SCOPE_TYPE_SCRIPT = 0
    SCOPE_TYPE_FUNCTION = 1
    SCOPE_TYPE_CLASS = 2

140 141 142
    def __init__(
        self, scope_name='', scope_type=SCOPE_TYPE_SCRIPT, parent_scope=None
    ):
143 144 145 146
        self.sub_scopes = []
        self.name_to_id = {}
        self.id_to_type = {}
        self.cur_id = 0
147 148 149

        self.scope_name = scope_name
        self.scope_type = scope_type
150 151 152 153
        self.parent_scope = parent_scope
        if parent_scope is not None:
            parent_scope.sub_scopes.append(self)

154 155 156 157 158 159 160 161 162 163
    def add_var_type(self, var_name, node_var_type):
        var_type = self.get_var_type(var_name)
        if var_type == {NodeVarType.UNKNOWN}:
            self.set_var_type(var_name, node_var_type)
        else:
            if isinstance(node_var_type, set):
                var_type.update(node_var_type)
            else:
                var_type.add(node_var_type)

164 165 166 167 168 169 170
    def set_var_type(self, var_name, node_var_type):
        if var_name in self.name_to_id:
            num_id = self.name_to_id[var_name]
        else:
            num_id = self.cur_id
            self.cur_id += 1
            self.name_to_id[var_name] = num_id
171 172 173
        self.id_to_type[num_id] = (
            node_var_type if isinstance(node_var_type, set) else {node_var_type}
        )
174 175 176 177 178 179

    def get_var_type(self, var_name):
        if var_name in self.name_to_id:
            num_id = self.name_to_id[var_name]
            return self.id_to_type[num_id]
        if self.parent_scope is None:
180
            return {NodeVarType.UNKNOWN}
181 182 183
        return self.parent_scope.get_var_type(var_name)


184
class AstVarEnv:
185
    """
186
    A class maintains scopes and mapping from name strings to type.
187 188 189 190 191
    """

    def __init__(self):
        self.cur_scope = AstVarScope()

192
    def enter_scope(self, scope_name, scope_type):
193 194 195
        self.cur_scope = AstVarScope(
            scope_name, scope_type, parent_scope=self.cur_scope
        )
196 197 198
        return self.cur_scope

    def exit_scope(self):
199 200
        assert self.cur_scope.parent_scope is not None, (
            "Call exit_scope in "
201
            "AstVarEnv when current scope doesn't have parent scope."
202
        )
203 204 205
        self.cur_scope = self.cur_scope.parent_scope
        return self.cur_scope

206
    def get_parent_scope(self):
207 208
        assert self.cur_scope.parent_scope is not None, (
            "Call parent_scope in "
209
            "AstVarEnv when current scope doesn't have parent scope."
210
        )
211 212 213 214 215
        return self.cur_scope.parent_scope

    def add_var_type(self, var_name, node_var_type):
        self.cur_scope.add_var_type(var_name, node_var_type)

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    def set_var_type(self, var_name, node_var_type):
        self.cur_scope.set_var_type(var_name, node_var_type)

    def get_var_type(self, var_name):
        return self.cur_scope.get_var_type(var_name)

    def get_scope_var_type(self):
        '''
        Returns a dict mapping from variable name to type. Used for debug and
        test.
        '''
        cur_scope_dict = {}
        for name in self.cur_scope.name_to_id:
            node_var_type = self.cur_scope.get_var_type(name)
            cur_scope_dict[name] = node_var_type
        return cur_scope_dict


234
class StaticAnalysisVisitor:
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    """
    A class that does static analysis
    """

    def __init__(self, ast_root=None):
        if ast_root is not None:
            self.run(ast_root)

    def run(self, ast_root):
        self.node_wrapper_root = None
        self.ancestor_wrappers = []
        self.node_to_wrapper_map = {}
        self.var_env = AstVarEnv()

        self.dfs_visit(ast_root)

    def dfs_visit(self, node):
        # AST reuses some gast.nodes, such as Param node of expr_context
        if node not in self.node_to_wrapper_map:
            cur_wrapper = AstNodeWrapper(node)
            self.node_to_wrapper_map[node] = cur_wrapper
        else:
            cur_wrapper = self.node_to_wrapper_map[node]

        if self.node_wrapper_root is None:
            self.node_wrapper_root = cur_wrapper

        if len(self.ancestor_wrappers) != 0:
            last_wrapper = self.ancestor_wrappers[-1]
            last_wrapper.children.append(cur_wrapper)
            cur_wrapper.parent = last_wrapper

        self.ancestor_wrappers.append(cur_wrapper)
        for child in gast.iter_child_nodes(node):
269
            if isinstance(child, gast.FunctionDef) or isinstance(
270 271
                child, gast.AsyncFunctionDef
            ):
272 273
                # TODO: current version is function name mapping to its type
                # consider complex case involving parameters
274 275 276
                self.var_env.enter_scope(
                    child.name, AstVarScope.SCOPE_TYPE_FUNCTION
                )
277 278 279 280
                func_type = self.dfs_visit(child)
                self.var_env.exit_scope()
            else:
                self.dfs_visit(child)
281 282 283 284 285 286 287 288 289 290 291 292 293 294
        self.ancestor_wrappers.pop()

        cur_wrapper.node_var_type = self._get_node_var_type(cur_wrapper)
        return cur_wrapper.node_var_type

    def get_node_wrapper_root(self):
        return self.node_wrapper_root

    def get_node_to_wrapper_map(self):
        return self.node_to_wrapper_map

    def get_var_env(self):
        return self.var_env

295 296 297 298 299 300 301 302
    def is_tensor_node(self, node):
        tensor_types = {NodeVarType.TENSOR, NodeVarType.PADDLE_RETURN_TYPES}
        node_wrapper = self.node_to_wrapper_map.get(node, None)
        if node_wrapper is None:
            return False
        if node_wrapper.node_var_type & tensor_types:
            return True

303
    def _get_constant_node_type(self, node):
304 305 306 307
        assert isinstance(node, gast.Constant), (
            "Type of input node should be gast.Constant, but received %s"
            % type(node)
        )
308 309 310 311 312 313 314 315 316 317 318 319 320 321
        # singleton: None, True or False
        if node.value is None:
            return {NodeVarType.NONE}
        if isinstance(node.value, bool):
            return {NodeVarType.BOOLEAN}
        if isinstance(node.value, int):
            return {NodeVarType.INT}
        if isinstance(node.value, float):
            return {NodeVarType.FLOAT}
        if isinstance(node.value, str):
            return {NodeVarType.STRING}

        return {NodeVarType.UNKNOWN}

322 323 324
    def _get_node_var_type(self, cur_wrapper):
        node = cur_wrapper.node
        if isinstance(node, gast.Constant):
325
            return self._get_constant_node_type(node)
326 327

        if isinstance(node, gast.BoolOp):
328
            return {NodeVarType.BOOLEAN}
329
        if isinstance(node, gast.Compare):
330
            return {NodeVarType.BOOLEAN}
331 332

        if isinstance(node, gast.Dict):
333
            return {NodeVarType.DICT}
334
        if isinstance(node, gast.Set):
335
            return {NodeVarType.SET}
336 337 338 339 340 341 342

        if isinstance(node, gast.UnaryOp):
            return self.node_to_wrapper_map[node.operand].node_var_type

        if isinstance(node, gast.BinOp):
            left_type = self.node_to_wrapper_map[node.left].node_var_type
            right_type = self.node_to_wrapper_map[node.right].node_var_type
343 344 345 346 347
            result_type = set()
            for l in left_type:
                for r in right_type:
                    result_type.add(NodeVarType.binary_op_output_type(l, r))
            return result_type
348 349 350 351 352 353 354

        if isinstance(node, gast.Assign):
            ret_type = self.node_to_wrapper_map[node.value].node_var_type
            for target in node.targets:
                if isinstance(target, gast.Name):
                    self.node_to_wrapper_map[target].node_var_type = ret_type
                    self.var_env.set_var_type(target.id, ret_type)
355 356 357 358 359
                # Handle statements like `a, b = paddle.shape(x)`
                elif isinstance(target, gast.Tuple):
                    for sub_target in target.elts:
                        if isinstance(sub_target, gast.Name):
                            self.node_to_wrapper_map[
360 361
                                sub_target
                            ].node_var_type = ret_type
362
                            self.var_env.set_var_type(sub_target.id, ret_type)
363 364
            return ret_type

365 366 367 368 369 370
        if isinstance(node, gast.AnnAssign):
            # TODO(0x45f): To determine whether need to support assignment statements
            # like `self.x: float = 2.1`.
            ret_type = {NodeVarType.type_from_annotation(node.annotation)}
            # if annotation and value(Constant) are diffent type, we use value type
            if node.value:
371
                node_value_type = self.node_to_wrapper_map[
372 373 374 375 376 377
                    node.value
                ].node_var_type
                if not (
                    node_value_type
                    & {NodeVarType.UNKNOWN, NodeVarType.STATEMENT}
                ):
378
                    ret_type = node_value_type
379 380 381 382 383
            if isinstance(node.target, gast.Name):
                self.node_to_wrapper_map[node.target].node_var_type = ret_type
                self.var_env.set_var_type(node.target.id, ret_type)
            return ret_type

384 385
        if isinstance(node, gast.Name):
            if node.id == "None":
386
                return {NodeVarType.NONE}
387
            if node.id in {"True", "False"}:
388
                return {NodeVarType.BOOLEAN}
389 390
            # If node is child of functionDef.arguments
            parent_node_wrapper = cur_wrapper.parent
391 392 393
            if parent_node_wrapper and isinstance(
                parent_node_wrapper.node, gast.arguments
            ):
394 395

                return self._get_func_argument_type(parent_node_wrapper, node)
396

397 398
            return self.var_env.get_var_type(node.id)

399
        if isinstance(node, gast.Return):
400 401 402 403
            # If return nothing:
            if node.value is None:
                return {NodeVarType.NONE}

404
            return_type = self.node_to_wrapper_map[node.value].node_var_type
405 406 407 408
            assert (
                self.var_env.cur_scope.scope_type
                == AstVarScope.SCOPE_TYPE_FUNCTION
            ), "Return at non-function scope"
409 410 411 412 413
            func_name = self.var_env.cur_scope.scope_name
            parent_scope = self.var_env.get_parent_scope()
            parent_scope.add_var_type(func_name, return_type)
            return return_type

414 415
        if isinstance(node, gast.Call):
            if is_dygraph_api(node):
416 417 418 419 420
                if isinstance(node.func, gast.Attribute):
                    if node.func.attr == "to_variable":
                        return {NodeVarType.TENSOR}
            if is_paddle_api(node):
                return {NodeVarType.PADDLE_RETURN_TYPES}
421 422
            if is_numpy_api(node):
                # In this simple version we assume numpy api returns nd-array
423 424 425 426
                return {NodeVarType.NUMPY_NDARRAY}

            if isinstance(node.func, gast.Name):
                return self.var_env.get_var_type(node.func.id)
427 428 429
        if isinstance(node, gast.Subscript):
            if self.is_tensor_node(node.value):
                return {NodeVarType.TENSOR}
430

431
        return {NodeVarType.STATEMENT}
432 433 434 435

    def _get_func_argument_type(self, parent_node_wrapper, node):
        """
        Returns type information by parsing annotation or default values.
436

437 438 439
        For example:
            1. parse by default values.
                foo(x, y=1, z='s') -> x: UNKNOWN, y: INT, z: STR
440

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
            2. parse by Py3 type annotation.
                foo(x: Tensor, y: int, z: str) -> x: Tensor, y: INT, z: STR

            3. parse by type annotation and default values.
                foo(x: Tensor, y: int, z: str = 'abc') -> x: Tensor, y: INT, z: STR

        NOTE: Currently, we only support Tensor, int, bool, float, str et.al.
              Other complicate types will be supported later.
        """
        assert isinstance(node, gast.Name)

        parent_node = parent_node_wrapper.node
        var_type = {NodeVarType.UNKNOWN}
        if node.annotation is not None:
            var_type = {NodeVarType.type_from_annotation(node.annotation)}
            self.var_env.set_var_type(node.id, var_type)

        # if annotation and value(Constant) are diffent type, we use value type
        if parent_node.defaults:
            index = index_in_list(parent_node.args, node)
            args_len = len(parent_node.args)
            if index != -1 and args_len - index <= len(parent_node.defaults):
                defaults_node = parent_node.defaults[index - args_len]
                if isinstance(defaults_node, gast.Constant):
                    var_type = self._get_constant_node_type(defaults_node)

                    # Add node with identified type into cur_env.
                    self.var_env.set_var_type(node.id, var_type)

        return var_type