| 14 | """ |
| 15 | |
| 16 | class StreamConv1d(nn.Module): |
| 17 | def __init__(self, |
| 18 | in_channels: int, |
| 19 | out_channels: int, |
| 20 | kernel_size: int, |
| 21 | stride: int=1, |
| 22 | padding: int=0, |
| 23 | dilation: int=1, |
| 24 | groups: int=1, |
| 25 | bias: bool=True, |
| 26 | *args, **kargs): |
| 27 | super(StreamConv1d, self).__init__(*args, *kargs) |
| 28 | |
| 29 | assert padding == 0, "To meet the demands of causal streaming requirements" |
| 30 | |
| 31 | self.Conv1d = nn.Conv1d(in_channels = in_channels, |
| 32 | out_channels = out_channels, |
| 33 | kernel_size = kernel_size, |
| 34 | stride = stride, |
| 35 | padding = padding, |
| 36 | dilation = dilation, |
| 37 | groups = groups, |
| 38 | bias = bias) |
| 39 | |
| 40 | def forward(self, x, cache): |
| 41 | """ |
| 42 | x: [bs, C, T_size] |
| 43 | cache: [bs, C, T_size-1] |
| 44 | """ |
| 45 | inp = torch.cat([cache, x], dim=-1) |
| 46 | oup = self.Conv1d(inp) |
| 47 | out_cache = inp[..., 1:] |
| 48 | return oup, out_cache |
| 49 | |
| 50 | |
| 51 | class StreamConv2d(nn.Module): |