






"""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 	lowerCamelCase__			(				_lowerCamelCase			:						Optional[Any]					) ->							str:
  if is_torch_version('<'						,							'2.0.0'					) or not hasattr(_lowerCamelCase						,							'_dynamo'					):
    return False
  return isinstance(_lowerCamelCase						,							torch._dynamo.eval_frame.OptimizedModule					)







def 	lowerCamelCase__			(				_lowerCamelCase			:						Optional[int]						,							_lowerCamelCase			:						bool = True					) ->							Optional[Any]:
  lowerCamelCase_       	=		(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

  lowerCamelCase_       	=		is_compiled_module(_lowerCamelCase					)
  if is_compiled:
    lowerCamelCase_       	=		model
    lowerCamelCase_       	=		model._orig_mod

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

  while isinstance(_lowerCamelCase						,							_lowerCamelCase					):
    lowerCamelCase_       	=		model.module

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

  if is_compiled:
    lowerCamelCase_       	=		model
    lowerCamelCase_       	=		compiled_model

  return model







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







def 	lowerCamelCase__			(				_lowerCamelCase			:						Dict						,							_lowerCamelCase			:						str					) ->							Optional[Any]:
  if PartialState().distributed_type == DistributedType.TPU:
    xm.save(_lowerCamelCase						,							_lowerCamelCase					)
  elif PartialState().local_process_index == 0:
    torch.save(_lowerCamelCase						,							_lowerCamelCase					)







@contextmanager
def 	lowerCamelCase__			(				**_lowerCamelCase			:						List[str]					) ->							Any:
  for key, value in kwargs.items():
    lowerCamelCase_       	=		str(_lowerCamelCase					)

  yield

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







def 	lowerCamelCase__			(				_lowerCamelCase			:						Union[str, Any]					) ->							Optional[int]:
  if not hasattr(_lowerCamelCase						,							'__qualname__'					) and not hasattr(_lowerCamelCase						,							'__name__'					):
    lowerCamelCase_       	=		getattr(_lowerCamelCase						,							'__class__'						,							_lowerCamelCase					)
  if hasattr(_lowerCamelCase						,							'__qualname__'					):
    return obj.__qualname__
  if hasattr(_lowerCamelCase						,							'__name__'					):
    return obj.__name__
  return str(_lowerCamelCase					)







def 	lowerCamelCase__			(				_lowerCamelCase			:						Optional[int]						,							_lowerCamelCase			:						Optional[int]					) ->							List[Any]:
  for key, value in source.items():
    if isinstance(_lowerCamelCase						,							_lowerCamelCase					):
      lowerCamelCase_       	=		destination.setdefault(_lowerCamelCase						,							{}					)
      merge_dicts(_lowerCamelCase						,							_lowerCamelCase					)
    else:
      lowerCamelCase_       	=		value

  return destination







def 	lowerCamelCase__			(				_lowerCamelCase			:						int = None					) ->							bool:
  if port is None:
    lowerCamelCase_       	=		29500
  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_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


_SCREAMING_SNAKE_CASE   :							List[str]          =		{}

try:
  if not is_sentencepiece_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _SCREAMING_SNAKE_CASE   :							str          =		['''NllbTokenizer''']

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _SCREAMING_SNAKE_CASE   :							Optional[Any]          =		['''NllbTokenizerFast''']


if TYPE_CHECKING:
  try:
    if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_nllb import NllbTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_nllb_fast import NllbTokenizerFast

else:
  import sys

  _SCREAMING_SNAKE_CASE   :							Any          =		_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




