
"""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 ):
    if is_torch_version('<'    ,'2.0.0' ) or not hasattr(_snake_case    ,'_dynamo' ):
        return False
    return isinstance(_snake_case    ,torch._dynamo.eval_frame.OptimizedModule )
def 					lowerCamelCase   (     _snake_case    ,_snake_case = True ):
    UpperCAmelCase__	:      Tuple     							=					(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

    UpperCAmelCase__	:      Any     							=					is_compiled_module(_snake_case )
    if is_compiled:
        UpperCAmelCase__	:      Union[str, Any]     							=					model
        UpperCAmelCase__	:      Optional[Any]     							=					model._orig_mod

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

    while isinstance(_snake_case    ,_snake_case ):
        UpperCAmelCase__	:      List[Any]     							=					model.module

    if not keep_fpaa_wrapper:
        UpperCAmelCase__	:      Union[str, Any]     							=					getattr(_snake_case    ,'forward' )
        UpperCAmelCase__	:      Optional[Any]     							=					model.__dict__.pop('_original_forward'    ,_snake_case )
        if original_forward is not None:
            while hasattr(_snake_case    ,'__wrapped__' ):
                UpperCAmelCase__	:      Union[str, Any]     							=					forward.__wrapped__
                if forward == original_forward:
                    break
            UpperCAmelCase__	:      Optional[Any]     							=					forward
        if getattr(_snake_case    ,'_converted_to_transformer_engine'    ,_snake_case ):
            convert_model(_snake_case    ,to_transformer_engine=_snake_case )

    if is_compiled:
        UpperCAmelCase__	:      Tuple     							=					model
        UpperCAmelCase__	:      Any     							=					compiled_model

    return model
def 					lowerCamelCase   (     ):
    PartialState().wait_for_everyone()
def 					lowerCamelCase   (     _snake_case    ,_snake_case ):
    if PartialState().distributed_type == DistributedType.TPU:
        xm.save(_snake_case    ,_snake_case )
    elif PartialState().local_process_index == 0:
        torch.save(_snake_case    ,_snake_case )
@contextmanager
def 					lowerCamelCase   (     **_snake_case ):
    for key, value in kwargs.items():
        UpperCAmelCase__	:      str     							=					str(_snake_case )

    yield

    for key in kwargs:
        if key.upper() in os.environ:
            del os.environ[key.upper()]
def 					lowerCamelCase   (     _snake_case ):
    if not hasattr(_snake_case    ,'__qualname__' ) and not hasattr(_snake_case    ,'__name__' ):
        UpperCAmelCase__	:      int     							=					getattr(_snake_case    ,'__class__'    ,_snake_case )
    if hasattr(_snake_case    ,'__qualname__' ):
        return obj.__qualname__
    if hasattr(_snake_case    ,'__name__' ):
        return obj.__name__
    return str(_snake_case )
def 					lowerCamelCase   (     _snake_case    ,_snake_case ):
    for key, value in source.items():
        if isinstance(_snake_case    ,_snake_case ):
            UpperCAmelCase__	:      Optional[Any]     							=					destination.setdefault(_snake_case    ,{} )
            merge_dicts(_snake_case    ,_snake_case )
        else:
            UpperCAmelCase__	:      Optional[int]     							=					value

    return destination

def 					lowerCamelCase   (     _snake_case = None ):
    if port is None:
        UpperCAmelCase__	:      List[Any]     							=					29500
    with socket.socket(socket.AF_INET    ,socket.SOCK_STREAM ) as s:
        return s.connect_ex(('localhost', port) ) == 0



"""simple docstring"""



from itertools import permutations
def 					lowerCamelCase   (     _snake_case ):
    if num[3] % 2 != 0:
        return False

    if (num[2] + num[3] + num[4]) % 3 != 0:
        return False

    if num[5] % 5 != 0:
        return False

    UpperCAmelCase__	:      List[str]     							=					[7, 11, 13, 17]
    for i, test in enumerate(_snake_case ):
        if (num[i + 4] * 100 + num[i + 5] * 10 + num[i + 6]) % test != 0:
            return False
    return True
def 					lowerCamelCase   (     _snake_case = 10 ):
    return sum(
        int(''.join(map(_snake_case    ,_snake_case ) ) )
        for num in permutations(range(_snake_case ) )
        if is_substring_divisible(_snake_case ) )


if __name__ == "__main__":
    print(f'{solution() = }')

