

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




  """simple docstring"""




  @parameterized.expand([(None,), ('''foo.json''',)]			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     Any					,lowercase__   :     Tuple			):
   __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					,5_0			)
   self.assertEqual(loaded_config.max_length					,2_0			)
   self.assertEqual(loaded_config.max_time					,_UpperCamelCase			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Tuple			):
   __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 					SCREAMING_SNAKE_CASE      (      self   :     int			):
   __lowercase							  = GenerationConfig()
   __lowercase							  = {
       'max_new_tokens': 1_0_2_4,
       '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					,1_0_2_4			)

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






  def 					SCREAMING_SNAKE_CASE      (      self   :     Tuple			):
   __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 					SCREAMING_SNAKE_CASE      (      self   :     int			):
   __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 			lowercase_	(unittest.TestCase      ):




  """simple docstring"""




  @classmethod
  def 					SCREAMING_SNAKE_CASE      (      cls   :     Tuple			):
   __lowercase							  = TOKEN
   HfFolder.save_token(_UpperCamelCase			)






  @classmethod
  def 					SCREAMING_SNAKE_CASE      (      cls   :     Union[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 					SCREAMING_SNAKE_CASE      (      self   :     Union[str, Any]			):
   __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 					SCREAMING_SNAKE_CASE      (      self   :     List[str]			):
   __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			)			)









from typing import List

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


__UpperCAmelCase								=						logging.get_logger(__name__)

__UpperCAmelCase								=						{
    'snap-research/efficientformer-l1-300': (
        'https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json'
    ),
}



class   lowerCamelCase      (_snake_case			):







		'''simple docstring'''



		_snake_case    :    Optional[int]       						=  '''efficientformer'''



		def __init__(							self				,		_UpperCamelCase = [3, 2, 6, 4]				,		_UpperCamelCase = [4_8, 9_6, 2_2_4, 4_4_8]				,		_UpperCamelCase = [True, True, True, True]				,		_UpperCamelCase = 4_4_8				,		_UpperCamelCase = 3_2				,		_UpperCamelCase = 4				,		_UpperCamelCase = 7				,		_UpperCamelCase = 5				,		_UpperCamelCase = 8				,		_UpperCamelCase = 4				,		_UpperCamelCase = 0.0				,		_UpperCamelCase = 1_6				,		_UpperCamelCase = 3				,		_UpperCamelCase = 3				,		_UpperCamelCase = 3				,		_UpperCamelCase = 2				,		_UpperCamelCase = 1				,		_UpperCamelCase = 0.0				,		_UpperCamelCase = 1				,		_UpperCamelCase = True				,		_UpperCamelCase = True				,		_UpperCamelCase = 1E-5				,		_UpperCamelCase = "gelu"				,		_UpperCamelCase = 0.02				,		_UpperCamelCase = 1E-12				,		_UpperCamelCase = 2_2_4				,		_UpperCamelCase = 1E-05				,		**_UpperCamelCase				,		)	->							None:
							super().__init__(**_UpperCamelCase   )

							UpperCAmelCase_    :      int            =						hidden_act
							UpperCAmelCase_    :      Union[str, Any]            =						hidden_dropout_prob
							UpperCAmelCase_    :      Tuple            =						hidden_sizes
							UpperCAmelCase_    :      Union[str, Any]            =						num_hidden_layers
							UpperCAmelCase_    :      List[str]            =						num_attention_heads
							UpperCAmelCase_    :      List[Any]            =						initializer_range
							UpperCAmelCase_    :      int            =						layer_norm_eps
							UpperCAmelCase_    :      List[str]            =						patch_size
							UpperCAmelCase_    :      Union[str, Any]            =						num_channels
							UpperCAmelCase_    :      Optional[Any]            =						depths
							UpperCAmelCase_    :      List[Any]            =						mlp_expansion_ratio
							UpperCAmelCase_    :      List[str]            =						downsamples
							UpperCAmelCase_    :      List[Any]            =						dim
							UpperCAmelCase_    :      Tuple            =						key_dim
							UpperCAmelCase_    :      Optional[int]            =						attention_ratio
							UpperCAmelCase_    :      str            =						resolution
							UpperCAmelCase_    :      Dict            =						pool_size
							UpperCAmelCase_    :      Union[str, Any]            =						downsample_patch_size
							UpperCAmelCase_    :      List[str]            =						downsample_stride
							UpperCAmelCase_    :      List[str]            =						downsample_pad
							UpperCAmelCase_    :      Any            =						drop_path_rate
							UpperCAmelCase_    :      Dict            =						num_metaad_blocks
							UpperCAmelCase_    :      Dict            =						distillation
							UpperCAmelCase_    :      int            =						use_layer_scale
							UpperCAmelCase_    :      Any            =						layer_scale_init_value
							UpperCAmelCase_    :      Any            =						image_size
							UpperCAmelCase_    :      Dict            =						batch_norm_eps






