


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


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




class A  (   unittest.TestCase     ):






      def        lowerCamelCase       (					self						:     List[Any] )					->						Dict:
             """simple docstring"""




             _lowerCamelCase       :		Union[str, Any]		=0






      def        lowerCamelCase       (					self						:     str )					->						int:
             """simple docstring"""




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






      def        lowerCamelCase       (					self						:     Optional[Any] )					->						List[str]:
             """simple docstring"""




             _lowerCamelCase       :		List[str]		=AutoConfig.from_pretrained('bert-base-uncased' )
             self.assertIsInstance(lowercase_			,   lowercase_ )






      def        lowerCamelCase       (					self						:     Tuple )					->						List[Any]:
             """simple docstring"""




             _lowerCamelCase       :		int		=AutoConfig.from_pretrained(lowercase_ )
             self.assertIsInstance(lowercase_			,   lowercase_ )






      def        lowerCamelCase       (					self						:     Dict )					->						Union[str, Any]:
             """simple docstring"""




             _lowerCamelCase       :		int		=AutoConfig.from_pretrained(lowercase_ )
             self.assertIsInstance(lowercase_			,   lowercase_ )






      def        lowerCamelCase       (					self						:     Tuple )					->						List[str]:
             """simple docstring"""




             _lowerCamelCase       :		Optional[int]		=AutoConfig.for_model('roberta' )
             self.assertIsInstance(lowercase_			,   lowercase_ )






      def        lowerCamelCase       (					self						:     Tuple )					->						str:
             """simple docstring"""




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






      def        lowerCamelCase       (					self						:     str )					->						List[str]:
             """simple docstring"""




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

                    # Now that the config is registered, it can be used as any other config with the auto-API
                    _lowerCamelCase       :		Union[str, Any]		=CustomConfig()
                    with tempfile.TemporaryDirectory() as tmp_dir:
                           config.save_pretrained(lowercase_ )
                           _lowerCamelCase       :		Any		=AutoConfig.from_pretrained(lowercase_ )
                           self.assertIsInstance(lowercase_			,   lowercase_ )

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






      def        lowerCamelCase       (					self						:     Any )					->						Optional[Any]:
             """simple docstring"""




             with self.assertRaisesRegex(
                 lowercase_			,   'bert-base is not a local folder and is not a valid model identifier' ):
                    _lowerCamelCase       :		Union[str, Any]		=AutoConfig.from_pretrained('bert-base' )






      def        lowerCamelCase       (					self						:     Tuple )					->						List[Any]:
             """simple docstring"""




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






      def        lowerCamelCase       (					self						:     str )					->						str:
             """simple docstring"""




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






      def        lowerCamelCase       (					self						:     Optional[Any] )					->						int:
             """simple docstring"""




             with self.assertRaises(lowercase_ ):
                    _lowerCamelCase       :		Optional[int]		=AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model' )
             # If remote code is disabled, we can't load this config.
             with self.assertRaises(lowercase_ ):
                    _lowerCamelCase       :		List[str]		=AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'			,   trust_remote_code=lowercase_ )

             _lowerCamelCase       :		List[Any]		=AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'			,   trust_remote_code=lowercase_ )
             self.assertEqual(config.__class__.__name__			,   'NewModelConfig' )

             # Test config can be reloaded.
             with tempfile.TemporaryDirectory() as tmp_dir:
                    config.save_pretrained(lowercase_ )
                    _lowerCamelCase       :		List[str]		=AutoConfig.from_pretrained(lowercase_			,   trust_remote_code=lowercase_ )
             self.assertEqual(reloaded_config.__class__.__name__			,   'NewModelConfig' )






      def        lowerCamelCase       (					self						:     str )					->						int:
             """simple docstring"""




             class A  (   UpperCamelCase_     ):
                   UpperCamelCase__							:  int      ='new-model'

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

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

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






import doctest
import logging
import os
import unittest
from pathlib import Path
from typing import List, Union

import transformers
from transformers.testing_utils import require_tf, require_torch, slow


lowerCamelCase											=       logging.getLogger()




@unittest.skip('Temporarily disable the doc tests.'     )
@require_torch
@require_tf
@slow
class A  (   unittest.TestCase     ):






      def        lowerCamelCase       (					self						:     Dict			,   lowercase_						:     Path			,   lowercase_						:     Union[str, None] = None			,   lowercase_						:     Union[List[str], None] = None			,   lowercase_						:     Union[str, List[str], None] = None			,   lowercase_						:     bool = True			,   )					->						Optional[int]:
             """simple docstring"""




             _lowerCamelCase       :		Optional[Any]		=[file for file in os.listdir(lowercase_ ) if os.path.isfile(os.path.join(lowercase_			,   lowercase_ ) )]

             if identifier is not None:
                    _lowerCamelCase       :		Any		=[file for file in files if identifier in file]

             if n_identifier is not None:
                    if isinstance(lowercase_			,   lowercase_ ):
                           for n_ in n_identifier:
                                  _lowerCamelCase       :		Any		=[file for file in files if n_ not in file]
                    else:
                           _lowerCamelCase       :		List[Any]		=[file for file in files if n_identifier not in file]

             _lowerCamelCase       :		List[Any]		=ignore_files or []
             ignore_files.append('__init__.py' )
             _lowerCamelCase       :		List[Any]		=[file for file in files if file not in ignore_files]

             for file in files:
                    # Open all files
                    print('Testing'			,   lowercase_ )

                    if only_modules:
                           _lowerCamelCase       :		Optional[int]		=file.split('.' )[0]
                           try:
                                  _lowerCamelCase       :		Any		=getattr(lowercase_			,   lowercase_ )
                                  _lowerCamelCase       :		Optional[int]		=doctest.DocTestSuite(lowercase_ )
                                  _lowerCamelCase       :		List[Any]		=unittest.TextTestRunner().run(lowercase_ )
                                  self.assertIs(len(result.failures )			,   0 )
                           except AttributeError:
                                  logger.info(F'''{module_identifier} is not a module.''' )
                    else:
                           _lowerCamelCase       :		int		=doctest.testfile(str('..' / directory / file )			,   optionflags=doctest.ELLIPSIS )
                           self.assertIs(result.failed			,   0 )






      def        lowerCamelCase       (					self						:     Any )					->						Tuple:
             """simple docstring"""




             _lowerCamelCase       :		int		=Path('src/transformers' )
             _lowerCamelCase       :		Optional[Any]		='modeling'
             _lowerCamelCase       :		List[str]		=[
                 'modeling_ctrl.py',
                 'modeling_tf_ctrl.py',
             ]
             self.analyze_directory(lowercase_			,   identifier=lowercase_			,   ignore_files=lowercase_ )






      def        lowerCamelCase       (					self						:     List[str] )					->						Tuple:
             """simple docstring"""




             _lowerCamelCase       :		Optional[int]		=Path('src/transformers' )
             _lowerCamelCase       :		Union[str, Any]		='tokenization'
             self.analyze_directory(lowercase_			,   identifier=lowercase_ )






      def        lowerCamelCase       (					self						:     Optional[int] )					->						Dict:
             """simple docstring"""




             _lowerCamelCase       :		str		=Path('src/transformers' )
             _lowerCamelCase       :		Any		='configuration'
             self.analyze_directory(lowercase_			,   identifier=lowercase_ )






      def        lowerCamelCase       (					self						:     List[str] )					->						Dict:
             """simple docstring"""




             _lowerCamelCase       :		Union[str, Any]		=Path('src/transformers' )
             _lowerCamelCase       :		int		=['configuration', 'modeling', 'tokenization']
             self.analyze_directory(lowercase_			,   n_identifier=lowercase_ )






      def        lowerCamelCase       (					self						:     Optional[Any] )					->						int:
             """simple docstring"""




             _lowerCamelCase       :		str		=Path('docs/source' )
             _lowerCamelCase       :		Dict		=['favicon.ico']
             self.analyze_directory(lowercase_			,   ignore_files=lowercase_			,   only_modules=lowercase_ )


