


'''simple docstring'''

import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


a_						:       List[Any]      			=				logging.get_logger(__name__)





class   snake_case					:





  """simple docstring"""




  _lowerCamelCase						=						None





@experimental
def __snake_case      (   UpperCAmelCase_		:		Union[str, Any]      ,       UpperCAmelCase_		:		Dict      ,       UpperCAmelCase_		:		Any      ,       UpperCAmelCase_		:		List[str]      ,       UpperCAmelCase_		:		Any      ,       UpperCAmelCase_		:		str      ,       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 __snake_case      (   UpperCAmelCase_		:		int      ,       UpperCAmelCase_		:		str      ,       UpperCAmelCase_		:		Tuple      ,       UpperCAmelCase_		:		List[Any]      ,       UpperCAmelCase_		:		List[Any]      ,       UpperCAmelCase_		:		Any      ,       UpperCAmelCase_		:		List[Any]	):
 lowerCamelCase_  				=						num_proc if num_proc <= len(UpperCAmelCase_	) else len(UpperCAmelCase_	)
 lowerCamelCase_  				=						[]  # We organize the splits ourselve (contiguous splits)
 for index in range(UpperCAmelCase_	):
  lowerCamelCase_  				=						len(UpperCAmelCase_	) // num_proc
  lowerCamelCase_  				=						len(UpperCAmelCase_	) % num_proc
  lowerCamelCase_  				=						div * index + min(UpperCAmelCase_      ,       UpperCAmelCase_	)
  lowerCamelCase_  				=						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_  				=						None, None
 if not disable_tqdm:
  lowerCamelCase_				,lowerCamelCase_  				=						(RLock(),), tqdm.set_lock
 with Pool(UpperCAmelCase_      ,       initargs=UpperCAmelCase_      ,       initializer=UpperCAmelCase_	) as pool:
  lowerCamelCase_  				=						pool.map(UpperCAmelCase_      ,       UpperCAmelCase_	)
 logger.info(F'''Finished {num_proc} processes'''	)
 lowerCamelCase_  				=						[obj for proc_res in mapped for obj in proc_res]
 logger.info(F'''Unpacked {len(UpperCAmelCase_	)} objects'''	)

 return mapped





def __snake_case      (   UpperCAmelCase_		:		Optional[int]      ,       UpperCAmelCase_		:		str      ,       UpperCAmelCase_		:		str      ,       UpperCAmelCase_		:		List[str]      ,       UpperCAmelCase_		:		Dict      ,       UpperCAmelCase_		:		Optional[Any]      ,       UpperCAmelCase_		:		Dict	):
 # 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 __snake_case      (   UpperCAmelCase_		:		str	):
 lowerCamelCase_  				=						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_  				=						None







'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


a_						:       List[Any]      			=				{
    """configuration_owlvit""": [
        """OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """OwlViTConfig""",
        """OwlViTOnnxConfig""",
        """OwlViTTextConfig""",
        """OwlViTVisionConfig""",
    ],
    """processing_owlvit""": ["""OwlViTProcessor"""],
}


try:
     if not is_vision_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     a_						:       int      			=				["""OwlViTFeatureExtractor"""]
     a_						:       Any      			=				["""OwlViTImageProcessor"""]

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     a_						:       Tuple      			=				[
         """OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST""",
         """OwlViTModel""",
         """OwlViTPreTrainedModel""",
         """OwlViTTextModel""",
         """OwlViTVisionModel""",
         """OwlViTForObjectDetection""",
     ]

if TYPE_CHECKING:
     from .configuration_owlvit import (
         OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP,
         OwlViTConfig,
         OwlViTOnnxConfig,
         OwlViTTextConfig,
         OwlViTVisionConfig,
     )
     from .processing_owlvit import OwlViTProcessor

     try:
          if not is_vision_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .feature_extraction_owlvit import OwlViTFeatureExtractor
          from .image_processing_owlvit import OwlViTImageProcessor

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_owlvit import (
              OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
              OwlViTForObjectDetection,
              OwlViTModel,
              OwlViTPreTrainedModel,
              OwlViTTextModel,
              OwlViTVisionModel,
          )

else:
     import sys

     a_						:       Tuple      			=				_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



