


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


     """simple docstring"""






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

     def 			lowerCamelCase__			(    self					:		Optional[Any]      )						->							Tuple:
          __UpperCAmelCase     :			List[Any]  							=      ids_tensor([self.batch_size, self.seq_length]							,			self.vocab_size      )

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

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

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

          __UpperCAmelCase     :			Dict  							=      self.get_config()

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

     def 			lowerCamelCase__			(    self					:		List[str]      )						->							Optional[int]:
          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=snake_case							,			initializer_range=self.initializer_range							,			)

     def 			lowerCamelCase__			(    self					:		Any      )						->							Union[str, Any]:
          __UpperCAmelCase     :			Optional[Any]  							=      self.get_config()
          __UpperCAmelCase     :			Optional[int]  							=      300
          return config

     def 			lowerCamelCase__			(    self					:		List[Any]      )						->							int:
          (
              (
              __UpperCAmelCase
          )							,       (
              __UpperCAmelCase
          )							,       (
              __UpperCAmelCase
          )							,       (
              __UpperCAmelCase
          )							,       (
              __UpperCAmelCase
          )							,       (
              __UpperCAmelCase
          )							,       (
              __UpperCAmelCase
          )							,       
          )     :			Optional[Any]  							=      self.prepare_config_and_inputs()

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

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

     def 			lowerCamelCase__			(    self					:		Union[str, Any]							,			snake_case					:		List[Any]							,			snake_case					:		Optional[int]							,			snake_case					:		Optional[int]							,			snake_case					:		Tuple							,			snake_case					:		str							,			snake_case					:		Optional[Any]							,			snake_case					:		List[str]      )						->							Tuple:
          __UpperCAmelCase     :			List[str]  							=      MraForMaskedLM(config=snake_case      )
          model.to(snake_case      )
          model.eval()
          __UpperCAmelCase     :			List[str]  							=      model(snake_case							,			attention_mask=snake_case							,			token_type_ids=snake_case							,			labels=snake_case      )
          self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.seq_length, self.vocab_size)      )

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

     def 			lowerCamelCase__			(    self					:		Dict							,			snake_case					:		Dict							,			snake_case					:		List[Any]							,			snake_case					:		int							,			snake_case					:		List[Any]							,			snake_case					:		Dict							,			snake_case					:		Any							,			snake_case					:		int      )						->							Optional[int]:
          __UpperCAmelCase     :			Tuple  							=      self.num_labels
          __UpperCAmelCase     :			Union[str, Any]  							=      MraForTokenClassification(config=snake_case      )
          model.to(snake_case      )
          model.eval()
          __UpperCAmelCase     :			str  							=      model(snake_case							,			attention_mask=snake_case							,			token_type_ids=snake_case							,			labels=snake_case      )
          self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.seq_length, self.num_labels)      )

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






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



@require_torch
class      a    (		_a     ,  unittest.TestCase	):


     """simple docstring"""






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

     SCREAMING_SNAKE_CASE	:			str							       =       ()

     def 			lowerCamelCase__			(    self					:		Any      )						->							Optional[int]:
          __UpperCAmelCase     :			List[str]  							=      MraModelTester(self      )
          __UpperCAmelCase     :			Optional[int]  							=      ConfigTester(self							,			config_class=snake_case							,			hidden_size=37      )

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

     def 			lowerCamelCase__			(    self					:		Dict      )						->							Union[str, Any]:
          __UpperCAmelCase     :			int  							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*snake_case      )

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

     def 			lowerCamelCase__			(    self					:		int      )						->							int:
          __UpperCAmelCase     :			Tuple  							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*snake_case      )

     def 			lowerCamelCase__			(    self					:		Optional[int]      )						->							int:
          __UpperCAmelCase     :			Optional[Any]  							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*snake_case      )

     def 			lowerCamelCase__			(    self					:		Optional[Any]      )						->							List[Any]:
          __UpperCAmelCase     :			Tuple  							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*snake_case      )

     def 			lowerCamelCase__			(    self					:		Union[str, Any]      )						->							Union[str, Any]:
          __UpperCAmelCase     :			int  							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*snake_case      )

     def 			lowerCamelCase__			(    self					:		int      )						->							str:
          __UpperCAmelCase     :			int  							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*snake_case      )

     @slow
     def 			lowerCamelCase__			(    self					:		List[str]      )						->							Optional[int]:
          for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __UpperCAmelCase     :			Any  							=      MraModel.from_pretrained(snake_case      )
               self.assertIsNotNone(snake_case      )






     @unittest.skip(reason='''MRA does not output attentions'''      )
     def 			lowerCamelCase__			(    self					:		Dict      )						->							Optional[Any]:
          return



@require_torch
class      a    (		unittest.TestCase	):


     """simple docstring"""






     @slow
     def 			lowerCamelCase__			(    self					:		int      )						->							List[Any]:
          __UpperCAmelCase     :			Optional[int]  							=      MraModel.from_pretrained('''uw-madison/mra-base-512-4'''      )
          __UpperCAmelCase     :			Any  							=      torch.arange(256      ).unsqueeze(0      )

          with torch.no_grad():
               __UpperCAmelCase     :			Any  							=      model(snake_case      )[0]

          __UpperCAmelCase     :			List[str]  							=      torch.Size((1, 256, 768)      )
          self.assertEqual(output.shape							,			snake_case      )

          __UpperCAmelCase     :			Optional[Any]  							=      torch.tensor(
              [[[-0.0_140, 0.0_830, -0.0_381], [0.1_546, 0.1_402, 0.0_220], [0.1_162, 0.0_851, 0.0_165]]]      )

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

     @slow
     def 			lowerCamelCase__			(    self					:		Optional[int]      )						->							int:
          __UpperCAmelCase     :			Tuple  							=      MraForMaskedLM.from_pretrained('''uw-madison/mra-base-512-4'''      )
          __UpperCAmelCase     :			Optional[int]  							=      torch.arange(256      ).unsqueeze(0      )

          with torch.no_grad():
               __UpperCAmelCase     :			str  							=      model(snake_case      )[0]

          __UpperCAmelCase     :			Optional[Any]  							=      5_0265

          __UpperCAmelCase     :			Optional[Any]  							=      torch.Size((1, 256, vocab_size)      )
          self.assertEqual(output.shape							,			snake_case      )

          __UpperCAmelCase     :			Optional[int]  							=      torch.tensor(
              [[[9.2_595, -3.6_038, 11.8_819], [9.3_869, -3.2_693, 11.0_956], [11.8_524, -3.4_938, 13.1_210]]]      )

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






     @slow
     def 			lowerCamelCase__			(    self					:		Any      )						->							Tuple:
          __UpperCAmelCase     :			Dict  							=      MraForMaskedLM.from_pretrained('''uw-madison/mra-base-4096-8-d3'''      )
          __UpperCAmelCase     :			Union[str, Any]  							=      torch.arange(4096      ).unsqueeze(0      )

          with torch.no_grad():
               __UpperCAmelCase     :			Optional[int]  							=      model(snake_case      )[0]

          __UpperCAmelCase     :			Union[str, Any]  							=      5_0265

          __UpperCAmelCase     :			Dict  							=      torch.Size((1, 4096, vocab_size)      )
          self.assertEqual(output.shape							,			snake_case      )

          __UpperCAmelCase     :			Tuple  							=      torch.tensor(
              [[[5.4_789, -2.3_564, 7.5_064], [7.9_067, -1.3_369, 9.9_668], [9.0_712, -1.8_106, 7.0_380]]]      )

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



'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__UpperCAmelCase      :List[Any]			    =       {
    "configuration_lxmert": ["LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "LxmertConfig"],
    "tokenization_lxmert": ["LxmertTokenizer"],
}

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase      :List[Any]			    =       ["LxmertTokenizerFast"]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase      :List[Any]			    =       [
          "LxmertEncoder",
          "LxmertForPreTraining",
          "LxmertForQuestionAnswering",
          "LxmertModel",
          "LxmertPreTrainedModel",
          "LxmertVisualFeatureEncoder",
          "LxmertXLayer",
      ]

try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase      :List[str]			    =       [
          "TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST",
          "TFLxmertForPreTraining",
          "TFLxmertMainLayer",
          "TFLxmertModel",
          "TFLxmertPreTrainedModel",
          "TFLxmertVisualFeatureEncoder",
      ]


if TYPE_CHECKING:
      from .configuration_lxmert import LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, LxmertConfig
      from .tokenization_lxmert import LxmertTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_lxmert_fast import LxmertTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_lxmert import (
                LxmertEncoder,
                LxmertForPreTraining,
                LxmertForQuestionAnswering,
                LxmertModel,
                LxmertPreTrainedModel,
                LxmertVisualFeatureEncoder,
                LxmertXLayer,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_lxmert import (
                TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                TFLxmertForPreTraining,
                TFLxmertMainLayer,
                TFLxmertModel,
                TFLxmertPreTrainedModel,
                TFLxmertVisualFeatureEncoder,
            )

else:
      import sys

      __UpperCAmelCase      :int			    =       _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)