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