o
    `Û·iä   ã                   @   s–   d dl Z d dlZd dlmZmZmZmZ d dlZd dl	m
Z
 d dlmZ d dlmZmZ d dlmZ er:d dlmZ e e¡ZeG dd	„ d	eƒƒZdS )
é    N)ÚTYPE_CHECKINGÚDictÚListÚOptional)ÚTrial)ÚDEFAULT_METRIC)ÚFIFOSchedulerÚTrialScheduler)Ú	PublicAPI)ÚTuneControllerc                       s.  e Zd ZdZ							d,ded	ee d
ee dedededef‡ fdd„Z	d	ee d
ee defdd„Z
dddef‡ fdd„Zdddededefdd„Zdddedefdd„Zdefdd„Zddded edefd!d"„Zd edee fd#d$„Zd%ee d efd&d'„Zded edejfd(d)„Zd*d+„ Z‡  ZS )-ÚMedianStoppingRuleaN  Implements the median stopping rule as described in the Vizier paper:

    https://research.google.com/pubs/pub46180.html

    Args:
        time_attr: The training result attr to use for comparing time.
            Note that you can pass in something non-temporal such as
            `training_iteration` as a measure of progress, the only requirement
            is that the attribute should increase monotonically.
        metric: The training result objective value attribute. Stopping
            procedures will use this attribute. If None but a mode was passed,
            the `ray.tune.result.DEFAULT_METRIC` will be used per default.
        mode: One of {min, max}. Determines whether objective is
            minimizing or maximizing the metric attribute.
        grace_period: Only stop trials at least this old in time.
            The mean will only be computed from this time onwards. The units
            are the same as the attribute named by `time_attr`.
        min_samples_required: Minimum number of trials to compute median
            over.
        min_time_slice: Each trial runs at least this long before
            yielding (assuming it isn't stopped). Note: trials ONLY yield if
            there are not enough samples to evaluate performance for the
            current result AND there are other trials waiting to run.
            The units are the same as the attribute named by `time_attr`.
        hard_stop: If False, pauses trials instead of stopping
            them. When all other trials are complete, paused trials will be
            resumed and allowed to run FIFO.
    Útime_total_sNç      N@é   r   TÚ	time_attrÚmetricÚmodeÚgrace_periodÚmin_samples_requiredÚmin_time_sliceÚ	hard_stopc                    s²   t ƒ  ¡  tƒ | _|| _|| _|| _|| _d | _d | _	|| _
|r@|dv s(J dƒ‚| j
dkr1tdƒntdƒ| _| j
dkr=tnt| _	|| _|| _i | _t dd„ ¡| _t t¡| _d S )N)ÚminÚmaxz`mode` must be 'min' or 'max'.r   ú-infÚinfc                   S   s   t dƒS )Nr   )Úfloat© r   r   ú^/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/tune/schedulers/median_stopping_rule.pyÚ<lambda>M   s    z-MedianStoppingRule.__init__.<locals>.<lambda>)ÚsuperÚ__init__ÚsetÚ_stopped_trialsÚ_grace_periodÚ_min_samples_requiredÚ_min_time_sliceÚ_metricÚ_worstÚ_compare_opÚ_moder   r   r   Ú
_time_attrÚ
_hard_stopÚ_trial_stateÚcollectionsÚdefaultdictÚ_last_pauseÚlistÚ_results)Úselfr   r   r   r   r   r   r   ©Ú	__class__r   r   r    1   s$   

zMedianStoppingRule.__init__Úreturnc                 K   sz   | j r|rdS | jr|rdS |r|| _ |r|| _| jdkr!tdƒntdƒ| _| jdkr-tnt| _| j d u r;| jr;t| _ dS )NFr   r   r   T)r&   r)   r   r'   r   r   r(   r   )r2   r   r   Úspecr   r   r   Úset_search_propertiesP   s   

z(MedianStoppingRule.set_search_propertiesÚtune_controllerr   Útrialc                    sB   | j r	| jr	| jstd | jj| j | j¡ƒ‚tt	| ƒ 
||¡ d S )NzÁ{} has been instantiated without a valid `metric` ({}) or `mode` ({}) parameter. Either pass these parameters when instantiating the scheduler, or pass them as parameters to `tune.TuneConfig()`)r&   r'   r(   Ú
ValueErrorÚformatr4   Ú__name__r)   r   r   Úon_trial_add)r2   r8   r9   r3   r   r   r=   f   s   ÿü	zMedianStoppingRule.on_trial_addÚresultc           
      C   s<  | j |vs
| j|vrtjS || jv r| jrJ ‚tjS || j  }| j|  |¡ || jk r/tjS |  	|¡}| 
|¡ t|ƒ| jk rf|  |||¡}|tjkrT|| j|< d}nd}t d t|ƒ|j||¡¡ |S |  ||¡}|  |¡}	t d ||	||¡¡ |  ||	¡|	kr›t d |¡¡ | j |¡ | jr˜tjS tjS tjS )a  Callback for early stopping.

        This stopping rule stops a running trial if the trial's best objective
        value by step `t` is strictly worse than the median of the running
        averages of all completed trials' objectives reported up to step `t`.
        zYielding time to other trials.zContinuing anyways.zLMedianStoppingRule: insufficient samples={} to evaluate trial {} at t={}. {}z-Trial {} best res={} vs median res={} at t={}z%MedianStoppingRule: early stopping {})r*   r&   r	   ÚCONTINUEr"   r+   r1   Úappendr#   Ú_trials_beyond_timeÚremoveÚlenr$   Ú_on_insufficient_samplesÚPAUSEr/   ÚloggerÚdebugr;   Útrial_idÚ_median_resultÚ_best_resultr(   ÚaddÚSTOP)
r2   r8   r9   r>   ÚtimeÚtrialsÚactionÚ
action_strÚmedian_resultÚbest_resultr   r   r   Úon_trial_results   sL   	







ÿþ
ÿÿz"MedianStoppingRule.on_trial_resultc                 C   s   | j |  |¡ d S )N)r1   r@   )r2   r8   r9   r>   r   r   r   Úon_trial_complete®   s   z$MedianStoppingRule.on_trial_completec                 C   s   d  t| jƒ¡S )Nz)Using MedianStoppingRule: num_stopped={}.)r;   rC   r"   ©r2   r   r   r   Údebug_string³   s   ÿzMedianStoppingRule.debug_stringrM   c                 C   s:   || j |  | jk}|odd„ | ¡ D ƒ}|rtjS tjS )Nc                 S   s"   g | ]}|j tjtjfv r|‘qS r   )Ústatusr   ÚPENDINGÚPAUSED)Ú.0Útr   r   r   Ú
<listcomp>¼   s
    þz?MedianStoppingRule._on_insufficient_samples.<locals>.<listcomp>)r/   r%   Úget_live_trialsr	   rE   r?   )r2   r8   r9   rM   Úpauser   r   r   rD   ¸   s
   
þz+MedianStoppingRule._on_insufficient_samplesc                    s   ‡ ‡fdd„ˆ j D ƒ}|S )Nc                    s(   g | ]}ˆ j | d  ˆ j ˆkr|‘qS )éÿÿÿÿ)r1   r*   ©rZ   r9   ©r2   rM   r   r   r\   Ä   s
    þz:MedianStoppingRule._trials_beyond_time.<locals>.<listcomp>)r1   )r2   rM   rN   r   ra   r   rA   Ã   s   þz&MedianStoppingRule._trials_beyond_timerN   c                    s   t  ‡ ‡fdd„|D ƒ¡S )Nc                    s   g | ]}ˆ   |ˆ¡‘qS r   )Ú_running_meanr`   ra   r   r   r\   Ì   s    z5MedianStoppingRule._median_result.<locals>.<listcomp>)ÚnpÚmedian)r2   rN   rM   r   ra   r   rI   Ë   s   z!MedianStoppingRule._median_resultc                    s6   ˆ j | }‡ ‡fdd„|D ƒ}t ‡ fdd„|D ƒ¡S )Nc                    s0   g | ]}ˆ j |ˆ j   krˆkrn n|‘qS r   )r#   r*   ©rZ   Úrra   r   r   r\   Ò   s    *z4MedianStoppingRule._running_mean.<locals>.<listcomp>c                    ó   g | ]}|ˆ j  ‘qS r   ©r&   re   rU   r   r   r\   Õ   ó    )r1   rc   Úmean)r2   r9   rM   ÚresultsÚscoped_resultsr   ra   r   rb   Î   s
   
ÿz MedianStoppingRule._running_meanc                    s"   ˆ j | }ˆ  ‡ fdd„|D ƒ¡S )Nc                    rg   r   rh   re   rU   r   r   r\   Ù   ri   z3MedianStoppingRule._best_result.<locals>.<listcomp>)r1   r(   )r2   r9   rk   r   rU   r   rJ   ×   s   
zMedianStoppingRule._best_result)r   NNr   r   r   T)r<   Ú
__module__Ú__qualname__Ú__doc__Ústrr   r   ÚintÚboolr    r7   r   r=   r   rS   rT   rV   rD   r   rA   rI   rc   Úndarrayrb   rJ   Ú__classcell__r   r   r3   r   r      s|    øþýüûúùøÿÿ
þÿÿÿ
þ;ÿÿ
ÿÿÿÿ
þ	r   )r-   ÚloggingÚtypingr   r   r   r   Únumpyrc   Úray.tune.experimentr   Úray.tune.resultr   Ú#ray.tune.schedulers.trial_schedulerr   r	   Úray.util.annotationsr
   Ú"ray.tune.execution.tune_controllerr   Ú	getLoggerr<   rF   r   r   r   r   r   Ú<module>   s    
