
    `i                         d dl Z d dlZd dlZd dlZ	 d dlZdZn# e$ r dZY nw xY wd dlmZ d dl	m
Z
 	 d
dZe j        ddddd	            ZdS )    NTF)_optimize_config)profiler c                 8    t           t          j                  sJ t                    sJ t                    sJ  fd}t	          j                    }|                    |           |                    | j         j	                   |j
        S )Nc                      |           }j         }	 t          j        ||          }|j                                        S # t
          $ r(}t          |          rt          j        cY d }~S |d }~ww xY w)N)max_duration)	max_total_time_per_trialr   	benchmark	gpu_timesmean	Exception
isinstancemathinf)	trialargsmax_total_timeperfeignore_erroroptimize_configsuggest_functarget_funcs	        n/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/optimizing/_optimize.py	objectivez_optimize.<locals>.objective   s    |E""(A	%T@ @ @D>&&((( 	 	 	!\** x		s#   /A 
A7A2*A70A22A7)n_trialstimeout)r   r   _OptimizationConfigcallableoptunacreate_studyenqueue_trialoptimize
max_trialsr   
best_trial)r   r   r   default_bestr   r   studys   ``` `  r   	_optimizer(      s     o'7'KLLLLLK     L!!!!!        !!E	%%%	NN +'  ) ) )     )keypathreadonlyc              +   v  K   t           st          d          t          j                    }t          j        | t
          |          }t          j        |           |^t          j        	                    |          r|
                    |           n)|r't          j        d                    |                     	 |V  |J|sH|                                st          j        	                    |          s|                    |           t          j        |           dS # t          j        |           w xY w)a  Context manager that optimizes kernel launch parameters.

    In this context, CuPy's routines find the best kernel launch parameter
    values (e.g., the number of threads and blocks). The found values are
    cached and reused with keys as the shapes, strides and dtypes of the
    given inputs arrays.

    Args:
        key (string or None): The cache key of optimizations.
        path (string or None): The path to save optimization cache records.
            When path is specified and exists, records will be loaded from
            the path. When readonly option is set to ``False``, optimization
            cache records will be saved to the path after the optimization.
        readonly (bool): See the description of ``path`` option.
        max_trials (int): The number of trials that defaults to 100.
        timeout (float):
            Stops study after the given number of seconds. Default is 1.
        max_total_time_per_trial (float):
            Repeats measuring the execution time of the routine for the
            given number of seconds. Default is 0.1.

    Examples
    --------
    >>> import cupy
    >>> from cupyx import optimizing
    >>>
    >>> x = cupy.arange(100)
    >>> with optimizing.optimize():
    ...     cupy.sum(x)
    ...
    array(4950)

    .. note::
      Optuna (https://optuna.org) installation is required.
      Currently it works for reduction operations only.
    zbOptuna is required to run optimization. See https://optuna.org/ for the installation instructions.Nzz
The specified path {} could not be found, and the readonly option is set.
The optimization results will never be stored.
)_optuna_availableRuntimeErrorr   get_current_contextget_new_contextr(   set_current_contextosr+   existsloadwarningswarnformat	_is_dirtysave)r*   r+   r,   config_dictold_contextcontexts         r   r#   r#   /   sD     L  JIJ J 	J #688K.sI{KKG(1117>>$ 	LL 	M  F4LL  
:H  "" #"'..*>*> #T""",[99999,[9999s   <AD" "D8)r   )
contextlibr   r3   r6   r    r.   ImportError
cupy._corer   cupyxr   r(   contextmanagerr#   r   r)   r   <module>rC      s         				 MMM    ( ' ' ' ' '      
 $&   : te =: =: =: =: =: =: =:s    ##