

"""simple docstring"""





import copy
import inspect

from transformers.testing_utils import require_torch, torch_device
from transformers.utils.backbone_utils import BackboneType

@require_torch
class 			__a      :

			"""simple docstring"""

			SCREAMING_SNAKE_CASE_     =  ()
			SCREAMING_SNAKE_CASE_     =  True






			def       _lowerCAmelCase	(							self   :    int  ):
								UpperCamelCase__						:			int				     =self.config_class

								# test default config
								UpperCamelCase__						:			Optional[int]				     =config_class()
								self.assertIsNotNone(lowercase_  )
								UpperCamelCase__						:			Optional[int]				     =['''stem'''] + [f'''stage{idx}''' for idx in range(1  ,     len(config.depths  ) + 1  )]
								self.assertEqual(config.stage_names  ,     lowercase_  )
								self.assertTrue(set(config.out_features  ).issubset(set(config.stage_names  )  )  )

								# Test out_features and out_indices are correctly set
								# out_features and out_indices both None
								UpperCamelCase__						:			List[Any]				     =config_class(out_features=lowercase_  ,     out_indices=lowercase_  )
								self.assertEqual(config.out_features  ,     [config.stage_names[-1]]  )
								self.assertEqual(config.out_indices  ,     [len(config.stage_names  ) - 1]  )

								# out_features and out_indices both set
								UpperCamelCase__						:			Union[str, Any]				     =config_class(out_features=['''stem''', '''stage1''']  ,     out_indices=[0, 1]  )
								self.assertEqual(config.out_features  ,     ['''stem''', '''stage1''']  )
								self.assertEqual(config.out_indices  ,     [0, 1]  )

								# Only out_features set
								UpperCamelCase__						:			int				     =config_class(out_features=['''stage1''', '''stage3''']  )
								self.assertEqual(config.out_features  ,     ['''stage1''', '''stage3''']  )
								self.assertEqual(config.out_indices  ,     [1, 3]  )

								# Only out_indices set
								UpperCamelCase__						:			Optional[Any]				     =config_class(out_indices=[0, 2]  )
								self.assertEqual(config.out_features  ,     [config.stage_names[0], config.stage_names[2]]  )
								self.assertEqual(config.out_indices  ,     [0, 2]  )

								# Error raised when out_indices do not correspond to out_features
								with self.assertRaises(lowercase_  ):
													UpperCamelCase__						:			int				     =config_class(out_features=['''stage1''', '''stage2''']  ,     out_indices=[0, 2]  )






			def       _lowerCAmelCase	(							self   :    List[Any]  ):
								UpperCamelCase__						:			Any				     =self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													UpperCamelCase__						:			List[str]				     =model_class(lowercase_  )
													UpperCamelCase__						:			Tuple				     =inspect.signature(model.forward  )
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													UpperCamelCase__						:			str				     =[*signature.parameters.keys()]
													UpperCamelCase__						:			Any				     =['''pixel_values''']
													self.assertListEqual(arg_names[:1]  ,     lowercase_  )






			def       _lowerCAmelCase	(							self   :    Optional[Any]  ):
								UpperCamelCase__						:			Any				     =self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													UpperCamelCase__						:			str				     =model_class(lowercase_  )
													self.assertEqual(len(model.channels  )  ,     len(config.out_features  )  )
													UpperCamelCase__						:			str				     =model.num_features
													UpperCamelCase__						:			Optional[Any]				     =[config.stage_names.index(lowercase_  ) for feat in config.out_features]
													UpperCamelCase__						:			Any				     =[num_features[idx] for idx in out_indices]
													self.assertListEqual(model.channels  ,     lowercase_  )

													UpperCamelCase__						:			List[Any]				     =copy.deepcopy(lowercase_  )
													UpperCamelCase__						:			List[str]				     =None
													UpperCamelCase__						:			Dict				     =model_class(lowercase_  )
													self.assertEqual(len(model.channels  )  ,     1  )
													self.assertListEqual(model.channels  ,     [num_features[-1]]  )

													UpperCamelCase__						:			Dict				     =copy.deepcopy(lowercase_  )
													UpperCamelCase__						:			List[Any]				     =None
													UpperCamelCase__						:			List[str]				     =model_class(lowercase_  )
													self.assertEqual(len(model.channels  )  ,     1  )
													self.assertListEqual(model.channels  ,     [num_features[-1]]  )






			def       _lowerCAmelCase	(							self   :    Optional[int]  ):
								UpperCamelCase__						:			List[Any]				     =self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													UpperCamelCase__						:			Optional[Any]				     =model_class(lowercase_  )
													model.to(lowercase_  )
													model.eval()
													UpperCamelCase__						:			Tuple				     =model(**lowercase_  )

													self.assertEqual(len(result.feature_maps  )  ,     len(config.out_features  )  )
													self.assertEqual(len(model.channels  )  ,     len(config.out_features  )  )
													self.assertEqual(len(result.feature_maps  )  ,     len(config.out_indices  )  )
													self.assertEqual(len(model.channels  )  ,     len(config.out_indices  )  )

													# Check output of last stage is taken if out_features=None, out_indices=None
													UpperCamelCase__						:			Optional[int]				     =copy.deepcopy(lowercase_  )
													UpperCamelCase__						:			Optional[int]				     =None
													UpperCamelCase__						:			List[Any]				     =model_class(lowercase_  )
													model.to(lowercase_  )
													model.eval()
													UpperCamelCase__						:			List[Any]				     =model(**lowercase_  )

													self.assertEqual(len(result.feature_maps  )  ,     1  )
													self.assertEqual(len(model.channels  )  ,     1  )

													UpperCamelCase__						:			Dict				     =copy.deepcopy(lowercase_  )
													UpperCamelCase__						:			List[str]				     =None
													UpperCamelCase__						:			str				     =model_class(lowercase_  )
													model.to(lowercase_  )
													model.eval()
													UpperCamelCase__						:			int				     =model(**lowercase_  )

													self.assertEqual(len(result.feature_maps  )  ,     1  )
													self.assertEqual(len(model.channels  )  ,     1  )

													# Check backbone can be initialized with fresh weights
													UpperCamelCase__						:			List[str]				     =copy.deepcopy(lowercase_  )
													UpperCamelCase__						:			List[str]				     =False
													UpperCamelCase__						:			Optional[Any]				     =model_class(lowercase_  )
													model.to(lowercase_  )
													model.eval()
													UpperCamelCase__						:			List[str]				     =model(**lowercase_  )






			def       _lowerCAmelCase	(							self   :    int  ):
								UpperCamelCase__						:			List[str]				     =self.model_tester.prepare_config_and_inputs_for_common()

								for backbone_class in self.all_model_classes:
													UpperCamelCase__						:			Tuple				     =backbone_class(lowercase_  )

													self.assertTrue(hasattr(lowercase_  ,     '''backbone_type'''  )  )
													self.assertTrue(hasattr(lowercase_  ,     '''stage_names'''  )  )
													self.assertTrue(hasattr(lowercase_  ,     '''num_features'''  )  )
													self.assertTrue(hasattr(lowercase_  ,     '''out_indices'''  )  )
													self.assertTrue(hasattr(lowercase_  ,     '''out_features'''  )  )
													self.assertTrue(hasattr(lowercase_  ,     '''out_feature_channels'''  )  )
													self.assertTrue(hasattr(lowercase_  ,     '''channels'''  )  )

													self.assertIsInstance(backbone.backbone_type  ,     lowercase_  )
													# Verify num_features has been initialized in the backbone init
													self.assertIsNotNone(backbone.num_features  )
													self.assertTrue(len(backbone.channels  ) == len(backbone.out_indices  )  )
													self.assertTrue(len(backbone.stage_names  ) == len(backbone.num_features  )  )
													self.assertTrue(len(backbone.channels  ) <= len(backbone.num_features  )  )
													self.assertTrue(len(backbone.out_feature_channels  ) == len(backbone.stage_names  )  )






			def       _lowerCAmelCase	(							self   :    List[str]  ):
								UpperCamelCase__						:			Dict				     =self.model_tester.prepare_config_and_inputs_for_common()
								UpperCamelCase__						:			Any				     =inputs_dict['''pixel_values'''].shape[0]

								for backbone_class in self.all_model_classes:
													UpperCamelCase__						:			Any				     =backbone_class(lowercase_  )
													backbone.to(lowercase_  )
													backbone.eval()

													UpperCamelCase__						:			str				     =backbone(**lowercase_  )

													# Test default outputs and verify feature maps
													self.assertIsInstance(outputs.feature_maps  ,     lowercase_  )
													self.assertTrue(len(outputs.feature_maps  ) == len(backbone.channels  )  )
													for feature_map, n_channels in zip(outputs.feature_maps  ,     backbone.channels  ):
																		self.assertTrue(feature_map.shape[:2]  ,     (batch_size, n_channels)  )
													self.assertIsNone(outputs.hidden_states  )
													self.assertIsNone(outputs.attentions  )

													# Test output_hidden_states=True
													UpperCamelCase__						:			List[str]				     =backbone(**lowercase_  ,     output_hidden_states=lowercase_  )
													self.assertIsNotNone(outputs.hidden_states  )
													self.assertTrue(len(outputs.hidden_states  )  ,     len(backbone.stage_names  )  )
													for hidden_state, n_channels in zip(outputs.hidden_states  ,     backbone.channels  ):
																		self.assertTrue(hidden_state.shape[:2]  ,     (batch_size, n_channels)  )

													# Test output_attentions=True
													if self.has_attentions:
																		UpperCamelCase__						:			Optional[int]				     =backbone(**lowercase_  ,     output_attentions=lowercase_  )
																		self.assertIsNotNone(outputs.attentions  )








import unittest

import numpy as np

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
				import tensorflow as tf

				from transformers.activations_tf import get_tf_activation





@require_tf
class 		A__				(   unittest.TestCase ):






	def  lowercase      (	self						)      ->   Dict:




						"""simple docstring"""






						__magic_name__					:							int		   =							tf.constant([-100, -1.0, -0.1, 0, 0.1, 1.0, 1_0_0.0]						)
						__magic_name__					:							List[str]		   =							get_tf_activation('''gelu'''						)
						__magic_name__					:							str		   =							get_tf_activation('''gelu_10'''						)

						__magic_name__					:							List[str]		   =							gelu(lowerCamelCase						)
						__magic_name__					:							Optional[Any]		   =							geluaa(lowerCamelCase						)

						__magic_name__					:							Optional[int]		   =							tf.where(y_gelu_aa < 1_0.0				,     1.0				,     0.0						)

						self.assertEqual(tf.math.reduce_max(lowerCamelCase						).numpy().item()				,     1_0.0						)
						self.assertTrue(np.allclose(y_gelu * clipped_mask				,     y_gelu_aa * clipped_mask						)						)






	def  lowercase      (	self						)      ->   Dict:




						"""simple docstring"""






						get_tf_activation('''gelu'''						)
						get_tf_activation('''gelu_10'''						)
						get_tf_activation('''gelu_fast'''						)
						get_tf_activation('''gelu_new'''						)
						get_tf_activation('''glu'''						)
						get_tf_activation('''mish'''						)
						get_tf_activation('''quick_gelu'''						)
						get_tf_activation('''relu'''						)
						get_tf_activation('''sigmoid'''						)
						get_tf_activation('''silu'''						)
						get_tf_activation('''swish'''						)
						get_tf_activation('''tanh'''						)
						with self.assertRaises(lowerCamelCase						):
											get_tf_activation('''bogus'''						)
						with self.assertRaises(lowerCamelCase						):
											get_tf_activation(lowerCamelCase						)



