
'''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__		):




    '''simple docstring'''



    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		):




    '''simple docstring'''



    A			:  List[str]   =		(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

    A			:  Any   =		is_compiled_module(snake_case__		)
    if is_compiled:
        A			:  Optional[Any]   =		model
        A			:  int   =		model._orig_mod

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

    while isinstance(snake_case__						,							snake_case__		):
        A			:  Dict   =		model.module

    if not keep_fpaa_wrapper:
        A			:  List[str]   =		getattr(snake_case__						,							'''forward'''		)
        A			:  Optional[int]   =		model.__dict__.pop('''_original_forward'''						,							snake_case__		)
        if original_forward is not None:
            while hasattr(snake_case__						,							'''__wrapped__'''		):
                A			:  Union[str, Any]   =		forward.__wrapped__
                if forward == original_forward:
                    break
            A			:  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:
        A			:  Any   =		model
        A			:  str   =		compiled_model

    return model







def 						lowerCAmelCase_							(  ):




    '''simple docstring'''



    PartialState().wait_for_everyone()







def 						lowerCAmelCase_							(  snake_case__						,							snake_case__		):




    '''simple docstring'''



    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__		):




    '''simple docstring'''



    for key, value in kwargs.items():
        A			:  int   =		str(snake_case__		)

    yield

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







def 						lowerCAmelCase_							(  snake_case__		):




    '''simple docstring'''



    if not hasattr(snake_case__						,							'''__qualname__'''		) and not hasattr(snake_case__						,							'''__name__'''		):
        A			:  List[str]   =		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__		):




    '''simple docstring'''



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

    return destination







def 						lowerCAmelCase_							(  snake_case__ = None		):




    '''simple docstring'''



    if port is None:
        A			:  Optional[Any]   =		2_9500
    with socket.socket(socket.AF_INET						,							socket.SOCK_STREAM		) as s:
        return s.connect_ex(('''localhost''', port)		) == 0






'''simple docstring'''




from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase     :							Any       						=     logging.get_logger(__name__)

lowercase     :							int       						=     {
    'facebook/timesformer': 'https://huggingface.co/facebook/timesformer/resolve/main/config.json',
}





class 					A							(				__snake_case		):
     __magic_name__          =						'''timesformer'''





     def __init__(				self		,     SCREAMING_SNAKE_CASE=224		,     SCREAMING_SNAKE_CASE=16		,     SCREAMING_SNAKE_CASE=3		,     SCREAMING_SNAKE_CASE=8		,     SCREAMING_SNAKE_CASE=768		,     SCREAMING_SNAKE_CASE=12		,     SCREAMING_SNAKE_CASE=12		,     SCREAMING_SNAKE_CASE=3072		,     SCREAMING_SNAKE_CASE="gelu"		,     SCREAMING_SNAKE_CASE=0.0		,     SCREAMING_SNAKE_CASE=0.0		,     SCREAMING_SNAKE_CASE=0.02		,     SCREAMING_SNAKE_CASE=1e-6		,     SCREAMING_SNAKE_CASE=True		,     SCREAMING_SNAKE_CASE="divided_space_time"		,     SCREAMING_SNAKE_CASE=0		,     **SCREAMING_SNAKE_CASE		,     )					->							Tuple:






         """simple docstring"""






         super().__init__(**SCREAMING_SNAKE_CASE      )

         A			:  List[Any]   =		image_size
         A			:  List[str]   =		patch_size
         A			:  List[str]   =		num_channels
         A			:  str   =		num_frames

         A			:  Any   =		hidden_size
         A			:  Dict   =		num_hidden_layers
         A			:  Any   =		num_attention_heads
         A			:  Optional[Any]   =		intermediate_size
         A			:  Union[str, Any]   =		hidden_act
         A			:  Tuple   =		hidden_dropout_prob
         A			:  Optional[int]   =		attention_probs_dropout_prob
         A			:  Tuple   =		initializer_range
         A			:  List[Any]   =		layer_norm_eps
         A			:  Union[str, Any]   =		qkv_bias

         A			:  List[str]   =		attention_type
         A			:  Union[str, Any]   =		drop_path_rate




