





"""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       _SCREAMING_SNAKE_CASE:
       SCREAMING_SNAKE_CASE_     :   str					=						()
       SCREAMING_SNAKE_CASE_     :   Dict					=						True




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :List[Any]	       =						self.config_class

             # test default config
             __SCREAMING_SNAKE_CASE     :List[Any]	       =						config_class()
             self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE     :Optional[int]	       =						['''stem'''] + [f'''stage{idx}''' for idx in range(1	,len(config.depths							) + 1							)]
             self.assertEqual(config.stage_names	,SCREAMING_SNAKE_CASE__							)
             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
             __SCREAMING_SNAKE_CASE     :Dict	       =						config_class(out_features=SCREAMING_SNAKE_CASE__	,out_indices=SCREAMING_SNAKE_CASE__							)
             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
             __SCREAMING_SNAKE_CASE     :List[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
             __SCREAMING_SNAKE_CASE     :Optional[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
             __SCREAMING_SNAKE_CASE     :Tuple	       =						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(SCREAMING_SNAKE_CASE__							):
                   __SCREAMING_SNAKE_CASE     :Optional[int]	       =						config_class(out_features=['''stage1''', '''stage2''']	,out_indices=[0, 2]							)




       def 					_UpperCamelCase  (		self							)    ->   List[str]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE     :Dict	       =						self.model_tester.prepare_config_and_inputs_for_common()

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




       def 					_UpperCamelCase  (		self							)    ->   str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE     :List[Any]	       =						self.model_tester.prepare_config_and_inputs_for_common()

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

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

                   __SCREAMING_SNAKE_CASE     :Any	       =						copy.deepcopy(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE     :int	       =						None
                   __SCREAMING_SNAKE_CASE     :Optional[int]	       =						model_class(SCREAMING_SNAKE_CASE__							)
                   self.assertEqual(len(model.channels							)	,1							)
                   self.assertListEqual(model.channels	,[num_features[-1]]							)




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE     :List[Any]	       =						self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                   __SCREAMING_SNAKE_CASE     :Optional[int]	       =						model_class(SCREAMING_SNAKE_CASE__							)
                   model.to(SCREAMING_SNAKE_CASE__							)
                   model.eval()
                   __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						model(**SCREAMING_SNAKE_CASE__							)

                   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
                   __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						copy.deepcopy(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE     :Optional[int]	       =						None
                   __SCREAMING_SNAKE_CASE     :Dict	       =						model_class(SCREAMING_SNAKE_CASE__							)
                   model.to(SCREAMING_SNAKE_CASE__							)
                   model.eval()
                   __SCREAMING_SNAKE_CASE     :Optional[int]	       =						model(**SCREAMING_SNAKE_CASE__							)

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

                   __SCREAMING_SNAKE_CASE     :Dict	       =						copy.deepcopy(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE     :int	       =						None
                   __SCREAMING_SNAKE_CASE     :int	       =						model_class(SCREAMING_SNAKE_CASE__							)
                   model.to(SCREAMING_SNAKE_CASE__							)
                   model.eval()
                   __SCREAMING_SNAKE_CASE     :int	       =						model(**SCREAMING_SNAKE_CASE__							)

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

                   # Check backbone can be initialized with fresh weights
                   __SCREAMING_SNAKE_CASE     :int	       =						copy.deepcopy(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE     :Dict	       =						False
                   __SCREAMING_SNAKE_CASE     :Dict	       =						model_class(SCREAMING_SNAKE_CASE__							)
                   model.to(SCREAMING_SNAKE_CASE__							)
                   model.eval()
                   __SCREAMING_SNAKE_CASE     :Any	       =						model(**SCREAMING_SNAKE_CASE__							)




       def 					_UpperCamelCase  (		self							)    ->   str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE     :List[str]	       =						self.model_tester.prepare_config_and_inputs_for_common()

             for backbone_class in self.all_model_classes:
                   __SCREAMING_SNAKE_CASE     :str	       =						backbone_class(SCREAMING_SNAKE_CASE__							)

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

                   self.assertIsInstance(backbone.backbone_type	,SCREAMING_SNAKE_CASE__							)
                   # 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 					_UpperCamelCase  (		self							)    ->   Any:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE     :Any	       =						self.model_tester.prepare_config_and_inputs_for_common()
             __SCREAMING_SNAKE_CASE     :Tuple	       =						inputs_dict['''pixel_values'''].shape[0]

             for backbone_class in self.all_model_classes:
                   __SCREAMING_SNAKE_CASE     :str	       =						backbone_class(SCREAMING_SNAKE_CASE__							)
                   backbone.to(SCREAMING_SNAKE_CASE__							)
                   backbone.eval()

                   __SCREAMING_SNAKE_CASE     :Dict	       =						backbone(**SCREAMING_SNAKE_CASE__							)

                   # Test default outputs and verify feature maps
                   self.assertIsInstance(outputs.feature_maps	,SCREAMING_SNAKE_CASE__							)
                   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
                   __SCREAMING_SNAKE_CASE     :List[Any]	       =						backbone(**SCREAMING_SNAKE_CASE__	,output_hidden_states=SCREAMING_SNAKE_CASE__							)
                   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:
                         __SCREAMING_SNAKE_CASE     :Optional[int]	       =						backbone(**SCREAMING_SNAKE_CASE__	,output_attentions=SCREAMING_SNAKE_CASE__							)
                         self.assertIsNotNone(outputs.attentions							)






"""simple docstring"""
# Lint as: python3

import hashlib
from typing import Union



def   __lowerCamelCase     (							a_  :	Union[str, int, bytes]							)    ->   bytes:
      if isinstance(a_				,     a_							):
            # Data already in bytes, returns as it as
            return hash_data
      elif isinstance(a_				,     a_							):
            # We keep the data as it as for it ot be later encoded to UTF-8
            # However replace `\\` with `/` for Windows compatibility
            __SCREAMING_SNAKE_CASE     :str	       =						hash_data.replace('''\\'''				,     '''/'''							)
      elif isinstance(a_				,     a_							):
            __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						str(a_							)
      else:
            # If data is not of the required type, raise error
            raise InvalidKeyError(a_							)

      return hash_data.encode('''utf-8'''							)






class       _SCREAMING_SNAKE_CASE(     A						):




       def __init__(		self	,SCREAMING_SNAKE_CASE__							)    ->   Optional[int]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :Tuple	       =						'''\nFAILURE TO GENERATE DATASET: Invalid key type detected'''
             __SCREAMING_SNAKE_CASE     :int	       =						f'''\nFound Key {hash_data} of type {type(SCREAMING_SNAKE_CASE__							)}'''
             __SCREAMING_SNAKE_CASE     :Optional[int]	       =						'''\nKeys should be either str, int or bytes type'''
             super().__init__(f'''{self.prefix}{self.err_msg}{self.suffix}'''							)






class       _SCREAMING_SNAKE_CASE(     A						):




       def __init__(		self	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__=""							)    ->   List[str]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :str	       =						key
             __SCREAMING_SNAKE_CASE     :str	       =						duplicate_key_indices
             __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						fix_msg
             __SCREAMING_SNAKE_CASE     :int	       =						'''Found multiple examples generated with the same key'''
             if len(SCREAMING_SNAKE_CASE__							) <= 20:
                   __SCREAMING_SNAKE_CASE     :Dict	       =						f'''\nThe examples at index {", ".join(SCREAMING_SNAKE_CASE__							)} have the key {key}'''
             else:
                   __SCREAMING_SNAKE_CASE     :List[str]	       =						f'''\nThe examples at index {", ".join(duplicate_key_indices[:20]							)}... ({len(SCREAMING_SNAKE_CASE__							) - 20} more) have the key {key}'''
             __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						'''\n''' + fix_msg if fix_msg else ''''''
             super().__init__(f'''{self.prefix}{self.err_msg}{self.suffix}'''							)






class       _SCREAMING_SNAKE_CASE:




       def __init__(		self	,SCREAMING_SNAKE_CASE__							)    ->   Union[str, Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :Dict	       =						hashlib.mda(_as_bytes(SCREAMING_SNAKE_CASE__							)							)




       def 					_UpperCamelCase  (		self	,SCREAMING_SNAKE_CASE__							)    ->   int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :Tuple	       =						self._split_mda.copy()
             __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						_as_bytes(SCREAMING_SNAKE_CASE__							)
             mda.update(SCREAMING_SNAKE_CASE__							)
             # Convert to integer with hexadecimal conversion
             return int(mda.hexdigest()	,16							)