
'''simple docstring'''




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

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


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

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402





class  A				(							unittest.TestCase						):





     def 				__lowerCAmelCase							(							self	) ->				List[str]:






         """simple docstring"""






         A       :   Optional[int]					=						0





     def 				__lowerCAmelCase							(							self	) ->				Union[str, Any]:






         """simple docstring"""






         A       :   int					=						AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32'''	)
         self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				List[Any]:






         """simple docstring"""






         with tempfile.TemporaryDirectory() as tmpdirname:
             A       :   Dict					=						Path(SCREAMING_SNAKE_CASE	) / '''preprocessor_config.json'''
             A       :   Optional[Any]					=						Path(SCREAMING_SNAKE_CASE	) / '''config.json'''
             json.dump(
                 {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}						,			open(SCREAMING_SNAKE_CASE						,			'''w'''	)						,			)
             json.dump({'''model_type''': '''clip'''}						,			open(SCREAMING_SNAKE_CASE						,			'''w'''	)	)

             A       :   Optional[Any]					=						AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE	)
             self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				str:






         """simple docstring"""






         with tempfile.TemporaryDirectory() as tmpdirname:
             A       :   int					=						Path(SCREAMING_SNAKE_CASE	) / '''preprocessor_config.json'''
             A       :   Tuple					=						Path(SCREAMING_SNAKE_CASE	) / '''config.json'''
             json.dump(
                 {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}						,			open(SCREAMING_SNAKE_CASE						,			'''w'''	)						,			)
             json.dump({'''model_type''': '''clip'''}						,			open(SCREAMING_SNAKE_CASE						,			'''w'''	)	)

             A       :   List[str]					=						AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE	)
             self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				List[str]:






         """simple docstring"""






         with tempfile.TemporaryDirectory() as tmpdirname:
             A       :   List[Any]					=						CLIPConfig()

             # Create a dummy config file with image_proceesor_type
             A       :   Any					=						Path(SCREAMING_SNAKE_CASE	) / '''preprocessor_config.json'''
             A       :   List[Any]					=						Path(SCREAMING_SNAKE_CASE	) / '''config.json'''
             json.dump(
                 {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}						,			open(SCREAMING_SNAKE_CASE						,			'''w'''	)						,			)
             json.dump({'''model_type''': '''clip'''}						,			open(SCREAMING_SNAKE_CASE						,			'''w'''	)	)

             # remove image_processor_type to make sure config.json alone is enough to load image processor locally
             A       :   List[str]					=						AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE	).to_dict()

             config_dict.pop('''image_processor_type'''	)
             A       :   List[str]					=						CLIPImageProcessor(**SCREAMING_SNAKE_CASE	)

             # save in new folder
             model_config.save_pretrained(SCREAMING_SNAKE_CASE	)
             config.save_pretrained(SCREAMING_SNAKE_CASE	)

             A       :   Optional[int]					=						AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE	)

             # make sure private variable is not incorrectly saved
             A       :   int					=						json.loads(config.to_json_string()	)
             self.assertTrue('''_processor_class''' not in dict_as_saved	)

         self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				Tuple:






         """simple docstring"""






         with tempfile.TemporaryDirectory() as tmpdirname:
             A       :   Optional[Any]					=						Path(SCREAMING_SNAKE_CASE	) / '''preprocessor_config.json'''
             json.dump(
                 {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}						,			open(SCREAMING_SNAKE_CASE						,			'''w'''	)						,			)

             A       :   Tuple					=						AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE	)
             self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				Tuple:






         """simple docstring"""






         with self.assertRaisesRegex(
             SCREAMING_SNAKE_CASE						,			'''clip-base is not a local folder and is not a valid model identifier'''	):
             A       :   Union[str, Any]					=						AutoImageProcessor.from_pretrained('''clip-base'''	)





     def 				__lowerCAmelCase							(							self	) ->				Any:






         """simple docstring"""






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





     def 				__lowerCAmelCase							(							self	) ->				str:






         """simple docstring"""






         with self.assertRaisesRegex(
             SCREAMING_SNAKE_CASE						,			'''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''						,			):
             A       :   Tuple					=						AutoImageProcessor.from_pretrained('''hf-internal-testing/config-no-model'''	)





     def 				__lowerCAmelCase							(							self	) ->				Optional[Any]:






         """simple docstring"""






         with self.assertRaises(SCREAMING_SNAKE_CASE	):
             A       :   Dict					=						AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor'''	)
         # If remote code is disabled, we can't load this config.
         with self.assertRaises(SCREAMING_SNAKE_CASE	):
             A       :   Union[str, Any]					=						AutoImageProcessor.from_pretrained(
                 '''hf-internal-testing/test_dynamic_image_processor'''						,			trust_remote_code=SCREAMING_SNAKE_CASE	)

         A       :   List[Any]					=						AutoImageProcessor.from_pretrained(
             '''hf-internal-testing/test_dynamic_image_processor'''						,			trust_remote_code=SCREAMING_SNAKE_CASE	)
         self.assertEqual(image_processor.__class__.__name__						,			'''NewImageProcessor'''	)

         # Test image processor can be reloaded.
         with tempfile.TemporaryDirectory() as tmp_dir:
             image_processor.save_pretrained(SCREAMING_SNAKE_CASE	)
             A       :   Union[str, Any]					=						AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE						,			trust_remote_code=SCREAMING_SNAKE_CASE	)
         self.assertEqual(reloaded_image_processor.__class__.__name__						,			'''NewImageProcessor'''	)





     def 				__lowerCAmelCase							(							self	) ->				Tuple:






         """simple docstring"""






         try:
             AutoConfig.register('''custom'''						,			SCREAMING_SNAKE_CASE	)
             AutoImageProcessor.register(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)
             # Trying to register something existing in the Transformers library will raise an error
             with self.assertRaises(SCREAMING_SNAKE_CASE	):
                 AutoImageProcessor.register(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)

             with tempfile.TemporaryDirectory() as tmpdirname:
                 A       :   Any					=						Path(SCREAMING_SNAKE_CASE	) / '''preprocessor_config.json'''
                 A       :   Any					=						Path(SCREAMING_SNAKE_CASE	) / '''config.json'''
                 json.dump(
                     {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}						,			open(SCREAMING_SNAKE_CASE						,			'''w'''	)						,			)
                 json.dump({'''model_type''': '''clip'''}						,			open(SCREAMING_SNAKE_CASE						,			'''w'''	)	)

                 A       :   Any					=						CustomImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE	)

             # Now that the config is registered, it can be used as any other config with the auto-API
             with tempfile.TemporaryDirectory() as tmp_dir:
                 image_processor.save_pretrained(SCREAMING_SNAKE_CASE	)
                 A       :   List[str]					=						AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE	)
                 self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)

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





     def 				__lowerCAmelCase							(							self	) ->				Any:






         """simple docstring"""






         class  A				(							__snake_case						):
              __magic_name__						=		True

         try:
             AutoConfig.register('''custom'''						,			SCREAMING_SNAKE_CASE	)
             AutoImageProcessor.register(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)
             # If remote code is not set, the default is to use local
             A       :   List[Any]					=						AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor'''	)
             self.assertEqual(image_processor.__class__.__name__						,			'''NewImageProcessor'''	)
             self.assertTrue(image_processor.is_local	)

             # If remote code is disabled, we load the local one.
             A       :   Union[str, Any]					=						AutoImageProcessor.from_pretrained(
                 '''hf-internal-testing/test_dynamic_image_processor'''						,			trust_remote_code=SCREAMING_SNAKE_CASE	)
             self.assertEqual(image_processor.__class__.__name__						,			'''NewImageProcessor'''	)
             self.assertTrue(image_processor.is_local	)

             # If remote is enabled, we load from the Hub
             A       :   int					=						AutoImageProcessor.from_pretrained(
                 '''hf-internal-testing/test_dynamic_image_processor'''						,			trust_remote_code=SCREAMING_SNAKE_CASE	)
             self.assertEqual(image_processor.__class__.__name__						,			'''NewImageProcessor'''	)
             self.assertTrue(not hasattr(SCREAMING_SNAKE_CASE						,			'''is_local'''	)	)

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






'''simple docstring'''




from collections import OrderedDict
from typing import Mapping

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


lowercase			:				Union[str, Any]    		=			logging.get_logger(__name__)

lowercase			:				str    		=			{
    'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/config.json',
    'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/config.json',
    'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/config.json',
    'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/config.json',
    'bert-base-multilingual-uncased': 'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/config.json',
    'bert-base-multilingual-cased': 'https://huggingface.co/bert-base-multilingual-cased/resolve/main/config.json',
    'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/config.json',
    'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/config.json',
    'bert-large-uncased-whole-word-masking': (
        'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/config.json'
    ),
    'bert-large-cased-whole-word-masking': (
        'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/config.json'
    ),
    'bert-large-uncased-whole-word-masking-finetuned-squad': (
        'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/config.json'
    ),
    'bert-large-cased-whole-word-masking-finetuned-squad': (
        'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/config.json'
    ),
    'bert-base-cased-finetuned-mrpc': 'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/config.json',
    'bert-base-german-dbmdz-cased': 'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/config.json',
    'bert-base-german-dbmdz-uncased': 'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/config.json',
    'cl-tohoku/bert-base-japanese': 'https://huggingface.co/cl-tohoku/bert-base-japanese/resolve/main/config.json',
    'cl-tohoku/bert-base-japanese-whole-word-masking': (
        'https://huggingface.co/cl-tohoku/bert-base-japanese-whole-word-masking/resolve/main/config.json'
    ),
    'cl-tohoku/bert-base-japanese-char': (
        'https://huggingface.co/cl-tohoku/bert-base-japanese-char/resolve/main/config.json'
    ),
    'cl-tohoku/bert-base-japanese-char-whole-word-masking': (
        'https://huggingface.co/cl-tohoku/bert-base-japanese-char-whole-word-masking/resolve/main/config.json'
    ),
    'TurkuNLP/bert-base-finnish-cased-v1': (
        'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/config.json'
    ),
    'TurkuNLP/bert-base-finnish-uncased-v1': (
        'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/config.json'
    ),
    'wietsedv/bert-base-dutch-cased': 'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/config.json',
    # See all BERT models at https://huggingface.co/models?filter=bert
}





class  A				(							__snake_case						):
     __magic_name__						=		'''bert'''





     def __init__(							self						,			SCREAMING_SNAKE_CASE=30522						,			SCREAMING_SNAKE_CASE=768						,			SCREAMING_SNAKE_CASE=12						,			SCREAMING_SNAKE_CASE=12						,			SCREAMING_SNAKE_CASE=3072						,			SCREAMING_SNAKE_CASE="gelu"						,			SCREAMING_SNAKE_CASE=0.1						,			SCREAMING_SNAKE_CASE=0.1						,			SCREAMING_SNAKE_CASE=512						,			SCREAMING_SNAKE_CASE=2						,			SCREAMING_SNAKE_CASE=0.02						,			SCREAMING_SNAKE_CASE=1e-12						,			SCREAMING_SNAKE_CASE=0						,			SCREAMING_SNAKE_CASE="absolute"						,			SCREAMING_SNAKE_CASE=True						,			SCREAMING_SNAKE_CASE=None						,			**SCREAMING_SNAKE_CASE						,			) ->				Optional[int]:






         """simple docstring"""






         super().__init__(pad_token_id=SCREAMING_SNAKE_CASE						,			**SCREAMING_SNAKE_CASE	)

         A       :   Optional[int]					=						vocab_size
         A       :   Optional[Any]					=						hidden_size
         A       :   List[Any]					=						num_hidden_layers
         A       :   List[str]					=						num_attention_heads
         A       :   Dict					=						hidden_act
         A       :   Optional[Any]					=						intermediate_size
         A       :   List[Any]					=						hidden_dropout_prob
         A       :   List[Any]					=						attention_probs_dropout_prob
         A       :   Optional[Any]					=						max_position_embeddings
         A       :   List[str]					=						type_vocab_size
         A       :   Dict					=						initializer_range
         A       :   str					=						layer_norm_eps
         A       :   int					=						position_embedding_type
         A       :   Dict					=						use_cache
         A       :   str					=						classifier_dropout





class  A				(							__snake_case						):





     @property
     def 				__lowerCAmelCase							(							self	) ->				Mapping[str, Mapping[int, str]]:






         """simple docstring"""






         if self.task == "multiple-choice":
             A       :   Optional[Any]					=						{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
         else:
             A       :   Optional[int]					=						{0: '''batch''', 1: '''sequence'''}
         return OrderedDict(
             [
                 ('''input_ids''', dynamic_axis),
                 ('''attention_mask''', dynamic_axis),
                 ('''token_type_ids''', dynamic_axis),
             ]	)




