





import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


snake_case 				=       logging.get_logger(__name__)





class 				A_				:







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : Dict      =  None

@experimental
def 							SCREAMING_SNAKE_CASE__       (  snake_case__  :Union[str, Any]			,							snake_case__  :Union[str, Any]			,							snake_case__  :Any			,							snake_case__  :List[str]			,							snake_case__  :Optional[Any]			,							snake_case__  :Union[str, Any]			,							snake_case__  :Dict     )      ->						Tuple:
  if ParallelBackendConfig.backend_name is None:
    return _map_with_multiprocessing_pool(
        snake_case__			,							snake_case__			,							snake_case__			,							snake_case__			,							snake_case__			,							snake_case__			,							snake_case__     )

  return _map_with_joblib(snake_case__			,							snake_case__			,							snake_case__			,							snake_case__			,							snake_case__			,							snake_case__			,							snake_case__     )

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

  if len(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(snake_case__     )}, """
        F"""length: {sum(len(i[1]     ) for i in split_kwds     )}"""     )

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

  return mapped

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







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






import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
      import torch

      from transformers.generation import DisjunctiveConstraint





@require_torch
class 				A_				(      unittest.TestCase	):







       """simple docstring"""





       def    __UpperCAmelCase (    self	:	Union[str, Any]  )						->					List[Any]:
         # For consistency across different places the DisjunctiveConstraint is called,
         # dc.token_ids is a list of integers. It is also initialized only by integers.

         _lowercase							= [[1, 2, 4], [1, 2, 3, 4]]
         _lowercase							= DisjunctiveConstraint(__A  )
         self.assertTrue(isinstance(dc.token_ids						,__A  )  )

         with self.assertRaises(__A  ):
           DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]]  )  )

         with self.assertRaises(__A  ):
           DisjunctiveConstraint([torch.LongTensor([1, 2, 4]  ), torch.LongTensor([1, 2, 3, 4, 5]  )]  )







       def    __UpperCAmelCase (    self	:	List[Any]  )						->					str:
         # We can't have constraints that are complete subsets of another. This leads to a preverse
         # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint?
         # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially
         # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm
         # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it).
         _lowercase							= [[1, 2], [1, 2, 3, 4]]

         with self.assertRaises(__A  ):
           DisjunctiveConstraint(__A  )  # fails here







       def    __UpperCAmelCase (    self	:	Dict  )						->					Optional[Any]:
         _lowercase							= [[1, 2, 3], [1, 2, 4]]

         _lowercase							= DisjunctiveConstraint(__A  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(1  )
         _lowercase							= stepped is True and completed is False and reset is False
         self.assertTrue(__A  )
         self.assertTrue(not dc.completed  )
         self.assertTrue(dc.current_seq == [1]  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(2  )
         _lowercase							= stepped is True and completed is False and reset is False
         self.assertTrue(__A  )
         self.assertTrue(not dc.completed  )
         self.assertTrue(dc.current_seq == [1, 2]  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(3  )
         _lowercase							= stepped is True and completed is True and reset is False
         self.assertTrue(__A  )
         self.assertTrue(dc.completed  )  # Completed!
         self.assertTrue(dc.current_seq == [1, 2, 3]  )







       def    __UpperCAmelCase (    self	:	List[Any]  )						->					Any:
         _lowercase							= [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

         _lowercase							= DisjunctiveConstraint(__A  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(1  )
         self.assertTrue(not dc.completed  )
         self.assertTrue(dc.current_seq == [1]  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(2  )
         self.assertTrue(not dc.completed  )
         self.assertTrue(dc.current_seq == [1, 2]  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(4  )
         self.assertTrue(not dc.completed  )
         self.assertTrue(dc.current_seq == [1, 2, 4]  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(5  )
         self.assertTrue(dc.completed  )  # Completed!
         self.assertTrue(dc.current_seq == [1, 2, 4, 5]  )

         dc.reset()

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(1  )
         self.assertTrue(not dc.completed  )
         self.assertTrue(dc.remaining() == 3  )
         self.assertTrue(dc.current_seq == [1]  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(2  )
         self.assertTrue(not dc.completed  )
         self.assertTrue(dc.remaining() == 2  )
         self.assertTrue(dc.current_seq == [1, 2]  )

         _lowercase						,		_lowercase						,		_lowercase							= dc.update(5  )
         self.assertTrue(dc.completed  )  # Completed!
         self.assertTrue(dc.remaining() == 0  )
         self.assertTrue(dc.current_seq == [1, 2, 5]  )