



import math

import tensorflow as tf
from packaging import version


def    __lowerCamelCase   (  lowerCamelCase__		):

				"""simple docstring"""


				lowercase__	:       Optional[int]						=	tf.convert_to_tensor(UpperCAmelCase_		)
				lowercase__	:       Optional[Any]						=	0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0		)	,					x.dtype		)		))

				return x * cdf


def    __lowerCamelCase   (  lowerCamelCase__		):

				"""simple docstring"""


				lowercase__	:       Any						=	tf.convert_to_tensor(UpperCAmelCase_		)
				lowercase__	:       Dict						=	tf.cast(math.pi	,					x.dtype		)
				lowercase__	:       Union[str, Any]						=	tf.cast(0.044715	,					x.dtype		)
				lowercase__	:       Union[str, Any]						=	0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi		) * (x + coeff * tf.pow(UpperCAmelCase_	,					3		))		))

				return x * cdf


def    __lowerCamelCase   (  lowerCamelCase__		):

				"""simple docstring"""


				lowercase__	:       Any						=	tf.convert_to_tensor(UpperCAmelCase_		)

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


def    __lowerCamelCase   (  lowerCamelCase__		):

				"""simple docstring"""


				lowercase__	:       Optional[Any]						=	tf.convert_to_tensor(UpperCAmelCase_		)
				lowercase__	:       List[str]						=	tf.cast(0.044715	,					x.dtype		)
				lowercase__	:       Union[str, Any]						=	tf.cast(0.7978845608	,					x.dtype		)

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


def    __lowerCamelCase   (  lowerCamelCase__		):

				"""simple docstring"""


				lowercase__	:       Any						=	tf.convert_to_tensor(UpperCAmelCase_		)
				lowercase__	:       Optional[int]						=	tf.cast(1.702	,					x.dtype		)
				return x * tf.math.sigmoid(coeff * x		)


def    __lowerCamelCase   (  lowerCamelCase__		):

				"""simple docstring"""


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


def    __lowerCamelCase   (  lowerCamelCase__	,					lowerCamelCase__=-1		):

				"""simple docstring"""


				lowercase__				,				lowercase__	:       Optional[Any]						=	tf.split(UpperCAmelCase_	,					2	,					axis=UpperCAmelCase_		)
				return a * tf.math.sigmoid(UpperCAmelCase_		)


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


				def    __lowerCamelCase   (  lowerCamelCase__		):

								"""simple docstring"""


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

				lowerCAmelCase__	    =    tf.keras.activations.gelu
				lowerCAmelCase__	    =    approximate_gelu_wrap
else:
				lowerCAmelCase__	    =    _gelu
				lowerCAmelCase__	    =    _gelu_new


lowerCAmelCase__	    =    {
    '''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__		):

				"""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       UpperCamelCase__			(   unittest.TestCase      ):

			"""simple docstring"""


			def   snake_case__	(      self		)      ->	Dict:
				A__											=					["a", "b", "c"]

				# Defaults to last layer if both are None
				A__							,							A__											=					get_aligned_output_features_output_indices(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)
				self.assertEqual(SCREAMING_SNAKE_CASE__						,	["c"]		)
				self.assertEqual(SCREAMING_SNAKE_CASE__						,	[2]		)

				# Out indices set to match out features
				A__							,							A__											=					get_aligned_output_features_output_indices(["a", "c"]						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)
				self.assertEqual(SCREAMING_SNAKE_CASE__						,	["a", "c"]		)
				self.assertEqual(SCREAMING_SNAKE_CASE__						,	[0, 2]		)

				# Out features set to match out indices
				A__							,							A__											=					get_aligned_output_features_output_indices(SCREAMING_SNAKE_CASE__						,	[0, 2]						,	SCREAMING_SNAKE_CASE__		)
				self.assertEqual(SCREAMING_SNAKE_CASE__						,	["a", "c"]		)
				self.assertEqual(SCREAMING_SNAKE_CASE__						,	[0, 2]		)

				# Out features selected from negative indices
				A__							,							A__											=					get_aligned_output_features_output_indices(SCREAMING_SNAKE_CASE__						,	[-3, -1]						,	SCREAMING_SNAKE_CASE__		)
				self.assertEqual(SCREAMING_SNAKE_CASE__						,	["a", "c"]		)
				self.assertEqual(SCREAMING_SNAKE_CASE__						,	[-3, -1]		)


			def   snake_case__	(      self		)      ->	Dict:
				# Stage names must be set
				with self.assertRaises(SCREAMING_SNAKE_CASE__		):
					verify_out_features_out_indices(["a", "b"]						,	(0, 1)						,	SCREAMING_SNAKE_CASE__		)

				# Out features must be a list
				with self.assertRaises(SCREAMING_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(SCREAMING_SNAKE_CASE__		):
					verify_out_features_out_indices(["a", "b"]						,	(0, 1)						,	["a"]		)

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

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

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

				# Out features should match out indices
				with self.assertRaises(SCREAMING_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(SCREAMING_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   snake_case__	(      self		)      ->	List[Any]:
				A__											=					BackboneMixin()

				A__											=					["a", "b", "c"]
				A__											=					["a", "c"]
				A__											=					[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
				A__											=					["a", "b"]
				self.assertEqual(backbone.out_features						,	["a", "b"]		)
				self.assertEqual(backbone.out_indices						,	[0, 1]		)

				A__											=					[-3, -1]
				self.assertEqual(backbone.out_features						,	["a", "c"]		)
				self.assertEqual(backbone.out_indices						,	[-3, -1]		)




