-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathutils.py
58 lines (45 loc) · 1.73 KB
/
utils.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
import contextlib
from typing import Any
import torch
from torch.utils._pytree import PyTree, tree_flatten, tree_unflatten
# Dumping ground for utilities that should eventual make their way into
# PyTorch proper
@contextlib.contextmanager
def no_dispatch():
guard = torch._C._DisableTorchDispatch()
try:
yield
finally:
del guard
def tree_map2(fn: Any, pytree1: PyTree, pytree2: PyTree) -> PyTree:
flat_args1, spec1 = tree_flatten(pytree1)
flat_args2, spec2 = tree_flatten(pytree2)
assert spec1 == spec2
return tree_unflatten([fn(i, j) for i, j in zip(flat_args1, flat_args2)], spec1)
# IDK if this is actually useful or not
def unmake_subclass(tensor):
with no_dispatch():
return torch.Tensor._make_subclass(torch.Tensor, tensor)
def fill_defaults(args, n, defaults_tail):
"""
__torch_dispatch__ doesn't guarantee the number of arguments you are
passed (e.g., defaulted arguments are not passed); but usually it is
convenient to pad out the arguments list with defaults. This function
helps you do that.
Args:
args: the list of positional arguments passed to __torch_dispatch__
n: the number of arguments you are expecting to get
defaults_tail: default values for the arguments, starting from the
end of the list
Example:
>>> fill_defaults([1, 2, 3], 5, [3, 4, 5])
[1, 2, 3, 4, 5]
>>> fill_defaults([1, 2, 3], 5, [None, None, None])
[1, 2, 3, None, None]]
"""
if n - len(defaults_tail) > len(args):
raise RuntimeError("not enough defaults to fill arguments")
r = list(args)
for i in range(len(args), n):
r.append(defaults_tail[i - n + len(defaults_tail)])
return r