

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       ,							_a       ,							_a=2       ,							_a=8       ,							_a=True       ,							_a=True       ,							_a=True       ,							_a=True       ,							_a=9_9       ,							_a=1_6       ,							_a=5       ,							_a=2       ,							_a=3_6       ,							_a="gelu"       ,							_a=0.0       ,							_a=0.0       ,							_a=5_1_2       ,							_a=1_6       ,							_a=2       ,							_a=0.02       ,							_a=3       ,							_a=4       ,							_a=None       ,							)    ->    Optional[Any]:
												_a	:    Tuple         =		parent
												_a	:    Dict         =		batch_size
												_a	:    Dict         =		seq_length
												_a	:    Optional[Any]         =		is_training
												_a	:    Tuple         =		use_input_mask
												_a	:    Tuple         =		use_token_type_ids
												_a	:    Any         =		use_labels
												_a	:    Any         =		vocab_size
												_a	:    int         =		hidden_size
												_a	:    str         =		num_hidden_layers
												_a	:    int         =		num_attention_heads
												_a	:    Optional[int]         =		intermediate_size
												_a	:    Optional[Any]         =		hidden_act
												_a	:    int         =		hidden_dropout_prob
												_a	:    List[str]         =		attention_probs_dropout_prob
												_a	:    str         =		max_position_embeddings
												_a	:    Optional[int]         =		type_vocab_size
												_a	:    List[str]         =		type_sequence_label_size
												_a	:    Optional[Any]         =		initializer_range
												_a	:    Any         =		num_labels
												_a	:    List[Any]         =		num_choices
												_a	:    List[str]         =		scope


							def 				__lowercase				(			self       )    ->    Any:
												_a	:    Tuple         =		ids_tensor([self.batch_size, self.seq_length]       ,							self.vocab_size       )

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

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

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

												_a	:    int         =		self.get_config()

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


							def 				__lowercase				(			self       )    ->    Tuple:
												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 				__lowercase				(			self       )    ->    Optional[Any]:
												_a	:    Union[str, Any]         =		self.get_config()
												_a	:    Union[str, Any]         =		3_0_0
												return config


							def 				__lowercase				(			self       )    ->    List[Any]:
												(
												    (
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					
												)	:    Union[str, Any]         =		self.prepare_config_and_inputs()

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


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							)    ->    int:
												_a	:    Optional[int]         =		True
												_a	:    List[Any]         =		MraModel(_a       )
												model.to(_a       )
												model.eval()
												_a	:    str         =		model(
												    _a       ,							attention_mask=_a       ,							token_type_ids=_a       ,							encoder_hidden_states=_a       ,							encoder_attention_mask=_a       ,							)
												_a	:    int         =		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 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    Optional[int]:
												_a	:    Optional[int]         =		MraForMaskedLM(config=_a       )
												model.to(_a       )
												model.eval()
												_a	:    List[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 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    List[Any]:
												_a	:    Tuple         =		MraForQuestionAnswering(config=_a       )
												model.to(_a       )
												model.eval()
												_a	:    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 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    Union[str, Any]:
												_a	:    List[Any]         =		self.num_labels
												_a	:    List[str]         =		MraForSequenceClassification(_a       )
												model.to(_a       )
												model.eval()
												_a	:    List[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 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    Optional[int]:
												_a	:    Any         =		self.num_labels
												_a	:    Any         =		MraForTokenClassification(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.num_labels)       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    int:
												_a	:    Dict         =		self.num_choices
												_a	:    Tuple         =		MraForMultipleChoice(config=_a       )
												model.to(_a       )
												model.eval()
												_a	:    Optional[int]         =		input_ids.unsqueeze(1       ).expand(-1       ,							self.num_choices       ,							-1       ).contiguous()
												_a	:    List[str]         =		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[int]         =		model(
												    _a       ,							attention_mask=_a       ,							token_type_ids=_a       ,							labels=_a       ,							)
												self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.num_choices)       )




							def 				__lowercase				(			self       )    ->    Optional[Any]:
												_a	:    Optional[int]         =		self.prepare_config_and_inputs()
												(
												    (
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					
												)	:    List[Any]         =		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_		(			__lowercase				,       unittest.TestCase   ):
							"""simple docstring"""







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

							UpperCAmelCase__       :						Optional[int]	    =    ()


							def 				__lowercase				(			self       )    ->    List[str]:
												_a	:    str         =		MraModelTester(self       )
												_a	:    Dict         =		ConfigTester(self       ,							config_class=_a       ,							hidden_size=3_7       )


							def 				__lowercase				(			self       )    ->    Optional[Any]:
												self.config_tester.run_common_tests()


							def 				__lowercase				(			self       )    ->    List[str]:
												_a	:    Optional[Any]         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_a       )


							def 				__lowercase				(			self       )    ->    Dict:
												_a	:    int         =		self.model_tester.prepare_config_and_inputs()
												for type in ["absolute", "relative_key", "relative_key_query"]:
																	_a	:    Any         =		type
																	self.model_tester.create_and_check_model(*_a       )


							def 				__lowercase				(			self       )    ->    Any:
												_a	:    Union[str, Any]         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_lm(*_a       )


							def 				__lowercase				(			self       )    ->    Tuple:
												_a	:    Optional[int]         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_multiple_choice(*_a       )


							def 				__lowercase				(			self       )    ->    str:
												_a	:    Optional[int]         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_question_answering(*_a       )


							def 				__lowercase				(			self       )    ->    Tuple:
												_a	:    Tuple         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_sequence_classification(*_a       )


							def 				__lowercase				(			self       )    ->    int:
												_a	:    Dict         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_token_classification(*_a       )


							@slow
							def 				__lowercase				(			self       )    ->    str:
												for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_a	:    List[str]         =		MraModel.from_pretrained(_a       )
																	self.assertIsNotNone(_a       )




							@unittest.skip(reason='''MRA does not output attentions'''       )
							def 				__lowercase				(			self       )    ->    Any:
												return



@require_torch
class 				UpperCAmelCase_		(			unittest.TestCase   ):
							"""simple docstring"""







							@slow
							def 				__lowercase				(			self       )    ->    Dict:
												_a	:    Tuple         =		MraModel.from_pretrained('''uw-madison/mra-base-512-4'''       )
												_a	:    Optional[int]         =		torch.arange(2_5_6       ).unsqueeze(0       )

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

												_a	:    List[str]         =		torch.Size((1, 2_5_6, 7_6_8)       )
												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 				__lowercase				(			self       )    ->    Dict:
												_a	:    List[Any]         =		MraForMaskedLM.from_pretrained('''uw-madison/mra-base-512-4'''       )
												_a	:    Union[str, Any]         =		torch.arange(2_5_6       ).unsqueeze(0       )

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

												_a	:    str         =		5_0_2_6_5

												_a	:    Any         =		torch.Size((1, 2_5_6, vocab_size)       )
												self.assertEqual(output.shape       ,							_a       )

												_a	:    Tuple         =		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 				__lowercase				(			self       )    ->    List[Any]:
												_a	:    Optional[Any]         =		MraForMaskedLM.from_pretrained('''uw-madison/mra-base-4096-8-d3'''       )
												_a	:    List[str]         =		torch.arange(4_0_9_6       ).unsqueeze(0       )

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

												_a	:    Any         =		5_0_2_6_5

												_a	:    List[Any]         =		torch.Size((1, 4_0_9_6, vocab_size)       )
												self.assertEqual(output.shape       ,							_a       )

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






import warnings

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

class 				UpperCAmelCase_		(			__lowercase   ):
							"""simple docstring"""







							UpperCAmelCase__       :						Union[str, Any]	    =    ["image_processor", "tokenizer"]
							UpperCAmelCase__       :						Dict	    =    "ChineseCLIPImageProcessor"
							UpperCAmelCase__       :						List[str]	    =    ("BertTokenizer", "BertTokenizerFast")


							def __init__(			self       ,							_a=None       ,							_a=None       ,							**_a       )    ->    Any:
												_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.'''       ,							_a       ,							)
																	_a	:    Tuple         =		kwargs.pop('''feature_extractor'''       )

												_a	:    Tuple         =		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__(_a       ,							_a       )
												_a	:    List[str]         =		self.image_processor


							def __call__(			self       ,							_a=None       ,							_a=None       ,							_a=None       ,							**_a       )    ->    int:

												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	:    List[str]         =		self.tokenizer(_a       ,							return_tensors=_a       ,							**_a       )

												if images is not None:
																	_a	:    Optional[Any]         =		self.image_processor(_a       ,							return_tensors=_a       ,							**_a       )

												if text is not None and images is not None:
																	_a	:    Union[str, Any]         =		image_features.pixel_values
																	return encoding
												elif text is not None:
																	return encoding
												else:
																	return BatchEncoding(data=dict(**_a       )       ,							tensor_type=_a       )


							def 				__lowercase				(			self       ,							*_a       ,							**_a       )    ->    Union[str, Any]:
												return self.tokenizer.batch_decode(*_a       ,							**_a       )


							def 				__lowercase				(			self       ,							*_a       ,							**_a       )    ->    Any:
												return self.tokenizer.decode(*_a       ,							**_a       )


							@property
							def 				__lowercase				(			self       )    ->    Optional[Any]:
												_a	:    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       )       )




							@property
							def 				__lowercase				(			self       )    ->    Dict:
												warnings.warn(
												    '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''       ,							_a       ,							)
												return self.image_processor_class



