(
self,
hidden_states,
segment_index,
token_type_mask,
attention_mask=None,
head_mask=None,
encoder_hidden_states=None,
encoder_attention_mask=None,
past_key_value=None,
output_attentions=False,
)
| 397 | self.output = PoNetOutput(config) |
| 398 | |
| 399 | def forward( |
| 400 | self, |
| 401 | hidden_states, |
| 402 | segment_index, |
| 403 | token_type_mask, |
| 404 | attention_mask=None, |
| 405 | head_mask=None, |
| 406 | encoder_hidden_states=None, |
| 407 | encoder_attention_mask=None, |
| 408 | past_key_value=None, |
| 409 | output_attentions=False, |
| 410 | ): |
| 411 | # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 |
| 412 | self_attn_past_key_value = past_key_value[: |
| 413 | 2] if past_key_value is not None else None |
| 414 | self_attention_outputs = self.attention( |
| 415 | hidden_states, |
| 416 | segment_index, |
| 417 | token_type_mask, |
| 418 | attention_mask, |
| 419 | head_mask, |
| 420 | output_attentions=output_attentions, |
| 421 | past_key_value=self_attn_past_key_value, |
| 422 | ) |
| 423 | attention_output = self_attention_outputs[0] |
| 424 | |
| 425 | # if decoder, the last output is tuple of self-attn cache |
| 426 | if self.is_decoder: |
| 427 | outputs = self_attention_outputs[1:-1] |
| 428 | present_key_value = self_attention_outputs[-1] |
| 429 | else: |
| 430 | outputs = self_attention_outputs[ |
| 431 | 1:] # add self attentions if we output attention weights |
| 432 | |
| 433 | cross_attn_present_key_value = None |
| 434 | if self.is_decoder and encoder_hidden_states is not None: |
| 435 | assert hasattr( |
| 436 | self, 'crossattention' |
| 437 | ), f'If `encoder_hidden_states` are passed, {self} has to be instantiated with cross-attention layers by setting `config.add_cross_attention=True`' # noqa * |
| 438 | |
| 439 | cross_attn_past_key_value = past_key_value[ |
| 440 | -2:] if past_key_value is not None else None |
| 441 | cross_attention_outputs = self.crossattention( |
| 442 | attention_output, |
| 443 | attention_mask, |
| 444 | head_mask, |
| 445 | encoder_hidden_states, |
| 446 | encoder_attention_mask, |
| 447 | cross_attn_past_key_value, |
| 448 | output_attentions, |
| 449 | ) |
| 450 | attention_output = cross_attention_outputs[0] |
| 451 | outputs = outputs + cross_attention_outputs[ |
| 452 | 1:-1] # add cross attentions if we output attention weights |
| 453 | |
| 454 | # add cross-attn cache to positions 3,4 of present_key_value tuple |
| 455 | cross_attn_present_key_value = cross_attention_outputs[-1] |
| 456 | present_key_value = present_key_value + cross_attn_present_key_value |
nothing calls this directly
no test coverage detected