


import datasets


_lowerCAmelCase:						List[str]   				=       '\\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'

_lowerCAmelCase:						List[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'

_lowerCAmelCase:						Union[str, Any]   				=       '\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   _lowercase(					__a				:						Optional[int]    ,     __a				:						List[Any]     ):
							return (preds == labels).mean()







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





				def 						__UpperCamelCase    (				self)  -> Any:
											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       ,  lowercase_       ,  lowercase_)  -> List[str]:
											return {"accuracy": simple_accuracy(lowercase_       ,  lowercase_)}






'''simple docstring'''

import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    MODEL_FOR_PRETRAINING_MAPPING,
			    AlbertForMaskedLM,
			    AlbertForMultipleChoice,
			    AlbertForPreTraining,
			    AlbertForQuestionAnswering,
			    AlbertForSequenceClassification,
			    AlbertForTokenClassification,
			    AlbertModel,
			)
			from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST

class 							a__:






			def __init__(     self					,   _UpperCAmelCase					,   _UpperCAmelCase=13					,   _UpperCAmelCase=7					,   _UpperCAmelCase=True					,   _UpperCAmelCase=True					,   _UpperCAmelCase=True					,   _UpperCAmelCase=True					,   _UpperCAmelCase=99					,   _UpperCAmelCase=16					,   _UpperCAmelCase=36					,   _UpperCAmelCase=6					,   _UpperCAmelCase=6					,   _UpperCAmelCase=6					,   _UpperCAmelCase=37					,   _UpperCAmelCase="gelu"					,   _UpperCAmelCase=0.1					,   _UpperCAmelCase=0.1					,   _UpperCAmelCase=512					,   _UpperCAmelCase=16					,   _UpperCAmelCase=2					,   _UpperCAmelCase=0.02					,   _UpperCAmelCase=3					,   _UpperCAmelCase=4					,   _UpperCAmelCase=None					,   )							->							Dict:
					snake_case__	     =parent
					snake_case__	     =batch_size
					snake_case__	     =seq_length
					snake_case__	     =is_training
					snake_case__	     =use_input_mask
					snake_case__	     =use_token_type_ids
					snake_case__	     =use_labels
					snake_case__	     =vocab_size
					snake_case__	     =embedding_size
					snake_case__	     =hidden_size
					snake_case__	     =num_hidden_layers
					snake_case__	     =num_hidden_groups
					snake_case__	     =num_attention_heads
					snake_case__	     =intermediate_size
					snake_case__	     =hidden_act
					snake_case__	     =hidden_dropout_prob
					snake_case__	     =attention_probs_dropout_prob
					snake_case__	     =max_position_embeddings
					snake_case__	     =type_vocab_size
					snake_case__	     =type_sequence_label_size
					snake_case__	     =initializer_range
					snake_case__	     =num_labels
					snake_case__	     =num_choices
					snake_case__	     =scope






			def 						_lowercase  (     self						)							->							Tuple:
					snake_case__	     =ids_tensor([self.batch_size, self.seq_length]					,   self.vocab_size						)

					snake_case__	     =None
					if self.use_input_mask:
							snake_case__	     =random_attention_mask([self.batch_size, self.seq_length]						)

					snake_case__	     =None
					if self.use_token_type_ids:
							snake_case__	     =ids_tensor([self.batch_size, self.seq_length]					,   self.type_vocab_size						)

					snake_case__	     =None
					snake_case__	     =None
					snake_case__	     =None
					if self.use_labels:
							snake_case__	     =ids_tensor([self.batch_size]					,   self.type_sequence_label_size						)
							snake_case__	     =ids_tensor([self.batch_size, self.seq_length]					,   self.num_labels						)
							snake_case__	     =ids_tensor([self.batch_size]					,   self.num_choices						)

					snake_case__	     =self.get_config()

					return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






			def 						_lowercase  (     self						)							->							Union[str, Any]:
					return AlbertConfig(
					    vocab_size=self.vocab_size					,   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					,   max_position_embeddings=self.max_position_embeddings					,   type_vocab_size=self.type_vocab_size					,   initializer_range=self.initializer_range					,   num_hidden_groups=self.num_hidden_groups					,   )






			def 						_lowercase  (     self					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase						)							->							List[str]:
					snake_case__	     =AlbertModel(config=_UpperCAmelCase						)
					model.to(_UpperCAmelCase						)
					model.eval()
					snake_case__	     =model(_UpperCAmelCase					,   attention_mask=_UpperCAmelCase					,   token_type_ids=_UpperCAmelCase						)
					snake_case__	     =model(_UpperCAmelCase					,   token_type_ids=_UpperCAmelCase						)
					snake_case__	     =model(_UpperCAmelCase						)
					self.parent.assertEqual(result.last_hidden_state.shape					,   (self.batch_size, self.seq_length, self.hidden_size)						)
					self.parent.assertEqual(result.pooler_output.shape					,   (self.batch_size, self.hidden_size)						)






			def 						_lowercase  (     self					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase						)							->							Dict:
					snake_case__	     =AlbertForPreTraining(config=_UpperCAmelCase						)
					model.to(_UpperCAmelCase						)
					model.eval()
					snake_case__	     =model(
					    _UpperCAmelCase					,   attention_mask=_UpperCAmelCase					,   token_type_ids=_UpperCAmelCase					,   labels=_UpperCAmelCase					,   sentence_order_label=_UpperCAmelCase					,   )
					self.parent.assertEqual(result.prediction_logits.shape					,   (self.batch_size, self.seq_length, self.vocab_size)						)
					self.parent.assertEqual(result.sop_logits.shape					,   (self.batch_size, config.num_labels)						)






			def 						_lowercase  (     self					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase						)							->							List[str]:
					snake_case__	     =AlbertForMaskedLM(config=_UpperCAmelCase						)
					model.to(_UpperCAmelCase						)
					model.eval()
					snake_case__	     =model(_UpperCAmelCase					,   attention_mask=_UpperCAmelCase					,   token_type_ids=_UpperCAmelCase					,   labels=_UpperCAmelCase						)
					self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.seq_length, self.vocab_size)						)






			def 						_lowercase  (     self					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase						)							->							List[str]:
					snake_case__	     =AlbertForQuestionAnswering(config=_UpperCAmelCase						)
					model.to(_UpperCAmelCase						)
					model.eval()
					snake_case__	     =model(
					    _UpperCAmelCase					,   attention_mask=_UpperCAmelCase					,   token_type_ids=_UpperCAmelCase					,   start_positions=_UpperCAmelCase					,   end_positions=_UpperCAmelCase					,   )
					self.parent.assertEqual(result.start_logits.shape					,   (self.batch_size, self.seq_length)						)
					self.parent.assertEqual(result.end_logits.shape					,   (self.batch_size, self.seq_length)						)






			def 						_lowercase  (     self					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase						)							->							Optional[int]:
					snake_case__	     =self.num_labels
					snake_case__	     =AlbertForSequenceClassification(_UpperCAmelCase						)
					model.to(_UpperCAmelCase						)
					model.eval()
					snake_case__	     =model(_UpperCAmelCase					,   attention_mask=_UpperCAmelCase					,   token_type_ids=_UpperCAmelCase					,   labels=_UpperCAmelCase						)
					self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.num_labels)						)






			def 						_lowercase  (     self					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase						)							->							Any:
					snake_case__	     =self.num_labels
					snake_case__	     =AlbertForTokenClassification(config=_UpperCAmelCase						)
					model.to(_UpperCAmelCase						)
					model.eval()
					snake_case__	     =model(_UpperCAmelCase					,   attention_mask=_UpperCAmelCase					,   token_type_ids=_UpperCAmelCase					,   labels=_UpperCAmelCase						)
					self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.seq_length, self.num_labels)						)






			def 						_lowercase  (     self					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase						)							->							Dict:
					snake_case__	     =self.num_choices
					snake_case__	     =AlbertForMultipleChoice(config=_UpperCAmelCase						)
					model.to(_UpperCAmelCase						)
					model.eval()
					snake_case__	     =input_ids.unsqueeze(1						).expand(-1					,   self.num_choices					,   -1						).contiguous()
					snake_case__	     =token_type_ids.unsqueeze(1						).expand(-1					,   self.num_choices					,   -1						).contiguous()
					snake_case__	     =input_mask.unsqueeze(1						).expand(-1					,   self.num_choices					,   -1						).contiguous()
					snake_case__	     =model(
					    _UpperCAmelCase					,   attention_mask=_UpperCAmelCase					,   token_type_ids=_UpperCAmelCase					,   labels=_UpperCAmelCase					,   )
					self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.num_choices)						)






			def 						_lowercase  (     self						)							->							List[Any]:
					snake_case__	     =self.prepare_config_and_inputs()
					(
					    (
					    snake_case__
					)     ,	(
					    snake_case__
					)     ,	(
					    snake_case__
					)     ,	(
					    snake_case__
					)     ,	(
					    snake_case__
					)     ,	(
					    snake_case__
					)     ,	(
					    snake_case__
					)     ,	
					)	     =config_and_inputs
					snake_case__	     ={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
					return config, inputs_dict





@require_torch
class 							a__( snake_case__   ,      snake_case__   ,      unittest.TestCase     ):
			a_	: Optional[int]									=      (
			    (
			        AlbertModel,
			        AlbertForPreTraining,
			        AlbertForMaskedLM,
			        AlbertForMultipleChoice,
			        AlbertForSequenceClassification,
			        AlbertForTokenClassification,
			        AlbertForQuestionAnswering,
			    )
			    if is_torch_available()
			    else ()
			)
			a_	: List[str]									=      (
			    {
			        '''feature-extraction''': AlbertModel,
			        '''fill-mask''': AlbertForMaskedLM,
			        '''question-answering''': AlbertForQuestionAnswering,
			        '''text-classification''': AlbertForSequenceClassification,
			        '''token-classification''': AlbertForTokenClassification,
			        '''zero-shot''': AlbertForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			a_	: int									=      True






			def 						_lowercase  (     self					,   _UpperCAmelCase					,   _UpperCAmelCase					,   _UpperCAmelCase=False						)							->							Any:
					snake_case__	     =super()._prepare_for_class(_UpperCAmelCase					,   _UpperCAmelCase					,   return_labels=_UpperCAmelCase						)

					if return_labels:
							if model_class in get_values(_UpperCAmelCase						):
									snake_case__	     =torch.zeros(
									    (self.model_tester.batch_size, self.model_tester.seq_length)					,   dtype=torch.long					,   device=_UpperCAmelCase						)
									snake_case__	     =torch.zeros(
									    self.model_tester.batch_size					,   dtype=torch.long					,   device=_UpperCAmelCase						)
					return inputs_dict






			def 						_lowercase  (     self						)							->							int:
					snake_case__	     =AlbertModelTester(self						)
					snake_case__	     =ConfigTester(self					,   config_class=_UpperCAmelCase					,   hidden_size=37						)






			def 						_lowercase  (     self						)							->							Dict:
					self.config_tester.run_common_tests()






			def 						_lowercase  (     self						)							->							str:
					snake_case__	     =self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_UpperCAmelCase						)






			def 						_lowercase  (     self						)							->							Optional[int]:
					snake_case__	     =self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_pretraining(*_UpperCAmelCase						)






			def 						_lowercase  (     self						)							->							Optional[Any]:
					snake_case__	     =self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*_UpperCAmelCase						)






			def 						_lowercase  (     self						)							->							Tuple:
					snake_case__	     =self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase						)






			def 						_lowercase  (     self						)							->							List[str]:
					snake_case__	     =self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase						)






			def 						_lowercase  (     self						)							->							Optional[int]:
					snake_case__	     =self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase						)






			def 						_lowercase  (     self						)							->							List[str]:
					snake_case__	     =self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
							snake_case__	     =type
							self.model_tester.create_and_check_model(*_UpperCAmelCase						)






			@slow
			def 						_lowercase  (     self						)							->							Union[str, Any]:
					for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							snake_case__	     =AlbertModel.from_pretrained(_UpperCAmelCase						)
							self.assertIsNotNone(_UpperCAmelCase						)





@require_torch
class 							a__( unittest.TestCase     ):






			@slow
			def 						_lowercase  (     self						)							->							str:
					snake_case__	     =AlbertModel.from_pretrained('albert-base-v2'						)
					snake_case__	     =torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]						)
					snake_case__	     =torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
					with torch.no_grad():
							snake_case__	     =model(_UpperCAmelCase					,   attention_mask=_UpperCAmelCase						)[0]
					snake_case__	     =torch.Size((1, 11, 768)						)
					self.assertEqual(output.shape					,   _UpperCAmelCase						)
					snake_case__	     =torch.tensor(
					    [[[-0.6_513, 1.5_035, -0.2_766], [-0.6_515, 1.5_046, -0.2_780], [-0.6_512, 1.5_049, -0.2_784]]]						)

					self.assertTrue(torch.allclose(output[:, 1:4, 1:4]					,   _UpperCAmelCase					,   atol=1E-4						)						)




