o
    $i                     @   sj   d dl Z d dlZd dlmZmZmZ d dlmZ d dlm	Z	 d dl
mZ eeZeG dd deZdS )    N)DictListOptional)Searcher)+_set_search_properties_backwards_compatible)	PublicAPIc                       s8  e Zd ZdZd0dededef fddZdd	 Zded
efddZ	de
e de
e ded
efddZded
e
e fddZ	d1dede
e defddZdeded
dfddZ			d2dedededede
ee  f
d d!Zd
efd"d#Zd$efd%d&Zd'efd(d)Zd'efd*d+Zdefd,d-Zdefd.d/Z  ZS )3ConcurrencyLimitera  A wrapper algorithm for limiting the number of concurrent trials.

    Certain Searchers have their own internal logic for limiting
    the number of concurrent trials. If such a Searcher is passed to a
    ``ConcurrencyLimiter``, the ``max_concurrent`` of the
    ``ConcurrencyLimiter`` will override the ``max_concurrent`` value
    of the Searcher. The ``ConcurrencyLimiter`` will then let the
    Searcher's internal logic take over.

    Args:
        searcher: Searcher object that the
            ConcurrencyLimiter will manage.
        max_concurrent: Maximum concurrent samples from the underlying
            searcher.
        batch: Whether to wait for all concurrent samples
            to finish before updating the underlying searcher.

    Example:

    .. code-block:: python

        from ray.tune.search import ConcurrencyLimiter
        search_alg = HyperOptSearch(metric="accuracy")
        search_alg = ConcurrencyLimiter(search_alg, max_concurrent=2)
        tuner = tune.Tuner(
            trainable,
            tune_config=tune.TuneConfig(
                search_alg=search_alg
            ),
        )
        tuner.fit()
    Fsearchermax_concurrentbatchc                    s   t |tu r
|dksJ || _|| _|| _t | _d| _i | _d| _	t
|ts1tdt | d|   tt| j| jj| jjd d S )Nr   TzAThe `ConcurrencyLimiter` only works with `Searcher` objects (got zH). Please try to pass `max_concurrent` to the search generator directly.)metricmode)typeintr	   r
   r   setlive_trialsnum_unfinished_live_trialscached_results_limit_concurrency
isinstancer   RuntimeError_set_searcher_max_concurrencysuperr   __init__r   r   )selfr	   r
   r   	__class__ `/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/tune/search/concurrency_limiter.pyr   /   s$   


zConcurrencyLimiter.__init__c                 C   s   | j | j | _d S N)r	   set_max_concurrencyr
   r   )r   r   r   r   r   F   s   z0ConcurrencyLimiter._set_searcher_max_concurrencyreturnc                 C   s
   || _ dS )NT)r
   )r   r
   r   r   r   r    M   s   z&ConcurrencyLimiter.set_max_concurrencyr   r   configc                 K   s"   |    t| jj|||fi |S r   )r   r   r	   set_search_properties)r   r   r   r"   specr   r   r   r#   S   s   z(ConcurrencyLimiter.set_search_propertiestrial_idc                 C   s   | j s	| j|S || jvsJ d| dt| j| jkr/td| dt| j| j d S | j|}|d tj	fvrI| j
| |  jd7  _|S )Nz	Trial ID z& must be unique: already found in set.zNot providing a suggestion for z! due to concurrency limit: %s/%s.   )r   r	   suggestr   lenr
   loggerdebugr   FINISHEDaddr   )r   r%   
suggestionr   r   r   r'   [   s"   

zConcurrencyLimiter.suggestNresulterrorc                 C   s   | j s| jj|||dS || jvrd S | jrN||f| j|< |  jd8  _| jdkrL| j D ]\}\}}| jj|||d | j| q.i | _d| _d S d S | jj|||d | j| |  jd8  _d S )N)r.   r/   r&   r   )	r   r	   on_trial_completer   r   r   r   itemsremove)r   r%   r.   r/   r   r   r   r0   q   s&   


z$ConcurrencyLimiter.on_trial_completec                 C   s   | j || d S r   )r	   on_trial_result)r   r%   r.   r   r   r   r3      s   z"ConcurrencyLimiter.on_trial_result
parametersvalueprunedintermediate_valuesc                 C   s   | j |||||S r   )r	   add_evaluated_point)r   r4   r5   r/   r6   r7   r   r   r   r8      s   
z&ConcurrencyLimiter.add_evaluated_pointc                 C   s   | j  }|d= t|S )Nr	   )__dict__copydeepcopyr   stater   r   r   	get_state   s   

zConcurrencyLimiter.get_stater=   c                 C      | j | d S r   )r9   updater<   r   r   r   	set_state      zConcurrencyLimiter.set_statecheckpoint_pathc                 C   r?   r   )r	   saver   rC   r   r   r   rD      rB   zConcurrencyLimiter.savec                 C   r?   r   )r	   restorerE   r   r   r   rF      rB   zConcurrencyLimiter.restorec                 C   r?   r   )r	   on_pauser   r%   r   r   r   rG      rB   zConcurrencyLimiter.on_pausec                 C   r?   r   )r	   
on_unpauserH   r   r   r   rI      rB   zConcurrencyLimiter.on_unpause)F)NF)FFN)__name__
__module____qualname____doc__r   r   boolr   r   r    r   strr   r#   r'   r0   r3   floatr   r8   r>   rA   rD   rF   rG   rI   __classcell__r   r   r   r   r      sX    !



r   )r:   loggingtypingr   r   r   ray.tune.search.searcherr   ray.tune.search.utilr   ray.util.annotationsr   	getLoggerrJ   r)   r   r   r   r   r   <module>   s    
