|
| 1 | +__all__ = [ |
| 2 | + "WindowConvolutionFrontendV1Config", |
| 3 | + "WindowConvolutionFrontendV1", |
| 4 | +] |
| 5 | + |
| 6 | +from dataclasses import dataclass |
| 7 | +from typing import Callable, Optional, Tuple, Union |
| 8 | + |
| 9 | +import torch |
| 10 | +from torch import nn |
| 11 | +from torch.nn import functional as F |
| 12 | + |
| 13 | +from i6_models.config import ModelConfiguration |
| 14 | + |
| 15 | +from .common import mask_pool, apply_same_padding |
| 16 | + |
| 17 | + |
| 18 | +@dataclass |
| 19 | +class WindowConvolutionFrontendV1Config(ModelConfiguration): |
| 20 | + """ |
| 21 | + Attributes: |
| 22 | + input_dim: number of input features to module |
| 23 | + output_dim: output dimension |
| 24 | + dropout: dropout after linear layer |
| 25 | + kernel_size: number of feature frames to convolve |
| 26 | + stride: skip (stride - 1) feature frames; stride > 1 implies subsampling |
| 27 | + activation: activation function applied after linear computation |
| 28 | + """ |
| 29 | + |
| 30 | + input_dim: int |
| 31 | + output_dim: int |
| 32 | + dropout: float |
| 33 | + kernel_size: int |
| 34 | + stride: int |
| 35 | + activation: Union[nn.Module, Callable[[torch.Tensor], torch.Tensor]] |
| 36 | + |
| 37 | + def __post_init__(self): |
| 38 | + super().__post_init__() |
| 39 | + assert self.stride >= 1, "Choose an integer >= 1 for stride" |
| 40 | + assert 0.0 <= self.dropout <= 1.0, "Dropout value must be a probability" |
| 41 | + |
| 42 | + |
| 43 | +class WindowConvolutionFrontendV1(nn.Module): |
| 44 | + """ |
| 45 | + Simple feed-forward front-end that computes over a window |
| 46 | + of input features. Choosing a stride > 1 allows for subsampling |
| 47 | + of the features. |
| 48 | + """ |
| 49 | + |
| 50 | + def __init__(self, cfg: WindowConvolutionFrontendV1Config): |
| 51 | + """ |
| 52 | + :param cfg: model configuration for this module |
| 53 | + """ |
| 54 | + super().__init__() |
| 55 | + self.conv = torch.nn.Conv1d( |
| 56 | + in_channels=cfg.input_dim, |
| 57 | + out_channels=cfg.output_dim, |
| 58 | + kernel_size=cfg.kernel_size, |
| 59 | + stride=cfg.stride, |
| 60 | + padding=0, |
| 61 | + bias=True, |
| 62 | + ) |
| 63 | + self.activation = cfg.activation |
| 64 | + self.pad = lambda x: apply_same_padding(x, cfg.kernel_size) |
| 65 | + self.dropout = torch.nn.Dropout(cfg.dropout) |
| 66 | + |
| 67 | + def forward(self, x: torch.Tensor, /, sequence_mask: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: |
| 68 | + """ |
| 69 | + T might be reduced to T' on stride |
| 70 | +
|
| 71 | + :param x: input tensor of shape [B,T,F] |
| 72 | + :param sequence_mask: the sequence mask for the tensor |
| 73 | + :return: torch.Tensor of shape [B,T',F'] and the shape of the sequence mask |
| 74 | + """ |
| 75 | + # torch 1d convolution is over last dim but we want time conv |
| 76 | + x = x.transpose(1, 2) # [B, F, T] |
| 77 | + x = self.pad(x) |
| 78 | + x = self.conv(x).transpose(1, 2) # [B, T', F'] |
| 79 | + |
| 80 | + # change masking according to stride value |
| 81 | + sequence_mask = mask_pool( |
| 82 | + sequence_mask, |
| 83 | + kernel_size=1, |
| 84 | + stride=self.conv.stride[0], |
| 85 | + padding=0, # done manually |
| 86 | + ) |
| 87 | + x = self.activation(x) |
| 88 | + x = self.dropout(x) |
| 89 | + |
| 90 | + return x, sequence_mask |
0 commit comments