





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 							__snake_case							(    lowerCAmelCase_	)				->						Optional[int]:
    if is_torch_version('''<''' ,							'''2.0.0'''	) or not hasattr(__snake_case ,							'''_dynamo'''	):
        return False
    return isinstance(__snake_case ,							torch._dynamo.eval_frame.OptimizedModule	)


def 							__snake_case							(    lowerCAmelCase_ ,							lowerCAmelCase_ = True	)				->						Union[str, Any]:
    SCREAMING_SNAKE_CASE__											=			(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

    SCREAMING_SNAKE_CASE__											=			is_compiled_module(__snake_case	)
    if is_compiled:
        SCREAMING_SNAKE_CASE__											=			model
        SCREAMING_SNAKE_CASE__											=			model._orig_mod

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

    while isinstance(__snake_case ,							__snake_case	):
        SCREAMING_SNAKE_CASE__											=			model.module

    if not keep_fpaa_wrapper:
        SCREAMING_SNAKE_CASE__											=			getattr(__snake_case ,							'''forward'''	)
        SCREAMING_SNAKE_CASE__											=			model.__dict__.pop('''_original_forward''' ,							__snake_case	)
        if original_forward is not None:
            while hasattr(__snake_case ,							'''__wrapped__'''	):
                SCREAMING_SNAKE_CASE__											=			forward.__wrapped__
                if forward == original_forward:
                    break
            SCREAMING_SNAKE_CASE__											=			forward
        if getattr(__snake_case ,							'''_converted_to_transformer_engine''' ,							__snake_case	):
            convert_model(__snake_case ,							to_transformer_engine=__snake_case	)

    if is_compiled:
        SCREAMING_SNAKE_CASE__											=			model
        SCREAMING_SNAKE_CASE__											=			compiled_model

    return model


def 							__snake_case							(    )				->						Optional[Any]:
    PartialState().wait_for_everyone()


def 							__snake_case							(    lowerCAmelCase_ ,							lowerCAmelCase_	)				->						List[str]:
    if PartialState().distributed_type == DistributedType.TPU:
        xm.save(__snake_case ,							__snake_case	)
    elif PartialState().local_process_index == 0:
        torch.save(__snake_case ,							__snake_case	)


@contextmanager
def 							__snake_case							(    **lowerCAmelCase_	)				->						Optional[int]:
    for key, value in kwargs.items():
        SCREAMING_SNAKE_CASE__											=			str(__snake_case	)

    yield

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


def 							__snake_case							(    lowerCAmelCase_	)				->						Optional[Any]:
    if not hasattr(__snake_case ,							'''__qualname__'''	) and not hasattr(__snake_case ,							'''__name__'''	):
        SCREAMING_SNAKE_CASE__											=			getattr(__snake_case ,							'''__class__''' ,							__snake_case	)
    if hasattr(__snake_case ,							'''__qualname__'''	):
        return obj.__qualname__
    if hasattr(__snake_case ,							'''__name__'''	):
        return obj.__name__
    return str(__snake_case	)


def 							__snake_case							(    lowerCAmelCase_ ,							lowerCAmelCase_	)				->						Dict:
    for key, value in source.items():
        if isinstance(__snake_case ,							__snake_case	):
            SCREAMING_SNAKE_CASE__											=			destination.setdefault(__snake_case ,							{}	)
            merge_dicts(__snake_case ,							__snake_case	)
        else:
            SCREAMING_SNAKE_CASE__											=			value

    return destination



def 							__snake_case							(    lowerCAmelCase_ = None	)				->						bool:
    if port is None:
        SCREAMING_SNAKE_CASE__											=			2_9_5_0_0
    with socket.socket(socket.AF_INET ,							socket.SOCK_STREAM	) as s:
        return s.connect_ex(('''localhost''', port)	) == 0





"""simple docstring"""






import copy
import os
from typing import Union

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


__snake_case				:  str =       logging.get_logger(__name__)

__snake_case				:  Union[str, Any] =       {
    'microsoft/git-base': 'https://huggingface.co/microsoft/git-base/resolve/main/config.json',
}



class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE        =      'git_vision_model'

	def __init__(	self:						Optional[int]			,     _SCREAMING_SNAKE_CASE:						List[Any]=768			,     _SCREAMING_SNAKE_CASE:						Optional[Any]=3072			,     _SCREAMING_SNAKE_CASE:						str=12			,     _SCREAMING_SNAKE_CASE:						Any=12			,     _SCREAMING_SNAKE_CASE:						Dict=3			,     _SCREAMING_SNAKE_CASE:						List[Any]=224			,     _SCREAMING_SNAKE_CASE:						List[str]=16			,     _SCREAMING_SNAKE_CASE:						str="quick_gelu"			,     _SCREAMING_SNAKE_CASE:						Any=1e-5			,     _SCREAMING_SNAKE_CASE:						List[str]=0.0			,     _SCREAMING_SNAKE_CASE:						Dict=0.02			,     **_SCREAMING_SNAKE_CASE:						str			,     )							->			Optional[Any]:

				"""simple docstring"""

				super().__init__(**_SCREAMING_SNAKE_CASE)

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







	@classmethod
	def 				_SCREAMING_SNAKE_CASE  (	cls:						Dict			,     _SCREAMING_SNAKE_CASE:						Union[str, os.PathLike]			,     **_SCREAMING_SNAKE_CASE:						Dict)							->			"PretrainedConfig":

				"""simple docstring"""

				cls._set_token_in_kwargs(_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase		,      __lowerCAmelCase :    List[Any]				=			cls.get_config_dict(_SCREAMING_SNAKE_CASE			,     **_SCREAMING_SNAKE_CASE)

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






class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE        =      'git'

	def __init__(	self:						Union[str, Any]			,     _SCREAMING_SNAKE_CASE:						Tuple=None			,     _SCREAMING_SNAKE_CASE:						Dict=3_0522			,     _SCREAMING_SNAKE_CASE:						Any=768			,     _SCREAMING_SNAKE_CASE:						str=6			,     _SCREAMING_SNAKE_CASE:						int=12			,     _SCREAMING_SNAKE_CASE:						Dict=3072			,     _SCREAMING_SNAKE_CASE:						Optional[Any]="gelu"			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]=0.1			,     _SCREAMING_SNAKE_CASE:						Any=0.1			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]=1024			,     _SCREAMING_SNAKE_CASE:						str=0.02			,     _SCREAMING_SNAKE_CASE:						Tuple=1e-12			,     _SCREAMING_SNAKE_CASE:						List[str]=0			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]="absolute"			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]=True			,     _SCREAMING_SNAKE_CASE:						Tuple=False			,     _SCREAMING_SNAKE_CASE:						Dict=101			,     _SCREAMING_SNAKE_CASE:						Tuple=102			,     _SCREAMING_SNAKE_CASE:						Any=None			,     **_SCREAMING_SNAKE_CASE:						List[Any]			,     )							->			List[Any]:

				"""simple docstring"""

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

				if vision_config is None:
							__lowerCAmelCase :    int				=			{}
							logger.info("vision_config is None. initializing the GitVisionConfig with default values.")

				__lowerCAmelCase :    List[str]				=			GitVisionConfig(**_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase :    List[Any]				=			vocab_size
				__lowerCAmelCase :    int				=			hidden_size
				__lowerCAmelCase :    Dict				=			num_hidden_layers
				__lowerCAmelCase :    Tuple				=			num_attention_heads
				__lowerCAmelCase :    Union[str, Any]				=			hidden_act
				__lowerCAmelCase :    Dict				=			intermediate_size
				__lowerCAmelCase :    List[str]				=			hidden_dropout_prob
				__lowerCAmelCase :    Union[str, Any]				=			attention_probs_dropout_prob
				__lowerCAmelCase :    Union[str, Any]				=			max_position_embeddings
				__lowerCAmelCase :    Optional[int]				=			initializer_range
				__lowerCAmelCase :    str				=			layer_norm_eps
				__lowerCAmelCase :    Optional[Any]				=			position_embedding_type
				__lowerCAmelCase :    str				=			use_cache
				__lowerCAmelCase :    str				=			tie_word_embeddings
				__lowerCAmelCase :    Optional[int]				=			num_image_with_embedding

				__lowerCAmelCase :    Optional[Any]				=			bos_token_id
				__lowerCAmelCase :    Any				=			eos_token_id







	def 				_SCREAMING_SNAKE_CASE  (	self:						Dict)							->			List[str]:

				"""simple docstring"""

				__lowerCAmelCase :    Optional[int]				=			copy.deepcopy(self.__dict__)
				__lowerCAmelCase :    List[Any]				=			self.vision_config.to_dict()
				__lowerCAmelCase :    Tuple				=			self.__class__.model_type
				return output