





"""simple docstring"""





import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


_lowerCamelCase    	=      logging.get_logger(__name__)





class      snake_case						:
      lowerCamelCase__         =					None







@experimental
def 						lowerCAmelCase_      (    lowercase_  :				List[Any]					, lowercase_  :				List[Any]					, lowercase_  :				Dict					, lowercase_  :				List[Any]					, lowercase_  :				Dict					, lowercase_  :				Optional[Any]					, lowercase_  :				Optional[Any]						):

 '''simple docstring'''






 if ParallelBackendConfig.backend_name is None:
  return _map_with_multiprocessing_pool(
      lowercase_					, lowercase_					, lowercase_					, lowercase_					, lowercase_					, lowercase_					, lowercase_						)

 return _map_with_joblib(lowercase_					, lowercase_					, lowercase_					, lowercase_					, lowercase_					, lowercase_					, lowercase_						)




def 						lowerCAmelCase_      (    lowercase_  :				str					, lowercase_  :				List[Any]					, lowercase_  :				Optional[int]					, lowercase_  :				Union[str, Any]					, lowercase_  :				List[Any]					, lowercase_  :				List[str]					, lowercase_  :				Tuple						):

 '''simple docstring'''






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

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

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

 return mapped




def 						lowerCAmelCase_      (    lowercase_  :				List[str]					, lowercase_  :				List[Any]					, lowercase_  :				Optional[int]					, lowercase_  :				List[str]					, lowercase_  :				Tuple					, lowercase_  :				Dict					, lowercase_  :				Any						):

 '''simple docstring'''






 import joblib

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






@experimental
@contextlib.contextmanager
def 						lowerCAmelCase_      (    lowercase_  :				str						):

 '''simple docstring'''






 __SCREAMING_SNAKE_CASE					:     Any   				=			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:
  __SCREAMING_SNAKE_CASE					:     Optional[int]   				=			None




'''simple docstring'''



from typing import TYPE_CHECKING

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


_UpperCamelCase						:  Optional[int]							       =				{
    "configuration_vision_text_dual_encoder": ["VisionTextDualEncoderConfig"],
    "processing_vision_text_dual_encoder": ["VisionTextDualEncoderProcessor"],
}


try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _UpperCamelCase						:  Dict							       =				["VisionTextDualEncoderModel"]


try:
     if not is_flax_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _UpperCamelCase						:  int							       =				["FlaxVisionTextDualEncoderModel"]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _UpperCamelCase						:  Tuple							       =				["TFVisionTextDualEncoderModel"]


if TYPE_CHECKING:
     from .configuration_vision_text_dual_encoder import VisionTextDualEncoderConfig
     from .processing_vision_text_dual_encoder import VisionTextDualEncoderProcessor

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_vision_text_dual_encoder import VisionTextDualEncoderModel

     try:
          if not is_flax_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_flax_vision_text_dual_encoder import FlaxVisionTextDualEncoderModel

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_vision_text_dual_encoder import TFVisionTextDualEncoderModel


else:
     import sys

     _UpperCamelCase						:  Optional[int]							       =				_LazyModule(__name__, globals()["__file__"], _import_structure)





