MCPcopy Create free account
hub / github.com/modelscope/modelscope / forward

Method forward

modelscope/models/nlp/peer/backbone.py:487–556  ·  view source on GitHub ↗
(
            self,
            hidden_states,
            attention_mask=None,
            head_mask=None,
            encoder_hidden_states=None,
            encoder_attention_mask=None,
            past_key_value=None,
            output_attentions=False,
            side_info_sets=dict(),
    )

Source from the content-addressed store, hash-verified

485 self.output = PeerOutput(config)
486
487 def forward(
488 self,
489 hidden_states,
490 attention_mask=None,
491 head_mask=None,
492 encoder_hidden_states=None,
493 encoder_attention_mask=None,
494 past_key_value=None,
495 output_attentions=False,
496 side_info_sets=dict(),
497 ):
498 # decoder uni-directional self-attention cached key/values tuple is at positions 1,2
499 self_attn_past_key_value = past_key_value[:
500 2] if past_key_value is not None else None
501 self_attention_outputs = self.attention(
502 hidden_states,
503 attention_mask,
504 head_mask,
505 output_attentions=output_attentions,
506 past_key_value=self_attn_past_key_value,
507 side_info_sets=side_info_sets,
508 )
509 attention_output = self_attention_outputs[0]
510
511 # if decoder, the last output is tuple of self-attn cache
512 if self.is_decoder:
513 outputs = self_attention_outputs[1:-1]
514 present_key_value = self_attention_outputs[-1]
515 else:
516 outputs = self_attention_outputs[
517 1:] # add self attentions if we output attention weights
518
519 cross_attn_present_key_value = None
520 if self.is_decoder and encoder_hidden_states is not None:
521 assert hasattr(
522 self, 'crossattention'
523 ), f'If `encoder_hidden_states` are passed, {self} has to be instantiated \
524 with cross-attention layers by setting `config.add_cross_attention=True`'
525
526 # cross_attn cached key/values tuple is at positions 3,4 of past_key_value tuple
527 cross_attn_past_key_value = past_key_value[
528 -2:] if past_key_value is not None else None
529 cross_attention_outputs = self.crossattention(
530 attention_output,
531 attention_mask,
532 head_mask,
533 encoder_hidden_states,
534 encoder_attention_mask,
535 cross_attn_past_key_value,
536 output_attentions,
537 )
538 attention_output = cross_attention_outputs[0]
539 outputs = outputs + cross_attention_outputs[
540 1:-1] # add cross attentions if we output attention weights
541
542 # add cross-attn cache to positions 3,4 of present_key_value tuple
543 cross_attn_present_key_value = cross_attention_outputs[-1]
544 present_key_value = present_key_value + cross_attn_present_key_value

Callers

nothing calls this directly

Calls 2

attentionMethod · 0.45

Tested by

no test coverage detected