





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


_UpperCAmelCase       : Dict			     =       logging.get_logger(__name__)

_UpperCAmelCase       : Optional[int]			     =       {
    "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      lowerCAmelCase							(      _SCREAMING_SNAKE_CASE    ):
 UpperCAmelCase__									=	"cvt"
 def __init__(   self						:   str    ,  UpperCAmelCase						:   str=3    ,  UpperCAmelCase						:   Any=[7, 3, 3]    ,  UpperCAmelCase						:   Union[str, Any]=[4, 2, 2]    ,  UpperCAmelCase						:   List[str]=[2, 1, 1]    ,  UpperCAmelCase						:   str=[64, 192, 384]    ,  UpperCAmelCase						:   Optional[Any]=[1, 3, 6]    ,  UpperCAmelCase						:   Dict=[1, 2, 10]    ,  UpperCAmelCase						:   Optional[int]=[4.0, 4.0, 4.0]    ,  UpperCAmelCase						:   Optional[Any]=[0.0, 0.0, 0.0]    ,  UpperCAmelCase						:   Optional[Any]=[0.0, 0.0, 0.0]    ,  UpperCAmelCase						:   int=[0.0, 0.0, 0.1]    ,  UpperCAmelCase						:   Any=[True, True, True]    ,  UpperCAmelCase						:   List[str]=[False, False, True]    ,  UpperCAmelCase						:   List[Any]=["dw_bn", "dw_bn", "dw_bn"]    ,  UpperCAmelCase						:   Optional[Any]=[3, 3, 3]    ,  UpperCAmelCase						:   Optional[int]=[1, 1, 1]    ,  UpperCAmelCase						:   List[Any]=[2, 2, 2]    ,  UpperCAmelCase						:   int=[1, 1, 1]    ,  UpperCAmelCase						:   str=[1, 1, 1]    ,  UpperCAmelCase						:   str=0.0_2    ,  UpperCAmelCase						:   Tuple=1e-12    ,  **UpperCAmelCase						:   str    ,  )    ->			List[Any]:
   super().__init__(**A_			)
   lowerCamelCase__					:   str      				=	num_channels
   lowerCamelCase__					:   List[Any]      				=	patch_sizes
   lowerCamelCase__					:   List[str]      				=	patch_stride
   lowerCamelCase__					:   Union[str, Any]      				=	patch_padding
   lowerCamelCase__					:   Union[str, Any]      				=	embed_dim
   lowerCamelCase__					:   Dict      				=	num_heads
   lowerCamelCase__					:   Any      				=	depth
   lowerCamelCase__					:   List[str]      				=	mlp_ratio
   lowerCamelCase__					:   Union[str, Any]      				=	attention_drop_rate
   lowerCamelCase__					:   Optional[Any]      				=	drop_rate
   lowerCamelCase__					:   Optional[Any]      				=	drop_path_rate
   lowerCamelCase__					:   Union[str, Any]      				=	qkv_bias
   lowerCamelCase__					:   str      				=	cls_token
   lowerCamelCase__					:   List[str]      				=	qkv_projection_method
   lowerCamelCase__					:   Union[str, Any]      				=	kernel_qkv
   lowerCamelCase__					:   Tuple      				=	padding_kv
   lowerCamelCase__					:   List[str]      				=	stride_kv
   lowerCamelCase__					:   int      				=	padding_q
   lowerCamelCase__					:   List[str]      				=	stride_q
   lowerCamelCase__					:   Tuple      				=	initializer_range
   lowerCamelCase__					:   List[str]      				=	layer_norm_eps












import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


_UpperCAmelCase       : Union[str, Any]			     =       logging.get_logger(__name__)

class      lowerCAmelCase							:
 UpperCAmelCase__									=	None


@experimental
def  SCREAMING_SNAKE_CASE   ( _UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase				)    ->  Any:
    if ParallelBackendConfig.backend_name is None:
        return _map_with_multiprocessing_pool(
            _UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase				)

    return _map_with_joblib(_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase				)


def  SCREAMING_SNAKE_CASE   ( _UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase				)    ->  Any:
    lowerCamelCase__					:   int      				=	num_proc if num_proc <= len(_UpperCAmelCase				) else len(_UpperCAmelCase				)
    lowerCamelCase__					:   Optional[int]      				=	[]  # We organize the splits ourselve (contiguous splits)
    for index in range(_UpperCAmelCase				):
        lowerCamelCase__					:   str      				=	len(_UpperCAmelCase				) // num_proc
        lowerCamelCase__					:   List[str]      				=	len(_UpperCAmelCase				) % num_proc
        lowerCamelCase__					:   Tuple      				=	div * index + min(_UpperCAmelCase		,			_UpperCAmelCase				)
        lowerCamelCase__					:   int      				=	start + div + (1 if index < mod else 0)
        split_kwds.append((function, iterable[start:end], types, index, disable_tqdm, desc)				)

    if len(_UpperCAmelCase				) != sum(len(i[1]				) for i in split_kwds				):
        raise ValueError(
            F"""Error dividing inputs iterable among processes. """
            F"""Total number of objects {len(_UpperCAmelCase				)}, """
            F"""length: {sum(len(i[1]				) for i in split_kwds				)}"""				)

    logger.info(
        F"""Spawning {num_proc} processes for {len(_UpperCAmelCase				)} objects in slices of {[len(i[1]				) for i in split_kwds]}"""				)
    lowerCamelCase__				,							lowerCamelCase__					:   List[str]      				=	None, None
    if not disable_tqdm:
        lowerCamelCase__				,							lowerCamelCase__					:   Union[str, Any]      				=	(RLock(),), tqdm.set_lock
    with Pool(_UpperCAmelCase		,			initargs=_UpperCAmelCase		,			initializer=_UpperCAmelCase				) as pool:
        lowerCamelCase__					:   Any      				=	pool.map(_UpperCAmelCase		,			_UpperCAmelCase				)
    logger.info(F"""Finished {num_proc} processes"""				)
    lowerCamelCase__					:   Union[str, Any]      				=	[obj for proc_res in mapped for obj in proc_res]
    logger.info(F"""Unpacked {len(_UpperCAmelCase				)} objects"""				)

    return mapped


def  SCREAMING_SNAKE_CASE   ( _UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase		,			_UpperCAmelCase				)    ->  int:
    # progress bar is not yet supported for _map_with_joblib, because tqdm couldn't accurately be applied to joblib,
    # and it requires monkey-patching joblib internal classes which is subject to change
    import joblib

    with joblib.parallel_backend(ParallelBackendConfig.backend_name		,			n_jobs=_UpperCAmelCase				):
        return joblib.Parallel()(
            joblib.delayed(_UpperCAmelCase				)((function, obj, types, None, True, None)				) for obj in iterable				)


@experimental
@contextlib.contextmanager
def  SCREAMING_SNAKE_CASE   ( _UpperCAmelCase				)    ->  Tuple:
    lowerCamelCase__					:   List[str]      				=	backend_name

    if backend_name == "spark":
        from joblibspark import register_spark

        register_spark()

        # TODO: call create_cache_and_write_probe if "download" in steps
        # TODO: raise NotImplementedError when Dataset.map etc is called

    try:
        yield
    finally:
        lowerCamelCase__					:   str      				=	None





