





"""simple docstring"""




import copy
import os
from typing import Union

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


lowercase				:	List[Any]      						=  logging.get_logger(__name__)

lowercase				:	Union[str, Any]      						=  {
    """Salesforce/blip-vqa-base""": """https://huggingface.co/Salesforce/blip-vqa-base/resolve/main/config.json""",
    """Salesforce/blip-vqa-capfit-large""": (
        """https://huggingface.co/Salesforce/blip-vqa-base-capfit/resolve/main/config.json"""
    ),
    """Salesforce/blip-image-captioning-base""": (
        """https://huggingface.co/Salesforce/blip-image-captioning-base/resolve/main/config.json"""
    ),
    """Salesforce/blip-image-captioning-large""": (
        """https://huggingface.co/Salesforce/blip-image-captioning-large/resolve/main/config.json"""
    ),
    """Salesforce/blip-itm-base-coco""": """https://huggingface.co/Salesforce/blip-itm-base-coco/resolve/main/config.json""",
    """Salesforce/blip-itm-large-coco""": """https://huggingface.co/Salesforce/blip-itm-large-coco/resolve/main/config.json""",
    """Salesforce/blip-itm-base-flikr""": """https://huggingface.co/Salesforce/blip-itm-base-flikr/resolve/main/config.json""",
    """Salesforce/blip-itm-large-flikr""": (
        """https://huggingface.co/Salesforce/blip-itm-large-flikr/resolve/main/config.json"""
    ),
}





class 			A__   (							__UpperCAmelCase     ):




   """simple docstring"""



   __A       :    Optional[Any]	  =  '''blip_text_model'''

   def __init__( self ,  lowercase=3_0524 ,  lowercase=768 ,  lowercase=768 ,  lowercase=3072 ,  lowercase=768 ,  lowercase=12 ,  lowercase=8 ,  lowercase=512 ,  lowercase="gelu" ,  lowercase=1e-12 ,  lowercase=0.0 ,  lowercase=0.0 ,  lowercase=0.02 ,  lowercase=3_0522 ,  lowercase=2 ,  lowercase=0 ,  lowercase=102 ,  lowercase=True ,  lowercase=True ,  **lowercase ,  )      ->						Any:







          '''simple docstring'''

          super().__init__(
              pad_token_id=lowercase ,  bos_token_id=lowercase ,  eos_token_id=lowercase ,  sep_token_id=lowercase ,  **lowercase ,  )

          a__						:     Any			=    vocab_size
          a__						:     Tuple			=    hidden_size
          a__						:     Optional[int]			=    encoder_hidden_size
          a__						:     Tuple			=    intermediate_size
          a__						:     Union[str, Any]			=    projection_dim
          a__						:     str			=    hidden_dropout_prob
          a__						:     List[Any]			=    num_hidden_layers
          a__						:     Optional[int]			=    num_attention_heads
          a__						:     Union[str, Any]			=    max_position_embeddings
          a__						:     Any			=    layer_norm_eps
          a__						:     str			=    hidden_act
          a__						:     Any			=    initializer_range
          a__						:     Any			=    attention_probs_dropout_prob
          a__						:     Optional[int]			=    is_decoder
          a__						:     Any			=    use_cache

   @classmethod
   def 							__lowercase		( cls ,  lowercase ,  **lowercase)      ->						"PretrainedConfig":







          '''simple docstring'''

          cls._set_token_in_kwargs(lowercase)

          a__						:     Dict			=    cls.get_config_dict(lowercase ,  **lowercase)

          # get the text config dict if we are loading from BlipConfig
          if config_dict.get('model_type') == "blip":
                 a__						:     Any			=    config_dict['text_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(lowercase ,  **lowercase)





class 			A__   (							__UpperCAmelCase     ):




   """simple docstring"""



   __A       :    Optional[Any]	  =  '''blip_vision_model'''

   def __init__( self ,  lowercase=768 ,  lowercase=3072 ,  lowercase=512 ,  lowercase=12 ,  lowercase=12 ,  lowercase=384 ,  lowercase=16 ,  lowercase="gelu" ,  lowercase=1e-5 ,  lowercase=0.0 ,  lowercase=1e-10 ,  **lowercase ,  )      ->						List[Any]:







          '''simple docstring'''

          super().__init__(**lowercase)

          a__						:     List[str]			=    hidden_size
          a__						:     Tuple			=    intermediate_size
          a__						:     List[Any]			=    projection_dim
          a__						:     Optional[int]			=    num_hidden_layers
          a__						:     int			=    num_attention_heads
          a__						:     int			=    patch_size
          a__						:     Optional[int]			=    image_size
          a__						:     Any			=    initializer_range
          a__						:     Union[str, Any]			=    attention_dropout
          a__						:     Optional[Any]			=    layer_norm_eps
          a__						:     Tuple			=    hidden_act

   @classmethod
   def 							__lowercase		( cls ,  lowercase ,  **lowercase)      ->						"PretrainedConfig":







          '''simple docstring'''

          cls._set_token_in_kwargs(lowercase)

          a__						:     Optional[int]			=    cls.get_config_dict(lowercase ,  **lowercase)

          # get the vision config dict if we are loading from BlipConfig
          if config_dict.get('model_type') == "blip":
                 a__						:     int			=    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(lowercase ,  **lowercase)





class 			A__   (							__UpperCAmelCase     ):




   """simple docstring"""



   __A       :    Any	  =  '''blip'''
   __A       :    Union[str, Any]	  =  True

   def __init__( self ,  lowercase=None ,  lowercase=None ,  lowercase=512 ,  lowercase=2.65_92 ,  lowercase=256 ,  **lowercase ,  )      ->						Any:







          '''simple docstring'''

          super().__init__(**lowercase)

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

          if vision_config is None:
                 a__						:     List[str]			=    {}
                 logger.info('`vision_config` is `None`. Initializing the `BlipVisionConfig` with default values.')

          a__						:     List[str]			=    BlipTextConfig(**lowercase)
          a__						:     List[str]			=    BlipVisionConfig(**lowercase)

          a__						:     Any			=    self.vision_config.hidden_size

          a__						:     Tuple			=    projection_dim
          a__						:     Union[str, Any]			=    logit_scale_init_value
          a__						:     Tuple			=    1.0
          a__						:     Dict			=    0.02
          a__						:     Dict			=    image_text_hidden_size

   @classmethod
   def 							__lowercase		( cls ,  lowercase ,  lowercase ,  **lowercase)      ->						int:







          '''simple docstring'''

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

   def 							__lowercase		( self)      ->						List[Any]:







          '''simple docstring'''

          a__						:     Optional[int]			=    copy.deepcopy(self.__dict__)
          a__						:     Optional[Any]			=    self.text_config.to_dict()
          a__						:     Dict			=    self.vision_config.to_dict()
          a__						:     str			=    self.__class__.model_type
          return output









import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
    from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
    import torch_xla.core.xla_model as xm



def A_     (		A__    )  ->		Optional[int]:
       if is_torch_version('<'		,				'2.0.0'    ) or not hasattr(A__		,				'_dynamo'    ):
              return False
       return isinstance(A__		,				torch._dynamo.eval_frame.OptimizedModule    )



def A_     (		A__		,				A__ = True    )  ->		int:
       a__						:     Optional[Any]			=    (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

       a__						:     Union[str, Any]			=    is_compiled_module(A__    )
       if is_compiled:
              a__						:     List[str]			=    model
              a__						:     Dict			=    model._orig_mod

       if is_deepspeed_available():
              options += (DeepSpeedEngine,)

       while isinstance(A__		,				A__    ):
              a__						:     str			=    model.module

       if not keep_fpaa_wrapper:
              a__						:     Union[str, Any]			=    getattr(A__		,				'forward'    )
              a__						:     List[Any]			=    model.__dict__.pop('_original_forward'		,				A__    )
              if original_forward is not None:
                     while hasattr(A__		,				'__wrapped__'    ):
                            a__						:     int			=    forward.__wrapped__
                            if forward == original_forward:
                                   break
                     a__						:     List[Any]			=    forward
              if getattr(A__		,				'_converted_to_transformer_engine'		,				A__    ):
                     convert_model(A__		,				to_transformer_engine=A__    )

       if is_compiled:
              a__						:     List[str]			=    model
              a__						:     Any			=    compiled_model

       return model



def A_     (		)  ->		int:
       PartialState().wait_for_everyone()



def A_     (		A__		,				A__    )  ->		Dict:
       if PartialState().distributed_type == DistributedType.TPU:
              xm.save(A__		,				A__    )
       elif PartialState().local_process_index == 0:
              torch.save(A__		,				A__    )



@contextmanager
def A_     (		**A__    )  ->		Any:
       for key, value in kwargs.items():
              a__						:     Optional[int]			=    str(A__    )

       yield

       for key in kwargs:
              if key.upper() in os.environ:
                     del os.environ[key.upper()]



def A_     (		A__    )  ->		List[str]:
       if not hasattr(A__		,				'__qualname__'    ) and not hasattr(A__		,				'__name__'    ):
              a__						:     Dict			=    getattr(A__		,				'__class__'		,				A__    )
       if hasattr(A__		,				'__qualname__'    ):
              return obj.__qualname__
       if hasattr(A__		,				'__name__'    ):
              return obj.__name__
       return str(A__    )



def A_     (		A__		,				A__    )  ->		Dict:
       for key, value in source.items():
              if isinstance(A__		,				A__    ):
                     a__						:     Optional[Any]			=    destination.setdefault(A__		,				{}    )
                     merge_dicts(A__		,				A__    )
              else:
                     a__						:     Optional[int]			=    value

       return destination



def A_     (		A__ = None    )  ->		bool:
       if port is None:
              a__						:     List[Any]			=    2_9500
       with socket.socket(socket.AF_INET		,				socket.SOCK_STREAM    ) as s:
              return s.connect_ex(('localhost', port)    ) == 0


