



import argparse
import os
import re

import packaging.version


_lowercase								=						"""examples/"""
_lowercase								=						{
    """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"""),
}
_lowercase								=						{
    """init""": """src/transformers/__init__.py""",
    """setup""": """setup.py""",
}
_lowercase								=						"""README.md"""






def      _A				(UpperCamelCase		:      int      ,			UpperCamelCase		:      str      ,			UpperCamelCase		:      Optional[Any] )  ->Optional[Any]:



			'''simple docstring'''







			with open(UpperCamelCase      ,			"""r"""      ,			encoding="""utf-8"""      ,			newline="""\n""" ) as f:
						lowerCamelCase__ :      int								=		f.read()
			lowerCamelCase__							,lowerCamelCase__ :      Union[str, Any]								=		REPLACE_PATTERNS[pattern]
			lowerCamelCase__ :      List[Any]								=		replace.replace("""VERSION"""      ,			UpperCamelCase )
			lowerCamelCase__ :      str								=		re_pattern.sub(UpperCamelCase      ,			UpperCamelCase )
			with open(UpperCamelCase      ,			"""w"""      ,			encoding="""utf-8"""      ,			newline="""\n""" ) as f:
						f.write(UpperCamelCase )






def      _A				(UpperCamelCase		:      Optional[int] )  ->int:



			'''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      _A				(UpperCamelCase		:      str      ,			UpperCamelCase		:      Dict=False )  ->str:



			'''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      _A				()  ->int:



			'''simple docstring'''







			lowerCamelCase__ :      Tuple								=		"""🤗 Transformers currently provides the following architectures"""
			lowerCamelCase__ :      Any								=		"""1. Want to contribute a new model?"""
			with open(UpperCamelCase      ,			"""r"""      ,			encoding="""utf-8"""      ,			newline="""\n""" ) as f:
						lowerCamelCase__ :      Union[str, Any]								=		f.readlines()

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

			lowerCamelCase__ :      str								=		start_index
			# Update the lines in the model list.
			while not lines[index].startswith(_end_prompt ):
						if lines[index].startswith("""1.""" ):
									lowerCamelCase__ :      List[Any]								=		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      _A				()  ->Optional[Any]:



			'''simple docstring'''







			with open(REPLACE_FILES["""init"""]      ,			"""r""" ) as f:
						lowerCamelCase__ :      Dict								=		f.read()
			lowerCamelCase__ :      List[Any]								=		REPLACE_PATTERNS["""init"""][0].search(UpperCamelCase ).groups()[0]
			return packaging.version.parse(UpperCamelCase )






def      _A				(UpperCamelCase		:      Tuple=False )  ->int:



			'''simple docstring'''







			lowerCamelCase__ :      Tuple								=		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__ :      List[Any]								=		default_version.base_version
			elif patch:
						lowerCamelCase__ :      int								=		f"{default_version.major}.{default_version.minor}.{default_version.micro + 1}"
			else:
						lowerCamelCase__ :      str								=		f"{default_version.major}.{default_version.minor + 1}.0"

			# Now let's ask nicely if that's the right one.
			lowerCamelCase__ :      Tuple								=		input(f"Which version are you releasing? [{default_version}]" )
			if len(UpperCamelCase ) == 0:
						lowerCamelCase__ :      Dict								=		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      _A				()  ->Optional[int]:



			'''simple docstring'''







			lowerCamelCase__ :      Optional[int]								=		get_version()
			lowerCamelCase__ :      List[str]								=		f"{current_version.major}.{current_version.minor + 1}.0.dev0"
			lowerCamelCase__ :      Optional[int]								=		current_version.base_version

			# Check with the user we got that right.
			lowerCamelCase__ :      Any								=		input(f"Which version are we developing now? [{dev_version}]" )
			if len(UpperCamelCase ) == 0:
						lowerCamelCase__ :      int								=		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__":
				_lowercase								=						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.''')
				_lowercase								=						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()








import random
import unittest

import numpy as np

import transformers
from transformers import is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax


if is_flax_available():
				import os

				import jax.numpy as jnp
				from jax import jit

				from transformers import AutoTokenizer, FlaxAutoModelForCausalLM
				from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model

				_lowercase     							=						'''0.12'''  # assumed parallelism: 8


if is_torch_available():
				import torch






def      _A				(UpperCamelCase		:      Dict      ,			UpperCamelCase		:      int      ,			UpperCamelCase		:      Optional[Any]=None )  ->Optional[int]:



			'''simple docstring'''







			if rng is None:
						lowerCamelCase__ :      List[Any]								=		random.Random()

			lowerCamelCase__ :      Tuple								=		1
			for dim in shape:
						total_dims *= dim

			lowerCamelCase__ :      str								=		[]
			for _ in range(UpperCamelCase ):
						values.append(rng.randint(0      ,			vocab_size - 1 ) )

			lowerCamelCase__ :      str								=		np.array(UpperCamelCase      ,			dtype=jnp.intaa ).reshape(UpperCamelCase )

			return output






def      _A				(UpperCamelCase		:      str      ,			UpperCamelCase		:      Dict=None )  ->List[str]:



			'''simple docstring'''







			lowerCamelCase__ :      Any								=		ids_tensor(UpperCamelCase      ,			vocab_size=2      ,			rng=UpperCamelCase )
			# make sure that at least one token is attended to for each batch
			lowerCamelCase__ :      int								=		1
			return attn_mask
@require_flax
class 				__A					:
			UpperCamelCase					:List[Any]     =			None
			UpperCamelCase					:Dict     =			()





			def        _snake_case    (self				):
						lowerCamelCase__							,lowerCamelCase__ :      Dict								=		self.model_tester.prepare_config_and_inputs_for_common()

						# cut to half length & take max batch_size 3
						lowerCamelCase__ :      List[Any]								=		2
						lowerCamelCase__ :      Any								=		inputs["""input_ids"""].shape[-1] // 2
						lowerCamelCase__ :      Dict								=		inputs["""input_ids"""][:max_batch_size, :sequence_length]

						lowerCamelCase__ :      List[Any]								=		jnp.ones_like(__magic_name__				)
						lowerCamelCase__ :      Dict								=		attention_mask[:max_batch_size, :sequence_length]

						# generate max 5 tokens
						lowerCamelCase__ :      Tuple								=		input_ids.shape[-1] + 5
						if config.eos_token_id is not None and config.pad_token_id is None:
									# hack to allow generate for models such as GPT2 as is done in `generate()`
									lowerCamelCase__ :      List[Any]								=		config.eos_token_id
						return config, input_ids, attention_mask, max_length





			@is_pt_flax_cross_test
			def        _snake_case    (self				):
						lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__ :      List[Any]								=		self._get_input_ids_and_config()
						lowerCamelCase__ :      Tuple								=		False
						lowerCamelCase__ :      Tuple								=		max_length
						lowerCamelCase__ :      List[Any]								=		0

						for model_class in self.all_generative_model_classes:
									lowerCamelCase__ :      List[Any]								=		model_class(__magic_name__				)

									lowerCamelCase__ :      Tuple								=		model_class.__name__[4:]  # Skip the "Flax" at the beginning
									lowerCamelCase__ :      Union[str, Any]								=		getattr(__magic_name__							,    __magic_name__				)
									lowerCamelCase__ :      Dict								=		pt_model_class(__magic_name__				).eval()
									lowerCamelCase__ :      Union[str, Any]								=		load_flax_weights_in_pytorch_model(__magic_name__							,    flax_model.params				)

									lowerCamelCase__ :      str								=		flax_model.generate(__magic_name__				).sequences
									lowerCamelCase__ :      str								=		pt_model.generate(torch.tensor(__magic_name__							,    dtype=torch.long				)				)

									if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
												lowerCamelCase__ :      Optional[Any]								=		flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

									self.assertListEqual(pt_generation_outputs.numpy().tolist()							,    flax_generation_outputs.tolist()				)





			def        _snake_case    (self				):
						lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__ :      Tuple								=		self._get_input_ids_and_config()
						lowerCamelCase__ :      Dict								=		False
						lowerCamelCase__ :      str								=		max_length

						for model_class in self.all_generative_model_classes:
									lowerCamelCase__ :      Tuple								=		model_class(__magic_name__				)

									lowerCamelCase__ :      int								=		model.generate(__magic_name__				).sequences
									self.assertEqual(generation_outputs.shape[-1]							,    __magic_name__				)

									lowerCamelCase__ :      int								=		jit(model.generate				)
									lowerCamelCase__ :      List[str]								=		jit_generate(__magic_name__				).sequences

									self.assertListEqual(generation_outputs.tolist()							,    jit_generation_outputs.tolist()				)





			def        _snake_case    (self				):
						lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__ :      Union[str, Any]								=		self._get_input_ids_and_config()
						lowerCamelCase__ :      List[str]								=		True
						lowerCamelCase__ :      Dict								=		max_length

						for model_class in self.all_generative_model_classes:
									lowerCamelCase__ :      str								=		model_class(__magic_name__				)

									lowerCamelCase__ :      Dict								=		model.generate(__magic_name__				).sequences
									self.assertEqual(generation_outputs.shape[-1]							,    __magic_name__				)

									lowerCamelCase__ :      Tuple								=		jit(model.generate				)
									lowerCamelCase__ :      List[str]								=		jit_generate(__magic_name__				).sequences

									self.assertListEqual(generation_outputs.tolist()							,    jit_generation_outputs.tolist()				)





			def        _snake_case    (self				):
						lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__ :      Dict								=		self._get_input_ids_and_config()
						lowerCamelCase__ :      Union[str, Any]								=		False
						lowerCamelCase__ :      Dict								=		max_length
						lowerCamelCase__ :      str								=		2

						for model_class in self.all_generative_model_classes:
									lowerCamelCase__ :      Optional[int]								=		model_class(__magic_name__				)

									lowerCamelCase__ :      Union[str, Any]								=		model.generate(__magic_name__				).sequences
									self.assertEqual(generation_outputs.shape[-1]							,    __magic_name__				)

									lowerCamelCase__ :      Dict								=		jit(model.generate				)
									lowerCamelCase__ :      Optional[int]								=		jit_generate(__magic_name__				).sequences

									self.assertListEqual(generation_outputs.tolist()							,    jit_generation_outputs.tolist()				)





			def        _snake_case    (self				):
						lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__ :      int								=		self._get_input_ids_and_config()
						lowerCamelCase__ :      Dict								=		False
						lowerCamelCase__ :      List[Any]								=		max_length
						lowerCamelCase__ :      List[Any]								=		2
						lowerCamelCase__ :      List[str]								=		2

						for model_class in self.all_generative_model_classes:
									lowerCamelCase__ :      Optional[Any]								=		model_class(__magic_name__				)

									lowerCamelCase__ :      Union[str, Any]								=		model.generate(__magic_name__				).sequences
									self.assertEqual(generation_outputs.shape[0]							,    input_ids.shape[0] * config.num_return_sequences				)





			def        _snake_case    (self				):
						lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__ :      Tuple								=		self._get_input_ids_and_config()
						lowerCamelCase__ :      Optional[int]								=		True
						lowerCamelCase__ :      str								=		max_length
						lowerCamelCase__ :      Optional[int]								=		0.8
						lowerCamelCase__ :      List[Any]								=		10
						lowerCamelCase__ :      int								=		0.3
						lowerCamelCase__ :      Union[str, Any]								=		1
						lowerCamelCase__ :      int								=		8
						lowerCamelCase__ :      Tuple								=		9

						for model_class in self.all_generative_model_classes:
									lowerCamelCase__ :      Optional[Any]								=		model_class(__magic_name__				)

									lowerCamelCase__ :      Any								=		model.generate(__magic_name__				).sequences
									self.assertEqual(generation_outputs.shape[-1]							,    __magic_name__				)

									lowerCamelCase__ :      Optional[int]								=		jit(model.generate				)
									lowerCamelCase__ :      List[Any]								=		jit_generate(__magic_name__				).sequences

									self.assertListEqual(generation_outputs.tolist()							,    jit_generation_outputs.tolist()				)





			def        _snake_case    (self				):
						lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__ :      List[str]								=		self._get_input_ids_and_config()
						lowerCamelCase__ :      str								=		max_length
						lowerCamelCase__ :      List[str]								=		1
						lowerCamelCase__ :      Optional[int]								=		8
						lowerCamelCase__ :      Union[str, Any]								=		9

						for model_class in self.all_generative_model_classes:
									lowerCamelCase__ :      Dict								=		model_class(__magic_name__				)

									lowerCamelCase__ :      Tuple								=		model.generate(__magic_name__				).sequences
									self.assertEqual(generation_outputs.shape[-1]							,    __magic_name__				)

									lowerCamelCase__ :      Optional[int]								=		jit(model.generate				)
									lowerCamelCase__ :      Dict								=		jit_generate(__magic_name__				).sequences

									self.assertListEqual(generation_outputs.tolist()							,    jit_generation_outputs.tolist()				)





			def        _snake_case    (self				):
						lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__ :      int								=		self._get_input_ids_and_config()
						lowerCamelCase__ :      Tuple								=		max_length
						lowerCamelCase__ :      Optional[int]								=		2
						lowerCamelCase__ :      Union[str, Any]								=		1
						lowerCamelCase__ :      Tuple								=		8
						lowerCamelCase__ :      Tuple								=		9

						for model_class in self.all_generative_model_classes:
									lowerCamelCase__ :      Optional[int]								=		model_class(__magic_name__				)

									lowerCamelCase__ :      List[Any]								=		model.generate(__magic_name__				).sequences
									self.assertEqual(generation_outputs.shape[-1]							,    __magic_name__				)

									lowerCamelCase__ :      List[Any]								=		jit(model.generate				)
									lowerCamelCase__ :      Union[str, Any]								=		jit_generate(__magic_name__				).sequences

									self.assertListEqual(generation_outputs.tolist()							,    jit_generation_outputs.tolist()				)





			def        _snake_case    (self				):
						lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__ :      Tuple								=		self._get_input_ids_and_config()

						# pad attention mask on the left
						lowerCamelCase__ :      Any								=		attention_mask.at[(0, 0)].set(0				)

						lowerCamelCase__ :      List[str]								=		False
						lowerCamelCase__ :      str								=		max_length

						for model_class in self.all_generative_model_classes:
									lowerCamelCase__ :      Dict								=		model_class(__magic_name__				)

									lowerCamelCase__ :      List[str]								=		model.generate(__magic_name__							,    attention_mask=__magic_name__				).sequences
									self.assertEqual(generation_outputs.shape[-1]							,    __magic_name__				)

									lowerCamelCase__ :      Tuple								=		jit(model.generate				)
									lowerCamelCase__ :      Any								=		jit_generate(__magic_name__							,    attention_mask=__magic_name__				).sequences

									self.assertListEqual(generation_outputs.tolist()							,    jit_generation_outputs.tolist()				)





			def        _snake_case    (self				):
						lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__ :      Optional[Any]								=		self._get_input_ids_and_config()

						# pad attention mask on the left
						lowerCamelCase__ :      Optional[Any]								=		attention_mask.at[(0, 0)].set(0				)

						lowerCamelCase__ :      int								=		True
						lowerCamelCase__ :      str								=		max_length

						for model_class in self.all_generative_model_classes:
									lowerCamelCase__ :      int								=		model_class(__magic_name__				)

									lowerCamelCase__ :      str								=		model.generate(__magic_name__							,    attention_mask=__magic_name__				).sequences
									self.assertEqual(generation_outputs.shape[-1]							,    __magic_name__				)

									lowerCamelCase__ :      List[Any]								=		jit(model.generate				)
									lowerCamelCase__ :      List[Any]								=		jit_generate(__magic_name__							,    attention_mask=__magic_name__				).sequences

									self.assertListEqual(generation_outputs.tolist()							,    jit_generation_outputs.tolist()				)





			def        _snake_case    (self				):
						lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__							,lowerCamelCase__ :      Optional[Any]								=		self._get_input_ids_and_config()

						# pad attention mask on the left
						lowerCamelCase__ :      Union[str, Any]								=		attention_mask.at[(0, 0)].set(0				)

						lowerCamelCase__ :      Tuple								=		2
						lowerCamelCase__ :      List[Any]								=		max_length

						for model_class in self.all_generative_model_classes:
									lowerCamelCase__ :      Optional[Any]								=		model_class(__magic_name__				)

									lowerCamelCase__ :      Union[str, Any]								=		model.generate(__magic_name__							,    attention_mask=__magic_name__				).sequences
									self.assertEqual(generation_outputs.shape[-1]							,    __magic_name__				)

									lowerCamelCase__ :      int								=		jit(model.generate				)
									lowerCamelCase__ :      Any								=		jit_generate(__magic_name__							,    attention_mask=__magic_name__				).sequences

									self.assertListEqual(generation_outputs.tolist()							,    jit_generation_outputs.tolist()				)







@require_flax
class 				__A					( unittest.TestCase      ):





			def        _snake_case    (self				):
						lowerCamelCase__ :      Dict								=		AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-bert"""				)
						lowerCamelCase__ :      Tuple								=		FlaxAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-bert-flax-only"""				)

						lowerCamelCase__ :      Dict								=		"""Hello world"""
						lowerCamelCase__ :      Union[str, Any]								=		tokenizer(__magic_name__							,    return_tensors="""np"""				).input_ids

						# typos are quickly detected (the correct argument is `do_sample`)
						with self.assertRaisesRegex(__magic_name__							,    """do_samples"""				):
									model.generate(__magic_name__							,    do_samples=__magic_name__				)

						# arbitrary arguments that will not be used anywhere are also not accepted
						with self.assertRaisesRegex(__magic_name__							,    """foo"""				):
									lowerCamelCase__ :      Tuple								=		{"""foo""": """bar"""}
									model.generate(__magic_name__							,    **__magic_name__				)



