/
context.py
121 lines (94 loc) · 3.25 KB
/
context.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# ------------------------------------------------------------------------
# Copyright (c) 2017-present, SeetaTech. All Rights Reserved.
#
# Licensed under the BSD 2-Clause License,
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://opensource.org/licenses/BSD-2-Clause
#
# 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.
# ------------------------------------------------------------------------
"""Framework context."""
from dragon.core.framework import config
from dragon.core.framework import device_spec
from dragon.core.framework import mapping
from dragon.core.util import tls
def device(device_type, device_index=0):
"""Context-manager to nest the device.
Examples:
```python
with dragon.device('cuda', 0):
x = dragon.constant(1)
```
Parameters
----------
device_type : str
The device type.
device_index : int, optional, default=0
The device index.
Returns
-------
dragon.DeviceSpec
The current nesting device spec.
"""
device_type = device_type.lower()
if device_type not in mapping.DEVICE_STRING_TO_DEVICE_TYPE:
raise ValueError("Unsupported device type: " + device_type)
device_type = mapping.DEVICE_STRING_TO_DEVICE_TYPE[device_type]
spec = device_spec.DeviceSpec(device_type, device_index)
return _GLOBAL_DEVICE_STACK.get_controller(spec)
def variable_scope(name):
"""Context-manager to nest the namespace for variables.
Parameters
----------
name : str
The namespace for tensors traced by python.
"""
return _GLOBAL_VARIABLE_SCOPE_STACK.get_controller(name)
def name_scope(name):
"""Context-manager to nest the name prefix for operations.
Examples:
```python
with dragon.name_scope('my_scope'):
x = dragon.constant(1)
print(x.name)
```
Parameters
----------
name : str
The prefix name.
Returns
-------
str
The current nesting prefix.
"""
if name != "":
prefix = name + "/"
else:
prefix = "" # Avoid duplicated separators.
default = get_name_scope() + prefix
return _GLOBAL_NAME_SCOPE_STACK.get_controller(default)
def get_device():
"""Return the nesting or default device."""
spec = _GLOBAL_DEVICE_STACK.get_default()
if spec is None:
cfg = config.config()
spec = device_spec.DeviceSpec(cfg.device_type, cfg.device_index)
return spec
def get_variable_scope(persistent=False):
"""Return the variable scope in current nesting."""
base = _GLOBAL_VARIABLE_SCOPE_STACK.get_default()
return base + "Ref" if persistent else base
def get_name_scope():
"""Return the name scope in current nesting."""
ret = _GLOBAL_NAME_SCOPE_STACK.get_default()
return ret if ret is not None else ""
# Thread-local stack for nesting scope.
_GLOBAL_DEVICE_STACK = tls.Stack()
_GLOBAL_VARIABLE_SCOPE_STACK = tls.Stack(["Variable"])
_GLOBAL_NAME_SCOPE_STACK = tls.Stack()