
"""simple docstring"""




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     __magic_name__      ( __snake_case							:			Optional[Any]    )					->							Optional[Any]:
       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     __magic_name__      ( __snake_case							:			Tuple	,					__snake_case							:			bool = True    )					->							Tuple:
       lowercase       :							Dict									=				(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

       lowercase       :							Optional[int]									=				is_compiled_module(__snake_case    )
       if is_compiled:
              lowercase       :							Dict									=				model
              lowercase       :							List[Any]									=				model._orig_mod

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

       while isinstance(__snake_case	,					__snake_case    ):
              lowercase       :							Dict									=				model.module

       if not keep_fpaa_wrapper:
              lowercase       :							Union[str, Any]									=				getattr(__snake_case	,					"forward"    )
              lowercase       :							Optional[int]									=				model.__dict__.pop("_original_forward"	,					__snake_case    )
              if original_forward is not None:
                     while hasattr(__snake_case	,					"__wrapped__"    ):
                            lowercase       :							Any									=				forward.__wrapped__
                            if forward == original_forward:
                                   break
                     lowercase       :							Optional[int]									=				forward
              if getattr(__snake_case	,					"_converted_to_transformer_engine"	,					__snake_case    ):
                     convert_model(__snake_case	,					to_transformer_engine=__snake_case    )

       if is_compiled:
              lowercase       :							str									=				model
              lowercase       :							Any									=				compiled_model

       return model


def     __magic_name__      ( )					->							Dict:
       PartialState().wait_for_everyone()


def     __magic_name__      ( __snake_case							:			Tuple	,					__snake_case							:			str    )					->							Dict:
       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     __magic_name__      ( **__snake_case							:			Tuple    )					->							Optional[int]:
       for key, value in kwargs.items():
              lowercase       :							Any									=				str(__snake_case    )

       yield

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


def     __magic_name__      ( __snake_case							:			Tuple    )					->							Tuple:
       if not hasattr(__snake_case	,					"__qualname__"    ) and not hasattr(__snake_case	,					"__name__"    ):
              lowercase       :							Dict									=				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     __magic_name__      ( __snake_case							:			Union[str, Any]	,					__snake_case							:			List[str]    )					->							Tuple:
       for key, value in source.items():
              if isinstance(__snake_case	,					__snake_case    ):
                     lowercase       :							Dict									=				destination.setdefault(__snake_case	,					{}    )
                     merge_dicts(__snake_case	,					__snake_case    )
              else:
                     lowercase       :							Optional[Any]									=				value

       return destination


def     __magic_name__      ( __snake_case							:			int = None    )					->							bool:
       if port is None:
              lowercase       :							int									=				2_9500
       with socket.socket(socket.AF_INET	,					socket.SOCK_STREAM    ) as s:
              return s.connect_ex(("localhost", port)    ) == 0




"""simple docstring"""




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


_A     :			Optional[Any]  						=			{
    """configuration_bridgetower""": [
        """BRIDGETOWER_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """BridgeTowerConfig""",
        """BridgeTowerTextConfig""",
        """BridgeTowerVisionConfig""",
    ],
    """processing_bridgetower""": ["""BridgeTowerProcessor"""],
}

try:
   if not is_vision_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _A     :			Any  						=			["""BridgeTowerImageProcessor"""]

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _A     :			List[Any]  						=			[
       """BRIDGETOWER_PRETRAINED_MODEL_ARCHIVE_LIST""",
       """BridgeTowerForContrastiveLearning""",
       """BridgeTowerForImageAndTextRetrieval""",
       """BridgeTowerForMaskedLM""",
       """BridgeTowerModel""",
       """BridgeTowerPreTrainedModel""",
   ]


if TYPE_CHECKING:
   from .configuration_bridgetower import (
       BRIDGETOWER_PRETRAINED_CONFIG_ARCHIVE_MAP,
       BridgeTowerConfig,
       BridgeTowerTextConfig,
       BridgeTowerVisionConfig,
   )
   from .processing_bridgetower import BridgeTowerProcessor

   try:
      if not is_vision_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .image_processing_bridgetower import BridgeTowerImageProcessor

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_bridgetower import (
          BRIDGETOWER_PRETRAINED_MODEL_ARCHIVE_LIST,
          BridgeTowerForContrastiveLearning,
          BridgeTowerForImageAndTextRetrieval,
          BridgeTowerForMaskedLM,
          BridgeTowerModel,
          BridgeTowerPreTrainedModel,
      )


else:
   import sys

   _A     :			Any  						=			_LazyModule(__name__, globals()["""__file__"""], _import_structure)


