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

Method forward

modelscope/models/nlp/T5/backbone.py:965–1195  ·  view source on GitHub ↗
(
        self,
        input_ids=None,
        attention_mask=None,
        encoder_hidden_states=None,
        encoder_attention_mask=None,
        inputs_embeds=None,
        head_mask=None,
        cross_attn_head_mask=None,
        past_key_values=None,
        use_cache=None,
        output_attentions=None,
        output_hidden_states=None,
        return_dict=None,
    )

Source from the content-addressed store, hash-verified

963 self.embed_tokens = new_embeddings
964
965 def forward(
966 self,
967 input_ids=None,
968 attention_mask=None,
969 encoder_hidden_states=None,
970 encoder_attention_mask=None,
971 inputs_embeds=None,
972 head_mask=None,
973 cross_attn_head_mask=None,
974 past_key_values=None,
975 use_cache=None,
976 output_attentions=None,
977 output_hidden_states=None,
978 return_dict=None,
979 ):
980 # Model parallel
981 if self.model_parallel:
982 torch.cuda.set_device(self.first_device)
983 self.embed_tokens = self.embed_tokens.to(self.first_device)
984 use_cache = use_cache if use_cache is not None else self.config.use_cache
985 output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
986 output_hidden_states = (
987 output_hidden_states if output_hidden_states is not None else
988 self.config.output_hidden_states)
989 return_dict = return_dict if return_dict is not None else self.config.use_return_dict
990
991 if input_ids is not None and inputs_embeds is not None:
992 err_msg_prefix = 'decoder_' if self.is_decoder else ''
993 raise ValueError(
994 f'You cannot specify both {err_msg_prefix}input_ids and {err_msg_prefix}inputs_embeds at the same time'
995 )
996 elif input_ids is not None:
997 input_shape = input_ids.size()
998 input_ids = input_ids.view(-1, input_shape[-1])
999 elif inputs_embeds is not None:
1000 input_shape = inputs_embeds.size()[:-1]
1001 else:
1002 err_msg_prefix = 'decoder_' if self.is_decoder else ''
1003 raise ValueError(
1004 f'You have to specify either {err_msg_prefix}input_ids or {err_msg_prefix}inputs_embeds'
1005 )
1006
1007 if inputs_embeds is None:
1008 assert self.embed_tokens is not None, 'You have to initialize the model with valid token embeddings'
1009 inputs_embeds = self.embed_tokens(input_ids)
1010
1011 batch_size, seq_length = input_shape
1012
1013 # required mask seq length can be calculated via length of past
1014 mask_seq_length = past_key_values[0][0].shape[
1015 2] + seq_length if past_key_values is not None else seq_length
1016
1017 if use_cache is True:
1018 assert self.is_decoder, f'`use_cache` can only be set to `True` if {self} is used as a decoder'
1019
1020 if attention_mask is None:
1021 attention_mask = torch.ones(batch_size, mask_seq_length).to(
1022 inputs_embeds.device)

Callers

nothing calls this directly

Calls 7

checkpointFunction · 0.50
create_custom_forwardFunction · 0.50
set_deviceMethod · 0.45
toMethod · 0.45
sizeMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected