A single layer transformer for GPT2. We use the following notation: h: hidden size n: number of attention heads b: batch size s: sequence length Transformore layer takes input with size [b, s, h] and returns an output of the same size. Arguments:
| 389 | |
| 390 | |
| 391 | class ParallelDecoderLayer(torch.nn.Module): |
| 392 | """A single layer transformer for GPT2. |
| 393 | |
| 394 | We use the following notation: |
| 395 | h: hidden size |
| 396 | n: number of attention heads |
| 397 | b: batch size |
| 398 | s: sequence length |
| 399 | Transformore layer takes input with size [b, s, h] and returns an |
| 400 | output of the same size. |
| 401 | |
| 402 | Arguments: |
| 403 | hidden_size: The hidden size of the self attention. |
| 404 | num_attention_heads: number of attention head in the self |
| 405 | attention. |
| 406 | attention_dropout_prob: dropout probability of the attention |
| 407 | score in self attention. |
| 408 | output_dropout_prob: dropout probability for the outputs |
| 409 | after self attention and final output. |
| 410 | layernorm_epsilon: epsilon used in layernorm to avoid |
| 411 | division by zero. |
| 412 | init_method: initialization method used for the weights. Note |
| 413 | that all biases are initialized to zero and |
| 414 | layernorm weight are initialized to one. |
| 415 | output_layer_init_method: output layers (attention output and |
| 416 | mlp output) initialization. If None, |
| 417 | use `init_method`. |
| 418 | """ |
| 419 | |
| 420 | def __init__(self, |
| 421 | hidden_size, |
| 422 | num_attention_heads, |
| 423 | attention_dropout_prob, |
| 424 | output_dropout_prob, |
| 425 | layernorm_epsilon, |
| 426 | init_method, |
| 427 | output_layer_init_method=None): |
| 428 | super(ParallelDecoderLayer, self).__init__() |
| 429 | # Set output layer initialization if not provided. |
| 430 | if output_layer_init_method is None: |
| 431 | output_layer_init_method = init_method |
| 432 | |
| 433 | # Layernorm on the input data. |
| 434 | self.input_layernorm = LayerNorm(hidden_size, eps=layernorm_epsilon) |
| 435 | |
| 436 | # Self attention. |
| 437 | self.self_attention = ParallelSelfAttention( |
| 438 | hidden_size, |
| 439 | num_attention_heads, |
| 440 | attention_dropout_prob, |
| 441 | output_dropout_prob, |
| 442 | init_method, |
| 443 | output_layer_init_method=output_layer_init_method) |
| 444 | |
| 445 | # Layernorm after the self attention. |
| 446 | self.post_self_layernorm = LayerNorm(hidden_size, eps=layernorm_epsilon) |
| 447 | |
| 448 | self.cross_attention = ParallelCrossAttention( |