


'''simple docstring'''



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 	UpperCamelCase__(       unittest.TestCase	):




				@parameterized.expand([(None,), ('''foo.json''',)]				)
				def 					a__(		self      :			Union[str, Any]				,   lowerCAmelCase      :			Tuple				)->					List[str]:

										"""simple docstring"""




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

										# Checks parameters that were specified
										self.assertEqual(loaded_config.do_sample				,   lowerCAmelCase				)
										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				,   lowerCAmelCase				)




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

										"""simple docstring"""




										UpperCAmelCase					    =     AutoConfig.from_pretrained('''gpt2'''				)
										UpperCAmelCase					    =     GenerationConfig.from_model_config(lowerCAmelCase				)
										UpperCAmelCase					    =     GenerationConfig()

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

										# 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 					a__(		self      :			Optional[Any]				)->					Optional[int]:

										"""simple docstring"""




										UpperCAmelCase					    =     GenerationConfig()
										UpperCAmelCase					    =     {
										    '''max_new_tokens''': 1024,
										    '''foo''': '''bar''',
										}
										UpperCAmelCase					    =     copy.deepcopy(lowerCAmelCase				)
										UpperCAmelCase					    =     generation_config.update(**lowerCAmelCase				)

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

										# 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(lowerCAmelCase				,   {'''foo''': '''bar'''}				)




				def 					a__(		self      :			int				)->					Any:

										"""simple docstring"""




										UpperCAmelCase					    =     GenerationConfig()
										UpperCAmelCase					    =     '''bar'''

										with tempfile.TemporaryDirectory('''test-generation-config'''				) as tmp_dir:
																generation_config.save_pretrained(lowerCAmelCase				)

																UpperCAmelCase					    =     GenerationConfig.from_pretrained(lowerCAmelCase				)
										# update_kwargs was used to update the config on valid attributes
										self.assertEqual(new_config.foo				,   '''bar'''				)

										UpperCAmelCase					    =     GenerationConfig.from_model_config(lowerCAmelCase				)
										assert not hasattr(lowerCAmelCase				,   '''foo'''				)  # no new kwargs should be initialized if from config





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

										"""simple docstring"""




										UpperCAmelCase					    =     GenerationConfig()
										self.assertEqual(default_config.temperature				,   1.0				)
										self.assertEqual(default_config.do_sample				,   lowerCAmelCase				)
										self.assertEqual(default_config.num_beams				,   1				)

										UpperCAmelCase					    =     GenerationConfig(
										    do_sample=lowerCAmelCase				,   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				,   lowerCAmelCase				)
										self.assertEqual(config.num_beams				,   1				)

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

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







@is_staging_test
class 	UpperCamelCase__(       unittest.TestCase	):




				@classmethod
				def 					a__(		cls      :			List[str]				)->					List[Any]:

										"""simple docstring"""




										UpperCAmelCase					    =     TOKEN
										HfFolder.save_token(lowerCAmelCase				)




				@classmethod
				def 					a__(		cls      :			int				)->					int:

										"""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 					a__(		self      :			Tuple				)->					int:

										"""simple docstring"""




										UpperCAmelCase					    =     GenerationConfig(
										    do_sample=lowerCAmelCase				,   temperature=0.7				,   length_penalty=1.0				,   )
										config.push_to_hub('''test-generation-config'''				,   use_auth_token=self._token				)

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

        # 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(
																    lowerCAmelCase				,   repo_id='''test-generation-config'''				,   push_to_hub=lowerCAmelCase				,   use_auth_token=self._token				)

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





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

										"""simple docstring"""




										UpperCAmelCase					    =     GenerationConfig(
										    do_sample=lowerCAmelCase				,   temperature=0.7				,   length_penalty=1.0				,   )
										config.push_to_hub('''valid_org/test-generation-config-org'''				,   use_auth_token=self._token				)

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

        # 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(
																    lowerCAmelCase				,   repo_id='''valid_org/test-generation-config-org'''				,   push_to_hub=lowerCAmelCase				,   use_auth_token=self._token				)

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









'''simple docstring'''



from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowercase      :      int            =					logging.get_logger(__name__)

_lowercase      :      Optional[Any]            =					{"""openai-gpt""": """https://huggingface.co/openai-gpt/resolve/main/config.json"""}






class 	UpperCamelCase__(       lowerCAmelCase	):
				__magic_name__    :				Union[str, Any]  		=	"openai-gpt"
				__magic_name__    :				Optional[int]  		=	{
				    "max_position_embeddings": "n_positions",
				    "hidden_size": "n_embd",
				    "num_attention_heads": "n_head",
				    "num_hidden_layers": "n_layer",
				}




				def __init__(		self      :			Optional[int]				,   lowerCAmelCase      :			Optional[Any]=40478				,   lowerCAmelCase      :			str=512				,   lowerCAmelCase      :			List[Any]=768				,   lowerCAmelCase      :			Tuple=12				,   lowerCAmelCase      :			int=12				,   lowerCAmelCase      :			List[str]="gelu"				,   lowerCAmelCase      :			List[str]=0.1				,   lowerCAmelCase      :			List[Any]=0.1				,   lowerCAmelCase      :			Dict=0.1				,   lowerCAmelCase      :			Tuple=1E-5				,   lowerCAmelCase      :			Tuple=0.02				,   lowerCAmelCase      :			Optional[int]="cls_index"				,   lowerCAmelCase      :			Dict=True				,   lowerCAmelCase      :			Optional[int]=None				,   lowerCAmelCase      :			Dict=True				,   lowerCAmelCase      :			int=0.1				,   **lowerCAmelCase      :			Optional[int]				,   )->					str:

										"""simple docstring"""




										UpperCAmelCase					    =     vocab_size
										UpperCAmelCase					    =     n_positions
										UpperCAmelCase					    =     n_embd
										UpperCAmelCase					    =     n_layer
										UpperCAmelCase					    =     n_head
										UpperCAmelCase					    =     afn
										UpperCAmelCase					    =     resid_pdrop
										UpperCAmelCase					    =     embd_pdrop
										UpperCAmelCase					    =     attn_pdrop
										UpperCAmelCase					    =     layer_norm_epsilon
										UpperCAmelCase					    =     initializer_range
										UpperCAmelCase					    =     summary_type
										UpperCAmelCase					    =     summary_use_proj
										UpperCAmelCase					    =     summary_activation
										UpperCAmelCase					    =     summary_first_dropout
										UpperCAmelCase					    =     summary_proj_to_labels
										super().__init__(**lowerCAmelCase				)





