
import inspect
import unittest

from transformers import ViTHybridConfig
from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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


if is_torch_available():
    import torch
    from torch import nn

    from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel
    from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image
class     a_			:






 '''simple docstring'''






 def __init__(      self :  int      ,   lowercase__ :  int      ,   lowercase__ :  List[str]=13      ,   lowercase__ :  Dict=64      ,   lowercase__ :  Tuple=2      ,   lowercase__ :  Union[str, Any]=3      ,   lowercase__ :  Optional[int]=True      ,   lowercase__ :  List[str]=True      ,   lowercase__ :  Union[str, Any]=32      ,   lowercase__ :  int=5      ,   lowercase__ :  Optional[Any]=4      ,   lowercase__ :  str=37      ,   lowercase__ :  int="gelu"      ,   lowercase__ :  Optional[int]=0.1      ,   lowercase__ :  Optional[int]=0.1      ,   lowercase__ :  str=10      ,   lowercase__ :  Dict=0.02      ,   lowercase__ :  Dict=[1, 16, 4, 4]      ,   lowercase__ :  List[str]=None      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = parent
        lowerCAmelCase__					     = batch_size
        lowerCAmelCase__					     = image_size
        lowerCAmelCase__					     = patch_size
        lowerCAmelCase__					     = num_channels
        lowerCAmelCase__					     = is_training
        lowerCAmelCase__					     = use_labels
        lowerCAmelCase__					     = hidden_size
        lowerCAmelCase__					     = num_hidden_layers
        lowerCAmelCase__					     = num_attention_heads
        lowerCAmelCase__					     = intermediate_size
        lowerCAmelCase__					     = hidden_act
        lowerCAmelCase__					     = hidden_dropout_prob
        lowerCAmelCase__					     = attention_probs_dropout_prob
        lowerCAmelCase__					     = type_sequence_label_size
        lowerCAmelCase__					     = initializer_range
        lowerCAmelCase__					     = scope
        lowerCAmelCase__					     = backbone_featmap_shape

        # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
        # the number of patches is based on the feature map of the backbone, which by default uses an output stride
        # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size
        lowerCAmelCase__					     = (self.image_size // 32) ** 2
        lowerCAmelCase__					     = num_patches + 1




 def 		__snake_case						(      self :  Optional[Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

        lowerCAmelCase__					     = None
        if self.use_labels:
               lowerCAmelCase__					     = ids_tensor([self.batch_size]      ,   self.type_sequence_label_size)

        lowerCAmelCase__					     = self.get_config()

        return config, pixel_values, labels




 def 		__snake_case						(      self :  Optional[Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = {
            """global_padding""": """same""",
            """layer_type""": """bottleneck""",
            """depths""": [3, 4, 9],
            """out_features""": ["""stage1""", """stage2""", """stage3"""],
            """embedding_dynamic_padding""": True,
            """hidden_sizes""": [4, 8, 16, 32],
            """num_groups""": 2,
        }

        return ViTHybridConfig(
            image_size=self.image_size      ,   patch_size=self.patch_size      ,   num_channels=self.num_channels      ,   hidden_size=self.hidden_size      ,   num_hidden_layers=self.num_hidden_layers      ,   num_attention_heads=self.num_attention_heads      ,   intermediate_size=self.intermediate_size      ,   hidden_act=self.hidden_act      ,   hidden_dropout_prob=self.hidden_dropout_prob      ,   attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,   is_decoder=_SCREAMING_SNAKE_CASE      ,   initializer_range=self.initializer_range      ,   backbone_featmap_shape=self.backbone_featmap_shape      ,   backbone_config=_SCREAMING_SNAKE_CASE      ,   )




 def 		__snake_case						(      self :  Any      ,   lowercase__ :  Optional[int]      ,   lowercase__ :  Dict      ,   lowercase__ :  str):







        '''simple docstring'''



        lowerCAmelCase__					     = ViTHybridModel(config=_SCREAMING_SNAKE_CASE)
        model.to(_SCREAMING_SNAKE_CASE)
        model.eval()
        lowerCAmelCase__					     = model(_SCREAMING_SNAKE_CASE)
        self.parent.assertEqual(result.last_hidden_state.shape      ,   (self.batch_size, self.seq_length, self.hidden_size))




 def 		__snake_case						(      self :  List[Any]      ,   lowercase__ :  int      ,   lowercase__ :  Union[str, Any]      ,   lowercase__ :  str):







        '''simple docstring'''



        lowerCAmelCase__					     = self.type_sequence_label_size
        lowerCAmelCase__					     = ViTHybridForImageClassification(_SCREAMING_SNAKE_CASE)
        model.to(_SCREAMING_SNAKE_CASE)
        model.eval()
        lowerCAmelCase__					     = model(_SCREAMING_SNAKE_CASE      ,   labels=_SCREAMING_SNAKE_CASE)
        self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.type_sequence_label_size))




 def 		__snake_case						(      self :  Union[str, Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = self.prepare_config_and_inputs()
        lowerCAmelCase__					     = config_and_inputs
        lowerCAmelCase__					     = {"""pixel_values""": pixel_values}
        return config, inputs_dict






@require_torch
class     a_			(						UpperCAmelCase__	,					UpperCAmelCase__	,					unittest.TestCase						):






 '''simple docstring'''






 UpperCAmelCase_     			= (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
 UpperCAmelCase_     			= (
     {'feature-extraction': ViTHybridModel, 'image-classification': ViTHybridForImageClassification}
     if is_torch_available()
     else {}
 )
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False




 def 		__snake_case						(      self :  Optional[int]):







        '''simple docstring'''



        lowerCAmelCase__					     = ViTHybridModelTester(self)
        lowerCAmelCase__					     = ConfigTester(self      ,   config_class=_SCREAMING_SNAKE_CASE      ,   has_text_modality=_SCREAMING_SNAKE_CASE      ,   hidden_size=37)




 def 		__snake_case						(      self :  Any):







        '''simple docstring'''



        self.config_tester.run_common_tests()




 @unittest.skip(reason='ViT does not use inputs_embeds')
 def 		__snake_case						(      self :  Union[str, Any]):







        '''simple docstring'''



        pass




 def 		__snake_case						(      self :  int):







        '''simple docstring'''



        lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               lowerCAmelCase__					     = model_class(_SCREAMING_SNAKE_CASE)
               self.assertIsInstance(model.get_input_embeddings()      ,   (nn.Module))
               lowerCAmelCase__					     = model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE      ,   nn.Linear))




 def 		__snake_case						(      self :  Optional[int]):







        '''simple docstring'''



        lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()

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

               lowerCAmelCase__					     = ["""pixel_values"""]
               self.assertListEqual(arg_names[:1]      ,   _SCREAMING_SNAKE_CASE)




 def 		__snake_case						(      self :  Any):







        '''simple docstring'''



        lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE)




 def 		__snake_case						(      self :  Dict):







        '''simple docstring'''



        lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE)




 def 		__snake_case						(      self :  Dict):







        '''simple docstring'''



        lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()

        lowerCAmelCase__					     = _config_zero_init(_SCREAMING_SNAKE_CASE)
        for model_class in self.all_model_classes:
               lowerCAmelCase__					     = model_class(config=_SCREAMING_SNAKE_CASE)
               # Skip the check for the backbone
               for name, module in model.named_modules():
                      if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
                             lowerCAmelCase__					     = [F"""{name}.{key}""" for key in module.state_dict().keys()]
                             break

               for name, param in model.named_parameters():
                      if param.requires_grad:
                             if name in backbone_params:
                                    continue
                             self.assertIn(
                                 ((param.data.mean() * 1e9).round() / 1e9).item()      ,   [0.0, 1.0]      ,   msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""      ,   )




 @slow
 def 		__snake_case						(      self :  Union[str, Any]):







        '''simple docstring'''



        for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowerCAmelCase__					     = ViTHybridModel.from_pretrained(_SCREAMING_SNAKE_CASE)
               self.assertIsNotNone(_SCREAMING_SNAKE_CASE)






def 				__lowerCamelCase		(						):
       lowerCAmelCase__					     = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
       return image



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






 '''simple docstring'''






 @cached_property
 def 		__snake_case						(      self :  Any):







        '''simple docstring'''



        return (
            ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0])
            if is_vision_available()
            else None
        )




 @slow
 def 		__snake_case						(      self :  Any):







        '''simple docstring'''



        lowerCAmelCase__					     = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]).to(
            _SCREAMING_SNAKE_CASE)

        lowerCAmelCase__					     = self.default_image_processor
        lowerCAmelCase__					     = prepare_img()
        lowerCAmelCase__					     = image_processor(images=_SCREAMING_SNAKE_CASE      ,   return_tensors='pt').to(_SCREAMING_SNAKE_CASE)

        # forward pass
        with torch.no_grad():
               lowerCAmelCase__					     = model(**_SCREAMING_SNAKE_CASE)

        # verify the logits
        lowerCAmelCase__					     = torch.Size((1, 1_000))
        self.assertEqual(outputs.logits.shape      ,   _SCREAMING_SNAKE_CASE)

        lowerCAmelCase__					     = torch.tensor([-1.9_090, -0.4_993, -0.2_389]).to(_SCREAMING_SNAKE_CASE)

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




 @slow
 @require_accelerate
 def 		__snake_case						(      self :  str):







        '''simple docstring'''



        lowerCAmelCase__					     = ViTHybridImageProcessor.from_pretrained('google/vit-hybrid-base-bit-384')
        lowerCAmelCase__					     = ViTHybridForImageClassification.from_pretrained('google/vit-hybrid-base-bit-384'      ,   device_map='auto')

        lowerCAmelCase__					     = prepare_img()

        lowerCAmelCase__					     = image_processor(images=_SCREAMING_SNAKE_CASE      ,   return_tensors='pt')
        lowerCAmelCase__					     = model(**_SCREAMING_SNAKE_CASE)
        lowerCAmelCase__					     = outputs.logits
        # model predicts one of the 1000 ImageNet classes
        lowerCAmelCase__					     = logits.argmax(-1).item()

        self.assertTrue(model.config.idalabel[predicted_class_idx]      ,   'tabby, tabby cat')

from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
    import PIL


lowerCAmelCase__     						=			logging.get_logger(__name__)
class     a_			(						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_     			= ['pixel_values']




 def __init__(      self :  Tuple      ,   lowercase__ :  bool = True      ,   lowercase__ :  Dict[str, int] = None      ,   lowercase__ :  PILImageResampling = PILImageResampling.BICUBIC      ,   lowercase__ :  bool = True      ,   lowercase__ :  Union[int, float] = 1 / 255      ,   lowercase__ :  bool = True      ,   lowercase__ :  Optional[Union[float, List[float]]] = None      ,   lowercase__ :  Optional[Union[float, List[float]]] = None      ,   lowercase__ :  bool = True      ,   **lowercase__ :  List[Any]      ,   ):







        '''simple docstring'''



        super().__init__(**lowercase__)
        lowerCAmelCase__					     = size if size is not None else {'height': 384, 'width': 384}
        lowerCAmelCase__					     = get_size_dict(lowercase__      ,   default_to_square=lowercase__)

        lowerCAmelCase__					     = do_resize
        lowerCAmelCase__					     = size
        lowerCAmelCase__					     = resample
        lowerCAmelCase__					     = do_rescale
        lowerCAmelCase__					     = rescale_factor
        lowerCAmelCase__					     = do_normalize
        lowerCAmelCase__					     = image_mean if image_mean is not None else OPENAI_CLIP_MEAN
        lowerCAmelCase__					     = image_std if image_std is not None else OPENAI_CLIP_STD
        lowerCAmelCase__					     = do_convert_rgb




 def 		__snake_case						(      self :  List[str]      ,   lowercase__ :  np.ndarray      ,   lowercase__ :  Dict[str, int]      ,   lowercase__ :  PILImageResampling = PILImageResampling.BICUBIC      ,   lowercase__ :  Optional[Union[str, ChannelDimension]] = None      ,   **lowercase__ :  Dict      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = get_size_dict(lowercase__      ,   default_to_square=lowercase__)
        if "height" not in size or "width" not in size:
               raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}""")
        lowerCAmelCase__					     = (size['height'], size['width'])
        return resize(lowercase__      ,   size=lowercase__      ,   resample=lowercase__      ,   data_format=lowercase__      ,   **lowercase__)




 def 		__snake_case						(      self :  List[str]      ,   lowercase__ :  np.ndarray      ,   lowercase__ :  Union[int, float]      ,   lowercase__ :  Optional[Union[str, ChannelDimension]] = None      ,   **lowercase__ :  Union[str, Any]      ,   ):







        '''simple docstring'''



        return rescale(lowercase__      ,   scale=lowercase__      ,   data_format=lowercase__      ,   **lowercase__)




 def 		__snake_case						(      self :  Optional[Any]      ,   lowercase__ :  np.ndarray      ,   lowercase__ :  Union[float, List[float]]      ,   lowercase__ :  Union[float, List[float]]      ,   lowercase__ :  Optional[Union[str, ChannelDimension]] = None      ,   **lowercase__ :  Any      ,   ):







        '''simple docstring'''



        return normalize(lowercase__      ,   mean=lowercase__      ,   std=lowercase__      ,   data_format=lowercase__      ,   **lowercase__)




 def 		__snake_case						(      self :  Any      ,   lowercase__ :  ImageInput      ,   lowercase__ :  Optional[bool] = None      ,   lowercase__ :  Optional[Dict[str, int]] = None      ,   lowercase__ :  PILImageResampling = None      ,   lowercase__ :  Optional[bool] = None      ,   lowercase__ :  Optional[float] = None      ,   lowercase__ :  Optional[bool] = None      ,   lowercase__ :  Optional[Union[float, List[float]]] = None      ,   lowercase__ :  Optional[Union[float, List[float]]] = None      ,   lowercase__ :  Optional[Union[str, TensorType]] = None      ,   lowercase__ :  bool = None      ,   lowercase__ :  ChannelDimension = ChannelDimension.FIRST      ,   **lowercase__ :  Dict      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = do_resize if do_resize is not None else self.do_resize
        lowerCAmelCase__					     = resample if resample is not None else self.resample
        lowerCAmelCase__					     = do_rescale if do_rescale is not None else self.do_rescale
        lowerCAmelCase__					     = rescale_factor if rescale_factor is not None else self.rescale_factor
        lowerCAmelCase__					     = do_normalize if do_normalize is not None else self.do_normalize
        lowerCAmelCase__					     = image_mean if image_mean is not None else self.image_mean
        lowerCAmelCase__					     = image_std if image_std is not None else self.image_std
        lowerCAmelCase__					     = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

        lowerCAmelCase__					     = size if size is not None else self.size
        lowerCAmelCase__					     = get_size_dict(lowercase__      ,   default_to_square=lowercase__)

        lowerCAmelCase__					     = make_list_of_images(lowercase__)

        if not valid_images(lowercase__):
               raise ValueError(
                   'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
                   'torch.Tensor, tf.Tensor or jax.ndarray.')

        if do_resize and size is None or resample is None:
               raise ValueError('Size and resample must be specified if do_resize is True.')

        if do_rescale and rescale_factor is None:
               raise ValueError('Rescale factor must be specified if do_rescale is True.')

        if do_normalize and (image_mean is None or image_std is None):
               raise ValueError('Image mean and std must be specified if do_normalize is True.')

        # PIL RGBA images are converted to RGB
        if do_convert_rgb:
               lowerCAmelCase__					     = [convert_to_rgb(lowercase__) for image in images]

        # All transformations expect numpy arrays.
        lowerCAmelCase__					     = [to_numpy_array(lowercase__) for image in images]

        if do_resize:
               lowerCAmelCase__					     = [self.resize(image=lowercase__      ,   size=lowercase__      ,   resample=lowercase__) for image in images]

        if do_rescale:
               lowerCAmelCase__					     = [self.rescale(image=lowercase__      ,   scale=lowercase__) for image in images]

        if do_normalize:
               lowerCAmelCase__					     = [self.normalize(image=lowercase__      ,   mean=lowercase__      ,   std=lowercase__) for image in images]

        lowerCAmelCase__					     = [to_channel_dimension_format(lowercase__      ,   lowercase__) for image in images]

        lowerCAmelCase__					     = BatchFeature(data={'pixel_values': images}      ,   tensor_type=lowercase__)

        return encoded_outputs
