'''simple docstring'''





import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


_lowerCAmelCase	:int						=     logging.get_logger(__name__)






class 					UpperCAmelCase			:




    '''simple docstring'''







    snake_case__				: Dict						    =    None






@experimental
def      __lowerCAmelCase    (							a_							,   a_							,   a_							,   a_							,   a_							,   a_							,   a_		)   ->						Union[str, Any]:





     '''simple docstring'''





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

     return _map_with_joblib(lowerCamelCase_							,   lowerCamelCase_							,   lowerCamelCase_							,   lowerCamelCase_							,   lowerCamelCase_							,   lowerCamelCase_							,   lowerCamelCase_		)


def      __lowerCAmelCase    (							a_							,   a_							,   a_							,   a_							,   a_							,   a_							,   a_		)   ->						Optional[int]:





     '''simple docstring'''





     SCREAMING_SNAKE_CASE       :      Optional[Any]     						=							num_proc if num_proc <= len(lowerCamelCase_		) else len(lowerCamelCase_		)
     SCREAMING_SNAKE_CASE       :      int     						=							[]  # We organize the splits ourselve (contiguous splits)
     for index in range(lowerCamelCase_		):
          SCREAMING_SNAKE_CASE       :      Any     						=							len(lowerCamelCase_		) // num_proc
          SCREAMING_SNAKE_CASE       :      List[Any]     						=							len(lowerCamelCase_		) % num_proc
          SCREAMING_SNAKE_CASE       :      Dict     						=							div * index + min(lowerCamelCase_							,   lowerCamelCase_		)
          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(lowerCamelCase_		) != sum(len(i[1]		) for i in split_kwds		):
          raise ValueError(
              f"""Error dividing inputs iterable among processes. """
              f"""Total number of objects {len(lowerCamelCase_		)}, """
              f"""length: {sum(len(i[1]		) for i in split_kwds		)}"""		)

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

     return mapped


def      __lowerCAmelCase    (							a_							,   a_							,   a_							,   a_							,   a_							,   a_							,   a_		)   ->						List[str]:





     '''simple docstring'''





     import joblib

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


@experimental
@contextlib.contextmanager
def      __lowerCAmelCase    (							a_		)   ->						Optional[Any]:





     '''simple docstring'''





     SCREAMING_SNAKE_CASE       :      List[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[Any]     						=							None











import importlib.metadata
import operator
import re
import sys
from typing import Optional

from packaging import version


A__      :     Tuple       =					{
    '<': operator.lt,
    '<=': operator.le,
    '==': operator.eq,
    '!=': operator.ne,
    '>=': operator.ge,
    '>': operator.gt,
}






def     a     (		lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_							):






				'''simple docstring'''
				if got_ver is None or want_ver is None:
								raise ValueError(
								    F"""Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider"""
								    F""" reinstalling {pkg}."""							)
				if not ops[op](version.parse(lowerCamelCase_							)						,  version.parse(lowerCamelCase_							)							):
								raise ImportError(
								    F"""{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}"""							)






def     a     (		lowerCamelCase_						,  lowerCamelCase_ = None							):






				'''simple docstring'''
				lowercase__  					=     F"""\n{hint}""" if hint is not None else ''''''

				# non-versioned check
				if re.match(r'''^[\w_\-\d]+$'''						,  lowerCamelCase_							):
								lowercase__					,		lowercase__					,		lowercase__  					=     requirement, None, None
				else:
								lowercase__  					=     re.findall(r'''^([^!=<>\s]+)([\s!=<>]{1,2}.+)'''						,  lowerCamelCase_							)
								if not match:
												raise ValueError(
												    '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but'''
												    F""" got {requirement}"""							)
								lowercase__					,		lowercase__  					=     match[0]
								lowercase__  					=     want_full.split(''','''							)  # there could be multiple requirements
								lowercase__  					=     {}
								for w in want_range:
												lowercase__  					=     re.findall(r'''^([\s!=<>]{1,2})(.+)'''						,  lowerCamelCase_							)
												if not match:
																raise ValueError(
																    '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,'''
																    F""" but got {requirement}"""							)
												lowercase__					,		lowercase__  					=     match[0]
												lowercase__  					=     want_ver
												if op not in ops:
																raise ValueError(F"""{requirement}: need one of {list(ops.keys()							)}, but got {op}"""							)

    # special case
				if pkg == "python":
								lowercase__  					=     '''.'''.join([str(lowerCamelCase_							) for x in sys.version_info[:3]]							)
								for op, want_ver in wanted.items():
												_compare_versions(lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_							)
								return

				# check if any version is installed
				try:
								lowercase__  					=     importlib.metadata.version(lowerCamelCase_							)
				except importlib.metadata.PackageNotFoundError:
								raise importlib.metadata.PackageNotFoundError(
								    F"""The '{requirement}' distribution was not found and is required by this application. {hint}"""							)

				# check that the right version is installed if version number or a range was provided
				if want_ver is not None:
								for op, want_ver in wanted.items():
												_compare_versions(lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_							)






def     a     (		lowerCamelCase_							):






				'''simple docstring'''
				lowercase__  					=     '''Try: pip install transformers -U or pip install -e \'.[dev]\' if you\'re working with git main'''
				return require_version(lowerCamelCase_						,  lowerCamelCase_							)




