





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




def 		lowerCAmelCase_    (  __UpperCAmelCase:			List[Any]		,   __UpperCAmelCase:			bool = True      )      ->	Tuple:
    UpperCamelCase__							:				str          =      (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

    UpperCamelCase__							:				Optional[int]          =      is_compiled_module(__UpperCAmelCase      )
    if is_compiled:
        UpperCamelCase__							:				List[str]          =      model
        UpperCamelCase__							:				Dict          =      model._orig_mod

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

    while isinstance(__UpperCAmelCase		,   __UpperCAmelCase      ):
        UpperCamelCase__							:				str          =      model.module

    if not keep_fpaa_wrapper:
        UpperCamelCase__							:				Optional[int]          =      getattr(__UpperCAmelCase		,   '''forward'''      )
        UpperCamelCase__							:				Tuple          =      model.__dict__.pop('''_original_forward'''		,   __UpperCAmelCase      )
        if original_forward is not None:
            while hasattr(__UpperCAmelCase		,   '''__wrapped__'''      ):
                UpperCamelCase__							:				Any          =      forward.__wrapped__
                if forward == original_forward:
                    break
            UpperCamelCase__							:				Tuple          =      forward
        if getattr(__UpperCAmelCase		,   '''_converted_to_transformer_engine'''		,   __UpperCAmelCase      ):
            convert_model(__UpperCAmelCase		,   to_transformer_engine=__UpperCAmelCase      )

    if is_compiled:
        UpperCamelCase__							:				Union[str, Any]          =      model
        UpperCamelCase__							:				List[Any]          =      compiled_model

    return model




def 		lowerCAmelCase_    (  )      ->	str:
    PartialState().wait_for_everyone()




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




@contextmanager
def 		lowerCAmelCase_    (  **__UpperCAmelCase:			List[str]      )      ->	Union[str, Any]:
    for key, value in kwargs.items():
        UpperCamelCase__							:				List[str]          =      str(__UpperCAmelCase      )

    yield

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




def 		lowerCAmelCase_    (  __UpperCAmelCase:			int      )      ->	int:
    if not hasattr(__UpperCAmelCase		,   '''__qualname__'''      ) and not hasattr(__UpperCAmelCase		,   '''__name__'''      ):
        UpperCamelCase__							:				Tuple          =      getattr(__UpperCAmelCase		,   '''__class__'''		,   __UpperCAmelCase      )
    if hasattr(__UpperCAmelCase		,   '''__qualname__'''      ):
        return obj.__qualname__
    if hasattr(__UpperCAmelCase		,   '''__name__'''      ):
        return obj.__name__
    return str(__UpperCAmelCase      )




def 		lowerCAmelCase_    (  __UpperCAmelCase:			Optional[Any]		,   __UpperCAmelCase:			Any      )      ->	Optional[Any]:
    for key, value in source.items():
        if isinstance(__UpperCAmelCase		,   __UpperCAmelCase      ):
            UpperCamelCase__							:				List[str]          =      destination.setdefault(__UpperCAmelCase		,   {}      )
            merge_dicts(__UpperCAmelCase		,   __UpperCAmelCase      )
        else:
            UpperCamelCase__							:				Optional[Any]          =      value

    return destination





def 		lowerCAmelCase_    (  __UpperCAmelCase:			int = None      )      ->	bool:
    if port is None:
        UpperCamelCase__							:				Dict          =      2_9500
    with socket.socket(socket.AF_INET		,   socket.SOCK_STREAM      ) as s:
        return s.connect_ex(('''localhost''', port)      ) == 0








from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCAmelCase_     							= {
    'configuration_convbert': ['CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ConvBertConfig', 'ConvBertOnnxConfig'],
    'tokenization_convbert': ['ConvBertTokenizer'],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_     							= ['ConvBertTokenizerFast']

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_     							= [
      'CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
      'ConvBertForMaskedLM',
      'ConvBertForMultipleChoice',
      'ConvBertForQuestionAnswering',
      'ConvBertForSequenceClassification',
      'ConvBertForTokenClassification',
      'ConvBertLayer',
      'ConvBertModel',
      'ConvBertPreTrainedModel',
      'load_tf_weights_in_convbert',
  ]


try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_     							= [
      'TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
      'TFConvBertForMaskedLM',
      'TFConvBertForMultipleChoice',
      'TFConvBertForQuestionAnswering',
      'TFConvBertForSequenceClassification',
      'TFConvBertForTokenClassification',
      'TFConvBertLayer',
      'TFConvBertModel',
      'TFConvBertPreTrainedModel',
  ]


if TYPE_CHECKING:
  from .configuration_convbert import CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvBertConfig, ConvBertOnnxConfig
  from .tokenization_convbert import ConvBertTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_convbert_fast import ConvBertTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_convbert import (
        CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        ConvBertForMaskedLM,
        ConvBertForMultipleChoice,
        ConvBertForQuestionAnswering,
        ConvBertForSequenceClassification,
        ConvBertForTokenClassification,
        ConvBertLayer,
        ConvBertModel,
        ConvBertPreTrainedModel,
        load_tf_weights_in_convbert,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_convbert import (
        TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFConvBertForMaskedLM,
        TFConvBertForMultipleChoice,
        TFConvBertForQuestionAnswering,
        TFConvBertForSequenceClassification,
        TFConvBertForTokenClassification,
        TFConvBertLayer,
        TFConvBertModel,
        TFConvBertPreTrainedModel,
    )


else:
  import sys

  UpperCAmelCase_     							= _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

