


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




	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						,				):

						'''simple docstring'''





						__a			:       Optional[int]					    =  parent
						__a			:       Tuple					    =  batch_size
						__a			:       Optional[Any]					    =  seq_length
						__a			:       Dict					    =  is_training
						__a			:       str					    =  use_input_mask
						__a			:       Dict					    =  use_token_type_ids
						__a			:       Any					    =  use_labels
						__a			:       Optional[int]					    =  vocab_size
						__a			:       Optional[Any]					    =  hidden_size
						__a			:       Tuple					    =  num_hidden_layers
						__a			:       Any					    =  num_attention_heads
						__a			:       Dict					    =  intermediate_size
						__a			:       Dict					    =  hidden_act
						__a			:       List[str]					    =  hidden_dropout_prob
						__a			:       Any					    =  attention_probs_dropout_prob
						__a			:       Optional[int]					    =  max_position_embeddings
						__a			:       Dict					    =  type_vocab_size
						__a			:       List[str]					    =  type_sequence_label_size
						__a			:       Union[str, Any]					    =  initializer_range
						__a			:       Any					    =  num_labels
						__a			:       int					    =  num_choices
						__a			:       Tuple					    =  scope




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       List[Any]					    =  ids_tensor([self.batch_size, self.seq_length]						,				self.vocab_size							)

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

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

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

						__a			:       Tuple					    =  self.get_config()

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




	def       __UpperCAmelCase							(   self							):

						'''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       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       List[str]					    =  self.get_config()
						__a			:       Dict					    =  300
						return config




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						(
						    (
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				
						)			:       Any					    =  self.prepare_config_and_inputs()

						__a			:       Dict					    =  True
						__a			:       str					    =  floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
						__a			:       Union[str, Any]					    =  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       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a							):

						'''simple docstring'''





						__a			:       List[Any]					    =  MraModel(config=__a							)
						model.to(__a							)
						model.eval()
						__a			:       Optional[Any]					    =  model(__a						,				attention_mask=__a						,				token_type_ids=__a							)
						__a			:       int					    =  model(__a						,				token_type_ids=__a							)
						__a			:       List[Any]					    =  model(__a							)
						self.parent.assertEqual(result.last_hidden_state.shape						,				(self.batch_size, self.seq_length, self.hidden_size)							)




	def       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a						,				):

						'''simple docstring'''





						__a			:       str					    =  True
						__a			:       Union[str, Any]					    =  MraModel(__a							)
						model.to(__a							)
						model.eval()
						__a			:       Optional[Any]					    =  model(
						    __a						,				attention_mask=__a						,				token_type_ids=__a						,				encoder_hidden_states=__a						,				encoder_attention_mask=__a						,				)
						__a			:       Union[str, Any]					    =  model(
						    __a						,				attention_mask=__a						,				token_type_ids=__a						,				encoder_hidden_states=__a						,				)
						__a			:       Optional[int]					    =  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       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a							):

						'''simple docstring'''





						__a			:       int					    =  MraForMaskedLM(config=__a							)
						model.to(__a							)
						model.eval()
						__a			:       Union[str, Any]					    =  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       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a							):

						'''simple docstring'''





						__a			:       Tuple					    =  MraForQuestionAnswering(config=__a							)
						model.to(__a							)
						model.eval()
						__a			:       List[str]					    =  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       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a							):

						'''simple docstring'''





						__a			:       Dict					    =  self.num_labels
						__a			:       Optional[Any]					    =  MraForSequenceClassification(__a							)
						model.to(__a							)
						model.eval()
						__a			:       Any					    =  model(__a						,				attention_mask=__a						,				token_type_ids=__a						,				labels=__a							)
						self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_labels)							)




	def       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a							):

						'''simple docstring'''





						__a			:       Optional[int]					    =  self.num_labels
						__a			:       Union[str, Any]					    =  MraForTokenClassification(config=__a							)
						model.to(__a							)
						model.eval()
						__a			:       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.num_labels)							)




	def       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a						,				__a							):

						'''simple docstring'''





						__a			:       Dict					    =  self.num_choices
						__a			:       str					    =  MraForMultipleChoice(config=__a							)
						model.to(__a							)
						model.eval()
						__a			:       Union[str, Any]					    =  input_ids.unsqueeze(1							).expand(-1						,				self.num_choices						,				-1							).contiguous()
						__a			:       Tuple					    =  token_type_ids.unsqueeze(1							).expand(-1						,				self.num_choices						,				-1							).contiguous()
						__a			:       Union[str, Any]					    =  input_mask.unsqueeze(1							).expand(-1						,				self.num_choices						,				-1							).contiguous()
						__a			:       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       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       List[str]					    =  self.prepare_config_and_inputs()
						(
						    (
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				
						)			:       Dict					    =  config_and_inputs
						__a			:       int					    =  {'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					):
	A_				=    (
	    (
	        MraModel,
	        MraForMaskedLM,
	        MraForMultipleChoice,
	        MraForQuestionAnswering,
	        MraForSequenceClassification,
	        MraForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	A_				=    False
	A_				=    False
	A_				=    False
	A_				=    False

	A_				=    ()




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       str					    =  MraModelTester(self							)
						__a			:       Tuple					    =  ConfigTester(self						,				config_class=__a						,				hidden_size=37							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						self.config_tester.run_common_tests()




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       int					    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__a							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Optional[Any]					    =  self.model_tester.prepare_config_and_inputs()
						for type in ["absolute", "relative_key", "relative_key_query"]:
											__a			:       List[str]					    =  type
											self.model_tester.create_and_check_model(*__a							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Dict					    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*__a							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Dict					    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*__a							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Optional[Any]					    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*__a							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Optional[Any]					    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*__a							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       str					    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*__a							)




	@slow
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__a			:       int					    =  MraModel.from_pretrained(__a							)
											self.assertIsNotNone(__a							)





	@unittest.skip(reason='MRA does not output attentions'							)
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return





@require_torch
class 							__UpperCamelCase				(		unittest.TestCase					):




	@slow
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Tuple					    =  MraModel.from_pretrained('uw-madison/mra-base-512-4'							)
						__a			:       List[Any]					    =  torch.arange(256							).unsqueeze(0							)

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

						__a			:       int					    =  torch.Size((1, 256, 768)							)
						self.assertEqual(output.shape						,				__a							)

						__a			:       Any					    =  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]						,				__a						,				atol=1E-4							)							)




	@slow
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Tuple					    =  MraForMaskedLM.from_pretrained('uw-madison/mra-base-512-4'							)
						__a			:       Union[str, Any]					    =  torch.arange(256							).unsqueeze(0							)

						with torch.no_grad():
											__a			:       int					    =  model(__a							)[0]

						__a			:       Any					    =  5_0265

						__a			:       Dict					    =  torch.Size((1, 256, vocab_size)							)
						self.assertEqual(output.shape						,				__a							)

						__a			:       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]						,				__a						,				atol=1E-4							)							)





	@slow
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       int					    =  MraForMaskedLM.from_pretrained('uw-madison/mra-base-4096-8-d3'							)
						__a			:       int					    =  torch.arange(4096							).unsqueeze(0							)

						with torch.no_grad():
											__a			:       Union[str, Any]					    =  model(__a							)[0]

						__a			:       Dict					    =  5_0265

						__a			:       Tuple					    =  torch.Size((1, 4096, vocab_size)							)
						self.assertEqual(output.shape						,				__a							)

						__a			:       List[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]						,				__a						,				atol=1E-4							)							)





"""simple docstring"""





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig


_a		       =							{
    'albert-base-v1': 'https://huggingface.co/albert-base-v1/resolve/main/config.json',
    'albert-large-v1': 'https://huggingface.co/albert-large-v1/resolve/main/config.json',
    'albert-xlarge-v1': 'https://huggingface.co/albert-xlarge-v1/resolve/main/config.json',
    'albert-xxlarge-v1': 'https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json',
    'albert-base-v2': 'https://huggingface.co/albert-base-v2/resolve/main/config.json',
    'albert-large-v2': 'https://huggingface.co/albert-large-v2/resolve/main/config.json',
    'albert-xlarge-v2': 'https://huggingface.co/albert-xlarge-v2/resolve/main/config.json',
    'albert-xxlarge-v2': 'https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json',
}


class A_	(lowercase__      ):




  '''simple docstring'''
  SCREAMING_SNAKE_CASE__			:						Optional[Any]       =		"""albert"""







  def __init__(		self				,   lowercase_=3_0000				,   lowercase_=128				,   lowercase_=4096				,   lowercase_=12				,   lowercase_=1				,   lowercase_=64				,   lowercase_=1_6384				,   lowercase_=1				,   lowercase_="gelu_new"				,   lowercase_=0				,   lowercase_=0				,   lowercase_=512				,   lowercase_=2				,   lowercase_=0.02				,   lowercase_=1E-1_2				,   lowercase_=0.1				,   lowercase_="absolute"				,   lowercase_=0				,   lowercase_=2				,   lowercase_=3				,   **lowercase_				,   ):

     """simple docstring"""
     super().__init__(pad_token_id=lowercase_				,   bos_token_id=lowercase_				,   eos_token_id=lowercase_				,   **lowercase_  )

     UpperCAmelCase_					:					int			     =    vocab_size
     UpperCAmelCase_					:					Optional[int]			     =    embedding_size
     UpperCAmelCase_					:					List[str]			     =    hidden_size
     UpperCAmelCase_					:					Optional[int]			     =    num_hidden_layers
     UpperCAmelCase_					:					Union[str, Any]			     =    num_hidden_groups
     UpperCAmelCase_					:					Dict			     =    num_attention_heads
     UpperCAmelCase_					:					Any			     =    inner_group_num
     UpperCAmelCase_					:					Union[str, Any]			     =    hidden_act
     UpperCAmelCase_					:					Union[str, Any]			     =    intermediate_size
     UpperCAmelCase_					:					List[str]			     =    hidden_dropout_prob
     UpperCAmelCase_					:					Union[str, Any]			     =    attention_probs_dropout_prob
     UpperCAmelCase_					:					Optional[Any]			     =    max_position_embeddings
     UpperCAmelCase_					:					Any			     =    type_vocab_size
     UpperCAmelCase_					:					List[str]			     =    initializer_range
     UpperCAmelCase_					:					Optional[int]			     =    layer_norm_eps
     UpperCAmelCase_					:					List[Any]			     =    classifier_dropout_prob
     UpperCAmelCase_					:					Tuple			     =    position_embedding_type




class A_	(lowercase__      ):




  '''simple docstring'''







  @property
  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     if self.task == "multiple-choice":
        UpperCAmelCase_					:					int			     =    {0: "batch", 1: "choice", 2: "sequence"}
     else:
        UpperCAmelCase_					:					Optional[Any]			     =    {0: "batch", 1: "sequence"}
     return OrderedDict(
         [
             ("input_ids", dynamic_axis),
             ("attention_mask", dynamic_axis),
             ("token_type_ids", dynamic_axis),
         ]  )


