
    0Ph                         d Z ddlZddlZddlmZ ddlZddlmZ ddlmZm	Z	 da
d Z G d d	ej                  Zd
 Z G d d          Zd ZddZdS )zWCustomizations of :mod:`joblib` and :mod:`threadpoolctl` tools for scikit-learn
usage.
    N)update_wrapper)ThreadpoolController   )config_context
get_configc                     t          | d          r|                     |          S t          j        dt                     | S )zFHelper function that intends to attach a config to a delayed function.with_configz`sklearn.utils.parallel.Parallel` needs to be used in conjunction with `sklearn.utils.parallel.delayed` instead of `joblib.delayed` to correctly propagate the scikit-learn configuration to the joblib workers.)hasattrr	   warningswarnUserWarning)delayed_funcconfigs     V/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/utils/parallel.py_with_configr      sN    |]++ ''///7
 	
 	
 	
     c                   "     e Zd ZdZ fdZ xZS )Parallela  Tweak of :class:`joblib.Parallel` that propagates the scikit-learn configuration.

    This subclass of :class:`joblib.Parallel` ensures that the active configuration
    (thread-local) of scikit-learn is propagated to the parallel workers for the
    duration of the execution of the parallel tasks.

    The API does not change and you can refer to :class:`joblib.Parallel`
    documentation for more details.

    .. versionadded:: 1.3
    c                     t                      fd|D             }t                                          |          S )aH  Dispatch the tasks and return the results.

        Parameters
        ----------
        iterable : iterable
            Iterable containing tuples of (delayed_function, args, kwargs) that should
            be consumed.

        Returns
        -------
        results : list
            List of results of the tasks.
        c              3   F   K   | ]\  }}}t          |          ||fV  d S N)r   ).0r   argskwargsr   s       r   	<genexpr>z$Parallel.__call__.<locals>.<genexpr>I   sN        
  
*dF ,//v> 
  
  
  
  
  
r   )r   super__call__)selfiterableiterable_with_configr   	__class__s      @r   r   zParallel.__call__6   sS    $  
  
  
  
.6 
  
  
 ww 4555r   )__name__
__module____qualname____doc__r   __classcell__)r!   s   @r   r   r   )   sB        
 
6 6 6 6 6 6 6 6 6r   r   c                 F     t          j                    fd            }|S )a^  Decorator used to capture the arguments of a function.

    This alternative to `joblib.delayed` is meant to be used in conjunction
    with `sklearn.utils.parallel.Parallel`. The latter captures the scikit-
    learn configuration by calling `sklearn.get_config()` in the current
    thread, prior to dispatching the first task. The captured configuration is
    then propagated and enabled for the duration of the execution of the
    delayed function in the joblib workers.

    .. versionchanged:: 1.3
       `delayed` was moved from `sklearn.utils.fixes` to `sklearn.utils.parallel`
       in scikit-learn 1.3.

    Parameters
    ----------
    function : callable
        The function to be delayed.

    Returns
    -------
    output: tuple
        Tuple containing the delayed function, the positional arguments, and the
        keyword arguments.
    c                  (    t                    | |fS r   )_FuncWrapper)r   r   functions     r   delayed_functionz!delayed.<locals>.delayed_functionk   s    H%%tV33r   	functoolswraps)r*   r+   s   ` r   delayedr/   Q   s;    4 _X4 4 4 4 4 r   c                   $    e Zd ZdZd Zd Zd ZdS )r)   z:Load the global configuration before calling the function.c                 >    || _         t          | | j                    d S r   )r*   r   )r   r*   s     r   __init__z_FuncWrapper.__init__u   s!     tT]+++++r   c                     || _         | S r   )r   )r   r   s     r   r	   z_FuncWrapper.with_configy   s    r   c                     t          | dd           }|t          j        dt                     i }t	          di |5   | j        |i |cd d d            S # 1 swxY w Y   d S )Nr   z`sklearn.utils.parallel.delayed` should be used with `sklearn.utils.parallel.Parallel` to make it possible to propagate the scikit-learn configuration of the current thread to the joblib workers. )getattrr   r   r   r   r*   )r   r   r   r   s       r   r   z_FuncWrapper.__call__}   s    x..>M+
    F%%f%% 	2 	2 4=$1&11	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2s   AAAN)r"   r#   r$   r%   r2   r	   r   r5   r   r   r)   r)   r   sG        DD, , ,  2 2 2 2 2r   r)   c                  :    t           t                      a t           S )z1Return the global threadpool controller instance.)_threadpool_controllerr   r5   r   r   _get_threadpool_controllerr9      s     %!5!7!7!!r      blasc                       fd}|S )a  Decorator to limit the number of threads used at the function level.

    It should be prefered over `threadpoolctl.ThreadpoolController.wrap` because this
    one only loads the shared libraries when the function is called while the latter
    loads them at import time.
    c                 L     t          j                    fd            }|S )Nc                      t                      }|                              5   | i |cd d d            S # 1 swxY w Y   d S )N)limitsuser_api)r9   limit)r   r   
controllerfuncr?   r@   s      r   wrapperzD_threadpool_controller_decorator.<locals>.decorator.<locals>.wrapper   s    355J!!(!CC - -tT,V,,- - - - - - - - - - - - - - - - - -s   <A A r,   )rC   rD   r?   r@   s   ` r   	decoratorz3_threadpool_controller_decorator.<locals>.decorator   sC    				- 	- 	- 	- 	- 	- 
		-
 r   r5   )r?   r@   rE   s   `` r    _threadpool_controller_decoratorrF      s*          r   )r:   r;   )r%   r-   r   r   joblibthreadpoolctlr   _configr   r   r8   r   r   r/   r)   r9   rF   r5   r   r   <module>rJ      s         $ $ $ $ $ $  . . . . . . 0 0 0 0 0 0 0 0    "$6 $6 $6 $6 $6v $6 $6 $6P  B2 2 2 2 2 2 2 28" " "     r   