
"""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       =     parent
         _lowercase       =     batch_size
         _lowercase       =     seq_length
         _lowercase       =     is_training
         _lowercase       =     use_input_mask
         _lowercase       =     use_token_type_ids
         _lowercase       =     use_labels
         _lowercase       =     vocab_size
         _lowercase       =     hidden_size
         _lowercase       =     num_hidden_layers
         _lowercase       =     num_attention_heads
         _lowercase       =     intermediate_size
         _lowercase       =     hidden_act
         _lowercase       =     hidden_dropout_prob
         _lowercase       =     attention_probs_dropout_prob
         _lowercase       =     max_position_embeddings
         _lowercase       =     type_vocab_size
         _lowercase       =     type_sequence_label_size
         _lowercase       =     initializer_range
         _lowercase       =     num_labels
         _lowercase       =     num_choices
         _lowercase       =     scope
       def 							lowercase_							(     self       )     ->	List[Any]:





         """simple docstring"""




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

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

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

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

         _lowercase       =     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       =     self.get_config()
         _lowercase       =     300
         return config
       def 							lowercase_							(     self       )     ->	List[str]:





         """simple docstring"""




         (
             _lowercase				
         )       =     self.prepare_config_and_inputs()

         _lowercase       =     True
         _lowercase       =     floats_tensor([self.batch_size, self.seq_length, self.hidden_size]       )
         _lowercase       =     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       =     MraModel(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase       =     model(A_ ,				attention_mask=A_ ,				token_type_ids=A_       )
         _lowercase       =     model(A_ ,				token_type_ids=A_       )
         _lowercase       =     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       =     True
         _lowercase       =     MraModel(A_       )
         model.to(A_       )
         model.eval()
         _lowercase       =     model(
             A_ ,				attention_mask=A_ ,				token_type_ids=A_ ,				encoder_hidden_states=A_ ,				encoder_attention_mask=A_ ,				)
         _lowercase       =     model(
             A_ ,				attention_mask=A_ ,				token_type_ids=A_ ,				encoder_hidden_states=A_ ,				)
         _lowercase       =     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       =     MraForMaskedLM(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase       =     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       =     MraForQuestionAnswering(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase       =     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       =     self.num_labels
         _lowercase       =     MraForSequenceClassification(A_       )
         model.to(A_       )
         model.eval()
         _lowercase       =     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       =     self.num_labels
         _lowercase       =     MraForTokenClassification(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase       =     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       =     self.num_choices
         _lowercase       =     MraForMultipleChoice(config=A_       )
         model.to(A_       )
         model.eval()
         _lowercase       =     input_ids.unsqueeze(1       ).expand(-1 ,				self.num_choices ,				-1       ).contiguous()
         _lowercase       =     token_type_ids.unsqueeze(1       ).expand(-1 ,				self.num_choices ,				-1       ).contiguous()
         _lowercase       =     input_mask.unsqueeze(1       ).expand(-1 ,				self.num_choices ,				-1       ).contiguous()
         _lowercase       =     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       =     self.prepare_config_and_inputs()
         (
             _lowercase				
         )       =     config_and_inputs
         _lowercase       =     {'''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       =     MraModelTester(self       )
         _lowercase       =     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       =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*A_       )
       def 							lowercase_							(     self       )     ->	List[str]:





         """simple docstring"""




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





         """simple docstring"""




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





         """simple docstring"""




         _lowercase       =     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       =     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       =     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       =     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       =     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       =     MraModel.from_pretrained('''uw-madison/mra-base-512-4'''       )
         _lowercase       =     torch.arange(256       ).unsqueeze(0       )

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

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

         _lowercase       =     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       =     MraForMaskedLM.from_pretrained('''uw-madison/mra-base-512-4'''       )
         _lowercase       =     torch.arange(256       ).unsqueeze(0       )

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

         _lowercase       =     5_0265

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

         _lowercase       =     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       =     MraForMaskedLM.from_pretrained('''uw-madison/mra-base-4096-8-d3'''       )
         _lowercase       =     torch.arange(4096       ).unsqueeze(0       )

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

         _lowercase       =     5_0265

         _lowercase       =     torch.Size((1, 4096, vocab_size)       )
         self.assertEqual(output.shape ,				A_       )

         _lowercase       =     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"""




import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


A__   :					Any							=   'hf-internal-testing/tiny-random-bert'
A__   :					int							=   os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
A__   :					Dict							=   '9b8c223d42b2188cb49d29af482996f9d0f3e5a6'


class 			__magic_name__       (       unittest.TestCase ):
       def 							lowercase_							(     self       )     ->	str:





         """simple docstring"""




         _lowercase:			Dict       =     cached_file(A_ ,				A_       )
         # Should have downloaded the file in here
         self.assertTrue(os.path.isdir(A_       )       )
         # Cache should contain at least those three subfolders:
         for subfolder in ["blobs", "refs", "snapshots"]:
           self.assertTrue(os.path.isdir(os.path.join(A_ ,				A_       )       )       )
         with open(os.path.join(A_ ,				'''refs''' ,				'''main'''       )       ) as f:
           _lowercase:			Tuple       =     f.read()
         self.assertEqual(A_ ,				os.path.join(A_ ,				'''snapshots''' ,				A_ ,				A_       )       )
         self.assertTrue(os.path.isfile(A_       )       )

         # File is cached at the same place the second time.
         _lowercase:			List[Any]       =     cached_file(A_ ,				A_       )
         self.assertEqual(A_ ,				A_       )

         # Using a specific revision to test the full commit hash.
         _lowercase:			Union[str, Any]       =     cached_file(A_ ,				A_ ,				revision='''9b8c223'''       )
         self.assertEqual(A_ ,				os.path.join(A_ ,				'''snapshots''' ,				A_ ,				A_       )       )
       def 							lowercase_							(     self       )     ->	Optional[Any]:





         """simple docstring"""




         with self.assertRaisesRegex(A_ ,				'''is not a valid model identifier'''       ):
           _lowercase:			Union[str, Any]       =     cached_file('''tiny-random-bert''' ,				A_       )

         with self.assertRaisesRegex(A_ ,				'''is not a valid git identifier'''       ):
           _lowercase:			List[Any]       =     cached_file(A_ ,				A_ ,				revision='''aaaa'''       )

         with self.assertRaisesRegex(A_ ,				'''does not appear to have a file named'''       ):
           _lowercase:			int       =     cached_file(A_ ,				'''conf'''       )
       def 							lowercase_							(     self       )     ->	Optional[int]:





         """simple docstring"""




         with self.assertRaisesRegex(A_ ,				'''does not appear to have a file named'''       ):
           _lowercase:			Dict       =     cached_file(A_ ,				'''conf'''       )

         with open(os.path.join(A_ ,				'''refs''' ,				'''main'''       )       ) as f:
           _lowercase:			int       =     f.read()
         self.assertTrue(os.path.isfile(os.path.join(A_ ,				'''.no_exist''' ,				A_ ,				'''conf'''       )       )       )

         _lowercase:			Optional[int]       =     cached_file(A_ ,				'''conf''' ,				_raise_exceptions_for_missing_entries=A_       )
         self.assertIsNone(A_       )

         _lowercase:			Optional[int]       =     cached_file(A_ ,				'''conf''' ,				local_files_only=A_ ,				_raise_exceptions_for_missing_entries=A_       )
         self.assertIsNone(A_       )

         _lowercase:			Optional[Any]       =     mock.Mock()
         _lowercase:			Optional[Any]       =     500
         _lowercase:			Optional[Any]       =     {}
         _lowercase:			Dict       =     HTTPError
         _lowercase:			Optional[Any]       =     {}

         # Under the mock environment we get a 500 error when trying to reach the tokenizer.
         with mock.patch('''requests.Session.request''' ,				return_value=A_       ) as mock_head:
           _lowercase:			Tuple       =     cached_file(A_ ,				'''conf''' ,				_raise_exceptions_for_connection_errors=A_       )
           self.assertIsNone(A_       )
           # This check we did call the fake head request
           mock_head.assert_called()
       def 							lowercase_							(     self       )     ->	str:





         """simple docstring"""




         self.assertTrue(has_file('''hf-internal-testing/tiny-bert-pt-only''' ,				A_       )       )
         self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' ,				A_       )       )
         self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' ,				A_       )       )
       def 							lowercase_							(     self       )     ->	Tuple:





         """simple docstring"""




         self.assertIsNone(get_file_from_repo('''bert-base-cased''' ,				'''ahah.txt'''       )       )

         # The function raises if the repository does not exist.
         with self.assertRaisesRegex(A_ ,				'''is not a valid model identifier'''       ):
           get_file_from_repo('''bert-base-case''' ,				A_       )

         # The function raises if the revision does not exist.
         with self.assertRaisesRegex(A_ ,				'''is not a valid git identifier'''       ):
           get_file_from_repo('''bert-base-cased''' ,				A_ ,				revision='''ahaha'''       )

         _lowercase:			Optional[int]       =     get_file_from_repo('''bert-base-cased''' ,				A_       )
         # The name is the cached name which is not very easy to test, so instead we load the content.
         _lowercase:			Union[str, Any]       =     json.loads(open(A_ ,				'''r'''       ).read()       )
         self.assertEqual(config['''hidden_size'''] ,				768       )



       def 							lowercase_							(     self       )     ->	Tuple:





         """simple docstring"""




         with tempfile.TemporaryDirectory() as tmp_dir:
           _lowercase:			List[Any]       =     Path(A_       ) / '''a.txt'''
           filename.touch()
           self.assertEqual(get_file_from_repo(A_ ,				'''a.txt'''       ) ,				str(A_       )       )

           self.assertIsNone(get_file_from_repo(A_ ,				'''b.txt'''       )       )
