



"""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					:							Any,   lowerCAmelCase					:							int,   lowerCAmelCase					:							int=13,   lowerCAmelCase					:							Dict=7,   lowerCAmelCase					:							Optional[int]=True,   lowerCAmelCase					:							List[Any]=True,   lowerCAmelCase					:							Any=True,   lowerCAmelCase					:							Tuple=True,   lowerCAmelCase					:							Union[str, Any]=99,   lowerCAmelCase					:							List[str]=16,   lowerCAmelCase					:							List[Any]=36,   lowerCAmelCase					:							str=6,   lowerCAmelCase					:							Optional[Any]=6,   lowerCAmelCase					:							Optional[int]=6,   lowerCAmelCase					:							List[str]=37,   lowerCAmelCase					:							Optional[Any]="gelu",   lowerCAmelCase					:							Union[str, Any]=0.1,   lowerCAmelCase					:							Optional[Any]=0.1,   lowerCAmelCase					:							Union[str, Any]=512,   lowerCAmelCase					:							Optional[Any]=16,   lowerCAmelCase					:							Dict=2,   lowerCAmelCase					:							int=0.02,   lowerCAmelCase					:							List[Any]=3,   lowerCAmelCase					:							Any=4,   lowerCAmelCase					:							Tuple=None,   )			->				Any:
         lowercase     :					Optional[int] 		=      parent
         lowercase     :					Optional[int] 		=      batch_size
         lowercase     :					List[str] 		=      seq_length
         lowercase     :					Union[str, Any] 		=      is_training
         lowercase     :					int 		=      use_input_mask
         lowercase     :					Optional[Any] 		=      use_token_type_ids
         lowercase     :					List[str] 		=      use_labels
         lowercase     :					Tuple 		=      vocab_size
         lowercase     :					Optional[Any] 		=      embedding_size
         lowercase     :					str 		=      hidden_size
         lowercase     :					Tuple 		=      num_hidden_layers
         lowercase     :					Any 		=      num_hidden_groups
         lowercase     :					Tuple 		=      num_attention_heads
         lowercase     :					int 		=      intermediate_size
         lowercase     :					Any 		=      hidden_act
         lowercase     :					str 		=      hidden_dropout_prob
         lowercase     :					Tuple 		=      attention_probs_dropout_prob
         lowercase     :					Union[str, Any] 		=      max_position_embeddings
         lowercase     :					str 		=      type_vocab_size
         lowercase     :					Optional[Any] 		=      type_sequence_label_size
         lowercase     :					Union[str, Any] 		=      initializer_range
         lowercase     :					Optional[int] 		=      num_labels
         lowercase     :					Optional[Any] 		=      num_choices
         lowercase     :					List[str] 		=      scope


   def 				lowercase (							self					:							Optional[int]	)			->				List[Any]:
         lowercase     :					List[Any] 		=      ids_tensor([self.batch_size, self.seq_length],   self.vocab_size	)

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

         lowercase     :					Dict 		=      None
         if self.use_token_type_ids:
               lowercase     :					Optional[Any] 		=      ids_tensor([self.batch_size, self.seq_length],   self.type_vocab_size	)

         lowercase     :					List[str] 		=      None
         lowercase     :					int 		=      None
         lowercase     :					Optional[Any] 		=      None
         if self.use_labels:
               lowercase     :					List[Any] 		=      ids_tensor([self.batch_size],   self.type_sequence_label_size	)
               lowercase     :					Optional[Any] 		=      ids_tensor([self.batch_size, self.seq_length],   self.num_labels	)
               lowercase     :					Optional[int] 		=      ids_tensor([self.batch_size],   self.num_choices	)

         lowercase     :					List[Any] 		=      self.get_config()

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


   def 				lowercase (							self					:							Optional[Any]	)			->				List[str]:
         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					:							str,   lowerCAmelCase					:							List[str],   lowerCAmelCase					:							Optional[int],   lowerCAmelCase					:							List[str],   lowerCAmelCase					:							List[str],   lowerCAmelCase					:							Optional[Any],   lowerCAmelCase					:							str,   lowerCAmelCase					:							Optional[int]	)			->				Optional[int]:
         lowercase     :					Optional[Any] 		=      AlbertModel(config=__lowercase	)
         model.to(__lowercase	)
         model.eval()
         lowercase     :					str 		=      model(__lowercase,   attention_mask=__lowercase,   token_type_ids=__lowercase	)
         lowercase     :					str 		=      model(__lowercase,   token_type_ids=__lowercase	)
         lowercase     :					str 		=      model(__lowercase	)
         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					:							Any,   lowerCAmelCase					:							Optional[Any],   lowerCAmelCase					:							Optional[Any],   lowerCAmelCase					:							List[str],   lowerCAmelCase					:							Dict,   lowerCAmelCase					:							Any,   lowerCAmelCase					:							Dict,   lowerCAmelCase					:							List[str]	)			->				int:
         lowercase     :					List[str] 		=      AlbertForPreTraining(config=__lowercase	)
         model.to(__lowercase	)
         model.eval()
         lowercase     :					Tuple 		=      model(
             __lowercase,   attention_mask=__lowercase,   token_type_ids=__lowercase,   labels=__lowercase,   sentence_order_label=__lowercase,   )
         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					:							int,   lowerCAmelCase					:							Optional[Any],   lowerCAmelCase					:							List[Any],   lowerCAmelCase					:							List[str],   lowerCAmelCase					:							str,   lowerCAmelCase					:							Dict,   lowerCAmelCase					:							Optional[Any],   lowerCAmelCase					:							Optional[int]	)			->				List[Any]:
         lowercase     :					int 		=      AlbertForMaskedLM(config=__lowercase	)
         model.to(__lowercase	)
         model.eval()
         lowercase     :					int 		=      model(__lowercase,   attention_mask=__lowercase,   token_type_ids=__lowercase,   labels=__lowercase	)
         self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.seq_length, self.vocab_size)	)


   def 				lowercase (							self					:							Optional[int],   lowerCAmelCase					:							Dict,   lowerCAmelCase					:							Dict,   lowerCAmelCase					:							Tuple,   lowerCAmelCase					:							Any,   lowerCAmelCase					:							Union[str, Any],   lowerCAmelCase					:							int,   lowerCAmelCase					:							Optional[int]	)			->				List[Any]:
         lowercase     :					List[str] 		=      AlbertForQuestionAnswering(config=__lowercase	)
         model.to(__lowercase	)
         model.eval()
         lowercase     :					Dict 		=      model(
             __lowercase,   attention_mask=__lowercase,   token_type_ids=__lowercase,   start_positions=__lowercase,   end_positions=__lowercase,   )
         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					:							List[Any],   lowerCAmelCase					:							Any,   lowerCAmelCase					:							List[Any],   lowerCAmelCase					:							Dict,   lowerCAmelCase					:							List[str],   lowerCAmelCase					:							Dict,   lowerCAmelCase					:							List[Any],   lowerCAmelCase					:							List[Any]	)			->				Any:
         lowercase     :					Dict 		=      self.num_labels
         lowercase     :					Optional[Any] 		=      AlbertForSequenceClassification(__lowercase	)
         model.to(__lowercase	)
         model.eval()
         lowercase     :					str 		=      model(__lowercase,   attention_mask=__lowercase,   token_type_ids=__lowercase,   labels=__lowercase	)
         self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.num_labels)	)


   def 				lowercase (							self					:							Tuple,   lowerCAmelCase					:							str,   lowerCAmelCase					:							List[Any],   lowerCAmelCase					:							str,   lowerCAmelCase					:							List[Any],   lowerCAmelCase					:							List[Any],   lowerCAmelCase					:							Optional[int],   lowerCAmelCase					:							List[str]	)			->				Optional[Any]:
         lowercase     :					Any 		=      self.num_labels
         lowercase     :					Tuple 		=      AlbertForTokenClassification(config=__lowercase	)
         model.to(__lowercase	)
         model.eval()
         lowercase     :					Optional[Any] 		=      model(__lowercase,   attention_mask=__lowercase,   token_type_ids=__lowercase,   labels=__lowercase	)
         self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.seq_length, self.num_labels)	)


   def 				lowercase (							self					:							Dict,   lowerCAmelCase					:							Tuple,   lowerCAmelCase					:							Union[str, Any],   lowerCAmelCase					:							Union[str, Any],   lowerCAmelCase					:							List[Any],   lowerCAmelCase					:							Tuple,   lowerCAmelCase					:							str,   lowerCAmelCase					:							Tuple	)			->				Tuple:
         lowercase     :					int 		=      self.num_choices
         lowercase     :					Dict 		=      AlbertForMultipleChoice(config=__lowercase	)
         model.to(__lowercase	)
         model.eval()
         lowercase     :					Optional[Any] 		=      input_ids.unsqueeze(1	).expand(-1,   self.num_choices,   -1	).contiguous()
         lowercase     :					List[Any] 		=      token_type_ids.unsqueeze(1	).expand(-1,   self.num_choices,   -1	).contiguous()
         lowercase     :					str 		=      input_mask.unsqueeze(1	).expand(-1,   self.num_choices,   -1	).contiguous()
         lowercase     :					List[str] 		=      model(
             __lowercase,   attention_mask=__lowercase,   token_type_ids=__lowercase,   labels=__lowercase,   )
         self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.num_choices)	)






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




@require_torch
class      a__						(  UpperCAmelCase__,     UpperCAmelCase__,     unittest.TestCase   ):
   _lowerCamelCase    							= (
       (
           AlbertModel,
           AlbertForPreTraining,
           AlbertForMaskedLM,
           AlbertForMultipleChoice,
           AlbertForSequenceClassification,
           AlbertForTokenClassification,
           AlbertForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   _lowerCamelCase    							= (
       {
           """feature-extraction""": AlbertModel,
           """fill-mask""": AlbertForMaskedLM,
           """question-answering""": AlbertForQuestionAnswering,
           """text-classification""": AlbertForSequenceClassification,
           """token-classification""": AlbertForTokenClassification,
           """zero-shot""": AlbertForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   _lowerCamelCase    							= True


   def 				lowercase (							self					:							Tuple,   lowerCAmelCase					:							Dict,   lowerCAmelCase					:							Optional[Any],   lowerCAmelCase					:							int=False	)			->				Optional[Any]:
         lowercase     :					Any 		=      super()._prepare_for_class(__lowercase,   __lowercase,   return_labels=__lowercase	)

         if return_labels:
               if model_class in get_values(__lowercase	):
                     lowercase     :					Optional[int] 		=      torch.zeros(
                         (self.model_tester.batch_size, self.model_tester.seq_length),   dtype=torch.long,   device=__lowercase	)
                     lowercase     :					Optional[Any] 		=      torch.zeros(
                         self.model_tester.batch_size,   dtype=torch.long,   device=__lowercase	)
         return inputs_dict


   def 				lowercase (							self					:							List[str]	)			->				str:
         lowercase     :					str 		=      AlbertModelTester(self	)
         lowercase     :					int 		=      ConfigTester(self,   config_class=__lowercase,   hidden_size=37	)


   def 				lowercase (							self					:							Any	)			->				int:
         self.config_tester.run_common_tests()


   def 				lowercase (							self					:							Union[str, Any]	)			->				List[Any]:
         lowercase     :					int 		=      self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__lowercase	)


   def 				lowercase (							self					:							List[str]	)			->				Dict:
         lowercase     :					List[str] 		=      self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_pretraining(*__lowercase	)


   def 				lowercase (							self					:							Optional[int]	)			->				List[str]:
         lowercase     :					Any 		=      self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*__lowercase	)


   def 				lowercase (							self					:							int	)			->				List[Any]:
         lowercase     :					List[str] 		=      self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*__lowercase	)


   def 				lowercase (							self					:							Dict	)			->				List[Any]:
         lowercase     :					Tuple 		=      self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*__lowercase	)


   def 				lowercase (							self					:							Union[str, Any]	)			->				List[str]:
         lowercase     :					Tuple 		=      self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*__lowercase	)


   def 				lowercase (							self					:							Optional[Any]	)			->				Union[str, Any]:
         lowercase     :					Any 		=      self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
               lowercase     :					Union[str, Any] 		=      type
               self.model_tester.create_and_check_model(*__lowercase	)






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




@require_torch
class      a__						(  unittest.TestCase   ):


   @slow
   def 				lowercase (							self					:							List[Any]	)			->				Optional[Any]:
         lowercase     :					Union[str, Any] 		=      AlbertModel.from_pretrained('albert-base-v2'	)
         lowercase     :					Optional[int] 		=      torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]	)
         lowercase     :					Dict 		=      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
         with torch.no_grad():
               lowercase     :					List[str] 		=      model(__lowercase,   attention_mask=__lowercase	)[0]
         lowercase     :					Tuple 		=      torch.Size((1, 11, 768)	)
         self.assertEqual(output.shape,   __lowercase	)
         lowercase     :					Optional[Any] 		=      torch.tensor(
             [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]	)

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




from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels
from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features
from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor
from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels