


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


lowerCAmelCase__      		= logging.get_logger(__name__)

lowerCAmelCase__      		= {
    'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/config.json',
    'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json',
    'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/config.json',
    'funnel-transformer/medium-base': 'https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json',
    'funnel-transformer/intermediate': (
        'https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json'
    ),
    'funnel-transformer/intermediate-base': (
        'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json'
    ),
    'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/config.json',
    'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json',
    'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json',
    'funnel-transformer/xlarge-base': 'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json',
}







class      lowerCAmelCase__       (				a):




     '''simple docstring'''
     __SCREAMING_SNAKE_CASE  			= "funnel"
     __SCREAMING_SNAKE_CASE  			= {
         "hidden_size": "d_model",
         "num_attention_heads": "n_head",
     }


     def __init__(						self ,						__lowerCamelCase=3_0_5_2_2 ,						__lowerCamelCase=[4, 4, 4] ,						__lowerCamelCase=None ,						__lowerCamelCase=2 ,						__lowerCamelCase=7_6_8 ,						__lowerCamelCase=1_2 ,						__lowerCamelCase=6_4 ,						__lowerCamelCase=3_0_7_2 ,						__lowerCamelCase="gelu_new" ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=0.0 ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=None ,						__lowerCamelCase=1e-9 ,						__lowerCamelCase="mean" ,						__lowerCamelCase="relative_shift" ,						__lowerCamelCase=True ,						__lowerCamelCase=True ,						__lowerCamelCase=True ,						**__lowerCamelCase ,						)			->				Tuple:
            _A      :       int				    = vocab_size
            _A      :       Optional[Any]				    = block_sizes
            _A      :       Tuple				    = [1] * len(__lowerCamelCase) if block_repeats is None else block_repeats
            assert len(__lowerCamelCase) == len(
                self.block_repeats), "`block_sizes` and `block_repeats` should have the same length."
            _A      :       Optional[Any]				    = num_decoder_layers
            _A      :       List[str]				    = d_model
            _A      :       Dict				    = n_head
            _A      :       Optional[int]				    = d_head
            _A      :       List[Any]				    = d_inner
            _A      :       Any				    = hidden_act
            _A      :       List[Any]				    = hidden_dropout
            _A      :       Dict				    = attention_dropout
            _A      :       List[Any]				    = activation_dropout
            _A      :       List[Any]				    = initializer_range
            _A      :       str				    = initializer_std
            _A      :       Any				    = layer_norm_eps
            assert pooling_type in [
                "mean",
                "max",
            ], F"Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported."
            _A      :       Union[str, Any]				    = pooling_type
            assert attention_type in [
                "relative_shift",
                "factorized",
            ], F"Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported."
            _A      :       List[Any]				    = attention_type
            _A      :       Tuple				    = separate_cls
            _A      :       Tuple				    = truncate_seq
            _A      :       int				    = pool_q_only

            super().__init__(**__lowerCamelCase)


     @property
     def  _lowerCamelCase (						self)			->				Any:
            return sum(self.block_sizes)


     @num_hidden_layers.setter
     def  _lowerCamelCase (						self ,						__lowerCamelCase)			->				int:
            raise NotImplementedError(
                "This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.")


     @property
     def  _lowerCamelCase (						self)			->				Any:
            return len(self.block_sizes)


     @num_blocks.setter
     def  _lowerCamelCase (						self ,						__lowerCamelCase)			->				Tuple:
            raise NotImplementedError("This model does not support the setting of `num_blocks`. Please set `block_sizes`.")




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




     '''simple docstring'''


     @parameterized.expand([(None,), ("foo.json",)])
     def  _lowerCamelCase (						self ,						__lowerCamelCase)			->				List[str]:
            _A      :       str				    = 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)
                   _A      :       Tuple				    = 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 ,						5_0)
            self.assertEqual(loaded_config.max_length ,						2_0)
            self.assertEqual(loaded_config.max_time ,						__lowerCamelCase)


     def  _lowerCamelCase (						self)			->				Optional[int]:
            _A      :       Optional[int]				    = AutoConfig.from_pretrained("gpt2")
            _A      :       int				    = GenerationConfig.from_model_config(__lowerCamelCase)
            _A      :       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  _lowerCamelCase (						self)			->				Optional[Any]:
            _A      :       Optional[Any]				    = GenerationConfig()
            _A      :       List[Any]				    = {
                "max_new_tokens": 1_0_2_4,
                "foo": "bar",
            }
            _A      :       List[str]				    = copy.deepcopy(__lowerCamelCase)
            _A      :       int				    = 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 ,						1_0_2_4)

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


     def  _lowerCamelCase (						self)			->				Any:
            _A      :       int				    = GenerationConfig()
            _A      :       int				    = "bar"

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

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

            _A      :       Optional[Any]				    = GenerationConfig.from_model_config(__lowerCamelCase)
            assert not hasattr(__lowerCamelCase ,						"foo")  # no new kwargs should be initialized if from config


     def  _lowerCamelCase (						self)			->				List[str]:
            _A      :       Union[str, Any]				    = GenerationConfig()
            self.assertEqual(default_config.temperature ,						1.0)
            self.assertEqual(default_config.do_sample ,						__lowerCamelCase)
            self.assertEqual(default_config.num_beams ,						1)

            _A      :       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)
                   _A      :       Optional[int]				    = 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      lowerCAmelCase__       (				unittest.TestCase):




     '''simple docstring'''


     @classmethod
     def  _lowerCamelCase (						cls)			->				Optional[int]:
            _A      :       Dict				    = TOKEN
            HfFolder.save_token(__lowerCamelCase)


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

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

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

            _A      :       Optional[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  _lowerCamelCase (						self)			->				Union[str, Any]:
            _A      :       Union[str, Any]				    = 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)

            _A      :       int				    = 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)

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