MCPcopy Create free account
hub / github.com/csslc/PiSA-SR / forward

Method forward

ram/models/bert_lora.py:473–563  ·  view source on GitHub ↗
(
        self,
        hidden_states,
        attention_mask=None,
        head_mask=None,
        encoder_hidden_states=None,
        encoder_attention_mask=None,
        past_key_values=None,
        use_cache=None,
        output_attentions=False,
        output_hidden_states=False,
        return_dict=True,
        mode='multimodal',
    )

Source from the content-addressed store, hash-verified

471 self.gradient_checkpointing = False
472
473 def forward(
474 self,
475 hidden_states,
476 attention_mask=None,
477 head_mask=None,
478 encoder_hidden_states=None,
479 encoder_attention_mask=None,
480 past_key_values=None,
481 use_cache=None,
482 output_attentions=False,
483 output_hidden_states=False,
484 return_dict=True,
485 mode='multimodal',
486 ):
487 all_hidden_states = () if output_hidden_states else None
488 all_self_attentions = () if output_attentions else None
489 all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None
490
491 next_decoder_cache = () if use_cache else None
492
493 for i in range(self.config.num_hidden_layers):
494 layer_module = self.layer[i]
495 if output_hidden_states:
496 all_hidden_states = all_hidden_states + (hidden_states,)
497
498 layer_head_mask = head_mask[i] if head_mask is not None else None
499 past_key_value = past_key_values[i] if past_key_values is not None else None
500
501 if self.gradient_checkpointing and self.training:
502
503 if use_cache:
504 logger.warn(
505 "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
506 )
507 use_cache = False
508
509 def create_custom_forward(module):
510 def custom_forward(*inputs):
511 return module(*inputs, past_key_value, output_attentions)
512
513 return custom_forward
514
515 layer_outputs = torch.utils.checkpoint.checkpoint(
516 create_custom_forward(layer_module),
517 hidden_states,
518 attention_mask,
519 layer_head_mask,
520 encoder_hidden_states,
521 encoder_attention_mask,
522 mode=mode,
523 )
524 else:
525 layer_outputs = layer_module(
526 hidden_states,
527 attention_mask,
528 layer_head_mask,
529 encoder_hidden_states,
530 encoder_attention_mask,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected