


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__							(   SCREAMING_SNAKE_CASE__)       ->   Optional[Any]:





  if is_torch_version("""<""" , """2.0.0""") or not hasattr(lowerCAmelCase__ , """_dynamo"""):
    return False
  return isinstance(lowerCAmelCase__ , torch._dynamo.eval_frame.OptimizedModule)







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

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

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

  while isinstance(lowerCAmelCase__ , lowerCAmelCase__):
    __snake_case:						str			  =						model.module

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

  if is_compiled:
    __snake_case:						Optional[Any]			  =						model
    __snake_case:						Tuple			  =						compiled_model

  return model







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







def       A__							(   SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__)       ->   Union[str, Any]:





  if PartialState().distributed_type == DistributedType.TPU:
    xm.save(lowerCAmelCase__ , lowerCAmelCase__)
  elif PartialState().local_process_index == 0:
    torch.save(lowerCAmelCase__ , lowerCAmelCase__)







@contextmanager
def       A__							(   **SCREAMING_SNAKE_CASE__)       ->   Any:





  for key, value in kwargs.items():
    __snake_case:						Any			  =						str(lowerCAmelCase__)

  yield

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







def       A__							(   SCREAMING_SNAKE_CASE__)       ->   str:





  if not hasattr(lowerCAmelCase__ , """__qualname__""") and not hasattr(lowerCAmelCase__ , """__name__"""):
    __snake_case:						Union[str, Any]			  =						getattr(lowerCAmelCase__ , """__class__""" , lowerCAmelCase__)
  if hasattr(lowerCAmelCase__ , """__qualname__"""):
    return obj.__qualname__
  if hasattr(lowerCAmelCase__ , """__name__"""):
    return obj.__name__
  return str(lowerCAmelCase__)







def       A__							(   SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__)       ->   int:





  for key, value in source.items():
    if isinstance(lowerCAmelCase__ , lowerCAmelCase__):
      __snake_case:						Any			  =						destination.setdefault(lowerCAmelCase__ , {})
      merge_dicts(lowerCAmelCase__ , lowerCAmelCase__)
    else:
      __snake_case:						Optional[Any]			  =						value

  return destination







def       A__							(   SCREAMING_SNAKE_CASE__ = None)       ->   Optional[int]:





  if port is None:
    __snake_case:						List[Any]			  =						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_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__UpperCAmelCase	:       List[str]    	=							{
    "configuration_roberta": ["ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP", "RobertaConfig", "RobertaOnnxConfig"],
    "tokenization_roberta": ["RobertaTokenizer"],
}

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase	:       Optional[Any]    	=							["RobertaTokenizerFast"]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase	:       Tuple    	=							[
          "ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST",
          "RobertaForCausalLM",
          "RobertaForMaskedLM",
          "RobertaForMultipleChoice",
          "RobertaForQuestionAnswering",
          "RobertaForSequenceClassification",
          "RobertaForTokenClassification",
          "RobertaModel",
          "RobertaPreTrainedModel",
      ]

try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase	:       Optional[int]    	=							[
          "TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST",
          "TFRobertaForCausalLM",
          "TFRobertaForMaskedLM",
          "TFRobertaForMultipleChoice",
          "TFRobertaForQuestionAnswering",
          "TFRobertaForSequenceClassification",
          "TFRobertaForTokenClassification",
          "TFRobertaMainLayer",
          "TFRobertaModel",
          "TFRobertaPreTrainedModel",
      ]

try:
      if not is_flax_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase	:       List[Any]    	=							[
          "FlaxRobertaForCausalLM",
          "FlaxRobertaForMaskedLM",
          "FlaxRobertaForMultipleChoice",
          "FlaxRobertaForQuestionAnswering",
          "FlaxRobertaForSequenceClassification",
          "FlaxRobertaForTokenClassification",
          "FlaxRobertaModel",
          "FlaxRobertaPreTrainedModel",
      ]


if TYPE_CHECKING:
      from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig
      from .tokenization_roberta import RobertaTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_roberta_fast import RobertaTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_roberta import (
                ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
                RobertaForCausalLM,
                RobertaForMaskedLM,
                RobertaForMultipleChoice,
                RobertaForQuestionAnswering,
                RobertaForSequenceClassification,
                RobertaForTokenClassification,
                RobertaModel,
                RobertaPreTrainedModel,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_roberta import (
                TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
                TFRobertaForCausalLM,
                TFRobertaForMaskedLM,
                TFRobertaForMultipleChoice,
                TFRobertaForQuestionAnswering,
                TFRobertaForSequenceClassification,
                TFRobertaForTokenClassification,
                TFRobertaMainLayer,
                TFRobertaModel,
                TFRobertaPreTrainedModel,
            )

      try:
            if not is_flax_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_flax_roberta import (
                FlaxRobertaForCausalLM,
                FlaxRobertaForMaskedLM,
                FlaxRobertaForMultipleChoice,
                FlaxRobertaForQuestionAnswering,
                FlaxRobertaForSequenceClassification,
                FlaxRobertaForTokenClassification,
                FlaxRobertaModel,
                FlaxRobertaPreTrainedModel,
            )

else:
      import sys

      __UpperCAmelCase	:       str    	=							_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)






