hidden_states: [seq_len, batch, hidden_size] attention_mask: [(1, 1), seq_len, seq_len]
(
self,
hidden_states: torch.Tensor,
position_ids,
attention_mask: torch.Tensor,
layer_id,
layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
use_cache: bool = False,
output_attentions: bool = False,
)
| 639 | ) |
| 640 | |
| 641 | def forward( |
| 642 | self, |
| 643 | hidden_states: torch.Tensor, |
| 644 | position_ids, |
| 645 | attention_mask: torch.Tensor, |
| 646 | layer_id, |
| 647 | layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, |
| 648 | use_cache: bool = False, |
| 649 | output_attentions: bool = False, |
| 650 | ): |
| 651 | """ |
| 652 | hidden_states: [seq_len, batch, hidden_size] |
| 653 | attention_mask: [(1, 1), seq_len, seq_len] |
| 654 | """ |
| 655 | |
| 656 | # Layer norm at the beginning of the transformer layer. |
| 657 | # [seq_len, batch, hidden_size] |
| 658 | attention_input = self.input_layernorm(hidden_states) |
| 659 | |
| 660 | # Self attention. |
| 661 | attention_outputs = self.attention( |
| 662 | attention_input, |
| 663 | position_ids, |
| 664 | attention_mask=attention_mask, |
| 665 | layer_id=layer_id, |
| 666 | layer_past=layer_past, |
| 667 | use_cache=use_cache, |
| 668 | output_attentions=output_attentions |
| 669 | ) |
| 670 | |
| 671 | attention_output = attention_outputs[0] |
| 672 | |
| 673 | outputs = attention_outputs[1:] |
| 674 | |
| 675 | # Residual connection. |
| 676 | alpha = (2 * self.num_layers) ** 0.5 |
| 677 | hidden_states = attention_input * alpha + attention_output |
| 678 | |
| 679 | mlp_input = self.post_attention_layernorm(hidden_states) |
| 680 | |
| 681 | # MLP. |
| 682 | mlp_output = self.mlp(mlp_input) |
| 683 | |
| 684 | # Second residual connection. |
| 685 | output = mlp_input * alpha + mlp_output |
| 686 | |
| 687 | if use_cache: |
| 688 | outputs = (output,) + outputs |
| 689 | else: |
| 690 | outputs = (output,) + outputs[1:] |
| 691 | |
| 692 | return outputs # hidden_states, present, attentions |
| 693 | |
| 694 | |
| 695 | class ChatGLMPreTrainedModel(PreTrainedModel): |
nothing calls this directly
no outgoing calls
no test coverage detected