


import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


snake_case__			 =						logging.get_logger(__name__)


class 					lowerCAmelCase_ :
      lowerCamelCase_         =     None






@experimental
def 				lowerCamelCase__	(      a	:   List[Any]	,  a	:   Optional[int]	,  a	:   List[Any]	,  a	:   int	,  a	:   Any	,  a	:   Optional[int]	,  a	:   Union[str, Any]				)    ->	str:





   """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 				lowerCamelCase__	(      a	:   Optional[Any]	,  a	:   Tuple	,  a	:   Tuple	,  a	:   List[Any]	,  a	:   Optional[int]	,  a	:   int	,  a	:   List[Any]				)    ->	Tuple:





   """simple docstring"""
   a__ :Optional[Any]						  =    num_proc if num_proc <= len(__UpperCamelCase				) else len(__UpperCamelCase				)
   a__ :int						  =    []  # We organize the splits ourselve (contiguous splits)
   for index in range(__UpperCamelCase				):
      a__ :List[str]						  =    len(__UpperCamelCase				) // num_proc
      a__ :Optional[Any]						  =    len(__UpperCamelCase				) % num_proc
      a__ :List[Any]						  =    div * index + min(__UpperCamelCase	,  __UpperCamelCase				)
      a__ :List[Any]						  =    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]}'''				)
   a__  ,  a__ :Tuple						  =    None, None
   if not disable_tqdm:
      a__  ,  a__ :Union[str, Any]						  =    (RLock(),), tqdm.set_lock
   with Pool(__UpperCamelCase	,  initargs=__UpperCamelCase	,  initializer=__UpperCamelCase				) as pool:
      a__ :str						  =    pool.map(__UpperCamelCase	,  __UpperCamelCase				)
   logger.info(F'''Finished {num_proc} processes'''				)
   a__ :Optional[int]						  =    [obj for proc_res in mapped for obj in proc_res]
   logger.info(F'''Unpacked {len(__UpperCamelCase				)} objects'''				)

   return mapped


def 				lowerCamelCase__	(      a	:   List[str]	,  a	:   Optional[int]	,  a	:   List[str]	,  a	:   int	,  a	:   Optional[Any]	,  a	:   str	,  a	:   Union[str, Any]				)    ->	Tuple:





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







@experimental
@contextlib.contextmanager
def 				lowerCamelCase__	(      a	:   Optional[int]				)    ->	List[str]:





   """simple docstring"""
   a__ :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:
      a__ :Any						  =    None






from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case__			 =						logging.get_logger(__name__)

snake_case__			 =						{
    '''google/pegasus-large''': '''https://huggingface.co/google/pegasus-large/resolve/main/config.json''',
    # See all PEGASUS models at https://huggingface.co/models?filter=pegasus
}


class 					lowerCAmelCase_ (       _a):
						lowerCamelCase_         =     'pegasus'
						lowerCamelCase_         =     ['past_key_values']
						lowerCamelCase_         =     {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}




						def __init__(	self						:   Any   ,     __A						:   Dict=50265   ,     __A						:   List[Any]=1024   ,     __A						:   int=12   ,     __A						:   Optional[Any]=4096   ,     __A						:   Optional[int]=16   ,     __A						:   Dict=12   ,     __A						:   List[Any]=4096   ,     __A						:   List[str]=16   ,     __A						:   Optional[int]=0.0   ,     __A						:   List[Any]=0.0   ,     __A						:   List[str]=True   ,     __A						:   Optional[int]=True   ,     __A						:   str="gelu"   ,     __A						:   Tuple=1024   ,     __A						:   Any=0.1   ,     __A						:   List[Any]=0.0   ,     __A						:   List[str]=0.0   ,     __A						:   Tuple=0.02   ,     __A						:   Union[str, Any]=0   ,     __A						:   Union[str, Any]=False   ,     __A						:   Optional[Any]=0   ,     __A						:   Tuple=1   ,     __A						:   str=1   ,     **__A						:   Any   ,     )      ->Union[str, Any]:






									"""simple docstring"""






									a__ :Any						  =    vocab_size
									a__ :List[str]						  =    max_position_embeddings
									a__ :int						  =    d_model
									a__ :Union[str, Any]						  =    encoder_ffn_dim
									a__ :List[Any]						  =    encoder_layers
									a__ :Union[str, Any]						  =    encoder_attention_heads
									a__ :Tuple						  =    decoder_ffn_dim
									a__ :List[Any]						  =    decoder_layers
									a__ :Tuple						  =    decoder_attention_heads
									a__ :Optional[int]						  =    dropout
									a__ :str						  =    attention_dropout
									a__ :Optional[int]						  =    activation_dropout
									a__ :str						  =    activation_function
									a__ :Dict						  =    init_std
									a__ :Any						  =    encoder_layerdrop
									a__ :int						  =    decoder_layerdrop
									a__ :Union[str, Any]						  =    use_cache
									a__ :List[Any]						  =    encoder_layers
									a__ :Dict						  =    scale_embedding  # scale factor will be sqrt(d_model) if True
									super().__init__(
									    pad_token_id=__A   ,     eos_token_id=__A   ,     is_encoder_decoder=__A   ,     decoder_start_token_id=__A   ,     forced_eos_token_id=__A   ,     **__A   ,     )




						@property
						def  _snake_case					(	self						:   Optional[Any]						)      ->int:






									"""simple docstring"""






									return self.encoder_attention_heads






						@property
						def  _snake_case					(	self						:   Union[str, Any]						)      ->int:






									"""simple docstring"""






									return self.d_model


