default_scope_funcs.py 2.4 KB
Newer Older
D
dzhwinter 已提交
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
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
"""

Q
Qiao Longfei 已提交
29
import paddle.v2.fluid.core
Y
Yu Yang 已提交
30 31 32 33 34
import threading

__tl_scope__ = threading.local()

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


def get_cur_scope():
    """
    Get current scope.
Q
Qiao Longfei 已提交
47
    :rtype: paddle.v2.fluid.core.Scope
Y
Yu Yang 已提交
48 49 50 51 52
    """
    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:
Q
Qiao Longfei 已提交
53
        __tl_scope__.cur_scope.append(paddle.v2.fluid.core.Scope())
Y
Yu Yang 已提交
54 55 56 57 58 59 60 61
    return __tl_scope__.cur_scope[-1]


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


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


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


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


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

Y
Yu Yang 已提交
92 93 94 95 96 97 98 99 100 101
    :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()