-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
35 lines (28 loc) · 1.2 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
import torch
import torch.nn as nn
import torch.nn.functional as F
# Block Diagonal Class for memory mixing in sLSTM Block
class BlockDiagonal(nn.Module):
def __init__(self, in_features, out_features, num_blocks, bias=True):
super(BlockDiagonal, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.num_blocks = num_blocks
assert out_features % num_blocks == 0
block_out_features = out_features // num_blocks
self.blocks = nn.ModuleList([
nn.Linear(in_features, block_out_features, bias=bias)
for _ in range(num_blocks)
])
def forward(self, x):
x = [block(x) for block in self.blocks]
x = torch.cat(x, dim=-1)
return x
class CausalConv1D(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, dilation=1, **kwargs):
super(CausalConv1D, self).__init__()
self.padding = (kernel_size - 1) * dilation
self.conv = nn.Conv1d(in_channels, out_channels, kernel_size, padding=self.padding, dilation=dilation, **kwargs)
def forward(self, x):
x = self.conv(x)
return x[:, :, :-self.padding]