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