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:
| 490 | |
| 491 | |
| 492 | class ParallelTransformerLayer(torch.nn.Module): |
| 493 | """A single layer transformer for GPT2. |
| 494 | |
| 495 | We use the following notation: |
| 496 | h: hidden size |
| 497 | n: number of attention heads |
| 498 | b: batch size |
| 499 | s: sequence length |
| 500 | Transformore layer takes input with size [b, s, h] and returns an |
| 501 | output of the same size. |
| 502 | |
| 503 | Arguments: |
| 504 | hidden_size: The hidden size of the self attention. |
| 505 | num_attention_heads: number of attention head in the self |
| 506 | attention. |
| 507 | attention_dropout_prob: dropout probability of the attention |
| 508 | score in self attention. |
| 509 | output_dropout_prob: dropout probability for the outputs |
| 510 | after self attention and final output. |
| 511 | layernorm_epsilon: epsilon used in layernorm to avoid |
| 512 | division by zero. |
| 513 | init_method: initialization method used for the weights. Note |
| 514 | that all biases are initialized to zero and |
| 515 | layernorm weight are initialized to one. |
| 516 | output_layer_init_method: output layers (attention output and |
| 517 | mlp output) initialization. If None, |
| 518 | use `init_method`. |
| 519 | """ |
| 520 | |
| 521 | def __init__(self, |
| 522 | hidden_size, |
| 523 | num_attention_heads, |
| 524 | attention_dropout_prob, |
| 525 | output_dropout_prob, |
| 526 | layernorm_epsilon, |
| 527 | init_method, |
| 528 | output_layer_init_method=None, |
| 529 | relative_encoding=False, |
| 530 | performer=False, |
| 531 | attention_scale=1.0): |
| 532 | super(ParallelTransformerLayer, self).__init__() |
| 533 | # Set output layer initialization if not provided. |
| 534 | if output_layer_init_method is None: |
| 535 | output_layer_init_method = init_method |
| 536 | |
| 537 | # Layernorm on the input data. |
| 538 | self.input_layernorm = LayerNorm(hidden_size, eps=layernorm_epsilon) |
| 539 | |
| 540 | # Self attention. |
| 541 | self.attention = ParallelSelfAttention( |
| 542 | hidden_size, |
| 543 | num_attention_heads, |
| 544 | attention_dropout_prob, |
| 545 | output_dropout_prob, |
| 546 | init_method, |
| 547 | output_layer_init_method=output_layer_init_method, |
| 548 | relative_encoding=relative_encoding, |
| 549 | performer=performer, |