

'''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    A_  ( snake_case ):
  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    A_  ( snake_case      ,       snake_case = True ):
  SCREAMING_SNAKE_CASE:Any						      =					(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

  SCREAMING_SNAKE_CASE:List[Any]						      =					is_compiled_module(snake_case )
  if is_compiled:
    SCREAMING_SNAKE_CASE:Optional[Any]						      =					model
    SCREAMING_SNAKE_CASE:Optional[Any]						      =					model._orig_mod

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

  while isinstance(snake_case      ,       snake_case ):
    SCREAMING_SNAKE_CASE:int						      =					model.module

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

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

  return model





def    A_  ( ):
  PartialState().wait_for_everyone()





def    A_  ( snake_case      ,       snake_case ):
  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    A_  ( **snake_case ):
  for key, value in kwargs.items():
    SCREAMING_SNAKE_CASE:Optional[int]						      =					str(snake_case )

  yield

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





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

  return destination





def    A_  ( snake_case = None ):
  if port is None:
    SCREAMING_SNAKE_CASE:str						      =					29500
  with socket.socket(socket.AF_INET      ,       socket.SOCK_STREAM ) as s:
    return s.connect_ex(("localhost", port) ) == 0









'''simple docstring'''

def    A_  ( snake_case = 100 ):
  SCREAMING_SNAKE_CASE:Dict						      =					0
  SCREAMING_SNAKE_CASE:Optional[int]						      =					0
  for i in range(1      ,       n + 1 ):
    sum_of_squares += i**2
    sum_of_ints += i
  return sum_of_ints**2 - sum_of_squares


if __name__ == "__main__":
    print(f'''{solution() = }''')






