

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


lowercase_   							=  get_tests_dir('fixtures/dummy-config.json')





class 					__lowerCAmelCase	(   unittest.TestCase				):




  def     A__	(							self   )    ->					List[Any]:
        '''simple docstring'''






        _lowercase      			=0




  def     A__	(							self   )    ->					Optional[int]:
        '''simple docstring'''






        self.assertIsNotNone(transformers.models.auto.__spec__   )
        self.assertIsNotNone(importlib.util.find_spec('transformers.models.auto'   )   )




  def     A__	(							self   )    ->					Union[str, Any]:
        '''simple docstring'''






        _lowercase      			=AutoConfig.from_pretrained('bert-base-uncased'   )
        self.assertIsInstance(lowerCAmelCase				,  lowerCAmelCase   )




  def     A__	(							self   )    ->					Any:
        '''simple docstring'''






        _lowercase      			=AutoConfig.from_pretrained(lowerCAmelCase   )
        self.assertIsInstance(lowerCAmelCase				,  lowerCAmelCase   )




  def     A__	(							self   )    ->					Any:
        '''simple docstring'''






        _lowercase      			=AutoConfig.from_pretrained(lowerCAmelCase   )
        self.assertIsInstance(lowerCAmelCase				,  lowerCAmelCase   )




  def     A__	(							self   )    ->					List[Any]:
        '''simple docstring'''






        _lowercase      			=AutoConfig.for_model('roberta'   )
        self.assertIsInstance(lowerCAmelCase				,  lowerCAmelCase   )




  def     A__	(							self   )    ->					List[Any]:
        '''simple docstring'''






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




  def     A__	(							self   )    ->					Optional[int]:
        '''simple docstring'''






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

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

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




  def     A__	(							self   )    ->					Optional[Any]:
        '''simple docstring'''






        with self.assertRaisesRegex(
            lowerCAmelCase				,  'bert-base is not a local folder and is not a valid model identifier'   ):
              _lowercase      			=AutoConfig.from_pretrained('bert-base'   )




  def     A__	(							self   )    ->					Optional[Any]:
        '''simple docstring'''






        with self.assertRaisesRegex(
            lowerCAmelCase				,  R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'   ):
              _lowercase      			=AutoConfig.from_pretrained(lowerCAmelCase				,  revision='aaaaaa'   )




  def     A__	(							self   )    ->					Optional[int]:
        '''simple docstring'''






        with self.assertRaisesRegex(
            lowerCAmelCase				,  'hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.'				,  ):
              _lowercase      			=AutoConfig.from_pretrained('hf-internal-testing/no-config-test-repo'   )




  def     A__	(							self   )    ->					Optional[int]:
        '''simple docstring'''






        with self.assertRaises(lowerCAmelCase   ):
              _lowercase      			=AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'   )
        # If remote code is disabled, we can't load this config.
        with self.assertRaises(lowerCAmelCase   ):
              _lowercase      			=AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'				,  trust_remote_code=lowerCAmelCase   )

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

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




  def     A__	(							self   )    ->					Union[str, Any]:
        '''simple docstring'''






        class 					__lowerCAmelCase	(   SCREAMING_SNAKE_CASE				):
          _a		     =    """new-model"""

        try:
              AutoConfig.register('new-model'				,  lowerCAmelCase   )
              # If remote code is not set, the default is to use local
              _lowercase      			=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.
              _lowercase      			=AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'				,  trust_remote_code=lowerCAmelCase   )
              self.assertEqual(config.__class__.__name__				,  'NewModelConfigLocal'   )

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

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





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


lowercase_   							=  logging.get_logger(__name__)

lowercase_   							=  {
    'facebook/timesformer': 'https://huggingface.co/facebook/timesformer/resolve/main/config.json',
}





class 					__lowerCAmelCase	(   SCREAMING_SNAKE_CASE				):
  _a		     =    """timesformer"""




  def __init__(							self				,  lowerCAmelCase=224				,  lowerCAmelCase=16				,  lowerCAmelCase=3				,  lowerCAmelCase=8				,  lowerCAmelCase=768				,  lowerCAmelCase=12				,  lowerCAmelCase=12				,  lowerCAmelCase=3_072				,  lowerCAmelCase="gelu"				,  lowerCAmelCase=0.0				,  lowerCAmelCase=0.0				,  lowerCAmelCase=0.02				,  lowerCAmelCase=1e-6				,  lowerCAmelCase=True				,  lowerCAmelCase="divided_space_time"				,  lowerCAmelCase=0				,  **lowerCAmelCase				,  )    ->					Any:
        '''simple docstring'''






        super().__init__(**lowerCAmelCase   )

        _lowercase      			=image_size
        _lowercase      			=patch_size
        _lowercase      			=num_channels
        _lowercase      			=num_frames

        _lowercase      			=hidden_size
        _lowercase      			=num_hidden_layers
        _lowercase      			=num_attention_heads
        _lowercase      			=intermediate_size
        _lowercase      			=hidden_act
        _lowercase      			=hidden_dropout_prob
        _lowercase      			=attention_probs_dropout_prob
        _lowercase      			=initializer_range
        _lowercase      			=layer_norm_eps
        _lowercase      			=qkv_bias

        _lowercase      			=attention_type
        _lowercase      			=drop_path_rate


