


"""simple docstring"""

import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


UpperCAmelCase__  							=logging.get_logger(__name__)





class    lowerCamelCase__						:
      a :     int											=		None






@experimental
def lowerCAmelCase_		(				UpperCamelCase__	: Optional[Any]				,   UpperCamelCase__	: Dict				,   UpperCamelCase__	: Optional[int]				,   UpperCamelCase__	: Optional[Any]				,   UpperCamelCase__	: str				,   UpperCamelCase__	: int				,   UpperCamelCase__	: Optional[Any] ):






 """simple docstring"""







 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 lowerCAmelCase_		(				UpperCamelCase__	: str				,   UpperCamelCase__	: Union[str, Any]				,   UpperCamelCase__	: Dict				,   UpperCamelCase__	: Optional[Any]				,   UpperCamelCase__	: int				,   UpperCamelCase__	: Tuple				,   UpperCamelCase__	: Optional[int] ):






 """simple docstring"""







 __lowercase									=	num_proc if num_proc <= len(UpperCamelCase__ ) else len(UpperCamelCase__ )
 __lowercase									=	[]  # We organize the splits ourselve (contiguous splits)
 for index in range(UpperCamelCase__ ):
  __lowercase									=	len(UpperCamelCase__ ) // num_proc
  __lowercase									=	len(UpperCamelCase__ ) % num_proc
  __lowercase									=	div * index + min(UpperCamelCase__				,   UpperCamelCase__ )
  __lowercase									=	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]}''' )
 __lowercase							,		__lowercase									=	None, None
 if not disable_tqdm:
  __lowercase							,		__lowercase									=	(RLock(),), tqdm.set_lock
 with Pool(UpperCamelCase__				,   initargs=UpperCamelCase__				,   initializer=UpperCamelCase__ ) as pool:
  __lowercase									=	pool.map(UpperCamelCase__				,   UpperCamelCase__ )
 logger.info(f'''Finished {num_proc} processes''' )
 __lowercase									=	[obj for proc_res in mapped for obj in proc_res]
 logger.info(f'''Unpacked {len(UpperCamelCase__ )} objects''' )

 return mapped


def lowerCAmelCase_		(				UpperCamelCase__	: List[Any]				,   UpperCamelCase__	: Union[str, Any]				,   UpperCamelCase__	: int				,   UpperCamelCase__	: Dict				,   UpperCamelCase__	: int				,   UpperCamelCase__	: Dict				,   UpperCamelCase__	: List[str] ):






 """simple docstring"""







 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 lowerCAmelCase_		(				UpperCamelCase__	: str ):






 """simple docstring"""







 __lowercase									=	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:
  __lowercase									=	None




"""simple docstring"""

from typing import TYPE_CHECKING

from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


UpperCAmelCase__  							={"configuration_van": ["VAN_PRETRAINED_CONFIG_ARCHIVE_MAP", "VanConfig"]}


try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       UpperCAmelCase__  							=[
           "VAN_PRETRAINED_MODEL_ARCHIVE_LIST",
           "VanForImageClassification",
           "VanModel",
           "VanPreTrainedModel",
       ]

if TYPE_CHECKING:
       from .configuration_van import VAN_PRETRAINED_CONFIG_ARCHIVE_MAP, VanConfig

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_van import (
                  VAN_PRETRAINED_MODEL_ARCHIVE_LIST,
                  VanForImageClassification,
                  VanModel,
                  VanPreTrainedModel,
              )

else:
       import sys

       UpperCAmelCase__  							=_LazyModule(__name__, globals()["__file__"], _import_structure)
