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