


import copy
import os
from typing import Union

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


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

lowerCAmelCase__  :str             =							{
    '''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      ):
		_a     :  int           =     'altclip_text_model'
		def __init__(      self      ,   _SCREAMING_SNAKE_CASE=250002      ,   _SCREAMING_SNAKE_CASE=1024      ,   _SCREAMING_SNAKE_CASE=24      ,   _SCREAMING_SNAKE_CASE=16      ,   _SCREAMING_SNAKE_CASE=4096      ,   _SCREAMING_SNAKE_CASE="gelu"      ,   _SCREAMING_SNAKE_CASE=0.1      ,   _SCREAMING_SNAKE_CASE=0.1      ,   _SCREAMING_SNAKE_CASE=514      ,   _SCREAMING_SNAKE_CASE=1      ,   _SCREAMING_SNAKE_CASE=0.02      ,   _SCREAMING_SNAKE_CASE=0.02      ,   _SCREAMING_SNAKE_CASE=1e-0_5      ,   _SCREAMING_SNAKE_CASE=1      ,   _SCREAMING_SNAKE_CASE=0      ,   _SCREAMING_SNAKE_CASE=2      ,   _SCREAMING_SNAKE_CASE="absolute"      ,   _SCREAMING_SNAKE_CASE=True      ,   _SCREAMING_SNAKE_CASE=768      ,   **_SCREAMING_SNAKE_CASE      ,   )			->	Optional[Any]:




									"""simple docstring"""





									super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE      ,   bos_token_id=_SCREAMING_SNAKE_CASE      ,   eos_token_id=_SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)

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


class 		__a				(    UpperCAmelCase      ):
		_a     :  Optional[Any]           =     'altclip_vision_model'
		def __init__(      self      ,   _SCREAMING_SNAKE_CASE=768      ,   _SCREAMING_SNAKE_CASE=3072      ,   _SCREAMING_SNAKE_CASE=512      ,   _SCREAMING_SNAKE_CASE=12      ,   _SCREAMING_SNAKE_CASE=12      ,   _SCREAMING_SNAKE_CASE=3      ,   _SCREAMING_SNAKE_CASE=224      ,   _SCREAMING_SNAKE_CASE=32      ,   _SCREAMING_SNAKE_CASE="quick_gelu"      ,   _SCREAMING_SNAKE_CASE=1e-5      ,   _SCREAMING_SNAKE_CASE=0.0      ,   _SCREAMING_SNAKE_CASE=0.02      ,   _SCREAMING_SNAKE_CASE=1.0      ,   **_SCREAMING_SNAKE_CASE      ,   )			->	Optional[Any]:




									"""simple docstring"""





									super().__init__(**_SCREAMING_SNAKE_CASE						)

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






		@classmethod
		def      UpperCAmelCase__    (      cls      ,   _SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)			->	"PretrainedConfig":




									"""simple docstring"""





									cls._set_token_in_kwargs(_SCREAMING_SNAKE_CASE						)

									_UpperCAmelCase		,	_UpperCAmelCase					=      cls.get_config_dict(_SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)

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


class 		__a				(    UpperCAmelCase      ):
		_a     :  int           =     'altclip'
		_a     :  Optional[int]           =     True
		def __init__(      self      ,   _SCREAMING_SNAKE_CASE=None      ,   _SCREAMING_SNAKE_CASE=None      ,   _SCREAMING_SNAKE_CASE=768      ,   _SCREAMING_SNAKE_CASE=2.6592      ,   **_SCREAMING_SNAKE_CASE						)			->	Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      kwargs.pop('text_config_dict'      ,   _SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      kwargs.pop('vision_config_dict'      ,   _SCREAMING_SNAKE_CASE						)

									super().__init__(**_SCREAMING_SNAKE_CASE						)

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

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

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

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

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

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

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

									_UpperCAmelCase					=      AltCLIPTextConfig(**_SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      AltCLIPVisionConfig(**_SCREAMING_SNAKE_CASE						)

									_UpperCAmelCase					=      projection_dim
									_UpperCAmelCase					=      logit_scale_init_value
									_UpperCAmelCase					=      1.0
		@classmethod
		def      UpperCAmelCase__    (      cls      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   **_SCREAMING_SNAKE_CASE						)			->	Any:




									"""simple docstring"""





									return cls(text_config=text_config.to_dict()      ,   vision_config=vision_config.to_dict()      ,   **_SCREAMING_SNAKE_CASE						)






		def      UpperCAmelCase__    (      self						)			->	Dict:




									"""simple docstring"""





									_UpperCAmelCase					=      copy.deepcopy(self.__dict__						)
									_UpperCAmelCase					=      self.text_config.to_dict()
									_UpperCAmelCase					=      self.vision_config.to_dict()
									_UpperCAmelCase					=      self.__class__.model_type
									return output




from importlib import import_module

from .logging import get_logger


lowerCAmelCase__  :Optional[Any]             =							get_logger(__name__)

class 		__a				:
		def __init__(      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE=None						)			->	List[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      attrs or []
									if module is not None:
																for key in module.__dict__:
																							if key in attrs or not key.startswith('__'						):
																														setattr(self      ,   _SCREAMING_SNAKE_CASE      ,   getattr(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)						)
									_UpperCAmelCase					=      module._original_module if isinstance(_SCREAMING_SNAKE_CASE      ,   _PatchedModuleObj						) else module



class 		__a				:
		_a     :  Any           =     []
		def __init__(      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE=None						)			->	Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      obj
									_UpperCAmelCase					=      target
									_UpperCAmelCase					=      new
									_UpperCAmelCase					=      target.split('.'						)[0]
									_UpperCAmelCase					=      {}
									_UpperCAmelCase					=      attrs or []
		def __enter__(      self						)			->	int:




									"""simple docstring"""





									*_UpperCAmelCase		,	_UpperCAmelCase					=      self.target.split('.'						)

									# Patch modules:
									# it's used to patch attributes of submodules like "os.path.join";
									# in this case we need to patch "os" and "os.path"

									for i in range(len(_SCREAMING_SNAKE_CASE						)						):
																try:
																							_UpperCAmelCase					=      import_module('.'.join(submodules[: i + 1]						)						)
																except ModuleNotFoundError:
																							continue
																# We iterate over all the globals in self.obj in case we find "os" or "os.path"
																for attr in self.obj.__dir__():
																							_UpperCAmelCase					=      getattr(self.obj      ,   _SCREAMING_SNAKE_CASE						)
																							# We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
																							# This allows to patch renamed modules like "from os import path as ospath".
																							if obj_attr is submodule or (
																							    (isinstance(_SCREAMING_SNAKE_CASE      ,   _PatchedModuleObj						) and obj_attr._original_module is submodule)
																							):
																														_UpperCAmelCase					=      obj_attr
																														# patch at top level
																														setattr(self.obj      ,   _SCREAMING_SNAKE_CASE      ,   _PatchedModuleObj(_SCREAMING_SNAKE_CASE      ,   attrs=self.attrs						)						)
																														_UpperCAmelCase					=      getattr(self.obj      ,   _SCREAMING_SNAKE_CASE						)
																														# construct lower levels patches
																														for key in submodules[i + 1 :]:
																																					setattr(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _PatchedModuleObj(getattr(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)      ,   attrs=self.attrs						)						)
																																					_UpperCAmelCase					=      getattr(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)
																														# finally set the target attribute
																														setattr(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   self.new						)

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

									if submodules:  # if it's an attribute of a submodule like "os.path.join"
																try:
																							_UpperCAmelCase					=      getattr(import_module('.'.join(_SCREAMING_SNAKE_CASE						)						)      ,   _SCREAMING_SNAKE_CASE						)
																except (AttributeError, ModuleNotFoundError):
																							return
																# We iterate over all the globals in self.obj in case we find "os.path.join"
																for attr in self.obj.__dir__():
																							# We don't check for the name of the global, but rather if its value *is* "os.path.join".
																							# This allows to patch renamed attributes like "from os.path import join as pjoin".
																							if getattr(self.obj      ,   _SCREAMING_SNAKE_CASE						) is attr_value:
																														_UpperCAmelCase					=      getattr(self.obj      ,   _SCREAMING_SNAKE_CASE						)
																														setattr(self.obj      ,   _SCREAMING_SNAKE_CASE      ,   self.new						)
									elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
																_UpperCAmelCase					=      globals()['__builtins__'][target_attr]
																setattr(self.obj      ,   _SCREAMING_SNAKE_CASE      ,   self.new						)
									else:
																raise RuntimeError(f'''Tried to patch attribute {target_attr} instead of a submodule.'''						)
		def __exit__(      self      ,   *_SCREAMING_SNAKE_CASE						)			->	Union[str, Any]:




									"""simple docstring"""





									for attr in list(self.original						):
																setattr(self.obj      ,   _SCREAMING_SNAKE_CASE      ,   self.original.pop(_SCREAMING_SNAKE_CASE						)						)
		def      UpperCAmelCase__    (      self						)			->	Optional[int]:




									"""simple docstring"""





									self.__enter__()
									self._active_patches.append(self						)






		def      UpperCAmelCase__    (      self						)			->	str:




									"""simple docstring"""





									try:
																self._active_patches.remove(self						)
									except ValueError:
																# If the patch hasn't been started this will fail
																return None

									return self.__exit__()
