MCPcopy Create free account
hub / github.com/THUDM/LongWriter / forward

Method forward

train/patch/modeling_llama.py:946–1063  ·  view source on GitHub ↗
(
        self,
        input_ids: torch.LongTensor = None,
        attention_mask: Optional[torch.Tensor] = None,
        position_ids: Optional[torch.LongTensor] = None,
        past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
        inputs_embeds: Optional[torch.FloatTensor] = None,
        use_cache: Optional[bool] = None,
        output_attentions: Optional[bool] = None,
        output_hidden_states: Optional[bool] = None,
        return_dict: Optional[bool] = None,
        cache_position: Optional[torch.LongTensor] = None,
    )

Source from the content-addressed store, hash-verified

944
945 @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
946 def forward(
947 self,
948 input_ids: torch.LongTensor = None,
949 attention_mask: Optional[torch.Tensor] = None,
950 position_ids: Optional[torch.LongTensor] = None,
951 past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
952 inputs_embeds: Optional[torch.FloatTensor] = None,
953 use_cache: Optional[bool] = None,
954 output_attentions: Optional[bool] = None,
955 output_hidden_states: Optional[bool] = None,
956 return_dict: Optional[bool] = None,
957 cache_position: Optional[torch.LongTensor] = None,
958 ) -> Union[Tuple, BaseModelOutputWithPast]:
959 output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
960 output_hidden_states = (
961 output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
962 )
963 use_cache = use_cache if use_cache is not None else self.config.use_cache
964 return_dict = return_dict if return_dict is not None else self.config.use_return_dict
965
966 if (input_ids is None) ^ (inputs_embeds is not None):
967 raise ValueError(
968 "You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one"
969 )
970
971 if self.gradient_checkpointing and self.training and use_cache:
972 logger.warning_once(
973 "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`."
974 )
975 use_cache = False
976
977 if inputs_embeds is None:
978 inputs_embeds = self.embed_tokens(input_ids)
979
980 return_legacy_cache = False
981 if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs)
982 return_legacy_cache = True
983 past_key_values = DynamicCache.from_legacy_cache(past_key_values)
984 logger.warning_once(
985 "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. "
986 "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)"
987 )
988
989 if cache_position is None:
990 past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
991 cache_position = torch.arange(
992 past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
993 )
994 if position_ids is None:
995 position_ids = cache_position.unsqueeze(0)
996
997 causal_mask = self._update_causal_mask(
998 attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions
999 )
1000 hidden_states = inputs_embeds
1001
1002 # create position embeddings to be shared across the decoder layers
1003 position_embeddings = self.rotary_emb(hidden_states, position_ids)

Callers

nothing calls this directly

Calls 1

_update_causal_maskMethod · 0.95

Tested by

no test coverage detected