o
    `Û·iˆ  ã                   @   s8   d dl Zd dlmZ d dlmZ eG dd„ deƒƒZdS )é    N)ÚStopper)Ú	PublicAPIc                   @   sP   e Zd ZdZ				ddededed	ed
ef
dd„Zdd„ Zdd„ Z	dd„ Z
dS )ÚExperimentPlateauStopperaÊ  Early stop the experiment when a metric plateaued across trials.

    Stops the entire experiment when the metric has plateaued
    for more than the given amount of iterations specified in
    the patience parameter.

    Args:
        metric: The metric to be monitored.
        std: The minimal standard deviation after which
            the tuning process has to stop.
        top: The number of best models to consider.
        mode: The mode to select the top results.
            Can either be "min" or "max".
        patience: Number of epochs to wait for
            a change in the top models.

    Raises:
        ValueError: If the mode parameter is not "min" nor "max".
        ValueError: If the top parameter is not an integer
            greater than 1.
        ValueError: If the standard deviation parameter is not
            a strictly positive float.
        ValueError: If the patience parameter is not
            a strictly positive integer.
    çü©ñÒMbP?é
   Úminr   ÚmetricÚstdÚtopÚmodeÚpatiencec                 C   sŒ   |dvrt dƒ‚t|tƒr|dkrt dƒ‚t|tƒr|dk r"t dƒ‚t|tƒr+|dkr/t dƒ‚|| _|| _|| _d| _|| _|| _	g | _
d S )N)r   Úmaxz1The mode parameter can only be either min or max.é   zDTop results to consider must be a positive integer greater than one.r   z-Patience must be a strictly positive integer.z@The standard deviation must be a strictly positive float number.)Ú
ValueErrorÚ
isinstanceÚintÚfloatÚ_modeÚ_metricÚ	_patienceÚ_iterationsÚ_stdÚ_topÚ_top_values)Úselfr   r	   r
   r   r   © r   úY/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/tune/stopper/experiment_plateau.pyÚ__init__#   s&   ÿÿ
z!ExperimentPlateauStopper.__init__c                 C   sx   | j  || j ¡ | jdkrt| j ƒd| j… | _ nt| j ƒ| j d… | _ |  ¡ r5|  jd7  _|  ¡ S d| _|  ¡ S )z8Return a boolean representing if the tuning has to stop.r   Nr   r   )	r   Úappendr   r   Úsortedr   Úhas_plateauedr   Ústop_all)r   Útrial_idÚresultr   r   r   Ú__call__@   s   
üz!ExperimentPlateauStopper.__call__c                 C   s"   t | jƒ| jkot | j¡| jkS )N)Úlenr   r   Únpr	   r   ©r   r   r   r   r    T   s    ÿz&ExperimentPlateauStopper.has_plateauedc                 C   s   |   ¡ o	| j| jkS )z8Return whether to stop and prevent trials from starting.)r    r   r   r'   r   r   r   r!   Y   s   z!ExperimentPlateauStopper.stop_allN)r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   r   r   r$   r    r!   r   r   r   r   r      s(    úþýüû
úr   )Únumpyr&   Úray.tune.stopper.stopperr   Úray.util.annotationsr   r   r   r   r   r   Ú<module>   s
    