-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
285 lines (244 loc) · 9.91 KB
/
Copy pathutils.py
File metadata and controls
285 lines (244 loc) · 9.91 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import torch
import torch.nn as nn
import torch.nn.functional as F
from spdnet import StiefelParameter
def symmetric_part(X: torch.Tensor) -> torch.Tensor:
return 0.5 * (X + X.transpose(-2, -1))
def ReEig(X: torch.Tensor, epsilon: float = 1e-6) -> torch.Tensor:
w, V = torch.linalg.eigh(symmetric_part(X))
w = torch.clamp(w, min=epsilon)
return (V @ torch.diag_embed(w) @ V.transpose(-2, -1)).contiguous()
def LogEig(X: torch.Tensor, epsilon: float = 1e-8) -> torch.Tensor:
w, V = torch.linalg.eigh(X)
w = torch.clamp(w, min=epsilon)
return (V @ torch.diag_embed(torch.log(w)) @ V.transpose(-2, -1)).contiguous()
def ExpEig(X: torch.Tensor) -> torch.Tensor:
w, V = torch.linalg.eigh(X)
return (V @ torch.diag_embed(torch.exp(w)) @ V.transpose(-2, -1)).contiguous()
def MatPow(X: torch.Tensor, p: float, epsilon: float = 1e-8) -> torch.Tensor:
w, V = torch.linalg.eigh(X)
w = torch.clamp(w, min=epsilon)
return (V @ torch.diag_embed(w**p) @ V.transpose(-2, -1)).contiguous()
def MatInvSqrt(X: torch.Tensor, epsilon: float = 1e-8) -> torch.Tensor:
return MatPow(X, -0.5, epsilon)
def MatSqrt(X: torch.Tensor, epsilon: float = 1e-8) -> torch.Tensor:
return MatPow(X, 0.5, epsilon)
def stiefel_retract(W: torch.Tensor) -> torch.Tensor:
if W.dim() == 2:
Q, _ = torch.linalg.qr(W)
return Q[:, :W.shape[1]]
elif W.dim() == 3:
Qs = []
for b in range(W.shape[0]):
Q, _ = torch.linalg.qr(W[b])
Qs.append(Q[:, :W.shape[2]])
return torch.stack(Qs, dim=0)
else:
return W
def geo_mix_logeuclid(S1: torch.Tensor, S2: torch.Tensor, alpha: torch.Tensor) -> torch.Tensor:
L1, L2 = LogEig(S1), LogEig(S2)
return ExpEig((1 - alpha) * L1 + alpha * L2)
def geo_mix_bures(S1: torch.Tensor, S2: torch.Tensor, alpha: torch.Tensor) -> torch.Tensor:
Xh = MatSqrt(S1)
invXh = MatInvSqrt(S1)
M = ReEig(invXh @ S2 @ invXh)
w, V = torch.linalg.eigh(M)
w = torch.clamp(w, min=1e-8)
if isinstance(alpha, torch.Tensor):
if alpha.dim() == 0:
alpha = alpha.view(1, 1, 1)
a = alpha
else:
a = torch.tensor(alpha, device=S1.device, dtype=S1.dtype).view(1, 1, 1)
w_a = w ** a
M_a = V @ torch.diag_embed(w_a) @ V.transpose(-2, -1)
return ReEig(Xh @ M_a @ Xh.transpose(-2, -1))
def geodesic_mix(S1: torch.Tensor, S2: torch.Tensor, alpha, metric: str = 'logeuclid') -> torch.Tensor:
if metric == 'logeuclid':
return geo_mix_logeuclid(S1, S2, alpha)
elif metric == 'bures':
return geo_mix_bures(S1, S2, alpha)
else:
raise NotImplementedError(f"Unknown metric: {metric}")
def tril_vectorize(S: torch.Tensor) -> torch.Tensor:
if S.dim() == 3:
B, m, _ = S.shape
idx = torch.tril_indices(m, m, offset=0, device=S.device)
return S[:, idx[0], idx[1]]
elif S.dim() == 4:
B, L, m, _ = S.shape
idx = torch.tril_indices(m, m, offset=0, device=S.device)
return S[:, :, idx[0], idx[1]]
else:
raise ValueError("Unsupported dims for tril_vectorize")
def sym_from_tril_vec(v: torch.Tensor, m: int) -> torch.Tensor:
dim = v.dim()
if dim == 2:
B, _ = v.shape
S = torch.zeros(B, m, m, device=v.device, dtype=v.dtype)
elif dim == 3:
B, L, _ = v.shape
S = torch.zeros(B, L, m, m, device=v.device, dtype=v.dtype)
else:
raise ValueError("Unsupported dims for sym_from_tril_vec")
tril_idx = torch.tril_indices(m, m, device=v.device)
if dim == 2:
S[:, tril_idx[0], tril_idx[1]] = v
else:
S[:, :, tril_idx[0], tril_idx[1]] = v
S = S + S.transpose(-1, -2) - torch.diag_embed(S.diagonal(dim1=-2, dim2=-1))
return S
class BiMap(nn.Module):
def __init__(self, in_dim: int, out_dim: int, in_channels: int = 1):
super().__init__()
self.in_dim = in_dim
self.out_dim = out_dim
if in_channels > 1:
self.W = StiefelParameter(torch.Tensor(in_channels, in_dim, out_dim), requires_grad=True)
else:
self.W = StiefelParameter(torch.Tensor(in_dim, out_dim), requires_grad=True)
nn.init.orthogonal_(self.W)
def forward(self, X: torch.Tensor) -> torch.Tensor:
W = self.W
Y = W.transpose(-2, -1) @ X @ W
return Y
def SubSec_multi(X: torch.Tensor, window_sizes: list[int]):
B, n, _ = X.shape
outputs = []
for w in window_sizes:
if w > n:
raise ValueError("window size cannot exceed matrix size")
subs = []
for i in range(0, n - w + 1):
subs.append(X[:, i:i + w, i:i + w])
outputs.append(torch.stack(subs, dim=1))
return outputs
class SPDToVector(nn.Module):
def __init__(self, s: int, out_dim: int):
super().__init__()
dim_vec = (s * (s + 1)) // 2
self.fc = nn.Linear(dim_vec, out_dim)
self.s = s
def forward(self, S: torch.Tensor) -> torch.Tensor:
if S.dim() == 3:
L = LogEig(S)
v = tril_vectorize(L)
return self.fc(v)
elif S.dim() == 4:
B, K, s, _ = S.shape
L = LogEig(S.view(-1, s, s)).view(B, K, s, s)
v = tril_vectorize(L) # (B,K,dim_vec)
B, K, _ = v.shape
return self.fc(v.view(B * K, -1)).view(B, K, -1)
else:
raise ValueError("Unsupported dims for SPDToVector")
class RiemannianBatchNormSPD(nn.Module):
def __init__(self, s: int, momentum: float = 0.1, affine: bool = True, eps: float = 1e-5, k1 = 5):
super().__init__()
self.s = s
self.momentum = momentum
self.affine = affine
self.eps = eps
self.register_buffer('running_mean_log', torch.zeros(s, s))
if affine:
self.gamma = nn.Parameter(torch.ones(1))
self.beta = nn.Parameter(torch.zeros(1))
else:
self.register_parameter('gamma', None)
self.register_parameter('beta', None)
def forward(self, X: torch.Tensor) -> torch.Tensor:
if X.dim() == 4:
B, K, s, _ = X.shape
X_flat = X.view(B * K, s, s)
else:
B, s, _ = X.shape
X_flat = X
K = 1
L = LogEig(X_flat)
if self.training:
mean_log = L.mean(dim=0)
self.running_mean_log.mul_(1 - self.momentum).add_(self.momentum * mean_log.detach())
else:
mean_log = self.running_mean_log
Lc = L - mean_log
if self.affine:
Lc = self.gamma * Lc + self.beta
Y_flat = ExpEig(Lc)
if X.dim() == 4:
return Y_flat.view(B, K, s, s)
else:
return Y_flat
class GeodesicSelfAttentionSPD(nn.Module):
def __init__(self, s: int, dropout: float = 0.1):
super().__init__()
self.s = s
self.dropout = nn.Dropout(dropout)
def forward(self, S: torch.Tensor) -> torch.Tensor:
B, L, s, _ = S.shape
assert s == self.s
Llogs = LogEig(S.view(-1, s, s)).view(B, L, s, s)
Li = Llogs.unsqueeze(2)
Lj = Llogs.unsqueeze(1)
diff = Li - Lj
score = -torch.sum(diff * diff, dim=(-1, -2)) / float(s)
attn = torch.softmax(score, dim=-1)
attn = self.dropout(attn)
attn_ = attn.view(B, L, L, 1, 1)
Lout = torch.sum(attn_ * Lj, dim=2)
Sout = ExpEig(Lout)
return Sout
class SPDVectorSelfAttention(nn.Module):
def __init__(self, s: int, num_heads: int = 4, dropout: float = 0.1):
super().__init__()
self.s = s
dim_vec = (s * (s + 1)) // 2
self.mha = nn.MultiheadAttention(dim_vec, num_heads, dropout=dropout, batch_first=True)
def forward(self, S: torch.Tensor) -> torch.Tensor:
B, L, s, _ = S.shape
assert s == self.s
L_logs = LogEig(S.view(-1, s, s)).view(B, L, s, s)
vec_in = tril_vectorize(L_logs)
vec_out, _ = self.mha(vec_in, vec_in, vec_in)
vec_out = vec_out + vec_in
sym_out = sym_from_tril_vec(vec_out, s)
spd_out = ExpEig(sym_out)
return spd_out
class SPDAttentionPool(nn.Module):
def __init__(self, s: int, eps: float = 1e-4, metric: str = 'logeuclid'):
super().__init__()
self.s = s
self.eps = eps
self.metric = metric
self.Q = nn.Parameter(torch.randn(s, s) * 0.02)
def forward(self, S: torch.Tensor) -> torch.Tensor:
B, K, s, _ = S.shape
assert s == self.s
S_log = LogEig(S.view(-1, s, s)).view(B, K, s, s)
Q = symmetric_part(self.Q)
diff = S_log - Q.view(1, 1, s, s)
score = -torch.sum(diff * diff, dim=(-1, -2)) / float(s)
alpha = F.softmax(score, dim=1).view(B, K, 1, 1)
Y_log = torch.sum(alpha * S_log, dim=1)
Y = ExpEig(Y_log)
Y = Y + self.eps * torch.eye(s, device=Y.device).unsqueeze(0)
return Y
def pad_or_truncate_spd(X: torch.Tensor, target: int, eps: float = 1e-6) -> torch.Tensor:
single = False
if X.dim() == 2:
X = X.unsqueeze(0)
single = True
B, s, _ = X.shape
if s == target:
return X.squeeze(0) if single else X
if s < target:
print('s < target')
pad_size = target - s
pads = eps * torch.eye(pad_size, device=X.device, dtype=X.dtype).unsqueeze(0).repeat(B, 1, 1)
top = torch.cat([X, torch.zeros(B, s, pad_size, device=X.device, dtype=X.dtype)], dim=2)
bottom = torch.cat([torch.zeros(B, pad_size, s, device=X.device, dtype=X.dtype), pads], dim=2)
Y = torch.cat([top, bottom], dim=1)
return Y.squeeze(0) if single else Y
else:
print(f's > target')
Y = X[:, :target, :target]
return Y.squeeze(0) if single else Y