default_scope_funcs.py 2.5 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13
# 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.
Y
Yu Yang 已提交
14 15 16
"""
Default scope function.

17 18 19
`Paddle` manages Scope as programming language's scope.  It just a
thread-local stack of Scope. Top of that stack is current scope, the bottom
of that stack is all scopes' parent.
Y
Yu Yang 已提交
20

21 22 23
Invoking `var/find_var`  can `new/find` variable in current scope.
Invoking `enter_local_scope/leave_local_scope` can create or destroy local
scope.
Y
Yu Yang 已提交
24

25 26
A `scoped_function` will take a `function` as input. That function will be
invoked in a new local scope.
Y
Yu Yang 已提交
27 28
"""

29 30
from __future__ import print_function

31
import paddle.fluid.core
Y
Yu Yang 已提交
32 33 34 35 36
import threading

__tl_scope__ = threading.local()

__all__ = [
37 38 39 40 41 42
    'get_cur_scope',
    'enter_local_scope',
    'leave_local_scope',
    'var',
    'find_var',
    'scoped_function',
Y
Yu Yang 已提交
43 44 45 46 47 48
]


def get_cur_scope():
    """
    Get current scope.
49
    :rtype: paddle.fluid.core.Scope
Y
Yu Yang 已提交
50 51 52 53 54
    """
    cur_scope_stack = getattr(__tl_scope__, 'cur_scope', None)
    if cur_scope_stack is None:
        __tl_scope__.cur_scope = list()
    if len(__tl_scope__.cur_scope) == 0:
55
        __tl_scope__.cur_scope.append(paddle.fluid.core.Scope())
Y
Yu Yang 已提交
56 57 58 59 60 61 62 63
    return __tl_scope__.cur_scope[-1]


def enter_local_scope():
    """
    Enter a new local scope
    """
    cur_scope = get_cur_scope()
Y
Yu Yang 已提交
64
    new_scope = cur_scope.new_scope()
Y
Yu Yang 已提交
65 66 67 68 69 70 71 72
    __tl_scope__.cur_scope.append(new_scope)


def leave_local_scope():
    """
    Leave local scope
    """
    __tl_scope__.cur_scope.pop()
Y
Yu Yang 已提交
73
    get_cur_scope().drop_kids()
Y
Yu Yang 已提交
74 75


D
dongzhihong 已提交
76
def var(name):
Y
Yu Yang 已提交
77 78 79
    """
    create variable in current scope.
    """
D
dongzhihong 已提交
80
    return get_cur_scope().var(name)
Y
Yu Yang 已提交
81 82


Y
Yu Yang 已提交
83
def find_var(name):
Y
Yu Yang 已提交
84 85 86
    """
    get variable in current scope.
    """
Y
Yu Yang 已提交
87
    return get_cur_scope().find_var(name)
Y
Yu Yang 已提交
88 89 90 91 92


def scoped_function(func):
    """
    invoke `func` in new scope.
93

Y
Yu Yang 已提交
94 95 96 97 98 99 100 101 102 103
    :param func: a callable function that will be run in new scope.
    :type func: callable
    """
    enter_local_scope()
    try:
        func()
    except:
        raise
    finally:
        leave_local_scope()