code: def llama_attn_forward(
      self,
      hidden_states: torch.Tensor,
      attention_mask: Optional[torch.Tensor] = None,
      position_ids: Optional[torch.LongTensor] = None,
      past_key_value: Optional[Tuple[torch.Tensor]] = None,
      output_attentions: bool = False,
      use_cache: bool = False,
  ):
      # a copy of the original forward.
      bsz, q_len, _ = hidden_states.size()
      query_states = (
          self.q_proj(hidden_states)
          .view(bsz, q_len, self.num_heads, self.head_dim)
          .transpose(1, 2)
      )
      key_states = (
          self.k_proj(hidden_states)
          .view(bsz, q_len, self.num_heads, self.head_dim)
          .transpose(1, 2)
      )
      value_states = (
          self.v_proj(hidden_states)
          .view(bsz, q_len, self.num_heads, self.head_dim)
          .transpose(1, 2)
      )
  
      kv_seq_len = key_states.shape[-2]
      if past_key_value is not None:
          kv_seq_len += past_key_value[0].shape[-2]
      cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
      query_states, key_states = apply_rotary_pos_emb(
          query_states, key_states, cos, sin, position_ids
      )
      # [bsz, nh, t, hd]
  
      if past_key_value is not None:
          # reuse k, v, self_attention
          key_states = torch.cat([past_key_value[0], key_states], dim=2)
          value_states = torch.cat(
              [past_key_value[1], value_states], dim=2
          )
  
      past_key_value = (key_states, value_states) if use_cache else None
  
      attn_weights = torch.matmul(
          query_states, key_states.transpose(2, 3)
      ) / math.sqrt(self.head_dim)
  
      if attn_weights.size() != (
          bsz,
          self.num_heads,
          q_len,
          kv_seq_len,
      ):
          raise ValueError(
              f"Attention weights should be of size {(bsz * self.num_heads, q_len, kv_seq_len)}, but is"
              f" {attn_weights.size()}"
          )
  
      if attention_mask is not None:
          if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
              raise ValueError(
                  f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
              )
          attn_weights = attn_weights + attention_mask
          attn_weights = torch.max(
              attn_weights,
              torch.tensor(torch.finfo(attn_weights.dtype).min),
          )
  
      # upcast attention to fp32
      attn_weights = nn.functional.softmax(
          attn_weights, dim=-1, dtype=torch.float32
      ).to(query_states.dtype)
  
      attn_output = torch.matmul(attn_weights, value_states)
  
      if attn_output.size() != (
          bsz,
          self.num_heads,
          q_len,
          self.head_dim,
      ):
          raise ValueError(
              f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
              f" {attn_output.size()}"
          )
  
      attn_output = attn_output.transpose(1, 2)
      attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
  
      attn_output = self.o_proj(attn_output)
  
      if not output_attentions:
          attn_weights = None
  
      return attn_output, attn_weights, past_key_value
  
file_path: prompt-highlighter/highlighter_modules/attention_llama_llava.py
imports: import math
  import torch
  import torch.nn
  from typing import Optional, Tuple, List
  from transformers import LlamaForCausalLM
  from transformers.models.llama.modeling_llama import LlamaAttention, apply_rotary_pos_emb, repeat_kv
  from llava.model.language_model.llava_llama import LlavaLlamaForCausalLM
  from torch.nn import CrossEntropyLoss
  import types
label_desc: Use image node_id 1 for calling a global function with example usage: llama_attn_forward(hidden_states, attention_mask, position_ids, past_key_value, output_attentions, use_cache) and returns: attn_output,  attn_weights,  past_key_value