| 11 | |
| 12 | |
| 13 | class SpecFormer(L.LightningModule): |
| 14 | def __init__( |
| 15 | self, |
| 16 | input_dim: int, |
| 17 | embed_dim: int, |
| 18 | num_layers: int, |
| 19 | num_heads: int, |
| 20 | max_len: int, |
| 21 | mask_num_chunks: int = 6, |
| 22 | mask_chunk_width: int = 50, |
| 23 | slice_section_length: int = 20, |
| 24 | slice_overlap: int = 10, |
| 25 | dropout: float = 0.1, |
| 26 | norm_first: bool = False, |
| 27 | ): |
| 28 | super().__init__() |
| 29 | self.save_hyperparameters() |
| 30 | |
| 31 | self.data_embed = nn.Linear(input_dim, embed_dim) |
| 32 | self.position_embed = nn.Embedding(max_len, embed_dim) |
| 33 | self.dropout = nn.Dropout(dropout) |
| 34 | self.blocks = nn.ModuleList( |
| 35 | [ |
| 36 | TransformerBlock( |
| 37 | embedding_dim=embed_dim, |
| 38 | num_heads=num_heads, |
| 39 | causal=False, |
| 40 | dropout=dropout, |
| 41 | bias=True, |
| 42 | ) |
| 43 | for _ in range(num_layers) |
| 44 | ] |
| 45 | ) |
| 46 | self.final_layernorm = LayerNorm(embed_dim, bias=True) |
| 47 | self.head = nn.Linear(embed_dim, input_dim, bias=True) |
| 48 | |
| 49 | self._reset_parameters_datapt() |
| 50 | |
| 51 | def forward(self, x: Tensor) -> torch.Tensor: |
| 52 | """Forward pass through the model.""" |
| 53 | x = self.preprocess(x) |
| 54 | return self.forward_without_preprocessing(x) |
| 55 | |
| 56 | def forward_without_preprocessing(self, x: Tensor): |
| 57 | """Forward pass through the model. |
| 58 | The training step performs masking before preprocessing, |
| 59 | thus samples should not be preprocessed again as in forward()""" |
| 60 | |
| 61 | t = x.shape[1] |
| 62 | if t > self.hparams.max_len: |
| 63 | raise ValueError( |
| 64 | f"Cannot forward sequence of length {t}, " |
| 65 | f"block size is only {self.hparams.max_len}" |
| 66 | ) |
| 67 | pos = torch.arange(0, t, dtype=torch.long, device=x.device) # shape (t) |
| 68 | |
| 69 | # forward the GPT model itself |
| 70 | data_emb = self.data_embed(x) # to shape (b, t, embedding_dim) |
no outgoing calls
no test coverage detected