






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 ):

  @parameterized.expand([(None,), ("""foo.json""",)]			)
  def    a					(    self						:	List[Any]   ,				lowerCamelCase__						:	str			):
     """simple docstring"""

     __UpperCamelCase	:			Union[str, Any]					=					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	:			List[str]					=					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						:	Optional[Any]			):
     """simple docstring"""

     __UpperCamelCase	:			Optional[Any]					=					AutoConfig.from_pretrained("""gpt2"""			)
     __UpperCamelCase	:			Union[str, Any]					=					GenerationConfig.from_model_config(lowerCamelCase__			)
     __UpperCamelCase	:			List[Any]					=					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						:	List[Any]			):
     """simple docstring"""

     __UpperCamelCase	:			Tuple					=					GenerationConfig()
     __UpperCamelCase	:			Any					=					{
         """max_new_tokens""": 10_24,
         """foo""": """bar""",
     }
     __UpperCamelCase	:			Optional[Any]					=					copy.deepcopy(lowerCamelCase__			)
     __UpperCamelCase	:			Optional[Any]					=					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    a					(    self						:	Optional[int]			):
     """simple docstring"""

     __UpperCamelCase	:			Tuple					=					GenerationConfig()
     __UpperCamelCase	:			Tuple					=					"""bar"""

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

        __UpperCamelCase	:			Optional[Any]					=					GenerationConfig.from_pretrained(lowerCamelCase__			)
     # update_kwargs was used to update the config on valid attributes
     self.assertEqual(new_config.foo   ,				"""bar"""			)

     __UpperCamelCase	:			Any					=					GenerationConfig.from_model_config(lowerCamelCase__			)
     assert not hasattr(lowerCamelCase__   ,				"""foo"""			)  # no new kwargs should be initialized if from config

  def    a					(    self						:	Tuple			):
     """simple docstring"""

     __UpperCamelCase	:			int					=					GenerationConfig()
     self.assertEqual(default_config.temperature   ,				1.0			)
     self.assertEqual(default_config.do_sample   ,				lowerCamelCase__			)
     self.assertEqual(default_config.num_beams   ,				1			)

     __UpperCamelCase	:			Optional[int]					=					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	:			Dict					=					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 ):

  @classmethod
  def    a					(    cls						:	List[Any]			):
     """simple docstring"""

     __UpperCamelCase	:			Dict					=					TOKEN
     HfFolder.save_token(lowerCamelCase__			)

  @classmethod
  def    a					(    cls						:	Optional[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						:	str			):
     """simple docstring"""

     __UpperCamelCase	:			str					=					GenerationConfig(
         do_sample=lowerCamelCase__   ,				temperature=0.7   ,				length_penalty=1.0   ,				)
     config.push_to_hub("""test-generation-config"""   ,				use_auth_token=self._token			)

     __UpperCamelCase	:			str					=					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	:			Union[str, Any]					=					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						:	List[Any]			):
     """simple docstring"""

     __UpperCamelCase	:			List[str]					=					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	:			str					=					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	:			str					=					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__			)			)











from __future__ import annotations


def 				__lowerCamelCase     (							__lowerCAmelCase							:					str					,							__lowerCAmelCase							:					Tuple					,							__lowerCAmelCase							:					int					,							__lowerCAmelCase							:					Tuple							)     ->  Optional[Any]:  # noqa: E741
   while r - l > 1:
      __UpperCamelCase	:			int					=					(l + r) // 2
      if v[m] >= key:
         __UpperCamelCase	:			Dict					=					m
      else:
         __UpperCamelCase	:			Optional[Any]					=					m  # noqa: E741
   return r


def 				__lowerCamelCase     (							__lowerCAmelCase							:					list[int]							)     ->  int:
   if len(__lowerCAmelCase							) == 0:
      return 0

   __UpperCamelCase	:			Optional[Any]					=					[0] * len(__lowerCAmelCase							)
   __UpperCamelCase	:			Optional[int]					=					1

   __UpperCamelCase	:			Union[str, Any]					=					v[0]

   for i in range(1					,							len(__lowerCAmelCase							)							):
      if v[i] < tail[0]:
         __UpperCamelCase	:			Dict					=					v[i]
      elif v[i] > tail[length - 1]:
         __UpperCamelCase	:			Optional[int]					=					v[i]
         length += 1
      else:
         __UpperCamelCase	:			List[Any]					=					v[i]

   return length


if __name__ == "__main__":
       import doctest

       doctest.testmod()



