





"""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   (      a_    :Dict)		->		Tuple:
				if is_torch_version('''<''' ,							'''2.0.0''') or not hasattr(a_ ,							'''_dynamo'''):
								return False
				return isinstance(a_ ,							torch._dynamo.eval_frame.OptimizedModule)

def      __A   (      a_    :Any ,							a_    :bool = True)		->		List[str]:
				__a			:    Any      							=	(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

				__a			:    Tuple      							=	is_compiled_module(a_)
				if is_compiled:
								__a			:    Tuple      							=	model
								__a			:    Optional[int]      							=	model._orig_mod

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

				while isinstance(a_ ,							a_):
								__a			:    Tuple      							=	model.module

				if not keep_fpaa_wrapper:
								__a			:    Tuple      							=	getattr(a_ ,							'''forward''')
								__a			:    List[str]      							=	model.__dict__.pop('''_original_forward''' ,							a_)
								if original_forward is not None:
												while hasattr(a_ ,							'''__wrapped__'''):
																__a			:    int      							=	forward.__wrapped__
																if forward == original_forward:
																				break
												__a			:    Tuple      							=	forward
								if getattr(a_ ,							'''_converted_to_transformer_engine''' ,							a_):
												convert_model(a_ ,							to_transformer_engine=a_)

				if is_compiled:
								__a			:    str      							=	model
								__a			:    List[Any]      							=	compiled_model

				return model

def      __A   (      )		->		Union[str, Any]:
				PartialState().wait_for_everyone()

def      __A   (      a_    :List[str] ,							a_    :List[Any])		->		Union[str, Any]:
				if PartialState().distributed_type == DistributedType.TPU:
								xm.save(a_ ,							a_)
				elif PartialState().local_process_index == 0:
								torch.save(a_ ,							a_)

@contextmanager
def      __A   (      **a_    :Optional[int])		->		str:
				for key, value in kwargs.items():
								__a			:    int      							=	str(a_)

				yield

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

def      __A   (      a_    :Any)		->		str:
				if not hasattr(a_ ,							'''__qualname__''') and not hasattr(a_ ,							'''__name__'''):
								__a			:    int      							=	getattr(a_ ,							'''__class__''' ,							a_)
				if hasattr(a_ ,							'''__qualname__'''):
								return obj.__qualname__
				if hasattr(a_ ,							'''__name__'''):
								return obj.__name__
				return str(a_)

def      __A   (      a_    :Any ,							a_    :Any)		->		int:
				for key, value in source.items():
								if isinstance(a_ ,							a_):
												__a			:    Tuple      							=	destination.setdefault(a_ ,							{})
												merge_dicts(a_ ,							a_)
								else:
												__a			:    Tuple      							=	value

				return destination






def      __A   (      a_    :int = None)		->		bool:
				if port is None:
								__a			:    Dict      							=	2_95_00
				with socket.socket(socket.AF_INET ,							socket.SOCK_STREAM) as s:
								return s.connect_ex(('''localhost''', port)) == 0



from __future__ import annotations

import matplotlib.pyplot as plt  # type: ignore
import numpy

# initial triangle of Koch snowflake
lowerCamelCase		   =			numpy.array([0, 0])
lowerCamelCase		   =			numpy.array([0.5, 0.866_0254])
lowerCamelCase		   =			numpy.array([1, 0])
lowerCamelCase		   =			[VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1]




def       a_ (       SCREAMING_SNAKE_CASE__   : list[numpy.ndarray]						,     SCREAMING_SNAKE_CASE__   : int	):
       '''simple docstring'''





       _lowerCamelCase			:						List[Any]            =initial_vectors
       for _ in range(SCREAMING_SNAKE_CASE__	):
              _lowerCamelCase			:						List[str]            =iteration_step(SCREAMING_SNAKE_CASE__	)
       return vectors




def       a_ (       SCREAMING_SNAKE_CASE__   : list[numpy.ndarray]	):
       '''simple docstring'''





       _lowerCamelCase			:						Dict            =[]
       for i, start_vector in enumerate(vectors[:-1]	):
              _lowerCamelCase			:						Tuple            =vectors[i + 1]
              new_vectors.append(SCREAMING_SNAKE_CASE__	)
              _lowerCamelCase			:						Tuple            =end_vector - start_vector
              new_vectors.append(start_vector + difference_vector / 3	)
              new_vectors.append(
                  start_vector + difference_vector / 3 + rotate(difference_vector / 3						,     60	)	)
              new_vectors.append(start_vector + difference_vector * 2 / 3	)
       new_vectors.append(vectors[-1]	)
       return new_vectors




def       a_ (       SCREAMING_SNAKE_CASE__   : numpy.ndarray						,     SCREAMING_SNAKE_CASE__   : float	):
       '''simple docstring'''





       _lowerCamelCase			:						Union[str, Any]            =numpy.radians(SCREAMING_SNAKE_CASE__	)
       _lowerCamelCase      ,	_lowerCamelCase			:						List[Any]            =numpy.cos(SCREAMING_SNAKE_CASE__	), numpy.sin(SCREAMING_SNAKE_CASE__	)
       _lowerCamelCase			:						str            =numpy.array(((c, -s), (s, c))	)
       return numpy.dot(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	)




def       a_ (       SCREAMING_SNAKE_CASE__   : list[numpy.ndarray]	):
       '''simple docstring'''





       _lowerCamelCase			:						Optional[Any]            =plt.gca()
       axes.set_aspect('equal'	)

       # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all
       # y-coordinates as inputs, which are constructed from the vector-list using
       # zip()
       _lowerCamelCase      ,	_lowerCamelCase			:						Tuple            =zip(*SCREAMING_SNAKE_CASE__	)
       plt.plot(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	)
       plt.show()


if __name__ == "__main__":
 import doctest

 doctest.testmod()

 lowerCamelCase		   =			iterate(INITIAL_VECTORS, 5)
 plot(processed_vectors)


