

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




				def    a							( self	):
											snake_case_          =   0




				def    a							( self	):
											snake_case_          =   AutoImageProcessor.from_pretrained('openai/clip-vit-base-patch32'	)
											self.assertIsInstance(snake_case				,       snake_case	)




				def    a							( self	):
											with tempfile.TemporaryDirectory() as tmpdirname:
																		snake_case_          =   Path(snake_case	) / 'preprocessor_config.json'
																		snake_case_          =   Path(snake_case	) / 'config.json'
																		json.dump(
																		    {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}				,       open(snake_case				,       'w'	)				,       )
																		json.dump({'model_type': 'clip'}				,       open(snake_case				,       'w'	)	)

																		snake_case_          =   AutoImageProcessor.from_pretrained(snake_case	)
																		self.assertIsInstance(snake_case				,       snake_case	)




				def    a							( self	):
											# Ensure we can load the image processor from the feature extractor config
											with tempfile.TemporaryDirectory() as tmpdirname:
																		snake_case_          =   Path(snake_case	) / 'preprocessor_config.json'
																		snake_case_          =   Path(snake_case	) / 'config.json'
																		json.dump(
																		    {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'}				,       open(snake_case				,       'w'	)				,       )
																		json.dump({'model_type': 'clip'}				,       open(snake_case				,       'w'	)	)

																		snake_case_          =   AutoImageProcessor.from_pretrained(snake_case	)
																		self.assertIsInstance(snake_case				,       snake_case	)




				def    a							( self	):
											with tempfile.TemporaryDirectory() as tmpdirname:
																		snake_case_          =   CLIPConfig()

																		# Create a dummy config file with image_proceesor_type
																		snake_case_          =   Path(snake_case	) / 'preprocessor_config.json'
																		snake_case_          =   Path(snake_case	) / 'config.json'
																		json.dump(
																		    {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}				,       open(snake_case				,       'w'	)				,       )
																		json.dump({'model_type': 'clip'}				,       open(snake_case				,       'w'	)	)

																		# remove image_processor_type to make sure config.json alone is enough to load image processor locally
																		snake_case_          =   AutoImageProcessor.from_pretrained(snake_case	).to_dict()

																		config_dict.pop('image_processor_type'	)
																		snake_case_          =   CLIPImageProcessor(**snake_case	)

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

																		snake_case_          =   AutoImageProcessor.from_pretrained(snake_case	)

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

											self.assertIsInstance(snake_case				,       snake_case	)




				def    a							( self	):
											with tempfile.TemporaryDirectory() as tmpdirname:
																		snake_case_          =   Path(snake_case	) / 'preprocessor_config.json'
																		json.dump(
																		    {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}				,       open(snake_case				,       'w'	)				,       )

																		snake_case_          =   AutoImageProcessor.from_pretrained(snake_case	)
																		self.assertIsInstance(snake_case				,       snake_case	)




				def    a							( self	):
											with self.assertRaisesRegex(
											    snake_case				,       'clip-base is not a local folder and is not a valid model identifier'	):
																		snake_case_          =   AutoImageProcessor.from_pretrained('clip-base'	)




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




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




				def    a							( self	):
											# If remote code is not set, we will time out when asking whether to load the model.
											with self.assertRaises(snake_case	):
																		snake_case_          =   AutoImageProcessor.from_pretrained('hf-internal-testing/test_dynamic_image_processor'	)
											# If remote code is disabled, we can't load this config.
											with self.assertRaises(snake_case	):
																		snake_case_          =   AutoImageProcessor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_image_processor'				,       trust_remote_code=snake_case	)

											snake_case_          =   AutoImageProcessor.from_pretrained(
											    'hf-internal-testing/test_dynamic_image_processor'				,       trust_remote_code=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(snake_case	)
																		snake_case_          =   AutoImageProcessor.from_pretrained(snake_case				,       trust_remote_code=snake_case	)
											self.assertEqual(reloaded_image_processor.__class__.__name__				,       'NewImageProcessor'	)




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

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

																									snake_case_          =   CustomImageProcessor.from_pretrained(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(snake_case	)
																									snake_case_          =   AutoImageProcessor.from_pretrained(snake_case	)
																									self.assertIsInstance(snake_case				,       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    a							( self	):







											class 			lowercase    (	lowercase_      ):
															__SCREAMING_SNAKE_CASE :			Tuple	      =      True

											try:
																		AutoConfig.register('custom'				,       snake_case	)
																		AutoImageProcessor.register(snake_case				,       snake_case	)
																		# If remote code is not set, the default is to use local
																		snake_case_          =   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.
																		snake_case_          =   AutoImageProcessor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_image_processor'				,       trust_remote_code=snake_case	)
																		self.assertEqual(image_processor.__class__.__name__				,       'NewImageProcessor'	)
																		self.assertTrue(image_processor.is_local	)

																		# If remote is enabled, we load from the Hub
																		snake_case_          =   AutoImageProcessor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_image_processor'				,       trust_remote_code=snake_case	)
																		self.assertEqual(image_processor.__class__.__name__				,       'NewImageProcessor'	)
																		self.assertTrue(not hasattr(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]







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


_UpperCAmelCase		:       str						    =	logging.get_logger(__name__)

_UpperCAmelCase		:       int						    =	{
    """google/canine-s""": """https://huggingface.co/google/canine-s/resolve/main/config.json""",
    # See all CANINE models at https://huggingface.co/models?filter=canine
}







class 			lowercase    (	lowercase_      ):
				__SCREAMING_SNAKE_CASE :			int	      =      '''canine'''




				def __init__( self				,       snake_case=768				,       snake_case=12				,       snake_case=12				,       snake_case=3072				,       snake_case="gelu"				,       snake_case=0.1				,       snake_case=0.1				,       snake_case=1_6384				,       snake_case=16				,       snake_case=0.02				,       snake_case=1e-1_2				,       snake_case=0				,       snake_case=0xE000				,       snake_case=0xE001				,       snake_case=4				,       snake_case=4				,       snake_case=8				,       snake_case=1_6384				,       snake_case=128				,       **snake_case				,       ):
											super().__init__(pad_token_id=snake_case				,       bos_token_id=snake_case				,       eos_token_id=snake_case				,       **snake_case	)

											snake_case_          =   max_position_embeddings
											snake_case_          =   hidden_size
											snake_case_          =   num_hidden_layers
											snake_case_          =   num_attention_heads
											snake_case_          =   intermediate_size
											snake_case_          =   hidden_act
											snake_case_          =   hidden_dropout_prob
											snake_case_          =   attention_probs_dropout_prob
											snake_case_          =   initializer_range
											snake_case_          =   type_vocab_size
											snake_case_          =   layer_norm_eps

											# Character config:
											snake_case_          =   downsampling_rate
											snake_case_          =   upsampling_kernel_size
											snake_case_          =   num_hash_functions
											snake_case_          =   num_hash_buckets
											snake_case_          =   local_transformer_stride




