
import copy
import os
from typing import Union

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


lowercase   =	logging.get_logger(__name__)

lowercase   =	{
    '''BAAI/AltCLIP''': '''https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json''',
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}





class    __A(					UpperCAmelCase							):
					SCREAMING_SNAKE_CASE           =     '''altclip_text_model'''

					def __init__(						self       :  Optional[int] ,       __UpperCamelCase       :  Optional[int]=2_5_0_0_0_2 ,       __UpperCamelCase       :  int=1_0_2_4 ,       __UpperCamelCase       :  List[Any]=2_4 ,       __UpperCamelCase       :  List[Any]=1_6 ,       __UpperCamelCase       :  Optional[int]=4_0_9_6 ,       __UpperCamelCase       :  List[str]="gelu" ,       __UpperCamelCase       :  int=0.1 ,       __UpperCamelCase       :  Optional[int]=0.1 ,       __UpperCamelCase       :  Optional[Any]=5_1_4 ,       __UpperCamelCase       :  List[Any]=1 ,       __UpperCamelCase       :  Any=0.02 ,       __UpperCamelCase       :  Any=0.02 ,       __UpperCamelCase       :  Optional[int]=1E-05 ,       __UpperCamelCase       :  List[str]=1 ,       __UpperCamelCase       :  Optional[Any]=0 ,       __UpperCamelCase       :  int=2 ,       __UpperCamelCase       :  Optional[int]="absolute" ,       __UpperCamelCase       :  str=True ,       __UpperCamelCase       :  List[Any]=7_6_8 ,       **__UpperCamelCase       :  List[str] ,       ):
												super().__init__(pad_token_id=__UpperCamelCase ,       bos_token_id=__UpperCamelCase ,       eos_token_id=__UpperCamelCase ,       **__UpperCamelCase )

												lowerCamelCase_					=       vocab_size
												lowerCamelCase_					=       hidden_size
												lowerCamelCase_					=       num_hidden_layers
												lowerCamelCase_					=       num_attention_heads
												lowerCamelCase_					=       hidden_act
												lowerCamelCase_					=       intermediate_size
												lowerCamelCase_					=       hidden_dropout_prob
												lowerCamelCase_					=       attention_probs_dropout_prob
												lowerCamelCase_					=       max_position_embeddings
												lowerCamelCase_					=       type_vocab_size
												lowerCamelCase_					=       initializer_range
												lowerCamelCase_					=       initializer_factor
												lowerCamelCase_					=       layer_norm_eps
												lowerCamelCase_					=       position_embedding_type
												lowerCamelCase_					=       use_cache
												lowerCamelCase_					=       project_dim





class    __A(					UpperCAmelCase							):
					SCREAMING_SNAKE_CASE           =     '''altclip_vision_model'''

					def __init__(						self       :  Optional[int] ,       __UpperCamelCase       :  List[str]=7_6_8 ,       __UpperCamelCase       :  Any=3_0_7_2 ,       __UpperCamelCase       :  Any=5_1_2 ,       __UpperCamelCase       :  str=1_2 ,       __UpperCamelCase       :  Optional[Any]=1_2 ,       __UpperCamelCase       :  Optional[Any]=3 ,       __UpperCamelCase       :  List[Any]=2_2_4 ,       __UpperCamelCase       :  Optional[Any]=3_2 ,       __UpperCamelCase       :  Optional[Any]="quick_gelu" ,       __UpperCamelCase       :  Optional[Any]=1E-5 ,       __UpperCamelCase       :  str=0.0 ,       __UpperCamelCase       :  int=0.02 ,       __UpperCamelCase       :  str=1.0 ,       **__UpperCamelCase       :  str ,       ):
												super().__init__(**__UpperCamelCase )

												lowerCamelCase_					=       hidden_size
												lowerCamelCase_					=       intermediate_size
												lowerCamelCase_					=       projection_dim
												lowerCamelCase_					=       num_hidden_layers
												lowerCamelCase_					=       num_attention_heads
												lowerCamelCase_					=       num_channels
												lowerCamelCase_					=       patch_size
												lowerCamelCase_					=       image_size
												lowerCamelCase_					=       initializer_range
												lowerCamelCase_					=       initializer_factor
												lowerCamelCase_					=       attention_dropout
												lowerCamelCase_					=       layer_norm_eps
												lowerCamelCase_					=       hidden_act



					@classmethod
					def  lowercase__   (						cls       :  Optional[Any] ,       __UpperCamelCase       :  Union[str, os.PathLike] ,       **__UpperCamelCase       :  int ):
												cls._set_token_in_kwargs(__UpperCamelCase )

												lowerCamelCase_	, lowerCamelCase_					=       cls.get_config_dict(__UpperCamelCase ,       **__UpperCamelCase )

												# get the vision config dict if we are loading from AltCLIPConfig
												if config_dict.get("""model_type""" ) == "altclip":
																			lowerCamelCase_					=       config_dict["""vision_config"""]

												if "model_type" in config_dict and hasattr(cls ,       """model_type""" ) and config_dict["model_type"] != cls.model_type:
																			logger.warning(
																			    F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
																			    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' )

												return cls.from_dict(__UpperCamelCase ,       **__UpperCamelCase )





class    __A(					UpperCAmelCase							):
					SCREAMING_SNAKE_CASE           =     '''altclip'''
					SCREAMING_SNAKE_CASE           =     True

					def __init__(						self       :  int ,       __UpperCamelCase       :  Any=None ,       __UpperCamelCase       :  Optional[int]=None ,       __UpperCamelCase       :  int=7_6_8 ,       __UpperCamelCase       :  List[Any]=2.6592 ,       **__UpperCamelCase       :  Union[str, Any] ):
												# If `_config_dict` exist, we use them for the backward compatibility.
												# We pop out these 2 attributes before calling `super().__init__` to avoid them being saved (which causes a lot
												# of confusion!).
												lowerCamelCase_					=       kwargs.pop("""text_config_dict""" ,       __UpperCamelCase )
												lowerCamelCase_					=       kwargs.pop("""vision_config_dict""" ,       __UpperCamelCase )

												super().__init__(**__UpperCamelCase )

												# Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
												# `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
												# cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
												if text_config_dict is not None:
																			if text_config is None:
																										lowerCamelCase_					=       {}

																			# This is the complete result when using `text_config_dict`.
																			lowerCamelCase_					=       AltCLIPTextConfig(**__UpperCamelCase ).to_dict()

																			# Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
																			for key, value in _text_config_dict.items():
																										if key in text_config and value != text_config[key] and key not in ["transformers_version"]:
																																	# If specified in `text_config_dict`
																																	if key in text_config_dict:
																																								lowerCamelCase_					=       (
																																								    F'''`{key}` is found in both `text_config_dict` and `text_config` but with different values. '''
																																								    F'''The value `text_config_dict["{key}"]` will be used instead.'''
																																								)
																																	# If inferred from default argument values (just to be super careful)
																																	else:
																																								lowerCamelCase_					=       (
																																								    F'''`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '''
																																								    F'''value `text_config["{key}"]` will be overriden.'''
																																								)
																																	logger.warning(__UpperCamelCase )

            # Update all values in `text_config` with the ones in `_text_config_dict`.
																			text_config.update(_text_config_dict )

												if vision_config_dict is not None:
																			if vision_config is None:
																										lowerCamelCase_					=       {}

																			# This is the complete result when using `vision_config_dict`.
																			lowerCamelCase_					=       AltCLIPVisionConfig(**__UpperCamelCase ).to_dict()
																			# convert keys to string instead of integer
																			if "id2label" in _vision_config_dict:
																										lowerCamelCase_					=       {
																										    str(__UpperCamelCase ): value for key, value in _vision_config_dict["""id2label"""].items()
																										}

																			# Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
																			for key, value in _vision_config_dict.items():
																										if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]:
																																	# If specified in `vision_config_dict`
																																	if key in vision_config_dict:
																																								lowerCamelCase_					=       (
																																								    F'''`{key}` is found in both `vision_config_dict` and `vision_config` but with different '''
																																								    F'''values. The value `vision_config_dict["{key}"]` will be used instead.'''
																																								)
																																	# If inferred from default argument values (just to be super careful)
																																	else:
																																								lowerCamelCase_					=       (
																																								    F'''`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '''
																																								    F'''The value `vision_config["{key}"]` will be overriden.'''
																																								)
																																	logger.warning(__UpperCamelCase )

            # Update all values in `vision_config` with the ones in `_vision_config_dict`.
																			vision_config.update(_vision_config_dict )

												if text_config is None:
																			lowerCamelCase_					=       {}
																			logger.info("""`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values.""" )

												if vision_config is None:
																			lowerCamelCase_					=       {}
																			logger.info("""`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values.""" )

												lowerCamelCase_					=       AltCLIPTextConfig(**__UpperCamelCase )
												lowerCamelCase_					=       AltCLIPVisionConfig(**__UpperCamelCase )

												lowerCamelCase_					=       projection_dim
												lowerCamelCase_					=       logit_scale_init_value
												lowerCamelCase_					=       1.0

					@classmethod
					def  lowercase__   (						cls       :  Optional[int] ,       __UpperCamelCase       :  AltCLIPTextConfig ,       __UpperCamelCase       :  AltCLIPVisionConfig ,       **__UpperCamelCase       :  Dict ):
												return cls(text_config=text_config.to_dict() ,       vision_config=vision_config.to_dict() ,       **__UpperCamelCase )



					def  lowercase__   (						self       :  Union[str, Any] ):
												lowerCamelCase_					=       copy.deepcopy(self.__dict__ )
												lowerCamelCase_					=       self.text_config.to_dict()
												lowerCamelCase_					=       self.vision_config.to_dict()
												lowerCamelCase_					=       self.__class__.model_type
												return output







import fire

from transformers import AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer



def   __lowerCAmelCase    (				UpperCAmelCase__					:   str      ,					UpperCAmelCase__					:   str      ,					**UpperCAmelCase__					:   List[str]							)	->     Tuple:
							lowerCamelCase_					=       AutoConfig.from_pretrained(UpperCAmelCase__      ,					**UpperCAmelCase__							)
							lowerCamelCase_					=       AutoModelForSeqaSeqLM.from_config(UpperCAmelCase__							)
							model.save_pretrained(UpperCAmelCase__							)
							AutoTokenizer.from_pretrained(UpperCAmelCase__							).save_pretrained(UpperCAmelCase__							)
							return model


if __name__ == "__main__":
		fire.Fire(save_randomly_initialized_version)





