code: def scaled_dot_product_attention(
      q, k, v, mask, attention_mask=None, head_mask=None
  ):
      # calculate attention
      matmul_qk = torch.matmul(q, k.permute(0, 1, 3, 2))
  
      dk = k.shape[-1]
      scaled_attention_logits = matmul_qk / np.sqrt(dk)
  
      if mask is not None:
          nd, ns = scaled_attention_logits.size(
              -2
          ), scaled_attention_logits.size(-1)
          scaled_attention_logits += mask[ns - nd : ns, :ns] * -1e4
  
      if attention_mask is not None:
          # Apply the attention mask
          scaled_attention_logits = (
              scaled_attention_logits + attention_mask
          )
  
      attention_weights = torch.softmax(scaled_attention_logits, dim=-1)
  
      # Mask heads if we want to
      if head_mask is not None:
          attention_weights = attention_weights * head_mask
  
      output = torch.matmul(attention_weights, v)
  
      return output, attention_weights
  
imports: from typing import Optional, Tuple, Union
  import numpy
  import torch
  from torch import nn
  from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
  from ...modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutput
  from ...modeling_utils import PreTrainedModel
  from ...pytorch_utils import Conv1D, find_pruneable_heads_and_indices, prune_linear_layer
  from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings
  from .configuration_ctrl import CTRLConfig
label_desc: Use image node_id 3 for calling a global function with example usage: scaled_dot_product_attention(q, k, v, mask, attention_mask, head_mask) and returns: output,  attention_weights