

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   (			SCREAMING_SNAKE_CASE   ):
      '''simple docstring'''







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







def 							_lowerCamelCase   (			SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE = True   ):
      '''simple docstring'''







      A_							       =		(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

      A_							       =		is_compiled_module(SCREAMING_SNAKE_CASE   )
      if is_compiled:
            A_							       =		model
            A_							       =		model._orig_mod

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

      while isinstance(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE   ):
            A_							       =		model.module

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

      if is_compiled:
            A_							       =		model
            A_							       =		compiled_model

      return model







def 							_lowerCamelCase   (			):
      '''simple docstring'''







      PartialState().wait_for_everyone()







def 							_lowerCamelCase   (			SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE   ):
      '''simple docstring'''







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







@contextmanager
def 							_lowerCamelCase   (			**SCREAMING_SNAKE_CASE   ):
      '''simple docstring'''







      for key, value in kwargs.items():
            A_							       =		str(SCREAMING_SNAKE_CASE   )

      yield

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







def 							_lowerCamelCase   (			SCREAMING_SNAKE_CASE   ):
      '''simple docstring'''







      if not hasattr(SCREAMING_SNAKE_CASE		,				'''__qualname__'''   ) and not hasattr(SCREAMING_SNAKE_CASE		,				'''__name__'''   ):
            A_							       =		getattr(SCREAMING_SNAKE_CASE		,				'''__class__'''		,				SCREAMING_SNAKE_CASE   )
      if hasattr(SCREAMING_SNAKE_CASE		,				'''__qualname__'''   ):
            return obj.__qualname__
      if hasattr(SCREAMING_SNAKE_CASE		,				'''__name__'''   ):
            return obj.__name__
      return str(SCREAMING_SNAKE_CASE   )







def 							_lowerCamelCase   (			SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE   ):
      '''simple docstring'''







      for key, value in source.items():
            if isinstance(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE   ):
                  A_							       =		destination.setdefault(SCREAMING_SNAKE_CASE		,				{}   )
                  merge_dicts(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE   )
            else:
                  A_							       =		value

      return destination







def 							_lowerCamelCase   (			SCREAMING_SNAKE_CASE = None   ):
      '''simple docstring'''







      if port is None:
            A_							       =		29500
      with socket.socket(socket.AF_INET		,				socket.SOCK_STREAM   ) as s:
            return s.connect_ex(('''localhost''', port)   ) == 0








def 							_lowerCamelCase   (			SCREAMING_SNAKE_CASE = 1000   ):
      '''simple docstring'''







      return sum(2 * a * ((a - 1) // 2) for a in range(3		,				n + 1   )   )


if __name__ == "__main__":
      print(solution())





