




'''simple docstring'''


import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / "utils"))

from test_module.custom_configuration import CustomConfig  # noqa E402


UpperCAmelCase_ 		=      get_tests_dir("fixtures/dummy-config.json")





class    __lowercase						(  unittest.TestCase ):







   def 		UpperCamelCase__			(	self      )				->    List[Any]:
     __a			=					0







   def 		UpperCamelCase__			(	self      )				->    Optional[int]:
     self.assertIsNotNone(transformers.models.auto.__spec__      )
     self.assertIsNotNone(importlib.util.find_spec('transformers.models.auto'      )      )







   def 		UpperCamelCase__			(	self      )				->    Union[str, Any]:
     __a			=					AutoConfig.from_pretrained('bert-base-uncased'      )
     self.assertIsInstance(UpperCamelCase      ,							UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    str:
     __a			=					AutoConfig.from_pretrained(UpperCamelCase      )
     self.assertIsInstance(UpperCamelCase      ,							UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    List[str]:
     __a			=					AutoConfig.from_pretrained(UpperCamelCase      )
     self.assertIsInstance(UpperCamelCase      ,							UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    List[str]:
     __a			=					AutoConfig.for_model('roberta'      )
     self.assertIsInstance(UpperCamelCase      ,							UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    str:
     with tempfile.TemporaryDirectory() as tmp_dir:
       # This model name contains bert and roberta, but roberta ends up being picked.
       __a			=					os.path.join(UpperCamelCase      ,							'fake-roberta'      )
       os.makedirs(UpperCamelCase      ,							exist_ok=UpperCamelCase      )
       with open(os.path.join(UpperCamelCase      ,							'config.json'      )      ,							'w'      ) as f:
         f.write(json.dumps({}      )      )
       __a			=					AutoConfig.from_pretrained(UpperCamelCase      )
       self.assertEqual(type(UpperCamelCase      )      ,							UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Tuple:
     try:
       AutoConfig.register('custom'      ,							UpperCamelCase      )
       # Wrong model type will raise an error
       with self.assertRaises(UpperCamelCase      ):
         AutoConfig.register('model'      ,							UpperCamelCase      )
       # Trying to register something existing in the Transformers library will raise an error
       with self.assertRaises(UpperCamelCase      ):
         AutoConfig.register('bert'      ,							UpperCamelCase      )

       # Now that the config is registered, it can be used as any other config with the auto-API
       __a			=					CustomConfig()
       with tempfile.TemporaryDirectory() as tmp_dir:
         config.save_pretrained(UpperCamelCase      )
         __a			=					AutoConfig.from_pretrained(UpperCamelCase      )
         self.assertIsInstance(UpperCamelCase      ,							UpperCamelCase      )

     finally:
       if "custom" in CONFIG_MAPPING._extra_content:
         del CONFIG_MAPPING._extra_content["custom"]







   def 		UpperCamelCase__			(	self      )				->    Dict:
     with self.assertRaisesRegex(
         UpperCamelCase      ,							'bert-base is not a local folder and is not a valid model identifier'      ):
       __a			=					AutoConfig.from_pretrained('bert-base'      )







   def 		UpperCamelCase__			(	self      )				->    Tuple:
     with self.assertRaisesRegex(
         UpperCamelCase      ,							R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'      ):
       __a			=					AutoConfig.from_pretrained(UpperCamelCase      ,							revision='aaaaaa'      )







   def 		UpperCamelCase__			(	self      )				->    Optional[int]:
     with self.assertRaisesRegex(
         UpperCamelCase      ,							'hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.'      ,							):
       __a			=					AutoConfig.from_pretrained('hf-internal-testing/no-config-test-repo'      )







   def 		UpperCamelCase__			(	self      )				->    int:
     # If remote code is not set, we will time out when asking whether to load the model.
     with self.assertRaises(UpperCamelCase      ):
       __a			=					AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'      )
     # If remote code is disabled, we can't load this config.
     with self.assertRaises(UpperCamelCase      ):
       __a			=					AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'      ,							trust_remote_code=UpperCamelCase      )

     __a			=					AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'      ,							trust_remote_code=UpperCamelCase      )
     self.assertEqual(config.__class__.__name__      ,							'NewModelConfig'      )

     # Test config can be reloaded.
     with tempfile.TemporaryDirectory() as tmp_dir:
       config.save_pretrained(UpperCamelCase      )
       __a			=					AutoConfig.from_pretrained(UpperCamelCase      ,							trust_remote_code=UpperCamelCase      )
     self.assertEqual(reloaded_config.__class__.__name__      ,							'NewModelConfig'      )







   def 		UpperCamelCase__			(	self      )				->    Tuple:





     class    __lowercase						(  __magic_name__ ):
        _a							=      """new-model"""

     try:
       AutoConfig.register('new-model'      ,							UpperCamelCase      )
       # If remote code is not set, the default is to use local
       __a			=					AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'      )
       self.assertEqual(config.__class__.__name__      ,							'NewModelConfigLocal'      )

       # If remote code is disabled, we load the local one.
       __a			=					AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'      ,							trust_remote_code=UpperCamelCase      )
       self.assertEqual(config.__class__.__name__      ,							'NewModelConfigLocal'      )

       # If remote is enabled, we load from the Hub
       __a			=					AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'      ,							trust_remote_code=UpperCamelCase      )
       self.assertEqual(config.__class__.__name__      ,							'NewModelConfig'      )

     finally:
       if "new-model" in CONFIG_MAPPING._extra_content:
         del CONFIG_MAPPING._extra_content["new-model"]









'''simple docstring'''


from collections import OrderedDict
from typing import Mapping

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


UpperCAmelCase_ 		=      logging.get_logger(__name__)

UpperCAmelCase_ 		=      {
    "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/config.json",
    "distilbert-base-uncased-distilled-squad": (
        "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json"
    ),
    "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/config.json",
    "distilbert-base-cased-distilled-squad": (
        "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json"
    ),
    "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json",
    "distilbert-base-multilingual-cased": (
        "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json"
    ),
    "distilbert-base-uncased-finetuned-sst-2-english": (
        "https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json"
    ),
}





class    __lowercase						(  __magic_name__ ):
   _a							=      """distilbert"""
   _a							=      {
       """hidden_size""": """dim""",
       """num_attention_heads""": """n_heads""",
       """num_hidden_layers""": """n_layers""",
   }







   def __init__(	self      ,							UpperCamelCase=3_0522      ,							UpperCamelCase=512      ,							UpperCamelCase=False      ,							UpperCamelCase=6      ,							UpperCamelCase=12      ,							UpperCamelCase=768      ,							UpperCamelCase=4 * 768      ,							UpperCamelCase=0.1      ,							UpperCamelCase=0.1      ,							UpperCamelCase="gelu"      ,							UpperCamelCase=0.02      ,							UpperCamelCase=0.1      ,							UpperCamelCase=0.2      ,							UpperCamelCase=0      ,							**UpperCamelCase      ,							)				->    int:
     __a			=					vocab_size
     __a			=					max_position_embeddings
     __a			=					sinusoidal_pos_embds
     __a			=					n_layers
     __a			=					n_heads
     __a			=					dim
     __a			=					hidden_dim
     __a			=					dropout
     __a			=					attention_dropout
     __a			=					activation
     __a			=					initializer_range
     __a			=					qa_dropout
     __a			=					seq_classif_dropout
     super().__init__(**UpperCamelCase      ,							pad_token_id=UpperCamelCase      )





class    __lowercase						(  __magic_name__ ):







   @property
   def 		UpperCamelCase__			(	self      )				->    Mapping[str, Mapping[int, str]]:
     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),
         ]      )



