





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



def 					__lowerCamelCase       (							a_					:		int			,       a_					:		bool = True							)      ->				Optional[int]:
      __SCREAMING_SNAKE_CASE       :Tuple		    =  (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

      __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  is_compiled_module(a_							)
      if is_compiled:
            __SCREAMING_SNAKE_CASE       :Tuple		    =  model
            __SCREAMING_SNAKE_CASE       :Tuple		    =  model._orig_mod

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

      while isinstance(a_			,       a_							):
            __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  model.module

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

      if is_compiled:
            __SCREAMING_SNAKE_CASE       :Optional[int]		    =  model
            __SCREAMING_SNAKE_CASE       :Optional[int]		    =  compiled_model

      return model



def 					__lowerCamelCase       (							)      ->				str:
      PartialState().wait_for_everyone()



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



@contextmanager
def 					__lowerCamelCase       (							**a_					:		int							)      ->				Tuple:
      for key, value in kwargs.items():
            __SCREAMING_SNAKE_CASE       :Any		    =  str(a_							)

      yield

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



def 					__lowerCamelCase       (							a_					:		List[str]							)      ->				List[Any]:
      if not hasattr(a_			,       '''__qualname__'''							) and not hasattr(a_			,       '''__name__'''							):
            __SCREAMING_SNAKE_CASE       :List[Any]		    =  getattr(a_			,       '''__class__'''			,       a_							)
      if hasattr(a_			,       '''__qualname__'''							):
            return obj.__qualname__
      if hasattr(a_			,       '''__name__'''							):
            return obj.__name__
      return str(a_							)



def 					__lowerCamelCase       (							a_					:		List[Any]			,       a_					:		List[str]							)      ->				Tuple:
      for key, value in source.items():
            if isinstance(a_			,       a_							):
                  __SCREAMING_SNAKE_CASE       :List[str]		    =  destination.setdefault(a_			,       {}							)
                  merge_dicts(a_			,       a_							)
            else:
                  __SCREAMING_SNAKE_CASE       :str		    =  value

      return destination







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






"""simple docstring"""
def 					__lowerCamelCase       (							a_					:		Union[str, Any]			,       a_					:		Optional[Any]							)      ->				Union[str, Any]:
      return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2



def 					__lowerCamelCase       (							a_					:		Optional[int]			,       a_					:		Any=0							)      ->				Optional[Any]:
      return sorted(a_			,       key=lambda a_							: x[column]							)



def 					__lowerCamelCase       (							a_					:		Optional[Any]			,       a_					:		Optional[int]			,       a_					:		str=float('''inf'''							)							)      ->				str:

      for i in range(points_counts - 1							):
            for j in range(i + 1			,       a_							):
                  __SCREAMING_SNAKE_CASE       :Dict		    =  euclidean_distance_sqr(points[i]			,       points[j]							)
                  if current_dis < min_dis:
                        __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  current_dis
      return min_dis



def 					__lowerCamelCase       (							a_					:		List[Any]			,       a_					:		Any			,       a_					:		Optional[int]=float('''inf'''							)							)      ->				Optional[Any]:

      for i in range(min(6			,       points_counts - 1							)			,       a_							):
            for j in range(max(0			,       i - 6							)			,       a_							):
                  __SCREAMING_SNAKE_CASE       :Dict		    =  euclidean_distance_sqr(points[i]			,       points[j]							)
                  if current_dis < min_dis:
                        __SCREAMING_SNAKE_CASE       :int		    =  current_dis
      return min_dis



def 					__lowerCamelCase       (							a_					:		str			,       a_					:		List[Any]			,       a_					:		int							)      ->				Optional[int]:

      # base case
      if points_counts <= 3:
            return dis_between_closest_pair(a_			,       a_							)

      # recursion
      __SCREAMING_SNAKE_CASE       :int		    =  points_counts // 2
      __SCREAMING_SNAKE_CASE       :Dict		    =  closest_pair_of_points_sqr(
          a_			,       points_sorted_on_y[:mid]			,       a_							)
      __SCREAMING_SNAKE_CASE       :Any		    =  closest_pair_of_points_sqr(
          a_			,       points_sorted_on_y[mid:]			,       points_counts - mid							)
      __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  min(a_			,       a_							)
      __SCREAMING_SNAKE_CASE       :str		    =  []
      for point in points_sorted_on_x:
            if abs(point[0] - points_sorted_on_x[mid][0]							) < closest_pair_dis:
                  cross_strip.append(a_							)

      __SCREAMING_SNAKE_CASE       :Dict		    =  dis_between_closest_in_strip(
          a_			,       len(a_							)			,       a_							)
      return min(a_			,       a_							)



def 					__lowerCamelCase       (							a_					:		int			,       a_					:		Tuple							)      ->				List[Any]:
      __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  column_based_sort(a_			,       column=0							)
      __SCREAMING_SNAKE_CASE       :int		    =  column_based_sort(a_			,       column=1							)
      return (
          closest_pair_of_points_sqr(
              a_			,       a_			,       a_							)
      ) ** 0.5


if __name__ == "__main__":
  lowerCamelCase_       =		[(2, 3), (1_2, 3_0), (4_0, 5_0), (5, 1), (1_2, 1_0), (3, 4)]
  print("Distance:", closest_pair_of_points(points, len(points)))