o
    +if                     @   s   d dl Z d dlmZmZ d dlmZ ddlmZmZ e	e
ZG dd dZedd	 Zd
d Zdd Zee jdefddZdS )    N)PoolRLock)tqdm   )experimentalloggingc                   @   s   e Zd ZdZdS )ParallelBackendConfigN)__name__
__module____qualname__backend_name r   r   S/var/www/html/corbot_env/lib/python3.10/site-packages/datasets/parallel/parallel.pyr      s    r   c	           	   
   C   s:   t jdu rt| ||||||||	S t| ||||||||	S )a  
    **Experimental.** Apply a function to iterable elements in parallel, where the implementation uses either
    multiprocessing.Pool or joblib for parallelization.

    Args:
        function (`Callable[[Any], Any]`): Function to be applied to `iterable`.
        iterable (`list`, `tuple` or `np.ndarray`): Iterable elements to apply function to.
        num_proc (`int`): Number of processes (if no backend specified) or jobs (using joblib).
        types (`tuple`): Additional types (besides `dict` values) to apply `function` recursively to their elements.
        disable_tqdm (`bool`): Whether to disable the tqdm progressbar.
        desc (`str`): Prefix for the tqdm progressbar.
        single_map_nested_func (`Callable`): Map function that applies `function` to an element from `iterable`.
            Takes a tuple of function, data_struct, types, rank, disable_tqdm, desc as input, where data_struct is an
            element of `iterable`, and `rank` is used for progress bar.
    N)r   r   _map_with_multiprocessing_pool_map_with_joblib	functioniterablenum_procbatched
batch_sizetypesdisable_tqdmdescsingle_map_nested_funcr   r   r   parallel_map   s   
r   c	                 C   s  |t |kr|nt |}g }	t|D ]4}
t || }t || }||
 t|
| }|| |
|k r2dnd }|	| ||| ||||
||f qt |tdd |	D krgtdt | dtdd |	D  td| d	t | d
dd |	D   d\}}|st ft	j
}}t|||d}|||	}W d    n1 sw   Y  td| d dd |D }tdt | d |S )N   r   c                 s       | ]	}t |d  V  qdS r   Nlen.0ir   r   r   	<genexpr>7       z1_map_with_multiprocessing_pool.<locals>.<genexpr>zHError dividing inputs iterable among processes. Total number of objects z
, length: c                 s   r   r   r   r!   r   r   r   r$   ;   r%   z	Spawning z processes for z objects in slices of c                 S   s   g | ]}t |d  qS )r   r   r!   r   r   r   
<listcomp>?   s    z2_map_with_multiprocessing_pool.<locals>.<listcomp>)NN)initargsinitializerz	Finished z
 processesc                 S   s   g | ]	}|D ]}|qqS r   r   )r"   proc_resobjr   r   r   r&   G   s    z	Unpacked z objects)r    rangeminappendsum
ValueErrorloggerinfor   r   set_lockr   map)r   r   r   r   r   r   r   r   r   
split_kwdsindexdivmodstartendr'   r(   poolmappedr   r   r   r   +   s:   $"r   c	           	         s`   dd l jtj|d   fdd|D W  d    S 1 s)w   Y  d S )Nr   )n_jobsc              
   3   s.    | ]} | d dd fV  qd S )NT)delayed)r"   r*   r   r   r   joblibr   r   r   r   r$   U   s
    
z#_map_with_joblib.<locals>.<genexpr>)r?   parallel_backendr   r   Parallelr   r   r>   r   r   M   s   $r   r   c                 c   s>    | t _| dkrddlm} |  z	dV  W dt _dS dt _w )a  
    **Experimental.**  Configures the parallel backend for parallelized dataset loading, which uses the parallelization
    implemented by joblib.

    Args:
        backend_name (str): Name of backend for parallelization implementation, has to be supported by joblib.

     Example usage:
     ```py
     with parallel_backend('spark'):
       dataset = load_dataset(..., num_proc=2)
     ```
    sparkr   )register_sparkN)r   r   joblibsparkrC   )r   rC   r   r   r   r@   [   s   r@   )
contextlibmultiprocessingr   r   	tqdm.autor   utilsr   r   
get_loggerr	   r0   r   r   r   r   contextmanagerstrr@   r   r   r   r   <module>   s    

"