



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





    '''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        lowerCamelCase  (				_snake_case  :						List[Any]     ,_snake_case  :						Union[str, Any] = True       ):





    '''simple docstring'''


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

    lowercase__		       =						is_compiled_module(_lowercase       )
    if is_compiled:
        lowercase__		       =						model
        lowercase__		       =						model._orig_mod

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

    while isinstance(_lowercase     ,_lowercase       ):
        lowercase__		       =						model.module

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

    if is_compiled:
        lowercase__		       =						model
        lowercase__		       =						compiled_model

    return model




def        lowerCamelCase  (				):





    '''simple docstring'''


    PartialState().wait_for_everyone()




def        lowerCamelCase  (				_snake_case  :						Dict     ,_snake_case  :						Union[str, Any]       ):





    '''simple docstring'''


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




@contextmanager
def        lowerCamelCase  (				**_snake_case  :						Union[str, Any]       ):





    '''simple docstring'''


    for key, value in kwargs.items():
        lowercase__		       =						str(_lowercase       )

    yield

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




def        lowerCamelCase  (				_snake_case  :						Any       ):





    '''simple docstring'''


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




def        lowerCamelCase  (				_snake_case  :						int     ,_snake_case  :						Optional[Any]       ):





    '''simple docstring'''


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

    return destination




def        lowerCamelCase  (				_snake_case  :						List[str] = None       ):





    '''simple docstring'''


    if port is None:
        lowercase__		       =						29_500
    with socket.socket(socket.AF_INET     ,socket.SOCK_STREAM       ) as s:
        return s.connect_ex(("localhost", port)       ) == 0









'''simple docstring'''





def        lowerCamelCase  (				_snake_case  :						int     ,_snake_case  :						float     ,_snake_case  :						float       ):





    '''simple docstring'''


    return round(float(moles / volume       ) * nfactor       )




def        lowerCamelCase  (				_snake_case  :						float     ,_snake_case  :						float     ,_snake_case  :						float       ):





    '''simple docstring'''


    return round(float((moles * 0.0_821 * temperature) / (volume)       )       )




def        lowerCamelCase  (				_snake_case  :						float     ,_snake_case  :						float     ,_snake_case  :						float       ):





    '''simple docstring'''


    return round(float((moles * 0.0_821 * temperature) / (pressure)       )       )




def        lowerCamelCase  (				_snake_case  :						float     ,_snake_case  :						float     ,_snake_case  :						float       ):





    '''simple docstring'''


    return round(float((pressure * volume) / (0.0_821 * moles)       )       )


if __name__ == "__main__":
 import doctest

 doctest.testmod()




