




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

		_A													=      """0.12"""  # assumed parallelism: 8


if is_torch_available():
		import torch



def 	lowercase_				(			__UpperCAmelCase    ,		__UpperCAmelCase    ,		__UpperCAmelCase=None				)						->    List[Any]:
						if rng is None:
												lowerCAmelCase__      :			Any   =	random.Random()

						lowerCAmelCase__      :			str   =	1
						for dim in shape:
												total_dims *= dim

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

						lowerCAmelCase__      :			Dict   =	np.array(__UpperCAmelCase    ,		dtype=jnp.intaa				).reshape(__UpperCAmelCase				)

						return output



def 	lowercase_				(			__UpperCAmelCase    ,		__UpperCAmelCase=None				)						->    List[Any]:
						lowerCAmelCase__      :			Dict   =	ids_tensor(__UpperCAmelCase    ,		vocab_size=2    ,		rng=__UpperCAmelCase				)
						# make sure that at least one token is attended to for each batch
						lowerCAmelCase__      :			str   =	1
						return attn_mask


@require_flax
class _lowerCamelCase    :
			_lowerCamelCase    :Optional[int]     				=     None
			_lowerCamelCase    :Optional[Any]     				=     ()






			def 	_lowerCAmelCase   (		self       :		Optional[Any]   )			->						int:

									"""simple docstring"""
									lowerCAmelCase__      :			List[str]   =	self.model_tester.prepare_config_and_inputs_for_common()

									# cut to half length & take max batch_size 3
									lowerCAmelCase__      :			Optional[Any]   =	2
									lowerCAmelCase__      :			Union[str, Any]   =	inputs["""input_ids"""].shape[-1] // 2
									lowerCAmelCase__      :			Tuple   =	inputs["""input_ids"""][:max_batch_size, :sequence_length]

									lowerCAmelCase__      :			str   =	jnp.ones_like(UpperCamelCase   )
									lowerCAmelCase__      :			Tuple   =	attention_mask[:max_batch_size, :sequence_length]

									# generate max 5 tokens
									lowerCAmelCase__      :			Union[str, Any]   =	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__      :			Dict   =	config.eos_token_id
									return config, input_ids, attention_mask, max_length






			@is_pt_flax_cross_test
			def 	_lowerCAmelCase   (		self       :		Dict   )			->						str:

									"""simple docstring"""
									lowerCAmelCase__      :			Union[str, Any]   =	self._get_input_ids_and_config()
									lowerCAmelCase__      :			Tuple   =	False
									lowerCAmelCase__      :			Optional[int]   =	max_length
									lowerCAmelCase__      :			Any   =	0

									for model_class in self.all_generative_model_classes:
															lowerCAmelCase__      :			List[str]   =	model_class(UpperCamelCase   )

															lowerCAmelCase__      :			Optional[Any]   =	model_class.__name__[4:]  # Skip the "Flax" at the beginning
															lowerCAmelCase__      :			Tuple   =	getattr(UpperCamelCase   ,     UpperCamelCase   )
															lowerCAmelCase__      :			List[Any]   =	pt_model_class(UpperCamelCase   ).eval()
															lowerCAmelCase__      :			Dict   =	load_flax_weights_in_pytorch_model(UpperCamelCase   ,     flax_model.params   )

															lowerCAmelCase__      :			Union[str, Any]   =	flax_model.generate(UpperCamelCase   ).sequences
															lowerCAmelCase__      :			List[Any]   =	pt_model.generate(torch.tensor(UpperCamelCase   ,     dtype=torch.long   )   )

															if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
																					lowerCAmelCase__      :			Union[str, Any]   =	flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

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






			def 	_lowerCAmelCase   (		self       :		Union[str, Any]   )			->						Tuple:

									"""simple docstring"""
									lowerCAmelCase__      :			Optional[int]   =	self._get_input_ids_and_config()
									lowerCAmelCase__      :			str   =	False
									lowerCAmelCase__      :			Optional[Any]   =	max_length

									for model_class in self.all_generative_model_classes:
															lowerCAmelCase__      :			int   =	model_class(UpperCamelCase   )

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

															lowerCAmelCase__      :			Union[str, Any]   =	jit(model.generate   )
															lowerCAmelCase__      :			Tuple   =	jit_generate(UpperCamelCase   ).sequences

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






			def 	_lowerCAmelCase   (		self       :		Union[str, Any]   )			->						Any:

									"""simple docstring"""
									lowerCAmelCase__      :			Union[str, Any]   =	self._get_input_ids_and_config()
									lowerCAmelCase__      :			List[Any]   =	True
									lowerCAmelCase__      :			List[Any]   =	max_length

									for model_class in self.all_generative_model_classes:
															lowerCAmelCase__      :			List[str]   =	model_class(UpperCamelCase   )

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

															lowerCAmelCase__      :			List[Any]   =	jit(model.generate   )
															lowerCAmelCase__      :			int   =	jit_generate(UpperCamelCase   ).sequences

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






			def 	_lowerCAmelCase   (		self       :		int   )			->						int:

									"""simple docstring"""
									lowerCAmelCase__      :			Any   =	self._get_input_ids_and_config()
									lowerCAmelCase__      :			Any   =	False
									lowerCAmelCase__      :			int   =	max_length
									lowerCAmelCase__      :			List[Any]   =	2

									for model_class in self.all_generative_model_classes:
															lowerCAmelCase__      :			List[str]   =	model_class(UpperCamelCase   )

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

															lowerCAmelCase__      :			Tuple   =	jit(model.generate   )
															lowerCAmelCase__      :			List[Any]   =	jit_generate(UpperCamelCase   ).sequences

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






			def 	_lowerCAmelCase   (		self       :		List[Any]   )			->						Dict:

									"""simple docstring"""
									lowerCAmelCase__      :			str   =	self._get_input_ids_and_config()
									lowerCAmelCase__      :			Dict   =	False
									lowerCAmelCase__      :			int   =	max_length
									lowerCAmelCase__      :			Any   =	2
									lowerCAmelCase__      :			Any   =	2

									for model_class in self.all_generative_model_classes:
															lowerCAmelCase__      :			Union[str, Any]   =	model_class(UpperCamelCase   )

															lowerCAmelCase__      :			Optional[int]   =	model.generate(UpperCamelCase   ).sequences
															self.assertEqual(generation_outputs.shape[0]   ,     input_ids.shape[0] * config.num_return_sequences   )






			def 	_lowerCAmelCase   (		self       :		List[str]   )			->						Any:

									"""simple docstring"""
									lowerCAmelCase__      :			Dict   =	self._get_input_ids_and_config()
									lowerCAmelCase__      :			List[Any]   =	True
									lowerCAmelCase__      :			Optional[int]   =	max_length
									lowerCAmelCase__      :			Optional[int]   =	0.8
									lowerCAmelCase__      :			Any   =	10
									lowerCAmelCase__      :			Any   =	0.3
									lowerCAmelCase__      :			Tuple   =	1
									lowerCAmelCase__      :			Optional[Any]   =	8
									lowerCAmelCase__      :			Tuple   =	9

									for model_class in self.all_generative_model_classes:
															lowerCAmelCase__      :			Any   =	model_class(UpperCamelCase   )

															lowerCAmelCase__      :			List[str]   =	model.generate(UpperCamelCase   ).sequences
															self.assertEqual(generation_outputs.shape[-1]   ,     UpperCamelCase   )

															lowerCAmelCase__      :			str   =	jit(model.generate   )
															lowerCAmelCase__      :			Any   =	jit_generate(UpperCamelCase   ).sequences

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






			def 	_lowerCAmelCase   (		self       :		Tuple   )			->						List[str]:

									"""simple docstring"""
									lowerCAmelCase__      :			Dict   =	self._get_input_ids_and_config()
									lowerCAmelCase__      :			int   =	max_length
									lowerCAmelCase__      :			Optional[Any]   =	1
									lowerCAmelCase__      :			Tuple   =	8
									lowerCAmelCase__      :			Union[str, Any]   =	9

									for model_class in self.all_generative_model_classes:
															lowerCAmelCase__      :			Union[str, Any]   =	model_class(UpperCamelCase   )

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

															lowerCAmelCase__      :			Tuple   =	jit(model.generate   )
															lowerCAmelCase__      :			int   =	jit_generate(UpperCamelCase   ).sequences

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






			def 	_lowerCAmelCase   (		self       :		Tuple   )			->						int:

									"""simple docstring"""
									lowerCAmelCase__      :			Any   =	self._get_input_ids_and_config()
									lowerCAmelCase__      :			Tuple   =	max_length
									lowerCAmelCase__      :			str   =	2
									lowerCAmelCase__      :			Tuple   =	1
									lowerCAmelCase__      :			List[Any]   =	8
									lowerCAmelCase__      :			int   =	9

									for model_class in self.all_generative_model_classes:
															lowerCAmelCase__      :			Dict   =	model_class(UpperCamelCase   )

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

															lowerCAmelCase__      :			Optional[Any]   =	jit(model.generate   )
															lowerCAmelCase__      :			Optional[int]   =	jit_generate(UpperCamelCase   ).sequences

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






			def 	_lowerCAmelCase   (		self       :		Any   )			->						Tuple:

									"""simple docstring"""
									lowerCAmelCase__      :			Optional[int]   =	self._get_input_ids_and_config()

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

									lowerCAmelCase__      :			str   =	False
									lowerCAmelCase__      :			Any   =	max_length

									for model_class in self.all_generative_model_classes:
															lowerCAmelCase__      :			Any   =	model_class(UpperCamelCase   )

															lowerCAmelCase__      :			Dict   =	model.generate(UpperCamelCase   ,     attention_mask=UpperCamelCase   ).sequences
															self.assertEqual(generation_outputs.shape[-1]   ,     UpperCamelCase   )

															lowerCAmelCase__      :			Any   =	jit(model.generate   )
															lowerCAmelCase__      :			Dict   =	jit_generate(UpperCamelCase   ,     attention_mask=UpperCamelCase   ).sequences

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






			def 	_lowerCAmelCase   (		self       :		List[Any]   )			->						Tuple:

									"""simple docstring"""
									lowerCAmelCase__      :			Dict   =	self._get_input_ids_and_config()

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

									lowerCAmelCase__      :			Dict   =	True
									lowerCAmelCase__      :			Union[str, Any]   =	max_length

									for model_class in self.all_generative_model_classes:
															lowerCAmelCase__      :			str   =	model_class(UpperCamelCase   )

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

															lowerCAmelCase__      :			List[str]   =	jit(model.generate   )
															lowerCAmelCase__      :			Tuple   =	jit_generate(UpperCamelCase   ,     attention_mask=UpperCamelCase   ).sequences

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






			def 	_lowerCAmelCase   (		self       :		Optional[int]   )			->						str:

									"""simple docstring"""
									lowerCAmelCase__      :			Optional[int]   =	self._get_input_ids_and_config()

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

									lowerCAmelCase__      :			Optional[int]   =	2
									lowerCAmelCase__      :			Optional[int]   =	max_length

									for model_class in self.all_generative_model_classes:
															lowerCAmelCase__      :			Tuple   =	model_class(UpperCamelCase   )

															lowerCAmelCase__      :			Optional[int]   =	model.generate(UpperCamelCase   ,     attention_mask=UpperCamelCase   ).sequences
															self.assertEqual(generation_outputs.shape[-1]   ,     UpperCamelCase   )

															lowerCAmelCase__      :			int   =	jit(model.generate   )
															lowerCAmelCase__      :			Union[str, Any]   =	jit_generate(UpperCamelCase   ,     attention_mask=UpperCamelCase   ).sequences

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






@require_flax
class _lowerCamelCase    (					unittest.TestCase						):






			def 	_lowerCAmelCase   (		self       :		Optional[int]   )			->						int:

									"""simple docstring"""
									lowerCAmelCase__      :			Optional[int]   =	AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-bert"""   )
									lowerCAmelCase__      :			str   =	FlaxAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-bert-flax-only"""   )

									lowerCAmelCase__      :			List[str]   =	"""Hello world"""
									lowerCAmelCase__      :			Dict   =	tokenizer(UpperCamelCase   ,     return_tensors="""np"""   ).input_ids

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

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











"""simple docstring"""


from argparse import ArgumentParser

from . import BaseTransformersCLICommand



def 	lowercase_				(			__UpperCAmelCase				)						->    List[str]:
      return DownloadCommand(args.model    ,		args.cache_dir    ,		args.force    ,		args.trust_remote_code				)


class _lowerCamelCase    (					a_						):






   @staticmethod
   def 	_lowerCAmelCase   (		UpperCamelCase       :		ArgumentParser   )			->						List[Any]:

         """simple docstring"""
         lowerCAmelCase__      :			int   =	parser.add_parser("""download"""   )
         download_parser.add_argument(
             """--cache-dir"""   ,     type=UpperCamelCase   ,     default=UpperCamelCase   ,     help="""Path to location to store the models"""   )
         download_parser.add_argument(
             """--force"""   ,     action="""store_true"""   ,     help="""Force the model to be download even if already in cache-dir"""   )
         download_parser.add_argument(
             """--trust-remote-code"""   ,     action="""store_true"""   ,     help="""Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you've reviewed the code as it will execute on your local machine"""   ,     )
         download_parser.add_argument("""model"""   ,     type=UpperCamelCase   ,     help="""Name of the model to download"""   )
         download_parser.set_defaults(func=UpperCamelCase   )






   def __init__(		self       :		Optional[int]   ,     UpperCamelCase       :		str   ,     UpperCamelCase       :		str   ,     UpperCamelCase       :		bool   ,     UpperCamelCase       :		bool   )			->						Any:

         """simple docstring"""
         lowerCAmelCase__      :			int   =	model
         lowerCAmelCase__      :			Union[str, Any]   =	cache
         lowerCAmelCase__      :			Optional[int]   =	force
         lowerCAmelCase__      :			Dict   =	trust_remote_code






   def 	_lowerCAmelCase   (		self       :		int   )			->						Any:

         """simple docstring"""
         from ..models.auto import AutoModel, AutoTokenizer

         AutoModel.from_pretrained(
             self._model   ,     cache_dir=self._cache   ,     force_download=self._force   ,     trust_remote_code=self._trust_remote_code   )
         AutoTokenizer.from_pretrained(
             self._model   ,     cache_dir=self._cache   ,     force_download=self._force   ,     trust_remote_code=self._trust_remote_code   )





