


import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


_A		       =						logging.get_logger(__name__)






class     UpperCAmelCase__       :



    """simple docstring"""






    UpperCAmelCase__						:		Any      =  None

@experimental
def 						_UpperCAmelCase				(			SCREAMING_SNAKE_CASE__			:					Any			,     SCREAMING_SNAKE_CASE__			:					Optional[int]			,     SCREAMING_SNAKE_CASE__			:					List[Any]			,     SCREAMING_SNAKE_CASE__			:					Optional[Any]			,     SCREAMING_SNAKE_CASE__			:					Any			,     SCREAMING_SNAKE_CASE__			:					List[Any]			,     SCREAMING_SNAKE_CASE__			:					Optional[int]				):
     if ParallelBackendConfig.backend_name is None:
          return _map_with_multiprocessing_pool(
              SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__				)

     return _map_with_joblib(SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__				)

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

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

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

     return mapped

def 						_UpperCAmelCase				(			SCREAMING_SNAKE_CASE__			:					List[Any]			,     SCREAMING_SNAKE_CASE__			:					Optional[Any]			,     SCREAMING_SNAKE_CASE__			:					Any			,     SCREAMING_SNAKE_CASE__			:					str			,     SCREAMING_SNAKE_CASE__			:					Optional[int]			,     SCREAMING_SNAKE_CASE__			:					Union[str, Any]			,     SCREAMING_SNAKE_CASE__			:					Optional[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=SCREAMING_SNAKE_CASE__				):
          return joblib.Parallel()(
              joblib.delayed(SCREAMING_SNAKE_CASE__				)((function, obj, types, None, True, None)				) for obj in iterable				)





@experimental
@contextlib.contextmanager
def 						_UpperCAmelCase				(			SCREAMING_SNAKE_CASE__			:					str				):
     __UpperCamelCase    							=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:
          __UpperCamelCase    							=None









from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_A		       =						{
    'configuration_jukebox': [
        'JUKEBOX_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'JukeboxConfig',
        'JukeboxPriorConfig',
        'JukeboxVQVAEConfig',
    ],
    'tokenization_jukebox': ['JukeboxTokenizer'],
}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _A		       =						[
       'JUKEBOX_PRETRAINED_MODEL_ARCHIVE_LIST',
       'JukeboxModel',
       'JukeboxPreTrainedModel',
       'JukeboxVQVAE',
       'JukeboxPrior',
   ]

if TYPE_CHECKING:
   from .configuration_jukebox import (
       JUKEBOX_PRETRAINED_CONFIG_ARCHIVE_MAP,
       JukeboxConfig,
       JukeboxPriorConfig,
       JukeboxVQVAEConfig,
   )
   from .tokenization_jukebox import JukeboxTokenizer

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_jukebox import (
          JUKEBOX_PRETRAINED_MODEL_ARCHIVE_LIST,
          JukeboxModel,
          JukeboxPreTrainedModel,
          JukeboxPrior,
          JukeboxVQVAE,
      )

else:
   import sys

   _A		       =						_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





