



"""simple docstring"""


import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


__A :				Any					    = logging.get_logger(__name__)
class       _a			:
			"""simple docstring"""
			UpperCamelCase__         =						None







@experimental
def 			lowercase						(   _SCREAMING_SNAKE_CASE :    Any       ,  _SCREAMING_SNAKE_CASE :    Any       ,  _SCREAMING_SNAKE_CASE :    str       ,  _SCREAMING_SNAKE_CASE :    Optional[int]       ,  _SCREAMING_SNAKE_CASE :    Optional[Any]       ,  _SCREAMING_SNAKE_CASE :    Dict       ,  _SCREAMING_SNAKE_CASE :    Tuple					):






				'''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 			lowercase						(   _SCREAMING_SNAKE_CASE :    Dict       ,  _SCREAMING_SNAKE_CASE :    List[Any]       ,  _SCREAMING_SNAKE_CASE :    Tuple       ,  _SCREAMING_SNAKE_CASE :    str       ,  _SCREAMING_SNAKE_CASE :    Optional[Any]       ,  _SCREAMING_SNAKE_CASE :    List[Any]       ,  _SCREAMING_SNAKE_CASE :    int					):






				'''simple docstring'''



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

				return mapped







def 			lowercase						(   _SCREAMING_SNAKE_CASE :    Optional[Any]       ,  _SCREAMING_SNAKE_CASE :    int       ,  _SCREAMING_SNAKE_CASE :    Union[str, Any]       ,  _SCREAMING_SNAKE_CASE :    Optional[Any]       ,  _SCREAMING_SNAKE_CASE :    Dict       ,  _SCREAMING_SNAKE_CASE :    Union[str, Any]       ,  _SCREAMING_SNAKE_CASE :    Any					):






				'''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 			lowercase						(   _SCREAMING_SNAKE_CASE :    str					):






				'''simple docstring'''



				_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







import warnings

from ...utils import is_sklearn_available, requires_backends


if is_sklearn_available():
     from scipy.stats import pearsonr, spearmanr
     from sklearn.metrics import fa_score, matthews_corrcoef


snake_case							:       List[str]										=	(
    '''This metric will be removed from the library soon, metrics should be handled with the 🤗 Evaluate '''
    '''library. You can have a look at this example script for pointers: '''
    '''https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py'''
)
def 							__lowerCamelCase		(					UpperCAmelCase_		:						List[str]					,    UpperCAmelCase_		:						Optional[Any]		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      return (preds == labels).mean()
def 							__lowerCamelCase		(					UpperCAmelCase_		:						str					,    UpperCAmelCase_		:						int		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      a		:Dict									=			simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)
      a		:Any									=			fa_score(y_true=UpperCAmelCase_					,    y_pred=UpperCAmelCase_		)
      return {
          "acc": acc,
          "f1": fa,
          "acc_and_f1": (acc + fa) / 2,
      }
def 							__lowerCamelCase		(					UpperCAmelCase_		:						Dict					,    UpperCAmelCase_		:						List[Any]		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      a		:str									=			pearsonr(UpperCAmelCase_					,    UpperCAmelCase_		)[0]
      a		:Union[str, Any]									=			spearmanr(UpperCAmelCase_					,    UpperCAmelCase_		)[0]
      return {
          "pearson": pearson_corr,
          "spearmanr": spearman_corr,
          "corr": (pearson_corr + spearman_corr) / 2,
      }
def 							__lowerCamelCase		(					UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						Dict		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      assert len(UpperCAmelCase_		) == len(UpperCAmelCase_		), F'''Predictions and labels have mismatched lengths {len(UpperCAmelCase_		)} and {len(UpperCAmelCase_		)}'''
      if task_name == "cola":
            return {"mcc": matthews_corrcoef(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "sst-2":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "mrpc":
            return acc_and_fa(UpperCAmelCase_					,    UpperCAmelCase_		)
      elif task_name == "sts-b":
            return pearson_and_spearman(UpperCAmelCase_					,    UpperCAmelCase_		)
      elif task_name == "qqp":
            return acc_and_fa(UpperCAmelCase_					,    UpperCAmelCase_		)
      elif task_name == "mnli":
            return {"mnli/acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "mnli-mm":
            return {"mnli-mm/acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "qnli":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "rte":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "wnli":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "hans":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      else:
            raise KeyError(UpperCAmelCase_		)






def 							__lowerCamelCase		(					UpperCAmelCase_		:						List[str]					,    UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						List[Any]		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      if len(UpperCAmelCase_		) != len(UpperCAmelCase_		):
            raise ValueError(F'''Predictions and labels have mismatched lengths {len(UpperCAmelCase_		)} and {len(UpperCAmelCase_		)}'''		)
      if task_name == "xnli":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      else:
            raise KeyError(UpperCAmelCase_		)





