




'''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      lowerCamelCase_							:




					'''simple docstring'''






					lowercase__    = ()
					lowercase__    = True






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       self.config_class

							# test default config
							_snake_case	       =       config_class()
							self.assertIsNotNone(_UpperCamelCase )
							_snake_case	       =       ["stem"] + [f'''stage{idx}''' for idx in range(1,  len(config.depths ) + 1 )]
							self.assertEqual(config.stage_names,  _UpperCamelCase )
							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
							_snake_case	       =       config_class(out_features=_UpperCamelCase,  out_indices=_UpperCamelCase )
							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
							_snake_case	       =       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
							_snake_case	       =       config_class(out_features=["stage1", "stage3"] )
							self.assertEqual(config.out_features,  ["stage1", "stage3"] )
							self.assertEqual(config.out_indices,  [1, 3] )

							# Only out_indices set
							_snake_case	       =       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(_UpperCamelCase ):
									_snake_case	       =       config_class(out_features=["stage1", "stage2"],  out_indices=[0, 2] )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case       ,    _snake_case	       =       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									_snake_case	       =       model_class(_UpperCamelCase )
									_snake_case	       =       inspect.signature(model.forward )
									# signature.parameters is an OrderedDict => so arg_names order is deterministic
									_snake_case	       =       [*signature.parameters.keys()]
									_snake_case	       =       ["pixel_values"]
									self.assertListEqual(arg_names[:1],  _UpperCamelCase )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case       ,    _snake_case	       =       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									_snake_case	       =       model_class(_UpperCamelCase )
									self.assertEqual(len(model.channels ),  len(config.out_features ) )
									_snake_case	       =       model.num_features
									_snake_case	       =       [config.stage_names.index(_UpperCamelCase ) for feat in config.out_features]
									_snake_case	       =       [num_features[idx] for idx in out_indices]
									self.assertListEqual(model.channels,  _UpperCamelCase )

									_snake_case	       =       copy.deepcopy(_UpperCamelCase )
									_snake_case	       =       None
									_snake_case	       =       model_class(_UpperCamelCase )
									self.assertEqual(len(model.channels ),  1 )
									self.assertListEqual(model.channels,  [num_features[-1]] )

									_snake_case	       =       copy.deepcopy(_UpperCamelCase )
									_snake_case	       =       None
									_snake_case	       =       model_class(_UpperCamelCase )
									self.assertEqual(len(model.channels ),  1 )
									self.assertListEqual(model.channels,  [num_features[-1]] )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case       ,    _snake_case	       =       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									_snake_case	       =       model_class(_UpperCamelCase )
									model.to(_UpperCamelCase )
									model.eval()
									_snake_case	       =       model(**_UpperCamelCase )

									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
									_snake_case	       =       copy.deepcopy(_UpperCamelCase )
									_snake_case	       =       None
									_snake_case	       =       model_class(_UpperCamelCase )
									model.to(_UpperCamelCase )
									model.eval()
									_snake_case	       =       model(**_UpperCamelCase )

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

									_snake_case	       =       copy.deepcopy(_UpperCamelCase )
									_snake_case	       =       None
									_snake_case	       =       model_class(_UpperCamelCase )
									model.to(_UpperCamelCase )
									model.eval()
									_snake_case	       =       model(**_UpperCamelCase )

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

									# Check backbone can be initialized with fresh weights
									_snake_case	       =       copy.deepcopy(_UpperCamelCase )
									_snake_case	       =       False
									_snake_case	       =       model_class(_UpperCamelCase )
									model.to(_UpperCamelCase )
									model.eval()
									_snake_case	       =       model(**_UpperCamelCase )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case       ,    _snake_case	       =       self.model_tester.prepare_config_and_inputs_for_common()

							for backbone_class in self.all_model_classes:
									_snake_case	       =       backbone_class(_UpperCamelCase )

									self.assertTrue(hasattr(_UpperCamelCase,  "backbone_type" ) )
									self.assertTrue(hasattr(_UpperCamelCase,  "stage_names" ) )
									self.assertTrue(hasattr(_UpperCamelCase,  "num_features" ) )
									self.assertTrue(hasattr(_UpperCamelCase,  "out_indices" ) )
									self.assertTrue(hasattr(_UpperCamelCase,  "out_features" ) )
									self.assertTrue(hasattr(_UpperCamelCase,  "out_feature_channels" ) )
									self.assertTrue(hasattr(_UpperCamelCase,  "channels" ) )

									self.assertIsInstance(backbone.backbone_type,  _UpperCamelCase )
									# 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 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case       ,    _snake_case	       =       self.model_tester.prepare_config_and_inputs_for_common()
							_snake_case	       =       inputs_dict["pixel_values"].shape[0]

							for backbone_class in self.all_model_classes:
									_snake_case	       =       backbone_class(_UpperCamelCase )
									backbone.to(_UpperCamelCase )
									backbone.eval()

									_snake_case	       =       backbone(**_UpperCamelCase )

									# Test default outputs and verify feature maps
									self.assertIsInstance(outputs.feature_maps,  _UpperCamelCase )
									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
									_snake_case	       =       backbone(**_UpperCamelCase,  output_hidden_states=_UpperCamelCase )
									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:
											_snake_case	       =       backbone(**_UpperCamelCase,  output_attentions=_UpperCamelCase )
											self.assertIsNotNone(outputs.attentions )











import unittest
from dataclasses import dataclass
from typing import Optional

from transformers.utils import ModelOutput
@dataclass
class       __a					( __lowerCamelCase     ):


     """simple docstring"""


     _A				:    float
     _A				:    Optional[float]									=  None
     _A				:    Optional[float]									=  None






class       __a					( unittest.TestCase     ):


     """simple docstring"""


     def   __A				(	self			: Dict						)		->   Optional[int]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0						)
         self.assertEqual(x.a				,3_0						)
         self.assertIsNone(x.b						)
         self.assertIsNone(x.c						)
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x.d




     def   __A				(	self			: Tuple						)		->   Dict:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,b=1_0						)
         self.assertEqual(x[0]				,3_0						)
         self.assertEqual(x[1]				,1_0						)
         self.assertEqual(x[:2]				,(3_0, 1_0)						)
         self.assertEqual(x[:]				,(3_0, 1_0)						)

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,c=1_0						)
         self.assertEqual(x[0]				,3_0						)
         self.assertEqual(x[1]				,1_0						)
         self.assertEqual(x[:2]				,(3_0, 1_0)						)
         self.assertEqual(x[:]				,(3_0, 1_0)						)




     def   __A				(	self			: List[str]						)		->   Dict:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,b=1_0						)
         self.assertEqual(x["""a"""]				,3_0						)
         self.assertEqual(x["""b"""]				,1_0						)
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x["""c"""]

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,c=1_0						)
         self.assertEqual(x["""a"""]				,3_0						)
         self.assertEqual(x["""c"""]				,1_0						)
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x["""b"""]




     def   __A				(	self			: Optional[int]						)		->   List[str]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0						)
         self.assertEqual(list(x.keys()						)				,["""a"""]						)
         self.assertEqual(list(x.values()						)				,[3_0]						)
         self.assertEqual(list(x.items()						)				,[("""a""", 3_0)]						)
         self.assertEqual(list(_UpperCamelCase						)				,["""a"""]						)

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,b=1_0						)
         self.assertEqual(list(x.keys()						)				,["""a""", """b"""]						)
         self.assertEqual(list(x.values()						)				,[3_0, 1_0]						)
         self.assertEqual(list(x.items()						)				,[("""a""", 3_0), ("""b""", 1_0)]						)
         self.assertEqual(list(_UpperCamelCase						)				,["""a""", """b"""]						)

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,c=1_0						)
         self.assertEqual(list(x.keys()						)				,["""a""", """c"""]						)
         self.assertEqual(list(x.values()						)				,[3_0, 1_0]						)
         self.assertEqual(list(x.items()						)				,[("""a""", 3_0), ("""c""", 1_0)]						)
         self.assertEqual(list(_UpperCamelCase						)				,["""a""", """c"""]						)

         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x.update({"""d""": 2_0}						)
         with self.assertRaises(_UpperCamelCase						):
             del x["a"]
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x.pop("""a"""						)
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x.setdefault("""d"""				,3_2						)




     def   __A				(	self			: int						)		->   List[Any]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0						)
         SCREAMING_SNAKE_CASE__								=1_0
         self.assertEqual(x.a				,1_0						)
         self.assertEqual(x["""a"""]				,1_0						)




     def   __A				(	self			: Union[str, Any]						)		->   Optional[Any]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0						)
         SCREAMING_SNAKE_CASE__								=1_0
         self.assertEqual(x.a				,1_0						)
         self.assertEqual(x["""a"""]				,1_0						)




     def   __A				(	self			: Dict						)		->   List[str]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest({"""a""": 3_0, """b""": 1_0}						)
         self.assertEqual(list(x.keys()						)				,["""a""", """b"""]						)
         self.assertEqual(x.a				,3_0						)
         self.assertEqual(x.b				,1_0						)




     def   __A				(	self			: Optional[int]						)		->   int:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest([("""a""", 3_0), ("""b""", 1_0)]						)
         self.assertEqual(list(x.keys()						)				,["""a""", """b"""]						)
         self.assertEqual(x.a				,3_0						)
         self.assertEqual(x.b				,1_0						)

         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=ModelOutputTest([("""a""", 3_0), (1_0, 1_0)]						)

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=(3_0, 3_0)						)
         self.assertEqual(list(x.keys()						)				,["""a"""]						)
         self.assertEqual(x.a				,(3_0, 3_0)						)






