





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							:     Union[str, Any]					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     str=2					,      __lowerCamelCase							:     Optional[Any]=8					,      __lowerCamelCase							:     Union[str, Any]=True					,      __lowerCamelCase							:     int=True					,      __lowerCamelCase							:     Tuple=True					,      __lowerCamelCase							:     str=True					,      __lowerCamelCase							:     List[Any]=99					,      __lowerCamelCase							:     Union[str, Any]=16					,      __lowerCamelCase							:     Optional[Any]=5					,      __lowerCamelCase							:     Any=2					,      __lowerCamelCase							:     Tuple=36					,      __lowerCamelCase							:     Any="gelu"					,      __lowerCamelCase							:     List[str]=0.0					,      __lowerCamelCase							:     Dict=0.0					,      __lowerCamelCase							:     Dict=512					,      __lowerCamelCase							:     str=16					,      __lowerCamelCase							:     Optional[Any]=2					,      __lowerCamelCase							:     str=0.02					,      __lowerCamelCase							:     Dict=3					,      __lowerCamelCase							:     List[str]=4					,      __lowerCamelCase							:     str=None					,      )       -> Optional[int]:

			'''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    UpperCAmelCase			(						self							:     List[str]   )       -> 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    UpperCAmelCase			(						self							:     Optional[int]   )       -> Union[str, Any]:

			'''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=__lowerCamelCase					,      initializer_range=self.initializer_range					,      )




	def    UpperCAmelCase			(						self							:     Optional[Any]   )       -> Dict:

			'''simple docstring'''




			__lowercase											=     self.get_config()
			__lowercase											=     300
			return config




	def    UpperCAmelCase			(						self							:     Dict   )       -> List[str]:

			'''simple docstring'''




			(
			    (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __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    UpperCAmelCase			(						self							:     Tuple					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     Optional[Any]					,      __lowerCamelCase							:     Optional[int]					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Union[str, Any]   )       -> int:

			'''simple docstring'''




			__lowercase											=     MraModel(config=__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase   )
			__lowercase											=     model(__lowerCamelCase					,      token_type_ids=__lowerCamelCase   )
			__lowercase											=     model(__lowerCamelCase   )
			self.parent.assertEqual(result.last_hidden_state.shape					,      (self.batch_size, self.seq_length, self.hidden_size)   )




	def    UpperCAmelCase			(						self							:     Tuple					,      __lowerCamelCase							:     Union[str, Any]					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Union[str, Any]					,      __lowerCamelCase							:     Dict					,      )       -> List[str]:

			'''simple docstring'''




			__lowercase											=     True
			__lowercase											=     MraModel(__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(
			    __lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      encoder_hidden_states=__lowerCamelCase					,      encoder_attention_mask=__lowerCamelCase					,      )
			__lowercase											=     model(
			    __lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      encoder_hidden_states=__lowerCamelCase					,      )
			__lowercase											=     model(__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase   )
			self.parent.assertEqual(result.last_hidden_state.shape					,      (self.batch_size, self.seq_length, self.hidden_size)   )




	def    UpperCAmelCase			(						self							:     Dict					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     Dict   )       -> int:

			'''simple docstring'''




			__lowercase											=     MraForMaskedLM(config=__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      labels=__lowerCamelCase   )
			self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.vocab_size)   )




	def    UpperCAmelCase			(						self							:     Optional[Any]					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     Optional[int]					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Any   )       -> Union[str, Any]:

			'''simple docstring'''




			__lowercase											=     MraForQuestionAnswering(config=__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(
			    __lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      start_positions=__lowerCamelCase					,      end_positions=__lowerCamelCase					,      )
			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							:     Optional[Any]					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     List[str]					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     List[Any]   )       -> str:

			'''simple docstring'''




			__lowercase											=     self.num_labels
			__lowercase											=     MraForSequenceClassification(__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      labels=__lowerCamelCase   )
			self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)   )




	def    UpperCAmelCase			(						self							:     Tuple					,      __lowerCamelCase							:     List[str]					,      __lowerCamelCase							:     List[Any]					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Optional[Any]   )       -> Dict:

			'''simple docstring'''




			__lowercase											=     self.num_labels
			__lowercase											=     MraForTokenClassification(config=__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      labels=__lowerCamelCase   )
			self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.num_labels)   )




	def    UpperCAmelCase			(						self							:     Tuple					,      __lowerCamelCase							:     Optional[int]					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Optional[Any]					,      __lowerCamelCase							:     Union[str, Any]					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     Union[str, Any]					,      __lowerCamelCase							:     List[Any]   )       -> Optional[Any]:

			'''simple docstring'''




			__lowercase											=     self.num_choices
			__lowercase											=     MraForMultipleChoice(config=__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			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(
			    __lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      labels=__lowerCamelCase					,      )
			self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_choices)   )






	def    UpperCAmelCase			(						self							:     Tuple   )       -> int:

			'''simple docstring'''




			__lowercase											=     self.prepare_config_and_inputs()
			(
			    (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __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 		snake_case_   (							UpperCAmelCase_     ,     unittest.TestCase						):







	'''simple docstring'''
	__UpperCamelCase   =		(
	    (
	        MraModel,
	        MraForMaskedLM,
	        MraForMultipleChoice,
	        MraForQuestionAnswering,
	        MraForSequenceClassification,
	        MraForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	__UpperCamelCase   =		False
	__UpperCamelCase   =		False
	__UpperCamelCase   =		False
	__UpperCamelCase   =		False

	__UpperCamelCase   =		()




	def    UpperCAmelCase			(						self							:     Tuple   )       -> Any:

			'''simple docstring'''




			__lowercase											=     MraModelTester(self   )
			__lowercase											=     ConfigTester(self					,      config_class=__lowerCamelCase					,      hidden_size=37   )




	def    UpperCAmelCase			(						self							:     List[str]   )       -> List[str]:

			'''simple docstring'''




			self.config_tester.run_common_tests()




	def    UpperCAmelCase			(						self							:     List[Any]   )       -> int:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Tuple   )       -> Optional[int]:

			'''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(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     List[Any]   )       -> Dict:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_masked_lm(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Dict   )       -> Dict:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_multiple_choice(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     List[str]   )       -> int:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Tuple   )       -> Dict:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     List[str]   )       -> str:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase   )




	@slow
	def    UpperCAmelCase			(						self							:     List[str]   )       -> Optional[int]:

			'''simple docstring'''




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






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

			'''simple docstring'''




			return





@require_torch
class 		snake_case_   (							unittest.TestCase						):







	'''simple docstring'''




	@slow
	def    UpperCAmelCase			(						self							:     int   )       -> 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(__lowerCamelCase   )[0]

			__lowercase											=     torch.Size((1, 256, 768)   )
			self.assertEqual(output.shape					,      __lowerCamelCase   )

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




	@slow
	def    UpperCAmelCase			(						self							:     Optional[Any]   )       -> Optional[int]:

			'''simple docstring'''




			__lowercase											=     MraForMaskedLM.from_pretrained('uw-madison/mra-base-512-4'   )
			__lowercase											=     torch.arange(256   ).unsqueeze(0   )

			with torch.no_grad():
					__lowercase											=     model(__lowerCamelCase   )[0]

			__lowercase											=     50_265

			__lowercase											=     torch.Size((1, 256, vocab_size)   )
			self.assertEqual(output.shape					,      __lowerCamelCase   )

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






	@slow
	def    UpperCAmelCase			(						self							:     str   )       -> int:

			'''simple docstring'''




			__lowercase											=     MraForMaskedLM.from_pretrained('uw-madison/mra-base-4096-8-d3'   )
			__lowercase											=     torch.arange(4_096   ).unsqueeze(0   )

			with torch.no_grad():
					__lowercase											=     model(__lowerCamelCase   )[0]

			__lowercase											=     50_265

			__lowercase											=     torch.Size((1, 4_096, vocab_size)   )
			self.assertEqual(output.shape					,      __lowerCamelCase   )

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











def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case						,   snake_case							)		->    bool:
		return not any(
		    neighbour == 1 and colored_vertices[i] == color
		    for i, neighbour in enumerate(snake_case							)							)



def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case						,   snake_case						,   snake_case							)		->    bool:

		# Base Case
		if index == len(snake_case							):
				return True

		# Recursive Step
		for i in range(snake_case							):
				if valid_coloring(graph[index]						,   snake_case						,   snake_case							):
						# Color current vertex
						__lowercase											=     i
						# Validate coloring
						if util_color(snake_case						,   snake_case						,   snake_case						,   index + 1							):
								return True
						# Backtrack
						__lowercase											=     -1
		return False



def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case							)		->    list[int]:
		__lowercase											=     [-1] * len(snake_case							)

		if util_color(snake_case						,   snake_case						,   snake_case						,   0							):
				return colored_vertices

		return []




