

'''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__		(    SCREAMING_SNAKE_CASE   : Dict    ):
  if is_torch_version('<'							, '2.0.0'    ) or not hasattr(SCREAMING_SNAKE_CASE							, '_dynamo'    ):
    return False
  return isinstance(SCREAMING_SNAKE_CASE							, torch._dynamo.eval_frame.OptimizedModule    )


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : int							, SCREAMING_SNAKE_CASE   : bool = True    ):
  UpperCAmelCase    				=						(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

  UpperCAmelCase    				=						is_compiled_module(SCREAMING_SNAKE_CASE    )
  if is_compiled:
    UpperCAmelCase    				=						model
    UpperCAmelCase    				=						model._orig_mod

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

  while isinstance(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE    ):
    UpperCAmelCase    				=						model.module

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

  if is_compiled:
    UpperCAmelCase    				=						model
    UpperCAmelCase    				=						compiled_model

  return model


def    lowerCamelCase__		(    ):
  PartialState().wait_for_everyone()


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : List[Any]							, SCREAMING_SNAKE_CASE   : Tuple    ):
  if PartialState().distributed_type == DistributedType.TPU:
    xm.save(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE    )
  elif PartialState().local_process_index == 0:
    torch.save(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE    )


@contextmanager
def    lowerCamelCase__		(    **SCREAMING_SNAKE_CASE   : Any    ):
  for key, value in kwargs.items():
    UpperCAmelCase    				=						str(SCREAMING_SNAKE_CASE    )

  yield

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


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : Any    ):
  if not hasattr(SCREAMING_SNAKE_CASE							, '__qualname__'    ) and not hasattr(SCREAMING_SNAKE_CASE							, '__name__'    ):
    UpperCAmelCase    				=						getattr(SCREAMING_SNAKE_CASE							, '__class__'							, SCREAMING_SNAKE_CASE    )
  if hasattr(SCREAMING_SNAKE_CASE							, '__qualname__'    ):
    return obj.__qualname__
  if hasattr(SCREAMING_SNAKE_CASE							, '__name__'    ):
    return obj.__name__
  return str(SCREAMING_SNAKE_CASE    )


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : str							, SCREAMING_SNAKE_CASE   : List[str]    ):
  for key, value in source.items():
    if isinstance(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE    ):
      UpperCAmelCase    				=						destination.setdefault(SCREAMING_SNAKE_CASE							, {}    )
      merge_dicts(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE    )
    else:
      UpperCAmelCase    				=						value

  return destination



def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : int = None    ):
  if port is None:
    UpperCAmelCase    				=						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


_a					:			str							=    logging.get_logger(__name__)

_a					:			Dict							=    {
    'microsoft/cvt-13': 'https://huggingface.co/microsoft/cvt-13/resolve/main/config.json',
    # See all Cvt models at https://huggingface.co/models?filter=cvt
}



class 							lowercase_						(		a			):

  '''simple docstring'''





  __lowerCAmelCase       :			List[Any]     	= "cvt"
  def __init__(  self ,			a_=3 ,			a_=[7, 3, 3] ,			a_=[4, 2, 2] ,			a_=[2, 1, 1] ,			a_=[6_4, 1_9_2, 3_8_4] ,			a_=[1, 3, 6] ,			a_=[1, 2, 1_0] ,			a_=[4.0, 4.0, 4.0] ,			a_=[0.0, 0.0, 0.0] ,			a_=[0.0, 0.0, 0.0] ,			a_=[0.0, 0.0, 0.1] ,			a_=[True, True, True] ,			a_=[False, False, True] ,			a_=["dw_bn", "dw_bn", "dw_bn"] ,			a_=[3, 3, 3] ,			a_=[1, 1, 1] ,			a_=[2, 2, 2] ,			a_=[1, 1, 1] ,			a_=[1, 1, 1] ,			a_=0.02 ,			a_=1E-12 ,			**a_ ,			)				->					Union[str, Any]:


    """simple docstring"""
    super().__init__(**a_  )
    UpperCAmelCase    				=						num_channels
    UpperCAmelCase    				=						patch_sizes
    UpperCAmelCase    				=						patch_stride
    UpperCAmelCase    				=						patch_padding
    UpperCAmelCase    				=						embed_dim
    UpperCAmelCase    				=						num_heads
    UpperCAmelCase    				=						depth
    UpperCAmelCase    				=						mlp_ratio
    UpperCAmelCase    				=						attention_drop_rate
    UpperCAmelCase    				=						drop_rate
    UpperCAmelCase    				=						drop_path_rate
    UpperCAmelCase    				=						qkv_bias
    UpperCAmelCase    				=						cls_token
    UpperCAmelCase    				=						qkv_projection_method
    UpperCAmelCase    				=						kernel_qkv
    UpperCAmelCase    				=						padding_kv
    UpperCAmelCase    				=						stride_kv
    UpperCAmelCase    				=						padding_q
    UpperCAmelCase    				=						stride_q
    UpperCAmelCase    				=						initializer_range
    UpperCAmelCase    				=						layer_norm_eps


