(
self,
hidden_states,
attention_mask=None,
head_mask=None,
encoder_hidden_states=None,
encoder_attention_mask=None,
past_key_value=None,
output_attentions=False,
)
| 570 | self.output = BertOutput(config) |
| 571 | |
| 572 | def forward( |
| 573 | self, |
| 574 | hidden_states, |
| 575 | attention_mask=None, |
| 576 | head_mask=None, |
| 577 | encoder_hidden_states=None, |
| 578 | encoder_attention_mask=None, |
| 579 | past_key_value=None, |
| 580 | output_attentions=False, |
| 581 | ): |
| 582 | # decoder uni-directional self-attention cached key/values tuple is at |
| 583 | # positions 1,2 |
| 584 | self_attn_past_key_value = past_key_value[: |
| 585 | 2] if past_key_value is not None else None |
| 586 | self_attention_outputs = self.attention( |
| 587 | hidden_states, |
| 588 | attention_mask, |
| 589 | head_mask, |
| 590 | output_attentions=output_attentions, |
| 591 | past_key_value=self_attn_past_key_value, |
| 592 | ) |
| 593 | attention_output = self_attention_outputs[0] |
| 594 | |
| 595 | outputs = self_attention_outputs[1:-1] |
| 596 | present_key_value = self_attention_outputs[-1] |
| 597 | |
| 598 | if self.has_cross_attention: |
| 599 | assert encoder_hidden_states is not None, 'encoder_hidden_states must be given for cross-attention layers' |
| 600 | |
| 601 | if type(encoder_hidden_states) == list: |
| 602 | cross_attention_outputs = self.crossattention( |
| 603 | attention_output, |
| 604 | attention_mask, |
| 605 | head_mask, |
| 606 | encoder_hidden_states[(self.layer_num |
| 607 | - self.config.fusion_layer) |
| 608 | % len(encoder_hidden_states)], |
| 609 | encoder_attention_mask[(self.layer_num |
| 610 | - self.config.fusion_layer) |
| 611 | % len(encoder_hidden_states)], |
| 612 | output_attentions=output_attentions, |
| 613 | ) |
| 614 | attention_output = cross_attention_outputs[0] |
| 615 | outputs = outputs + cross_attention_outputs[1:-1] |
| 616 | |
| 617 | else: |
| 618 | cross_attention_outputs = self.crossattention( |
| 619 | attention_output, |
| 620 | attention_mask, |
| 621 | head_mask, |
| 622 | encoder_hidden_states, |
| 623 | encoder_attention_mask, |
| 624 | output_attentions=output_attentions, |
| 625 | ) |
| 626 | attention_output = cross_attention_outputs[0] |
| 627 | outputs = outputs + cross_attention_outputs[ |
| 628 | 1: |
| 629 | -1] # add cross attentions if we output attention weights |
nothing calls this directly
no test coverage detected