o �J�h^p�@s6dZddlZddlZddlZddlZddlZddlmZmZddl m Z ddl m Z m Z e durPddlmZddlmZdd lmZmZdd lmZdd lmZGd d �d ed�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�dee�ZGdd�deee�ZGdd�dee�Z Gdd�de!�Z"dd�Z#dS)z, Backends for embarrassingly parallel code. �N)�ABCMeta�abstractmethod�)�mp)�*_retrieve_traceback_capturing_wrapped_call�_TracebackCapturingWrapper)� ThreadPool)�get_memmapping_executor)� cpu_count�process_executor)�ShutdownExecutorError)�MemmappingPoolcs�eZdZdZdZdZdZedd��Zedd��Z dZ d.�fd d � Z gd �Z d Z ed d��Zd/dd�Zd/dd�Zdd�Zd/dd�Z d0dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd1d$d%�Zd&d'�Zd(d)�Zejd*d+��Ze d,d-��Z!�Z"S)2�ParallelBackendBasezEHelper abc which defines all methods a ParallelBackend must implementrFcC�|jS�N��supports_retrieve_callback��self�r�WC:\pinokio\api\whisper-webui.git\app\env\lib\site-packages\joblib\_parallel_backends.py�supports_return_generator)�z-ParallelBackendBase.supports_return_generatorcCrrrrrrr�supports_timeout-rz$ParallelBackendBase.supports_timeoutNc s t���||_||_||_dSr)�super�__init__� nesting_level�inner_max_num_threads�backend_kwargs)rrrr�� __class__rrr3s  zParallelBackendBase.__init__)ZOMP_NUM_THREADSZOPENBLAS_NUM_THREADSZMKL_NUM_THREADSZBLIS_NUM_THREADSZVECLIB_MAXIMUM_THREADS�NUMBA_NUM_THREADSZNUMEXPR_NUM_THREADSZ ENABLE_IPCcC�dS)a�Determine the number of jobs that can actually run in parallel n_jobs is the number of workers requested by the callers. Passing n_jobs=-1 means requesting all available workers for instance matching the number of CPU cores on the worker host(s). This method should return a guesstimate of the number of workers that can actually perform work concurrently. The primary use case is to make it possible for the caller to know in how many chunks to slice the work. In general working on larger data chunks is more efficient (less scheduling overhead and better use of CPU cache prefetching heuristics) as long as all the workers have enough work to do. Nr�r�n_jobsrrr�effective_n_jobsG�z$ParallelBackendBase.effective_n_jobscC�td��)z'Deprecated: implement `submit` instead.zImplement `submit` instead.)�NotImplementedError�r�func�callbackrrr� apply_asyncY�zParallelBackendBase.apply_asynccCst�dt�|�||�S)a8Schedule a function to be run and return a future-like object. This method should return a future-like object that allow tracking the progress of the task. If ``supports_retrieve_callback`` is False, the return value of this method is passed to ``retrieve_result`` instead of calling ``retrieve_result_callback``. Parameters ---------- func: callable The function to be run in parallel. callback: callable A callable that will be called when the task is completed. This callable is a wrapper around ``retrieve_result_callback``. This should be added to the future-like object returned by this method, so that the callback is called when the task is completed. For future-like backends, this can be achieved with something like ``future.add_done_callback(callback)``. Returns ------- future: future-like A future-like object to track the execution of the submitted function. z@`apply_async` is deprecated, implement and use `submit` instead.)�warnings�warn�DeprecationWarningr,r)rrr�submit]s � zParallelBackendBase.submitcCr")a�Called within the callback function passed to `submit`. This method can customise how the result of the function is retrieved from the future-like object. Parameters ---------- future: future-like The future-like object returned by the `submit` method. Returns ------- result: object The result of the function executed in parallel. Nr�r�outrrr�retrieve_result_callback�r&z,ParallelBackendBase.retrieve_result_callbackcCs|jr |j|d�S|��S)aHook to retrieve the result when support_retrieve_callback=False. The argument `out` is the result of the `submit` call. This method should return the result of the computation or raise an exception if the computation failed. )�timeout)r�get)rr3r5rrr�retrieve_result�s z#ParallelBackendBase.retrieve_resultcKs||_|�|�S)z�Reconfigure the backend and return the number of workers. This makes it possible to reuse an existing backend instance for successive independent calls to Parallel with different parameters. )�parallelr%)rr$r8�prefer�requirerrrr� configure�s zParallelBackendBase.configurecCr")z;Call-back method called at the beginning of a Parallel callNrrrrr� start_call�r&zParallelBackendBase.start_callcCr")z5Call-back method called at the end of a Parallel callNrrrrr� stop_call�r&zParallelBackendBase.stop_callcCr")z0Shutdown the workers and free the shared memory.Nrrrrr� terminate�r&zParallelBackendBase.terminatecCr")� Determine the optimal batch sizerrrrrr�compute_batch_size�sz&ParallelBackendBase.compute_batch_sizecCr")�1Callback indicate how long it took to run a batchNr)r� batch_size�durationrrr�batch_completed�r&z#ParallelBackendBase.batch_completedTcCr")auAbort any running tasks This is called when an exception has been raised when executing a task and all the remaining tasks will be ignored and can therefore be aborted to spare computation resources. If ensure_ready is True, the backend should be left in an operating state as future tasks might be re-submitted via that same backend instance. If ensure_ready is False, the implementer of this method can decide to leave the backend in a closed / terminated state as no new task are expected to be submitted to this backend. Setting ensure_ready to False is an optimization that can be leveraged when aborting tasks via killing processes from a local process pool managed by the backend it-self: if we expect no new tasks, there is no point in re-creating new workers. Nr�rZ ensure_readyrrr�abort_everything�sz$ParallelBackendBase.abort_everythingcCs4t|dd�d}|dkrt|d�dfSt|d�dfS)z�Backend instance to be used by nested Parallel calls. By default a thread-based backend is used for the first level of nesting. Beyond, switch to sequential backend to avoid spawning too many threads on the host. rrr�rN)�getattr�SequentialBackend�ThreadingBackend)rrrrr�get_nested_backend�sz&ParallelBackendBase.get_nested_backendcCsh|j}tt�|d�}i}|jD]}|durtj�||�}n|}t|�||<q|jtjvr2d||j<|S)afReturn environment variables limiting threadpools in external libs. This function return a dict containing environment variables to pass when creating a pool of process. These environment variables limit the number of threads to `n_threads` for OpenMP, MKL, Accelerated and OpenBLAS libraries in the child processes. rN�1) r�maxr �MAX_NUM_THREADS_VARS�os�environr6�str�TBB_ENABLE_IPC_VAR)rr$Zexplicit_n_threadsZdefault_n_threads�env�var� var_valuerrr�_prepare_worker_env�s   z'ParallelBackendBase._prepare_worker_envccs �dVdS)a�Context manager to manage an execution context. Calls to Parallel.retrieve will be made inside this context. By default, this does nothing. It may be useful for subclasses to handle nested parallelism. In particular, it may be required to avoid deadlocks if a backend manages a fixed number of workers, when those workers may be asked to do nested Parallel calls. Without 'retrieval_context' this could lead to deadlock, as all the workers managed by the backend may be "busy" waiting for the nested parallel calls to finish, but the backend has no free workers to execute those tasks. Nrrrrr�retrieval_context�s� z%ParallelBackendBase.retrieval_contextcCstt��tj�Sr)� isinstance� threading�current_thread� _MainThreadrrrr�in_main_thread sz"ParallelBackendBase.in_main_thread)NNr�rNNN�T)#�__name__� __module__� __qualname__�__doc__Zdefault_n_jobs�supports_inner_max_num_threadsr�propertyrrrrrNrRrr%r,r1r4r7r;r<r=r>r@rDrFrKrV� contextlib�contextmanagerrW� staticmethodr\� __classcell__rrrrrsD  �    #  �    r)� metaclassc@sBeZdZdZdZdZdZdZdd�Zd dd�Z d d �Z d d �Z dS)rIz�A ParallelBackend which will execute all batches sequentially. Does not use/create any threading objects, and hence has minimal overhead. Used when n_jobs == 1. TFcCs|dkrtd��dS)�?Determine the number of jobs which are going to run in parallelr�&n_jobs == 0 in Parallel has no meaningr)� ValueErrorr#rrrr%sz"SequentialBackend.effective_n_jobsNcCr')�Schedule a func to be run�-Should never be called for SequentialBackend.�� RuntimeErrorr)rrrr1"r-zSequentialBackend.submitcCr')Nrnror2rrrr4&sz*SequentialBackend.retrieve_result_callbackcCsddlm}|�S)Nr)�get_active_backend)r8rq)rrqrrrrK)s z$SequentialBackend.get_nested_backendr) r_r`rarb� uses_threadsrr�supports_sharedmemr%r1r4rKrrrrrIs  rIc@sHeZdZdZdZdd�Zdd�Zdd�Zdd d �Zd d �Z ddd�Z dS)�PoolManagerMixinz,A helper class for managing pool of workers.NcCsD|dkrtd��tdus|durdS|dkr tt�d|d�}|S)rjrrkNr)rlrrMr r#rrrr%7sz!PoolManagerMixin.effective_n_jobscCs,|jdur|j��|j��d|_dSdS�z#Shutdown the process or thread poolN)�_pool�closer>rrrrr>Cs    �zPoolManagerMixin.terminatecCr)z;Used by `submit` to make it possible to implement lazy init)rvrrrr� _get_poolJrzPoolManagerMixin._get_poolcCs|��jt|�d||d�S)rmr)r+Zerror_callback)rxr,rr)rrrr1Ns �zPoolManagerMixin.submitcCst|�S)z=Mimic concurrent.futures results, raising an error if needed.)r)r�resultrrrr4Zsz)PoolManagerMixin.retrieve_result_callbackTcCs4|��|r|jd|jj|jd�|jj��dSdS)z@Shutdown the pool and restart a new one with the same parameters�r$r8Nr)r>r;r8r$Z_backend_kwargsrErrrrFbs� ��z!PoolManagerMixin.abort_everythingrr^) r_r`rarbrvr%r>rxr1r4rFrrrrrt2s   rtcsHeZdZdZdZdZdZdZ�fdd�Zdd �Z d d �Z d d �Z �Z S)�AutoBatchingMixinz/A helper class for automagically batching jobs.皙�����?�rgc s&t�jdi|��|j|_|j|_dS)Nr)rr�_DEFAULT_EFFECTIVE_BATCH_SIZE�_effective_batch_size� _DEFAULT_SMOOTHED_BATCH_DURATION�_smoothed_batch_duration)r�kwargsrrrr~s zAutoBatchingMixin.__init__cCs�|j}|j}|dkr?||jkr?t||j|�}|d9}td||�}t|d�}||_|jjdkr>|j�d|�d|�d��n2||j kro|dkrot||j|�}td|d�}||_|jjdkrn|j�d|�d|�d��n|}||kry|j |_|S) r?rr}r� zBatch computation too fast (zs.) Setting batch_size=�.zBatch computation too slow () rr��MIN_IDEAL_BATCH_DURATION�int�minrMr8�verbose�_print�MAX_IDEAL_BATCH_DURATIONr�)rZold_batch_sizeZbatch_durationZideal_batch_sizerBrrrr@�sD �  ��� � ���z$AutoBatchingMixin.compute_batch_sizecCs>||jkr|j}||jkr|}nd|d|}||_dSdS)rAg�������?r|N)rr�r�)rrBrCZ old_durationZ new_durationrrrrD�s   �z!AutoBatchingMixin.batch_completedcCs|j|_|j|_dS)zgReset batch statistics to default values. This avoids interferences with future jobs. N)r~rr�r�rrrr�reset_batch_stats�s z#AutoBatchingMixin.reset_batch_stats) r_r`rarbr�r�r~r�rr@rDr�rhrrrrr{ms ;r{c@s.eZdZdZdZdZdZd dd�Zdd�ZdS) rJa�A ParallelBackend which will use a thread pool to execute batches in. This is a low-overhead backend but it suffers from the Python Global Interpreter Lock if the called function relies a lot on Python objects. Mostly useful when the execution bottleneck is a compiled extension that explicitly releases the GIL (for instance a Cython loop wrapped in a "with nogil" block or an expensive call to a library such as NumPy). The actual thread pool is lazily initialized: the actual thread pool construction is delayed to the first call to apply_async. ThreadingBackend is used as the default backend for nested calls. TrNcKs2|�|�}|dkrtt|jd���||_||_|S�z?Build a process or thread pool and return the number of workersrrG)r%�FallbackToBackendrIrr8�_n_jobs)rr$r8rrrrr;�s zThreadingBackend.configurecCs|jdur t|j�|_|jS)z�Lazily initialize the thread pool The actual pool of worker threads is only initialized at the first call to apply_async. N)rvrr�rrrrrx�s  zThreadingBackend._get_pool)rN) r_r`rarbrrrrsr;rxrrrrrJ�s  rJcsFeZdZdZdZdZ�fdd�Z    d dd �Z�fd d �Z�Z S) �MultiprocessingBackendaA ParallelBackend which will use a multiprocessing.Pool. Will introduce some communication and memory overhead when exchanging input and output data with the with the worker Python processes. However, does not suffer from the Python Global Interpreter Lock. TFcs�tdurdSt��jr |dkrt�rd}nd}tj|dd�dStjdkr2|dkr0tjddd�dS|��sH|j dksH|dkrFtjd dd�dSt t |�� |�S) z�Determine the number of jobs which are going to run in parallel. This also checks if we are attempting to create a nested parallel loop. Nrz�Inside a Dask worker with daemon=True, setting n_jobs=1. Possible work-arounds: - dask.config.set({'distributed.worker.daemon': False})- set the environment variable DASK_DISTRIBUTED__WORKER__DAEMON=False before creating your Dask cluster.zHMultiprocessing-backed parallel loops cannot be nested, setting n_jobs=1��� stacklevelrzTMultiprocessing-backed parallel loops cannot be nested, below loky, setting n_jobs=1zVMultiprocessing-backed parallel loops cannot be nested below threads, setting n_jobs=1) r�current_process�daemon�inside_dask_workerr.r/r �_CURRENT_DEPTHr\rrr�r%�rr$�msgrrrr% s4 � � ��z'MultiprocessingBackend.effective_n_jobsrNcKsT|�|�}|dkrtt|jd���i|j�|�}t��t|fi|��|_||_ |Sr�) r%r�rIrr�gc�collectr rvr8)rr$r8r9r:Zmemmapping_pool_kwargsrrrr;>s ��z MultiprocessingBackend.configurecstt|���|��dSru)rr�r>r�rrrrr>Vs z MultiprocessingBackend.terminater]) r_r`rarbrrr%r;r>rhrrrrr��s 6 �r�c@sXeZdZdZdZdZ     ddd�Zdd�Zdd d �Zd d �Z d d�Z ddd�Z dS)� LokyBackendz>Managing pool of workers with loky instead of multiprocessing.TrNcKs�|�|�}|dkrtt|jd���i|j�|�}d|vr td��|dur+|j�dd�}t|f||j|d�|j d �|��|_ ||_ |S) z9Build a process executor and return the number of workersrrGr5ztThe 'timeout' parameter is not supported by the LokyBackend. Please use the `idle_worker_timeout` parameter instead.N�idle_worker_timeouti,)r$)r5rS� context_id) r%r�rIrrrlr6r rV�_id�_workersr8)rr$r8r9r:r�Zmemmapping_executor_kwargsrrrr;bs2 ���� ��zLokyBackend.configurecCs�|dkrtd��tdus|durdSt��jr,|dkr*t�r!d}nd}tj|dd�dS|��sB|jdksB|dkr@tjd dd�dS|dkrPt t �d|d�}|S) rjrrkNrz�Inside a Dask worker with daemon=True, setting n_jobs=1. Possible work-arounds: - dask.config.set({'distributed.worker.daemon': False}) - set the environment variable DASK_DISTRIBUTED__WORKER__DAEMON=False before creating your Dask cluster.zRLoky-backed parallel loops cannot be called in a multiprocessing, setting n_jobs=1r�r�zKLoky-backed parallel loops cannot be nested below threads, setting n_jobs=1) rlrr�r�r�r.r/r\rrMr r�rrrr%�s. � ��zLokyBackend.effective_n_jobscCs"|j�|�}|dur|�|�|S)rmN)r�r1�add_done_callback)rr*r+�futurerrrr1�s  zLokyBackend.submitcCs"z|��WStytd��w)z;Retrieve the result, here out is the future given by submitaThe executor underlying Parallel has been shutdown. This is likely due to the garbage collection of a previous generator from a call to Parallel with return_as='generator'. Make sure the generator is not garbage collected when submitting a new job or that it is first properly exhausted.)ryr rp)rr�rrrr4�s  ��z$LokyBackend.retrieve_result_callbackcCs2|jdur|jjj|jjdd�d|_|��dS)NF)r��force)r�Z_temp_folder_managerZ_clean_temporary_resourcesr8r�r�rrrrr>�s � zLokyBackend.terminatecCs4|jjdd�d|_|r|j|jj|jd�dSdS)zCShutdown the workers and restart a new one with the same parametersT)� kill_workersNrz)r�r>r;r8r$rErrrrF�s �zLokyBackend.abort_everything)rNNNNrr^) r_r`rarbrrcr;r%r1r4r>rFrrrrr�\s �' *  r�c@seZdZdZdd�ZdS)r�z<Raised when configuration should fallback to another backendcCs ||_dSr)�backend)rr�rrrr�s zFallbackToBackend.__init__N)r_r`rarbrrrrrr��s r�cCsHzddlm}Wn tyYdSwz|�WdSty#YdSw)zDCheck whether the current function is executed inside a Dask worker.r�� get_workerFT)� distributedr�� ImportErrorrlr�rrrr��s � �r�)$rbrer�rOrYr.�abcrr�_multiprocessing_helpersr�_utilsrrZmultiprocessing.poolr�executorr Zexternals.lokyr r Zexternals.loky.process_executorr �poolr rrI�objectrtr{rJr�r�� Exceptionr�r�rrrr�<module>s4     t";j(] 
Memory