MCPcopy Create free account
hub / github.com/pytorch/pytorch / _ContextInfo

Class _ContextInfo

caffe2/python/context.py:9–35  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7
8
9class _ContextInfo:
10 def __init__(self, cls, allow_default):
11 self.cls = cls
12 self.allow_default = allow_default
13 self._local_stack = threading.local()
14
15 @property
16 def _stack(self):
17 if not hasattr(self._local_stack, 'obj'):
18 self._local_stack.obj = []
19 return self._local_stack.obj
20
21 def enter(self, value):
22 self._stack.append(value)
23
24 def exit(self, value):
25 assert len(self._stack) > 0, 'Context %s is empty.' % self.cls
26 assert self._stack.pop() == value
27
28 def get_active(self, required=True):
29 if len(self._stack) == 0:
30 if not required:
31 return None
32 assert self.allow_default, (
33 'Context %s is required but none is active.' % self.cls)
34 self.enter(self.cls())
35 return self._stack[-1]
36
37
38class _ContextRegistry:

Callers 1

getMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…