-
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathlayers.py
More file actions
155 lines (134 loc) · 5.45 KB
/
Copy pathlayers.py
File metadata and controls
155 lines (134 loc) · 5.45 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
"""
"""
# Created by Wenjie Du <wenjay.du@gmail.com>
# License: BSD-3-Clause
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from scipy import signal
from scipy import special as ss
class HiPPO_LegT(nn.Module):
def __init__(self, N, dt=1.0, discretization="bilinear"):
"""
N: the order of the HiPPO projection
dt: discretization step size - should be roughly inverse to the length of the sequence
"""
super().__init__()
self.N = N
A, B = self.transition(N)
C = np.ones((1, N))
D = np.zeros((1,))
A, B, _, _, _ = signal.cont2discrete((A, B, C, D), dt=dt, method=discretization)
B = B.squeeze(-1)
self.register_buffer("A", torch.Tensor(A))
self.register_buffer("B", torch.Tensor(B))
vals = np.arange(0.0, 1.0, dt)
self.register_buffer(
"eval_matrix",
torch.Tensor(ss.eval_legendre(np.arange(N)[:, None], 1 - 2 * vals).T),
)
@staticmethod
def transition(N):
Q = np.arange(N, dtype=np.float64)
R = (2 * Q + 1)[:, None] # / theta
j, i = np.meshgrid(Q, Q)
A = np.where(i < j, -1, (-1.0) ** (i - j + 1)) * R
B = (-1.0) ** Q[:, None] * R
return A, B
def forward(self, inputs: torch.Tensor):
"""
inputs : (length, ...)
output : (length, ..., N) where N is the order of the HiPPO projection
"""
device = inputs.device
c = torch.zeros(inputs.shape[:-1] + tuple([self.N])).to(device)
cs = []
for f in inputs.permute([-1, 0, 1]):
f = f.unsqueeze(-1)
new = f @ self.B.unsqueeze(0)
c = F.linear(c, self.A) + new
cs.append(c)
return torch.stack(cs, dim=0)
def reconstruct(self, c):
return (self.eval_matrix @ c.unsqueeze(-1)).squeeze(-1)
class SpectralConv1d(nn.Module):
def __init__(
self,
in_channels,
out_channels,
seq_len,
modes1,
ratio=0.5,
mode_type=0,
# compression=0, # never used in the official implementation, hence deprecate it here
):
"""
1D Fourier layer. It does FFT, linear transform, and Inverse FFT.
"""
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.modes1 = modes1
self.ratio = ratio
if mode_type == 0:
self.modes2 = min(32, seq_len // 2)
self.index = list(range(0, self.modes2))
elif mode_type == 1:
modes2 = modes1
self.modes2 = min(modes2, seq_len // 2)
self.index0 = list(range(0, int(ratio * min(seq_len // 2, modes2))))
self.index1 = list(range(len(self.index0), self.modes2))
np.random.shuffle(self.index1)
self.index1 = self.index1[: min(seq_len // 2, self.modes2) - int(ratio * min(seq_len // 2, modes2))]
self.index = self.index0 + self.index1
self.index.sort()
elif mode_type == 2:
modes2 = modes1
self.modes2 = min(modes2, seq_len // 2)
self.index = list(range(0, seq_len // 2))
np.random.shuffle(self.index)
self.index = self.index[: self.modes2]
self.scale = 1 / (in_channels * out_channels)
self.weights1 = nn.Parameter(
self.scale * torch.rand(in_channels, out_channels, len(self.index), dtype=torch.cfloat)
)
# Register index as a buffer to ensure it's properly handled by DataParallel
self.register_buffer("index_buffer", torch.tensor(self.index, dtype=torch.long))
def forward(self, x):
B, H, E, N = x.shape
x_ft = torch.fft.rfft(x)
out_ft = torch.zeros(
B,
H,
self.out_channels,
x.size(-1) // 2 + 1,
device=x.device,
dtype=torch.cfloat,
)
if self.modes1 > 1000:
for wi, i in enumerate(self.index):
# Handle complex einsum by splitting into real and imaginary parts
a_i = x_ft[:, :, :, i]
w_i = self.weights1[:, :, wi]
a_real, a_imag = a_i.real, a_i.imag
w_real, w_imag = w_i.real, w_i.imag
# Complex multiplication
out_real = torch.einsum("bji,io->bjo", a_real, w_real) - torch.einsum("bji,io->bjo", a_imag, w_imag)
out_imag = torch.einsum("bji,io->bjo", a_real, w_imag) + torch.einsum("bji,io->bjo", a_imag, w_real)
out_ft[:, :, :, i] = torch.complex(out_real, out_imag)
else:
a = x_ft[:, :, :, : self.modes2]
# Handle complex einsum by splitting into real and imaginary parts
# to avoid issues with DataParallel
a_real = a.real
a_imag = a.imag
w_real = self.weights1.real
w_imag = self.weights1.imag
# Complex multiplication: (a_real + i*a_imag) * (w_real + i*w_imag)
# = (a_real*w_real - a_imag*w_imag) + i*(a_real*w_imag + a_imag*w_real)
out_real = torch.einsum("bjix,iox->bjox", a_real, w_real) - torch.einsum("bjix,iox->bjox", a_imag, w_imag)
out_imag = torch.einsum("bjix,iox->bjox", a_real, w_imag) + torch.einsum("bjix,iox->bjox", a_imag, w_real)
out_ft[:, :, :, : self.modes2] = torch.complex(out_real, out_imag)
x = torch.fft.irfft(out_ft, n=x.size(-1))
return x