MLP. MLP will take the input with h hidden state, project it to 4*h hidden dimension, perform nonlinear transformation, and project the state back into h hidden dimension. At the end, dropout is also applied.
| 12 | |
| 13 | |
| 14 | class MLP(torch.nn.Module): |
| 15 | """MLP. |
| 16 | MLP will take the input with h hidden state, project it to 4*h |
| 17 | hidden dimension, perform nonlinear transformation, and project the |
| 18 | state back into h hidden dimension. At the end, dropout is also |
| 19 | applied. |
| 20 | """ |
| 21 | |
| 22 | def __init__( |
| 23 | self, |
| 24 | hidden_size, |
| 25 | ): |
| 26 | super(MLP, self).__init__() |
| 27 | self.hidden_size = hidden_size |
| 28 | # Project to 4h. |
| 29 | self.dense_h_to_4h = torch.nn.Linear( |
| 30 | self.hidden_size, |
| 31 | 4 * self.hidden_size, |
| 32 | ) |
| 33 | |
| 34 | self.activation_func = fast_gelu |
| 35 | |
| 36 | # Project back to h. |
| 37 | self.dense_4h_to_h = torch.nn.Linear( |
| 38 | 4 * self.hidden_size, |
| 39 | self.hidden_size, |
| 40 | ) |
| 41 | |
| 42 | def forward(self, hidden_states): |
| 43 | # [s, b, 4hp] |
| 44 | intermediate_parallel = self.dense_h_to_4h(hidden_states) |
| 45 | intermediate_parallel = self.activation_func(intermediate_parallel) |
| 46 | # [s, b, h] |
| 47 | output = self.dense_4h_to_h(intermediate_parallel) |
| 48 | |
| 49 | return output |
| 50 | |
| 51 | |
| 52 | class SelfAttention(torch.nn.Module): |