-
Notifications
You must be signed in to change notification settings - Fork 2
/
generic.py
238 lines (195 loc) · 6.3 KB
/
generic.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"""
Generic type & functions for torch.Tensor and np.ndarray
"""
from typing import Sequence, TypeVar, Union
import numpy as np
import torch
from numpy import ndarray
from torch import Tensor
TensArr = TypeVar('TensArr', Tensor, ndarray)
TensArrOrSeq = Union[TensArr, Sequence[TensArr]]
dict_package = {Tensor: torch, ndarray: np}
dict_cat_stack_fn = {(Tensor, 'cat'): torch.cat,
(ndarray, 'cat'): np.concatenate,
(Tensor, 'stack'): torch.stack,
(ndarray, 'stack'): np.stack,
}
class DataPerDevice:
__slots__ = ('data',)
def __init__(self, data_np: ndarray):
self.data = {ndarray: data_np}
def __getitem__(self, typeOrtup):
if type(typeOrtup) == tuple:
_type, device = typeOrtup
elif typeOrtup == ndarray:
_type = ndarray
device = None
else:
raise IndexError
if _type == ndarray:
return self.data[ndarray]
else:
if typeOrtup not in self.data:
self.data[typeOrtup] = convert(self.data[ndarray].astype(np.float32),
Tensor,
device=device)
return self.data[typeOrtup]
def get_like(self, other: TensArr):
if type(other) == Tensor:
return self[Tensor, other.device]
else:
return self[ndarray]
def convert_dtype(dtype: type, pkg) -> type:
if hasattr(dtype, '__name__'):
if pkg == np:
return dtype
else:
return eval(f'torch.{dtype.__name__}')
else:
if pkg == np:
return eval(f'np.{str(dtype).split(".")[-1]}')
else:
return dtype
def copy(a: TensArr, requires_grad=True) -> TensArr:
if type(a) == Tensor:
return a.clone() if requires_grad else torch.tensor(a)
elif type(a) == ndarray:
return np.copy(a)
else:
raise TypeError
def convert(a: TensArr, astype: type, device: Union[int, torch.device] = None) -> TensArr:
if astype == Tensor:
if type(a) == Tensor:
return a.to(device)
else:
return torch.as_tensor(a, dtype=torch.float32, device=device)
elif astype == ndarray:
if type(a) == Tensor:
return a.cpu().numpy()
else:
return a
else:
raise ValueError(astype)
def convert_like(a: TensArr, b: TensArr) -> TensArr:
if type(b) == Tensor:
if type(a) == Tensor:
return a.to(b.device)
else:
return torch.as_tensor(a, device=b.device)
elif type(b) == ndarray:
if type(a) == Tensor:
return a.cpu().numpy()
else:
return a
else:
raise ValueError(type(b))
def ndim(a: TensArr) -> int:
if type(a) == Tensor:
return a.dim()
elif type(a) == ndarray:
return a.ndim
else:
raise TypeError
def transpose(a: TensArr, axes: Union[int, Sequence[int]] = None) -> TensArr:
if type(a) == Tensor:
if not axes:
if a.dim() >= 2:
return a.permute((1, 0) + tuple(range(2, a.dim())))
else:
return a
else:
return a.permute(axes)
elif type(a) == ndarray:
if a.ndim == 1 and not axes:
return a
else:
return a.transpose(axes)
else:
raise TypeError
def einsum(subscripts: str,
operands: Sequence[TensArr],
astype: type = None) -> TensArr:
if not astype:
astype = type(operands[0])
if astype != Tensor and astype != ndarray:
raise TypeError
else:
types = [type(item) for item in operands]
for idx, type_ in enumerate(types):
if type_ != astype:
if type(operands) != list:
operands = list(operands)
operands[idx] = convert(operands[idx], astype)
return dict_package[astype].einsum(subscripts, operands)
def arctan2(a: TensArr, b: TensArr, out: TensArr = None) -> TensArr:
if type(a) == Tensor:
return torch.atan2(a, b, out=out)
else:
return np.arctan2(a, b, out=out)
def unwrap(a: TensArr, axis: int = -1) -> TensArr:
if type(a) == Tensor:
if axis < 0:
axis += a.dim()
def get_slice(*args, **kwargs):
return ((slice(None),) * axis
+ (slice(*args, **kwargs),)
+ (slice(None),) * (a.dim() - axis - 1)
)
diff = a[get_slice(1, None)] - a[get_slice(None, -1)]
dps = (diff + np.pi) % (2 * np.pi) - np.pi
dps[(dps == -np.pi) & (diff > 0)] = np.pi
dps -= diff
corr = dps
corr[torch.abs(diff) < np.pi] = 0
cumsum = torch.cumsum(corr, dim=axis, out=corr)
result = torch.empty_like(a)
result[get_slice(0, 1)] = a[get_slice(0, 1)]
result[get_slice(1, None)] = a[get_slice(1, None)] + cumsum
return result
elif type(a) == ndarray:
return np.unwrap(a, axis=axis)
else:
raise TypeError
def where(a: TensArr):
if type(a) == Tensor:
return tuple([item.squeeze() for item in a.nonzero().split(1, 1)])
elif type(a) == ndarray:
return np.where(a)
else:
raise TypeError
def expand_dims(a: TensArr, axis: int) -> TensArr:
if type(a) == Tensor:
return a.unsqueeze_(axis)
elif type(a) == ndarray:
return np.expand_dims(a, axis)
else:
raise TypeError
def _cat_stack(fn: str,
a: Sequence[TensArr],
axis=0,
astype: type = None) -> TensArr:
types = [type(item) for item in a]
if not astype:
astype = types[0]
for idx, type_ in enumerate(types):
if type_ != astype:
if type(a) == tuple:
a = list(a)
a[idx] = convert(a[idx], astype)
return dict_cat_stack_fn[(astype, fn)](a, axis)
def cat(*args, **kargs) -> TensArr:
"""
<parameters>
a: Iterable[TensArr]
axis=0
astype: type=None
"""
return _cat_stack('cat', *args, **kargs)
def stack(*args, **kargs) -> TensArr:
"""
<parameters>
a: Iterable[TensArr]
axis=0
astype: type=None
"""
return _cat_stack('stack', *args, **kargs)