



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




     @parameterized.expand([(None,), ('''foo.json''',)])
     def 		snake_case_		(					self			,    a__):
         A__					=       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__)
             A__					=       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			,    5_0)
         self.assertEqual(loaded_config.max_length			,    2_0)
         self.assertEqual(loaded_config.max_time			,    a__)




     def 		snake_case_		(					self):
         A__					=       AutoConfig.from_pretrained('''gpt2''')
         A__					=       GenerationConfig.from_model_config(a__)
         A__					=       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 		snake_case_		(					self):
         A__					=       GenerationConfig()
         A__					=       {
             '''max_new_tokens''': 1_0_2_4,
             '''foo''': '''bar''',
         }
         A__					=       copy.deepcopy(a__)
         A__					=       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_0_2_4)

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




     def 		snake_case_		(					self):
         A__					=       GenerationConfig()
         A__					=       '''bar'''

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

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

         A__					=       GenerationConfig.from_model_config(a__)
         assert not hasattr(a__			,    '''foo''')  # no new kwargs should be initialized if from config




     def 		snake_case_		(					self):
         A__					=       GenerationConfig()
         self.assertEqual(default_config.temperature			,    1.0)
         self.assertEqual(default_config.do_sample			,    a__)
         self.assertEqual(default_config.num_beams			,    1)

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




     @classmethod
     def 		snake_case_		(					cls):
         A__					=       TOKEN
         HfFolder.save_token(a__)




     @classmethod
     def 		snake_case_		(					cls):
         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 		snake_case_		(					self):
         A__					=       GenerationConfig(
             do_sample=a__			,    temperature=0.7			,    length_penalty=1.0			,    )
         config.push_to_hub('''test-generation-config'''			,    use_auth_token=self._token)

         A__					=       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)

         A__					=       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 		snake_case_		(					self):
         A__					=       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)

         A__					=       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)

         A__					=       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__))





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_lowercase    	=		logging.get_logger(__name__)

_lowercase    	=		{
    "xlm-roberta-base": "https://huggingface.co/xlm-roberta-base/resolve/main/config.json",
    "xlm-roberta-large": "https://huggingface.co/xlm-roberta-large/resolve/main/config.json",
    "xlm-roberta-large-finetuned-conll02-dutch": (
        "https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json"
    ),
    "xlm-roberta-large-finetuned-conll02-spanish": (
        "https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json"
    ),
    "xlm-roberta-large-finetuned-conll03-english": (
        "https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json"
    ),
    "xlm-roberta-large-finetuned-conll03-german": (
        "https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json"
    ),
}



class      _UpperCAmelCase      (							A__  ):
     UpperCamelCase__		     =					'''xlm-roberta'''




     def __init__(					self			,    a__=3_0_5_2_2			,    a__=7_6_8			,    a__=1_2			,    a__=1_2			,    a__=3_0_7_2			,    a__="gelu"			,    a__=0.1			,    a__=0.1			,    a__=5_1_2			,    a__=2			,    a__=0.0_2			,    a__=1e-12			,    a__=1			,    a__=0			,    a__=2			,    a__="absolute"			,    a__=True			,    a__=None			,    **a__			,    ):
         super().__init__(pad_token_id=a__			,    bos_token_id=a__			,    eos_token_id=a__			,    **a__)

         A__					=       vocab_size
         A__					=       hidden_size
         A__					=       num_hidden_layers
         A__					=       num_attention_heads
         A__					=       hidden_act
         A__					=       intermediate_size
         A__					=       hidden_dropout_prob
         A__					=       attention_probs_dropout_prob
         A__					=       max_position_embeddings
         A__					=       type_vocab_size
         A__					=       initializer_range
         A__					=       layer_norm_eps
         A__					=       position_embedding_type
         A__					=       use_cache
         A__					=       classifier_dropout







class      _UpperCAmelCase      (							A__  ):




     @property
     def 		snake_case_		(					self):
         if self.task == "multiple-choice":
             A__					=       {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
         else:
             A__					=       {0: '''batch''', 1: '''sequence'''}
         return OrderedDict(
             [
                 ('''input_ids''', dynamic_axis),
                 ('''attention_mask''', dynamic_axis),
             ])
