


'''simple docstring'''



import datasets


__lowercase	:						List[Any]       =	'\\n@InProceedings{conneau2018xnli,\n  author = "Conneau, Alexis\n                 and Rinott, Ruty\n                 and Lample, Guillaume\n                 and Williams, Adina\n                 and Bowman, Samuel R.\n                 and Schwenk, Holger\n                 and Stoyanov, Veselin",\n  title = "XNLI: Evaluating Cross-lingual Sentence Representations",\n  booktitle = "Proceedings of the 2018 Conference on Empirical Methods\n               in Natural Language Processing",\n  year = "2018",\n  publisher = "Association for Computational Linguistics",\n  location = "Brussels, Belgium",\n}\n'

__lowercase	:						Optional[Any]       =	'\\nXNLI is a subset of a few thousand examples from MNLI which has been translated\ninto a 14 different languages (some low-ish resource). As with MNLI, the goal is\nto predict textual entailment (does sentence A imply/contradict/neither sentence\nB) and is a classification task (given two sentences, predict one of three\nlabels).\n'

__lowercase	:						Tuple       =	'\nComputes XNLI score which is just simple accuracy.\nArgs:\n    predictions: Predicted labels.\n    references: Ground truth labels.\nReturns:\n    \'accuracy\': accuracy\nExamples:\n\n    >>> predictions = [0, 1]\n    >>> references = [0, 1]\n    >>> xnli_metric = datasets.load_metric("xnli")\n    >>> results = xnli_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'accuracy\': 1.0}\n'






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   str						,      _SCREAMING_SNAKE_CASE      :   Dict   ):
					return (preds == labels).mean()





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




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return datasets.MetricInfo(
						    description=_DESCRIPTION						,				citation=_CITATION						,				inputs_description=_KWARGS_DESCRIPTION						,				features=datasets.Features(
						        {
						            'predictions': datasets.Value('int64' if self.config_name != 'sts-b' else 'float32'							),
						            'references': datasets.Value('int64' if self.config_name != 'sts-b' else 'float32'							),
						        }							)						,				codebase_urls=[]						,				reference_urls=[]						,				format='numpy'						,				)





	def       __UpperCAmelCase							(   self						,				__a						,				__a							):

						'''simple docstring'''





						return {"accuracy": simple_accuracy(_snake_case						,				_snake_case							)}








"""simple docstring"""







import collections
import inspect
import unittest

from transformers import SwinvaConfig
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, _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 SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
							from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor
class        __A       :







		'''simple docstring'''





		def __init__(	self						:  Optional[int]			,_snake_case						:  Optional[Any]			,_snake_case						:  Union[str, Any]=13			,_snake_case						:  Any=32			,_snake_case						:  int=2			,_snake_case						:  str=3			,_snake_case						:  Optional[Any]=16			,_snake_case						:  List[Any]=[1, 2, 1]			,_snake_case						:  Dict=[2, 2, 4]			,_snake_case						:  List[Any]=2			,_snake_case						:  Any=2.0			,_snake_case						:  Optional[int]=True			,_snake_case						:  Optional[int]=0.0			,_snake_case						:  Union[str, Any]=0.0			,_snake_case						:  str=0.1			,_snake_case						:  List[Any]="gelu"			,_snake_case						:  Tuple=False			,_snake_case						:  Optional[int]=True			,_snake_case						:  str=0.02			,_snake_case						:  List[str]=1e-5			,_snake_case						:  int=True			,_snake_case						:  Dict=None			,_snake_case						:  str=True			,_snake_case						:  List[Any]=10			,_snake_case						:  Any=8			,)					->		Union[str, Any]:






				"""simple docstring"""



				lowercase__   :    Dict				=			parent
				lowercase__   :    Any				=			batch_size
				lowercase__   :    Union[str, Any]				=			image_size
				lowercase__   :    Dict				=			patch_size
				lowercase__   :    int				=			num_channels
				lowercase__   :    Any				=			embed_dim
				lowercase__   :    int				=			depths
				lowercase__   :    Dict				=			num_heads
				lowercase__   :    List[Any]				=			window_size
				lowercase__   :    int				=			mlp_ratio
				lowercase__   :    Optional[int]				=			qkv_bias
				lowercase__   :    str				=			hidden_dropout_prob
				lowercase__   :    List[Any]				=			attention_probs_dropout_prob
				lowercase__   :    Dict				=			drop_path_rate
				lowercase__   :    int				=			hidden_act
				lowercase__   :    Tuple				=			use_absolute_embeddings
				lowercase__   :    Tuple				=			patch_norm
				lowercase__   :    Tuple				=			layer_norm_eps
				lowercase__   :    Optional[Any]				=			initializer_range
				lowercase__   :    int				=			is_training
				lowercase__   :    Optional[int]				=			scope
				lowercase__   :    str				=			use_labels
				lowercase__   :    Dict				=			type_sequence_label_size
				lowercase__   :    Union[str, Any]				=			encoder_stride







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Any:






				"""simple docstring"""



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

				lowercase__   :    Optional[Any]				=			None
				if self.use_labels:
						lowercase__   :    Optional[int]				=			ids_tensor([self.batch_size]			,self.type_sequence_label_size						)

				lowercase__   :    Union[str, Any]				=			self.get_config()

				return config, pixel_values, labels







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






				"""simple docstring"""



				return SwinvaConfig(
				    image_size=self.image_size			,patch_size=self.patch_size			,num_channels=self.num_channels			,embed_dim=self.embed_dim			,depths=self.depths			,num_heads=self.num_heads			,window_size=self.window_size			,mlp_ratio=self.mlp_ratio			,qkv_bias=self.qkv_bias			,hidden_dropout_prob=self.hidden_dropout_prob			,attention_probs_dropout_prob=self.attention_probs_dropout_prob			,drop_path_rate=self.drop_path_rate			,hidden_act=self.hidden_act			,use_absolute_embeddings=self.use_absolute_embeddings			,path_norm=self.patch_norm			,layer_norm_eps=self.layer_norm_eps			,initializer_range=self.initializer_range			,encoder_stride=self.encoder_stride			,)







		def 					UpperCAmelCase    (	self						:  str			,_snake_case						:  Dict			,_snake_case						:  List[str]			,_snake_case						:  Optional[int]						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Any				=			SwinvaModel(config=_snake_case						)
				model.to(_snake_case						)
				model.eval()
				lowercase__   :    str				=			model(_snake_case						)

				lowercase__   :    List[Any]				=			((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths						) - 1))
				lowercase__   :    Tuple				=			int(config.embed_dim * 2 ** (len(config.depths						) - 1)						)

				self.parent.assertEqual(result.last_hidden_state.shape			,(self.batch_size, expected_seq_len, expected_dim)						)







		def 					UpperCAmelCase    (	self						:  Union[str, Any]			,_snake_case						:  List[str]			,_snake_case						:  Optional[Any]			,_snake_case						:  int						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Union[str, Any]				=			SwinvaForMaskedImageModeling(config=_snake_case						)
				model.to(_snake_case						)
				model.eval()
				lowercase__   :    Tuple				=			model(_snake_case						)
				self.parent.assertEqual(
				    result.logits.shape			,(self.batch_size, self.num_channels, self.image_size, self.image_size)						)

				# test greyscale images
				lowercase__   :    Optional[int]				=			1
				lowercase__   :    List[Any]				=			SwinvaForMaskedImageModeling(_snake_case						)
				model.to(_snake_case						)
				model.eval()

				lowercase__   :    Union[str, Any]				=			floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
				lowercase__   :    str				=			model(_snake_case						)
				self.parent.assertEqual(result.logits.shape			,(self.batch_size, 1, self.image_size, self.image_size)						)







		def 					UpperCAmelCase    (	self						:  str			,_snake_case						:  str			,_snake_case						:  str			,_snake_case						:  Tuple						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Tuple				=			self.type_sequence_label_size
				lowercase__   :    Dict				=			SwinvaForImageClassification(_snake_case						)
				model.to(_snake_case						)
				model.eval()
				lowercase__   :    str				=			model(_snake_case			,labels=_snake_case						)
				self.parent.assertEqual(result.logits.shape			,(self.batch_size, self.type_sequence_label_size)						)







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Dict:






				"""simple docstring"""



				lowercase__   :    Optional[int]				=			self.prepare_config_and_inputs()
				lowercase__		,			lowercase__		,			lowercase__   :    Union[str, Any]				=			config_and_inputs
				lowercase__   :    List[str]				=			{'''pixel_values''': pixel_values}
				return config, inputs_dict





@require_torch
class        __A       (	A_     ,A_     ,unittest.TestCase    ):







		'''simple docstring'''





		lowerCAmelCase			:					Union[str, Any]   	=				(
		    (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
		)
		lowerCAmelCase			:					Optional[int]   	=				(
		    {"feature-extraction": SwinvaModel, "image-classification": SwinvaForImageClassification}
		    if is_torch_available()
		    else {}
		)

		lowerCAmelCase			:					List[Any]   	=				False
		lowerCAmelCase			:					Dict   	=				False
		lowerCAmelCase			:					List[Any]   	=				False
		lowerCAmelCase			:					Any   	=				False







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






				"""simple docstring"""



				lowercase__   :    Optional[Any]				=			SwinvaModelTester(self						)
				lowercase__   :    List[str]				=			ConfigTester(self			,config_class=_snake_case			,embed_dim=37						)







		def 					UpperCAmelCase    (	self						:  int						)					->		Any:






				"""simple docstring"""



				self.config_tester.create_and_test_config_to_json_string()
				self.config_tester.create_and_test_config_to_json_file()
				self.config_tester.create_and_test_config_from_and_save_pretrained()
				self.config_tester.create_and_test_config_with_num_labels()
				self.config_tester.check_config_can_be_init_without_params()
				self.config_tester.check_config_arguments_init()







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






				"""simple docstring"""



				lowercase__   :    int				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_snake_case						)







		@unittest.skip(reason='''Got `CUDA error: misaligned address` with PyTorch 2.0.0.'''						)
		def 					UpperCAmelCase    (	self						:  Optional[Any]						)					->		Optional[int]:






				"""simple docstring"""



				pass







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






				"""simple docstring"""



				pass







		def 					UpperCAmelCase    (	self						:  Optional[int]						)					->		Tuple:






				"""simple docstring"""



				lowercase__		,			lowercase__   :    Optional[int]				=			self.model_tester.prepare_config_and_inputs_for_common()

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







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






				"""simple docstring"""



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

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

						lowercase__   :    Tuple				=			['''pixel_values''']
						self.assertListEqual(arg_names[:1]			,_snake_case						)







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






				"""simple docstring"""



				lowercase__		,			lowercase__   :    List[str]				=			self.model_tester.prepare_config_and_inputs_for_common()
				lowercase__   :    Tuple				=			True

				for model_class in self.all_model_classes:
						lowercase__   :    Optional[int]				=			True
						lowercase__   :    str				=			False
						lowercase__   :    Union[str, Any]				=			True
						lowercase__   :    Optional[Any]				=			model_class(_snake_case						)
						model.to(_snake_case						)
						model.eval()
						with torch.no_grad():
								lowercase__   :    str				=			model(**self._prepare_for_class(_snake_case			,_snake_case						)						)
						lowercase__   :    Dict				=			outputs.attentions
						lowercase__   :    Any				=			len(self.model_tester.depths						)
						self.assertEqual(len(_snake_case						)			,_snake_case						)

						# check that output_attentions also work using config
						del inputs_dict["output_attentions"]
						lowercase__   :    List[Any]				=			True
						lowercase__   :    Optional[Any]				=			config.window_size**2
						lowercase__   :    Any				=			model_class(_snake_case						)
						model.to(_snake_case						)
						model.eval()
						with torch.no_grad():
								lowercase__   :    List[str]				=			model(**self._prepare_for_class(_snake_case			,_snake_case						)						)
						lowercase__   :    Optional[Any]				=			outputs.attentions
						self.assertEqual(len(_snake_case						)			,_snake_case						)

						self.assertListEqual(
						    list(attentions[0].shape[-3:]						)			,[self.model_tester.num_heads[0], window_size_squared, window_size_squared]			,)
						lowercase__   :    Optional[Any]				=			len(_snake_case						)

						# Check attention is always last and order is fine
						lowercase__   :    Optional[int]				=			True
						lowercase__   :    Tuple				=			True
						lowercase__   :    Optional[Any]				=			model_class(_snake_case						)
						model.to(_snake_case						)
						model.eval()
						with torch.no_grad():
								lowercase__   :    Optional[Any]				=			model(**self._prepare_for_class(_snake_case			,_snake_case						)						)

						if hasattr(self.model_tester			,'''num_hidden_states_types'''						):
								lowercase__   :    int				=			self.model_tester.num_hidden_states_types
						else:
								# also another +1 for reshaped_hidden_states
								lowercase__   :    List[str]				=			2
						self.assertEqual(out_len + added_hidden_states			,len(_snake_case						)						)

						lowercase__   :    Optional[int]				=			outputs.attentions

						self.assertEqual(len(_snake_case						)			,_snake_case						)

						self.assertListEqual(
						    list(self_attentions[0].shape[-3:]						)			,[self.model_tester.num_heads[0], window_size_squared, window_size_squared]			,)







		def 					UpperCAmelCase    (	self						:  List[str]			,_snake_case						:  int			,_snake_case						:  List[str]			,_snake_case						:  Optional[int]			,_snake_case						:  List[Any]						)					->		Union[str, Any]:






				"""simple docstring"""



				lowercase__   :    List[Any]				=			model_class(_snake_case						)
				model.to(_snake_case						)
				model.eval()

				with torch.no_grad():
						lowercase__   :    int				=			model(**self._prepare_for_class(_snake_case			,_snake_case						)						)

				lowercase__   :    Optional[int]				=			outputs.hidden_states

				lowercase__   :    List[Any]				=			getattr(
				    self.model_tester			,'''expected_num_hidden_layers'''			,len(self.model_tester.depths						) + 1						)
				self.assertEqual(len(_snake_case						)			,_snake_case						)

				# Swinv2 has a different seq_length
				lowercase__   :    Dict				=			(
				    config.patch_size
				    if isinstance(config.patch_size			,collections.abc.Iterable						)
				    else (config.patch_size, config.patch_size)
				)

				lowercase__   :    int				=			(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

				self.assertListEqual(
				    list(hidden_states[0].shape[-2:]						)			,[num_patches, self.model_tester.embed_dim]			,)

				lowercase__   :    Tuple				=			outputs.reshaped_hidden_states
				self.assertEqual(len(_snake_case						)			,_snake_case						)

				lowercase__		,			lowercase__		,			lowercase__		,			lowercase__   :    List[str]				=			reshaped_hidden_states[0].shape
				lowercase__   :    int				=			(
				    reshaped_hidden_states[0].view(_snake_case			,_snake_case			,height * width						).permute(0			,2			,1						)
				)
				self.assertListEqual(
				    list(reshaped_hidden_states.shape[-2:]						)			,[num_patches, self.model_tester.embed_dim]			,)







		def 					UpperCAmelCase    (	self						:  Tuple						)					->		int:






				"""simple docstring"""



				lowercase__		,			lowercase__   :    Optional[int]				=			self.model_tester.prepare_config_and_inputs_for_common()

				lowercase__   :    str				=			(
				    self.model_tester.image_size
				    if isinstance(self.model_tester.image_size			,collections.abc.Iterable						)
				    else (self.model_tester.image_size, self.model_tester.image_size)
				)

				for model_class in self.all_model_classes:
						lowercase__   :    List[str]				=			True
						self.check_hidden_states_output(_snake_case			,_snake_case			,_snake_case			,_snake_case						)

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						lowercase__   :    str				=			True

						self.check_hidden_states_output(_snake_case			,_snake_case			,_snake_case			,_snake_case						)







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






				"""simple docstring"""



				lowercase__		,			lowercase__   :    List[str]				=			self.model_tester.prepare_config_and_inputs_for_common()
				lowercase__   :    List[Any]				=			3

				lowercase__   :    Tuple				=			(
				    self.model_tester.image_size
				    if isinstance(self.model_tester.image_size			,collections.abc.Iterable						)
				    else (self.model_tester.image_size, self.model_tester.image_size)
				)
				lowercase__   :    Optional[int]				=			(
				    config.patch_size
				    if isinstance(config.patch_size			,collections.abc.Iterable						)
				    else (config.patch_size, config.patch_size)
				)

				lowercase__   :    Dict				=			image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
				lowercase__   :    Dict				=			image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

				for model_class in self.all_model_classes:
						lowercase__   :    str				=			True
						self.check_hidden_states_output(_snake_case			,_snake_case			,_snake_case			,(padded_height, padded_width)						)

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						lowercase__   :    Dict				=			True
						self.check_hidden_states_output(_snake_case			,_snake_case			,_snake_case			,(padded_height, padded_width)						)







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






				"""simple docstring"""



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







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Tuple				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*_snake_case						)







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






				"""simple docstring"""



				for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowercase__   :    Union[str, Any]				=			SwinvaModel.from_pretrained(_snake_case						)
						self.assertIsNotNone(_snake_case						)







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






				"""simple docstring"""



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

				lowercase__   :    Tuple				=			_config_zero_init(_snake_case						)
				for model_class in self.all_model_classes:
						lowercase__   :    Optional[int]				=			model_class(config=_snake_case						)
						for name, param in model.named_parameters():
								if "embeddings" not in name and "logit_scale" not in name and param.requires_grad:
										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"""			,)





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







		'''simple docstring'''





		@cached_property
		def 					UpperCAmelCase    (	self						:  Optional[Any]						)					->		Tuple:






				"""simple docstring"""



				return (
				    AutoImageProcessor.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256'''						)
				    if is_vision_available()
				    else None
				)







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






				"""simple docstring"""



				lowercase__   :    str				=			SwinvaForImageClassification.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256'''						).to(
				    _snake_case						)
				lowercase__   :    Union[str, Any]				=			self.default_image_processor

				lowercase__   :    List[str]				=			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
				lowercase__   :    Dict				=			image_processor(images=_snake_case			,return_tensors='''pt'''						).to(_snake_case						)

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

				# verify the logits
				lowercase__   :    str				=			torch.Size((1, 1_000)						)
				self.assertEqual(outputs.logits.shape			,_snake_case						)
				lowercase__   :    Dict				=			torch.tensor([-0.3947, -0.4306, 0.0026]						).to(_snake_case						)
				self.assertTrue(torch.allclose(outputs.logits[0, :3]			,_snake_case			,atol=1e-4						)						)
