o
    `۷im                     @   s   d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZ er,d dlmZ eG dd dZeG d	d
 d
eZdS )    )TYPE_CHECKINGDictOptionaltag_scheduler)Trial)DEFAULT_METRIC)DeveloperAPI	PublicAPI)TuneControllerc                   @   s  e Zd ZdZdZdZdZdZdZdZ	dd	 Z
ed
d Zedd Zdee dee defddZdddefddZddde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ddefdd Zddd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S )*TrialSchedulerzInterface for implementing a Trial Scheduler class.

    Note to Tune developers: If a new scheduler is added, please update
    `air/_internal/usage.py`.
    CONTINUEPAUSESTOPNOOPNTc                 C   s   t |  d S Nr   self r   Y/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/tune/schedulers/trial_scheduler.py__init__!   s   zTrialScheduler.__init__c                 C      | j S r   )_metricr   r   r   r   metric$      zTrialScheduler.metricc                 C   r   r   )_supports_buffered_resultsr   r   r   r   supports_buffered_results(   r   z(TrialScheduler.supports_buffered_resultsr   modereturnc                 K   s,   | j r|rdS |r|| _ | j du rt| _ dS )a  Pass search properties to scheduler.

        This method acts as an alternative to instantiating schedulers
        that react to metrics with their own `metric` and `mode` parameters.

        Args:
            metric: Metric to optimize
            mode: One of ["min", "max"]. Direction to optimize.
            **spec: Any kwargs for forward compatibility.
                Info like Experiment.PUBLIC_KEYS is provided through here.
        FNT)r   r   )r   r   r   specr   r   r   set_search_properties,   s   

z$TrialScheduler.set_search_propertiestune_controllerr   trialc                 C      t )z5Called when a new trial is added to the trial runner.NotImplementedErrorr   r!   r"   r   r   r   on_trial_addE      zTrialScheduler.on_trial_addc                 C   r#   )znNotification for the error of trial.

        This will only be called when the trial is in the RUNNING state.r$   r&   r   r   r   on_trial_errorJ   s   zTrialScheduler.on_trial_errorresultc                 C   r#   )zCalled on each intermediate result returned by a trial.

        At this point, the trial scheduler can make a decision by returning
        one of CONTINUE, PAUSE, and STOP. This will only be called when the
        trial is in the RUNNING state.r$   r   r!   r"   r*   r   r   r   on_trial_resultQ   s   	zTrialScheduler.on_trial_resultc                 C   r#   )zNotification for the completion of trial.

        This will only be called when the trial is in the RUNNING state and
        either completes naturally or by manual termination.r$   r+   r   r   r   on_trial_complete\      z TrialScheduler.on_trial_completec                 C   r#   )zCalled to remove trial.

        This is called when the trial is in PAUSED or PENDING state. Otherwise,
        call `on_trial_complete`.r$   r&   r   r   r   on_trial_removef   s   zTrialScheduler.on_trial_removec                 C   r#   )zCalled to choose a new trial to run.

        This should return one of the trials in tune_controller that is in
        the PENDING or PAUSED state. This function must be idempotent.

        If no trial is ready, return None.r$   )r   r!   r   r   r   choose_trial_to_runn   r.   z"TrialScheduler.choose_trial_to_runc                 C   r#   )z=Returns a human readable message for printing to the console.r$   r   r   r   r   debug_stringx   r(   zTrialScheduler.debug_stringcheckpoint_pathc                 C   r#   )z$Save trial scheduler to a checkpointr$   r   r2   r   r   r   save}      zTrialScheduler.savec                 C   r#   )z(Restore trial scheduler from checkpoint.r$   r3   r   r   r   restore   r5   zTrialScheduler.restore)__name__
__module____qualname____doc__r   r   r   r   r   r   r   propertyr   r   r   strboolr    r   r'   r)   r   r,   r-   r/   r0   r1   r4   r6   r   r   r   r   r      sV    






r   c                       s   e Zd ZdZ fddZdddefddZddde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ddefddZdddee fddZde	fddZ  ZS )FIFOSchedulerz;Simple scheduler that just runs trials in submission order.c                    s   t    d S r   )superr   r   	__class__r   r   r      s   zFIFOScheduler.__init__r!   r   r"   c                 C      d S r   r   r&   r   r   r   r'         zFIFOScheduler.on_trial_addc                 C   rB   r   r   r&   r   r   r   r)      rC   zFIFOScheduler.on_trial_errorr*   r   c                 C   s   t jS r   )r   r   r+   r   r   r   r,      s   zFIFOScheduler.on_trial_resultc                 C   rB   r   r   r+   r   r   r   r-      r(   zFIFOScheduler.on_trial_completec                 C   rB   r   r   r&   r   r   r   r/      rC   zFIFOScheduler.on_trial_removec                 C   sH   |  D ]}|jtjkr|  S q|  D ]}|jtjkr!|  S qd S r   )
get_trialsstatusr   PENDINGPAUSEDr&   r   r   r   r0      s   z!FIFOScheduler.choose_trial_to_runc                 C   s   dS )Nz Using FIFO scheduling algorithm.r   r   r   r   r   r1      rC   zFIFOScheduler.debug_string)r7   r8   r9   r:   r   r   r'   r)   r   r<   r,   r-   r/   r   r0   r1   __classcell__r   r   r@   r   r>      s0    

	r>   N)typingr   r   r   ray.air._internal.usager   ray.tune.experimentr   ray.tune.resultr   ray.util.annotationsr	   r
   "ray.tune.execution.tune_controllerr   r   r>   r   r   r   r   <module>   s    y