






'''simple docstring'''





from scipy.stats import pearsonr, spearmanr
from sklearn.metrics import fa_score, matthews_corrcoef

import datasets


a_  			=			'\\n@inproceedings{wang2019glue,\n  title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding},\n  author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.},\n  note={In the Proceedings of ICLR.},\n  year={2019}\n}\n'

a_  			=			'\\nGLUE, the General Language Understanding Evaluation benchmark\n(https://gluebenchmark.com/) is a collection of resources for training,\nevaluating, and analyzing natural language understanding systems.\n'

a_  			=			'\nCompute GLUE evaluation metric associated to each GLUE dataset.\nArgs:\n    predictions: list of predictions to score.\n        Each translation should be tokenized into a list of tokens.\n    references: list of lists of references for each translation.\n        Each reference should be tokenized into a list of tokens.\nReturns: depending on the GLUE subset, one or several of:\n    "accuracy": Accuracy\n    "f1": F1 score\n    "pearson": Pearson Correlation\n    "spearmanr": Spearman Correlation\n    "matthews_correlation": Matthew Correlation\nExamples:\n\n    >>> glue_metric = datasets.load_metric(\'glue\', \'sst2\')  # \'sst2\' or any of ["mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'accuracy\': 1.0}\n\n    >>> glue_metric = datasets.load_metric(\'glue\', \'mrpc\')  # \'mrpc\' or \'qqp\'\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'accuracy\': 1.0, \'f1\': 1.0}\n\n    >>> glue_metric = datasets.load_metric(\'glue\', \'stsb\')\n    >>> references = [0., 1., 2., 3., 4., 5.]\n    >>> predictions = [0., 1., 2., 3., 4., 5.]\n    >>> results = glue_metric.compute(predictions=predictions, references=references)\n    >>> print({"pearson": round(results["pearson"], 2), "spearmanr": round(results["spearmanr"], 2)})\n    {\'pearson\': 1.0, \'spearmanr\': 1.0}\n\n    >>> glue_metric = datasets.load_metric(\'glue\', \'cola\')\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'matthews_correlation\': 1.0}\n'



def       __UpperCAmelCase					(lowercase__						,lowercase__					)		->					Tuple:

						'''simple docstring'''


						return float((preds == labels).mean()					)



def       __UpperCAmelCase					(lowercase__						,lowercase__					)		->					List[str]:

						'''simple docstring'''


						a_					  = simple_accuracy(snake_case__						,snake_case__					)
						a_					  = float(fa_score(y_true=snake_case__						,y_pred=snake_case__					)					)
						return {
						    "accuracy": acc,
						    "f1": fa,
						}



def       __UpperCAmelCase					(lowercase__						,lowercase__					)		->					Any:

						'''simple docstring'''


						a_					  = float(pearsonr(snake_case__						,snake_case__					)[0]					)
						a_					  = float(spearmanr(snake_case__						,snake_case__					)[0]					)
						return {
						    "pearson": pearson_corr,
						    "spearmanr": spearman_corr,
						}





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



		def _lowerCAmelCase  (     self:     str)	->str:






								'''simple docstring'''


								if self.config_name not in [
								    "sst2",
								    "mnli",
								    "mnli_mismatched",
								    "mnli_matched",
								    "cola",
								    "stsb",
								    "mrpc",
								    "qqp",
								    "qnli",
								    "rte",
								    "wnli",
								    "hans",
								]:
														raise KeyError(
														    "You should supply a configuration name selected in "
														    "[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", "
														    "\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]")
								return datasets.MetricInfo(
								    description=_DESCRIPTION					, citation=_CITATION					, inputs_description=_KWARGS_DESCRIPTION					, features=datasets.Features(
								        {
								            "predictions": datasets.Value("int64" if self.config_name != "stsb" else "float32"),
								            "references": datasets.Value("int64" if self.config_name != "stsb" else "float32"),
								        })					, codebase_urls=[]					, reference_urls=[]					, format="numpy"					, )




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






								'''simple docstring'''


								if self.config_name == "cola":
														return {"matthews_correlation": matthews_corrcoef(a					, a)}
								elif self.config_name == "stsb":
														return pearson_and_spearman(a					, a)
								elif self.config_name in ["mrpc", "qqp"]:
														return acc_and_fa(a					, a)
								elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]:
														return {"accuracy": simple_accuracy(a					, a)}
								else:
														raise KeyError(
														    "You should supply a configuration name selected in "
														    "[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", "
														    "\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]")





'''simple docstring'''




import inspect
import unittest

from transformers import ViTConfig
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 ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
      from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import ViTImageProcessor





class 					A							:





     def __init__(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE=13		,     SCREAMING_SNAKE_CASE=30		,     SCREAMING_SNAKE_CASE=2		,     SCREAMING_SNAKE_CASE=3		,     SCREAMING_SNAKE_CASE=True		,     SCREAMING_SNAKE_CASE=True		,     SCREAMING_SNAKE_CASE=32		,     SCREAMING_SNAKE_CASE=5		,     SCREAMING_SNAKE_CASE=4		,     SCREAMING_SNAKE_CASE=37		,     SCREAMING_SNAKE_CASE="gelu"		,     SCREAMING_SNAKE_CASE=0.1		,     SCREAMING_SNAKE_CASE=0.1		,     SCREAMING_SNAKE_CASE=10		,     SCREAMING_SNAKE_CASE=0.02		,     SCREAMING_SNAKE_CASE=None		,     SCREAMING_SNAKE_CASE=2		,     )					->							Any:






         """simple docstring"""






         A			:  Union[str, Any]   =		parent
         A			:  Optional[Any]   =		batch_size
         A			:  Tuple   =		image_size
         A			:  str   =		patch_size
         A			:  Dict   =		num_channels
         A			:  Tuple   =		is_training
         A			:  List[Any]   =		use_labels
         A			:  Optional[Any]   =		hidden_size
         A			:  Dict   =		num_hidden_layers
         A			:  Union[str, Any]   =		num_attention_heads
         A			:  List[Any]   =		intermediate_size
         A			:  Any   =		hidden_act
         A			:  Dict   =		hidden_dropout_prob
         A			:  Any   =		attention_probs_dropout_prob
         A			:  Dict   =		type_sequence_label_size
         A			:  List[Any]   =		initializer_range
         A			:  Dict   =		scope
         A			:  Any   =		encoder_stride

         # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
         A			:  int   =		(image_size // patch_size) ** 2
         A			:  Optional[Any]   =		num_patches + 1





     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """simple docstring"""






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

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

         A			:  Dict   =		self.get_config()

         return config, pixel_values, labels





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






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





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							int:






         """simple docstring"""






         A			:  str   =		ViTModel(config=SCREAMING_SNAKE_CASE      )
         model.to(SCREAMING_SNAKE_CASE      )
         model.eval()
         A			:  Optional[Any]   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size)      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							Any:






         """simple docstring"""






         A			:  Any   =		ViTForMaskedImageModeling(config=SCREAMING_SNAKE_CASE      )
         model.to(SCREAMING_SNAKE_CASE      )
         model.eval()
         A			:  Any   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(
             result.reconstruction.shape		,     (self.batch_size, self.num_channels, self.image_size, self.image_size)      )

         # test greyscale images
         A			:  Optional[Any]   =		1
         A			:  str   =		ViTForMaskedImageModeling(SCREAMING_SNAKE_CASE      )
         model.to(SCREAMING_SNAKE_CASE      )
         model.eval()

         A			:  List[str]   =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
         A			:  Union[str, Any]   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.reconstruction.shape		,     (self.batch_size, 1, self.image_size, self.image_size)      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							int:






         """simple docstring"""






         A			:  str   =		self.type_sequence_label_size
         A			:  Optional[int]   =		ViTForImageClassification(SCREAMING_SNAKE_CASE      )
         model.to(SCREAMING_SNAKE_CASE      )
         model.eval()
         A			:  List[Any]   =		model(SCREAMING_SNAKE_CASE		,     labels=SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.type_sequence_label_size)      )

         # test greyscale images
         A			:  int   =		1
         A			:  Any   =		ViTForImageClassification(SCREAMING_SNAKE_CASE      )
         model.to(SCREAMING_SNAKE_CASE      )
         model.eval()

         A			:  Dict   =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
         A			:  str   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.type_sequence_label_size)      )





     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """simple docstring"""






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





@require_torch
class 					A							(				__snake_case       ,     __snake_case       ,     unittest.TestCase		):
     __magic_name__          =						(
         (
             ViTModel,
             ViTForImageClassification,
             ViTForMaskedImageModeling,
         )
         if is_torch_available()
         else ()
     )
     __magic_name__          =						(
         {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification}
         if is_torch_available()
         else {}
     )
     __magic_name__          =						True

     __magic_name__          =						False
     __magic_name__          =						False
     __magic_name__          =						False





     def 							__lowerCAmelCase				(				self      )					->							Tuple:






         """simple docstring"""






         A			:  List[str]   =		ViTModelTester(self      )
         A			:  List[str]   =		ConfigTester(self		,     config_class=SCREAMING_SNAKE_CASE		,     has_text_modality=SCREAMING_SNAKE_CASE		,     hidden_size=37      )





     def 							__lowerCAmelCase				(				self      )					->							Optional[Any]:






         """simple docstring"""






         self.config_tester.run_common_tests()





     @unittest.skip(reason='''ViT does not use inputs_embeds'''      )
     def 							__lowerCAmelCase				(				self      )					->							Dict:






         """simple docstring"""






         pass





     def 							__lowerCAmelCase				(				self      )					->							int:






         """simple docstring"""






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

         for model_class in self.all_model_classes:
             A			:  Any   =		model_class(SCREAMING_SNAKE_CASE      )
             self.assertIsInstance(model.get_input_embeddings()		,     (nn.Module)      )
             A			:  Optional[int]   =		model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE		,     nn.Linear      )      )





     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """simple docstring"""






         A,    A			:  Union[str, Any]   =		self.model_tester.prepare_config_and_inputs_for_common()

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

             A			:  Optional[int]   =		['''pixel_values''']
             self.assertListEqual(arg_names[:1]		,     SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """simple docstring"""






         A			:  Optional[Any]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """simple docstring"""






         A			:  Union[str, Any]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         A			:  Optional[Any]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE      )





     @slow
     def 							__lowerCAmelCase				(				self      )					->							List[str]:






         """simple docstring"""






         for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             A			:  Optional[Any]   =		ViTModel.from_pretrained(SCREAMING_SNAKE_CASE      )
             self.assertIsNotNone(SCREAMING_SNAKE_CASE      )







def 						lowerCAmelCase_							(  ):




    '''simple docstring'''



    A			:  Optional[int]   =		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
    return image







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





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






         """simple docstring"""






         return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224'''      ) if is_vision_available() else None





     @slow
     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         A			:  List[Any]   =		ViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''      ).to(SCREAMING_SNAKE_CASE      )

         A			:  Optional[int]   =		self.default_image_processor
         A			:  int   =		prepare_img()
         A			:  Tuple   =		image_processor(images=SCREAMING_SNAKE_CASE		,     return_tensors='''pt'''      ).to(SCREAMING_SNAKE_CASE      )

         # forward pass
         with torch.no_grad():
             A			:  Any   =		model(**SCREAMING_SNAKE_CASE      )

         # verify the logits
         A			:  List[Any]   =		torch.Size((1, 1000)      )
         self.assertEqual(outputs.logits.shape		,     SCREAMING_SNAKE_CASE      )

         A			:  List[str]   =		torch.tensor([-0.2_744, 0.8_215, -0.0_836]      ).to(SCREAMING_SNAKE_CASE      )

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





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






         """simple docstring"""






         A			:  Dict   =		ViTModel.from_pretrained('''facebook/dino-vits8'''      ).to(SCREAMING_SNAKE_CASE      )

         A			:  int   =		ViTImageProcessor.from_pretrained('''facebook/dino-vits8'''		,     size=480      )
         A			:  Tuple   =		prepare_img()
         A			:  List[Any]   =		image_processor(images=SCREAMING_SNAKE_CASE		,     return_tensors='''pt'''      )
         A			:  Tuple   =		inputs.pixel_values.to(SCREAMING_SNAKE_CASE      )

         # forward pass
         with torch.no_grad():
             A			:  Any   =		model(SCREAMING_SNAKE_CASE		,     interpolate_pos_encoding=SCREAMING_SNAKE_CASE      )

         # verify the logits
         A			:  int   =		torch.Size((1, 3601, 384)      )
         self.assertEqual(outputs.last_hidden_state.shape		,     SCREAMING_SNAKE_CASE      )

         A			:  Union[str, Any]   =		torch.tensor(
             [[4.2_340, 4.3_906, -6.6_692], [4.5_463, 1.8_928, -6.7_257], [4.4_429, 0.8_496, -5.8_585]]      ).to(SCREAMING_SNAKE_CASE      )

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





     @slow
     @require_accelerate
     @require_torch_gpu
     def 							__lowerCAmelCase				(				self      )					->							Union[str, Any]:






         """simple docstring"""






         A			:  List[Any]   =		ViTModel.from_pretrained('''facebook/dino-vits8'''		,     torch_dtype=torch.floataa		,     device_map='''auto'''      )
         A			:  int   =		self.default_image_processor

         A			:  List[str]   =		prepare_img()
         A			:  Union[str, Any]   =		image_processor(images=SCREAMING_SNAKE_CASE		,     return_tensors='''pt'''      )
         A			:  List[Any]   =		inputs.pixel_values.to(SCREAMING_SNAKE_CASE      )

         # forward pass to make sure inference works in fp16
         with torch.no_grad():
             A			:  str   =		model(SCREAMING_SNAKE_CASE      )




