"""simple docstring"""






import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch

       from transformers import CvtForImageClassification, CvtModel
       from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor


class        UpperCAmelCase		(__A    ):



    """simple docstring"""

    def _snake_case     (  self				):
       lowercase__:  Any   = self.config_class(**self.inputs_dict				)
       self.parent.assertTrue(hasattr(_UpperCAmelCase       ,     '''embed_dim'''				)				)
       self.parent.assertTrue(hasattr(_UpperCAmelCase       ,     '''num_heads'''				)				)



class        UpperCAmelCase		:



    """simple docstring"""

    def __init__(  self       ,     _UpperCAmelCase       ,     _UpperCAmelCase=13       ,     _UpperCAmelCase=64       ,     _UpperCAmelCase=3       ,     _UpperCAmelCase=[16, 48, 96]       ,     _UpperCAmelCase=[1, 3, 6]       ,     _UpperCAmelCase=[1, 2, 10]       ,     _UpperCAmelCase=[7, 3, 3]       ,     _UpperCAmelCase=[4, 2, 2]       ,     _UpperCAmelCase=[2, 1, 1]       ,     _UpperCAmelCase=[2, 2, 2]       ,     _UpperCAmelCase=[False, False, True]       ,     _UpperCAmelCase=[0.0, 0.0, 0.0]       ,     _UpperCAmelCase=0.02       ,     _UpperCAmelCase=1e-1_2       ,     _UpperCAmelCase=True       ,     _UpperCAmelCase=True       ,     _UpperCAmelCase=2       ,     ):
       lowercase__:  int   = parent
       lowercase__:  Tuple   = batch_size
       lowercase__:  Optional[int]   = image_size
       lowercase__:  Optional[Any]   = patch_sizes
       lowercase__:  int   = patch_stride
       lowercase__:  Optional[int]   = patch_padding
       lowercase__:  Tuple   = is_training
       lowercase__:  Union[str, Any]   = use_labels
       lowercase__:  Optional[Any]   = num_labels
       lowercase__:  Any   = num_channels
       lowercase__:  Tuple   = embed_dim
       lowercase__:  int   = num_heads
       lowercase__:  Optional[Any]   = stride_kv
       lowercase__:  List[str]   = depth
       lowercase__:  Dict   = cls_token
       lowercase__:  Dict   = attention_drop_rate
       lowercase__:  List[Any]   = initializer_range
       lowercase__:  Optional[int]   = layer_norm_eps






    def _snake_case     (  self				):
       lowercase__:  str   = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

       lowercase__:  List[Any]   = None
       if self.use_labels:
          lowercase__:  List[str]   = ids_tensor([self.batch_size]       ,     self.num_labels				)

       lowercase__:  int   = self.get_config()
       return config, pixel_values, labels






    def _snake_case     (  self				):
       return CvtConfig(
           image_size=self.image_size       ,     num_labels=self.num_labels       ,     num_channels=self.num_channels       ,     embed_dim=self.embed_dim       ,     num_heads=self.num_heads       ,     patch_sizes=self.patch_sizes       ,     patch_padding=self.patch_padding       ,     patch_stride=self.patch_stride       ,     stride_kv=self.stride_kv       ,     depth=self.depth       ,     cls_token=self.cls_token       ,     attention_drop_rate=self.attention_drop_rate       ,     initializer_range=self.initializer_range       ,     )






    def _snake_case     (  self       ,     _UpperCAmelCase       ,     _UpperCAmelCase       ,     _UpperCAmelCase				):
       lowercase__:  str   = CvtModel(config=_UpperCAmelCase				)
       model.to(_UpperCAmelCase				)
       model.eval()
       lowercase__:  Optional[int]   = model(_UpperCAmelCase				)
       lowercase__:  str   = (self.image_size, self.image_size)
       lowercase__:  Any   = image_size[0], image_size[1]
       for i in range(len(self.depth				)				):
          lowercase__:  str   = floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1				)
          lowercase__:  str   = floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1				)
       self.parent.assertEqual(result.last_hidden_state.shape       ,     (self.batch_size, self.embed_dim[-1], height, width)				)






    def _snake_case     (  self       ,     _UpperCAmelCase       ,     _UpperCAmelCase       ,     _UpperCAmelCase				):
       lowercase__:  List[Any]   = self.num_labels
       lowercase__:  str   = CvtForImageClassification(_UpperCAmelCase				)
       model.to(_UpperCAmelCase				)
       model.eval()
       lowercase__:  int   = model(_UpperCAmelCase       ,     labels=_UpperCAmelCase				)
       self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.num_labels)				)






    def _snake_case     (  self				):
       lowercase__:  int   = self.prepare_config_and_inputs()
       lowercase__:  Optional[Any]   = config_and_inputs
       lowercase__:  Tuple   = {'''pixel_values''': pixel_values}
       return config, inputs_dict



@require_torch
class        UpperCAmelCase		(__A       ,__A       ,unittest.TestCase    ):



    """simple docstring"""

    _UpperCAmelCase      :Optional[Any]		       =					(CvtModel, CvtForImageClassification) if is_torch_available() else ()
    _UpperCAmelCase      :List[str]		       =					(
        {"feature-extraction": CvtModel, "image-classification": CvtForImageClassification}
        if is_torch_available()
        else {}
    )

    _UpperCAmelCase      :List[Any]		       =					False
    _UpperCAmelCase      :Optional[Any]		       =					False
    _UpperCAmelCase      :Dict		       =					False
    _UpperCAmelCase      :Tuple		       =					False
    _UpperCAmelCase      :Tuple		       =					False






    def _snake_case     (  self				):
       lowercase__:  int   = CvtModelTester(self				)
       lowercase__:  str   = ConfigTester(self       ,     config_class=_UpperCAmelCase       ,     has_text_modality=_UpperCAmelCase       ,     hidden_size=37				)






    def _snake_case     (  self				):
       self.create_and_test_config_common_properties()
       self.config_tester.create_and_test_config_to_json_string()
       self.config_tester.create_and_test_config_to_json_file()
       self.config_tester.create_and_test_config_from_and_save_pretrained()
       self.config_tester.create_and_test_config_with_num_labels()
       self.config_tester.check_config_can_be_init_without_params()
       self.config_tester.check_config_arguments_init()






    def _snake_case     (  self				):
       return






    @unittest.skip(reason='''Cvt does not output attentions'''				)
    def _snake_case     (  self				):
       pass






    @unittest.skip(reason='''Cvt does not use inputs_embeds'''				)
    def _snake_case     (  self				):
       pass






    @unittest.skip(reason='''Cvt does not support input and output embeddings'''				)
    def _snake_case     (  self				):
       pass






    def _snake_case     (  self				):
       lowercase__:  int   = self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
          lowercase__:  Tuple   = model_class(_UpperCAmelCase				)
          lowercase__:  List[Any]   = inspect.signature(model.forward				)
          # signature.parameters is an OrderedDict => so arg_names order is deterministic
          lowercase__:  Tuple   = [*signature.parameters.keys()]

          lowercase__:  str   = ['''pixel_values''']
          self.assertListEqual(arg_names[:1]       ,     _UpperCAmelCase				)






    def _snake_case     (  self				):
       lowercase__:  str   = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_UpperCAmelCase				)






    def _snake_case     (  self				):

       def check_hidden_states_output(_UpperCAmelCase       ,     _UpperCAmelCase       ,     _UpperCAmelCase				):
          lowercase__:  List[str]   = model_class(_UpperCAmelCase				)
          model.to(_UpperCAmelCase				)
          model.eval()

          with torch.no_grad():
             lowercase__:  Tuple   = model(**self._prepare_for_class(_UpperCAmelCase       ,     _UpperCAmelCase				)				)

          lowercase__:  List[str]   = outputs.hidden_states

          lowercase__:  str   = len(self.model_tester.depth				)
          self.assertEqual(len(_UpperCAmelCase				)       ,     _UpperCAmelCase				)

          # verify the first hidden states (first block)
          self.assertListEqual(
              list(hidden_states[0].shape[-3:]				)       ,     [
                  self.model_tester.embed_dim[0],
                  self.model_tester.image_size // 4,
                  self.model_tester.image_size // 4,
              ]       ,     )

       lowercase__:  str   = self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
          lowercase__:  Dict   = True
          check_hidden_states_output(_UpperCAmelCase       ,     _UpperCAmelCase       ,     _UpperCAmelCase				)

          # check that output_hidden_states also work using config
          del inputs_dict["output_hidden_states"]
          lowercase__:  str   = True

          check_hidden_states_output(_UpperCAmelCase       ,     _UpperCAmelCase       ,     _UpperCAmelCase				)






    def _snake_case     (  self				):
       lowercase__:  Union[str, Any]   = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase				)






    @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''				)
    def _snake_case     (  self				):
       pass






    @slow
    def _snake_case     (  self				):

       for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowercase__:  List[str]   = CvtModel.from_pretrained(_UpperCAmelCase				)
          self.assertIsNotNone(_UpperCAmelCase				)



def SCREAMING_SNAKE_CASE__     (  )    ->    List[str]:
   lowercase__:  Tuple   = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					)
   return image




@require_torch
@require_vision
class        UpperCAmelCase		(unittest.TestCase    ):



    """simple docstring"""

    @cached_property
    def _snake_case     (  self				):
       return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]				)






    @slow
    def _snake_case     (  self				):
       lowercase__:  Any   = CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]				).to(_UpperCAmelCase				)

       lowercase__:  Tuple   = self.default_image_processor
       lowercase__:  str   = prepare_img()
       lowercase__:  List[str]   = image_processor(images=_UpperCAmelCase       ,     return_tensors='''pt'''				).to(_UpperCAmelCase				)

       # forward pass
       with torch.no_grad():
          lowercase__:  Optional[Any]   = model(**_UpperCAmelCase				)

       # verify the logits
       lowercase__:  List[str]   = torch.Size((1, 1000)				)
       self.assertEqual(outputs.logits.shape       ,     _UpperCAmelCase				)

       lowercase__:  Optional[int]   = torch.tensor([0.9_285, 0.9_015, -0.3_150]				).to(_UpperCAmelCase				)

       self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,     _UpperCAmelCase       ,     atol=1e-4				)				)








'''simple docstring'''



from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


SCREAMING_SNAKE_CASE							=						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE							=						{
    'google/bit-50': 'https://huggingface.co/google/bit-50/resolve/main/config.json',
}
class UpperCAmelCase_       (     __A					,							__A					):




      """simple docstring"""




      UpperCamelCase_             =      '''bit'''
      UpperCamelCase_             =      ['''preactivation''', '''bottleneck''']
      UpperCamelCase_             =      ['''SAME''', '''VALID''']






      def __init__(			self      :		Any  ,   UpperCAmelCase      :		Optional[int]=3  ,   UpperCAmelCase      :		Any=64  ,   UpperCAmelCase      :		List[str]=[256, 512, 1024, 2048]  ,   UpperCAmelCase      :		Dict=[3, 4, 6, 3]  ,   UpperCAmelCase      :		List[Any]="preactivation"  ,   UpperCAmelCase      :		Tuple="relu"  ,   UpperCAmelCase      :		Tuple=None  ,   UpperCAmelCase      :		Tuple=32  ,   UpperCAmelCase      :		Optional[int]=0.0  ,   UpperCAmelCase      :		Union[str, Any]=False  ,   UpperCAmelCase      :		Dict=32  ,   UpperCAmelCase      :		Any=1  ,   UpperCAmelCase      :		str=None  ,   UpperCAmelCase      :		Union[str, Any]=None  ,   **UpperCAmelCase      :		str  ,   )	->	Dict:


       '''simple docstring'''

       super().__init__(**UpperCAmelCase		)
       if layer_type not in self.layer_types:
        raise ValueError(f'layer_type={layer_type} is not one of {",".join(self.layer_types		)}'		)
       if global_padding is not None:
        if global_padding.upper() in self.supported_padding:
         lowercase       : Any						=global_padding.upper()
        else:
         raise ValueError(f'Padding strategy {global_padding} not supported'		)
       lowercase       : List[Any]						=num_channels
       lowercase       : List[str]						=embedding_size
       lowercase       : Union[str, Any]						=hidden_sizes
       lowercase       : Union[str, Any]						=depths
       lowercase       : Optional[int]						=layer_type
       lowercase       : Optional[Any]						=hidden_act
       lowercase       : List[str]						=global_padding
       lowercase       : Dict						=num_groups
       lowercase       : Optional[Any]						=drop_path_rate
       lowercase       : Union[str, Any]						=embedding_dynamic_padding
       lowercase       : Any						=output_stride
       lowercase       : Union[str, Any]						=width_factor

       lowercase       : Union[str, Any]						=['''stem'''] + [f'stage{idx}' for idx in range(1  ,   len(UpperCAmelCase		) + 1		)]
       lowercase	,		lowercase       : List[str]						=get_aligned_output_features_output_indices(
           out_features=UpperCAmelCase  ,   out_indices=UpperCAmelCase  ,   stage_names=self.stage_names		)



