






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




	def      __magic_name__	(      self	:							str      )	->						str:
							SCREAMING_SNAKE_CASE__     :				Any									=0




	def      __magic_name__	(      self	:							List[str]      )	->						Tuple:
							SCREAMING_SNAKE_CASE__     :				List[Any]									=AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32'''      )
							self.assertIsInstance(UpperCAmelCase_					,      UpperCAmelCase_      )




	def      __magic_name__	(      self	:							str      )	->						Union[str, Any]:
							with tempfile.TemporaryDirectory() as tmpdirname:
													SCREAMING_SNAKE_CASE__     :				Tuple									=Path(UpperCAmelCase_      ) / '''preprocessor_config.json'''
													SCREAMING_SNAKE_CASE__     :				Any									=Path(UpperCAmelCase_      ) / '''config.json'''
													json.dump(
													    {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}					,      open(UpperCAmelCase_					,      '''w'''      )					,      )
													json.dump({'''model_type''': '''clip'''}					,      open(UpperCAmelCase_					,      '''w'''      )      )

													SCREAMING_SNAKE_CASE__     :				Any									=AutoImageProcessor.from_pretrained(UpperCAmelCase_      )
													self.assertIsInstance(UpperCAmelCase_					,      UpperCAmelCase_      )




	def      __magic_name__	(      self	:							Optional[int]      )	->						Any:
							# Ensure we can load the image processor from the feature extractor config
							with tempfile.TemporaryDirectory() as tmpdirname:
													SCREAMING_SNAKE_CASE__     :				int									=Path(UpperCAmelCase_      ) / '''preprocessor_config.json'''
													SCREAMING_SNAKE_CASE__     :				Optional[int]									=Path(UpperCAmelCase_      ) / '''config.json'''
													json.dump(
													    {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}					,      open(UpperCAmelCase_					,      '''w'''      )					,      )
													json.dump({'''model_type''': '''clip'''}					,      open(UpperCAmelCase_					,      '''w'''      )      )

													SCREAMING_SNAKE_CASE__     :				str									=AutoImageProcessor.from_pretrained(UpperCAmelCase_      )
													self.assertIsInstance(UpperCAmelCase_					,      UpperCAmelCase_      )




	def      __magic_name__	(      self	:							List[str]      )	->						Tuple:
							with tempfile.TemporaryDirectory() as tmpdirname:
													SCREAMING_SNAKE_CASE__     :				Dict									=CLIPConfig()

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

													# remove image_processor_type to make sure config.json alone is enough to load image processor locally
													SCREAMING_SNAKE_CASE__     :				Dict									=AutoImageProcessor.from_pretrained(UpperCAmelCase_      ).to_dict()

													config_dict.pop('''image_processor_type'''      )
													SCREAMING_SNAKE_CASE__     :				Any									=CLIPImageProcessor(**UpperCAmelCase_      )

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

													SCREAMING_SNAKE_CASE__     :				Optional[int]									=AutoImageProcessor.from_pretrained(UpperCAmelCase_      )

													# make sure private variable is not incorrectly saved
													SCREAMING_SNAKE_CASE__     :				List[str]									=json.loads(config.to_json_string()      )
													self.assertTrue('''_processor_class''' not in dict_as_saved      )

							self.assertIsInstance(UpperCAmelCase_					,      UpperCAmelCase_      )




	def      __magic_name__	(      self	:							List[str]      )	->						str:
							with tempfile.TemporaryDirectory() as tmpdirname:
													SCREAMING_SNAKE_CASE__     :				Optional[Any]									=Path(UpperCAmelCase_      ) / '''preprocessor_config.json'''
													json.dump(
													    {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}					,      open(UpperCAmelCase_					,      '''w'''      )					,      )

													SCREAMING_SNAKE_CASE__     :				Any									=AutoImageProcessor.from_pretrained(UpperCAmelCase_      )
													self.assertIsInstance(UpperCAmelCase_					,      UpperCAmelCase_      )




	def      __magic_name__	(      self	:							Any      )	->						str:
							with self.assertRaisesRegex(
							    UpperCAmelCase_					,      '''clip-base is not a local folder and is not a valid model identifier'''      ):
													SCREAMING_SNAKE_CASE__     :				Tuple									=AutoImageProcessor.from_pretrained('''clip-base'''      )




	def      __magic_name__	(      self	:							Optional[Any]      )	->						Optional[int]:
							with self.assertRaisesRegex(
							    UpperCAmelCase_					,      r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''      ):
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=AutoImageProcessor.from_pretrained(UpperCAmelCase_					,      revision='''aaaaaa'''      )




	def      __magic_name__	(      self	:							List[str]      )	->						List[Any]:
							with self.assertRaisesRegex(
							    UpperCAmelCase_					,      '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''					,      ):
													SCREAMING_SNAKE_CASE__     :				str									=AutoImageProcessor.from_pretrained('''hf-internal-testing/config-no-model'''      )




	def      __magic_name__	(      self	:							Optional[Any]      )	->						Optional[int]:
							# If remote code is not set, we will time out when asking whether to load the model.
							with self.assertRaises(UpperCAmelCase_      ):
													SCREAMING_SNAKE_CASE__     :				List[str]									=AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor'''      )
							# If remote code is disabled, we can't load this config.
							with self.assertRaises(UpperCAmelCase_      ):
													SCREAMING_SNAKE_CASE__     :				Optional[Any]									=AutoImageProcessor.from_pretrained(
													    '''hf-internal-testing/test_dynamic_image_processor'''					,      trust_remote_code=UpperCAmelCase_      )

							SCREAMING_SNAKE_CASE__     :				Any									=AutoImageProcessor.from_pretrained(
							    '''hf-internal-testing/test_dynamic_image_processor'''					,      trust_remote_code=UpperCAmelCase_      )
							self.assertEqual(image_processor.__class__.__name__					,      '''NewImageProcessor'''      )

							# Test image processor can be reloaded.
							with tempfile.TemporaryDirectory() as tmp_dir:
													image_processor.save_pretrained(UpperCAmelCase_      )
													SCREAMING_SNAKE_CASE__     :				Optional[int]									=AutoImageProcessor.from_pretrained(UpperCAmelCase_					,      trust_remote_code=UpperCAmelCase_      )
							self.assertEqual(reloaded_image_processor.__class__.__name__					,      '''NewImageProcessor'''      )




	def      __magic_name__	(      self	:							List[Any]      )	->						Tuple:
							try:
													AutoConfig.register('''custom'''					,      UpperCAmelCase_      )
													AutoImageProcessor.register(UpperCAmelCase_					,      UpperCAmelCase_      )
													# Trying to register something existing in the Transformers library will raise an error
													with self.assertRaises(UpperCAmelCase_      ):
																			AutoImageProcessor.register(UpperCAmelCase_					,      UpperCAmelCase_      )

													with tempfile.TemporaryDirectory() as tmpdirname:
																			SCREAMING_SNAKE_CASE__     :				Optional[int]									=Path(UpperCAmelCase_      ) / '''preprocessor_config.json'''
																			SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=Path(UpperCAmelCase_      ) / '''config.json'''
																			json.dump(
																			    {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}					,      open(UpperCAmelCase_					,      '''w'''      )					,      )
																			json.dump({'''model_type''': '''clip'''}					,      open(UpperCAmelCase_					,      '''w'''      )      )

																			SCREAMING_SNAKE_CASE__     :				Any									=CustomImageProcessor.from_pretrained(UpperCAmelCase_      )

													# 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(UpperCAmelCase_      )
																			SCREAMING_SNAKE_CASE__     :				Optional[Any]									=AutoImageProcessor.from_pretrained(UpperCAmelCase_      )
																			self.assertIsInstance(UpperCAmelCase_					,      UpperCAmelCase_      )

							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      __magic_name__	(      self	:							Union[str, Any]      )	->						str:






							class      __SCREAMING_SNAKE_CASE      (			__UpperCAmelCase				):
								snake_case_  							=							True

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

													# If remote is enabled, we load from the Hub
													SCREAMING_SNAKE_CASE__     :				str									=AutoImageProcessor.from_pretrained(
													    '''hf-internal-testing/test_dynamic_image_processor'''					,      trust_remote_code=UpperCAmelCase_      )
													self.assertEqual(image_processor.__class__.__name__					,      '''NewImageProcessor'''      )
													self.assertTrue(not hasattr(UpperCAmelCase_					,      '''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 ...configuration_utils import PretrainedConfig
from ...utils import logging


a_					= logging.get_logger(__name__)

a_					= {
    'EleutherAI/gpt-neox-20b': 'https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/config.json',
    # See all GPTNeoX models at https://huggingface.co/models?filter=gpt_neox
}






class      __SCREAMING_SNAKE_CASE      (			lowerCamelCase				):
 snake_case_  							=							"""gpt_neox"""




 def __init__(      self	:							List[Any]					,      __lowercase	:							Union[str, Any]=5_04_32					,      __lowercase	:							int=61_44					,      __lowercase	:							Tuple=44					,      __lowercase	:							List[str]=64					,      __lowercase	:							str=2_45_76					,      __lowercase	:							Dict="gelu"					,      __lowercase	:							Tuple=0.25					,      __lowercase	:							Tuple=1_00_00					,      __lowercase	:							Tuple=0.0					,      __lowercase	:							str=0.0					,      __lowercase	:							List[Any]=0.1					,      __lowercase	:							Dict=20_48					,      __lowercase	:							Any=0.02					,      __lowercase	:							Dict=1e-5					,      __lowercase	:							List[Any]=True					,      __lowercase	:							str=0					,      __lowercase	:							Optional[Any]=2					,      __lowercase	:							Tuple=False					,      __lowercase	:							List[Any]=True					,      __lowercase	:							Optional[Any]=None					,      **__lowercase	:							Any					,      )	->						Dict:
       super().__init__(bos_token_id=__lowercase					,      eos_token_id=__lowercase					,      **__lowercase      )
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=vocab_size
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=max_position_embeddings
       SCREAMING_SNAKE_CASE__     :				Any									=hidden_size
       SCREAMING_SNAKE_CASE__     :				str									=num_hidden_layers
       SCREAMING_SNAKE_CASE__     :				Any									=num_attention_heads
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=intermediate_size
       SCREAMING_SNAKE_CASE__     :				Dict									=hidden_act
       SCREAMING_SNAKE_CASE__     :				str									=rotary_pct
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=rotary_emb_base
       SCREAMING_SNAKE_CASE__     :				List[Any]									=attention_dropout
       SCREAMING_SNAKE_CASE__     :				List[Any]									=hidden_dropout
       SCREAMING_SNAKE_CASE__     :				str									=classifier_dropout
       SCREAMING_SNAKE_CASE__     :				Any									=initializer_range
       SCREAMING_SNAKE_CASE__     :				Dict									=layer_norm_eps
       SCREAMING_SNAKE_CASE__     :				Any									=use_cache
       SCREAMING_SNAKE_CASE__     :				Tuple									=tie_word_embeddings
       SCREAMING_SNAKE_CASE__     :				Tuple									=use_parallel_residual
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=rope_scaling
       self._rope_scaling_validation()

       if self.hidden_size % self.num_attention_heads != 0:
             raise ValueError(
                 '''The hidden size is not divisble by the number of attention heads! Make sure to update them!'''      )




 def      __magic_name__	(      self	:							Optional[Any]      )	->						Optional[Any]:
       if self.rope_scaling is None:
             return

       if not isinstance(self.rope_scaling					,      __lowercase      ) or len(self.rope_scaling      ) != 2:
             raise ValueError(
                 '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '''
                 F"got {self.rope_scaling}"      )
       SCREAMING_SNAKE_CASE__     :				int									=self.rope_scaling.get('''type'''					,      __lowercase      )
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=self.rope_scaling.get('''factor'''					,      __lowercase      )
       if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
             raise ValueError(
                 F"`rope_scaling`'s name field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"      )
       if rope_scaling_factor is None or not isinstance(__lowercase					,      __lowercase      ) or rope_scaling_factor <= 1.0:
             raise ValueError(F"`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}"      )