





'''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__    (    lowercase :					List[str]				)		->		List[str]:





     """simple docstring"""

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







def  a__    (    lowercase :					int, lowercase :					bool = True				)		->		Dict:





     """simple docstring"""

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

     _UpperCamelCase       =	is_compiled_module(lowercase				)
     if is_compiled:
          _UpperCamelCase       =	model
          _UpperCamelCase       =	model._orig_mod

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

     while isinstance(lowercase, lowercase				):
          _UpperCamelCase       =	model.module

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

     if is_compiled:
          _UpperCamelCase       =	model
          _UpperCamelCase       =	compiled_model

     return model







def  a__    (    )		->		str:





     """simple docstring"""

     PartialState().wait_for_everyone()







def  a__    (    lowercase :					List[Any], lowercase :					List[Any]				)		->		int:





     """simple docstring"""

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







@contextmanager
def  a__    (    **lowercase :					Any				)		->		str:





     """simple docstring"""

     for key, value in kwargs.items():
          _UpperCamelCase       =	str(lowercase				)

     yield

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







def  a__    (    lowercase :					Any				)		->		List[Any]:





     """simple docstring"""

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







def  a__    (    lowercase :					Optional[int], lowercase :					Dict				)		->		List[Any]:





     """simple docstring"""

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

     return destination







def  a__    (    lowercase :					int = None				)		->		bool:





     """simple docstring"""

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










'''simple docstring'''

from cva import destroyAllWindows, imread, imshow, waitKey







def  a__    (    lowercase :					str				)		->		Optional[int]:





     """simple docstring"""

     _UpperCamelCase   ,    _UpperCamelCase       =	img.shape[0], img.shape[1]

     # converting each pixel's color to its negative
     for i in range(lowercase				):
          for j in range(lowercase				):
               _UpperCamelCase       =	[255, 255, 255] - img[i][j]

     return img


if __name__ == "__main__":
   # read original image
   lowercase__						:      Optional[int]	=    imread('image_data/lena.jpg', 1)

   # convert to its negative
   lowercase__						:      Union[str, Any]	=    convert_to_negative(img)

   # show result image
   imshow('negative of original image', img)
   waitKey(0)
   destroyAllWindows()



