


import copy
import tempfile
import unittest

from huggingface_hub import HfFolder, delete_repo
from parameterized import parameterized
from requests.exceptions import HTTPError

from transformers import AutoConfig, GenerationConfig
from transformers.testing_utils import TOKEN, USER, is_staging_test



class 				A       (			unittest.TestCase				):
					'''simple docstring'''






					@parameterized.expand([(None,), ("""foo.json""",)]						)
					def     lowerCamelCase__	(self       :		Optional[Any] ,   _UpperCAmelCase       :		Optional[Any]						)		->  Optional[Any]:




							"""simple docstring"""


							lowercase__	     =						GenerationConfig(
							    do_sample=_UpperCAmelCase ,   temperature=0.7 ,   length_penalty=1.0 ,   bad_words_ids=[[1, 2, 3], [4, 5]] ,   )
							with tempfile.TemporaryDirectory() as tmp_dir:
									config.save_pretrained(_UpperCAmelCase ,   config_name=_UpperCAmelCase						)
									lowercase__	     =						GenerationConfig.from_pretrained(_UpperCAmelCase ,   config_name=_UpperCAmelCase						)

							# Checks parameters that were specified
							self.assertEqual(loaded_config.do_sample ,   _UpperCAmelCase						)
							self.assertEqual(loaded_config.temperature ,   0.7						)
							self.assertEqual(loaded_config.length_penalty ,   1.0						)
							self.assertEqual(loaded_config.bad_words_ids ,   [[1, 2, 3], [4, 5]]						)

							# Checks parameters that were not specified (defaults)
							self.assertEqual(loaded_config.top_k ,   50						)
							self.assertEqual(loaded_config.max_length ,   20						)
							self.assertEqual(loaded_config.max_time ,   _UpperCAmelCase						)

					def     lowerCamelCase__	(self       :		Any						)		->  List[Any]:




							"""simple docstring"""


							lowercase__	     =						AutoConfig.from_pretrained("""gpt2"""						)
							lowercase__	     =						GenerationConfig.from_model_config(_UpperCAmelCase						)
							lowercase__	     =						GenerationConfig()

							# The generation config has loaded a few non-default parameters from the model config
							self.assertNotEqual(_UpperCAmelCase ,   _UpperCAmelCase						)

							# One of those parameters is eos_token_id -- check if it matches
							self.assertNotEqual(generation_config_from_model.eos_token_id ,   default_generation_config.eos_token_id						)
							self.assertEqual(generation_config_from_model.eos_token_id ,   model_config.eos_token_id						)

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




							"""simple docstring"""


							lowercase__	     =						GenerationConfig()
							lowercase__	     =						{
							    """max_new_tokens""": 1024,
							    """foo""": """bar""",
							}
							lowercase__	     =						copy.deepcopy(_UpperCAmelCase						)
							lowercase__	     =						generation_config.update(**_UpperCAmelCase						)

							# update_kwargs was not modified (no side effects)
							self.assertEqual(_UpperCAmelCase ,   _UpperCAmelCase						)

							# update_kwargs was used to update the config on valid attributes
							self.assertEqual(generation_config.max_new_tokens ,   1024						)

							# `.update()` returns a dictionary of unused kwargs
							self.assertEqual(_UpperCAmelCase ,   {"""foo""": """bar"""}						)

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




							"""simple docstring"""


							lowercase__	     =						GenerationConfig()
							lowercase__	     =						"""bar"""

							with tempfile.TemporaryDirectory("""test-generation-config"""						) as tmp_dir:
									generation_config.save_pretrained(_UpperCAmelCase						)

									lowercase__	     =						GenerationConfig.from_pretrained(_UpperCAmelCase						)
							# update_kwargs was used to update the config on valid attributes
							self.assertEqual(new_config.foo ,   """bar"""						)

							lowercase__	     =						GenerationConfig.from_model_config(_UpperCAmelCase						)
							assert not hasattr(_UpperCAmelCase ,   """foo"""						)  # no new kwargs should be initialized if from config


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




							"""simple docstring"""


							lowercase__	     =						GenerationConfig()
							self.assertEqual(default_config.temperature ,   1.0						)
							self.assertEqual(default_config.do_sample ,   _UpperCAmelCase						)
							self.assertEqual(default_config.num_beams ,   1						)

							lowercase__	     =						GenerationConfig(
							    do_sample=_UpperCAmelCase ,   temperature=0.7 ,   length_penalty=1.0 ,   bad_words_ids=[[1, 2, 3], [4, 5]] ,   )
							self.assertEqual(config.temperature ,   0.7						)
							self.assertEqual(config.do_sample ,   _UpperCAmelCase						)
							self.assertEqual(config.num_beams ,   1						)

							with tempfile.TemporaryDirectory() as tmp_dir:
									config.save_pretrained(_UpperCAmelCase						)
									lowercase__	     =						GenerationConfig.from_pretrained(_UpperCAmelCase ,   temperature=1.0						)

							self.assertEqual(loaded_config.temperature ,   1.0						)
							self.assertEqual(loaded_config.do_sample ,   _UpperCAmelCase						)
							self.assertEqual(loaded_config.num_beams ,   1						)  # default value






@is_staging_test
class 				A       (			unittest.TestCase				):
					'''simple docstring'''






					@classmethod
					def     lowerCamelCase__	(cls       :		int						)		->  Optional[Any]:




							"""simple docstring"""


							lowercase__	     =						TOKEN
							HfFolder.save_token(_UpperCAmelCase						)

					@classmethod
					def     lowerCamelCase__	(cls       :		Tuple						)		->  List[Any]:




							"""simple docstring"""


							try:
									delete_repo(token=cls._token ,   repo_id="""test-generation-config"""						)
							except HTTPError:
									pass

							try:
									delete_repo(token=cls._token ,   repo_id="""valid_org/test-generation-config-org"""						)
							except HTTPError:
									pass

					def     lowerCamelCase__	(self       :		Dict						)		->  str:




							"""simple docstring"""


							lowercase__	     =						GenerationConfig(
							    do_sample=_UpperCAmelCase ,   temperature=0.7 ,   length_penalty=1.0 ,   )
							config.push_to_hub("""test-generation-config""" ,   use_auth_token=self._token						)

							lowercase__	     =						GenerationConfig.from_pretrained(f'''{USER}/test-generation-config'''						)
							for k, v in config.to_dict().items():
									if k != "transformers_version":
											self.assertEqual(_UpperCAmelCase ,   getattr(_UpperCAmelCase ,   _UpperCAmelCase						)						)

        # Reset repo
							delete_repo(token=self._token ,   repo_id="""test-generation-config"""						)

							# Push to hub via save_pretrained
							with tempfile.TemporaryDirectory() as tmp_dir:
									config.save_pretrained(
									    _UpperCAmelCase ,   repo_id="""test-generation-config""" ,   push_to_hub=_UpperCAmelCase ,   use_auth_token=self._token						)

							lowercase__	     =						GenerationConfig.from_pretrained(f'''{USER}/test-generation-config'''						)
							for k, v in config.to_dict().items():
									if k != "transformers_version":
											self.assertEqual(_UpperCAmelCase ,   getattr(_UpperCAmelCase ,   _UpperCAmelCase						)						)


					def     lowerCamelCase__	(self       :		int						)		->  Any:




							"""simple docstring"""


							lowercase__	     =						GenerationConfig(
							    do_sample=_UpperCAmelCase ,   temperature=0.7 ,   length_penalty=1.0 ,   )
							config.push_to_hub("""valid_org/test-generation-config-org""" ,   use_auth_token=self._token						)

							lowercase__	     =						GenerationConfig.from_pretrained("""valid_org/test-generation-config-org"""						)
							for k, v in config.to_dict().items():
									if k != "transformers_version":
											self.assertEqual(_UpperCAmelCase ,   getattr(_UpperCAmelCase ,   _UpperCAmelCase						)						)

        # Reset repo
							delete_repo(token=self._token ,   repo_id="""valid_org/test-generation-config-org"""						)

							# Push to hub via save_pretrained
							with tempfile.TemporaryDirectory() as tmp_dir:
									config.save_pretrained(
									    _UpperCAmelCase ,   repo_id="""valid_org/test-generation-config-org""" ,   push_to_hub=_UpperCAmelCase ,   use_auth_token=self._token						)

							lowercase__	     =						GenerationConfig.from_pretrained("""valid_org/test-generation-config-org"""						)
							for k, v in config.to_dict().items():
									if k != "transformers_version":
											self.assertEqual(_UpperCAmelCase ,   getattr(_UpperCAmelCase ,   _UpperCAmelCase						)						)






"""simple docstring"""






import unittest

from transformers import AutoTokenizer, is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, slow


if is_flax_available():
     import jax.numpy as jnp

     from transformers import FlaxXLMRobertaModel



@require_sentencepiece
@require_tokenizers
@require_flax
class     UpperCAmelCase    (	unittest.TestCase  ):



   @slow
   def    __UpperCAmelCase    (       self    :	int				):



     """simple docstring"""
     _snake_case   	=      FlaxXLMRobertaModel.from_pretrained('''xlm-roberta-base'''				)
     _snake_case   	=      AutoTokenizer.from_pretrained('''xlm-roberta-base'''				)
     _snake_case   	=      '''The dog is cute and lives in the garden house'''
     _snake_case   	=      jnp.array([tokenizer.encode(__lowerCamelCase				)]				)

     _snake_case   	=      (1, 1_2, 7_6_8)  # batch_size, sequence_length, embedding_vector_dim
     _snake_case   	=      jnp.array(
         [[-0.0_1_0_1, 0.1_2_1_8, -0.0_8_0_3, 0.0_8_0_1, 0.1_3_2_7, 0.0_7_7_6, -0.1_2_1_5, 0.2_3_8_3, 0.3_3_3_8, 0.3_1_0_6, 0.0_3_0_0, 0.0_2_5_2]]				)

     _snake_case   	=      model(__lowerCamelCase				)['''last_hidden_state''']
     self.assertEqual(output.shape							,       __lowerCamelCase				)
     # compare the actual values for a slice of last dim
     self.assertTrue(jnp.allclose(output[:, :, -1]							,       __lowerCamelCase							,       atol=1E-3				)				)





