-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathntops.py
More file actions
55 lines (38 loc) · 1.42 KB
/
ntops.py
File metadata and controls
55 lines (38 loc) · 1.42 KB
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
import sys
import infinicore
def use_ntops():
import ntops
return _TemporaryAttributes(
(("ntops.torch.torch", infinicore),)
+ tuple(
(f"infinicore.{op_name}", getattr(ntops.torch, op_name))
for op_name in ntops.torch.__all__
)
)
class _TemporaryAttributes:
def __init__(self, attribute_mappings):
self._attribute_mappings = attribute_mappings
self._original_values = {}
def __enter__(self):
for attr_path, new_value in self._attribute_mappings:
parent, attr_name = self._resolve_path(attr_path)
try:
self._original_values[attr_path] = getattr(parent, attr_name)
except AttributeError:
pass
setattr(parent, attr_name, new_value)
return self
def __exit__(self, exc_type, exc_value, traceback):
for attr_path, _ in self._attribute_mappings:
parent, attr_name = self._resolve_path(attr_path)
if attr_path in self._original_values:
setattr(parent, attr_name, self._original_values[attr_path])
else:
delattr(parent, attr_name)
@staticmethod
def _resolve_path(path):
*parent_parts, attr_name = path.split(".")
curr = sys.modules[parent_parts[0]]
for part in parent_parts[1:]:
curr = getattr(curr, part)
return curr, attr_name