2017-09-28 23:00:15 +00:00
|
|
|
# Copyright (c) 2016-present, Facebook, Inc.
|
|
|
|
|
#
|
|
|
|
|
# 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.
|
|
|
|
|
##############################################################################
|
|
|
|
|
|
2017-03-29 13:44:02 +00:00
|
|
|
## @package scope
|
|
|
|
|
# Module caffe2.python.scope
|
2016-07-21 18:26:41 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
from __future__ import division
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
|
|
import contextlib
|
2016-11-14 22:58:04 +00:00
|
|
|
import threading
|
2017-06-29 23:52:01 +00:00
|
|
|
from past.builtins import basestring
|
2016-07-21 18:26:41 +00:00
|
|
|
|
|
|
|
|
from caffe2.proto import caffe2_pb2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# The name scope and device scope when creating a new operator.
|
|
|
|
|
_NAMESCOPE_SEPARATOR = '/'
|
|
|
|
|
|
2016-11-14 22:58:04 +00:00
|
|
|
_threadlocal_scope = threading.local()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def CurrentNameScope():
|
|
|
|
|
global _threadlocal_scope
|
|
|
|
|
if not hasattr(_threadlocal_scope, "namescope"):
|
|
|
|
|
_threadlocal_scope.namescope = ''
|
|
|
|
|
return _threadlocal_scope.namescope
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def CurrentDeviceScope():
|
|
|
|
|
global _threadlocal_scope
|
|
|
|
|
if not hasattr(_threadlocal_scope, "devicescope"):
|
|
|
|
|
_threadlocal_scope.devicescope = None
|
|
|
|
|
return _threadlocal_scope.devicescope
|
|
|
|
|
|
2016-07-21 18:26:41 +00:00
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
|
def NameScope(prefix, reset=False):
|
2016-11-14 22:58:04 +00:00
|
|
|
global _threadlocal_scope
|
2016-07-21 18:26:41 +00:00
|
|
|
assert isinstance(prefix, basestring), \
|
|
|
|
|
"NameScope takes in a string as its argument."
|
2016-11-14 22:58:04 +00:00
|
|
|
old_scope = CurrentNameScope()
|
2016-09-06 22:54:56 +00:00
|
|
|
prefix = prefix + _NAMESCOPE_SEPARATOR if prefix is not '' else ''
|
2016-07-21 18:26:41 +00:00
|
|
|
if reset:
|
2016-11-14 22:58:04 +00:00
|
|
|
_threadlocal_scope.namescope = prefix
|
2016-07-21 18:26:41 +00:00
|
|
|
else:
|
2016-11-14 22:58:04 +00:00
|
|
|
_threadlocal_scope.namescope = _threadlocal_scope.namescope + prefix
|
2017-04-25 05:34:40 +00:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
yield
|
|
|
|
|
finally:
|
|
|
|
|
assert _threadlocal_scope.namescope.endswith(prefix), \
|
|
|
|
|
"The namescope variable is changed from outside NameScope() calls."
|
|
|
|
|
_threadlocal_scope.namescope = old_scope
|
2016-07-21 18:26:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
|
def DeviceScope(scope):
|
|
|
|
|
assert isinstance(scope, caffe2_pb2.DeviceOption), \
|
|
|
|
|
"DeviceScope takes in a caffe2_pb2.DeviceOption as its argument."
|
2016-11-14 22:58:04 +00:00
|
|
|
global _threadlocal_scope
|
|
|
|
|
old_scope = CurrentDeviceScope()
|
|
|
|
|
_threadlocal_scope.devicescope = scope
|
2017-04-25 05:34:40 +00:00
|
|
|
try:
|
|
|
|
|
yield
|
|
|
|
|
finally:
|
|
|
|
|
assert _threadlocal_scope.devicescope == scope, \
|
|
|
|
|
"The device scope is changed from outside DeviceScope() calls."
|
|
|
|
|
_threadlocal_scope.devicescope = old_scope
|