






import inspect
import unittest

from transformers import ViTMSNConfig
from transformers.testing_utils import 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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch
				from torch import nn

				from transformers import ViTMSNForImageClassification, ViTMSNModel
				from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
				from PIL import Image

				from transformers import ViTImageProcessor

class 		_snake_case  :






							def __init__(       self		,					a		,					a=13		,					a=30		,					a=2		,					a=3		,					a=True		,					a=True		,					a=32		,					a=5		,					a=4		,					a=37		,					a="gelu"		,					a=0.1		,					a=0.1		,					a=10		,					a=0.02		,					a=None		,					)      ->       List[Any]:
										SCREAMING_SNAKE_CASE						= parent
										SCREAMING_SNAKE_CASE						= batch_size
										SCREAMING_SNAKE_CASE						= image_size
										SCREAMING_SNAKE_CASE						= patch_size
										SCREAMING_SNAKE_CASE						= num_channels
										SCREAMING_SNAKE_CASE						= is_training
										SCREAMING_SNAKE_CASE						= use_labels
										SCREAMING_SNAKE_CASE						= hidden_size
										SCREAMING_SNAKE_CASE						= num_hidden_layers
										SCREAMING_SNAKE_CASE						= num_attention_heads
										SCREAMING_SNAKE_CASE						= intermediate_size
										SCREAMING_SNAKE_CASE						= hidden_act
										SCREAMING_SNAKE_CASE						= hidden_dropout_prob
										SCREAMING_SNAKE_CASE						= attention_probs_dropout_prob
										SCREAMING_SNAKE_CASE						= type_sequence_label_size
										SCREAMING_SNAKE_CASE						= initializer_range
										SCREAMING_SNAKE_CASE						= scope

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






							def    SCREAMING_SNAKE_CASE__    (       self)      ->       Optional[int]:
										SCREAMING_SNAKE_CASE						= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

										SCREAMING_SNAKE_CASE						= self.get_config()

										return config, pixel_values, labels






							def    SCREAMING_SNAKE_CASE__    (       self)      ->       List[Any]:
										return ViTMSNConfig(
										    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		,					initializer_range=self.initializer_range		,					)






							def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a)      ->       Tuple:
										SCREAMING_SNAKE_CASE						= ViTMSNModel(config=a)
										model.to(a)
										model.eval()
										SCREAMING_SNAKE_CASE						= model(a)
										self.parent.assertEqual(result.last_hidden_state.shape		,					(self.batch_size, self.seq_length, self.hidden_size))






							def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a)      ->       Optional[Any]:
										SCREAMING_SNAKE_CASE						= self.type_sequence_label_size
										SCREAMING_SNAKE_CASE						= ViTMSNForImageClassification(a)
										model.to(a)
										model.eval()
										SCREAMING_SNAKE_CASE						= model(a		,					labels=a)
										print('Pixel and labels shape: {pixel_values.shape}, {labels.shape}')
										print('Labels: {labels}')
										self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.type_sequence_label_size))

										# test greyscale images
										SCREAMING_SNAKE_CASE						= 1
										SCREAMING_SNAKE_CASE						= ViTMSNForImageClassification(a)
										model.to(a)
										model.eval()

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







							def    SCREAMING_SNAKE_CASE__    (       self)      ->       Union[str, Any]:
										SCREAMING_SNAKE_CASE						= self.prepare_config_and_inputs()
										SCREAMING_SNAKE_CASE				,						SCREAMING_SNAKE_CASE				,						SCREAMING_SNAKE_CASE						= config_and_inputs
										SCREAMING_SNAKE_CASE						= {'pixel_values': pixel_values}
										return config, inputs_dict



@require_torch
class 		_snake_case  (  A__   ,    A__   ,    unittest.TestCase					):
							_lowercase		:    Tuple			     =							(ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else ()
							_lowercase		:    Tuple			     =							(
							    {'''feature-extraction''': ViTMSNModel, '''image-classification''': ViTMSNForImageClassification}
							    if is_torch_available()
							    else {}
							)

							_lowercase		:    str			     =							False
							_lowercase		:    List[Any]			     =							False
							_lowercase		:    Any			     =							False
							_lowercase		:    str			     =							False






							def    SCREAMING_SNAKE_CASE__    (       self)      ->       int:
										SCREAMING_SNAKE_CASE						= ViTMSNModelTester(self)
										SCREAMING_SNAKE_CASE						= ConfigTester(self		,					config_class=a		,					has_text_modality=a		,					hidden_size=37)






							def    SCREAMING_SNAKE_CASE__    (       self)      ->       Union[str, Any]:
										self.config_tester.run_common_tests()






							@unittest.skip(reason='ViTMSN does not use inputs_embeds')
							def    SCREAMING_SNAKE_CASE__    (       self)      ->       Optional[Any]:
										pass






							def    SCREAMING_SNAKE_CASE__    (       self)      ->       Optional[Any]:
										SCREAMING_SNAKE_CASE				,						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs_for_common()

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






							def    SCREAMING_SNAKE_CASE__    (       self)      ->       Optional[int]:
										SCREAMING_SNAKE_CASE				,						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs_for_common()

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

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






							def    SCREAMING_SNAKE_CASE__    (       self)      ->       Optional[Any]:
										SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*a)






							def    SCREAMING_SNAKE_CASE__    (       self)      ->       Any:
										SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*a)







							@slow
							def    SCREAMING_SNAKE_CASE__    (       self)      ->       List[str]:
										for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													SCREAMING_SNAKE_CASE						= ViTMSNModel.from_pretrained(a)
													self.assertIsNotNone(a)






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







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






							@cached_property
							def    SCREAMING_SNAKE_CASE__    (       self)      ->       Dict:
										return ViTImageProcessor.from_pretrained('facebook/vit-msn-small') if is_vision_available() else None







							@slow
							def    SCREAMING_SNAKE_CASE__    (       self)      ->       str:
										torch.manual_seed(2)
										SCREAMING_SNAKE_CASE						= ViTMSNForImageClassification.from_pretrained('facebook/vit-msn-small').to(a)

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

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

										# verify the logits
										SCREAMING_SNAKE_CASE						= torch.Size((1, 1000))
										self.assertEqual(outputs.logits.shape		,					a)

										SCREAMING_SNAKE_CASE						= torch.tensor([-0.08_03, -0.44_54, -0.23_75]).to(a)

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












import numpy as np
from scipy.spatial.distance import cdist
from sklearn.metrics import fa_score

import datasets


a_		:						Optional[int]             =    '\\n    @inproceedings{kakwani2020indicnlpsuite,\n    title={{IndicNLPSuite: Monolingual Corpora, Evaluation Benchmarks and Pre-trained Multilingual Language Models for Indian Languages}},\n    author={Divyanshu Kakwani and Anoop Kunchukuttan and Satish Golla and Gokul N.C. and Avik Bhattacharyya and Mitesh M. Khapra and Pratyush Kumar},\n    year={2020},\n    booktitle={Findings of EMNLP},\n}\n'

a_		:						Tuple             =    '\\n    IndicGLUE is a natural language understanding benchmark for Indian languages. It contains a wide\n    variety of tasks and covers 11 major Indian languages - as, bn, gu, hi, kn, ml, mr, or, pa, ta, te.\n'

a_		:						Optional[Any]             =    '\nCompute IndicGLUE evaluation metric associated to each IndicGLUE dataset.\nArgs:\n    predictions: list of predictions to score (as int64),\n        except for \'cvit-mkb-clsr\' where each prediction is a vector (of float32).\n    references: list of ground truth labels corresponding to the predictions (as int64),\n        except for \'cvit-mkb-clsr\' where each reference is a vector (of float32).\nReturns: depending on the IndicGLUE subset, one or several of:\n    "accuracy": Accuracy\n    "f1": F1 score\n    "precision": Precision@10\nExamples:\n\n    >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'wnli\')  # \'wnli\' or any of ["copa", "sna", "csqa", "wstp", "inltkh", "bbca", "iitp-mr", "iitp-pr", "actsa-sc", "md"]\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'accuracy\': 1.0}\n\n    >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'wiki-ner\')\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'accuracy\': 1.0, \'f1\': 1.0}\n\n    >>> indic_glue_metric = datasets.load_metric(\'indic_glue\', \'cvit-mkb-clsr\')\n    >>> references = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n    >>> predictions = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]\n    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'precision@10\': 1.0}\n\n'






def 				lowerCamelCase__				(_UpperCAmelCase							,       _UpperCAmelCase):
			return float((preds == labels).mean())






def 				lowerCamelCase__				(_UpperCAmelCase							,       _UpperCAmelCase):
			SCREAMING_SNAKE_CASE						= simple_accuracy(_UpperCAmelCase							,       _UpperCAmelCase)
			SCREAMING_SNAKE_CASE						= float(fa_score(y_true=_UpperCAmelCase							,       y_pred=_UpperCAmelCase))
			return {
			    "accuracy": acc,
			    "f1": fa,
			}






def 				lowerCamelCase__				(_UpperCAmelCase							,       _UpperCAmelCase):
			SCREAMING_SNAKE_CASE						= np.array(_UpperCAmelCase)
			SCREAMING_SNAKE_CASE						= np.array(_UpperCAmelCase)
			SCREAMING_SNAKE_CASE						= en_sentvecs.shape[0]

			# mean centering
			SCREAMING_SNAKE_CASE						= en_sentvecs - np.mean(_UpperCAmelCase							,       axis=0)
			SCREAMING_SNAKE_CASE						= in_sentvecs - np.mean(_UpperCAmelCase							,       axis=0)

			SCREAMING_SNAKE_CASE						= cdist(_UpperCAmelCase							,       _UpperCAmelCase							,       'cosine')
			SCREAMING_SNAKE_CASE						= np.array(range(_UpperCAmelCase))
			SCREAMING_SNAKE_CASE						= sim.argsort(axis=1)[:, :10]
			SCREAMING_SNAKE_CASE						= np.any(preds == actual[:, None]							,       axis=1)
			return float(matches.mean())







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






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Dict:
						if self.config_name not in [
						    "wnli",
						    "copa",
						    "sna",
						    "csqa",
						    "wstp",
						    "inltkh",
						    "bbca",
						    "cvit-mkb-clsr",
						    "iitp-mr",
						    "iitp-pr",
						    "actsa-sc",
						    "md",
						    "wiki-ner",
						]:
									raise KeyError(
									    'You should supply a configuration name selected in '
									    '["wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", '
									    '"cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", '
									    '"wiki-ner"]')
						return datasets.MetricInfo(
						    description=_DESCRIPTION		,					citation=_CITATION		,					inputs_description=_KWARGS_DESCRIPTION		,					features=datasets.Features(
						        {
						            'predictions': datasets.Value('int64')
						            if self.config_name != 'cvit-mkb-clsr'
						            else datasets.Sequence(datasets.Value('float32')),
						            'references': datasets.Value('int64')
						            if self.config_name != 'cvit-mkb-clsr'
						            else datasets.Sequence(datasets.Value('float32')),
						        })		,					codebase_urls=[]		,					reference_urls=[]		,					format='numpy' if self.config_name != 'cvit-mkb-clsr' else None		,					)







			def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a)      ->       List[Any]:
						if self.config_name == "cvit-mkb-clsr":
									return {"precision@10": precision_at_aa(a		,					a)}
						elif self.config_name in ["wiki-ner"]:
									return acc_and_fa(a		,					a)
						elif self.config_name in [
						    "wnli",
						    "copa",
						    "sna",
						    "csqa",
						    "wstp",
						    "inltkh",
						    "bbca",
						    "iitp-mr",
						    "iitp-pr",
						    "actsa-sc",
						    "md",
						]:
									return {"accuracy": simple_accuracy(a		,					a)}
						else:
									raise KeyError(
									    'You should supply a configuration name selected in '
									    '["wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", '
									    '"cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", '
									    '"wiki-ner"]')




