
"""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  _UpperCamelCase	(						_A      )    ->  List[str]:





    """simple docstring"""

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


def  _UpperCamelCase	(						_A					,      _A = True      )    ->  Any:





    """simple docstring"""

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

    _UpperCAmelCase          =   is_compiled_module(_A      )
    if is_compiled:
        _UpperCAmelCase          =   model
        _UpperCAmelCase          =   model._orig_mod

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

    while isinstance(_A					,      _A      ):
        _UpperCAmelCase          =   model.module

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

    if is_compiled:
        _UpperCAmelCase          =   model
        _UpperCAmelCase          =   compiled_model

    return model


def  _UpperCamelCase	(						)    ->  Optional[int]:





    """simple docstring"""

    PartialState().wait_for_everyone()


def  _UpperCamelCase	(						_A					,      _A      )    ->  Union[str, Any]:





    """simple docstring"""

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


@contextmanager
def  _UpperCamelCase	(						**_A      )    ->  Optional[Any]:





    """simple docstring"""

    for key, value in kwargs.items():
        _UpperCAmelCase          =   str(_A      )

    yield

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


def  _UpperCamelCase	(						_A      )    ->  str:





    """simple docstring"""

    if not hasattr(_A					,      """__qualname__"""      ) and not hasattr(_A					,      """__name__"""      ):
        _UpperCAmelCase          =   getattr(_A					,      """__class__"""					,      _A      )
    if hasattr(_A					,      """__qualname__"""      ):
        return obj.__qualname__
    if hasattr(_A					,      """__name__"""      ):
        return obj.__name__
    return str(_A      )


def  _UpperCamelCase	(						_A					,      _A      )    ->  int:





    """simple docstring"""

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

    return destination







def  _UpperCamelCase	(						_A = None      )    ->  bool:





    """simple docstring"""

    if port is None:
        _UpperCAmelCase          =   2_9_5_0_0
    with socket.socket(socket.AF_INET					,      socket.SOCK_STREAM      ) as s:
        return s.connect_ex(("""localhost""", port)      ) == 0

"""simple docstring"""



import colorsys

from PIL import Image  # type: ignore


def  _UpperCamelCase	(						_A					,      _A					,      _A      )    ->  float:





    """simple docstring"""

    _UpperCAmelCase          =   x
    _UpperCAmelCase          =   y
    for step in range(_A      ):  # noqa: B007
        _UpperCAmelCase          =   a * a - b * b + x
        _UpperCAmelCase          =   2 * a * b + y
        _UpperCAmelCase          =   a_new

        # divergence happens for all complex number with an absolute value
        # greater than 4
        if a * a + b * b > 4:
            break
    return step / (max_step - 1)


def  _UpperCamelCase	(						_A      )    ->  tuple:





    """simple docstring"""

    if distance == 1:
        return (0, 0, 0)
    else:
        return (2_5_5, 2_5_5, 2_5_5)


def  _UpperCamelCase	(						_A      )    ->  tuple:





    """simple docstring"""

    if distance == 1:
        return (0, 0, 0)
    else:
        return tuple(round(i * 2_5_5      ) for i in colorsys.hsv_to_rgb(_A					,      1					,      1      )      )


def  _UpperCamelCase	(						_A = 8_0_0					,      _A = 6_0_0					,      _A = -0.6					,      _A = 0					,      _A = 3.2					,      _A = 5_0					,      _A = True					,      )    ->  Image.Image:





    """simple docstring"""

    _UpperCAmelCase          =   Image.new("""RGB"""					,      (image_width, image_height)      )
    _UpperCAmelCase          =   img.load()

    # loop through the image-coordinates
    for image_x in range(_A      ):
        for image_y in range(_A      ):
            # determine the figure-coordinates based on the image-coordinates
            _UpperCAmelCase          =   figure_width / image_width * image_height
            _UpperCAmelCase          =   figure_center_x + (image_x / image_width - 0.5) * figure_width
            _UpperCAmelCase          =   figure_center_y + (image_y / image_height - 0.5) * figure_height

            _UpperCAmelCase          =   get_distance(_A					,      _A					,      _A      )

            # color the corresponding pixel based on the selected coloring-function
            if use_distance_color_coding:
                _UpperCAmelCase          =   get_color_coded_rgb(_A      )
            else:
                _UpperCAmelCase          =   get_black_and_white_rgb(_A      )

    return img


if __name__ == "__main__":
 import doctest

 doctest.testmod()

 # colored version, full figure
 a					:	List[str]							=		get_image()

 # uncomment for colored version, different section, zoomed in
 # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4,
 # figure_width = 0.8)

 # uncomment for black and white version, full figure
 # img = get_image(use_distance_color_coding = False)

 # uncomment to save the image
 # img.save("mandelbrot.png")

 img.show()