




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 		UpperCamelCase__	:

    '''simple docstring'''



    def __init__(    self     ,  UpperCamelCase__     ,  UpperCamelCase__=2     ,  UpperCamelCase__=8     ,  UpperCamelCase__=True     ,  UpperCamelCase__=True     ,  UpperCamelCase__=True     ,  UpperCamelCase__=True     ,  UpperCamelCase__=99     ,  UpperCamelCase__=16     ,  UpperCamelCase__=5     ,  UpperCamelCase__=2     ,  UpperCamelCase__=36     ,  UpperCamelCase__="gelu"     ,  UpperCamelCase__=0.0     ,  UpperCamelCase__=0.0     ,  UpperCamelCase__=512     ,  UpperCamelCase__=16     ,  UpperCamelCase__=2     ,  UpperCamelCase__=0.02     ,  UpperCamelCase__=3     ,  UpperCamelCase__=4     ,  UpperCamelCase__=None     ,  )     ->      Union[str, Any]:
           lowerCamelCase   :      Tuple			 =					parent
           lowerCamelCase   :      Optional[Any]			 =					batch_size
           lowerCamelCase   :      Dict			 =					seq_length
           lowerCamelCase   :      int			 =					is_training
           lowerCamelCase   :      Dict			 =					use_input_mask
           lowerCamelCase   :      Optional[Any]			 =					use_token_type_ids
           lowerCamelCase   :      Optional[int]			 =					use_labels
           lowerCamelCase   :      List[Any]			 =					vocab_size
           lowerCamelCase   :      List[Any]			 =					hidden_size
           lowerCamelCase   :      Dict			 =					num_hidden_layers
           lowerCamelCase   :      Tuple			 =					num_attention_heads
           lowerCamelCase   :      Union[str, Any]			 =					intermediate_size
           lowerCamelCase   :      List[Any]			 =					hidden_act
           lowerCamelCase   :      Tuple			 =					hidden_dropout_prob
           lowerCamelCase   :      List[str]			 =					attention_probs_dropout_prob
           lowerCamelCase   :      List[str]			 =					max_position_embeddings
           lowerCamelCase   :      int			 =					type_vocab_size
           lowerCamelCase   :      Any			 =					type_sequence_label_size
           lowerCamelCase   :      List[str]			 =					initializer_range
           lowerCamelCase   :      Dict			 =					num_labels
           lowerCamelCase   :      Optional[int]			 =					num_choices
           lowerCamelCase   :      Any			 =					scope







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

           lowerCamelCase   :      Optional[Any]			 =					None
           if self.use_input_mask:
                  lowerCamelCase   :      str			 =					random_attention_mask([self.batch_size, self.seq_length]     )

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

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

           lowerCamelCase   :      Any			 =					self.get_config()

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







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







    def     _lowercase     (    self     )     ->      Optional[int]:
           (
               (
               lowerCamelCase
           ) ,      (
               lowerCamelCase
           ) ,      (
               lowerCamelCase
           ) ,      (
               lowerCamelCase
           ) ,      (
               lowerCamelCase
           ) ,      (
               lowerCamelCase
           ) ,      (
               lowerCamelCase
           ) ,      
           )   :      List[str]			 =					self.prepare_config_and_inputs()

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







    def     _lowercase     (    self     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  )     ->      Union[str, Any]:
           lowerCamelCase   :      Union[str, Any]			 =					True
           lowerCamelCase   :      Dict			 =					MraModel(UpperCamelCase__     )
           model.to(UpperCamelCase__     )
           model.eval()
           lowerCamelCase   :      Optional[int]			 =					model(
               UpperCamelCase__     ,  attention_mask=UpperCamelCase__     ,  token_type_ids=UpperCamelCase__     ,  encoder_hidden_states=UpperCamelCase__     ,  encoder_attention_mask=UpperCamelCase__     ,  )
           lowerCamelCase   :      List[Any]			 =					model(
               UpperCamelCase__     ,  attention_mask=UpperCamelCase__     ,  token_type_ids=UpperCamelCase__     ,  encoder_hidden_states=UpperCamelCase__     ,  )
           lowerCamelCase   :      List[Any]			 =					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     _lowercase     (    self     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     )     ->      Tuple:
           lowerCamelCase   :      Tuple			 =					MraForMaskedLM(config=UpperCamelCase__     )
           model.to(UpperCamelCase__     )
           model.eval()
           lowerCamelCase   :      Union[str, Any]			 =					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__     )     ->      int:
           lowerCamelCase   :      str			 =					MraForQuestionAnswering(config=UpperCamelCase__     )
           model.to(UpperCamelCase__     )
           model.eval()
           lowerCamelCase   :      int			 =					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__     )     ->      List[str]:
           lowerCamelCase   :      str			 =					self.num_labels
           lowerCamelCase   :      Union[str, Any]			 =					MraForSequenceClassification(UpperCamelCase__     )
           model.to(UpperCamelCase__     )
           model.eval()
           lowerCamelCase   :      Dict			 =					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__     )     ->      Union[str, Any]:
           lowerCamelCase   :      int			 =					self.num_labels
           lowerCamelCase   :      str			 =					MraForTokenClassification(config=UpperCamelCase__     )
           model.to(UpperCamelCase__     )
           model.eval()
           lowerCamelCase   :      List[str]			 =					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__     )     ->      Any:
           lowerCamelCase   :      Any			 =					self.num_choices
           lowerCamelCase   :      Dict			 =					MraForMultipleChoice(config=UpperCamelCase__     )
           model.to(UpperCamelCase__     )
           model.eval()
           lowerCamelCase   :      int			 =					input_ids.unsqueeze(1     ).expand(-1     ,  self.num_choices     ,  -1     ).contiguous()
           lowerCamelCase   :      Any			 =					token_type_ids.unsqueeze(1     ).expand(-1     ,  self.num_choices     ,  -1     ).contiguous()
           lowerCamelCase   :      List[str]			 =					input_mask.unsqueeze(1     ).expand(-1     ,  self.num_choices     ,  -1     ).contiguous()
           lowerCamelCase   :      Optional[int]			 =					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     )     ->      Optional[Any]:
           lowerCamelCase   :      Optional[int]			 =					self.prepare_config_and_inputs()
           (
               (
               lowerCamelCase
           ) ,      (
               lowerCamelCase
           ) ,      (
               lowerCamelCase
           ) ,      (
               lowerCamelCase
           ) ,      (
               lowerCamelCase
           ) ,      (
               lowerCamelCase
           ) ,      (
               lowerCamelCase
           ) ,      
           )   :      Tuple			 =					config_and_inputs
           lowerCamelCase   :      Optional[Any]			 =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
           return config, inputs_dict




@require_torch
class 		UpperCamelCase__	(lowerCAmelCase__    ,				unittest.TestCase     ):

    '''simple docstring'''



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

    lowerCamelCase_      :	Optional[int]	     =      ()







    def     _lowercase     (    self     )     ->      Union[str, Any]:
           lowerCamelCase   :      Optional[Any]			 =					MraModelTester(self     )
           lowerCamelCase   :      Union[str, Any]			 =					ConfigTester(self     ,  config_class=UpperCamelCase__     ,  hidden_size=37     )







    def     _lowercase     (    self     )     ->      Optional[int]:
           self.config_tester.run_common_tests()







    def     _lowercase     (    self     )     ->      Tuple:
           lowerCamelCase   :      Any			 =					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCamelCase__     )







    def     _lowercase     (    self     )     ->      List[str]:
           lowerCamelCase   :      Tuple			 =					self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                  lowerCamelCase   :      Optional[Any]			 =					type
                  self.model_tester.create_and_check_model(*UpperCamelCase__     )







    def     _lowercase     (    self     )     ->      Dict:
           lowerCamelCase   :      Tuple			 =					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase__     )







    def     _lowercase     (    self     )     ->      Union[str, Any]:
           lowerCamelCase   :      Tuple			 =					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase__     )







    def     _lowercase     (    self     )     ->      Tuple:
           lowerCamelCase   :      List[str]			 =					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__     )







    def     _lowercase     (    self     )     ->      List[Any]:
           lowerCamelCase   :      List[Any]			 =					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__     )







    def     _lowercase     (    self     )     ->      int:
           lowerCamelCase   :      Union[str, Any]			 =					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__     )







    @slow
    def     _lowercase     (    self     )     ->      int:
           for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  lowerCamelCase   :      int			 =					MraModel.from_pretrained(UpperCamelCase__     )
                  self.assertIsNotNone(UpperCamelCase__     )







    @unittest.skip(reason="MRA does not output attentions"     )
    def     _lowercase     (    self     )     ->      Optional[int]:
           return




@require_torch
class 		UpperCamelCase__	(unittest.TestCase     ):

    '''simple docstring'''



    @slow
    def     _lowercase     (    self     )     ->      int:
           lowerCamelCase   :      Dict			 =					MraModel.from_pretrained("uw-madison/mra-base-512-4"     )
           lowerCamelCase   :      Dict			 =					torch.arange(256     ).unsqueeze(0     )

           with torch.no_grad():
                  lowerCamelCase   :      Dict			 =					model(UpperCamelCase__     )[0]

           lowerCamelCase   :      str			 =					torch.Size((1, 256, 768)     )
           self.assertEqual(output.shape     ,  UpperCamelCase__     )

           lowerCamelCase   :      List[str]			 =					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     _lowercase     (    self     )     ->      Optional[Any]:
           lowerCamelCase   :      Any			 =					MraForMaskedLM.from_pretrained("uw-madison/mra-base-512-4"     )
           lowerCamelCase   :      List[str]			 =					torch.arange(256     ).unsqueeze(0     )

           with torch.no_grad():
                  lowerCamelCase   :      Optional[int]			 =					model(UpperCamelCase__     )[0]

           lowerCamelCase   :      Union[str, Any]			 =					5_0265

           lowerCamelCase   :      int			 =					torch.Size((1, 256, vocab_size)     )
           self.assertEqual(output.shape     ,  UpperCamelCase__     )

           lowerCamelCase   :      List[str]			 =					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     _lowercase     (    self     )     ->      List[str]:
           lowerCamelCase   :      List[str]			 =					MraForMaskedLM.from_pretrained("uw-madison/mra-base-4096-8-d3"     )
           lowerCamelCase   :      Optional[int]			 =					torch.arange(4096     ).unsqueeze(0     )

           with torch.no_grad():
                  lowerCamelCase   :      Any			 =					model(UpperCamelCase__     )[0]

           lowerCamelCase   :      int			 =					5_0265

           lowerCamelCase   :      Optional[Any]			 =					torch.Size((1, 4096, vocab_size)     )
           self.assertEqual(output.shape     ,  UpperCamelCase__     )

           lowerCamelCase   :      Any			 =					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     )     )












from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


SCREAMING_SNAKE_CASE__       :      List[Any]  =   {'processing_layoutxlm': ['LayoutXLMProcessor']}

try:
       if not is_sentencepiece_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       SCREAMING_SNAKE_CASE__       :      Optional[Any]  =   ['LayoutXLMTokenizer']

try:
       if not is_tokenizers_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       SCREAMING_SNAKE_CASE__       :      Dict  =   ['LayoutXLMTokenizerFast']

if TYPE_CHECKING:
       from .processing_layoutxlm import LayoutXLMProcessor

       try:
              if not is_sentencepiece_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_layoutxlm import LayoutXLMTokenizer

       try:
              if not is_tokenizers_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_layoutxlm_fast import LayoutXLMTokenizerFast

else:
       import sys

       SCREAMING_SNAKE_CASE__       :      str  =   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)






