


'''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   __UpperCamelCase		:
							lowercase	:						List[Any]      						=     ()
							lowercase	:						Union[str, Any]      						=     True





							def 						a__      (     self						:Union[str, Any]	):
									snake_case_						:							List[Any]					=  self.config_class

									# test default config
									snake_case_						:							Union[str, Any]					=  config_class()
									self.assertIsNotNone(_UpperCamelCase	)
									snake_case_						:							Optional[int]					=  ["""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_						:							str					=  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_						:							str					=  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_						:							Any					=  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_						:							str					=  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_						:							Dict					=  config_class(out_features=["""stage1""", """stage2"""]     ,out_indices=[0, 2]	)





							def 						a__      (     self						:str	):
									snake_case_						:							Union[str, Any]					=  self.model_tester.prepare_config_and_inputs_for_common()

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





							def 						a__      (     self						:List[Any]	):
									snake_case_						:							Dict					=  self.model_tester.prepare_config_and_inputs_for_common()

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

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

											snake_case_						:							Optional[Any]					=  copy.deepcopy(_UpperCamelCase	)
											snake_case_						:							List[Any]					=  None
											snake_case_						:							Any					=  model_class(_UpperCamelCase	)
											self.assertEqual(len(model.channels	)     ,1	)
											self.assertListEqual(model.channels     ,[num_features[-1]]	)





							def 						a__      (     self						:Union[str, Any]	):
									snake_case_						:							str					=  self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											snake_case_						:							List[Any]					=  model_class(_UpperCamelCase	)
											model.to(_UpperCamelCase	)
											model.eval()
											snake_case_						:							str					=  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_						:							Dict					=  copy.deepcopy(_UpperCamelCase	)
											snake_case_						:							int					=  None
											snake_case_						:							Any					=  model_class(_UpperCamelCase	)
											model.to(_UpperCamelCase	)
											model.eval()
											snake_case_						:							Optional[int]					=  model(**_UpperCamelCase	)

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

											snake_case_						:							List[Any]					=  copy.deepcopy(_UpperCamelCase	)
											snake_case_						:							Optional[Any]					=  None
											snake_case_						:							Union[str, Any]					=  model_class(_UpperCamelCase	)
											model.to(_UpperCamelCase	)
											model.eval()
											snake_case_						:							Dict					=  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_						:							Union[str, Any]					=  copy.deepcopy(_UpperCamelCase	)
											snake_case_						:							int					=  False
											snake_case_						:							Tuple					=  model_class(_UpperCamelCase	)
											model.to(_UpperCamelCase	)
											model.eval()
											snake_case_						:							Any					=  model(**_UpperCamelCase	)





							def 						a__      (     self						:List[str]	):
									snake_case_						:							Any					=  self.model_tester.prepare_config_and_inputs_for_common()

									for backbone_class in self.all_model_classes:
											snake_case_						:							List[Any]					=  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 						a__      (     self						:List[str]	):
									snake_case_						:							int					=  self.model_tester.prepare_config_and_inputs_for_common()
									snake_case_						:							Union[str, Any]					=  inputs_dict["""pixel_values"""].shape[0]

									for backbone_class in self.all_model_classes:
											snake_case_						:							Union[str, Any]					=  backbone_class(_UpperCamelCase	)
											backbone.to(_UpperCamelCase	)
											backbone.eval()

											snake_case_						:							Tuple					=  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_						:							Optional[Any]					=  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_						:							Optional[Any]					=  backbone(**_UpperCamelCase     ,output_attentions=_UpperCamelCase	)
													self.assertIsNotNone(outputs.attentions	)


import importlib.metadata
import subprocess
import sys





def 	snake_case_      (__A     :						bool = False     )			->	str:
   __lowerCAmelCase  :			Any					    =						False
   if "IPython" in sys.modules:
      __lowerCAmelCase  :			Optional[int]					    =						"""google.colab""" in str(sys.modules["""IPython"""].get_ipython()     )

   if in_colab:
      if upgrade:
         __lowerCAmelCase  :			int					    =						["""pip""", """install""", """-U""", """torch"""]
         subprocess.run(__A					,   check=__A     )
      # get the current version of torch
      __lowerCAmelCase  :			List[Any]					    =						importlib.metadata.version("""torch"""     )
      __lowerCAmelCase  :			List[Any]					    =						torch_version[: torch_version.rindex("""."""     )]
      __lowerCAmelCase  :			int					    =						f'''https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-{torch_version_trunc}-cp37-cp37m-linux_x86_64.whl'''
      __lowerCAmelCase  :			List[Any]					    =						["""pip""", """install""", xla_wheel]
      subprocess.run(__A					,   check=__A     )
   else:
      raise RuntimeError("""`install_xla` utility works only on google colab."""     )


