utils.py 44.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 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.

from __future__ import print_function

17
import ast
18
import astor
19 20
import atexit
import copy
21
import collections
22
from paddle.utils import gast
23 24 25 26
import inspect
import os
import six
import tempfile
27
import textwrap
28
import numpy as np
29

30
import paddle
31
from paddle.fluid import unique_name
32
from paddle.fluid.data_feeder import convert_dtype
33
from paddle.fluid import core
34 35
from paddle.fluid.layer_helper import LayerHelper
from paddle.fluid.layers import assign
36

37 38 39 40 41
# Note(Aurelius): Do not forget the dot `.` to distinguish other
# module such as paddlenlp.
PADDLE_MODULE_PREFIX = 'paddle.'
DYGRAPH_MODULE_PREFIX = 'paddle.fluid.dygraph'
DYGRAPH_TO_STATIC_MODULE_PREFIX = 'paddle.fluid.dygraph.dygraph_to_static'
42 43 44
GET_ARGS_FUNC_PREFIX = 'get_args'
SET_ARGS_FUNC_PREFIX = 'set_args'
ARGS_NAME = '__args'
45 46
# NOTE(liym27): Please use `getattr(ast_node, ORIGI_INFO)` instead of . operation to get the original information of ast node.
ORIGI_INFO = "Original information of source code for ast node."
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

class BaseNodeVisitor(gast.NodeVisitor):
    """
    Implement customized NodeVisitor inherited from gast.NodeVisitor. 
    Ancestor nodes are traced to easily support more operations of currently
    visited node.
    """

    def __init__(self):
        self.ancestor_nodes = []

    def visit(self, node):
        """Visit a node."""
        self.ancestor_nodes.append(node)

        method = 'visit_' + node.__class__.__name__
        visitor = getattr(self, method, self.generic_visit)
        ret = visitor(node)
        self.ancestor_nodes.pop()
        return ret


70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
# imp is deprecated in python3
from importlib.machinery import SourceFileLoader

dygraph_class_to_static_api = {
    "CosineDecay": "cosine_decay",
    "ExponentialDecay": "exponential_decay",
    "InverseTimeDecay": "inverse_time_decay",
    "NaturalExpDecay": "natural_exp_decay",
    "NoamDecay": "noam_decay",
    "PiecewiseDecay": "piecewise_decay",
    "PolynomialDecay": "polynomial_decay",
}

FOR_ITER_INDEX_PREFIX = '__for_loop_var_index'
FOR_ITER_TUPLE_PREFIX = '__for_loop_iter_tuple'
FOR_ITER_TUPLE_INDEX_PREFIX = '__for_loop_iter_tuple_index'
FOR_ITER_VAR_LEN_PREFIX = '__for_loop_var_len'
FOR_ITER_VAR_NAME_PREFIX = '__for_loop_iter_var'
FOR_ITER_ZIP_TO_LIST_PREFIX = '__for_loop_iter_zip'

# FullArgSpec is valid from Python3. Defined a Namedtuple to
# to make it available in Python2.
FullArgSpec = collections.namedtuple('FullArgSpec', [
    'args', 'varargs', 'varkw', 'defaults', 'kwonlyargs', 'kwonlydefaults',
    'annotations'
])


98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
def data_layer_not_check(name, shape, dtype='float32', lod_level=0):
    """
    This function creates a Tensor on the global block. The created Tensor
    doesn't check the dtype and the shape of feed data because dygraph input
    data can be various-length. This API is used in translating dygraph into
    static graph.

     Note: 
        The default :code:`stop_gradient` attribute of the Tensor created by
        this API is true, which means the gradient won't be passed backward
        through the data Tensor. Set :code:`var.stop_gradient = False` If
        user would like to pass backward gradient.

    Args:
       name (str): The name/alias of the Tensor, see :ref:`api_guide_Name`
           for more details.
       shape (list|tuple): List|Tuple of integers declaring the shape. You can
           set "None" at a dimension to indicate the dimension can be of any
           size. For example, it is useful to set changeable batch size as "None" 
       dtype (np.dtype|VarType|str, optional): The type of the data. Supported
           dtype: bool, float16, float32, float64, int8, int16, int32, int64,
           uint8. Default: float32
       lod_level (int, optional): The LoD level of the LoDTensor. Usually users
           don't have to set this value. For more details about when and how to
           use LoD level, see :ref:`user_guide_lod_tensor` . Default: 0

    Returns:
        Tensor: The global Tensor that gives access to the data.
    """
    helper = LayerHelper('data', **locals())
    shape = list(shape)
    for i in six.moves.range(len(shape)):
        if shape[i] is None:
            shape[i] = -1

133 134 135 136 137 138 139 140
    return helper.create_global_variable(name=name,
                                         shape=shape,
                                         dtype=dtype,
                                         type=core.VarDesc.VarType.LOD_TENSOR,
                                         stop_gradient=True,
                                         lod_level=lod_level,
                                         is_data=True,
                                         need_check_feed=False)
141

142

143 144 145 146 147 148 149 150
def create_undefined_var_like(variable):
    """ create a undefined var with the same shape and dtype like varaible.
    """
    from paddle.fluid.dygraph.dygraph_to_static.return_transformer import RETURN_NO_VALUE_MAGIC_NUM
    var = data_layer_not_check(unique_name.generate("undefined_var"),
                               variable.shape, variable.dtype)
    assign(RETURN_NO_VALUE_MAGIC_NUM, var)
    return var
151

152

153 154 155 156 157 158
def create_undefined_variable():
    from paddle.fluid.dygraph.dygraph_to_static.return_transformer import RETURN_NO_VALUE_MAGIC_NUM
    var = data_layer_not_check(unique_name.generate("undefined_var"), [1],
                               "float64")
    assign(RETURN_NO_VALUE_MAGIC_NUM, var)
    return var
159 160


161 162 163 164 165 166 167 168 169 170
class UndefinedVar:

    def __init__(self, name):
        self.name = name

    def check(self):
        raise UnboundLocalError(
            "local variable '{}' should be created before using it.")


171 172 173 174 175 176
class Dygraph2StaticException(Exception):

    def __init__(self, message):
        super().__init__(message)


177 178 179 180 181 182 183
def saw(x):
    if isinstance(x, UndefinedVar):
        return x.check()
    else:
        return x


184 185 186 187 188
def getfullargspec(target):
    if hasattr(inspect, "getfullargspec"):
        return inspect.getfullargspec(target)
    else:
        argspec = inspect.getargspec(target)
189 190 191 192 193 194 195
        return FullArgSpec(args=argspec.args,
                           varargs=argspec.varargs,
                           varkw=argspec.keywords,
                           defaults=argspec.defaults,
                           kwonlyargs=[],
                           kwonlydefaults=None,
                           annotations={})
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217


def parse_arg_and_kwargs(function):
    """
    Returns full argument names as list. e.g ['x', 'y', 'z']
    """
    fullargspec = getfullargspec(function)
    arg_names = fullargspec.args
    if arg_names and 'self' == arg_names[0]:
        arg_names = fullargspec.args[1:]

    # parse default kwargs
    default_kwargs = {}
    default_values = fullargspec.defaults
    if default_values:
        assert len(default_values) <= len(arg_names)
        default_kwarg_names = arg_names[-len(default_values):]
        default_kwargs = dict(zip(default_kwarg_names, default_values))

    return arg_names, default_kwargs


W
WeiXin 已提交
218 219 220 221 222 223 224 225 226
def parse_varargs_name(function):
    """
    Returns varargs name string of function. e.g: 'input' from `foo(x, *input)`
    """
    fullargspec = getfullargspec(function)
    varargs = fullargspec.varargs
    return varargs


227 228 229 230 231 232 233 234
def type_name(v):
    return type(v).__name__


def make_hashable(x, error_msg=None):
    """
    Makes input `x` hashable.

235
    For some unhashable objects, such as `dict/list/set/np.ndarray`,applying hash function by using their values.
236
    """
237
    if isinstance(x, (tuple, list, set)):
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
        return tuple(map(make_hashable, x))

    try:
        hash(x)
    except TypeError:
        if isinstance(x, np.ndarray):
            # Note: `tostring()` will return the binary data from np.ndarray that
            # means different value will lead to different hash code.
            return hash(x.tostring())
        elif isinstance(x, dict):
            return tuple(map(make_hashable, x.values()))

        error_msg = error_msg or "Requires a hashable object."
        raise ValueError(error_msg + " But received type: %s" % type_name(x))

    return x

255

256 257 258 259 260 261 262
def _is_api_in_module_helper(obj, module_prefix):
    m = inspect.getmodule(obj)
    return m is not None and m.__name__.startswith(module_prefix)


def is_api_in_module(node, module_prefix):
    assert isinstance(node, gast.Call), "Input non-Call node for is_dygraph_api"
263 264 265 266 267 268 269 270

    # Python can have gast.Call as function, for example: covert_call(func)(x)
    # We only check the most outside function
    func_node = node.func
    while isinstance(func_node, gast.Call):
        func_node = func_node.func

    func_str = astor.to_source(gast.gast_to_ast(func_node)).strip()
271
    try:
272 273 274 275 276
        # TODO(liym27):
        #  Consider a better to import modules like:
        #  source_file = inspect.getfile(dyfunc)
        #  import_statements = ImportVisitor(source_file).transform()
        #  import_str = "".join(import_statements)
277
        import paddle
L
liym27 已提交
278
        import paddle.fluid as fluid
279
        import paddle.fluid.dygraph as dygraph
L
liym27 已提交
280
        import paddle.fluid.layers as layers
281
        import paddle.jit.dy2static as _jst
282

283
        from paddle.fluid.dygraph import to_variable
284 285
        from paddle import to_tensor

286 287
        return eval("_is_api_in_module_helper({}, '{}')".format(
            func_str, module_prefix))
288
    except Exception:
289 290 291 292
        return False


def is_dygraph_api(node):
293

294
    # Note: A api in module dygraph_to_static is not a real dygraph api.
295
    if is_api_in_module(node, DYGRAPH_TO_STATIC_MODULE_PREFIX):
296 297
        return False

298 299
    # TODO(liym27): A better way to determine whether it is a dygraph api.
    #  Consider the decorator @dygraph_only
300
    return is_api_in_module(node, DYGRAPH_MODULE_PREFIX)
301 302 303


def is_paddle_api(node):
304 305 306 307 308 309
    return is_api_in_module(node, PADDLE_MODULE_PREFIX)


def is_paddle_func(func):
    m = inspect.getmodule(func)
    return m is not None and m.__name__.startswith(PADDLE_MODULE_PREFIX)
310 311 312 313 314 315 316 317 318 319 320 321 322 323


# Is numpy_api cannot reuse is_api_in_module because of numpy module problem
def is_numpy_api(node):
    assert isinstance(node, gast.Call), "Input non-Call node for is_numpy_api"
    func_str = astor.to_source(gast.gast_to_ast(node.func))
    try:
        import numpy as np
        module_result = eval("_is_api_in_module_helper({}, '{}')".format(
            func_str, "numpy"))
        # BUG: np.random.uniform doesn't have module and cannot be analyzed
        # TODO: find a better way
        if not module_result:
            return func_str.startswith("numpy.") or func_str.startswith("np.")
324
    except Exception:
325 326 327
        return False


L
liym27 已提交
328 329 330
def is_control_flow_to_transform(node,
                                 static_analysis_visitor=None,
                                 var_name_to_type=None):
331
    """
L
liym27 已提交
332 333
    Determines whether the node is a PaddlePaddle control flow statement which needs to
    be transformed into a static graph control flow statement.
334 335 336
    """
    assert isinstance(node, gast.AST), \
        "The type of input node must be gast.AST, but received %s." % type(node)
337 338 339
    visitor = IsControlFlowVisitor(node,
                                   static_analysis_visitor,
                                   node_var_type_map=var_name_to_type)
L
liym27 已提交
340 341
    need_to_transform = visitor.transform()
    return need_to_transform
342 343


344 345
def _delete_keywords_from(node):
    assert isinstance(node, gast.Call)
346
    func_src = astor.to_source(gast.gast_to_ast(node.func))
347 348 349 350 351 352 353 354 355 356 357 358
    import paddle.fluid as fluid
    full_args = eval("inspect.getargspec({})".format(func_src))
    full_args_name = full_args[0]

    node.keywords = [k for k in node.keywords if k.arg in full_args_name]
    return


def to_static_api(dygraph_class):
    if dygraph_class in dygraph_class_to_static_api:
        return dygraph_class_to_static_api[dygraph_class]
    else:
359 360 361
        raise NotImplementedError(
            "Paddle dygraph API {} cannot be converted "
            "to static graph at present.".format(dygraph_class))
362 363 364 365 366 367 368 369 370 371


def _add_keywords_to(node, dygraph_api_name):
    assert isinstance(node, gast.Call)
    if dygraph_api_name == "Linear":
        for ast_keyword in node.keywords:
            if ast_keyword.arg == "output_dim":
                ast_keyword.arg = "size"

        node.keywords.append(
372 373
            gast.keyword(arg="num_flatten_dims",
                         value=gast.Constant(value=-1, kind=None)))
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

    if dygraph_api_name == "BilinearTensorProduct":
        for ast_keyword in node.keywords:
            if ast_keyword.arg == "output_dim":
                ast_keyword.arg = "size"

    if dygraph_api_name == "PRelu":
        for ast_keyword in node.keywords:
            if ast_keyword.arg == "input":
                ast_keyword.arg = "x"
    return


def to_static_ast(node, class_node):
    assert isinstance(node, gast.Call)
    assert isinstance(class_node, gast.Call)
    static_api = to_static_api(class_node.func.attr)

392 393 394 395 396 397 398 399 400
    node.func = gast.Attribute(attr=static_api,
                               ctx=gast.Load(),
                               value=gast.Attribute(attr='layers',
                                                    ctx=gast.Load(),
                                                    value=gast.Name(
                                                        ctx=gast.Load(),
                                                        id='fluid',
                                                        annotation=None,
                                                        type_comment=None)))
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420

    update_args_of_func(node, class_node, 'forward')

    node.args.extend(class_node.args)
    node.keywords.extend(class_node.keywords)
    _add_keywords_to(node, class_node.func.attr)
    _delete_keywords_from(node)

    gast.fix_missing_locations(node)

    return node


def update_args_of_func(node, dygraph_node, method_name):
    assert isinstance(node, gast.Call)
    if method_name not in ["__init__", "forward"]:
        raise ValueError(
            "The method name of class to update args should be '__init__' or 'forward'"
        )

421
    class_src = astor.to_source(gast.gast_to_ast(dygraph_node.func))
422 423 424
    import paddle.fluid as fluid
    if method_name == "__init__" or eval(
            "issubclass({}, fluid.dygraph.Layer)".format(class_src)):
425 426
        full_args = eval("inspect.getargspec({}.{})".format(
            class_src, method_name))
427 428 429 430 431 432 433 434 435 436 437
        full_args_name = [
            arg_name for arg_name in full_args[0] if arg_name != "self"
        ]
    else:
        full_args_name = []
    added_keywords = []
    for idx, arg in enumerate(node.args):
        added_keywords.append(gast.keyword(arg=full_args_name[idx], value=arg))

    node.args = []
    node.keywords = added_keywords + node.keywords
438 439 440


def create_api_shape_node(tensor_shape_node):
441 442 443 444 445
    assert isinstance(tensor_shape_node,
                      (gast.Name, gast.Attribute, gast.Subscript))

    if isinstance(tensor_shape_node, gast.Name):
        api_shape_node = gast.Call(
446
            func=gast.parse('paddle.shape').body[0].value,
447 448 449
            args=[tensor_shape_node],
            keywords=[])
        return api_shape_node
450 451 452

    if isinstance(tensor_shape_node, gast.Attribute):
        api_shape_node = gast.Call(
453
            func=gast.parse('paddle.shape').body[0].value,
454 455 456 457 458 459 460 461
            args=[tensor_shape_node.value],
            keywords=[])
        return api_shape_node

    if isinstance(tensor_shape_node, gast.Subscript):
        result_node = copy.deepcopy(tensor_shape_node)
        result_node.value = create_api_shape_node(result_node.value)
        return result_node
462 463


464
def get_constant_variable_node(name, value, shape=[1], dtype='int64'):
465 466
    return gast.parse('%s = paddle.full(%s, "%s", %s)' %
                      (name, str(shape), str(value), dtype))
467 468 469 470 471 472 473 474 475


def get_attribute_full_name(node):
    assert isinstance(
        node,
        gast.Attribute), "Input non-Attribute node to get attribute full name"
    return astor.to_source(gast.gast_to_ast(node)).strip()


476
def generate_name_node(name_ids, ctx=gast.Load(), gen_tuple_if_single=False):
477
    """
478 479 480 481 482 483 484
    If name_ids is list or tuple or set with multiple strings, this function
    generates gast.Tuple of gast.Name.
    If the name_ids is single string or contains only 1 string, this function
    returns gast.Name if gen_tuple_if_single==False else returns gast.Tuple
    with only one gast.Name

    This function is used at several gast.Return statements.
485 486 487 488
    """
    if isinstance(name_ids, six.string_types):
        name_ids = [name_ids]
    if not isinstance(name_ids, (list, tuple, set)):
489 490 491
        raise TypeError(
            'name_ids must be list or tuple or set, but received %s' %
            type(type(name_ids)))
492 493 494 495 496 497 498 499 500 501

    def create_node_for_name(name):
        if '.' not in name:
            return gast.Name(id=name,
                             ctx=ctx,
                             annotation=None,
                             type_comment=None)
        return gast.parse(name).body[0].value

    gast_names = [create_node_for_name(name_id) for name_id in name_ids]
502
    if len(gast_names) == 1 and not gen_tuple_if_single:
503 504 505 506 507 508 509 510 511 512 513 514 515
        name_node = gast_names[0]
    else:
        name_node = gast.Tuple(elts=gast_names, ctx=ctx)
    return name_node


def create_funcDef_node(nodes, name, input_args, return_name_ids):
    """
    Wrapper all statements of nodes into one ast.FunctionDef, which can be
    called by ast.Call.
    """
    nodes = copy.copy(nodes)
    # add return statement
516 517
    if return_name_ids:
        nodes.append(gast.Return(value=generate_name_node(return_name_ids)))
518 519
    else:
        nodes.append(gast.Return(value=None))
520 521 522 523 524 525
    func_def_node = gast.FunctionDef(name=name,
                                     args=input_args,
                                     body=nodes,
                                     decorator_list=[],
                                     returns=None,
                                     type_comment=None)
526 527 528
    return func_def_node


529 530 531 532 533 534 535 536
def index_in_list(array_list, item):
    try:
        return array_list.index(item)
    except ValueError:
        # Item not in array_list
        return -1


537 538 539 540 541 542 543 544 545
def create_assign_node(name, node):
    """
    Creates a `gast.Assign` node by given name_id as target and node as value.
    """
    targets = generate_name_node(name, ctx=gast.Store())
    assign_node = gast.Assign(targets=[targets], value=node)
    return targets, assign_node


546
def ast_to_func(ast_root, dyfunc, delete_on_exit=True):
547 548
    """
    Transform modified AST of decorated function into python callable object.
549 550
    TODO: If only decorate one of inner function instead of decorating the main
    function, the other inner functions are invisible for the decorated function.
551
    """
552

553
    def remove_if_exit(filepath):
554 555 556
        if os.path.exists(filepath):
            os.remove(filepath)

557
    source = ast_to_source_code(ast_root)
558
    source = _inject_import_statements() + source
559

560 561 562 563
    f = tempfile.NamedTemporaryFile(mode='w',
                                    suffix='.py',
                                    delete=False,
                                    encoding='utf-8')
564 565 566 567 568
    with f:
        module_name = os.path.basename(f.name[:-3])
        f.write(source)

    if delete_on_exit:
569 570
        atexit.register(lambda: remove_if_exit(f.name))
        atexit.register(lambda: remove_if_exit(f.name[:-3] + ".pyc"))
571

T
tianshuo78520a 已提交
572
    module = SourceFileLoader(module_name, f.name).load_module()
573
    func_name = dyfunc.__name__
W
WeiXin 已提交
574 575 576 577 578 579 580 581
    # The 'forward' or 'another_forward' of 'TranslatedLayer' cannot be obtained
    # through 'func_name'. So set the special function name '__i_m_p_l__'.
    if hasattr(module, '__i_m_p_l__'):
        callable_func = getattr(module, '__i_m_p_l__')
        callable_func.__name__ = func_name
    elif hasattr(module, func_name):
        callable_func = getattr(module, func_name)
    else:
582 583 584
        raise ValueError(
            'Function: %s doesn\'t exist in the Module transformed from AST.' %
            func_name)
585 586 587 588 589 590 591 592
    # After transform dygraph function into callable_func saved in tmp file,
    # it lost the global variables from imported statements or defined in source file.
    # Recovers the necessary variables by `__globals__`.
    recover_globals_attribute(dyfunc, callable_func)

    return callable_func, f.name


593 594
def _inject_import_statements():
    import_statements = [
595
        "import paddle", "from paddle import Tensor",
596 597
        "import paddle.fluid as fluid", "import paddle.jit.dy2static as _jst",
        "from typing import *", "import numpy as np"
598 599 600 601
    ]
    return '\n'.join(import_statements) + '\n'


602 603 604 605 606
def recover_globals_attribute(src_obj, dst_obj):
    attr_name = '__globals__'

    src_globals = getattr(src_obj, attr_name, {})
    dst_globals = getattr(dst_obj, attr_name, {})
607

608
    for k, v in six.iteritems(src_globals):
609 610 611
        # ignore builtin attribute.
        if not (k.startswith('__') and k.endswith('__')):
            dst_globals[k] = v
612 613


614 615 616 617 618 619
def func_to_source_code(function, dedent=True):
    """
    Transforms function into raw string of source code.
    """
    if not (inspect.isfunction(function) or inspect.ismethod(function)):
        raise TypeError(
620 621
            "The type of 'function' should be a function or method, but received {}."
            .format(type(function).__name__))
622
    source_code_list, _ = inspect.getsourcelines(function)
623
    # Replace comments with blank lines so that error messages are not misplaced
624
    source_code_list = [
625 626
        line if not line.lstrip().startswith('#') else '\n'
        for line in source_code_list
627 628
    ]
    source_code = ''.join(source_code_list)
629 630 631 632 633 634
    if dedent:
        source_code = textwrap.dedent(source_code)

    return source_code


635 636
def ast_to_source_code(ast_node):
    """
637
    Transforms ast node into source code.
638 639 640 641 642 643 644
    """
    if not isinstance(ast_node, (gast.AST, ast.AST)):
        raise TypeError(
            "Type of ast_root should be gast.AST or ast.AST, but received %s." %
            type(ast_node))
    if isinstance(ast_node, gast.AST):
        ast_node = gast.gast_to_ast(ast_node)
645 646 647 648 649 650

    # Do not wrap lines even if they are too long
    def pretty_source(source):
        return ''.join(source)

    source_code = astor.to_source(ast_node, pretty_source=pretty_source)
651
    return source_code
L
liym27 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674


def is_candidate_node(node):
    """
    Nodes with specified type will be dependent on tensor.
    """
    is_compare_node = isinstance(node, (gast.Compare, gast.BoolOp, gast.UnaryOp,
                                        gast.For, gast.If, gast.While))
    # TODO(Aurelius84): `.numpy()` may be an customized function,
    # and should consider a more elegant way to solve this problem.
    has_numpy_attr = ".numpy()" in ast_to_source_code(node)
    return is_compare_node or has_numpy_attr


def compare_with_none(node):
    """
    Whether the comparator of `gast.Compare` node is `None`.
    """
    if isinstance(node, gast.Compare):
        for child in [node.left, node.comparators]:
            # node.comparators is a list.
            if isinstance(child, list):
                child = child[0]
675 676 677
            if (isinstance(child, gast.Constant)
                    and child.value is None) or (isinstance(child, gast.Name)
                                                 and child.id == 'None'):
L
liym27 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
                return True
    return False


class IsControlFlowVisitor(gast.NodeVisitor):
    """
    Judge whether the ast_node of control flow from Dygraph code dependent on paddle Tensor.
    `ast_node` can be gast.If, gast.For, gast.While, gast.If.test(gast.Compare, gast.BoolOp, gast.UnaryOp).

    If returns True,
    gast.If.test must meet at least one of the following requirements:
        1. involves at least one var whose type is Tensor.
        2. the Tensor var calls `.numpy()[]` interface or Tensor.shape is [1].
        3. involves Tensor.shape[i] and the shape[i] is unknown in compile time.
    gast.While must meet at least one of the requirements 1 to 5:
        4. has `break` statement.
        5. has `continue` statement.
695
    gast.For must meet at least one of the requirements 4 to 8:
L
liym27 已提交
696
        6. calls `range` function in `for` statement and the argument of range is Tensor.
697 698
        7. calls `enumerate` function in `for` statement and the argument of enumerate is Tensor.
        8. the iterable varaible in `for` statement is Tensor.
L
liym27 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
        TODO: Support non-range case

    The following examples should not be considered as control_flow_if:
        1. `if Tensor_var` or `if Tensor_var is None`
        2. if Tensor.shape[i] is determined with fixed value (not -1 or None)

    Note: pred in ConditionalBlock require variable, which means all vars should be Tensor
          or transformed into Tensor, like fill_constant(shape=[1], dtype='int32', value=Tensor.shape[i]).

    TODO: 1. need to deal with `tensor.shape[i]` which need to eval the data of shape[i],
             because reshape_op may be called before this statement.
    """

    def __init__(self,
                 ast_node,
                 static_analysis_visitor=None,
                 node_var_type_map=None):
        assert isinstance(
            ast_node, gast.AST
        ), "Type of input node should be gast.AST, but received %s." % type(
            ast_node)
        self.ast_root = ast_node
        if static_analysis_visitor is None:
            from .static_analysis import StaticAnalysisVisitor
            static_analysis_visitor = StaticAnalysisVisitor(ast_node)
        self.static_analysis_visitor = static_analysis_visitor
        self.node_to_wrapper_map = self.static_analysis_visitor.get_node_to_wrapper_map(
        )
        self.node_var_type_map = node_var_type_map

        self.is_control_flow_num = 0
        self._compare_node_tenor_set = set()

    def transform(self):
        node = self.ast_root
734 735 736 737 738 739 740 741
        if isinstance(node, gast.If):
            self._visit_If(node)
        elif isinstance(node, gast.For):
            self._visit_For(node)
        elif isinstance(node, gast.While):
            self._visit_While(node)
        else:
            self.visit(node)
L
liym27 已提交
742 743 744 745 746 747 748 749 750
        return self.is_control_flow_num > 0

    def _visit_If(self, node):
        assert isinstance(node, gast.If)
        self.visit(node.test)
        return

    def _visit_For(self, node):
        assert isinstance(node, gast.For)
751 752 753 754 755 756 757 758 759 760 761 762 763 764
        if isinstance(node.iter, gast.Call):
            # for in range(var[0]|var.numpy()[0]) or for in enumerate(var|var.numpy())
            if isinstance(node.iter.func, gast.Name):
                if node.iter.func.id == "range" or node.iter.func.id == "enumerate":
                    for arg in node.iter.args:
                        self.visit(arg)
                else:
                    return
            # for in var.numpy()
            elif isinstance(node.iter.func, gast.Attribute):
                if node.iter.func.attr == 'numpy':
                    self._visit_Call(node.iter)
                else:
                    return
765 766
            else:
                return
767 768 769
        elif isinstance(node.iter, gast.Name):
            # for in var
            self.visit(node.iter)
770
        else:
L
liym27 已提交
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
            return

        for child_node in gast.walk(node):
            if isinstance(child_node, (gast.Continue, gast.Break)):
                self._visit_break_continue(child_node)
        return

    def _visit_While(self, node):
        assert isinstance(node, gast.While)
        test = node.test
        self.generic_visit(test)
        for child_node in gast.walk(node):
            if isinstance(child_node, (gast.Continue, gast.Break)):
                self._visit_break_continue(child_node)
        return

    def _visit_break_continue(self, node):
        assert isinstance(node, (gast.Break, gast.Continue))
        wrapper_node = self.node_to_wrapper_map.get(node)
        if not wrapper_node:
            # Transformed node is not in node_to_wrapper_map
            return

        while wrapper_node.parent:
            parent_node = wrapper_node.parent.node
            if isinstance(parent_node, (gast.For, gast.While)):
                if parent_node is self.ast_root:
                    self.is_control_flow_num += 1
                    return
                else:
                    return

            wrapper_node = wrapper_node.parent

        return

    def visit_BoolOp(self, node):
        for i, child in enumerate(node.values):
809
            self.visit(child)
L
liym27 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
        return node

    def visit_Compare(self, node):
        pre_control_flow_num = self.is_control_flow_num
        if not compare_with_none(node):
            self.generic_visit(node)
            for child in gast.walk(node):
                if isinstance(child, gast.Subscript):
                    self._visit_Subscript(child)
        if self.is_control_flow_num > pre_control_flow_num:
            self._compare_node_tenor_set.add(node)
        return node

    def _visit_Subscript(self, node):
        self.generic_visit(node)
        if hasattr(node, 'value') and isinstance(node.value, gast.Call):
            self._visit_Call(node.value)
        return node

    def _visit_Call(self, node):
        assert isinstance(node, gast.Call)
        if isinstance(node.func, gast.Attribute):
            attr_node = node.func
            if attr_node.attr == 'numpy':
                self.is_control_flow_num += 1

    def visit_Call(self, node):
        self._visit_Call(node)
        if is_paddle_api(node):
            self.is_control_flow_num += 1
        return node

    def visit_Name(self, node):
        if self._is_node_with_tensor(node, node.id):
            self.is_control_flow_num += 1
        return node

    def visit_Constant(self, node):
        if self._is_node_with_tensor(node, node.value):
            self.is_control_flow_num += 1
        return node

    def _is_node_with_tensor(self, node, name_id):
        from paddle.fluid.dygraph.dygraph_to_static.static_analysis import NodeVarType

        # Look up the node_var_type_map by name_id.
        if self.node_var_type_map:
            if name_id and isinstance(name_id, six.string_types):
                var_type = self.node_var_type_map.get(name_id, None)
859
                if var_type and var_type & NodeVarType.TENSOR_TYPES:
L
liym27 已提交
860 861
                    return True
        # if not found, look up the node_to_wrapper_map by node.
862
        wrapper_node = self.node_to_wrapper_map.get(node, None)
L
liym27 已提交
863
        if wrapper_node is not None:
864
            if wrapper_node.node_var_type & NodeVarType.TENSOR_TYPES:
L
liym27 已提交
865 866 867 868 869 870
                return True

        return False

    def get_compare_nodes_with_tensor(self):
        return self._compare_node_tenor_set
871 872


873 874 875 876 877 878 879 880 881 882 883 884 885 886
# NOTE: inspect.unwrap() exits in PY3 but not in PY2.
def unwrap(func):
    """
    Returns the object wrapped by decorators.
    """

    def _is_wrapped(f):
        return hasattr(f, '__wrapped__')

    unwrapped_f = func
    while (_is_wrapped(unwrapped_f)):
        unwrapped_f = unwrapped_f.__wrapped__

    return unwrapped_f
887 888


C
Chen Weihang 已提交
889
def input_specs_compatible(src_input_specs, desired_input_specs):
890 891 892 893
    """
    Returns True if the two input specs are compatible, otherwise False.

    args:
894 895 896 897
        src_input_spec (list or tuple[InputSpec et.al]): list/tuple of
            paddle.static.InputSpec or int/str et.al
        desired_input_specs (list or tuple[InputSpec et.al]): list/tuple of
            paddle.static.InputSpec or int/str et.al
898 899
    """
    len_specs = len(src_input_specs)
C
Chen Weihang 已提交
900 901
    if len_specs != len(desired_input_specs):
        # NOTE(chenweihang): if the input_spec of jit.save is a subset of
902
        # input_spec of to_static, also compatible
C
Chen Weihang 已提交
903 904 905 906
        for spec in src_input_specs:
            if spec not in desired_input_specs:
                return False
    else:
907 908 909 910 911 912 913 914
        for (src_spec, desired_spec) in zip(src_input_specs,
                                            desired_input_specs):
            if isinstance(src_spec, paddle.static.InputSpec) or isinstance(
                    desired_spec, paddle.static.InputSpec):
                if not _compatible_tensor_spec(src_spec, desired_spec):
                    return False
            else:
                if not _compatible_non_tensor_spec(src_spec, desired_spec):
C
Chen Weihang 已提交
915 916
                    return False

917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
    return True


def _compatible_tensor_spec(src_spec, desired_spec):
    """
    Check whether two tensor type spec is compatible.
    """
    for spec in [src_spec, desired_spec]:
        if not isinstance(spec, paddle.static.InputSpec):
            return False
    src_shape = src_spec.shape
    other_shape = desired_spec.shape
    len_shape = len(src_shape)
    if len_shape != len(other_shape):
        return False
    for j in range(len_shape):
        if src_shape[j] is None or src_shape[j] < 0:
            continue
        if other_shape[j] is None or other_shape[j] < 0:
            continue
        if src_shape[j] != other_shape[j]:
            return False

    src_dtype = convert_dtype(src_spec.dtype)
    other_dtype = convert_dtype(desired_spec.dtype)
    if src_dtype != other_dtype:
        return False
944 945

    return True
946

947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967

def _compatible_non_tensor_spec(src_spec, desired_spec):
    """
    Check whether two non-tensor type spec is compatible.
    """

    def hash_value(spec):
        try:
            hash_val = make_hashable(spec)
        except:
            hash_val = None
        return hash_val

    src_hash_val = hash_value(src_spec)
    desired_hash_val = hash_value(desired_spec)

    if src_hash_val != desired_hash_val:
        return False
    else:
        return True

968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991

def slice_is_num(slice_node):
    # A slice_node.slice can be a:
    # (1) ast.Index, which is a simple number such as [1], [-2]
    # (2) ast.Slice, which is represented by bounds such as [2:-1]
    # (3) ast.Tuple, which includes the above two cases such as [2:-1, 1]
    # If slice node is case (1), return True, Otherwise, return False.
    #
    # NOTE: In (1) case, when gast>=0.4.0, gast.Index is not used, which is replaced
    # other gast node such as gast.Constant, gast.Name, gast.UnaryOp and so on.
    # Considering the compatibility of gast, here use ast note to check whether the
    # node is a num. For more details, please visit https://github.com/serge-sans-paille/gast

    assert isinstance(slice_node, gast.Subscript)
    slice_node_str = ast_to_source_code(slice_node).strip()
    ast_node = ast.parse(slice_node_str).body[0].value

    if isinstance(ast_node.slice, (ast.Tuple, ast.Slice)):
        return False

    if isinstance(ast_node.slice, ast.Index):
        return True

    return False
992 993


994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
class NameScope:

    def __init__(self):
        """ 
            A NameScope is a object which manager all the variable names. 
            only FunctionDef and Controlflow node will have a namescope property.

            type can be "function" and "controlflow"

            we don't analyze the read only variable because they don't affect the analysis.
        """
        self.globals = set()
        self.nonlocals = set()
        self.args = set()
        self.father = None  # point to the nearest function name scope.
        self.w_vars = set()  # all qualified + normal names been stored
        self.created = set(
        )  # useful for control flow compatibility. may be remove later

    def set_father(self, father):
        self.father = father

    def existed_vars(self):
        """ vars existing in current scope. 
            they must not contain qualified names.
        """
        local_vars = self.w_vars - self.globals - self.nonlocals - self.args
        return set(filter(lambda x: '.' not in x, local_vars))

    def created_vars(self):
        return self.created

    def modified_vars(self):
        # may be globals / non-locals / args / qualified names and created_vars
        return self.w_vars

    def control_flow_vars(self):
        valid_names = self.w_vars
        tmp = self.father.global_vars & valid_names,
        return {"global": tmp, "nonlocal": self.w_vars - tmp}

    def global_vars(self):
        return self.globals

    def merge_from(self, name_scope):
        self.globals |= name_scope.globals
        self.nonlocals |= name_scope.nonlocals
        self.args |= name_scope.args
        self.w_vars |= name_scope.w_vars


class FunctionNameLivenessAnalysis(gast.NodeVisitor):
    """ analyze the liveness of a function.

        every variables stored in this scope will be collected,
        in addition with global/nonlocal information.

        1. global variable is stored in node.var_globals.
        2. nonlocal variable is stored in node.var_nonlocals.
        3. arguments is stored in node.var_args.

        For example:

        def func(*args, **kargs):
            a = 12
            global i,j
            nonlocal x,y
            print(a)
            i = k
            for m in range(10):
                q = 12
        
        After this visitor we have: 
        # node is the FunctionDef node with name: "func"
        node.pd_scope = NameScope(
            globals = ['i', 'j'],
            nonlocals = ['x', 'y'],
            args = ['args', 'kargs'], 
            wr_vars = ['a', 'i', 'q', 'm']
        )
    """

    def __init__(self, root_node):
        self.scope_node_stack = []  # controlflow, functiondef node
        self.visit(root_node)

    def _reset_name_scope(self, node):
        # always reset the node as empty namescope.
        setattr(node, "pd_scope", NameScope())

    def _get_name_scope(self, node):
        if not hasattr(node, "pd_scope"):
            setattr(node, "pd_scope", NameScope())
        return node.pd_scope

    def _current_name_scope(self):
        return self._get_name_scope(self.scope_node_stack[-1])

    def _father_name_scope(self):
        if len(self.scope_node_stack) == 1: return None
        return self._get_name_scope(self.scope_node_stack[-2])

    def _nearest_function_scope(self):
        if len(self.scope_node_stack) == 1: return None
        for node in self.scope_node_stack[-2::-1]:
            if isinstance(node, gast.FunctionDef):
                return self._get_name_scope(node)

    def visit_Name(self, node):
        self.generic_visit(node)
        write_context = (gast.Store, gast.AugStore, gast.Del)
        if isinstance(node.ctx, write_context):
            self._current_name_scope().w_vars.add(node.id)

    def visit_FunctionDef(self, node):

        def pre_func():
            self._current_name_scope().args |= set(
                self._get_argument_names(node))

        def post_func():
            """ NOTE: why we need merge w_vars here ? 
                because we do ifelse_transformer after loop_transformer. Loops will changed into functioons. but we know this function will be called in if. so we add w_vars to father function scope.
            """
            from paddle.fluid.dygraph.dygraph_to_static.loop_transformer import WHILE_CONDITION_PREFIX, WHILE_BODY_PREFIX, FOR_CONDITION_PREFIX, FOR_BODY_PREFIX
            from paddle.fluid.dygraph.dygraph_to_static.ifelse_transformer import TRUE_FUNC_PREFIX, FALSE_FUNC_PREFIX
            control_flow_function_def = [
                WHILE_BODY_PREFIX, WHILE_BODY_PREFIX, FOR_CONDITION_PREFIX,
                FOR_BODY_PREFIX, TRUE_FUNC_PREFIX, FALSE_FUNC_PREFIX
            ]

            def is_control_flow_def_node():
                for prefix in control_flow_function_def:
                    if node.name.startswith(prefix): return True
                return False

            if self._father_name_scope() and is_control_flow_def_node():
                self._father_name_scope().w_vars |= self._current_name_scope(
                ).w_vars

        self._visit_scope_node(node, pre_func, post_func)

    def _visit_scope_node(self, node, pre_func, post_func):
        """ scope node main visit logic.
            pre_func and post_func is callbacks
        """
        self._reset_name_scope(node)
        self.scope_node_stack.append(node)
        self._current_name_scope().father = self._nearest_function_scope()
        if pre_func: pre_func()
        self.generic_visit(node)
        if post_func: post_func()
        self.scope_node_stack.pop()

    def _visit_controlflow_node(self, node):

        def post_func():
            self._father_name_scope().merge_from(self._current_name_scope())
            self._current_name_scope().created = self._nearest_function_scope(
            ).existed_vars() - node.before_created

        def pre_func():
            setattr(node, "before_created",
                    self._nearest_function_scope().existed_vars())

        self._visit_scope_node(node, pre_func, post_func)

    def visit_For(self, node):
        self._visit_controlflow_node(node)

    def visit_While(self, node):
        self._visit_controlflow_node(node)

    def visit_If(self, node):
        self._visit_controlflow_node(node)

    def visit_Global(self, node):
        self._current_name_scope().globals |= set(node.names)

    def visit_Nonlocal(self, node):
        self._current_name_scope().nonlocals |= set(node.names)

    def visit_Attribute(self, node):
        self.generic_visit(node)
        write_context = (gast.Store, gast.AugStore, gast.Del)
        if isinstance(node.ctx, write_context):
            name = ast_to_source_code(node).strip()
            self._current_name_scope().w_vars.add(name)

    def _get_argument_names(self, node):
        """ get all arguments name in the functiondef node.
            this node is local to the function and shouldn't 
            be created.
        """
        assert isinstance(
            node, gast.FunctionDef), "Input node is not function define node"
        names = [a for a in node.args.args]
        names.append(node.args.vararg)
        names.append(node.args.kwarg)
        names = [i.id for i in names if i is not None]
        return names


1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
def create_get_args_node(names):
    """
    Create get_args function as follows:

        def get_args_0():
            nonlocal x, y
            return x, y
    """

    def empty_node():
        func_def = """
        def {func_name}():
            return
        """.format(func_name=unique_name.generate(GET_ARGS_FUNC_PREFIX))
        return gast.parse(textwrap.dedent(func_def)).body[0]

    assert isinstance(names, (list, tuple))
    mapped = list(filter(lambda n: '.' not in n, names))
    nonlocal_names = sorted(
        mapped,
        key=mapped.index)  # to keep the order, we can't use set() to unique
1218 1219 1220 1221 1222 1223
    if not names:
        return empty_node()
    if not nonlocal_names:
        nonlocal_vars = "\n"
    else:
        nonlocal_vars = "nonlocal " + ",".join(nonlocal_names)
1224 1225
    template = """
    def {func_name}():
1226
        {nonlocal_vars}
1227
        return {vars},
1228 1229 1230
    """
    func_def = template.format(
        func_name=unique_name.generate(GET_ARGS_FUNC_PREFIX),
1231
        nonlocal_vars=nonlocal_vars,
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
        vars=",".join(names))
    return gast.parse(textwrap.dedent(func_def)).body[0]


def create_set_args_node(names):
    """
    Create set_args function as follows:

        def set_args_0(__args):
            nonlocal x, y
            x, y = __args
    """

    def empty_node():
        func_def = """
        def {func_name}({args}):
            pass
        """.format(func_name=unique_name.generate(SET_ARGS_FUNC_PREFIX),
                   args=ARGS_NAME)
        return gast.parse(textwrap.dedent(func_def)).body[0]

    assert isinstance(names, (list, tuple))
    mapped = list(filter(lambda n: '.' not in n, names))
    nonlocal_names = sorted(
        mapped,
        key=mapped.index)  # to keep the order, we can't use set() to unique
1258 1259 1260 1261 1262 1263
    if not names:
        return empty_node()
    if not nonlocal_names:
        nonlocal_vars = "\n"
    else:
        nonlocal_vars = "nonlocal " + ",".join(nonlocal_names)
1264 1265
    template = """
    def {func_name}({args}):
1266
        {nonlocal_vars}
1267
        {vars}, = {args}
1268 1269 1270 1271
    """
    func_def = template.format(
        func_name=unique_name.generate(SET_ARGS_FUNC_PREFIX),
        args=ARGS_NAME,
1272
        nonlocal_vars=nonlocal_vars,
1273 1274 1275 1276
        vars=",".join(names))
    return gast.parse(textwrap.dedent(func_def)).body[0]


1277
def create_nonlocal_stmt_nodes(names):
1278 1279 1280 1281 1282 1283
    assert isinstance(names, (list, tuple))

    mapped = list(filter(lambda n: '.' not in n, names))
    names = sorted(
        mapped,
        key=mapped.index)  # to keep the order, we can't use set() to unique
1284 1285
    if not names:
        return []
1286
    func_code = "nonlocal {}".format(','.join(names))
1287
    return [gast.parse(func_code).body[0]]