code: def apply_rotary_pos_emb_chatglm(
      x: torch.Tensor, rope_cache: torch.Tensor
  ) -> torch.Tensor:
      # x: [sq, b, np, hn]
      sq, b, np, hn = x.size(0), x.size(1), x.size(2), x.size(3)
      rot_dim = rope_cache.shape[-2] * 2
      x, x_pass = x[..., :rot_dim], x[..., rot_dim:]
      # truncate to support variable sizes
      rope_cache = rope_cache[:sq]
      xshaped = x.reshape(sq, -1, np, rot_dim // 2, 2)
      rope_cache = rope_cache.view(sq, -1, 1, xshaped.size(3), 2)
      x_out2 = torch.stack(
          [
              xshaped[..., 0] * rope_cache[..., 0]
              - xshaped[..., 1] * rope_cache[..., 1],
              xshaped[..., 1] * rope_cache[..., 0]
              + xshaped[..., 0] * rope_cache[..., 1],
          ],
          -1,
      )
      x_out2 = x_out2.flatten(3)
      return torch.cat((x_out2, x_pass), dim=-1)
  
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 2 for calling a global function with example usage: apply_rotary_pos_emb_chatglm(x, rope_cache) and returns: torch