code: def chatglm2_model_forward(
      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,
  ):
      output_hidden_states = (
          output_hidden_states
          if output_hidden_states is not None
          else self.config.output_hidden_states
      )
      use_cache = (
          use_cache if use_cache is not None else self.config.use_cache
      )
      return_dict = (
          return_dict
          if return_dict is not None
          else self.config.use_return_dict
      )
  
      batch_size, seq_length = input_ids.shape
  
      if inputs_embeds is None:
          inputs_embeds = self.embedding(input_ids)
  
      if full_attention_mask is None:
          if (
              attention_mask is not None and not attention_mask.all()
          ) or (past_key_values and seq_length != 1):
              full_attention_mask = self.get_masks(
                  input_ids,
                  past_key_values,
                  padding_mask=attention_mask,
              )
  
      use_fuse_rope = input_ids.device.type == "xpu"
      use_fuse_rope = use_fuse_rope and not self.training
  
      # Rotary positional embeddings
      rotary_pos_emb = self.rotary_pos_emb(self.seq_length)
      if position_ids is not None:
          rotary_pos_emb = rotary_pos_emb[position_ids]
      else:
          rotary_pos_emb = rotary_pos_emb[None, :seq_length]
      if use_fuse_rope:
          # Repeat cos sin here, call only once for each token.
          # Chatglm2's rotary embedding is similar to gptj's, is rotate_every_two.
          # If put this to attension forward, it will generate too many times.
          cos, sin = rotary_pos_emb.split(
              rotary_pos_emb.shape[-1] // 2, dim=-1
          )
          cos = cos.squeeze(-1)
          sin = sin.squeeze(-1)
          cos = torch.repeat_interleave(cos[:, :, None, :], 2, 3)
          sin = torch.repeat_interleave(sin[:, :, None, :], 2, 3)
          rotary_pos_emb = (cos, sin)
      else:
          rotary_pos_emb = rotary_pos_emb.transpose(0, 1).contiguous()
  
      # Run encoder.
      (
          hidden_states,
          presents,
          all_hidden_states,
          all_self_attentions,
      ) = self.encoder(
          inputs_embeds,
          full_attention_mask,
          rotary_pos_emb=rotary_pos_emb,
          kv_caches=past_key_values,
          use_cache=use_cache,
          output_hidden_states=output_hidden_states,
      )
  
      if not return_dict:
          return tuple(
              v
              for v in [
                  hidden_states,
                  presents,
                  all_hidden_states,
                  all_self_attentions,
              ]
              if v is not None
          )
  
      return BaseModelOutputWithPast(
          last_hidden_state=hidden_states,
          past_key_values=presents,
          hidden_states=all_hidden_states,
          attentions=all_self_attentions,
      )
  
file_path: BigDL/python/llm/src/bigdl/llm/transformers/models/chatglm2.py
imports: import math
  import torch
  from typing import Optional, Tuple, List
  import torch.nn.functional
  from transformers.modeling_outputs import BaseModelOutputWithPast
  from bigdl.llm.transformers.models.utils import init_kv_cache, extend_kv_cache, append_kv_cache
  from bigdl.llm.transformers.models.utils import use_flash_attention
  from bigdl.llm.transformers.models.llama import get_ipex_version
label_desc: Use image node_id 4 for calling a global function with example usage: chatglm2_model_forward(input_ids, position_ids, attention_mask, full_attention_mask, past_key_values, inputs_embeds, use_cache, output_hidden_states, return_dict) and returns: BaseModelOutputWithPast, tuple