import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


__magic_name__								=logging.get_logger(__name__)



class     _A   :
  SCREAMING_SNAKE_CASE_	:	Tuple						=None







@experimental
def  __UpperCamelCase    (						A ,							A ,							A ,							A ,							A ,							A ,							A							):
     if ParallelBackendConfig.backend_name is None:
          return _map_with_multiprocessing_pool(
              A ,							A ,							A ,							A ,							A ,							A ,							A							)

     return _map_with_joblib(A ,							A ,							A ,							A ,							A ,							A ,							A							)







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

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

     logger.info(
         f"Spawning {num_proc} processes for {len(A							)} 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(A ,							initargs=A ,							initializer=A							) as pool:
          UpperCamelCase__										=      pool.map(A ,							A							)
     logger.info(f"Finished {num_proc} processes"							)
     UpperCamelCase__										=      [obj for proc_res in mapped for obj in proc_res]
     logger.info(f"Unpacked {len(A							)} objects"							)

     return mapped







def  __UpperCamelCase    (						A ,							A ,							A ,							A ,							A ,							A ,							A							):
     # 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=A							):
          return joblib.Parallel()(
              joblib.delayed(A							)((function, obj, types, None, True, None)							) for obj in iterable							)







@experimental
@contextlib.contextmanager
def  __UpperCamelCase    (						A							):
     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 unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
     import torch

     from transformers.activations import gelu_new, gelu_python, get_activation



@require_torch
class     _A   (    unittest.TestCase							):






  def 		_a					(self	)		->      str:





       '''simple docstring'''





       UpperCamelCase__										=      torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]	)
       UpperCamelCase__										=      get_activation('''gelu'''	)
       self.assertTrue(torch.allclose(gelu_python(SCREAMING_SNAKE_CASE_	)   ,				torch_builtin(SCREAMING_SNAKE_CASE_	)	)	)
       self.assertFalse(torch.allclose(gelu_python(SCREAMING_SNAKE_CASE_	)   ,				gelu_new(SCREAMING_SNAKE_CASE_	)	)	)






  def 		_a					(self	)		->      str:





       '''simple docstring'''





       UpperCamelCase__										=      torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]	)
       UpperCamelCase__										=      get_activation('''gelu'''	)
       UpperCamelCase__										=      get_activation('''gelu_10'''	)

       UpperCamelCase__										=      torch_builtin(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      geluaa(SCREAMING_SNAKE_CASE_	)

       UpperCamelCase__										=      torch.where(y_gelu_aa < 10.0   ,				1   ,				0	)

       self.assertTrue(torch.max(SCREAMING_SNAKE_CASE_	).item() == 10.0	)
       self.assertTrue(torch.allclose(y_gelu * clipped_mask   ,				y_gelu_aa * clipped_mask	)	)






  def 		_a					(self	)		->      Optional[Any]:





       '''simple docstring'''





       get_activation('''gelu'''	)
       get_activation('''gelu_10'''	)
       get_activation('''gelu_fast'''	)
       get_activation('''gelu_new'''	)
       get_activation('''gelu_python'''	)
       get_activation('''gelu_pytorch_tanh'''	)
       get_activation('''linear'''	)
       get_activation('''mish'''	)
       get_activation('''quick_gelu'''	)
       get_activation('''relu'''	)
       get_activation('''sigmoid'''	)
       get_activation('''silu'''	)
       get_activation('''swish'''	)
       get_activation('''tanh'''	)
       with self.assertRaises(SCREAMING_SNAKE_CASE_	):
            get_activation('''bogus'''	)
       with self.assertRaises(SCREAMING_SNAKE_CASE_	):
            get_activation(SCREAMING_SNAKE_CASE_	)






  def 		_a					(self	)		->      Optional[Any]:





       '''simple docstring'''





       UpperCamelCase__										=      get_activation('''gelu'''	)
       UpperCamelCase__										=      1
       UpperCamelCase__										=      get_activation('''gelu'''	)
       self.assertEqual(acta.a   ,				1	)
       with self.assertRaises(SCREAMING_SNAKE_CASE_	):
            UpperCamelCase__										=      acta.a



