
"""simple docstring"""




import unittest

from transformers import MraConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_torch_available():
  import torch

  from transformers import (
      MraForMaskedLM,
      MraForMultipleChoice,
      MraForQuestionAnswering,
      MraForSequenceClassification,
      MraForTokenClassification,
      MraModel,
  )
  from transformers.models.mra.modeling_mra import MRA_PRETRAINED_MODEL_ARCHIVE_LIST


class 			__magic_name__       :
       def __init__(     self ,				A_ ,				A_=2 ,				A_=8 ,				A_=True ,				A_=True ,				A_=True ,				A_=True ,				A_=99 ,				A_=16 ,				A_=5 ,				A_=2 ,				A_=36 ,				A_="gelu" ,				A_=0.0 ,				A_=0.0 ,				A_=512 ,				A_=16 ,				A_=2 ,				A_=0.02 ,				A_=3 ,				A_=4 ,				A_=None ,				)     ->	Union[str, Any]:





         """simple docstring"""




         _lowercase:			Any       =     parent
         _lowercase:			Dict       =     batch_size
         _lowercase:			Optional[int]       =     seq_length
         _lowercase:			int       =     is_training
         _lowercase:			List[str]       =     use_input_mask
         _lowercase:			Dict       =     use_token_type_ids
         _lowercase:			List[Any]       =     use_labels
         _lowercase:			Optional[int]       =     vocab_size
         _lowercase:			int       =     hidden_size
         _lowercase:			Optional[Any]       =     num_hidden_layers
         _lowercase:			Union[str, Any]       =     num_attention_heads
         _lowercase:			Dict       =     intermediate_size
         _lowercase:			Optional[int]       =     hidden_act
         _lowercase:			str       =     hidden_dropout_prob
         _lowercase:			int       =     attention_probs_dropout_prob
         _lowercase:			Optional[int]       =     max_position_embeddings
         _lowercase:			Union[str, Any]       =     type_vocab_size
         _lowercase:			List[str]       =     type_sequence_label_size
         _lowercase:			List[str]       =     initializer_range
         _lowercase:			str       =     num_labels
         _lowercase:			Any       =     num_choices
         _lowercase:			Optional[int]       =     scope
       def 							lowercase_							(     self       )     ->	List[Any]:





         """simple docstring"""




         _lowercase:			int       =     ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size       )

         _lowercase:			int       =     None
         if self.use_input_mask:
           _lowercase:			Union[str, Any]       =     random_attention_mask([self.batch_size, self.seq_length]       )

         _lowercase:			Optional[Any]       =     None
         if self.use_token_type_ids:
           _lowercase:			Union[str, Any]       =     ids_tensor([self.batch_size, self.seq_length] ,				self.type_vocab_size       )

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

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

         return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
       def 							lowercase_							(     self       )     ->	Tuple:





         """simple docstring"""




         return MraConfig(
             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 ,				is_decoder=A_ ,				initializer_range=self.initializer_range ,				)
       def 							lowercase_							(     self       )     ->	Optional[Any]:





         """simple docstring"""




         _lowercase:			Optional[int]       =     self.get_config()
         _lowercase:			Any       =     300
         return config
       def 							lowercase_							(     self       )     ->	List[str]:





         """simple docstring"""




         (
             (
             _lowercase
         ) ,				(
             _lowercase
         ) ,				(
             _lowercase
         ) ,				(
             _lowercase
         ) ,				(
             _lowercase
         ) ,				(
             _lowercase
         ) ,				(
             _lowercase
         ) ,				
         ):			str       =     self.prepare_config_and_inputs()

         _lowercase:			List[str]       =     True
         _lowercase:			str       =     floats_tensor([self.batch_size, self.seq_length, self.hidden_size]       )
         _lowercase:			Dict       =     ids_tensor([self.batch_size, self.seq_length] ,				vocab_size=2       )

         return (
             config,
             input_ids,
             token_type_ids,
             input_mask,
             sequence_labels,
             token_labels,
             choice_labels,
             encoder_hidden_states,
             encoder_attention_mask,
         )
       def 							lowercase_							(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_       )     ->	Union[str, Any]:





         """simple docstring"""




         _lowercase:			Optional[int]       =     MraModel(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase:			Dict       =     model(A_ ,				attention_mask=A_ ,				token_type_ids=A_       )
         _lowercase:			List[str]       =     model(A_ ,				token_type_ids=A_       )
         _lowercase:			Union[str, Any]       =     model(A_       )
         self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.hidden_size)       )
       def 							lowercase_							(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				)     ->	Union[str, Any]:





         """simple docstring"""




         _lowercase:			List[Any]       =     True
         _lowercase:			str       =     MraModel(A_       )
         model.to(A_       )
         model.eval()
         _lowercase:			Optional[Any]       =     model(
             A_ ,				attention_mask=A_ ,				token_type_ids=A_ ,				encoder_hidden_states=A_ ,				encoder_attention_mask=A_ ,				)
         _lowercase:			int       =     model(
             A_ ,				attention_mask=A_ ,				token_type_ids=A_ ,				encoder_hidden_states=A_ ,				)
         _lowercase:			Tuple       =     model(A_ ,				attention_mask=A_ ,				token_type_ids=A_       )
         self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.hidden_size)       )
       def 							lowercase_							(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_       )     ->	Union[str, Any]:





         """simple docstring"""




         _lowercase:			str       =     MraForMaskedLM(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase:			Optional[int]       =     model(A_ ,				attention_mask=A_ ,				token_type_ids=A_ ,				labels=A_       )
         self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.vocab_size)       )
       def 							lowercase_							(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_       )     ->	Tuple:





         """simple docstring"""




         _lowercase:			Optional[Any]       =     MraForQuestionAnswering(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase:			Dict       =     model(
             A_ ,				attention_mask=A_ ,				token_type_ids=A_ ,				start_positions=A_ ,				end_positions=A_ ,				)
         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 ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_       )     ->	Dict:





         """simple docstring"""




         _lowercase:			List[Any]       =     self.num_labels
         _lowercase:			Optional[Any]       =     MraForSequenceClassification(A_       )
         model.to(A_       )
         model.eval()
         _lowercase:			str       =     model(A_ ,				attention_mask=A_ ,				token_type_ids=A_ ,				labels=A_       )
         self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)       )
       def 							lowercase_							(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_       )     ->	Dict:





         """simple docstring"""




         _lowercase:			str       =     self.num_labels
         _lowercase:			Optional[int]       =     MraForTokenClassification(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase:			Tuple       =     model(A_ ,				attention_mask=A_ ,				token_type_ids=A_ ,				labels=A_       )
         self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.num_labels)       )
       def 							lowercase_							(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ ,				A_       )     ->	str:





         """simple docstring"""




         _lowercase:			Optional[Any]       =     self.num_choices
         _lowercase:			int       =     MraForMultipleChoice(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase:			Dict       =     input_ids.unsqueeze(1       ).expand(-1 ,				self.num_choices ,				-1       ).contiguous()
         _lowercase:			Tuple       =     token_type_ids.unsqueeze(1       ).expand(-1 ,				self.num_choices ,				-1       ).contiguous()
         _lowercase:			Dict       =     input_mask.unsqueeze(1       ).expand(-1 ,				self.num_choices ,				-1       ).contiguous()
         _lowercase:			Dict       =     model(
             A_ ,				attention_mask=A_ ,				token_type_ids=A_ ,				labels=A_ ,				)
         self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_choices)       )



       def 							lowercase_							(     self       )     ->	str:





         """simple docstring"""




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


@require_torch
class 			__magic_name__       (       SCREAMING_SNAKE_CASE__   ,					unittest.TestCase ):
       UpperCamelCase_   =		(
           (
               MraModel,
               MraForMaskedLM,
               MraForMultipleChoice,
               MraForQuestionAnswering,
               MraForSequenceClassification,
               MraForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       UpperCamelCase_   =		False
       UpperCamelCase_   =		False
       UpperCamelCase_   =		False
       UpperCamelCase_   =		False

       UpperCamelCase_   =		()
       def 							lowercase_							(     self       )     ->	Any:





         """simple docstring"""




         _lowercase:			Union[str, Any]       =     MraModelTester(self       )
         _lowercase:			List[str]       =     ConfigTester(self ,				config_class=A_ ,				hidden_size=37       )
       def 							lowercase_							(     self       )     ->	str:





         """simple docstring"""




         self.config_tester.run_common_tests()
       def 							lowercase_							(     self       )     ->	str:





         """simple docstring"""




         _lowercase:			Union[str, Any]       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*A_       )
       def 							lowercase_							(     self       )     ->	List[str]:





         """simple docstring"""




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





         """simple docstring"""




         _lowercase:			List[Any]       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*A_       )
       def 							lowercase_							(     self       )     ->	Tuple:





         """simple docstring"""




         _lowercase:			Any       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*A_       )
       def 							lowercase_							(     self       )     ->	List[Any]:





         """simple docstring"""




         _lowercase:			Tuple       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*A_       )
       def 							lowercase_							(     self       )     ->	Optional[int]:





         """simple docstring"""




         _lowercase:			Optional[Any]       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*A_       )
       def 							lowercase_							(     self       )     ->	Optional[Any]:





         """simple docstring"""




         _lowercase:			Any       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*A_       )
       @slow
       def 							lowercase_							(     self       )     ->	Dict:





         """simple docstring"""




         for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowercase:			List[str]       =     MraModel.from_pretrained(A_       )
           self.assertIsNotNone(A_       )



       @unittest.skip(reason='''MRA does not output attentions'''       )
       def 							lowercase_							(     self       )     ->	List[str]:





         """simple docstring"""




         return


@require_torch
class 			__magic_name__       (       unittest.TestCase ):
       @slow
       def 							lowercase_							(     self       )     ->	Optional[int]:





         """simple docstring"""




         _lowercase:			Tuple       =     MraModel.from_pretrained('''uw-madison/mra-base-512-4'''       )
         _lowercase:			Dict       =     torch.arange(256       ).unsqueeze(0       )

         with torch.no_grad():
           _lowercase:			Optional[Any]       =     model(A_       )[0]

         _lowercase:			Any       =     torch.Size((1, 256, 768)       )
         self.assertEqual(output.shape ,				A_       )

         _lowercase:			Any       =     torch.tensor(
             [[[-0.01_40, 0.08_30, -0.03_81], [0.15_46, 0.14_02, 0.02_20], [0.11_62, 0.08_51, 0.01_65]]]       )

         self.assertTrue(torch.allclose(output[:, :3, :3] ,				A_ ,				atol=1E-4       )       )
       @slow
       def 							lowercase_							(     self       )     ->	Union[str, Any]:





         """simple docstring"""




         _lowercase:			Dict       =     MraForMaskedLM.from_pretrained('''uw-madison/mra-base-512-4'''       )
         _lowercase:			Optional[int]       =     torch.arange(256       ).unsqueeze(0       )

         with torch.no_grad():
           _lowercase:			Tuple       =     model(A_       )[0]

         _lowercase:			List[str]       =     5_0265

         _lowercase:			Any       =     torch.Size((1, 256, vocab_size)       )
         self.assertEqual(output.shape ,				A_       )

         _lowercase:			Optional[int]       =     torch.tensor(
             [[[9.25_95, -3.60_38, 11.88_19], [9.38_69, -3.26_93, 11.09_56], [11.85_24, -3.49_38, 13.12_10]]]       )

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



       @slow
       def 							lowercase_							(     self       )     ->	Tuple:





         """simple docstring"""




         _lowercase:			List[Any]       =     MraForMaskedLM.from_pretrained('''uw-madison/mra-base-4096-8-d3'''       )
         _lowercase:			str       =     torch.arange(4096       ).unsqueeze(0       )

         with torch.no_grad():
           _lowercase:			Optional[Any]       =     model(A_       )[0]

         _lowercase:			Tuple       =     5_0265

         _lowercase:			List[str]       =     torch.Size((1, 4096, vocab_size)       )
         self.assertEqual(output.shape ,				A_       )

         _lowercase:			Any       =     torch.tensor(
             [[[5.47_89, -2.35_64, 7.50_64], [7.90_67, -1.33_69, 9.96_68], [9.07_12, -1.81_06, 7.03_80]]]       )

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


"""simple docstring"""




def  _lowerCAmelCase		(		_UpperCamelCase						):






  """simple docstring"""



  if not isinstance(_UpperCamelCase ,				_UpperCamelCase						):
    raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''						)
  if len(_UpperCamelCase						) == 0:
    raise ValueError('''Input list must be a non empty list'''						)
  if len(_UpperCamelCase						) == 1:
    return True
  _lowercase:			Any       =     series[1] - series[0]
  for index in range(len(_UpperCamelCase						) - 1						):
    if series[index + 1] - series[index] != common_diff:
      return False
  return True




def  _lowerCAmelCase		(		_UpperCamelCase						):






  """simple docstring"""



  if not isinstance(_UpperCamelCase ,				_UpperCamelCase						):
    raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''						)
  if len(_UpperCamelCase						) == 0:
    raise ValueError('''Input list must be a non empty list'''						)
  _lowercase:			Tuple       =     0
  for val in series:
    answer += val
  return answer / len(_UpperCamelCase						)


if __name__ == "__main__":
  import doctest

  doctest.testmod()
