program_translator.py 68.1 KB
Newer Older
1
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14
#
# 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
import collections
16
import inspect
17
import os
18 19
import textwrap
import threading
20
import warnings
21
import weakref
22

23
from paddle.fluid import core, framework
24
from paddle.fluid.data_feeder import check_type
25 26 27 28 29
from paddle.fluid.dygraph.base import (
    _switch_declarative_mode_guard_,
    param_guard,
    switch_to_static_graph,
)
30
from paddle.framework import in_dynamic_mode
31
from paddle.nn.layer import layers
32
from paddle.utils import flatten, gast
33 34 35 36 37 38 39 40 41

from . import error, logging_utils
from .ast_transformer import DygraphToStaticAst
from .function_spec import (
    FunctionSpec,
    _hash_spec_names,
    get_buffers,
    get_parameters,
)
42
from .origin_info import (
43 44 45 46
    attach_origin_info,
    create_and_update_origin_info_map,
    update_op_callstack_with_origin_info,
)
X
xiongkun 已提交
47
from .partial_program import PartialProgramLayerHook, partial_program_from
48
from .utils import (
49
    ALREADY_D2S,
50
    NO_SHAPE_VAR_TYPE,
51 52
    ast_to_func,
    ast_to_source_code,
53
    backend_guard,
54 55
    func_to_source_code,
    input_specs_compatible,
56
    is_paddle_func,
57
    make_hashable,
58
    prim_or_cinn_is_enabled,
59 60
    type_name,
    unwrap,
61
)
62

63
__all__ = []
64

65 66 67 68
# For each traced function, we set `max_traced_program_count` = 10 to consider caching performance.
# Once exceeding the threshold, we will raise warning to users to make sure the conversion is as expected.
MAX_TRACED_PROGRAM_COUNT = 10

69 70
CONVERSION_OPTIONS = "__jst_not_to_static"

71

72 73 74 75 76 77 78 79 80 81
def synchronized(func):
    func.__lock__ = threading.Lock()

    def lock_func(*args, **kwargs):
        with func.__lock__:
            return func(*args, **kwargs)

    return lock_func


82
class FunctionCache:
83 84 85 86 87
    """
    Caches the transformed functions to avoid redundant conversions of the same function.
    """

    def __init__(self):
88
        # Caches the converted static functions. {dygraph_func: static_func}
X
xiongkun 已提交
89
        self._converted_static_func_caches = weakref.WeakKeyDictionary()
90
        # Caches the converted ast node for same source code. {source_code: ast_root}
91
        self._code_to_ast_caches = {}
92
        self._dygraph_to_static = DygraphToStaticAst()
93

94 95 96 97 98 99
    def convert_with_cache(self, func):
        """
        Returns the cached static function or converts it when first encounters the function.
        """
        # If hit cache, return it directly.
        static_func = self._converted_static_func_caches.get(func, None)
100 101

        if static_func is None:
102 103
            static_func = self._convert(func)
            self._converted_static_func_caches[func] = static_func
104 105 106

        return static_func

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    def _convert(self, func):
        """
        Converts dygraph function into static function. For two functions with same dedent code,
        the second function will reuse the transformed ast node of previous one.

        For example:
            # A.py
            def foo(x, y):
                z = x + y
                return z

            # B.py
            def foo(x, y):
                z = x + y
                return z

        If the conversion of A.foo happens after B.foo, it will reuse the transformed ast node of B.foo
        to speed up the conversion.
        """
        # Note: In Python2, it will raise OSError when inspect function
        # with decorator directly and function.__wrapped__ holds the actual function.
128
        func = unwrap(func)
129
        source_code = func_to_source_code(func)
130 131 132 133 134

        # TODO(liym27):
        #  Consider this case: source_code in self._code_to_ast_caches,
        #  but actually they are methods in different classes.
        #  Maybe use (__class__, source_code) as key
135
        if source_code in self._code_to_ast_caches:
136
            root = self._code_to_ast_caches[source_code]
137 138
        else:
            root = gast.parse(source_code)
139
            root = attach_origin_info(root, func)
140 141
            root = self._dygraph_to_static.get_static_ast(root)
            self._code_to_ast_caches[source_code] = root
142

143
        # Get static function from AST
144
        static_func, file_name = ast_to_func(root, func)
145

146
        create_and_update_origin_info_map(root, static_func)
147
        return static_func
148 149

    def exist(self, func):
150
        return func in self._converted_static_func_caches
151 152


153 154 155 156
_CACHE_LOCK = threading.Lock()
_FUNCTION_CACHE = FunctionCache()


157
def convert_to_static(function):
158
    """
159
    Transforms function of dygraph into static function using the cache mechanism.
160

161 162
    Note(dev): It will return function.__func__ if encountering class method.

163 164
    Args:
        function(callable): The function with dygraph layers that will be converted into static layers.
165
    """
166 167
    if getattr(function, ALREADY_D2S, None):
        return function
168 169 170

    # Return directly if decorated with @not_to_static and DO NOT Cache it
    options = getattr(function, CONVERSION_OPTIONS, None)
171 172 173 174 175
    # or ignore paddle api
    need_skip = (options is not None and options.not_convert) or is_paddle_func(
        function
    )
    if need_skip:
176 177
        return function.__func__ if inspect.ismethod(function) else function

178
    with _CACHE_LOCK:
179
        static_func = _FUNCTION_CACHE.convert_with_cache(function)
180
        setattr(static_func, ALREADY_D2S, True)
181 182 183
        return static_func


184
class CacheKey:
185 186 187
    """
    Cached key for ProgramCache.
    """
188

189
    __slots__ = [
190 191 192 193 194 195
        'function_spec',
        'input_args_with_spec',
        'input_kwargs_with_spec',
        'class_instance',
        'kwargs',
        '_spec_names_id',
196
        '_new_ir_flags',
197
    ]
198

199 200 201 202 203 204
    def __init__(
        self,
        function_spec,
        input_args_with_spec,
        input_kwargs_with_spec,
        class_instance,
205
        **kwargs,
206
    ):
207 208
        """
        Initializes a cache key.
209

210 211
        Args:
            functions_spec(FunctionSpec): a FunctionSpec instance of decorated function.
212 213
            input_args_with_spec(list[InputSpec]): actual input args with some arguments replaced by InputSpec.
            input_kwargs_with_spec(list[{string:InputSpec}]): actual input kwargs with some arguments replaced by InputSpec.
214
            class_instance(object): a instance of class `Layer`.
215
            **kwargs(dict): manage other arguments used for better scalability
216
        """
217
        self.function_spec = function_spec
218 219
        self.input_args_with_spec = input_args_with_spec
        self.input_kwargs_with_spec = input_kwargs_with_spec
220
        self.class_instance = class_instance
221 222
        # NOTE: `kwargs` is usually not considered as basic member for `__hash__`
        self.kwargs = kwargs
223 224 225
        self._spec_names_id = _hash_spec_names(
            input_args_with_spec, input_kwargs_with_spec
        )
226 227 228
        self._new_ir_flags = os.environ.get(
            'FLAGS_enable_new_ir_in_executor', None
        )
229 230 231

    @classmethod
    def from_func_and_args(cls, function_spec, args, kwargs, class_instance):
232
        """
233 234 235 236 237 238 239 240 241 242 243
        Generated a CacheKey instance by given inputs.

        Args:
            functions_spec(FunctionSpec): a FunctionSpec instance of decorated function.
            args(tuple): tuple of actual inputs arguments.
            kwargs(dict): dict of actual inputs keyword arguments.
            class_instance(object): a instance of class `Layer`.
        """
        # 1. filter `self` in args
        if args and isinstance(args[0], layers.Layer):
            args = args[1:]
244
        # 2. convert tensor and numpy array into InputSpec
245
        _args, _kwargs = function_spec.unified_args_and_kwargs(args, kwargs)
246 247 248 249
        (
            input_args_with_spec,
            input_kwargs_with_spec,
        ) = function_spec.args_to_input_spec(_args, _kwargs)
250 251

        # 3. check whether hit the cache or build a new program for the input arguments
252 253 254 255 256 257
        return CacheKey(
            function_spec,
            input_args_with_spec,
            input_kwargs_with_spec,
            class_instance,
        )
258 259 260

    def __hash__(self):
        error_msg = "Arguments to a `@paddle.jit.to_static` must be a hashable Python objects (or nested structures of these types)."
261
        with_hook = self.kwargs.get("with_hook", False)
262
        is_train = self.kwargs.get("is_train", False)
263 264 265 266 267 268 269 270 271
        return hash(
            (
                id(self.function_spec),
                make_hashable(self.input_args_with_spec, error_msg),
                make_hashable(self.input_kwargs_with_spec, error_msg),
                self._spec_names_id,
                self.class_instance,
                with_hook,
                is_train,
272
                self._new_ir_flags,
273 274
            )
        )
275 276 277 278 279 280 281 282

    def __eq__(self, other):
        return (type(self) is type(other)) and hash(self) == hash(other)

    def __neq__(self, other):
        return not self == other

    def __repr__(self):
283
        return "id(function_spec): {}, input_args_with_spec: {}, input_kwargs_with_spec: {}, class_instance: {}".format(
284 285 286 287 288
            id(self.function_spec),
            self.input_args_with_spec,
            self.input_kwargs_with_spec,
            self.class_instance,
        )
289 290 291 292 293 294 295 296 297


def unwrap_decorators(func):
    """
    Unwraps a decorated function and returns the decorator list and inner target.
    """
    decorators = []
    cur = func
    while True:
298
        if isinstance(cur, StaticFunction):
299 300 301 302 303
            decorators.append(cur)
            # Note: if `cur` is a method, keep it as bound method of class.
            instance = cur._class_instance
            if instance is not None:
                cur = cur.dygraph_function.__get__(instance)
304
            else:
305 306 307 308
                cur = cur.dygraph_function
        else:
            break
    return decorators, cur
309

310

311
class StaticFunction:
312
    def __init__(self, function, input_spec=None, **kwargs):
313
        """
314
        Initializes a `StaticFunction`.
315 316 317 318

        Args:
            function(callable): A function or method that will be converted into static program.
            input_spec(list[InputSpec]): list of InputSpec to specify the `shape/dtype/name` information for each input argument, default None.
319
            **kwargs(dict): other arguments like `build_strategy` et.al.
320 321
        """
        # save the instance `self` while decorating a method of class.
322

323
        if inspect.ismethod(function):
324 325
            self._dygraph_function = function.__func__
            self._class_instance = function.__self__
326

327 328 329
            if not hasattr(self._class_instance, '_original_funcs'):
                raise TypeError(
                    "When using 'to_static' to convert method of a class, "
330 331
                    "please ensure the class inherits from nn.Layer"
                )
332
            self._class_instance._original_funcs[
333 334
                function.__name__
            ] = self._dygraph_function
335 336 337 338
        else:
            self._dygraph_function = function
            self._class_instance = None

339
        if input_spec is not None and prim_or_cinn_is_enabled(
340
            kwargs.get("build_strategy", None), kwargs.get("backend", None)
341
        ):
J
Jiabin Yang 已提交
342 343
            from paddle.static import InputSpec

344
            for spec in flatten(input_spec):
J
Jiabin Yang 已提交
345
                if isinstance(spec, InputSpec) and -1 in spec.shape:
346 347 348 349 350 351
                    input_spec = None
                    warnings.warn(
                        'Now prim and cinn do not support -1 shape, but input_spec has -1 shape so we set it to None.'
                    )
                    break

352 353 354
        self._input_spec = input_spec
        self._function_spec = FunctionSpec(function, input_spec)
        self._program_cache = ProgramCache()
355
        self._descriptor_cache = weakref.WeakKeyDictionary()
356
        # Note: Hold a reference to ProgramTranslator for switching `enable_to_static`.
357
        self._program_trans = ProgramTranslator()
358
        self._kwargs = kwargs
359
        self._training = True
360 361
        self._cuda_graph_capture_mode = ""
        self._cuda_graph_pool_id = 0
362 363 364 365 366 367 368
        self._property = kwargs.get("property", False)

    @property
    def is_property(self):
        # whether is class proproty to be exported.
        return self._property

369
    def train(self):
370 371
        if (
            isinstance(self._class_instance, layers.Layer)
372
            and self._class_instance.training is False
373
        ):
374 375 376
            raise RuntimeError(
                "Failed to switch train mode. {} is a Layer's method, "
                "please use Layer.train() to switch train mode.".format(
377 378 379
                    self.dygraph_function
                )
            )
380 381 382
        self._training = True

    def eval(self):
383 384
        if (
            isinstance(self._class_instance, layers.Layer)
385
            and self._class_instance.training is True
386
        ):
387 388 389
            raise RuntimeError(
                "Failed to switch eval mode. {} is a Layer's method, "
                "please use Layer.eval() to switch eval mode.".format(
390 391 392
                    self.dygraph_function
                )
            )
393
        self._training = False
394 395 396 397 398 399

    def __get__(self, instance, owner):
        """
        Overrides this method to parse the class instance and call bound method correctly.

        For example:
400

401 402 403 404
            '''
            class Net(Layer):
                def __init__(self):
                    pass
405

406 407 408 409 410 411 412
                @paddle.jit.to_static
                def forward(self, x, y):
                    return x + y

            net = Net()
            out = net(x, y)
            '''
413

414 415
        In above case, `net(x, y)` will call `net.forward(x, y)` firstly that is a bound method
        of `Net` instance. After decorated by `@paddle.jit.to_static`, it will firstly to call `__get__`
416
        to parse the class instance correctly instead of the `StaticFunction` instance.
417
        """
418 419 420
        if instance not in self._descriptor_cache:
            if instance is None:
                return self
421
            # Note(Aurelius84): To construct new instance of StaticFunction when we
422 423
            # first encouter the bound function of layer and cache it.
            new_static_layer = self._clone()
424 425 426 427 428 429 430
            if (
                self._dygraph_function.__name__
                not in instance._original_funcs.keys()
            ):
                instance._original_funcs[
                    self._dygraph_function.__name__
                ] = self._dygraph_function
431 432 433 434 435 436
            new_static_layer._class_instance = instance
            self._descriptor_cache[instance] = new_static_layer

        return self._descriptor_cache[instance]

    def _clone(self):
437
        return self.__class__(
438
            self.dygraph_function, self._input_spec, **self._kwargs
439
        )
440 441

    def __call__(self, *args, **kwargs):
442
        """
443 444 445 446
        Supports to call the returned instance with input `args` and `kwargs` directly.

        Args:
            *args(tuple): tuple of all input arguments from original decorated function.
447
            **kwargs(dict): dict of all input keyward arguments from original decorated function.
448 449 450

        Return:
            Outputs of decorated function.
451
        """
452 453
        if self._property:
            return self._call_dygraph_function(*args, **kwargs)
454

455
        # 1. call dygraph function directly if not enable `declarative`
456
        if not self._program_trans.enable_to_static:
457 458 459 460
            # NOTE(liym27):
            # Here calls `warnings.warn` but not `logging_utils.warn` because by default warnings.warn(message)
            # will show up **only once**. StaticFunction.__call__ will run many times, it is appropriate to
            # display this warning message only once.
461
            logging_utils.warn(
R
Ryan 已提交
462
                "The decorator '@paddle.jit.to_static' does NOT work when setting 'paddle.jit.enable_to_static' to False. "
463
                "We will just return dygraph output. If you would like to get static graph output, please call API "
R
Ryan 已提交
464
                "paddle.jit.enable_to_static(True)"
465
            )
466 467
            return self._call_dygraph_function(*args, **kwargs)

468
        if not in_dynamic_mode():
469 470
            raise RuntimeError(
                "Failed to run the callable object {} decorated by '@paddle.jit.to_static', "
471
                "because it is NOT in dynamic mode. Please disable the static graph mode to enter dynamic mode with the "
472
                "following API: paddle.disable_static().".format(
473 474 475
                    self.dygraph_function
                )
            )
476

X
xiongkun 已提交
477
        return self._perform_call(*args, **kwargs)
478

479 480
    def _is_train_mode(self):
        if self._class_instance is not None:
481 482 483
            if not hasattr(self._class_instance, 'training'):
                raise TypeError(
                    "When using 'to_static' to convert method of a class, "
484 485
                    "please ensure the class inherits from nn.Layer"
                )
486 487 488 489
            return self._class_instance.training
        else:
            return self._training

490 491 492 493 494 495
    def _call_dygraph_function(self, *args, **kwargs):
        """
        Calls dygraph function directly and returns the outputs.

        Args:
            *args(tuple): tuple of all input arguments from original decorated function.
496
            **kwargs(dict): dict of all input keyward arguments from original decorated function.
497 498 499 500

        Return:
            Outputs of dygraph function.
        """
501
        return self.dygraph_function(*args, **kwargs)
502

503 504 505 506 507 508 509 510 511
    def _raise_when_property(self):
        """raise RuntimeError when property=True

        Raises:
            RuntimeError: can not call this func when property=True
        """
        if self.is_property:
            raise RuntimeError("Can not call the func when property=True.")

X
xiongkun 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
    def get_concrete_program(self, *args, **kwargs):
        raise NotImplementedError("Not implemented yet.")

    def get_concrete_program_with_cache_key(self, cached_key):
        raise NotImplementedError("Not implemented yet.")

    def get_traced_count(self):
        raise NotImplementedError("Not implemented yet.")

    @property
    def code(self):
        raise NotImplementedError("Not implemented yet.")

    @property
    def dygraph_function(self):
        """
        Returns the original decorated function.
        """
        if self._class_instance is not None:
            return self._dygraph_function.__get__(self._class_instance)
        else:
            return self._dygraph_function

    @property
    def concrete_program(self):
        raise NotImplementedError("Not implemented yet.")

    def concrete_program_specify_input_spec(
        self, input_spec=None, with_hook=False, is_prim_infer=False
    ):
        raise NotImplementedError("Not implemented yet.")

    def rollback(self):
        """
        Rollback into original dygraph functions for current class instance.

        Returns:
            Function or Method

        Example::
            .. code-block:: python

                >>> # doctest: +SKIP
                >>> import paddle

                >>> class Net(paddle.nn.Layer):
                ...     def __init__(self):
                ...         super().__init__()
                ...
                ...     def forward(self, x, flag=True):
                ...         if flag:
                ...             out = x + 1
                ...         else:
                ...             out = x - 1
                ...         return out
                ...
                >>> x = paddle.randn([10, 1], 'float32')
                >>> net = paddle.jit.to_static(Net())  # convert into static graph mode
                >>> out = net(x)

                >>> net.forward.rollback()  # rollback into dygraph mode
                >>> out = net(x)
        """

        def rollback_impl(class_instance):
            for name, func in class_instance._original_funcs.items():
                setattr(class_instance, name, func.__get__(class_instance))

            for sublayer in class_instance.sublayers(include_self=False):
                rollback_impl(sublayer)

        if self._class_instance is None:
            return self._dygraph_function

        # only rollback sub-functions on path of top _dygraph_function
        func_name = self._dygraph_function.__name__
        assert (
            func_name in self._class_instance._original_funcs
        ), "Not Found function '{}' in class '{}'.".format(
591
            func_name, self._class_instance.__class__
X
xiongkun 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 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 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 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
        )
        func = self._class_instance._original_funcs[func_name]
        setattr(
            self._class_instance, func_name, func.__get__(self._class_instance)
        )

        for sublayer in self._class_instance.sublayers(include_self=False):
            rollback_impl(sublayer)

        return getattr(self._class_instance, func_name)

    def __deepcopy__(self, memo):
        """
        Customized behavior for copy.deepcopy, return original decorated function instead
        of a new StaticFunction Object. StaticFunction itself is not copyable becuase it's
        associated with class_instance.

        We add __deepcopy__ here only for the following usage:

        Example::
            .. code-block:: python

                >>> import copy
                >>> import paddle

                >>> class Net(paddle.nn.Layer):
                ...     def __init__(self):
                ...         super().__init__()
                ...
                ...     def forward(self, x, flag=True):
                ...         if flag:
                ...             out = x + 1
                ...         else:
                ...             out = x - 1
                ...         return out
                ...
                >>> x = paddle.randn([10, 1], 'float32')
                >>> net = paddle.jit.to_static(Net())  # convert into static graph mode

                >>> copy_net = copy.deepcopy(net)      # deepcopy a new net without @to_static

        Please attention that original 'net' will unwrap @to_static and rollback into simple Layer.
        """
        if self._class_instance is not None:
            net_name = type(self._class_instance).__name__
            logging_utils.log(
                level=-1,
                msg="Not recommend to deepcopy '{}' decorated with @to_static, it has side effect that will"
                " rollback into original state before @to_static. Please deepcopy '{}' before applying @to_static.".format(
                    net_name, net_name
                ),
            )
            self.rollback()
            return self._dygraph_function.__get__(
                memo[id(self._class_instance)]
            )
        else:
            return self._dygraph_function

    @property
    def inputs(self):
        raise NotImplementedError("Not implemented yet.")

    @property
    def outputs(self):
        raise NotImplementedError("Not implemented yet.")

    @property
    def main_program(self):
        raise NotImplementedError("Not implemented yet.")

    @property
    def program_cache(self):
        raise NotImplementedError("Not implemented yet.")

    @property
    def function_spec(self):
        raise NotImplementedError("Not implemented yet.")


def raise_error_template(func_str):
    def _raise_error(*args, **kwargs):
        error_template = (
            "Can't call {func} when enable_fallback=True."
            "Use paddle.jit.to_static(enable_fallback=False) instead."
        )
        raise RuntimeError(error_template.format(func=func_str))

    return _raise_error


class SymbolicStaticFunction(StaticFunction):
    def __init__(self, function, input_spec=None, **kwargs):
        if input_spec is not None:
            warnings.warn(
                "\nSymbolic Trace don't support input_spec arguments. It will Will not produce any effect.\n"
                "1. You can disable fallback mode by `paddle.jit.to_static(enable_fallback=False)` to switch to AST to static, then you can assign input spec.\n"
            )
        super().__init__(function, input_spec, **kwargs)
        self.last_call_input_spec = None

    def _perform_call(self, *args, **kwargs):
        args, kwargs = self._function_spec.unified_args_and_kwargs(args, kwargs)
        (
            input_args_with_spec,
            input_kwargs_with_spec,
        ) = self._function_spec.args_to_input_spec(args, kwargs)
        self.last_call_input_spec = input_args_with_spec

        try:
            from sot import symbolic_translate
        except:
            import os

            os.system(
                "pip install git+https://github.com/PaddlePaddle/PaddleSOT@develop"
            )
            from sot import symbolic_translate

        build_strategy = self._kwargs.get("build_strategy", None)
        traced_fun = symbolic_translate(
            self._dygraph_function, build_strategy=build_strategy
        )
        if self._class_instance is not None:
            args = (self._class_instance,) + args
        return traced_fun(*args, **kwargs)

    @property
    def code(self):
        raise_error_template("code")()

    @property
    def concrete_program(self):
        raise_error_template("concrete_program")()

    concrete_program_specify_input_spec = raise_error_template(
        "concrete_program_specify_input_spec"
    )
    get_concrete_program = raise_error_template("get_concrete_program")
    get_concrete_program_with_cache_key = raise_error_template(
        "get_concrete_program_with_cache_key"
    )
    get_traced_count = raise_error_template("get_traced_count")

    @property
    def inputs(self):
        raise_error_template("inputs")()

    @property
    def outputs(self):
        raise_error_template("outputs")()

    @property
    def main_program(self):
        raise_error_template("main_program")()

    @property
    def program_cache(self):
        raise_error_template("program_cache")()

    @property
    def function_spec(self):
        raise_error_template("function_spec ")()


class ASTStaticFunction(StaticFunction):
    """
    Wrapper class to Manage program conversion of decorated function.

    """

    def __init__(self, function, input_spec=None, **kwargs):
        super().__init__(function, input_spec, **kwargs)

    def _perform_call(self, *args, **kwargs):
        # 1. trace ops from dygraph layers and cache the generated program.
        args, kwargs = self._function_spec.unified_args_and_kwargs(args, kwargs)

        try:
            concrete_program, partial_program_layer = self.get_concrete_program(
                *args, **kwargs, is_train=self._is_train_mode()
            )
            # 2. synchronize self.training attribute.
            if isinstance(self._class_instance, layers.Layer):
                partial_program_layer.training = self._class_instance.training
            else:
                partial_program_layer.training = self._training

            partial_program_layer._cuda_graph_capture_mode = (
                self._cuda_graph_capture_mode
            )
            partial_program_layer._cuda_graph_pool_id = self._cuda_graph_pool_id

            # 3. return outputs.
            try:
                return partial_program_layer(args)
            except Exception as e:
                if not hasattr(e, error.ERROR_DATA):
                    # runtime error
                    error.attach_error_data(e, in_runtime=True)
                    raise
        except Exception as e:
            error_data = getattr(e, error.ERROR_DATA, None)
            if error_data:
                error_data.raise_new_exception()
            else:
                logging_utils.warn(
                    "Please file an issue at 'https://github.com/PaddlePaddle/Paddle/issues'"
                    " if you can't handle this {} yourself.".format(type(e))
                )
                raise e

804 805 806 807 808 809 810 811 812 813 814
    def get_concrete_program(self, *args, **kwargs):
        """
        Returns traced concrete program and inner executable partial layer.

        Args:
            *args(tuple): input arguments values or InputSpec
            **kwargs(dict) : input kwargs values.

        Returns:
            Traced ConcreteProgram and executable translated Layer.
        """
815
        self._raise_when_property()
816

817
        with_hook = kwargs.get("with_hook", False)
818
        is_train = kwargs.get("is_train", True)
819
        is_prim_infer = kwargs.get("is_prim_infer", False)
820 821 822 823
        if "is_train" in kwargs:
            kwargs.pop("is_train")
        if "with_hook" in kwargs:
            kwargs.pop("with_hook")
824 825
        if "is_prim_infer" in kwargs:
            kwargs.pop("is_prim_infer")
826 827
        # 1. unify args/kwargs and replace Tensor with InputSpec
        if len(args) != len(self._function_spec.args_name):
828
            args, kwargs = self._function_spec.unified_args_and_kwargs(
829 830 831 832 833 834
                args, kwargs
            )
        (
            input_args_with_spec,
            input_kwargs_with_spec,
        ) = self._function_spec.args_to_input_spec(args, kwargs)
835 836

        # 2. generate cache key
837 838 839 840 841 842 843
        cache_key = CacheKey(
            self._function_spec,
            input_args_with_spec,
            input_kwargs_with_spec,
            self._class_instance,
            **self._kwargs,
            with_hook=with_hook,
844
            is_train=is_train,
845
        )
846 847 848 849 850 851 852 853 854 855 856
        if is_prim_infer:
            (
                concrete_program,
                partial_program_layer,
            ) = self._program_cache.get_program_without_cache(cache_key)
        else:
            # 3. check whether hit the cache or build a new program for the input arguments
            concrete_program, partial_program_layer = self._program_cache[
                cache_key
            ]
        return concrete_program, partial_program_layer
857

858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
    def get_concrete_program_with_cache_key(self, cached_key):
        """
        Returns traced concrete program and inner executable partial layer by cached key.

        Args:
            cached_key(CacheKey): The cached key use to get concrete program.

        Returns:
            Traced ConcreteProgram and executable translated Layer.
        """
        self._raise_when_property()
        (
            concrete_program,
            partial_program_layer,
        ) = self._program_cache.get_program_without_cache(cached_key)
873 874 875 876 877 878 879 880 881 882 883 884 885
        return concrete_program, partial_program_layer

    def get_traced_count(self):
        """
        Returns the number of traced programs for the decorated function.
        """
        return len(self._program_cache)

    @property
    def code(self):
        """
        Returns the source code of transformed static function for debugging.
        """
886
        static_func = convert_to_static(self.dygraph_function)
887 888 889 890 891 892 893
        source_code = func_to_source_code(static_func)
        return source_code

    @property
    def concrete_program(self):
        """
        Returns recent ConcreteProgram instance of decorated function.
A
Aurelius84 已提交
894 895 896 897

        Examples:
            .. code-block:: python

898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
                >>> # doctest: +SKIP
                >>> import paddle
                >>> from paddle.jit import to_static
                >>> from paddle.static import InputSpec

                >>> paddle.disable_static()

                >>> def foo(x, y):
                ...     z = x + y
                ...     return z
                ...
                >>> # usage 1:
                >>> decorated_foo = to_static(foo, input_spec=[InputSpec([10], name='x'), InputSpec([10], name='y')])
                >>> print(decorated_foo.concrete_program)

                >>> # usage 2:
                >>> decorated_foo = to_static(foo)
                >>> out_foo = decorated_foo(paddle.rand([10]), paddle.rand([10]))
                >>> print(decorated_foo.concrete_program)
917
        """
918 919
        return self.concrete_program_specify_input_spec(input_spec=None)

920
    def concrete_program_specify_input_spec(
921
        self, input_spec=None, with_hook=False, is_prim_infer=False
922
    ):
923 924 925
        """
        Returns recent ConcreteProgram instance of decorated function while
        specifying input_spec. If the self._function_spec already has
926
        input_spec, it will check the compatibility of input input_spec and
927 928 929 930 931 932 933
        the self._function_spec.input_spec. If input input_spec=None, then
        this method uses self._function_spec.input_spec

        args:
            input_spec (list[InputSpec], optional): Describes the input of
                the translate function.
        """
934
        self._raise_when_property()
935 936 937 938
        # if specific the `input_spec`, the length of program_cache will always 1,
        # else, return the last one.
        cached_program_len = len(self._program_cache)
        # If specific `input_spec`, apply convertion from dygraph layers into static Program.
939 940
        # NOTE(jiabin): is_prim_infer indicates this method called by paddle.jit.save and it is worked in prim mode

941
        if cached_program_len == 0:
C
Chen Weihang 已提交
942 943 944
            desired_input_spec = input_spec
            if self._function_spec.input_spec is not None:
                if input_spec is not None and not input_specs_compatible(
945 946
                    flatten(input_spec), flatten(self._function_spec.input_spec)
                ):
947
                    raise ValueError(
948 949 950 951
                        "The `input_spec`: {} used to construct concrete_program is conflict with the `input_spec`: {} in `@paddle.jit.to_static`".format(
                            input_spec, self._function_spec.input_spec
                        )
                    )
C
Chen Weihang 已提交
952 953 954
                # NOTE(chenweihang): we should always translated program based on the `input_spec`
                # decorated on forward if it is valid
                desired_input_spec = self._function_spec.input_spec
955 956
                if input_spec is not None:
                    logging_utils.warn(
957 958 959 960
                        "\n\nYou have specified `input_spec` both in function definition (higher priority) and `paddle.jit.save` (will be ignored.)\n\n\t Using: {}\n\n\t Ignore: {}\n".format(
                            desired_input_spec, input_spec
                        )
                    )
961

962
            has_input_spec = desired_input_spec is not None
A
Aurelius84 已提交
963
            if has_input_spec:
C
Chen Weihang 已提交
964
                concrete_program, _ = self.get_concrete_program(
965 966
                    *desired_input_spec,
                    with_hook=with_hook,
967
                    is_train=self._is_train_mode(),
968
                    is_prim_infer=is_prim_infer,
969
                )
970
                return concrete_program
971
            else:
A
Aurelius84 已提交
972
                raise ValueError(
973 974 975 976
                    "No valid transformed program for {}.\n\t    Please specific `input_spec` in `@paddle.jit.to_static` or feed input tensor to call the decorated function at once.\n".format(
                        self._function_spec
                    )
                )
977 978 979
        elif with_hook:
            cache_key = self._program_cache._recent_cache_key
            cache_key.kwargs["with_hook"] = True
980 981 982 983 984 985 986 987
            if not is_prim_infer:
                concrete_program, _ = self._program_cache[cache_key]
                return concrete_program
            else:
                concrete_program, _ = self.get_concrete_program_with_cache_key(
                    cache_key
                )
                return concrete_program
988 989
        # If more than one programs have been cached, return the recent converted program by default.
        elif cached_program_len > 1:
990
            logging_utils.warn(
991 992 993 994
                "Current {} has more than one cached programs: {}, the last traced progam will be return by default.".format(
                    self._function_spec, cached_program_len
                )
            )
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
        if not is_prim_infer:
            cache_key, (
                concrete_program,
                partial_layer,
            ) = self._program_cache.last()
            return concrete_program
        else:
            cache_key = self._program_cache._recent_cache_key
            concrete_program, _ = self.get_concrete_program_with_cache_key(
                cache_key
            )
            return concrete_program
1007

1008 1009 1010 1011 1012
    @property
    def inputs(self):
        """
        Returns input tensors of recent converted static program.
        """
1013
        self._raise_when_property()
1014 1015
        concrete_program = self.concrete_program
        inputs = [
1016 1017
            var
            for var in flatten(concrete_program.inputs)
1018 1019 1020
            if isinstance(var, framework.Variable)
        ]
        return inputs
1021

1022
    @property
1023 1024 1025 1026
    def outputs(self):
        """
        Returns output tensors of recent converted static program.
        """
1027
        self._raise_when_property()
1028 1029
        concrete_program = self.concrete_program
        outputs = [
1030 1031
            var
            for var in flatten(concrete_program.outputs)
1032 1033 1034 1035
            if isinstance(var, framework.Variable)
        ]

        return outputs
1036

1037
    @property
1038 1039 1040 1041
    def main_program(self):
        """
        Returns recent converted static main program.
        """
1042
        self._raise_when_property()
1043 1044 1045
        concrete_program = self.concrete_program
        main_program = concrete_program.main_program
        return main_program
1046

1047 1048 1049
    @property
    def program_cache(self):
        return self._program_cache
1050

1051 1052 1053
    @property
    def function_spec(self):
        return self._function_spec
1054 1055


1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
def _verify_init_in_dynamic_mode(class_instance):
    """
    Verifies the instance is initialized in dynamic mode.
    """
    if isinstance(class_instance, layers.Layer):
        if not class_instance._init_in_dynamic_mode:
            raise RuntimeError(
                " `paddle.jit.to_static` is only available in dynamic mode. Please call `paddle.disable_static()` before "
                "initializing your Layer class `{}` . Because parameters of Layer class should be initialized firstly "
                "in dynamic mode while applying transformation.".format(
1066 1067 1068
                    class_instance
                )
            )
1069 1070


1071
class HookHelper:
1072 1073 1074 1075 1076 1077 1078 1079 1080
    """
    Only For converting pre/post hooks operation in outermost layer while jit.save.
    Because hooks in sublayer have been processed automatically.
    """

    def __init__(self, func, class_instance, with_hook=False):
        self.func = func
        self.class_instance = class_instance
        self.with_hook = with_hook
1081 1082 1083
        self.need_apply_hook = (
            with_hook
            and isinstance(self.class_instance, layers.Layer)
1084
            and func.__name__ == "forward"
1085
        )
1086 1087 1088 1089 1090

    def apply_pre_hooks(self, inputs):
        """
        Apply _forward_pre_hooks from outermost layer
        """
1091 1092
        if not self.need_apply_hook:
            return inputs
1093 1094 1095 1096 1097 1098

        inputs = inputs[1:]
        for forward_pre_hook in self.class_instance._forward_pre_hooks.values():
            hook_result = forward_pre_hook(self.class_instance, inputs)
            if hook_result is not None:
                if not isinstance(hook_result, tuple):
1099
                    hook_result = (hook_result,)
1100 1101 1102 1103 1104 1105 1106 1107
                inputs = hook_result

        return [self.class_instance] + list(inputs)

    def apply_post_hooks(self, inputs, outputs):
        """
        Apply _forward_post_hooks from outermost layer
        """
1108 1109
        if not self.need_apply_hook:
            return outputs
1110 1111

        inputs = inputs[1:]
1112 1113 1114 1115 1116 1117
        for (
            forward_post_hook
        ) in self.class_instance._forward_post_hooks.values():
            hook_result = forward_post_hook(
                self.class_instance, inputs, outputs
            )
1118 1119 1120 1121 1122 1123 1124
            if hook_result is not None:
                outputs = hook_result

        inputs.insert(0, self.class_instance)
        return outputs


1125
class ConcreteProgram:
1126
    __slots__ = [
1127 1128 1129 1130 1131 1132 1133
        'inputs',
        'outputs',
        'main_program',
        "startup_program",
        "parameters",
        "function",
        'kwargs',
1134 1135
    ]

1136 1137 1138 1139 1140 1141 1142 1143
    def __init__(
        self,
        inputs,
        outputs,
        parameters,
        function,
        main_program,
        startup_program=None,
1144
        **kwargs,
1145
    ):
1146 1147 1148
        self.inputs = inputs
        self.outputs = outputs
        self.main_program = main_program
1149
        self.startup_program = startup_program
1150
        self.parameters = parameters
1151
        self.function = function
1152
        self.kwargs = kwargs
1153 1154 1155

    @staticmethod
    @switch_to_static_graph
1156 1157 1158
    def from_func_spec(
        func_spec, input_spec, input_kwargs_spec, class_instance, **kwargs
    ):
1159
        """
1160 1161
        Builds the main_program with specialized inputs and returns outputs
        of program as fetch_list.
1162 1163 1164

        Args:
            func_spec(FunctionSpec): A FunctionSpec instance for decorated function.
1165
            input_spec(list[InputSpec]):
1166
        """
1167 1168 1169
        # verify the instance is initialized in imperative mode.
        _verify_init_in_dynamic_mode(class_instance)

1170
        # Transforms dygraph function into static function and caches it.
1171
        dygraph_function = func_spec.dygraph_function
1172
        static_func = convert_to_static(dygraph_function)
1173
        # apply pre\post hook for outermost layer
1174 1175 1176
        hook_helper = HookHelper(
            dygraph_function, class_instance, kwargs.get("with_hook", False)
        )
1177

1178 1179
        main_program, startup_program = framework.Program(), framework.Program()
        # Note: The random seed should be synchronized into cached program
1180
        # if set in `fluid.dygraph_guard` because some ops rely on it, such as
1181
        # `fluid.layers.dropout`.
1182
        main_program.random_seed = framework.default_main_program().random_seed
1183 1184 1185
        startup_program.random_seed = (
            framework.default_startup_program().random_seed
        )
1186

1187
        with framework.program_guard(main_program, startup_program):
1188
            with _switch_declarative_mode_guard_(is_declarative=True):
1189
                # 1. Adds `paddle.static.data` layers for input if needed
1190
                static_inputs = func_spec.to_static_inputs_with_spec(
1191 1192
                    input_spec, main_program
                )
1193
                _kwargs = func_spec.to_static_inputs_with_spec(
1194 1195
                    input_kwargs_spec, main_program
                )
1196
                if class_instance:
1197 1198 1199
                    static_inputs = tuple(
                        [class_instance] + list(static_inputs)
                    )
1200

1201
                # 2. Builds program only once and returns the output Variables.
1202 1203 1204
                with param_guard(
                    get_parameters(class_instance, False)
                ), param_guard(get_buffers(class_instance, False)):
1205
                    try:
1206 1207
                        # only for jit.save, do nothing while train and eval process
                        inputs = hook_helper.apply_pre_hooks(static_inputs)
1208 1209
                        if _kwargs:
                            outputs = static_func(*inputs, **_kwargs)
1210 1211
                        else:
                            outputs = static_func(*inputs)
1212
                        outputs = hook_helper.apply_post_hooks(inputs, outputs)
1213 1214
                    except BaseException as e:
                        # NOTE: If e is raised in compile time, e should be attached to ERROR_DATA here.
1215
                        error.attach_error_data(e)
1216 1217 1218
                        error_data = getattr(e, error.ERROR_DATA, None)
                        if error_data:
                            error_data.raise_new_exception()
1219 1220
                        raise

1221 1222 1223 1224 1225 1226 1227
                # 3. Gets all ParamBases and buffered VarBases in the function
                all_parameters_and_buffers = (
                    ProgramTranslator.get_instance()._params_recorder.pop(
                        main_program
                    )
                )

1228
                if outputs is not None:
1229 1230 1231 1232
                    need_wrap_into_list = (
                        not isinstance(outputs, (tuple, list))
                        or len(outputs) == 1
                    )
1233 1234
                    if need_wrap_into_list:
                        outputs = [outputs]
1235

1236 1237
        main_program = update_op_callstack_with_origin_info(main_program)

1238 1239 1240 1241 1242 1243 1244
        return ConcreteProgram(
            inputs=static_inputs,
            outputs=outputs,
            parameters=all_parameters_and_buffers,
            function=dygraph_function,
            main_program=main_program,
            startup_program=startup_program,
1245
            **kwargs,
1246
        )
1247 1248


1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
class ParametersRecorder:
    def __init__(self):
        self.params_dict = {}

    @synchronized
    def add(self, program, param):
        """use the default_program as key, append param the parameter list."""
        key = self._program_hash(program)
        if key not in self.params_dict:
            self.params_dict[key] = set()
        params = self.params_dict[key]
        params.add(param)

    def pop(self, program):
        params = self.params_dict.get(self._program_hash(program))
        if params is None:
            return []
        del self.params_dict[self._program_hash(program)]
        return list(params)

    def _program_hash(self, program):
        """
        because program is not deleted while calling from_func_spec.
        so it's ok to use id(program)
        """
        return id(program)


1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
class ParametersMap:
    def __init__(self):
        self.params_dict = {}

    @synchronized
    def add(self, program, id, param):
        """use the default_program as key, append param the parameter list."""
        key = self._program_hash(program)
        if key not in self.params_dict:
            self.params_dict[key] = {}

        params = self.params_dict[key]
        params[id] = param

    def get(self, program, id):
        params = self.params_dict.get(self._program_hash(program))
        if params is None:
            return None
        if id in params.keys():
            return params[id]
        return None

    def _program_hash(self, program):
        """
        because program is not deleted while calling from_func_spec.
        so it's ok to use id(program)
        """
        return id(program)


1307
class FallbackProgramLayer:
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
    __slots__ = [
        '_instance',
        '_dy_func',
        'training',
        '_cuda_graph_capture_mode',
        '_cuda_graph_pool_id',
    ]

    def __init__(self, instance, dy_func):
        self._instance = instance
        self._dy_func = dy_func

    def __call__(self, inputs):
        return self._dy_func(*inputs)

    def __getattr__(self, key):
        if key not in self.__slots__:
            raise RuntimeError(
                "There raises a exception after applying `@paddle.jit.to_static()` and already switch into fallback mode. \n"
                "You can't get attribute for a fallback program layer. Please check `to_static.error` file for detail."
            )
        elif key in ['training']:
            if self._instance is not None:
                return getattr(self._instance, key)
            return

        return super().__getattr__(key)

    def __setattr__(self, key, value):
        if key not in self.__slots__:
            raise RuntimeError(
                "There raises a exception after applying `@paddle.jit.to_static()` and already switch into fallback mode. \n"
                "You can't get attribute for a fallback program layer. Please check `to_static.error` file for detail."
            )
        elif key in ['training']:
            if self._instance is not None:
                return setattr(self._instance, key, value)
            return

        return super().__setattr__(key, value)


1350
class ProgramCache:
1351 1352 1353
    """
    Wrapper class for the program functions defined by dygraph function.
    """
1354

1355 1356
    dy2static_error_file = "to_static.error"

1357
    def __init__(self):
1358
        # {hash_id : (concrete_program, partial_layer)}
1359
        self._caches = collections.OrderedDict()
1360
        # trace mostly recent used program
1361
        self._recent_key = None
1362
        self._recent_cache_key = None
1363

1364
    def _build_once(self, cache_key):
1365 1366
        # TODO(Aurelius84): Need a gloabl FLAGS to enable/disable to_prim
        enable_prim = cache_key.kwargs['build_strategy'].build_cinn_pass
1367

1368 1369 1370 1371 1372 1373 1374 1375
        # NOTE(xiongkun): Need a global FLAGS to enable/disable fallback
        enable_fallback = enable_prim
        try:
            concrete_program = ConcreteProgram.from_func_spec(
                func_spec=cache_key.function_spec,
                input_spec=cache_key.input_args_with_spec,
                input_kwargs_spec=cache_key.input_kwargs_with_spec,
                class_instance=cache_key.class_instance,
1376
                **cache_key.kwargs,
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
            )
        except Exception as e:
            if enable_fallback:
                warnings.warn(
                    "Exception is thrown while applying @paddle.jit.to_static. It will fallback into dygraph mode for training.\n"
                    "1. You can check `to_static.error` file in current workspace directory for detail.\n"
                    "2. In fallback mode, you can only do training, can't call paddle.jit.save(). Please modify model code according `to_static.error` firstly"
                )
                # TODO(xiongkun) change different file name to avoid overwrite.
                with open(self.dy2static_error_file, "w") as fp:
                    fp.write(str(e))
1388

1389 1390 1391 1392 1393 1394 1395
                fallback_layer = FallbackProgramLayer(
                    cache_key.class_instance,
                    cache_key.function_spec.dygraph_function,
                )
                return fallback_layer, fallback_layer
            else:
                raise
1396

1397 1398
        backend = cache_key.kwargs['backend']
        if prim_or_cinn_is_enabled(cache_key.kwargs['build_strategy'], backend):
1399
            for var in concrete_program.main_program.list_vars():
1400
                if var.type not in NO_SHAPE_VAR_TYPE and -1 in var.shape:
1401 1402 1403 1404 1405
                    warnings.warn(
                        "Now prim and cinn do not support -1 shape, but the shape of var {} is {}".format(
                            var.name, var.shape
                        )
                    )
1406

1407 1408 1409
        partial_program = partial_program_from(
            concrete_program, cache_key.class_instance is not None
        )
1410 1411 1412 1413 1414
        with backend_guard(backend):
            if core._is_fwd_prim_enabled():
                partial_program.set_hooker(
                    PrimHooker(concrete_program.main_program, backend)
                )
1415 1416
        return concrete_program, partial_program

1417
    def __getitem__(self, item):
1418
        if not isinstance(item, CacheKey):
1419 1420 1421 1422
            raise ValueError(
                'type(item) should be CacheKey, but received %s'
                % type_name(item)
            )
1423
        item_id = hash(item)
1424
        self._recent_cache_key = item
1425
        self._recent_key = item_id
1426 1427
        if item_id not in self._caches:
            self._caches[item_id] = self._build_once(item)
1428 1429 1430
            # Note: raise warnings if number of traced program is more than `max_tracing_count`
            current_tracing_count = len(self._caches)
            if current_tracing_count > MAX_TRACED_PROGRAM_COUNT:
1431
                logging_utils.warn(
1432
                    "Current traced program number: {} > `max_tracing_count`:{}. Too much cached programs will bring expensive overhead. "
1433 1434 1435 1436
                    "The reason may be: (1) passing tensors with different shapes, (2) passing python objects instead of tensors.".format(
                        current_tracing_count, MAX_TRACED_PROGRAM_COUNT
                    )
                )
1437

1438
        return self._caches[item_id]
1439

1440 1441 1442
    def get_program_without_cache(self, cache_key):
        return self._build_once(cache_key=cache_key)

1443
    def get_program(self, item):
1444
        if not isinstance(item, CacheKey):
1445
            raise ValueError(
1446 1447 1448
                "Input item's type should be FunctionSpec, but received %s"
                % type_name(item)
            )
1449 1450
        item_id = hash(item)
        if item_id not in self._caches:
1451
            raise RuntimeError(
1452
                "Failed to find program for input item, please decorate input function by `@paddle.jit.to_static`."
1453
            )
1454
        return self._caches[item_id]
1455

1456
    def last(self):
1457 1458 1459
        assert (
            len(self._caches) >= 1
        ), "No valid cached program in ProgramCache."
1460 1461
        assert self._recent_key is not None
        return self._recent_key, self._caches[self._recent_key]
1462

1463 1464 1465 1466
    def __len__(self):
        return len(self._caches)

    def concrete_programs(self):
1467
        return [cp for key, (cp, _) in self._caches.items()]
1468

1469 1470 1471
    def clear(self):
        self._caches = collections.OrderedDict()

1472

1473
class PrimHooker(PartialProgramLayerHook):
1474
    def __init__(self, original_program, backend):
1475 1476 1477 1478
        if len(original_program.blocks) > 1:
            raise ValueError(
                'The primitive mode only support one block currently.'
            )
1479
        self.backend = backend
1480
        self.custom_vjps = set()
1481 1482 1483 1484 1485 1486 1487
        with backend_guard(self.backend):
            if core._is_all_prim_enabled():
                self.custom_vjps = {
                    op.type
                    for op in original_program.block(0).ops
                    if core.has_comp_grad_op_maker(op.type)
                }
1488 1489

    def before_append_backward(self, forward_program):
1490 1491 1492 1493
        with backend_guard(self.backend):
            if core._is_fwd_prim_enabled():
                _to_prim(forward_program.blocks, blacklist=self.custom_vjps)
            return forward_program
1494 1495

    def after_append_backward(self, whole_program, backward_start_idx):
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
        with backend_guard(self.backend):
            backward_length = (
                len(whole_program.block(0).ops) - backward_start_idx
            )
            if core._is_fwd_prim_enabled() and len(self.custom_vjps) != 0:
                # only process backward part of block
                _to_prim(whole_program.blocks, backward_length=backward_length)
            new_start_index = len(whole_program.block(0).ops) - backward_length
            if backward_length > 0:
                # only process forward part of block
                _to_prim(whole_program.blocks, start_idx=new_start_index)
            return whole_program, new_start_index
1508 1509

    def after_infer(self, infer_program):
1510 1511 1512 1513
        with backend_guard(self.backend):
            if core._is_fwd_prim_enabled():
                _to_prim(infer_program.block(0))
            return infer_program
1514 1515


1516
class ProgramTranslator:
1517
    """
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
    Class to translate dygraph function into static graph function. The object
    of this class is a singleton.

    Args:
        None.

    Returns:
        ProgramTranslator: the singleton object.

    Examples:
        .. code-block:: python

1530
            >>> import paddle
1531

1532 1533 1534
            >>> # Two methods get same object because ProgramTranslator is a singleton
            >>> paddle.jit.dy2static.program_translator.ProgramTranslator()
            >>> paddle.jit.dy2static.program_translator.ProgramTranslator.get_instance()
1535

1536 1537
    """

1538
    _singleton_lock = threading.Lock()
1539 1540 1541 1542 1543 1544
    _instance = None

    @synchronized
    def __new__(cls, *args, **kwargs):
        if cls._instance is None:
            cls._instance = object.__new__(cls, *args, **kwargs)
1545
            cls._instance._initialized = False
1546 1547 1548 1549 1550
        return cls._instance

    @classmethod
    def get_instance(cls):
        if cls._instance is None:
1551 1552
            with cls._singleton_lock:
                cls._instance = cls()
1553 1554 1555 1556 1557
        return cls._instance

    @classmethod
    def reset(cls):
        if cls._instance is not None:
1558
            cls._instance._initialized = False
1559 1560
            cls._instance.__init__()

1561
    def __init__(self):
1562
        # To make sure that calls __init__ only once.
1563
        if self._initialized:
1564
            return
1565 1566
        self._initialized = True
        self._program_cache = ProgramCache()
1567
        self._params_recorder = ParametersRecorder()
1568
        self._params_map = ParametersMap()
1569
        self.enable_to_static = True
1570

1571
    def enable(self, enable_to_static):
1572
        """
1573
        Enable or disable the converting from imperative to static graph by
1574 1575 1576
        ProgramTranslator globally.

        Args:
1577
            enable_to_static (bool): True or False to enable or disable converting to static.
1578 1579 1580 1581 1582 1583 1584

        Returns:
            None.

        Examples:
            .. code-block:: python

1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
                >>> # doctest: +SKIP
                >>> import paddle
                >>> def func(x):
                ...     if paddle.mean(x) > 0:
                ...         x_v = x - 1
                ...     else:
                ...         x_v = x + 1
                ...     return x_v
                ...
                ...
                >>> prog_trans = paddle.jit.dy2static.program_translator.ProgramTranslator()

                >>> x = paddle.ones([1, 2])
                >>> x_v = prog_trans.get_output(func, x)
                >>> print(x_v)
                Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[0., 0.]])
1602
        """
1603 1604 1605 1606 1607 1608
        check_type(
            enable_to_static,
            "enable_to_static",
            bool,
            "ProgramTranslator.enable",
        )
1609
        self.enable_to_static = enable_to_static
1610

1611 1612
    def get_output(self, dygraph_func, *args, **kwargs):
        """
1613
        Returns the output dygraph Tensor for dygraph function. The dygraph
1614
        function will be translated into static graph function so the under
1615
        beneath numerical result will be calculated by static graph mode.
1616 1617 1618

        Args:
            dygraph_func (callable): the dygraph function.
1619 1620
            *args (tuple): the input argument of dygraph_func.
            **kwargs (dict): the input argument of dygraph_func.
1621 1622

        Returns:
1623
            Tensor or tuple of Tensors: the dygraph Tensor containing digital result.
1624 1625 1626 1627

        Examples:
            .. code-block:: python

1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
                >>> # doctest: +SKIP
                >>> import paddle
                >>> def func(x):
                ...     if paddle.mean(x) > 0:
                ...         x_v = x - 1
                ...     else:
                ...         x_v = x + 1
                ...     return x_v
                ...
                ...
                >>> prog_trans = paddle.jit.dy2static.program_translator.ProgramTranslator()

                >>> x = paddle.ones([1, 2])
                >>> x_v = prog_trans.get_output(func, x)
                >>> print(x_v)
                Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[0., 0.]])
1645
        """
1646 1647 1648
        assert callable(
            dygraph_func
        ), "Input dygraph_func is not a callable in ProgramTranslator.get_output"
1649

1650
        if not self.enable_to_static:
1651 1652
            # Here calls `warnings.warn` but not `logging_utils.warn` because by default warnings.warn(message)
            # will show up **only once**.
1653
            logging_utils.warn(
1654 1655 1656 1657
                "The ProgramTranslator.get_output doesn't work when setting ProgramTranslator.enable to False. "
                "We will just return dygraph output. "
                "Please call ProgramTranslator.enable(True) if you would like to get static output."
            )
1658
            return dygraph_func(*args, **kwargs)
1659
        try:
1660
            function_spec = FunctionSpec(dygraph_func)
1661
            cache_key = CacheKey.from_func_and_args(
1662 1663 1664 1665 1666
                function_spec,
                args,
                kwargs,
                getattr(dygraph_func, '__self__', None),
            )
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
            _, partial_program_layer = self._program_cache[cache_key]

            if args and isinstance(args[0], layers.Layer):
                # Synchronize self.training attribute.
                partial_program_layer.training = args[0].training
                args = args[1:]
            try:
                return partial_program_layer(args)
            except BaseException as e:
                # NOTE:
                # 1. If e is raised in compile time, e should have been attached to ERROR_DATA before;
                # 2. If e raised in runtime, e should be attached to ERROR_DATA here.
                if not hasattr(e, error.ERROR_DATA):
                    # runtime error
                    error.attach_error_data(e, in_runtime=True)
                raise
1683
        except BaseException as e:
1684 1685 1686 1687 1688 1689
            error_data = getattr(e, error.ERROR_DATA, None)
            if error_data:
                error_data.raise_new_exception()
            else:
                logging_utils.warn(
                    "Please file an issue at 'https://github.com/PaddlePaddle/Paddle/issues'"
1690 1691
                    " if you can't handle this {} yourself.".format(type(e))
                )
1692
                raise e
1693 1694 1695

    def get_func(self, dygraph_func):
        """
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
        Returns a callable function which converts imperative dygraph APIs of
        the input dygraph_func into declarative net-building APIs, which means
        it doesn't return immediate digital result as get_output does.
        Users should handle Program and Executor by themselves.

        Args:
            dygraph_func (callable): the dygraph function.

        Returns:
            callable: converting imperative dygraph APIs into declarative
            net-building APIs.
1707 1708 1709 1710

        Examples:
            .. code-block:: python

1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
                >>> # doctest: +SKIP
                >>> import paddle
                >>> def func(x):
                ...     if paddle.mean(x) > 0:
                ...         x_v = x - 1
                ...     else:
                ...         x_v = x + 1
                ...     return x_v
                ...
                >>> prog_trans = paddle.jit.dy2static.program_translator.ProgramTranslator()
                >>> static_func = prog_trans.get_func(func)
                >>> print(callable(static_func))
                True
1724
        """
1725 1726 1727
        assert callable(
            dygraph_func
        ), "Input dygraph_func is not a callable in ProgramTranslator.get_func"
1728

1729
        if not self.enable_to_static:
1730
            logging_utils.warn(
1731 1732 1733
                "The ProgramTranslator.get_func doesn't work when setting ProgramTranslator.enable to False. We will "
                "just return dygraph output. Please call ProgramTranslator.enable(True) if you would like to get static output."
            )
1734
            return dygraph_func
1735

1736
        static_func = convert_to_static(dygraph_func)
1737 1738
        return static_func

1739 1740
    def get_program(self, dygraph_func, *args, **kwargs):
        """
1741
        Returns the translated static program and input/output Tensors from
1742 1743 1744 1745
        dygraph function. The users can use the program to run by executor.

        Args:
            dygraph_func (callable): the dygraph function.
1746 1747
            *args (tuple): the input argument of dygraph_func.
            **kwargs (dict): the input argument of dygraph_func.
1748 1749 1750

        Returns:
            tuple of (main_program, startup_program, inputs, outputs) whose
1751
            types are (Program, Program, list of Tensors, list of Tensors).
1752 1753
            main_program: the converted main program.
            startup_program: the converted startup program.
1754 1755
            inputs: list of input Tensors which need to be fed.
            outputs: list of output Tensors which users can fetch.
1756 1757 1758 1759

        Examples:
            .. code-block:: python

1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
                >>> # doctest: +SKIP
                >>> import paddle
                >>> def func(x):
                ...     if paddle.mean(x) > 0:
                ...         x_v = x - 1
                ...     else:
                ...         x_v = x + 1
                ...     return x_v
                ...
                >>> prog_trans = paddle.jit.dy2static.program_translator.ProgramTranslator()
                >>> x = paddle.ones([1, 2])
                >>> main_prog, start_prog, inputs, outputs = prog_trans.get_program(func, x)
                >>> print([i.name for i in inputs])
                >>> # [u'generated_tensor_0'] the feed input Tensor name representing x
                >>> print([o.name for o in outputs])
                >>> # [u'_generated_var_4'] the fetch output Tensor name representing x_v
1776
        """
1777 1778 1779
        assert callable(
            dygraph_func
        ), "Input dygraph_func is not a callable in ProgramTranslator.get_program"
1780

1781
        if not self.enable_to_static:
1782
            logging_utils.warn(
1783 1784 1785 1786
                "The ProgramTranslator.get_program doesn't work when setting ProgramTranslator.enable to False."
                "We will just return dygraph output. "
                "Please call ProgramTranslator.enable(True) if you would like to get static output."
            )
1787
            return dygraph_func(*args, **kwargs)
1788

1789
        function_spec = FunctionSpec(dygraph_func)
1790
        cache_key = CacheKey.from_func_and_args(
1791 1792
            function_spec, args, kwargs, getattr(dygraph_func, '__self__', None)
        )
1793 1794
        concrete_program, partial_program_layer = self._program_cache[cache_key]

1795 1796
        # Note: concrete_program hold all input/output infos include non-Variable
        input_vars = [
1797 1798
            var
            for var in concrete_program.inputs
1799 1800 1801
            if isinstance(var, framework.Variable)
        ]
        output_vars = [
1802 1803
            var
            for var in concrete_program.outputs
1804 1805 1806
            if isinstance(var, framework.Variable)
        ]

1807 1808 1809 1810 1811 1812
        return (
            concrete_program.main_program,
            concrete_program.startup_program,
            input_vars,
            output_vars,
        )
1813

1814 1815
    def get_code(self, dygraph_func):
        """
1816 1817 1818 1819 1820 1821
        Returns the translated static function string code from dygraph function.

        Args:
            dygraph_func (callable): the dygraph function.

        Returns:
1822 1823 1824 1825 1826
            str: the string code of translated static function.

        Examples:
            .. code-block:: python

1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
                >>> # doctest: +SKIP
                >>> import paddle
                >>> def func(x):
                ...     if paddle.mean(x) > 0:
                ...         x_v = x - 1
                ...     else:
                ...         x_v = x + 1
                ...     return x_v
                ...
                >>> prog_trans = paddle.jit.dy2static.program_translator.ProgramTranslator()

                >>> code = prog_trans.get_code(func)
                >>> print(type(code))
                <class 'str'>
1841
        """
1842 1843 1844
        assert callable(
            dygraph_func
        ), "Input dygraph_func is not a callable in ProgramTranslator.get_code"
1845
        # Gets AST from dygraph function
1846 1847 1848

        unwrap_func = unwrap(dygraph_func)
        raw_code = inspect.getsource(unwrap_func)
1849 1850 1851 1852 1853
        code = textwrap.dedent(raw_code)
        root = gast.parse(code)

        # Transform AST
        dygraph_to_static = DygraphToStaticAst()
1854
        root = dygraph_to_static.get_static_ast(root)
1855 1856

        # Get source_code
1857
        source_code = ast_to_source_code(root)
1858 1859
        return source_code

1860
    def get_program_cache(self):
1861
        """
1862 1863 1864 1865 1866 1867 1868 1869 1870
        Returns the ProgramCache instance. This method is used by PaddlePaddle
        developers to manage program cache in ProgramTranslator. Normal users
        don't have to call this method.

        Returns:
            ProgramCache: ProgramCache instance of ProgramTranslator.

        Examples:
            .. code-block:: python
1871

1872
                >>> import paddle
1873

1874 1875
                >>> prog_trans = paddle.jit.dy2static.program_translator.ProgramTranslator()
                >>> prog_cache = prog_trans.get_program_cache()
1876
        """
1877
        return self._program_cache
R
Ryan 已提交
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893


def enable_to_static(enable_to_static_bool):
    """
    Enable or disable the converting from imperative to static graph by
    ProgramTranslator globally.

    Args:
        enable_to_static_bool (bool): True or False to enable or disable converting to static.

    Returns:
        None.

    Examples:
        .. code-block:: python

1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
            >>> import paddle
            >>> @paddle.jit.to_static
            >>> def func(x):
            ...     if paddle.mean(x) > 0:
            ...         x_v = x - 1
            ...     else:
            ...         x_v = x + 1
            ...     return x_v
            ...
            >>> paddle.jit.enable_to_static(False)

            >>> x = paddle.ones([1, 2])
            >>> # ProgramTranslator is disabled so the func is run in dygraph
            >>> print(func(x))
            Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[0., 0.]])
R
Ryan 已提交
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919

    """
    check_type(
        enable_to_static_bool,
        "enable_to_static_bool",
        bool,
        "paddle.jit.enable_to_static",
    )
    _program_trans = ProgramTranslator()
    _program_trans.enable(enable_to_static_bool)
1920 1921 1922


@switch_to_static_graph
1923 1924 1925 1926 1927 1928 1929
def _to_prim(
    blocks,
    blacklist=frozenset(),
    whitelist=frozenset(),
    start_idx=-1,
    backward_length=-1,
):
1930
    """Swith to static graph and call to_prim."""
1931 1932 1933
    # TODO(Aurelius84): Fix this cycle import problem
    from paddle.incubate.autograd import primapi

1934 1935 1936 1937 1938 1939 1940
    primapi.to_prim(
        blocks,
        blacklist=blacklist,
        whitelist=whitelist,
        start_idx=start_idx,
        backward_length=backward_length,
    )