





"""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 			__lowercase     :







					'''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.0_2		,     _UpperCAmelCase=3		,     _UpperCAmelCase=4		,     _UpperCAmelCase=None		,     ):
									__a	:  str         =					parent
									__a	:  Tuple         =					batch_size
									__a	:  Dict         =					seq_length
									__a	:  List[str]         =					is_training
									__a	:  List[Any]         =					use_input_mask
									__a	:  Union[str, Any]         =					use_token_type_ids
									__a	:  str         =					use_labels
									__a	:  int         =					vocab_size
									__a	:  Union[str, Any]         =					hidden_size
									__a	:  List[Any]         =					num_hidden_layers
									__a	:  Optional[Any]         =					num_attention_heads
									__a	:  List[Any]         =					intermediate_size
									__a	:  Tuple         =					hidden_act
									__a	:  List[str]         =					hidden_dropout_prob
									__a	:  Optional[int]         =					attention_probs_dropout_prob
									__a	:  Any         =					max_position_embeddings
									__a	:  int         =					type_vocab_size
									__a	:  List[str]         =					type_sequence_label_size
									__a	:  List[Any]         =					initializer_range
									__a	:  List[str]         =					num_labels
									__a	:  Any         =					num_choices
									__a	:  List[Any]         =					scope







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[Any]         =					ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size       )

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

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

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

									__a	:  Optional[int]         =					self.get_config()

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







					def 	_lowerCamelCase						(  self       ):
									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 	_lowerCamelCase						(  self       ):
									__a	:  Union[str, Any]         =					self.get_config()
									__a	:  Dict         =					300
									return config







					def 	_lowerCamelCase						(  self       ):
									(
									    (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  
									)	:  str         =					self.prepare_config_and_inputs()

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







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     ):
									__a	:  int         =					True
									__a	:  List[str]         =					MraModel(_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  int         =					model(
									    _UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     token_type_ids=_UpperCAmelCase		,     encoder_hidden_states=_UpperCAmelCase		,     encoder_attention_mask=_UpperCAmelCase		,     )
									__a	:  Dict         =					model(
									    _UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     token_type_ids=_UpperCAmelCase		,     encoder_hidden_states=_UpperCAmelCase		,     )
									__a	:  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 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Optional[int]         =					MraForMaskedLM(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  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 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Optional[int]         =					MraForQuestionAnswering(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  Optional[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 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Union[str, Any]         =					self.num_labels
									__a	:  Union[str, Any]         =					MraForSequenceClassification(_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  Optional[int]         =					model(_UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     token_type_ids=_UpperCAmelCase		,     labels=_UpperCAmelCase       )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Union[str, Any]         =					self.num_labels
									__a	:  List[str]         =					MraForTokenClassification(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  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 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Any         =					self.num_choices
									__a	:  Tuple         =					MraForMultipleChoice(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  Union[str, Any]         =					input_ids.unsqueeze(1       ).expand(-1		,     self.num_choices		,     -1       ).contiguous()
									__a	:  int         =					token_type_ids.unsqueeze(1       ).expand(-1		,     self.num_choices		,     -1       ).contiguous()
									__a	:  int         =					input_mask.unsqueeze(1       ).expand(-1		,     self.num_choices		,     -1       ).contiguous()
									__a	:  Optional[Any]         =					model(
									    _UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     token_type_ids=_UpperCAmelCase		,     labels=_UpperCAmelCase		,     )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_choices)       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Union[str, Any]         =					self.prepare_config_and_inputs()
									(
									    (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  
									)	:  Dict         =					config_and_inputs
									__a	:  Any         =					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
									return config, inputs_dict






@require_torch
class 			__lowercase     (     _UpperCamelCase				, unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					(
					    (
					        MraModel,
					        MraForMaskedLM,
					        MraForMultipleChoice,
					        MraForQuestionAnswering,
					        MraForSequenceClassification,
					        MraForTokenClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					__lowerCAmelCase					=					False
					__lowerCAmelCase					=					False
					__lowerCAmelCase					=					False
					__lowerCAmelCase					=					False

					__lowerCAmelCase					=					()







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[Any]         =					MraModelTester(self       )
									__a	:  Union[str, Any]         =					ConfigTester(self		,     config_class=_UpperCAmelCase		,     hidden_size=37       )







					def 	_lowerCamelCase						(  self       ):
									self.config_tester.run_common_tests()







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[Any]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Union[str, Any]         =					self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
													__a	:  int         =					type
													self.model_tester.create_and_check_model(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[int]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[int]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Dict         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase       )







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







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







					@slow
					def 	_lowerCamelCase						(  self       ):
									for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__a	:  Optional[int]         =					MraModel.from_pretrained(_UpperCAmelCase       )
													self.assertIsNotNone(_UpperCAmelCase       )







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






@require_torch
class 			__lowercase     (     unittest.TestCase							):







					'''simple docstring'''




					@slow
					def 	_lowerCamelCase						(  self       ):
									__a	:  Any         =					MraModel.from_pretrained('''uw-madison/mra-base-512-4'''       )
									__a	:  Dict         =					torch.arange(256       ).unsqueeze(0       )

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

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

									__a	:  List[Any]         =					torch.tensor(
									    [[[-0.0_1_4_0, 0.0_8_3_0, -0.0_3_8_1], [0.1_5_4_6, 0.1_4_0_2, 0.0_2_2_0], [0.1_1_6_2, 0.0_8_5_1, 0.0_1_6_5]]]       )

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







					@slow
					def 	_lowerCamelCase						(  self       ):
									__a	:  Dict         =					MraForMaskedLM.from_pretrained('''uw-madison/mra-base-512-4'''       )
									__a	:  Tuple         =					torch.arange(256       ).unsqueeze(0       )

									with torch.no_grad():
													__a	:  List[Any]         =					model(_UpperCAmelCase       )[0]

									__a	:  Tuple         =					50265

									__a	:  Union[str, Any]         =					torch.Size((1, 256, vocab_size)       )
									self.assertEqual(output.shape		,     _UpperCAmelCase       )

									__a	:  str         =					torch.tensor(
									    [[[9.2_5_9_5, -3.6_0_3_8, 1_1.8_8_1_9], [9.3_8_6_9, -3.2_6_9_3, 1_1.0_9_5_6], [1_1.8_5_2_4, -3.4_9_3_8, 1_3.1_2_1_0]]]       )

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







					@slow
					def 	_lowerCamelCase						(  self       ):
									__a	:  int         =					MraForMaskedLM.from_pretrained('''uw-madison/mra-base-4096-8-d3'''       )
									__a	:  Any         =					torch.arange(4096       ).unsqueeze(0       )

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

									__a	:  Optional[Any]         =					50265

									__a	:  Optional[int]         =					torch.Size((1, 4096, vocab_size)       )
									self.assertEqual(output.shape		,     _UpperCAmelCase       )

									__a	:  Union[str, Any]         =					torch.tensor(
									    [[[5.4_7_8_9, -2.3_5_6_4, 7.5_0_6_4], [7.9_0_6_7, -1.3_3_6_9, 9.9_6_6_8], [9.0_7_1_2, -1.8_1_0_6, 7.0_3_8_0]]]       )

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






"""simple docstring"""






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding




class 			__lowercase     (     _UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					['''image_processor''', '''tokenizer''']
					__lowerCAmelCase					=					'''CLIPImageProcessor'''
					__lowerCAmelCase					=					('''XLMRobertaTokenizer''', '''XLMRobertaTokenizerFast''')







					def __init__(  self		,     _UpperCAmelCase=None		,     _UpperCAmelCase=None		,     **_UpperCAmelCase       ):
									__a	:  Optional[Any]         =					None
									if "feature_extractor" in kwargs:
													warnings.warn(
													    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
													    ''' instead.'''		,     _UpperCAmelCase		,     )
													__a	:  Any         =					kwargs.pop('''feature_extractor'''       )

									__a	:  Optional[Any]         =					image_processor if image_processor is not None else feature_extractor
									if image_processor is None:
													raise ValueError('''You need to specify an `image_processor`.'''       )
									if tokenizer is None:
													raise ValueError('''You need to specify a `tokenizer`.'''       )

									super().__init__(_UpperCAmelCase		,     _UpperCAmelCase       )







					def __call__(  self		,     _UpperCAmelCase=None		,     _UpperCAmelCase=None		,     _UpperCAmelCase=None		,     **_UpperCAmelCase       ):

									if text is None and images is None:
													raise ValueError('''You have to specify either text or images. Both cannot be none.'''       )

									if text is not None:
													__a	:  Any         =					self.tokenizer(_UpperCAmelCase		,     return_tensors=_UpperCAmelCase		,     **_UpperCAmelCase       )

									if images is not None:
													__a	:  List[str]         =					self.image_processor(_UpperCAmelCase		,     return_tensors=_UpperCAmelCase		,     **_UpperCAmelCase       )

									if text is not None and images is not None:
													__a	:  Optional[int]         =					image_features.pixel_values
													return encoding
									elif text is not None:
													return encoding
									else:
													return BatchEncoding(data=dict(**_UpperCAmelCase       )		,     tensor_type=_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self		,     *_UpperCAmelCase		,     **_UpperCAmelCase       ):
									return self.tokenizer.batch_decode(*_UpperCAmelCase		,     **_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self		,     *_UpperCAmelCase		,     **_UpperCAmelCase       ):
									return self.tokenizer.decode(*_UpperCAmelCase		,     **_UpperCAmelCase       )







					@property
					def 	_lowerCamelCase						(  self       ):
									__a	:  Union[str, Any]         =					self.tokenizer.model_input_names
									__a	:  Dict         =					self.image_processor.model_input_names
									return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names       )       )