





'''simple docstring'''


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

  lowerCAmelCase_   :   Optional[int]       =					'''0.12'''  # assumed parallelism: 8


if is_torch_available():
  import torch







def    _lowerCamelCase  (__lowerCamelCase  :     Dict					,  __lowerCamelCase  :     str					,  __lowerCamelCase  :     Any=None     )   ->    List[Any]:

   if rng is None:
      a__     = random.Random()

   a__     = 1
   for dim in shape:
      total_dims *= dim

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

   a__     = np.array(UpperCAmelCase__					,  dtype=jnp.intaa     ).reshape(UpperCAmelCase__     )

   return output







def    _lowerCamelCase  (__lowerCamelCase  :     int					,  __lowerCamelCase  :     Dict=None     )   ->    Dict:
   a__     = ids_tensor(UpperCAmelCase__					,  vocab_size=2					,  rng=UpperCAmelCase__     )
   # make sure that at least one token is attended to for each batch
   a__     = 1
   return attn_mask






@require_flax
class       UpperCamelCase__  :
 lowerCAmelCase__					:			Union[str, Any]								=						None
 lowerCAmelCase__					:			Union[str, Any]								=						()







 def 						__a   (				self			:   List[Any]		):




    '''simple docstring'''
    a__   ,				a__     = self.model_tester.prepare_config_and_inputs_for_common()

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

    a__     = jnp.ones_like(__lowerCAmelCase		)
    a__     = attention_mask[:max_batch_size, :sequence_length]

    # generate max 5 tokens
    a__     = 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()`
       a__     = config.eos_token_id
    return config, input_ids, attention_mask, max_length







 @is_pt_flax_cross_test
 def 						__a   (				self			:   Dict		):




    '''simple docstring'''
    a__   ,				a__   ,				a__   ,				a__     = self._get_input_ids_and_config()
    a__     = False
    a__     = max_length
    a__     = 0

    for model_class in self.all_generative_model_classes:
       a__     = model_class(__lowerCAmelCase		)

       a__     = model_class.__name__[4:]  # Skip the "Flax" at the beginning
       a__     = getattr(__lowerCAmelCase , __lowerCAmelCase		)
       a__     = pt_model_class(__lowerCAmelCase		).eval()
       a__     = load_flax_weights_in_pytorch_model(__lowerCAmelCase , flax_model.params		)

       a__     = flax_model.generate(__lowerCAmelCase		).sequences
       a__     = pt_model.generate(torch.tensor(__lowerCAmelCase , dtype=torch.long		)		)

       if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
          a__     = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

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







 def 						__a   (				self			:   Optional[Any]		):




    '''simple docstring'''
    a__   ,				a__   ,				a__   ,				a__     = self._get_input_ids_and_config()
    a__     = False
    a__     = max_length

    for model_class in self.all_generative_model_classes:
       a__     = model_class(__lowerCAmelCase		)

       a__     = model.generate(__lowerCAmelCase		).sequences
       self.assertEqual(generation_outputs.shape[-1] , __lowerCAmelCase		)

       a__     = jit(model.generate		)
       a__     = jit_generate(__lowerCAmelCase		).sequences

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







 def 						__a   (				self			:   Union[str, Any]		):




    '''simple docstring'''
    a__   ,				a__   ,				a__   ,				a__     = self._get_input_ids_and_config()
    a__     = True
    a__     = max_length

    for model_class in self.all_generative_model_classes:
       a__     = model_class(__lowerCAmelCase		)

       a__     = model.generate(__lowerCAmelCase		).sequences
       self.assertEqual(generation_outputs.shape[-1] , __lowerCAmelCase		)

       a__     = jit(model.generate		)
       a__     = jit_generate(__lowerCAmelCase		).sequences

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







 def 						__a   (				self			:   Dict		):




    '''simple docstring'''
    a__   ,				a__   ,				a__   ,				a__     = self._get_input_ids_and_config()
    a__     = False
    a__     = max_length
    a__     = 2

    for model_class in self.all_generative_model_classes:
       a__     = model_class(__lowerCAmelCase		)

       a__     = model.generate(__lowerCAmelCase		).sequences
       self.assertEqual(generation_outputs.shape[-1] , __lowerCAmelCase		)

       a__     = jit(model.generate		)
       a__     = jit_generate(__lowerCAmelCase		).sequences

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







 def 						__a   (				self			:   Dict		):




    '''simple docstring'''
    a__   ,				a__   ,				a__   ,				a__     = self._get_input_ids_and_config()
    a__     = False
    a__     = max_length
    a__     = 2
    a__     = 2

    for model_class in self.all_generative_model_classes:
       a__     = model_class(__lowerCAmelCase		)

       a__     = model.generate(__lowerCAmelCase		).sequences
       self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences		)







 def 						__a   (				self			:   str		):




    '''simple docstring'''
    a__   ,				a__   ,				a__   ,				a__     = self._get_input_ids_and_config()
    a__     = True
    a__     = max_length
    a__     = 0.8
    a__     = 1_0
    a__     = 0.3
    a__     = 1
    a__     = 8
    a__     = 9

    for model_class in self.all_generative_model_classes:
       a__     = model_class(__lowerCAmelCase		)

       a__     = model.generate(__lowerCAmelCase		).sequences
       self.assertEqual(generation_outputs.shape[-1] , __lowerCAmelCase		)

       a__     = jit(model.generate		)
       a__     = jit_generate(__lowerCAmelCase		).sequences

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







 def 						__a   (				self			:   Optional[int]		):




    '''simple docstring'''
    a__   ,				a__   ,				a__   ,				a__     = self._get_input_ids_and_config()
    a__     = max_length
    a__     = 1
    a__     = 8
    a__     = 9

    for model_class in self.all_generative_model_classes:
       a__     = model_class(__lowerCAmelCase		)

       a__     = model.generate(__lowerCAmelCase		).sequences
       self.assertEqual(generation_outputs.shape[-1] , __lowerCAmelCase		)

       a__     = jit(model.generate		)
       a__     = jit_generate(__lowerCAmelCase		).sequences

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







 def 						__a   (				self			:   str		):




    '''simple docstring'''
    a__   ,				a__   ,				a__   ,				a__     = self._get_input_ids_and_config()
    a__     = max_length
    a__     = 2
    a__     = 1
    a__     = 8
    a__     = 9

    for model_class in self.all_generative_model_classes:
       a__     = model_class(__lowerCAmelCase		)

       a__     = model.generate(__lowerCAmelCase		).sequences
       self.assertEqual(generation_outputs.shape[-1] , __lowerCAmelCase		)

       a__     = jit(model.generate		)
       a__     = jit_generate(__lowerCAmelCase		).sequences

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







 def 						__a   (				self			:   Optional[int]		):




    '''simple docstring'''
    a__   ,				a__   ,				a__   ,				a__     = self._get_input_ids_and_config()

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

    a__     = False
    a__     = max_length

    for model_class in self.all_generative_model_classes:
       a__     = model_class(__lowerCAmelCase		)

       a__     = model.generate(__lowerCAmelCase , attention_mask=__lowerCAmelCase		).sequences
       self.assertEqual(generation_outputs.shape[-1] , __lowerCAmelCase		)

       a__     = jit(model.generate		)
       a__     = jit_generate(__lowerCAmelCase , attention_mask=__lowerCAmelCase		).sequences

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







 def 						__a   (				self			:   int		):




    '''simple docstring'''
    a__   ,				a__   ,				a__   ,				a__     = self._get_input_ids_and_config()

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

    a__     = True
    a__     = max_length

    for model_class in self.all_generative_model_classes:
       a__     = model_class(__lowerCAmelCase		)

       a__     = model.generate(__lowerCAmelCase , attention_mask=__lowerCAmelCase		).sequences
       self.assertEqual(generation_outputs.shape[-1] , __lowerCAmelCase		)

       a__     = jit(model.generate		)
       a__     = jit_generate(__lowerCAmelCase , attention_mask=__lowerCAmelCase		).sequences

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







 def 						__a   (				self			:   Dict		):




    '''simple docstring'''
    a__   ,				a__   ,				a__   ,				a__     = self._get_input_ids_and_config()

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

    a__     = 2
    a__     = max_length

    for model_class in self.all_generative_model_classes:
       a__     = model_class(__lowerCAmelCase		)

       a__     = model.generate(__lowerCAmelCase , attention_mask=__lowerCAmelCase		).sequences
       self.assertEqual(generation_outputs.shape[-1] , __lowerCAmelCase		)

       a__     = jit(model.generate		)
       a__     = jit_generate(__lowerCAmelCase , attention_mask=__lowerCAmelCase		).sequences

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






@require_flax
class       UpperCamelCase__  (		unittest.TestCase     ):







 def 						__a   (				self			:   Dict		):




    '''simple docstring'''
    a__     = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-bert"		)
    a__     = FlaxAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-bert-flax-only"		)

    a__     = "Hello world"
    a__     = tokenizer(__lowerCAmelCase , return_tensors="np"		).input_ids

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

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










'''simple docstring'''


import argparse
import os
import re

import packaging.version


lowerCAmelCase_   :   Optional[int]       =					"examples/"
lowerCAmelCase_   :   Optional[int]       =					{
    "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"),
}
lowerCAmelCase_   :   Optional[int]       =					{
    "init": "src/transformers/__init__.py",
    "setup": "setup.py",
}
lowerCAmelCase_   :   List[Any]       =					"README.md"







def    _lowerCamelCase  (__lowerCamelCase  :     Dict					,  __lowerCamelCase  :     List[Any]					,  __lowerCamelCase  :     Optional[Any]     )   ->    str:
			with open(__lowerCamelCase					,  "r"					,  encoding="utf-8"					,  newline="\n"     ) as f:
						a__     = f.read()
			a__   ,				a__     = REPLACE_PATTERNS[pattern]
			a__     = replace.replace("VERSION"					,  __lowerCamelCase     )
			a__     = re_pattern.sub(__lowerCamelCase					,  __lowerCamelCase     )
			with open(__lowerCamelCase					,  "w"					,  encoding="utf-8"					,  newline="\n"     ) as f:
						f.write(__lowerCamelCase     )







def    _lowerCamelCase  (__lowerCamelCase  :     Union[str, Any]     )   ->    Dict:
			for folder, directories, fnames in os.walk(__lowerCamelCase     ):
						# 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(__lowerCamelCase					,  __lowerCamelCase     )					,  __lowerCamelCase					,  pattern="examples"     )







def    _lowerCamelCase  (__lowerCamelCase  :     int					,  __lowerCamelCase  :     Any=False     )   ->    Optional[Any]:
			for pattern, fname in REPLACE_FILES.items():
						update_version_in_file(__lowerCamelCase					,  __lowerCamelCase					,  __lowerCamelCase     )
			if not patch:
						update_version_in_examples(__lowerCamelCase     )







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

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

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

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







def    _lowerCamelCase  ()   ->    Tuple:
			with open(REPLACE_FILES["init"]					,  "r"     ) as f:
						a__     = f.read()
			a__     = REPLACE_PATTERNS["init"][0].search(__lowerCamelCase     ).groups()[0]
			return packaging.version.parse(__lowerCamelCase     )







def    _lowerCamelCase  (__lowerCamelCase  :     Any=False     )   ->    int:
			a__     = 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:
						a__     = default_version.base_version
			elif patch:
						a__     = f'''{default_version.major}.{default_version.minor}.{default_version.micro + 1}'''
			else:
						a__     = f'''{default_version.major}.{default_version.minor + 1}.0'''

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

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







def    _lowerCamelCase  ()   ->    Tuple:
			a__     = get_version()
			a__     = f'''{current_version.major}.{current_version.minor + 1}.0.dev0'''
			a__     = current_version.base_version

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

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


if __name__ == "__main__":
		lowerCAmelCase_   :   List[str]       =					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.")
		lowerCAmelCase_   :   int       =					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()



