code: def scaled_dot_product_attention(
      q, k, v, mask, attention_mask=None, head_mask=None
  ):
      # calculate attention
      matmul_qk = tf.matmul(q, k, transpose_b=True)
  
      dk = tf.cast(shape_list(k)[-1], dtype=matmul_qk.dtype)
      scaled_attention_logits = matmul_qk / tf.math.sqrt(dk)
  
      if mask is not None:
          scaled_attention_logits += tf.cast(
              mask * -1e4, dtype=scaled_attention_logits.dtype
          )
  
      if attention_mask is not None:
          # Apply the attention mask
          attention_mask = tf.cast(
              attention_mask, dtype=scaled_attention_logits.dtype
          )
          scaled_attention_logits = (
              scaled_attention_logits + attention_mask
          )
  
      attention_weights = stable_softmax(
          scaled_attention_logits, axis=-1
      )
  
      # Mask heads if we want to
      if head_mask is not None:
          attention_weights = attention_weights * head_mask
  
      output = tf.matmul(attention_weights, v)
  
      return output, attention_weights
  
imports: import warnings
  from typing import Optional, Tuple, Union
  import numpy
  import tensorflow
  from ...modeling_tf_outputs import TFBaseModelOutputWithPast, TFCausalLMOutputWithPast, TFSequenceClassifierOutput
  from ...modeling_tf_utils import TFCausalLanguageModelingLoss, TFModelInputType, TFPreTrainedModel, TFSequenceClassificationLoss, TFSharedEmbeddings, get_initializer, keras_serializable, unpack_inputs
  from ...tf_utils import shape_list, stable_softmax
  from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
  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