

import copy
import os
from typing import Union

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


a		=logging.get_logger(__name__)

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






class 	A_		(	lowerCAmelCase_    ):
							_UpperCAmelCase   :  str										=				'altclip_text_model'
							def __init__(		self		:      str       ,SCREAMING_SNAKE_CASE__		:      int=2_5_0_0_0_2       ,SCREAMING_SNAKE_CASE__		:      int=1_0_2_4       ,SCREAMING_SNAKE_CASE__		:      str=2_4       ,SCREAMING_SNAKE_CASE__		:      Optional[Any]=1_6       ,SCREAMING_SNAKE_CASE__		:      Optional[int]=4_0_9_6       ,SCREAMING_SNAKE_CASE__		:      Tuple="gelu"       ,SCREAMING_SNAKE_CASE__		:      Union[str, Any]=0.1       ,SCREAMING_SNAKE_CASE__		:      Tuple=0.1       ,SCREAMING_SNAKE_CASE__		:      str=5_1_4       ,SCREAMING_SNAKE_CASE__		:      Dict=1       ,SCREAMING_SNAKE_CASE__		:      Tuple=0.02       ,SCREAMING_SNAKE_CASE__		:      Dict=0.02       ,SCREAMING_SNAKE_CASE__		:      Optional[int]=1E-05       ,SCREAMING_SNAKE_CASE__		:      Dict=1       ,SCREAMING_SNAKE_CASE__		:      Union[str, Any]=0       ,SCREAMING_SNAKE_CASE__		:      Tuple=2       ,SCREAMING_SNAKE_CASE__		:      int="absolute"       ,SCREAMING_SNAKE_CASE__		:      List[str]=True       ,SCREAMING_SNAKE_CASE__		:      Union[str, Any]=7_6_8       ,**SCREAMING_SNAKE_CASE__		:      Optional[int]       ,):
								super().__init__(pad_token_id=__lowerCAmelCase       ,bos_token_id=__lowerCAmelCase       ,eos_token_id=__lowerCAmelCase       ,**__lowerCAmelCase)

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






class 	A_		(	lowerCAmelCase_    ):
							_UpperCAmelCase   :  List[Any]										=				'altclip_vision_model'
							def __init__(		self		:      Any       ,SCREAMING_SNAKE_CASE__		:      List[str]=7_6_8       ,SCREAMING_SNAKE_CASE__		:      Optional[int]=3_0_7_2       ,SCREAMING_SNAKE_CASE__		:      Optional[Any]=5_1_2       ,SCREAMING_SNAKE_CASE__		:      int=1_2       ,SCREAMING_SNAKE_CASE__		:      Any=1_2       ,SCREAMING_SNAKE_CASE__		:      Any=3       ,SCREAMING_SNAKE_CASE__		:      List[Any]=2_2_4       ,SCREAMING_SNAKE_CASE__		:      List[str]=3_2       ,SCREAMING_SNAKE_CASE__		:      List[str]="quick_gelu"       ,SCREAMING_SNAKE_CASE__		:      str=1E-5       ,SCREAMING_SNAKE_CASE__		:      str=0.0       ,SCREAMING_SNAKE_CASE__		:      Dict=0.02       ,SCREAMING_SNAKE_CASE__		:      Optional[int]=1.0       ,**SCREAMING_SNAKE_CASE__		:      Tuple       ,):
								super().__init__(**__lowerCAmelCase)

								__lowerCamelCase    :    int 	=			hidden_size
								__lowerCamelCase    :    List[Any] 	=			intermediate_size
								__lowerCamelCase    :    Optional[int] 	=			projection_dim
								__lowerCamelCase    :    Optional[int] 	=			num_hidden_layers
								__lowerCamelCase    :    List[str] 	=			num_attention_heads
								__lowerCamelCase    :    List[str] 	=			num_channels
								__lowerCamelCase    :    List[str] 	=			patch_size
								__lowerCamelCase    :    Union[str, Any] 	=			image_size
								__lowerCamelCase    :    Dict 	=			initializer_range
								__lowerCamelCase    :    Optional[Any] 	=			initializer_factor
								__lowerCamelCase    :    int 	=			attention_dropout
								__lowerCamelCase    :    Optional[Any] 	=			layer_norm_eps
								__lowerCamelCase    :    Optional[Any] 	=			hidden_act


							@classmethod
							def    lowerCAmelCase					(		cls		:      Optional[int]       ,SCREAMING_SNAKE_CASE__		:      Union[str, os.PathLike]       ,**SCREAMING_SNAKE_CASE__		:      str):
								cls._set_token_in_kwargs(__lowerCAmelCase)

								__lowerCamelCase	,			__lowerCamelCase    :    Tuple 	=			cls.get_config_dict(__lowerCAmelCase       ,**__lowerCAmelCase)

								# get the vision config dict if we are loading from AltCLIPConfig
								if config_dict.get('model_type') == "altclip":
									__lowerCamelCase    :    str 	=			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(__lowerCAmelCase       ,**__lowerCAmelCase)






class 	A_		(	lowerCAmelCase_    ):
							_UpperCAmelCase   :  Dict										=				'altclip'
							_UpperCAmelCase   :  Union[str, Any]										=				True
							def __init__(		self		:      Tuple       ,SCREAMING_SNAKE_CASE__		:      int=None       ,SCREAMING_SNAKE_CASE__		:      Tuple=None       ,SCREAMING_SNAKE_CASE__		:      List[Any]=7_6_8       ,SCREAMING_SNAKE_CASE__		:      Dict=2.6592       ,**SCREAMING_SNAKE_CASE__		:      List[str]):
								# 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    :    Any 	=			kwargs.pop('text_config_dict'       ,__lowerCAmelCase)
								__lowerCamelCase    :    str 	=			kwargs.pop('vision_config_dict'       ,__lowerCAmelCase)

								super().__init__(**__lowerCAmelCase)

								# 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    :    str 	=			{}

									# This is the complete result when using `text_config_dict`.
									__lowerCamelCase    :    Any 	=			AltCLIPTextConfig(**__lowerCAmelCase).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    :    Any 	=			(
												    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    :    List[str] 	=			(
												    F"`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The "
												    F"value `text_config[\"{key}\"]` will be overriden."
												)
											logger.warning(__lowerCAmelCase)

											# 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    :    List[Any] 	=			{}

									# This is the complete result when using `vision_config_dict`.
									__lowerCamelCase    :    Optional[int] 	=			AltCLIPVisionConfig(**__lowerCAmelCase).to_dict()
									# convert keys to string instead of integer
									if "id2label" in _vision_config_dict:
										__lowerCamelCase    :    Any 	=			{
										    str(__lowerCAmelCase): 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    :    List[Any] 	=			(
												    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    :    str 	=			(
												    F"`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. "
												    F"The value `vision_config[\"{key}\"]` will be overriden."
												)
											logger.warning(__lowerCAmelCase)

											# 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    :    int 	=			{}
									logger.info('`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values.')

								if vision_config is None:
									__lowerCamelCase    :    Optional[int] 	=			{}
									logger.info('`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values.')

								__lowerCamelCase    :    Any 	=			AltCLIPTextConfig(**__lowerCAmelCase)
								__lowerCamelCase    :    Dict 	=			AltCLIPVisionConfig(**__lowerCAmelCase)

								__lowerCamelCase    :    str 	=			projection_dim
								__lowerCamelCase    :    List[Any] 	=			logit_scale_init_value
								__lowerCamelCase    :    Tuple 	=			1.0
							@classmethod
							def    lowerCAmelCase					(		cls		:      List[str]       ,SCREAMING_SNAKE_CASE__		:      AltCLIPTextConfig       ,SCREAMING_SNAKE_CASE__		:      AltCLIPVisionConfig       ,**SCREAMING_SNAKE_CASE__		:      List[str]):
								return cls(text_config=text_config.to_dict()       ,vision_config=vision_config.to_dict()       ,**__lowerCAmelCase)


							def    lowerCAmelCase					(		self		:      Union[str, Any]):
								__lowerCamelCase    :    Optional[int] 	=			copy.deepcopy(self.__dict__)
								__lowerCamelCase    :    Any 	=			self.text_config.to_dict()
								__lowerCamelCase    :    Optional[Any] 	=			self.vision_config.to_dict()
								__lowerCamelCase    :    str 	=			self.__class__.model_type
								return output







# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

from ...utils.dataclasses import (
    ComputeEnvironment,
    DistributedType,
    DynamoBackend,
    PrecisionType,
    SageMakerDistributedType,
)
from ..menu import BulletMenu


a		=[
    """EAGER""",
    """AOT_EAGER""",
    """INDUCTOR""",
    """NVFUSER""",
    """AOT_NVFUSER""",
    """AOT_CUDAGRAPHS""",
    """OFI""",
    """FX2TRT""",
    """ONNXRT""",
    """IPEX""",
]






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__     ,			lowerCamelCase__=None     ,			lowerCamelCase__=None     ,			lowerCamelCase__=None      )  ->					Optional[int]:
	__lowerCamelCase    :    int 	=			True
	while ask_again:
		__lowerCamelCase    :    Dict 	=			input(lowerCamelCase__      )
		try:
			if default is not None and len(lowerCamelCase__      ) == 0:
				return default
			return convert_value(lowerCamelCase__      ) if convert_value is not None else result
		except Exception:
			if error_message is not None:
				print(lowerCamelCase__      )






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__     ,			lowerCamelCase__=[]     ,			lowerCamelCase__=None     ,			lowerCamelCase__=0      )  ->					str:
	__lowerCamelCase    :    Union[str, Any] 	=			BulletMenu(lowerCamelCase__     ,			lowerCamelCase__      )
	__lowerCamelCase    :    Tuple 	=			menu.run(default_choice=lowerCamelCase__      )
	return convert_value(lowerCamelCase__      ) if convert_value is not None else result






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__      )  ->					Dict:
	__lowerCamelCase    :    List[str] 	=			int(lowerCamelCase__      )
	return ComputeEnvironment(['LOCAL_MACHINE', 'AMAZON_SAGEMAKER'][value]      )






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__      )  ->					Optional[int]:
	__lowerCamelCase    :    Union[str, Any] 	=			int(lowerCamelCase__      )
	return DistributedType(['NO', 'MULTI_CPU', 'MULTI_XPU', 'MULTI_GPU', 'MULTI_NPU', 'TPU'][value]      )






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__      )  ->					Union[str, Any]:
	__lowerCamelCase    :    Optional[Any] 	=			int(lowerCamelCase__      )
	return DynamoBackend(DYNAMO_BACKENDS[value]      ).value






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__      )  ->					int:
	__lowerCamelCase    :    Union[str, Any] 	=			int(lowerCamelCase__      )
	return PrecisionType(['no', 'fp16', 'bf16', 'fp8'][value]      )






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__      )  ->					str:
	__lowerCamelCase    :    Optional[Any] 	=			int(lowerCamelCase__      )
	return SageMakerDistributedType(['NO', 'DATA_PARALLEL', 'MODEL_PARALLEL'][value]      )






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__      )  ->					Optional[Any]:
	return {"yes": True, "no": False}[value.lower()]






class 	A_		(	argparse.RawDescriptionHelpFormatter    ):
							def    lowerCAmelCase					(		self		:      Optional[int]       ,SCREAMING_SNAKE_CASE__		:      str       ,SCREAMING_SNAKE_CASE__		:      int       ,SCREAMING_SNAKE_CASE__		:      Union[str, Any]       ,SCREAMING_SNAKE_CASE__		:      str):
								__lowerCamelCase    :    int 	=			super()._format_usage(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)
								__lowerCamelCase    :    Union[str, Any] 	=			usage.replace('<command> [<args>] '       ,'')
								return usage




