


"""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  :
							'''simple docstring'''







							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=5_12    ,   __a=16    ,   __a=2    ,   __a=0.0_2    ,   __a=3    ,   __a=4    ,   __a=None    ,   ):
										__lowerCAmelCase				    =			parent
										__lowerCAmelCase				    =			batch_size
										__lowerCAmelCase				    =			seq_length
										__lowerCAmelCase				    =			is_training
										__lowerCAmelCase				    =			use_input_mask
										__lowerCAmelCase				    =			use_token_type_ids
										__lowerCAmelCase				    =			use_labels
										__lowerCAmelCase				    =			vocab_size
										__lowerCAmelCase				    =			hidden_size
										__lowerCAmelCase				    =			num_hidden_layers
										__lowerCAmelCase				    =			num_attention_heads
										__lowerCAmelCase				    =			intermediate_size
										__lowerCAmelCase				    =			hidden_act
										__lowerCAmelCase				    =			hidden_dropout_prob
										__lowerCAmelCase				    =			attention_probs_dropout_prob
										__lowerCAmelCase				    =			max_position_embeddings
										__lowerCAmelCase				    =			type_vocab_size
										__lowerCAmelCase				    =			type_sequence_label_size
										__lowerCAmelCase				    =			initializer_range
										__lowerCAmelCase				    =			num_labels
										__lowerCAmelCase				    =			num_choices
										__lowerCAmelCase				    =			scope

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			ids_tensor([self.batch_size, self.seq_length]    ,   self.vocab_size	)

										__lowerCAmelCase				    =			None
										if self.use_input_mask:
													__lowerCAmelCase				    =			random_attention_mask([self.batch_size, self.seq_length]	)

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

										__lowerCAmelCase				    =			None
										__lowerCAmelCase				    =			None
										__lowerCAmelCase				    =			None
										if self.use_labels:
													__lowerCAmelCase				    =			ids_tensor([self.batch_size]    ,   self.type_sequence_label_size	)
													__lowerCAmelCase				    =			ids_tensor([self.batch_size, self.seq_length]    ,   self.num_labels	)
													__lowerCAmelCase				    =			ids_tensor([self.batch_size]    ,   self.num_choices	)

										__lowerCAmelCase				    =			self.get_config()

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

							def 			snake_case				(     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=__a    ,   initializer_range=self.initializer_range    ,   )

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			self.get_config()
										__lowerCAmelCase				    =			3_00
										return config

							def 			snake_case				(     self	):
										(
										    (
										    __lowerCAmelCase
										)    ,	(
										    __lowerCAmelCase
										)    ,	(
										    __lowerCAmelCase
										)    ,	(
										    __lowerCAmelCase
										)    ,	(
										    __lowerCAmelCase
										)    ,	(
										    __lowerCAmelCase
										)    ,	(
										    __lowerCAmelCase
										)    ,	
										)				    =			self.prepare_config_and_inputs()

										__lowerCAmelCase				    =			True
										__lowerCAmelCase				    =			floats_tensor([self.batch_size, self.seq_length, self.hidden_size]	)
										__lowerCAmelCase				    =			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 			snake_case				(     self    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a	):
										__lowerCAmelCase				    =			MraModel(config=__a	)
										model.to(__a	)
										model.eval()
										__lowerCAmelCase				    =			model(__a    ,   attention_mask=__a    ,   token_type_ids=__a	)
										__lowerCAmelCase				    =			model(__a    ,   token_type_ids=__a	)
										__lowerCAmelCase				    =			model(__a	)
										self.parent.assertEqual(result.last_hidden_state.shape    ,   (self.batch_size, self.seq_length, self.hidden_size)	)

							def 			snake_case				(     self    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   ):
										__lowerCAmelCase				    =			True
										__lowerCAmelCase				    =			MraModel(__a	)
										model.to(__a	)
										model.eval()
										__lowerCAmelCase				    =			model(
										    __a    ,   attention_mask=__a    ,   token_type_ids=__a    ,   encoder_hidden_states=__a    ,   encoder_attention_mask=__a    ,   )
										__lowerCAmelCase				    =			model(
										    __a    ,   attention_mask=__a    ,   token_type_ids=__a    ,   encoder_hidden_states=__a    ,   )
										__lowerCAmelCase				    =			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 			snake_case				(     self    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a	):
										__lowerCAmelCase				    =			MraForMaskedLM(config=__a	)
										model.to(__a	)
										model.eval()
										__lowerCAmelCase				    =			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 			snake_case				(     self    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a	):
										__lowerCAmelCase				    =			MraForQuestionAnswering(config=__a	)
										model.to(__a	)
										model.eval()
										__lowerCAmelCase				    =			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 			snake_case				(     self    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a	):
										__lowerCAmelCase				    =			self.num_labels
										__lowerCAmelCase				    =			MraForSequenceClassification(__a	)
										model.to(__a	)
										model.eval()
										__lowerCAmelCase				    =			model(__a    ,   attention_mask=__a    ,   token_type_ids=__a    ,   labels=__a	)
										self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.num_labels)	)

							def 			snake_case				(     self    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a	):
										__lowerCAmelCase				    =			self.num_labels
										__lowerCAmelCase				    =			MraForTokenClassification(config=__a	)
										model.to(__a	)
										model.eval()
										__lowerCAmelCase				    =			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 			snake_case				(     self    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a    ,   __a	):
										__lowerCAmelCase				    =			self.num_choices
										__lowerCAmelCase				    =			MraForMultipleChoice(config=__a	)
										model.to(__a	)
										model.eval()
										__lowerCAmelCase				    =			input_ids.unsqueeze(1	).expand(-1    ,   self.num_choices    ,   -1	).contiguous()
										__lowerCAmelCase				    =			token_type_ids.unsqueeze(1	).expand(-1    ,   self.num_choices    ,   -1	).contiguous()
										__lowerCAmelCase				    =			input_mask.unsqueeze(1	).expand(-1    ,   self.num_choices    ,   -1	).contiguous()
										__lowerCAmelCase				    =			model(
										    __a    ,   attention_mask=__a    ,   token_type_ids=__a    ,   labels=__a    ,   )
										self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.num_choices)	)



							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			self.prepare_config_and_inputs()
										(
										    (
										    __lowerCAmelCase
										)    ,	(
										    __lowerCAmelCase
										)    ,	(
										    __lowerCAmelCase
										)    ,	(
										    __lowerCAmelCase
										)    ,	(
										    __lowerCAmelCase
										)    ,	(
										    __lowerCAmelCase
										)    ,	(
										    __lowerCAmelCase
										)    ,	
										)				    =			config_and_inputs
										__lowerCAmelCase				    =			{"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		):
							'''simple docstring'''







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

							__UpperCAmelCase						:    List[Any]				      =()

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			MraModelTester(self	)
										__lowerCAmelCase				    =			ConfigTester(self    ,   config_class=__a    ,   hidden_size=37	)

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

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__a	)

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
													__lowerCAmelCase				    =			type
													self.model_tester.create_and_check_model(*__a	)

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*__a	)

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*__a	)

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*__a	)

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*__a	)

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*__a	)

							@slow
							def 			snake_case				(     self	):
										for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__lowerCAmelCase				    =			MraModel.from_pretrained(__a	)
													self.assertIsNotNone(__a	)



							@unittest.skip(reason="MRA does not output attentions"	)
							def 			snake_case				(     self	):
										return
@require_torch
class   _UpperCamelCase  (   unittest.TestCase		):
							'''simple docstring'''







							@slow
							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			MraModel.from_pretrained("uw-madison/mra-base-512-4"	)
										__lowerCAmelCase				    =			torch.arange(2_56	).unsqueeze(0	)

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

										__lowerCAmelCase				    =			torch.Size((1, 2_56, 7_68)	)
										self.assertEqual(output.shape    ,   __a	)

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

							@slow
							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			MraForMaskedLM.from_pretrained("uw-madison/mra-base-512-4"	)
										__lowerCAmelCase				    =			torch.arange(2_56	).unsqueeze(0	)

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

										__lowerCAmelCase				    =			5_02_65

										__lowerCAmelCase				    =			torch.Size((1, 2_56, vocab_size)	)
										self.assertEqual(output.shape    ,   __a	)

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



							@slow
							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			MraForMaskedLM.from_pretrained("uw-madison/mra-base-4096-8-d3"	)
										__lowerCAmelCase				    =			torch.arange(40_96	).unsqueeze(0	)

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

										__lowerCAmelCase				    =			5_02_65

										__lowerCAmelCase				    =			torch.Size((1, 40_96, vocab_size)	)
										self.assertEqual(output.shape    ,   __a	)

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








"""simple docstring"""


import argparse
import os
import re

import packaging.version


A     :   Any			    =			"examples/"
A     :   Optional[Any]			    =			{
    "examples": (re.compile(R"^check_min_version\(\"[^\"]+\"\)\s*$", re.MULTILINE), "check_min_version(\"VERSION\")\n"),
    "init": (re.compile(R"^__version__\s+=\s+\"([^\"]+)\"\s*$", re.MULTILINE), "__version__ = \"VERSION\"\n"),
    "setup": (re.compile(R"^(\s*)version\s*=\s*\"[^\"]+\",", re.MULTILINE), R"\1version=\"VERSION\","),
    "doc": (re.compile(R"^(\s*)release\s*=\s*\"[^\"]+\"$", re.MULTILINE), "release = \"VERSION\"\n"),
}
A     :   Optional[int]			    =			{
    "init": "src/transformers/__init__.py",
    "setup": "setup.py",
}
A     :   List[Any]			    =			"README.md"


def _lowerCamelCase	(		_UpperCamelCase    , _UpperCamelCase    , _UpperCamelCase      ):

			'''simple docstring'''




			with open(_UpperCamelCase    , "r"    , encoding="utf-8"    , newline="\n"      ) as f:
						__lowerCAmelCase				    =			f.read()
			__lowerCAmelCase    ,	__lowerCAmelCase				    =			REPLACE_PATTERNS[pattern]
			__lowerCAmelCase				    =			replace.replace("VERSION"    , _UpperCamelCase      )
			__lowerCAmelCase				    =			re_pattern.sub(_UpperCamelCase    , _UpperCamelCase      )
			with open(_UpperCamelCase    , "w"    , encoding="utf-8"    , newline="\n"      ) as f:
						f.write(_UpperCamelCase      )


def _lowerCamelCase	(		_UpperCamelCase      ):

			'''simple docstring'''




			for folder, directories, fnames in os.walk(_UpperCamelCase      ):
						# Removing some of the folders with non-actively maintained examples from the walk
						if "research_projects" in directories:
									directories.remove("research_projects"      )
						if "legacy" in directories:
									directories.remove("legacy"      )
						for fname in fnames:
									if fname.endswith(".py"      ):
												update_version_in_file(os.path.join(_UpperCamelCase    , _UpperCamelCase      )    , _UpperCamelCase    , pattern="examples"      )


def _lowerCamelCase	(		_UpperCamelCase    , _UpperCamelCase=False      ):

			'''simple docstring'''




			for pattern, fname in REPLACE_FILES.items():
						update_version_in_file(_UpperCamelCase    , _UpperCamelCase    , _UpperCamelCase      )
			if not patch:
						update_version_in_examples(_UpperCamelCase      )


def _lowerCamelCase	(		):

			'''simple docstring'''




			__lowerCAmelCase				    =			"🤗 Transformers currently provides the following architectures"
			__lowerCAmelCase				    =			"1. Want to contribute a new model?"
			with open(_UpperCamelCase    , "r"    , encoding="utf-8"    , newline="\n"      ) as f:
						__lowerCAmelCase				    =			f.readlines()

			# Find the start of the list.
			__lowerCAmelCase				    =			0
			while not lines[start_index].startswith(_start_prompt      ):
						start_index += 1
			start_index += 1

			__lowerCAmelCase				    =			start_index
			# Update the lines in the model list.
			while not lines[index].startswith(_end_prompt      ):
						if lines[index].startswith("1."      ):
									__lowerCAmelCase				    =			lines[index].replace(
									    "https://huggingface.co/docs/transformers/main/model_doc"    , "https://huggingface.co/docs/transformers/model_doc"    , )
						index += 1

			with open(_UpperCamelCase    , "w"    , encoding="utf-8"    , newline="\n"      ) as f:
						f.writelines(_UpperCamelCase      )


def _lowerCamelCase	(		):

			'''simple docstring'''




			with open(REPLACE_FILES["init"]    , "r"      ) as f:
						__lowerCAmelCase				    =			f.read()
			__lowerCAmelCase				    =			REPLACE_PATTERNS["init"][0].search(_UpperCamelCase      ).groups()[0]
			return packaging.version.parse(_UpperCamelCase      )


def _lowerCamelCase	(		_UpperCamelCase=False      ):

			'''simple docstring'''




			__lowerCAmelCase				    =			get_version()
			if patch and default_version.is_devrelease:
						raise ValueError("Can't create a patch version from the dev branch, checkout a released version!"      )
			if default_version.is_devrelease:
						__lowerCAmelCase				    =			default_version.base_version
			elif patch:
						__lowerCAmelCase				    =			f"{default_version.major}.{default_version.minor}.{default_version.micro + 1}"
			else:
						__lowerCAmelCase				    =			f"{default_version.major}.{default_version.minor + 1}.0"

			# Now let's ask nicely if that's the right one.
			__lowerCAmelCase				    =			input(f"Which version are you releasing? [{default_version}]"      )
			if len(_UpperCamelCase      ) == 0:
						__lowerCAmelCase				    =			default_version

			print(f"Updating version to {version}."      )
			global_version_update(_UpperCamelCase    , patch=_UpperCamelCase      )
			if not patch:
						print("Cleaning main README, don't forget to run `make fix-copies`."      )
						clean_main_ref_in_model_list()


def _lowerCamelCase	(		):

			'''simple docstring'''




			__lowerCAmelCase				    =			get_version()
			__lowerCAmelCase				    =			f"{current_version.major}.{current_version.minor + 1}.0.dev0"
			__lowerCAmelCase				    =			current_version.base_version

			# Check with the user we got that right.
			__lowerCAmelCase				    =			input(f"Which version are we developing now? [{dev_version}]"      )
			if len(_UpperCamelCase      ) == 0:
						__lowerCAmelCase				    =			dev_version

			print(f"Updating version to {version}."      )
			global_version_update(_UpperCamelCase      )
			print("Cleaning main README, don't forget to run `make fix-copies`."      )
			clean_main_ref_in_model_list()


if __name__ == "__main__":
	A     :   Union[str, Any]			    =			argparse.ArgumentParser()
	parser.add_argument("--post_release", action="store_true", help="Whether this is pre or post release.")
	parser.add_argument("--patch", action="store_true", help="Whether or not this is a patch release.")
	A     :   Dict			    =			parser.parse_args()
	if not args.post_release:
		pre_release_work(patch=args.patch)
	elif args.patch:
		print("Nothing to do after a patch :-)")
	else:
		post_release_work()




