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

Method forward

train/patch/modeling_chatglm.py:671–720  ·  view source on GitHub ↗
(
            self,
            input_ids,
            position_ids: Optional[torch.Tensor] = None,
            attention_mask: Optional[torch.BoolTensor] = None,
            full_attention_mask: Optional[torch.BoolTensor] = None,
            past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
            inputs_embeds: Optional[torch.Tensor] = None,
            use_cache: Optional[bool] = None,
            output_hidden_states: Optional[bool] = None,
            return_dict: Optional[bool] = None,
    )

Source from the content-addressed store, hash-verified

669 return self.embedding.word_embeddings
670
671 def forward(
672 self,
673 input_ids,
674 position_ids: Optional[torch.Tensor] = None,
675 attention_mask: Optional[torch.BoolTensor] = None,
676 full_attention_mask: Optional[torch.BoolTensor] = None,
677 past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
678 inputs_embeds: Optional[torch.Tensor] = None,
679 use_cache: Optional[bool] = None,
680 output_hidden_states: Optional[bool] = None,
681 return_dict: Optional[bool] = None,
682 ):
683 output_hidden_states = (
684 output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
685 )
686 use_cache = use_cache if use_cache is not None else self.config.use_cache
687 return_dict = return_dict if return_dict is not None else self.config.use_return_dict
688
689 batch_size, seq_length = input_ids.shape
690
691 if inputs_embeds is None:
692 inputs_embeds = self.embedding(input_ids)
693
694 # if full_attention_mask is None:
695 # if (attention_mask is not None and not attention_mask.all()) or (past_key_values and seq_length != 1):
696 # full_attention_mask = self.get_masks(input_ids, past_key_values, padding_mask=attention_mask)
697
698 # Rotary positional embeddings
699 rotary_pos_emb = self.rotary_pos_emb(self.seq_length)
700 if position_ids is not None:
701 rotary_pos_emb = rotary_pos_emb[position_ids]
702 else:
703 rotary_pos_emb = rotary_pos_emb[None, :seq_length]
704 rotary_pos_emb = rotary_pos_emb.transpose(0, 1).contiguous()
705
706 # Run encoder.
707 hidden_states, presents, all_hidden_states, all_self_attentions = self.encoder(
708 inputs_embeds, attention_mask, rotary_pos_emb=rotary_pos_emb,
709 kv_caches=past_key_values, use_cache=use_cache, output_hidden_states=output_hidden_states
710 )
711
712 if not return_dict:
713 return tuple(v for v in [hidden_states, presents, all_hidden_states, all_self_attentions] if v is not None)
714
715 return BaseModelOutputWithPast(
716 last_hidden_state=hidden_states,
717 past_key_values=presents,
718 hidden_states=all_hidden_states,
719 attentions=all_self_attentions,
720 )
721
722
723class ChatGLMForConditionalGeneration(ChatGLMPreTrainedModel):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected