






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




					@parameterized.expand([(None,), ('''foo.json''',)]      )
					def 		A      (  self  :		Dict				, _A  :		Dict      )    ->							Any:
								UpperCAmelCase_      :       List[str]          =       GenerationConfig(
								    do_sample=_A				, temperature=0.7				, length_penalty=1.0				, bad_words_ids=[[1, 2, 3], [4, 5]]				, )
								with tempfile.TemporaryDirectory() as tmp_dir:
											config.save_pretrained(_A				, config_name=_A      )
											UpperCAmelCase_      :       Optional[int]          =       GenerationConfig.from_pretrained(_A				, config_name=_A      )

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




					def 		A      (  self  :		Tuple      )    ->							Optional[Any]:
								UpperCAmelCase_      :       List[str]          =       AutoConfig.from_pretrained('''gpt2'''      )
								UpperCAmelCase_      :       Optional[int]          =       GenerationConfig.from_model_config(_A      )
								UpperCAmelCase_      :       List[str]          =       GenerationConfig()

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

								# 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  :		int      )    ->							Optional[Any]:
								UpperCAmelCase_      :       Union[str, Any]          =       GenerationConfig()
								UpperCAmelCase_      :       List[Any]          =       {
								    '''max_new_tokens''': 10_24,
								    '''foo''': '''bar''',
								}
								UpperCAmelCase_      :       Optional[int]          =       copy.deepcopy(_A      )
								UpperCAmelCase_      :       List[Any]          =       generation_config.update(**_A      )

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

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




					def 		A      (  self  :		Optional[int]      )    ->							List[Any]:
								UpperCAmelCase_      :       int          =       GenerationConfig()
								UpperCAmelCase_      :       Union[str, Any]          =       '''bar'''

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

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

								UpperCAmelCase_      :       int          =       GenerationConfig.from_model_config(_A      )
								assert not hasattr(_A				, '''foo'''      )  # no new kwargs should be initialized if from config





					def 		A      (  self  :		str      )    ->							Any:
								UpperCAmelCase_      :       int          =       GenerationConfig()
								self.assertEqual(default_config.temperature				, 1.0      )
								self.assertEqual(default_config.do_sample				, _A      )
								self.assertEqual(default_config.num_beams				, 1      )

								UpperCAmelCase_      :       List[Any]          =       GenerationConfig(
								    do_sample=_A				, 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				, _A      )
								self.assertEqual(config.num_beams				, 1      )

								with tempfile.TemporaryDirectory() as tmp_dir:
											config.save_pretrained(_A      )
											UpperCAmelCase_      :       Dict          =       GenerationConfig.from_pretrained(_A				, temperature=1.0      )

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






@is_staging_test
class  snake_case__      (					unittest.TestCase):




					@classmethod
					def 		A      (  cls  :		List[str]      )    ->							str:
								UpperCAmelCase_      :       List[str]          =       TOKEN
								HfFolder.save_token(_A      )




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

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

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

								UpperCAmelCase_      :       Optional[int]          =       GenerationConfig.from_pretrained(F"{USER}/test-generation-config"      )
								for k, v in config.to_dict().items():
											if k != "transformers_version":
														self.assertEqual(_A				, getattr(_A				, _A      )      )





					def 		A      (  self  :		int      )    ->							Tuple:
								UpperCAmelCase_      :       str          =       GenerationConfig(
								    do_sample=_A				, temperature=0.7				, length_penalty=1.0				, )
								config.push_to_hub('''valid_org/test-generation-config-org'''				, use_auth_token=self._token      )

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

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

								UpperCAmelCase_      :       List[str]          =       GenerationConfig.from_pretrained('''valid_org/test-generation-config-org'''      )
								for k, v in config.to_dict().items():
											if k != "transformers_version":
														self.assertEqual(_A				, getattr(_A				, _A      )      )










'''simple docstring'''





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
						import torch

						from transformers.generation import DisjunctiveConstraint


@require_torch
class  snake_case__      (					unittest.TestCase):




					def 		A      (  self  :		Union[str, Any]      )    ->							List[str]:
								# For consistency across different places the DisjunctiveConstraint is called,
								# dc.token_ids is a list of integers. It is also initialized only by integers.

								UpperCAmelCase_      :       Union[str, Any]          =       [[1, 2, 4], [1, 2, 3, 4]]
								UpperCAmelCase_      :       Dict          =       DisjunctiveConstraint(_A      )
								self.assertTrue(isinstance(dc.token_ids				, _A      )      )

								with self.assertRaises(_A      ):
											DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]]      )      )

								with self.assertRaises(_A      ):
											DisjunctiveConstraint([torch.LongTensor([1, 2, 4]      ), torch.LongTensor([1, 2, 3, 4, 5]      )]      )




					def 		A      (  self  :		Optional[int]      )    ->							List[Any]:
								# We can't have constraints that are complete subsets of another. This leads to a preverse
								# interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint?
								# It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially
								# fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm
								# will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it).
								UpperCAmelCase_      :       Optional[Any]          =       [[1, 2], [1, 2, 3, 4]]

								with self.assertRaises(_A      ):
											DisjunctiveConstraint(_A      )  # fails here




					def 		A      (  self  :		List[str]      )    ->							int:
								UpperCAmelCase_      :       Dict          =       [[1, 2, 3], [1, 2, 4]]

								UpperCAmelCase_      :       List[Any]          =       DisjunctiveConstraint(_A      )

								UpperCAmelCase_    ,      UpperCAmelCase_    ,      UpperCAmelCase_      :       List[str]          =       dc.update(1      )
								UpperCAmelCase_      :       int          =       stepped is True and completed is False and reset is False
								self.assertTrue(_A      )
								self.assertTrue(not dc.completed      )
								self.assertTrue(dc.current_seq == [1]      )

								UpperCAmelCase_    ,      UpperCAmelCase_    ,      UpperCAmelCase_      :       Tuple          =       dc.update(2      )
								UpperCAmelCase_      :       Optional[int]          =       stepped is True and completed is False and reset is False
								self.assertTrue(_A      )
								self.assertTrue(not dc.completed      )
								self.assertTrue(dc.current_seq == [1, 2]      )

								UpperCAmelCase_    ,      UpperCAmelCase_    ,      UpperCAmelCase_      :       Any          =       dc.update(3      )
								UpperCAmelCase_      :       List[Any]          =       stepped is True and completed is True and reset is False
								self.assertTrue(_A      )
								self.assertTrue(dc.completed      )  # Completed!
								self.assertTrue(dc.current_seq == [1, 2, 3]      )





					def 		A      (  self  :		List[Any]      )    ->							Any:
								UpperCAmelCase_      :       int          =       [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

								UpperCAmelCase_      :       str          =       DisjunctiveConstraint(_A      )

								UpperCAmelCase_    ,      UpperCAmelCase_    ,      UpperCAmelCase_      :       Tuple          =       dc.update(1      )
								self.assertTrue(not dc.completed      )
								self.assertTrue(dc.current_seq == [1]      )

								UpperCAmelCase_    ,      UpperCAmelCase_    ,      UpperCAmelCase_      :       str          =       dc.update(2      )
								self.assertTrue(not dc.completed      )
								self.assertTrue(dc.current_seq == [1, 2]      )

								UpperCAmelCase_    ,      UpperCAmelCase_    ,      UpperCAmelCase_      :       str          =       dc.update(4      )
								self.assertTrue(not dc.completed      )
								self.assertTrue(dc.current_seq == [1, 2, 4]      )

								UpperCAmelCase_    ,      UpperCAmelCase_    ,      UpperCAmelCase_      :       Any          =       dc.update(5      )
								self.assertTrue(dc.completed      )  # Completed!
								self.assertTrue(dc.current_seq == [1, 2, 4, 5]      )

								dc.reset()

								UpperCAmelCase_    ,      UpperCAmelCase_    ,      UpperCAmelCase_      :       List[str]          =       dc.update(1      )
								self.assertTrue(not dc.completed      )
								self.assertTrue(dc.remaining() == 3      )
								self.assertTrue(dc.current_seq == [1]      )

								UpperCAmelCase_    ,      UpperCAmelCase_    ,      UpperCAmelCase_      :       Optional[int]          =       dc.update(2      )
								self.assertTrue(not dc.completed      )
								self.assertTrue(dc.remaining() == 2      )
								self.assertTrue(dc.current_seq == [1, 2]      )

								UpperCAmelCase_    ,      UpperCAmelCase_    ,      UpperCAmelCase_      :       int          =       dc.update(5      )
								self.assertTrue(dc.completed      )  # Completed!
								self.assertTrue(dc.remaining() == 0      )
								self.assertTrue(dc.current_seq == [1, 2, 5]      )


