


import copy
import inspect

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

@require_torch
class   lowerCamelCase						:
       _UpperCamelCase     		=   ()
       _UpperCamelCase     		=   True

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Dict							=   self.config_class

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

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case	,       snake_case :			int							=   self.model_tester.prepare_config_and_inputs_for_common()

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

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case	,       snake_case :			str							=   self.model_tester.prepare_config_and_inputs_for_common()

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

         snake_case :			Dict							=   copy.deepcopy(a__       )
         snake_case :			Optional[int]							=   None
         snake_case :			int							=   model_class(a__       )
         self.assertEqual(len(model.channels       )						,    1       )
         self.assertListEqual(model.channels						,    [num_features[-1]]       )

         snake_case :			Tuple							=   copy.deepcopy(a__       )
         snake_case :			str							=   None
         snake_case :			int							=   model_class(a__       )
         self.assertEqual(len(model.channels       )						,    1       )
         self.assertListEqual(model.channels						,    [num_features[-1]]       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case	,       snake_case :			str							=   self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
         snake_case :			Tuple							=   model_class(a__       )
         model.to(a__       )
         model.eval()
         snake_case :			List[str]							=   model(**a__       )

         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 :			List[Any]							=   copy.deepcopy(a__       )
         snake_case :			List[Any]							=   None
         snake_case :			Optional[Any]							=   model_class(a__       )
         model.to(a__       )
         model.eval()
         snake_case :			List[str]							=   model(**a__       )

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

         snake_case :			Optional[Any]							=   copy.deepcopy(a__       )
         snake_case :			List[Any]							=   None
         snake_case :			Optional[int]							=   model_class(a__       )
         model.to(a__       )
         model.eval()
         snake_case :			List[str]							=   model(**a__       )

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

         # Check backbone can be initialized with fresh weights
         snake_case :			List[Any]							=   copy.deepcopy(a__       )
         snake_case :			Any							=   False
         snake_case :			Any							=   model_class(a__       )
         model.to(a__       )
         model.eval()
         snake_case :			int							=   model(**a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case	,       snake_case :			Optional[Any]							=   self.model_tester.prepare_config_and_inputs_for_common()

        for backbone_class in self.all_model_classes:
         snake_case :			Optional[int]							=   backbone_class(a__       )

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

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






        '''simple docstring'''






        snake_case	,       snake_case :			Union[str, Any]							=   self.model_tester.prepare_config_and_inputs_for_common()
        snake_case :			List[Any]							=   inputs_dict['''pixel_values'''].shape[0]

        for backbone_class in self.all_model_classes:
         snake_case :			List[Any]							=   backbone_class(a__       )
         backbone.to(a__       )
         backbone.eval()

         snake_case :			Tuple							=   backbone(**a__       )

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










import copy

from transformers.configuration_utils import PretrainedConfig
from transformers.utils import logging


a					:				Optional[Any]       =      logging.get_logger(__name__)

class   lowerCamelCase						(	SCREAMING_SNAKE_CASE  ):
       _UpperCamelCase     		=   """hybrid-clip"""
       _UpperCamelCase     		=   True

       def __init__(      self						,    a__=5_12						,    **a__       ):






        '''simple docstring'''






        super().__init__(**a__       )

        if "text_config" not in kwargs:
         raise ValueError('''`text_config` can not be `None`.'''       )

        if "vision_config" not in kwargs:
         raise ValueError('''`vision_config` can not be `None`.'''       )

        snake_case :			int							=   kwargs.pop('''text_config'''       )
        snake_case :			Optional[int]							=   kwargs.pop('''vision_config'''       )

        snake_case :			Optional[Any]							=   text_config.pop('''model_type'''       )
        snake_case :			str							=   vision_config.pop('''model_type'''       )

        from transformers import AutoConfig

        snake_case :			List[Any]							=   AutoConfig.for_model(a__						,    **a__       )

        if vision_model_type == "clip":
         snake_case :			Dict							=   AutoConfig.for_model(a__						,    **a__       ).vision_config
        elif vision_model_type == "clip_vision_model":
         from transformers import CLIPVisionConfig

         snake_case :			str							=   CLIPVisionConfig(**a__       )
        else:
         snake_case :			Any							=   AutoConfig.for_model(a__						,    **a__       )

        snake_case :			Optional[Any]							=   projection_dim
        snake_case :			int							=   1.0

       @classmethod
       def 					UpperCAmelCase					(      cls						,    a__						,    a__						,    **a__       ):






        '''simple docstring'''






        return cls(text_config=text_config.to_dict()						,    vision_config=vision_config.to_dict()						,    **a__       )



       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Dict							=   copy.deepcopy(self.__dict__       )
        snake_case :			Dict							=   self.text_config.to_dict()
        snake_case :			int							=   self.vision_config.to_dict()
        snake_case :			List[Any]							=   self.__class__.model_type
        return output






