'''simple docstring'''


import math

import tensorflow as tf
from packaging import version


def  _lowerCamelCase   (				lowerCamelCase_ :							int       ):



     """simple docstring"""





     UpperCAmelCase_							:       str       =				tf.convert_to_tensor(lowerCamelCase_       )
     UpperCAmelCase_							:       int       =				0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0       )     ,					x.dtype       )       ))

     return x * cdf


def  _lowerCamelCase   (				lowerCamelCase_ :							Optional[int]       ):



     """simple docstring"""





     UpperCAmelCase_							:       int       =				tf.convert_to_tensor(lowerCamelCase_       )
     UpperCAmelCase_							:       Union[str, Any]       =				tf.cast(math.pi     ,					x.dtype       )
     UpperCAmelCase_							:       Optional[Any]       =				tf.cast(0.04_4715     ,					x.dtype       )
     UpperCAmelCase_							:       List[Any]       =				0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi       ) * (x + coeff * tf.pow(lowerCamelCase_     ,					3       ))       ))

     return x * cdf


def  _lowerCamelCase   (				lowerCamelCase_ :							List[str]       ):



     """simple docstring"""





     UpperCAmelCase_							:       Optional[int]       =				tf.convert_to_tensor(lowerCamelCase_       )

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


def  _lowerCamelCase   (				lowerCamelCase_ :							List[str]       ):



     """simple docstring"""





     UpperCAmelCase_							:       Dict       =				tf.convert_to_tensor(lowerCamelCase_       )
     UpperCAmelCase_							:       Optional[int]       =				tf.cast(0.04_4715     ,					x.dtype       )
     UpperCAmelCase_							:       Dict       =				tf.cast(0.79_7884_5608     ,					x.dtype       )

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


def  _lowerCamelCase   (				lowerCamelCase_ :							Dict       ):



     """simple docstring"""





     UpperCAmelCase_							:       str       =				tf.convert_to_tensor(lowerCamelCase_       )
     UpperCAmelCase_							:       int       =				tf.cast(1.702     ,					x.dtype       )
     return x * tf.math.sigmoid(coeff * x       )


def  _lowerCamelCase   (				lowerCamelCase_ :							Optional[int]       ):



     """simple docstring"""





     return tf.clip_by_value(_gelu(lowerCamelCase_       )     ,					-10     ,					10       )


def  _lowerCamelCase   (				lowerCamelCase_ :							Union[str, Any]     ,					lowerCamelCase_ :							Union[str, Any]=-1       ):



     """simple docstring"""





     UpperCAmelCase_       ,      UpperCAmelCase_							:       Union[str, Any]       =				tf.split(lowerCamelCase_     ,					2     ,					axis=lowerCamelCase_       )
     return a * tf.math.sigmoid(lowerCamelCase_       )


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


 def  _lowerCamelCase   (				lowerCamelCase_ :							Optional[Any]       ):



      """simple docstring"""





      return tf.keras.activations.gelu(lowerCamelCase_     ,					approximate=lowerCamelCase_       )

 snake_case__					:      Dict				      =  tf.keras.activations.gelu





 snake_case__					:      Optional[Any]				      =  approximate_gelu_wrap
else:
 snake_case__					:      List[str]				      =  _gelu
 snake_case__					:      List[str]				      =  _gelu_new


snake_case__					:      Optional[int]				      =  {
    '''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   (				lowerCamelCase_ :							str       ):



     """simple docstring"""





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







'''simple docstring'''


import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)






class 					__SCREAMING_SNAKE_CASE						(	unittest.TestCase	):







  '''simple docstring'''






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				['a', 'b', 'c']

       # Defaults to last layer if both are None
       UpperCAmelCase_       ,      UpperCAmelCase_							:       List[str]       =				get_aligned_output_features_output_indices(snake_case_				,		snake_case_				,		snake_case_						)
       self.assertEqual(snake_case_				,		['c']						)
       self.assertEqual(snake_case_				,		[2]						)

       # Out indices set to match out features
       UpperCAmelCase_       ,      UpperCAmelCase_							:       int       =				get_aligned_output_features_output_indices(['a', 'c']				,		snake_case_				,		snake_case_						)
       self.assertEqual(snake_case_				,		['a', 'c']						)
       self.assertEqual(snake_case_				,		[0, 2]						)

       # Out features set to match out indices
       UpperCAmelCase_       ,      UpperCAmelCase_							:       List[Any]       =				get_aligned_output_features_output_indices(snake_case_				,		[0, 2]				,		snake_case_						)
       self.assertEqual(snake_case_				,		['a', 'c']						)
       self.assertEqual(snake_case_				,		[0, 2]						)

       # Out features selected from negative indices
       UpperCAmelCase_       ,      UpperCAmelCase_							:       int       =				get_aligned_output_features_output_indices(snake_case_				,		[-3, -1]				,		snake_case_						)
       self.assertEqual(snake_case_				,		['a', 'c']						)
       self.assertEqual(snake_case_				,		[-3, -1]						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['a', 'b']				,		(0, 1)				,		snake_case_						)

       # Out features must be a list
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(('a', 'b')				,		(0, 1)				,		['a', 'b']						)

       # Out features must be a subset of stage names
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['a', 'b']				,		(0, 1)				,		['a']						)

       # Out indices must be a list or tuple
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(snake_case_				,		0				,		['a', 'b']						)

       # Out indices must be a subset of stage names
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(snake_case_				,		(0, 1)				,		['a']						)

       # Out features and out indices must be the same length
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['a', 'b']				,		(0,)				,		['a', 'b', 'c']						)

       # Out features should match out indices
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['a', 'b']				,		(0, 2)				,		['a', 'b', 'c']						)

       # Out features and out indices should be in order
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['b', 'a']				,		(0, 1)				,		['a', 'b']						)

       # Check passes with valid inputs
       verify_out_features_out_indices(['a', 'b', 'd']				,		(0, 1, -1)				,		['a', 'b', 'c', 'd']						)






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[str]       =				BackboneMixin()

       UpperCAmelCase_							:       Any       =				['a', 'b', 'c']
       UpperCAmelCase_							:       str       =				['a', 'c']
       UpperCAmelCase_							:       str       =				[0, 2]

       # Check that the output features and indices are set correctly
       self.assertEqual(backbone.out_features				,		['a', 'c']						)
       self.assertEqual(backbone.out_indices				,		[0, 2]						)

       # Check out features and indices are updated correctly
       UpperCAmelCase_							:       str       =				['a', 'b']
       self.assertEqual(backbone.out_features				,		['a', 'b']						)
       self.assertEqual(backbone.out_indices				,		[0, 1]						)

       UpperCAmelCase_							:       Optional[int]       =				[-3, -1]
       self.assertEqual(backbone.out_features				,		['a', 'c']						)
       self.assertEqual(backbone.out_indices				,		[-3, -1]						)






