





'''simple docstring'''


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_   :





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





        """simple docstring"""




        lowercase_			:Any    			= parent
        lowercase_			:str    			= batch_size
        lowercase_			:Any    			= image_size
        lowercase_			:int    			= patch_size
        lowercase_			:List[str]    			= num_channels
        lowercase_			:Tuple    			= is_training
        lowercase_			:str    			= use_labels
        lowercase_			:Optional[int]    			= hidden_size
        lowercase_			:List[Any]    			= num_hidden_layers
        lowercase_			:List[Any]    			= num_attention_heads
        lowercase_			:List[str]    			= intermediate_size
        lowercase_			:Optional[Any]    			= hidden_act
        lowercase_			:List[str]    			= hidden_dropout_prob
        lowercase_			:Optional[Any]    			= attention_probs_dropout_prob
        lowercase_			:Optional[int]    			= type_sequence_label_size
        lowercase_			:Tuple    			= initializer_range
        lowercase_			:Dict    			= scope
        lowercase_			:Optional[Any]    			= 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
        lowercase_			:str    			= (self.image_size // 32) ** 2
        lowercase_			:str    			= num_patches + 1





     def 							lowercase__      ( self		:					Optional[Any]						):





        """simple docstring"""




        lowercase_			:str    			= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

        lowercase_			:List[str]    			= None
        if self.use_labels:
           lowercase_			:Dict    			= ids_tensor([self.batch_size]	,      self.type_sequence_label_size						)

        lowercase_			:Any    			= self.get_config()

        return config, pixel_values, labels





     def 							lowercase__      ( self		:					Tuple						):





        """simple docstring"""




        lowercase_			:List[str]    			= {
            "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=__a	,      initializer_range=self.initializer_range	,      backbone_featmap_shape=self.backbone_featmap_shape	,      backbone_config=__a	,      )





     def 							lowercase__      ( self		:					Dict	,      lowercase		:					Union[str, Any]	,      lowercase		:					Union[str, Any]	,      lowercase		:					Tuple						):





        """simple docstring"""




        lowercase_			:Tuple    			= ViTHybridModel(config=__a						)
        model.to(__a						)
        model.eval()
        lowercase_			:Optional[int]    			= model(__a						)
        self.parent.assertEqual(result.last_hidden_state.shape	,      (self.batch_size, self.seq_length, self.hidden_size)						)





     def 							lowercase__      ( self		:					Optional[int]	,      lowercase		:					List[str]	,      lowercase		:					Tuple	,      lowercase		:					List[str]						):





        """simple docstring"""




        lowercase_			:Optional[int]    			= self.type_sequence_label_size
        lowercase_			:int    			= ViTHybridForImageClassification(__a						)
        model.to(__a						)
        model.eval()
        lowercase_			:Any    			= model(__a	,      labels=__a						)
        self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.type_sequence_label_size)						)







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





        """simple docstring"""




        lowercase_			:int    			= self.prepare_config_and_inputs()
        lowercase_			,							lowercase_			,							lowercase_			:Union[str, Any]    			= config_and_inputs
        lowercase_			:Tuple    			= {"pixel_values": pixel_values}
        return config, inputs_dict







@require_torch
class       a_   (				_lowerCAmelCase	,						_lowerCAmelCase	,						unittest.TestCase							):
     __A      =			(ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
     __A      =			(
         {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification}
         if is_torch_available()
         else {}
     )
     __A      =			False
     __A      =			False
     __A      =			False





     def 							lowercase__      ( self		:					Optional[Any]						):





        """simple docstring"""




        lowercase_			:List[Any]    			= ViTHybridModelTester(self						)
        lowercase_			:Optional[Any]    			= ConfigTester(self	,      config_class=__a	,      has_text_modality=__a	,      hidden_size=37						)





     def 							lowercase__      ( self		:					List[str]						):





        """simple docstring"""




        self.config_tester.run_common_tests()





     @unittest.skip(reason="ViT does not use inputs_embeds"						)
     def 							lowercase__      ( self		:					List[str]						):





        """simple docstring"""




        pass





     def 							lowercase__      ( self		:					str						):





        """simple docstring"""




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

        for model_class in self.all_model_classes:
           lowercase_			:Union[str, Any]    			= model_class(__a						)
           self.assertIsInstance(model.get_input_embeddings()	,      (nn.Module)						)
           lowercase_			:Union[str, Any]    			= model.get_output_embeddings()
           self.assertTrue(x is None or isinstance(__a	,      nn.Linear						)						)





     def 							lowercase__      ( self		:					Any						):





        """simple docstring"""




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

        for model_class in self.all_model_classes:
           lowercase_			:int    			= model_class(__a						)
           lowercase_			:Any    			= inspect.signature(model.forward						)
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           lowercase_			:Union[str, Any]    			= [*signature.parameters.keys()]

           lowercase_			:int    			= ["pixel_values"]
           self.assertListEqual(arg_names[:1]	,      __a						)





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





        """simple docstring"""




        lowercase_			:List[Any]    			= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__a						)





     def 							lowercase__      ( self		:					int						):





        """simple docstring"""




        lowercase_			:Union[str, Any]    			= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__a						)





     def 							lowercase__      ( self		:					Dict						):





        """simple docstring"""




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

        lowercase_			:Optional[Any]    			= _config_zero_init(__a						)
        for model_class in self.all_model_classes:
           lowercase_			:Union[str, Any]    			= model_class(config=__a						)
           # Skip the check for the backbone
           for name, module in model.named_modules():
              if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
                 lowercase_			:Tuple    			= [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 							lowercase__      ( self		:					Optional[Any]						):





        """simple docstring"""




        for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase_			:Dict    			= ViTHybridModel.from_pretrained(__a						)
           self.assertIsNotNone(__a						)







def 							UpperCAmelCase_  (						):
   lowercase_			:Optional[int]    			= Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
   return image


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





     @cached_property
     def 							lowercase__      ( self		:					List[str]						):





        """simple docstring"""




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





     @slow
     def 							lowercase__      ( self		:					Tuple						):





        """simple docstring"""




        lowercase_			:List[str]    			= ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]						).to(
            __a						)

        lowercase_			:Any    			= self.default_image_processor
        lowercase_			:List[Any]    			= prepare_img()
        lowercase_			:Optional[Any]    			= image_processor(images=__a	,      return_tensors="pt"						).to(__a						)

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

        # verify the logits
        lowercase_			:Tuple    			= torch.Size((1, 1_000)						)
        self.assertEqual(outputs.logits.shape	,      __a						)

        lowercase_			:str    			= torch.tensor([-1.90_90, -0.49_93, -0.23_89]						).to(__a						)

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







     @slow
     @require_accelerate
     def 							lowercase__      ( self		:					Any						):





        """simple docstring"""




        lowercase_			:str    			= ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384"						)
        lowercase_			:Union[str, Any]    			= ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384"	,      device_map="auto"						)

        lowercase_			:Any    			= prepare_img()

        lowercase_			:Tuple    			= image_processor(images=__a	,      return_tensors="pt"						)
        lowercase_			:List[Any]    			= model(**__a						)
        lowercase_			:List[Any]    			= outputs.logits
        # model predicts one of the 1000 ImageNet classes
        lowercase_			:List[str]    			= logits.argmax(-1						).item()

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




"""simple docstring"""





from sklearn.metrics import matthews_corrcoef

import datasets


_a														=		"""
Compute the Matthews correlation coefficient (MCC)

The Matthews correlation coefficient is used in machine learning as a
measure of the quality of binary and multiclass classifications. It takes
into account true and false positives and negatives and is generally
regarded as a balanced measure which can be used even if the classes are of
very different sizes. The MCC is in essence a correlation coefficient value
between -1 and +1. A coefficient of +1 represents a perfect prediction, 0
an average random prediction and -1 an inverse prediction.  The statistic
is also known as the phi coefficient. [source: Wikipedia]
"""

_a														=		"""
Args:
    predictions (list of int): Predicted labels, as returned by a model.
    references (list of int): Ground truth labels.
    sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.
Returns:
    matthews_correlation (dict containing float): Matthews correlation.
Examples:
    Example 1, a basic example with only predictions and references as inputs:
        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3])
        >>> print(round(results['matthews_correlation'], 2))
        0.54

    Example 2, the same example as above, but also including sample weights:
        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3],
        ...                                     sample_weight=[0.5, 3, 1, 1, 1, 2])
        >>> print(round(results['matthews_correlation'], 2))
        0.1

    Example 3, the same example as above, but with sample weights that cause a negative correlation:
        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3],
        ...                                     sample_weight=[0.5, 1, 0, 0, 0, 1])
        >>> print(round(results['matthews_correlation'], 2))
        -0.25
"""

_a														=		"""\
@article{scikit-learn,
  title={Scikit-learn: Machine Learning in {P}ython},
  author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
  journal={Journal of Machine Learning Research},
  volume={12},
  pages={2825--2830},
  year={2011}
}
"""






@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION      ,     _KWARGS_DESCRIPTION	)
class     _UpperCAmelCase(    datasets.Metric	):



		def 						UpperCAmelCase							(					self)       ->       List[Any]:






							'''simple docstring'''
							return datasets.MetricInfo(
							    description=_DESCRIPTION				,		citation=_CITATION				,		inputs_description=_KWARGS_DESCRIPTION				,		features=datasets.Features(
							        {
							            '''predictions''': datasets.Value('''int32'''),
							            '''references''': datasets.Value('''int32'''),
							        })				,		reference_urls=[
							        '''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html'''
							    ]				,		)




		def 						UpperCAmelCase							(					self				,		__a				,		__a				,		__a=None)       ->       Dict:






							'''simple docstring'''
							return {
							    "matthews_correlation": float(matthews_corrcoef(__a				,		__a				,		sample_weight=__a)),
							}






