


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





							'''simple docstring'''






							@parameterized.expand([(None,), ('''foo.json''',)]  )
							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_  )			->				Union[str, Any]:
														lowerCAmelCase__			  =      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_  )
																					lowerCAmelCase__			  =      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     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														lowerCAmelCase__			  =      AutoConfig.from_pretrained('''gpt2'''  )
														lowerCAmelCase__			  =      GenerationConfig.from_model_config(lowerCamelCase_  )
														lowerCAmelCase__			  =      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     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														lowerCAmelCase__			  =      GenerationConfig()
														lowerCAmelCase__			  =      {
														    '''max_new_tokens''': 10_24,
														    '''foo''': '''bar''',
														}
														lowerCAmelCase__			  =      copy.deepcopy(lowerCamelCase_  )
														lowerCAmelCase__			  =      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   ,       10_24  )

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







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														lowerCAmelCase__			  =      GenerationConfig()
														lowerCAmelCase__			  =      '''bar'''

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

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

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







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Tuple:
														lowerCAmelCase__			  =      GenerationConfig()
														self.assertEqual(default_config.temperature   ,       1.0  )
														self.assertEqual(default_config.do_sample   ,       lowerCamelCase_  )
														self.assertEqual(default_config.num_beams   ,       1  )

														lowerCAmelCase__			  =      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_  )
																					lowerCAmelCase__			  =      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 						a__							(				unittest.TestCase    ):





							'''simple docstring'''






							@classmethod
							def     __SCREAMING_SNAKE_CASE				(						cls  )			->				Any:
														lowerCAmelCase__			  =      TOKEN
														HfFolder.save_token(lowerCamelCase_  )







							@classmethod
							def     __SCREAMING_SNAKE_CASE				(						cls  )			->				List[str]:
														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     __SCREAMING_SNAKE_CASE				(						self  )			->				List[str]:
														lowerCAmelCase__			  =      GenerationConfig(
														    do_sample=lowerCamelCase_   ,       temperature=0.7   ,       length_penalty=1.0   ,       )
														config.push_to_hub('''test-generation-config'''   ,       use_auth_token=self._token  )

														lowerCAmelCase__			  =      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  )

														lowerCAmelCase__			  =      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     __SCREAMING_SNAKE_CASE				(						self  )			->				Any:
														lowerCAmelCase__			  =      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  )

														lowerCAmelCase__			  =      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  )

														lowerCAmelCase__			  =      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'''






import logging
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional, Union

from .generation.configuration_utils import GenerationConfig
from .training_args import TrainingArguments
from .utils import add_start_docstrings


__UpperCAmelCase       							=				logging.getLogger(__name__)




@dataclass
@add_start_docstrings(TrainingArguments.__doc__    )
class 						a__							(				a__    ):





							'''simple docstring'''






							lowercase__     :  bool      		= field(default=a__							,							metadata={"help": "Whether to use SortishSampler or not."}    )
							lowercase__     :  bool      		= field(
							    default=a__							,							metadata={"help": "Whether to use generate to calculate generative metrics (ROUGE, BLEU)."}    )
							lowercase__     :  Optional[int]      		= field(
							    default=a__							,							metadata={
							        "help": (
							            "The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default "
							            "to the `max_length` value of the model configuration."
							        )
							    }							,							)
							lowercase__     :  Optional[int]      		= field(
							    default=a__							,							metadata={
							        "help": (
							            "The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default "
							            "to the `num_beams` value of the model configuration."
							        )
							    }							,							)
							lowercase__     :  Optional[Union[str, Path, GenerationConfig]]      		= field(
							    default=a__							,							metadata={
							        "help": "Model id, file path or url pointing to a GenerationConfig json file, to use during prediction."
							    }							,							)







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														lowerCAmelCase__			  =      super().to_dict()
														for k, v in d.items():
																					if isinstance(lowerCamelCase_   ,       lowerCamelCase_  ):
																												lowerCAmelCase__			  =      v.to_dict()
														return d