


"""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 		a__						(	self:						Union[str, Any]     ,		__a:						Dict	)->    List[str]:
           lowerCamelCase					:					int        =	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	)
                 lowerCamelCase					:					Union[str, Any]        =	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:						List[str]	)->    Dict:
           lowerCamelCase					:					List[str]        =	AutoConfig.from_pretrained("""gpt2"""	)
           lowerCamelCase					:					Optional[int]        =	GenerationConfig.from_model_config(__a	)
           lowerCamelCase					:					Dict        =	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:						Union[str, Any]	)->    Tuple:
           lowerCamelCase					:					str        =	GenerationConfig()
           lowerCamelCase					:					Optional[int]        =	{
               """max_new_tokens""": 1_024,
               """foo""": """bar""",
           }
           lowerCamelCase					:					Any        =	copy.deepcopy(__a	)
           lowerCamelCase					:					List[str]        =	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     ,		1_024	)

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

     def 		a__						(	self:						List[str]	)->    Tuple:
           lowerCamelCase					:					Tuple        =	GenerationConfig()
           lowerCamelCase					:					Any        =	"""bar"""

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

                 lowerCamelCase					:					List[str]        =	GenerationConfig.from_pretrained(__a	)
           # update_kwargs was used to update the config on valid attributes
           self.assertEqual(new_config.foo     ,		"""bar"""	)

           lowerCamelCase					:					Dict        =	GenerationConfig.from_model_config(__a	)
           assert not hasattr(__a     ,		"""foo"""	)  # no new kwargs should be initialized if from config







     def 		a__						(	self:						str	)->    List[Any]:
           lowerCamelCase					:					Dict        =	GenerationConfig()
           self.assertEqual(default_config.temperature     ,		1.0	)
           self.assertEqual(default_config.do_sample     ,		__a	)
           self.assertEqual(default_config.num_beams     ,		1	)

           lowerCamelCase					:					int        =	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	)
                 lowerCamelCase					:					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 			A__	(						unittest.TestCase):


     """simple docstring"""

     @classmethod
     def 		a__						(	cls:						Any	)->    int:
           lowerCamelCase					:					Dict        =	TOKEN
           HfFolder.save_token(__a	)

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

           lowerCamelCase					:					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(__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	)

           lowerCamelCase					:					str        =	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:						List[Any]	)->    Any:
           lowerCamelCase					:					Any        =	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	)

           lowerCamelCase					:					Any        =	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	)

           lowerCamelCase					:					Optional[Any]        =	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"""


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


__lowerCamelCase      :Optional[int]	    =		logging.get_logger(__name__)

__lowerCamelCase      :List[str]	    =		{
    'google/realm-cc-news-pretrained-embedder': (
        'https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json'
    ),
    'google/realm-cc-news-pretrained-encoder': (
        'https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json'
    ),
    'google/realm-cc-news-pretrained-scorer': (
        'https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json'
    ),
    'google/realm-cc-news-pretrained-openqa': (
        'https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json'
    ),
    'google/realm-orqa-nq-openqa': 'https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json',
    'google/realm-orqa-nq-reader': 'https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json',
    'google/realm-orqa-wq-openqa': 'https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json',
    'google/realm-orqa-wq-reader': 'https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json',
    # See all REALM models at https://huggingface.co/models?filter=realm
}


class 			A__	(						__lowercase):


     """simple docstring"""
     snake_case__	:					Optional[Any]													='''realm'''

     def __init__(	self:						Union[str, Any]     ,		__a:						List[Any]=30_522     ,		__a:						List[Any]=768     ,		__a:						List[Any]=128     ,		__a:						Union[str, Any]=12     ,		__a:						Union[str, Any]=12     ,		__a:						Optional[Any]=8     ,		__a:						Dict=3_072     ,		__a:						List[Any]="gelu_new"     ,		__a:						List[Any]=0.1     ,		__a:						Tuple=0.1     ,		__a:						Optional[Any]=512     ,		__a:						Optional[int]=2     ,		__a:						str=0.02     ,		__a:						int=1e-1_2     ,		__a:						Optional[Any]=256     ,		__a:						Any=10     ,		__a:						Dict=1e-3     ,		__a:						Optional[Any]=5     ,		__a:						Dict=320     ,		__a:						Tuple=13_353_718     ,		__a:						List[Any]=5_000     ,		__a:						Dict=1     ,		__a:						int=0     ,		__a:						Dict=2     ,		**__a:						List[str]     ,		)->    Any:
           super().__init__(pad_token_id=__a     ,		bos_token_id=__a     ,		eos_token_id=__a     ,		**__a	)

           # Common config
           lowerCamelCase					:					Optional[Any]        =	vocab_size
           lowerCamelCase					:					str        =	max_position_embeddings
           lowerCamelCase					:					Dict        =	hidden_size
           lowerCamelCase					:					Dict        =	retriever_proj_size
           lowerCamelCase					:					Optional[Any]        =	num_hidden_layers
           lowerCamelCase					:					List[str]        =	num_attention_heads
           lowerCamelCase					:					Tuple        =	num_candidates
           lowerCamelCase					:					int        =	intermediate_size
           lowerCamelCase					:					Dict        =	hidden_act
           lowerCamelCase					:					List[str]        =	hidden_dropout_prob
           lowerCamelCase					:					Dict        =	attention_probs_dropout_prob
           lowerCamelCase					:					Optional[int]        =	initializer_range
           lowerCamelCase					:					Dict        =	type_vocab_size
           lowerCamelCase					:					Optional[Any]        =	layer_norm_eps

           # Reader config
           lowerCamelCase					:					List[str]        =	span_hidden_size
           lowerCamelCase					:					Dict        =	max_span_width
           lowerCamelCase					:					Optional[Any]        =	reader_layer_norm_eps
           lowerCamelCase					:					Optional[int]        =	reader_beam_size
           lowerCamelCase					:					List[Any]        =	reader_seq_len

           # Retrieval config
           lowerCamelCase					:					int        =	num_block_records
           lowerCamelCase					:					Dict        =	searcher_beam_size




