





'''simple docstring'''






from scipy.stats import pearsonr

import datasets


lowerCAmelCase									=   """
Pearson correlation coefficient and p-value for testing non-correlation.
The Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.
The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets.
"""


lowerCAmelCase									=   """
Args:
    predictions (`list` of `int`): Predicted class labels, as returned by a model.
    references (`list` of `int`): Ground truth labels.
    return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`.

Returns:
    pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation.
    p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities.

Examples:

    Example 1-A simple example using only predictions and references.
        >>> pearsonr_metric = datasets.load_metric(\"pearsonr\")
        >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5])
        >>> print(round(results[\'pearsonr\'], 2))
        -0.74

    Example 2-The same as Example 1, but that also returns the `p-value`.
        >>> pearsonr_metric = datasets.load_metric(\"pearsonr\")
        >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True)
        >>> print(sorted(list(results.keys())))
        [\'p-value\', \'pearsonr\']
        >>> print(round(results[\'pearsonr\'], 2))
        -0.74
        >>> print(round(results[\'p-value\'], 2))
        0.15
"""


lowerCAmelCase									=   """
@article{2020SciPy-NMeth,
author  = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and
      Haberland, Matt and Reddy, Tyler and Cournapeau, David and
      Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and
      Bright, Jonathan and {van der Walt}, St{\'e}fan J. and
      Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and
      Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and
      Kern, Robert and Larson, Eric and Carey, C J and
      Polat, Ilhan and Feng, Yu and Moore, Eric W. and
      {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and
      Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and
      Harris, Charles R. and Archibald, Anne M. and
      Ribeiro, Antonio H. and Pedregosa, Fabian and
      {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},
title   = {{{SciPy} 1.0: Fundamental Algorithms for Scientific
      Computing in Python}},
journal = {Nature Methods},
year    = {2020},
volume  = {17},
pages   = {261--272},
adsurl  = {https://rdcu.be/b08Wh},
doi = {10.1038/s41592-019-0686-2},
}
"""
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION   , _KWARGS_DESCRIPTION							)
class 		lowerCamelCase	(   datasets.Metric							):



	def 	_lowerCamelCase     ( self						):
			return datasets.MetricInfo(
			    description=_DESCRIPTION		,   citation=_CITATION		,   inputs_description=_KWARGS_DESCRIPTION		,   features=datasets.Features(
			        {
			            "predictions": datasets.Value("float"						),
			            "references": datasets.Value("float"						),
			        }						)		,   reference_urls=["https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html"]		,   )





	def 	_lowerCamelCase     ( self		,   a_		,   a_		,   a_=False						):


			if return_pvalue:
					lowerCAmelCase  :     Dict       						= pearsonr(a_		,   a_						)
					return {"pearsonr": results[0], "p-value": results[1]}
			else:
					return {"pearsonr": float(pearsonr(a_		,   a_						)[0]						)}









'''simple docstring'''







import inspect
import unittest
import warnings

from transformers import DeiTConfig
from transformers.models.auto import get_values
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch
    from torch import nn

    from transformers import (
        MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
        MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
        MODEL_MAPPING,
        DeiTForImageClassification,
        DeiTForImageClassificationWithTeacher,
        DeiTForMaskedImageModeling,
        DeiTModel,
    )
    from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import DeiTImageProcessor

class 	_snake_case:






 def __init__(self       :  List[Any]    ,		a       :  str    ,		a       :  Any=13    ,		a       :  Optional[Any]=30    ,		a       :  Union[str, Any]=2    ,		a       :  List[str]=3    ,		a       :  List[str]=True    ,		a       :  List[Any]=True    ,		a       :  Tuple=32    ,		a       :  Optional[Any]=5    ,		a       :  str=4    ,		a       :  List[str]=37    ,		a       :  List[str]="gelu"    ,		a       :  int=0.1    ,		a       :  int=0.1    ,		a       :  str=10    ,		a       :  Tuple=0.02    ,		a       :  Union[str, Any]=3    ,		a       :  List[str]=None    ,		a       :  Any=2    ,		)       ->   Optional[int]:






    """simple docstring"""


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

    # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
    A__       =     (image_size // patch_size) ** 2
    A__       =     num_patches + 2






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






    """simple docstring"""


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

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

    A__       =     self.get_config()

    return config, pixel_values, labels






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






    """simple docstring"""


    return DeiTConfig(
        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    ,		encoder_stride=self.encoder_stride    ,		)






 def 						_UpperCamelCase (self       :  Optional[int]    ,		a       :  Tuple    ,		a       :  Dict    ,		a       :  Optional[Any]    )       ->   Optional[Any]:






    """simple docstring"""


    A__       =     DeiTModel(config=a    )
    model.to(a    )
    model.eval()
    A__       =     model(a    )
    self.parent.assertEqual(result.last_hidden_state.shape    ,		(self.batch_size, self.seq_length, self.hidden_size)    )






 def 						_UpperCamelCase (self       :  Optional[int]    ,		a       :  Any    ,		a       :  Optional[Any]    ,		a       :  Optional[int]    )       ->   Dict:






    """simple docstring"""


    A__       =     DeiTForMaskedImageModeling(config=a    )
    model.to(a    )
    model.eval()
    A__       =     model(a    )
    self.parent.assertEqual(
        result.reconstruction.shape    ,		(self.batch_size, self.num_channels, self.image_size, self.image_size)    )

    # test greyscale images
    A__       =     1
    A__       =     DeiTForMaskedImageModeling(a    )
    model.to(a    )
    model.eval()

    A__       =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
    A__       =     model(a    )
    self.parent.assertEqual(result.reconstruction.shape    ,		(self.batch_size, 1, self.image_size, self.image_size)    )






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






    """simple docstring"""


    A__       =     self.type_sequence_label_size
    A__       =     DeiTForImageClassification(a    )
    model.to(a    )
    model.eval()
    A__       =     model(a    ,		labels=a    )
    self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.type_sequence_label_size)    )

    # test greyscale images
    A__       =     1
    A__       =     DeiTForImageClassification(a    )
    model.to(a    )
    model.eval()

    A__       =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
    A__       =     model(a    ,		labels=a    )
    self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.type_sequence_label_size)    )






 def 						_UpperCamelCase (self       :  List[Any]    )       ->   Optional[int]:






    """simple docstring"""


    A__       =     self.prepare_config_and_inputs()
    (
        (
        A__
    )  ,			(
        A__
    )  ,			(
        A__
    )  ,			
    )       =     config_and_inputs
    A__       =     {'pixel_values': pixel_values}
    return config, inputs_dict




@require_torch
class 	_snake_case(			UpperCAmelCase       , UpperCAmelCase       , unittest.TestCase      ):
 __snake_case:							Optional[Any]						=					(
     (
         DeiTModel,
         DeiTForImageClassification,
         DeiTForImageClassificationWithTeacher,
         DeiTForMaskedImageModeling,
     )
     if is_torch_available()
     else ()
 )
 __snake_case:							int						=					(
     {
         '''feature-extraction''': DeiTModel,
         '''image-classification''': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
     }
     if is_torch_available()
     else {}
 )

 __snake_case:							Any						=					False
 __snake_case:							Any						=					False
 __snake_case:							Any						=					False






 def 						_UpperCamelCase (self       :  Any    )       ->   int:






    """simple docstring"""


    A__       =     DeiTModelTester(self    )
    A__       =     ConfigTester(self    ,		config_class=a    ,		has_text_modality=a    ,		hidden_size=37    )






 def 						_UpperCamelCase (self       :  Dict    )       ->   Tuple:






    """simple docstring"""


    self.config_tester.run_common_tests()






 @unittest.skip(reason='DeiT does not use inputs_embeds'    )
 def 						_UpperCamelCase (self       :  List[str]    )       ->   List[str]:






    """simple docstring"""


    pass






 def 						_UpperCamelCase (self       :  Optional[Any]    )       ->   Tuple:






    """simple docstring"""


    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()

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






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






    """simple docstring"""


    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()

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

       A__       =     ['pixel_values']
       self.assertListEqual(arg_names[:1]    ,		a    )






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






    """simple docstring"""


    A__       =     self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*a    )






 def 						_UpperCamelCase (self       :  Dict    )       ->   Any:






    """simple docstring"""


    A__       =     self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_image_modeling(*a    )






 def 						_UpperCamelCase (self       :  List[Any]    )       ->   Optional[Any]:






    """simple docstring"""


    A__       =     self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*a    )






 def 						_UpperCamelCase (self       :  Optional[int]    ,		a       :  int    ,		a       :  Union[str, Any]    ,		a       :  List[Any]=False    )       ->   Optional[int]:






    """simple docstring"""


    A__       =     super()._prepare_for_class(a    ,		a    ,		return_labels=a    )

    if return_labels:
       if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
          del inputs_dict["labels"]

    return inputs_dict






 def 						_UpperCamelCase (self       :  Any    )       ->   Tuple:






    """simple docstring"""


    if not self.model_tester.is_training:
       return

    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()
    A__       =     True

    for model_class in self.all_model_classes:
       # DeiTForImageClassificationWithTeacher supports inference-only
       if (
           model_class in get_values(a    )
           or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
       ):
          continue
       A__       =     model_class(a    )
       model.to(a    )
       model.train()
       A__       =     self._prepare_for_class(a    ,		a    ,		return_labels=a    )
       A__       =     model(**a    ).loss
       loss.backward()






 def 						_UpperCamelCase (self       :  Optional[Any]    )       ->   int:






    """simple docstring"""


    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()
    if not self.model_tester.is_training:
       return

    A__       =     False
    A__       =     True

    for model_class in self.all_model_classes:
       if model_class in get_values(a    ) or not model_class.supports_gradient_checkpointing:
          continue
       # DeiTForImageClassificationWithTeacher supports inference-only
       if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
          continue
       A__       =     model_class(a    )
       model.gradient_checkpointing_enable()
       model.to(a    )
       model.train()
       A__       =     self._prepare_for_class(a    ,		a    ,		return_labels=a    )
       A__       =     model(**a    ).loss
       loss.backward()






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






    """simple docstring"""


    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()

    A__       =     [
        {'title': 'multi_label_classification', 'num_labels': 2, 'dtype': torch.float},
        {'title': 'single_label_classification', 'num_labels': 1, 'dtype': torch.long},
        {'title': 'regression', 'num_labels': 1, 'dtype': torch.float},
    ]

    for model_class in self.all_model_classes:
       if (
           model_class
           not in [
               *get_values(a    ),
               *get_values(a    ),
           ]
           or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
       ):
          continue

       for problem_type in problem_types:
          with self.subTest(msg=f"""Testing {model_class} with {problem_type["title"]}"""    ):
             A__       =     problem_type['title']
             A__       =     problem_type['num_labels']

             A__       =     model_class(a    )
             model.to(a    )
             model.train()

             A__       =     self._prepare_for_class(a    ,		a    ,		return_labels=a    )

             if problem_type["num_labels"] > 1:
                A__       =     inputs['labels'].unsqueeze(1    ).repeat(1    ,		problem_type['num_labels']    )

             A__       =     inputs['labels'].to(problem_type['dtype']    )

             # This tests that we do not trigger the warning form PyTorch "Using a target size that is different
             # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
             # they have the same size." which is a symptom something in wrong for the regression problem.
             # See https://github.com/huggingface/transformers/issues/11780
             with warnings.catch_warnings(record=a    ) as warning_list:
                A__       =     model(**a    ).loss
             for w in warning_list:
                if "Using a target size that is different to the input size" in str(w.message    ):
                   raise ValueError(
                       f"""Something is going wrong in the regression problem: intercepted {w.message}"""    )

             loss.backward()






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






    """simple docstring"""


    for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       A__       =     DeiTModel.from_pretrained(a    )
       self.assertIsNotNone(a    )







def 						_A      ( ):






   '''simple docstring'''







   A__       =     Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
   return image







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






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






    """simple docstring"""


    return (
        DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224'    )
        if is_vision_available()
        else None
    )






 @slow
 def 						_UpperCamelCase (self       :  List[str]    )       ->   Optional[Any]:






    """simple docstring"""


    A__       =     DeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224'    ).to(
        a    )

    A__       =     self.default_image_processor
    A__       =     prepare_img()
    A__       =     image_processor(images=a    ,		return_tensors='pt'    ).to(a    )

    # forward pass
    with torch.no_grad():
       A__       =     model(**a    )

    # verify the logits
    A__       =     torch.Size((1, 10_00)    )
    self.assertEqual(outputs.logits.shape    ,		a    )

    A__       =     torch.tensor([-1.0266, 0.1912, -1.2861]    ).to(a    )

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






 @slow
 @require_accelerate
 @require_torch_gpu
 def 						_UpperCamelCase (self       :  Tuple    )       ->   str:






    """simple docstring"""


    A__       =     DeiTModel.from_pretrained(
        'facebook/deit-base-distilled-patch16-224'    ,		torch_dtype=torch.floataa    ,		device_map='auto'    )
    A__       =     self.default_image_processor

    A__       =     prepare_img()
    A__       =     image_processor(images=a    ,		return_tensors='pt'    )
    A__       =     inputs.pixel_values.to(a    )

    # forward pass to make sure inference works in fp16
    with torch.no_grad():
       A__       =     model(a    )


