Standard MLP layer with optional dropout.
| 19 | |
| 20 | |
| 21 | class Mlp(nn.Module, ListForwardMixin): |
| 22 | """Standard MLP layer with optional dropout.""" |
| 23 | |
| 24 | def __init__( |
| 25 | self, |
| 26 | in_features: int, |
| 27 | hidden_features: Optional[int] = None, |
| 28 | out_features: Optional[int] = None, |
| 29 | act_layer: Callable[..., nn.Module] = nn.GELU, |
| 30 | drop: float = 0.0, |
| 31 | bias: bool = True, |
| 32 | device=None, |
| 33 | ) -> None: |
| 34 | super().__init__() |
| 35 | out_features = out_features or in_features |
| 36 | hidden_features = hidden_features or in_features |
| 37 | self.fc1 = nn.Linear(in_features, hidden_features, bias=bias, device=device) |
| 38 | self.act = act_layer() |
| 39 | self.fc2 = nn.Linear(hidden_features, out_features, bias=bias, device=device) |
| 40 | self.drop = nn.Dropout(drop) |
| 41 | |
| 42 | def forward(self, x: Tensor) -> Tensor: |
| 43 | x = self.fc1(x) |
| 44 | x = self.act(x) |
| 45 | x = self.drop(x) |
| 46 | x = self.fc2(x) |
| 47 | x = self.drop(x) |
| 48 | return x |
| 49 | |
| 50 | |
| 51 | class SwiGLUFFN(nn.Module, ListForwardMixin): |