





'''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      snake_case     :




 """simple docstring"""



 def __init__(       self       :							str							,	__A       :							Tuple							,	__A       :							List[str]=2							,	__A       :							str=8							,	__A       :							int=True							,	__A       :							str=True							,	__A       :							Optional[int]=True							,	__A       :							Any=True							,	__A       :							Tuple=9_9							,	__A       :							Dict=1_6							,	__A       :							Optional[int]=5							,	__A       :							Any=2							,	__A       :							int=3_6							,	__A       :							List[Any]="gelu"							,	__A       :							str=0.0							,	__A       :							Dict=0.0							,	__A       :							List[str]=5_1_2							,	__A       :							Optional[int]=1_6							,	__A       :							Tuple=2							,	__A       :							Tuple=0.02							,	__A       :							int=3							,	__A       :							Tuple=4							,	__A       :							List[Any]=None							,	):
        __UpperCamelCase       =  parent
        __UpperCamelCase       =  batch_size
        __UpperCamelCase       =  seq_length
        __UpperCamelCase       =  is_training
        __UpperCamelCase       =  use_input_mask
        __UpperCamelCase       =  use_token_type_ids
        __UpperCamelCase       =  use_labels
        __UpperCamelCase       =  vocab_size
        __UpperCamelCase       =  hidden_size
        __UpperCamelCase       =  num_hidden_layers
        __UpperCamelCase       =  num_attention_heads
        __UpperCamelCase       =  intermediate_size
        __UpperCamelCase       =  hidden_act
        __UpperCamelCase       =  hidden_dropout_prob
        __UpperCamelCase       =  attention_probs_dropout_prob
        __UpperCamelCase       =  max_position_embeddings
        __UpperCamelCase       =  type_vocab_size
        __UpperCamelCase       =  type_sequence_label_size
        __UpperCamelCase       =  initializer_range
        __UpperCamelCase       =  num_labels
        __UpperCamelCase       =  num_choices
        __UpperCamelCase       =  scope






 def        _lowerCamelCase					(       self       :							str	):
        __UpperCamelCase       =  ids_tensor([self.batch_size, self.seq_length]							,	self.vocab_size	)

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

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

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

        __UpperCamelCase       =  self.get_config()

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






 def        _lowerCamelCase					(       self       :							Optional[Any]	):
        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=UpperCAmelCase__							,	initializer_range=self.initializer_range							,	)






 def        _lowerCamelCase					(       self       :							Union[str, Any]	):
        __UpperCamelCase       =  self.get_config()
        __UpperCamelCase       =  3_0_0
        return config






 def        _lowerCamelCase					(       self       :							List[str]	):
        (
            (
            __UpperCamelCase
        )       ,						(
            __UpperCamelCase
        )       ,						(
            __UpperCamelCase
        )       ,						(
            __UpperCamelCase
        )       ,						(
            __UpperCamelCase
        )       ,						(
            __UpperCamelCase
        )       ,						(
            __UpperCamelCase
        )       ,						
        )       =  self.prepare_config_and_inputs()

        __UpperCamelCase       =  True
        __UpperCamelCase       =  floats_tensor([self.batch_size, self.seq_length, self.hidden_size]	)
        __UpperCamelCase       =  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        _lowerCamelCase					(       self       :							List[Any]							,	__A       :							Dict							,	__A       :							Optional[int]							,	__A       :							List[Any]							,	__A       :							Tuple							,	__A       :							Dict							,	__A       :							Dict							,	__A       :							Dict	):
        __UpperCamelCase       =  MraModel(config=UpperCAmelCase__	)
        model.to(UpperCAmelCase__	)
        model.eval()
        __UpperCamelCase       =  model(UpperCAmelCase__							,	attention_mask=UpperCAmelCase__							,	token_type_ids=UpperCAmelCase__	)
        __UpperCamelCase       =  model(UpperCAmelCase__							,	token_type_ids=UpperCAmelCase__	)
        __UpperCamelCase       =  model(UpperCAmelCase__	)
        self.parent.assertEqual(result.last_hidden_state.shape							,	(self.batch_size, self.seq_length, self.hidden_size)	)






 def        _lowerCamelCase					(       self       :							Tuple							,	__A       :							Union[str, Any]							,	__A       :							Any							,	__A       :							List[Any]							,	__A       :							Union[str, Any]							,	__A       :							Optional[Any]							,	__A       :							Optional[Any]							,	__A       :							Tuple							,	__A       :							Tuple							,	__A       :							Optional[Any]							,	):
        __UpperCamelCase       =  True
        __UpperCamelCase       =  MraModel(UpperCAmelCase__	)
        model.to(UpperCAmelCase__	)
        model.eval()
        __UpperCamelCase       =  model(
            UpperCAmelCase__							,	attention_mask=UpperCAmelCase__							,	token_type_ids=UpperCAmelCase__							,	encoder_hidden_states=UpperCAmelCase__							,	encoder_attention_mask=UpperCAmelCase__							,	)
        __UpperCamelCase       =  model(
            UpperCAmelCase__							,	attention_mask=UpperCAmelCase__							,	token_type_ids=UpperCAmelCase__							,	encoder_hidden_states=UpperCAmelCase__							,	)
        __UpperCamelCase       =  model(UpperCAmelCase__							,	attention_mask=UpperCAmelCase__							,	token_type_ids=UpperCAmelCase__	)
        self.parent.assertEqual(result.last_hidden_state.shape							,	(self.batch_size, self.seq_length, self.hidden_size)	)






 def        _lowerCamelCase					(       self       :							Dict							,	__A       :							Union[str, Any]							,	__A       :							str							,	__A       :							Dict							,	__A       :							int							,	__A       :							Dict							,	__A       :							str							,	__A       :							int	):
        __UpperCamelCase       =  MraForMaskedLM(config=UpperCAmelCase__	)
        model.to(UpperCAmelCase__	)
        model.eval()
        __UpperCamelCase       =  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        _lowerCamelCase					(       self       :							str							,	__A       :							Dict							,	__A       :							Any							,	__A       :							Optional[Any]							,	__A       :							int							,	__A       :							Tuple							,	__A       :							str							,	__A       :							List[str]	):
        __UpperCamelCase       =  MraForQuestionAnswering(config=UpperCAmelCase__	)
        model.to(UpperCAmelCase__	)
        model.eval()
        __UpperCamelCase       =  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        _lowerCamelCase					(       self       :							str							,	__A       :							Optional[int]							,	__A       :							Any							,	__A       :							List[str]							,	__A       :							List[Any]							,	__A       :							Union[str, Any]							,	__A       :							Optional[int]							,	__A       :							Tuple	):
        __UpperCamelCase       =  self.num_labels
        __UpperCamelCase       =  MraForSequenceClassification(UpperCAmelCase__	)
        model.to(UpperCAmelCase__	)
        model.eval()
        __UpperCamelCase       =  model(UpperCAmelCase__							,	attention_mask=UpperCAmelCase__							,	token_type_ids=UpperCAmelCase__							,	labels=UpperCAmelCase__	)
        self.parent.assertEqual(result.logits.shape							,	(self.batch_size, self.num_labels)	)






 def        _lowerCamelCase					(       self       :							Optional[int]							,	__A       :							int							,	__A       :							Tuple							,	__A       :							List[Any]							,	__A       :							List[Any]							,	__A       :							List[str]							,	__A       :							Optional[int]							,	__A       :							Optional[Any]	):
        __UpperCamelCase       =  self.num_labels
        __UpperCamelCase       =  MraForTokenClassification(config=UpperCAmelCase__	)
        model.to(UpperCAmelCase__	)
        model.eval()
        __UpperCamelCase       =  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        _lowerCamelCase					(       self       :							Optional[Any]							,	__A       :							Any							,	__A       :							int							,	__A       :							Dict							,	__A       :							int							,	__A       :							Union[str, Any]							,	__A       :							Tuple							,	__A       :							Tuple	):
        __UpperCamelCase       =  self.num_choices
        __UpperCamelCase       =  MraForMultipleChoice(config=UpperCAmelCase__	)
        model.to(UpperCAmelCase__	)
        model.eval()
        __UpperCamelCase       =  input_ids.unsqueeze(1	).expand(-1							,	self.num_choices							,	-1	).contiguous()
        __UpperCamelCase       =  token_type_ids.unsqueeze(1	).expand(-1							,	self.num_choices							,	-1	).contiguous()
        __UpperCamelCase       =  input_mask.unsqueeze(1	).expand(-1							,	self.num_choices							,	-1	).contiguous()
        __UpperCamelCase       =  model(
            UpperCAmelCase__							,	attention_mask=UpperCAmelCase__							,	token_type_ids=UpperCAmelCase__							,	labels=UpperCAmelCase__							,	)
        self.parent.assertEqual(result.logits.shape							,	(self.batch_size, self.num_choices)	)






 def        _lowerCamelCase					(       self       :							Union[str, Any]	):
        __UpperCamelCase       =  self.prepare_config_and_inputs()
        (
            (
            __UpperCamelCase
        )       ,						(
            __UpperCamelCase
        )       ,						(
            __UpperCamelCase
        )       ,						(
            __UpperCamelCase
        )       ,						(
            __UpperCamelCase
        )       ,						(
            __UpperCamelCase
        )       ,						(
            __UpperCamelCase
        )       ,						
        )       =  config_and_inputs
        __UpperCamelCase       =  {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
        return config, inputs_dict






@require_torch
class      snake_case     (    __lowerCamelCase			,       unittest.TestCase       ):




 """simple docstring"""



 SCREAMING_SNAKE_CASE_	:					Optional[int]					=(
     (
         MraModel,
         MraForMaskedLM,
         MraForMultipleChoice,
         MraForQuestionAnswering,
         MraForSequenceClassification,
         MraForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 SCREAMING_SNAKE_CASE_	:					Any					=False
 SCREAMING_SNAKE_CASE_	:					List[Any]					=False
 SCREAMING_SNAKE_CASE_	:					Union[str, Any]					=False
 SCREAMING_SNAKE_CASE_	:					List[Any]					=False

 SCREAMING_SNAKE_CASE_	:					List[Any]					=()






 def        _lowerCamelCase					(       self       :							List[Any]	):
        __UpperCamelCase       =  MraModelTester(self	)
        __UpperCamelCase       =  ConfigTester(self							,	config_class=UpperCAmelCase__							,	hidden_size=3_7	)






 def        _lowerCamelCase					(       self       :							Any	):
        self.config_tester.run_common_tests()






 def        _lowerCamelCase					(       self       :							Union[str, Any]	):
        __UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*UpperCAmelCase__	)






 def        _lowerCamelCase					(       self       :							int	):
        __UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
               __UpperCamelCase       =  type
               self.model_tester.create_and_check_model(*UpperCAmelCase__	)






 def        _lowerCamelCase					(       self       :							Tuple	):
        __UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase__	)






 def        _lowerCamelCase					(       self       :							Tuple	):
        __UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*UpperCAmelCase__	)






 def        _lowerCamelCase					(       self       :							Tuple	):
        __UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase__	)






 def        _lowerCamelCase					(       self       :							List[str]	):
        __UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase__	)






 def        _lowerCamelCase					(       self       :							int	):
        __UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase__	)






 @slow
 def        _lowerCamelCase					(       self       :							Any	):
        for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __UpperCamelCase       =  MraModel.from_pretrained(UpperCAmelCase__	)
               self.assertIsNotNone(UpperCAmelCase__	)






 @unittest.skip(reason='MRA does not output attentions'	)
 def        _lowerCamelCase					(       self       :							Union[str, Any]	):
        return






@require_torch
class      snake_case     (    unittest.TestCase       ):




 """simple docstring"""



 @slow
 def        _lowerCamelCase					(       self       :							Tuple	):
        __UpperCamelCase       =  MraModel.from_pretrained('uw-madison/mra-base-512-4'	)
        __UpperCamelCase       =  torch.arange(2_5_6	).unsqueeze(0	)

        with torch.no_grad():
               __UpperCamelCase       =  model(UpperCAmelCase__	)[0]

        __UpperCamelCase       =  torch.Size((1, 2_5_6, 7_6_8)	)
        self.assertEqual(output.shape							,	UpperCAmelCase__	)

        __UpperCamelCase       =  torch.tensor(
            [[[-0.0140, 0.0830, -0.0381], [0.1546, 0.1402, 0.0220], [0.1162, 0.0851, 0.0165]]]	)

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






 @slow
 def        _lowerCamelCase					(       self       :							Union[str, Any]	):
        __UpperCamelCase       =  MraForMaskedLM.from_pretrained('uw-madison/mra-base-512-4'	)
        __UpperCamelCase       =  torch.arange(2_5_6	).unsqueeze(0	)

        with torch.no_grad():
               __UpperCamelCase       =  model(UpperCAmelCase__	)[0]

        __UpperCamelCase       =  5_0_2_6_5

        __UpperCamelCase       =  torch.Size((1, 2_5_6, vocab_size)	)
        self.assertEqual(output.shape							,	UpperCAmelCase__	)

        __UpperCamelCase       =  torch.tensor(
            [[[9.2595, -3.6038, 11.8819], [9.3869, -3.2693, 11.0956], [11.8524, -3.4938, 13.1210]]]	)

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






 @slow
 def        _lowerCamelCase					(       self       :							Tuple	):
        __UpperCamelCase       =  MraForMaskedLM.from_pretrained('uw-madison/mra-base-4096-8-d3'	)
        __UpperCamelCase       =  torch.arange(4_0_9_6	).unsqueeze(0	)

        with torch.no_grad():
               __UpperCamelCase       =  model(UpperCAmelCase__	)[0]

        __UpperCamelCase       =  5_0_2_6_5

        __UpperCamelCase       =  torch.Size((1, 4_0_9_6, vocab_size)	)
        self.assertEqual(output.shape							,	UpperCAmelCase__	)

        __UpperCamelCase       =  torch.tensor(
            [[[5.4789, -2.3564, 7.5064], [7.9067, -1.3369, 9.9668], [9.0712, -1.8106, 7.0380]]]	)

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



"""simple docstring"""





import baseaa




def   _A  (  UpperCamelCase_     :       str)	->						bytes:
   '''simple docstring'''


   return baseaa.baaencode(string.encode("utf-8"))




def   _A  (  UpperCamelCase_     :       bytes)	->						str:
   '''simple docstring'''


   return baseaa.baadecode(UpperCamelCase_).decode("utf-8")


if __name__ == "__main__":
   _a       =							'Hello World!'
   _a       =							baseaa_encode(test)
   print(encoded)

   _a       =							baseaa_decode(encoded)
   print(decoded)




