MLP for GPT2. MLP will take the input with h hidden state, project it to 4*h hidden dimension, perform gelu transformation, and project the state back into h hidden dimension. At the end, dropout is also applied. Arguments: hidden_size: The hidden size of the self atten
| 341 | |
| 342 | |
| 343 | class ParallelMLP(torch.nn.Module): |
| 344 | """MLP for GPT2. |
| 345 | |
| 346 | MLP will take the input with h hidden state, project it to 4*h |
| 347 | hidden dimension, perform gelu transformation, and project the |
| 348 | state back into h hidden dimension. At the end, dropout is also |
| 349 | applied. |
| 350 | |
| 351 | Arguments: |
| 352 | hidden_size: The hidden size of the self attention. |
| 353 | output_dropout_prob: dropout probability for the outputs |
| 354 | after self attention and final output. |
| 355 | init_method: initialization method used for the weights. Note |
| 356 | that all biases are initialized to zero and |
| 357 | layernorm weight are initialized to one. |
| 358 | output_layer_init_method: output layer initialization. If None, |
| 359 | use `init_method`. |
| 360 | """ |
| 361 | |
| 362 | def __init__(self, hidden_size, output_dropout_prob, init_method, |
| 363 | output_layer_init_method=None): |
| 364 | super(ParallelMLP, self).__init__() |
| 365 | # Set output layer initialization if not provided. |
| 366 | if output_layer_init_method is None: |
| 367 | output_layer_init_method = init_method |
| 368 | # Project to 4h. |
| 369 | self.dense_h_to_4h = ColumnParallelLinear(hidden_size, 4 * hidden_size, |
| 370 | gather_output=False, |
| 371 | init_method=init_method) |
| 372 | # Project back to h. |
| 373 | self.dense_4h_to_h = RowParallelLinear( |
| 374 | 4 * hidden_size, |
| 375 | hidden_size, |
| 376 | input_is_parallel=True, |
| 377 | init_method=output_layer_init_method) |
| 378 | self.dropout = torch.nn.Dropout(output_dropout_prob) |
| 379 | |
| 380 | def forward(self, hidden_states): |
| 381 | # [b, s, 4hp] |
| 382 | intermediate_parallel = self.dense_h_to_4h(hidden_states) |
| 383 | intermediate_parallel = gelu(intermediate_parallel) |
| 384 | |
| 385 | # [b, s, h] |
| 386 | output = self.dense_4h_to_h(intermediate_parallel) |
| 387 | output = self.dropout(output) |
| 388 | return output |
| 389 | |
| 390 | |
| 391 | class ParallelDecoderLayer(torch.nn.Module): |