import math

import tensorflow as tf
from packaging import version




def       __lowerCAmelCase      (						A_   : int					):
  __UpperCAmelCase						    =       tf.convert_to_tensor(A_					)
  __UpperCAmelCase						    =       0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0					)						,	x.dtype					)					))

  return x * cdf




def       __lowerCAmelCase      (						A_   : Optional[int]					):
  __UpperCAmelCase						    =       tf.convert_to_tensor(A_					)
  __UpperCAmelCase						    =       tf.cast(math.pi						,	x.dtype					)
  __UpperCAmelCase						    =       tf.cast(0.04_47_15						,	x.dtype					)
  __UpperCAmelCase						    =       0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi					) * (x + coeff * tf.pow(A_						,	3					))					))

  return x * cdf




def       __lowerCAmelCase      (						A_   : Optional[int]					):
  __UpperCAmelCase						    =       tf.convert_to_tensor(A_					)

  return x * tf.tanh(tf.math.softplus(A_					)					)




def       __lowerCAmelCase      (						A_   : str					):
  __UpperCAmelCase						    =       tf.convert_to_tensor(A_					)
  __UpperCAmelCase						    =       tf.cast(0.04_47_15						,	x.dtype					)
  __UpperCAmelCase						    =       tf.cast(0.79_78_84_56_08						,	x.dtype					)

  return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)					))




def       __lowerCAmelCase      (						A_   : Dict					):
  __UpperCAmelCase						    =       tf.convert_to_tensor(A_					)
  __UpperCAmelCase						    =       tf.cast(1.7_02						,	x.dtype					)
  return x * tf.math.sigmoid(coeff * x					)




def       __lowerCAmelCase      (						A_   : List[str]					):
  return tf.clip_by_value(_gelu(A_					)						,	-10						,	10					)




def       __lowerCAmelCase      (						A_   : Dict						,	A_   : Union[str, Any]=-1					):
  __UpperCAmelCase    ,		__UpperCAmelCase						    =       tf.split(A_						,	2						,	axis=A_					)
  return a * tf.math.sigmoid(A_					)


if version.parse(tf.version.VERSION) >= version.parse("""2.4"""):




       def       __lowerCAmelCase      (						A_   : List[Any]					):
         return tf.keras.activations.gelu(A_						,	approximate=A_					)

       a_							=      tf.keras.activations.gelu





       a_							=      approximate_gelu_wrap
else:
       a_							=      _gelu
       a_							=      _gelu_new


a_							=      {
    """gelu""": gelu,
    """gelu_10""": gelu_aa,
    """gelu_fast""": gelu_fast,
    """gelu_new""": gelu_new,
    """glu""": glu,
    """mish""": mish,
    """quick_gelu""": quick_gelu,
    """relu""": tf.keras.activations.relu,
    """sigmoid""": tf.keras.activations.sigmoid,
    """silu""": tf.keras.activations.swish,
    """swish""": tf.keras.activations.swish,
    """tanh""": tf.keras.activations.tanh,
}





def       __lowerCAmelCase      (						A_   : Any					):
  if activation_string in ACTaFN:
    return ACTaFN[activation_string]
  else:
    raise KeyError(F'''function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()					)}'''					)




def       __lowerCAmelCase      (						A_   : str					)    ->  Any:
  if not head:
    return True
  # split the list to two parts
  __UpperCAmelCase    ,		__UpperCAmelCase						    =       head.next, head
  while fast and fast.next:
    __UpperCAmelCase						    =       fast.next.next
    __UpperCAmelCase						    =       slow.next
  __UpperCAmelCase						    =       slow.next
  __UpperCAmelCase						    =       None  # Don't forget here! But forget still works!
  # reverse the second part
  __UpperCAmelCase						    =       None
  while second:
    __UpperCAmelCase						    =       second.next
    __UpperCAmelCase						    =       node
    __UpperCAmelCase						    =       second
    __UpperCAmelCase						    =       nxt
  # compare two parts
  # second part has the same or one less node
  while node:
    if node.val != head.val:
      return False
    __UpperCAmelCase						    =       node.next
    __UpperCAmelCase						    =       head.next
  return True




def       __lowerCAmelCase      (						A_   : Optional[Any]					)    ->  Optional[Any]:
  if not head or not head.next:
    return True

  # 1. Get the midpoint (slow)
  __UpperCAmelCase						    =       __UpperCAmelCase						    =       __UpperCAmelCase						    =       head
  while fast and fast.next:
    __UpperCAmelCase    ,		__UpperCAmelCase						    =       fast.next.next, slow.next

  # 2. Push the second half into the stack
  __UpperCAmelCase						    =       [slow.val]
  while slow.next:
    __UpperCAmelCase						    =       slow.next
    stack.append(slow.val					)

  # 3. Comparison
  while stack:
    if stack.pop() != cur.val:
      return False
    __UpperCAmelCase						    =       cur.next

  return True





def       __lowerCAmelCase      (						A_   : int					)    ->  Optional[int]:
  if not head or not head.next:
    return True
  __UpperCAmelCase						    =       {}
  __UpperCAmelCase						    =       0
  while head:
    if head.val in d:
      d[head.val].append(A_					)
    else:
      __UpperCAmelCase						    =       [pos]
    __UpperCAmelCase						    =       head.next
    pos += 1
  __UpperCAmelCase						    =       pos - 1
  __UpperCAmelCase						    =       0
  for v in d.values():
    if len(A_					) % 2 != 0:
      middle += 1
    else:
      __UpperCAmelCase						    =       0
      for i in range(0						,	len(A_					)					):
        if v[i] + v[len(A_					) - 1 - step] != checksum:
          return False
        step += 1
    if middle > 1:
      return False
  return True



