






'''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(					UpperCamelCase__			:  Optional[int]							):




      '''simple docstring'''






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





def 	_a(					UpperCamelCase__			:  List[Any],		UpperCamelCase__			:  bool = True							):




      '''simple docstring'''






      SCREAMING_SNAKE_CASE__       :			Optional[Any]													=(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

      SCREAMING_SNAKE_CASE__       :			Any													=is_compiled_module(UpperCamelCase__							)
      if is_compiled:
            SCREAMING_SNAKE_CASE__       :			Dict													=model
            SCREAMING_SNAKE_CASE__       :			Dict													=model._orig_mod

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

      while isinstance(UpperCamelCase__,		UpperCamelCase__							):
            SCREAMING_SNAKE_CASE__       :			Optional[Any]													=model.module

      if not keep_fpaa_wrapper:
            SCREAMING_SNAKE_CASE__       :			Optional[Any]													=getattr(UpperCamelCase__,		'''forward'''							)
            SCREAMING_SNAKE_CASE__       :			List[Any]													=model.__dict__.pop('''_original_forward''',		UpperCamelCase__							)
            if original_forward is not None:
                  while hasattr(UpperCamelCase__,		'''__wrapped__'''							):
                        SCREAMING_SNAKE_CASE__       :			Optional[int]													=forward.__wrapped__
                        if forward == original_forward:
                              break
                  SCREAMING_SNAKE_CASE__       :			Optional[Any]													=forward
            if getattr(UpperCamelCase__,		'''_converted_to_transformer_engine''',		UpperCamelCase__							):
                  convert_model(UpperCamelCase__,		to_transformer_engine=UpperCamelCase__							)

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

      return model





def 	_a(					):




      '''simple docstring'''






      PartialState().wait_for_everyone()





def 	_a(					UpperCamelCase__			:  Optional[Any],		UpperCamelCase__			:  str							):




      '''simple docstring'''






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





@contextmanager
def 	_a(					**UpperCamelCase__			:  Union[str, Any]							):




      '''simple docstring'''






      for key, value in kwargs.items():
            SCREAMING_SNAKE_CASE__       :			List[Any]													=str(UpperCamelCase__							)

      yield

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





def 	_a(					UpperCamelCase__			:  Any							):




      '''simple docstring'''






      if not hasattr(UpperCamelCase__,		'''__qualname__'''							) and not hasattr(UpperCamelCase__,		'''__name__'''							):
            SCREAMING_SNAKE_CASE__       :			Optional[int]													=getattr(UpperCamelCase__,		'''__class__''',		UpperCamelCase__							)
      if hasattr(UpperCamelCase__,		'''__qualname__'''							):
            return obj.__qualname__
      if hasattr(UpperCamelCase__,		'''__name__'''							):
            return obj.__name__
      return str(UpperCamelCase__							)





def 	_a(					UpperCamelCase__			:  Union[str, Any],		UpperCamelCase__			:  int							):




      '''simple docstring'''






      for key, value in source.items():
            if isinstance(UpperCamelCase__,		UpperCamelCase__							):
                  SCREAMING_SNAKE_CASE__       :			Optional[int]													=destination.setdefault(UpperCamelCase__,		{}							)
                  merge_dicts(UpperCamelCase__,		UpperCamelCase__							)
            else:
                  SCREAMING_SNAKE_CASE__       :			List[str]													=value

      return destination







def 	_a(					UpperCamelCase__			:  int = None							):




      '''simple docstring'''






      if port is None:
            SCREAMING_SNAKE_CASE__       :			Any													=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'''
from decimal import Decimal, getcontext
from math import ceil, factorial





def 	_a(					UpperCamelCase__			:  int							):




      '''simple docstring'''






      if not isinstance(UpperCamelCase__,		UpperCamelCase__							):
            raise TypeError('''Undefined for non-integers'''							)
      elif precision < 1:
            raise ValueError('''Undefined for non-natural numbers'''							)

      SCREAMING_SNAKE_CASE__       :			Optional[Any]													=precision
      SCREAMING_SNAKE_CASE__       :			int													=ceil(precision / 1_4							)
      SCREAMING_SNAKE_CASE__       :			int													=4_2_6_8_8_0 * Decimal(1_0_0_0_5							).sqrt()
      SCREAMING_SNAKE_CASE__       :			Tuple													=1
      SCREAMING_SNAKE_CASE__       :			Any													=1_3_5_9_1_4_0_9
      SCREAMING_SNAKE_CASE__       :			List[Any]													=Decimal(UpperCamelCase__							)
      for k in range(1,		UpperCamelCase__							):
            SCREAMING_SNAKE_CASE__       :			str													=factorial(6 * k							) // (factorial(3 * k							) * factorial(UpperCamelCase__							) ** 3)
            linear_term += 5_4_5_1_4_0_1_3_4
            exponential_term *= -2_6_2_5_3_7_4_1_2_6_4_0_7_6_8_0_0_0
            partial_sum += Decimal(multinomial_term * linear_term							) / exponential_term
      return str(constant_term / partial_sum							)[:-1]


if __name__ == "__main__":
  a_					=	5_0
  print(F'''The first {n} digits of pi is: {pi(n)}''')