o
    `۷i_                     @   s   d dl Z d dlZd dl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 eeZdZ	 de	defd	d
ZG dd dZeG dd de	ZdS )    N)DictListOptional)Searcher)+_set_search_properties_backwards_compatible)	PublicAPI__trial_index__searchernum_samplesc                 C   s*   t | tr|| j rtd d S d S d S )Na  `num_samples` is now expected to be the total number of trials, including the repeat trials. For example, set num_samples=15 if you intend to obtain 3 search algorithm suggestions and repeat each suggestion 5 times. Any leftover trials (num_samples mod repeat) will be ignored.)
isinstanceRepeaterrepeatloggerwarning)r	   r
    r   N/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/tune/search/repeater.py_warn_num_samples   s
   r   c                   @   s   e Zd ZdZddededefddZdefd	d
Zde	fddZ
dedefddZde	fddZdeee  fddZdefddZdS )_TrialGroupa  Internal class for grouping trials of same parameters.

    This is used when repeating trials for reducing training variance.

    Args:
        primary_trial_id: Trial ID of the "primary trial".
            This trial is the one that the Searcher is aware of.
        config: Suggested configuration shared across all trials
            in the trial group.
        max_trials: Max number of trials to execute within this group.

       primary_trial_idconfig
max_trialsc                 C   s:   t |tu sJ d||| _|| _|d i| _|| _d S )Nzconfig is not a dict, got {})typedictformatr   r   _trialsr   )selfr   r   r   r   r   r   __init__*   s
   

z_TrialGroup.__init__trial_idc                 C   s&   t | j| jk s
J | j|d  d S N)lenr   r   
setdefault)r   r   r   r   r   add1   s   z_TrialGroup.addreturnc                 C   s   t | j| jkS r   )r    r   r   r   r   r   r   full5      z_TrialGroup.fullscorec                 C   s,   || j v sJ |d u rtd|| j |< d S )Nz%Internal Error: Score cannot be None.)r   
ValueError)r   r   r'   r   r   r   report8   s   z_TrialGroup.reportc                 C   s   d | j  vot| j | jkS r   )r   valuesr    r   r$   r   r   r   finished_reporting>   s   z_TrialGroup.finished_reportingc                 C   s   t | j S r   )listr   r*   r$   r   r   r   scoresC   s   z_TrialGroup.scoresc                 C   s
   t | jS r   )r    r   r$   r   r   r   countF   s   
z_TrialGroup.countN)r   )__name__
__module____qualname____doc__strr   intr   r"   boolr%   floatr)   r+   r   r   r-   r.   r   r   r   r   r      s    r   c                       s   e Zd ZdZd dededef fddZd	ed
e	e
 fddZd!d	e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efddZde	e de	e de
d
efddZ  ZS )"r   a  A wrapper algorithm for repeating trials of same parameters.

    Set tune.TuneConfig(num_samples=...) to be a multiple of `repeat`. For example,
    set num_samples=15 if you intend to obtain 3 search algorithm suggestions
    and repeat each suggestion 5 times. Any leftover trials
    (num_samples mod repeat) will be ignored.

    It is recommended that you do not run an early-stopping TrialScheduler
    simultaneously.

    Args:
        searcher: Searcher object that the
            Repeater will optimize. Note that the Searcher
            will only see 1 trial among multiple repeated trials.
            The result/metric passed to the Searcher upon
            trial completion will be averaged among all repeats.
        repeat: Number of times to generate a trial with a repeated
            configuration. Defaults to 1.
        set_index: Sets a tune.search.repeater.TRIAL_INDEX in
            Trainable/Function config which corresponds to the index of the
            repeated trial. This can be used for seeds. Defaults to True.

    Example:

    .. code-block:: python

        from ray.tune.search import Repeater

        search_alg = BayesOptSearch(...)
        re_search_alg = Repeater(search_alg, repeat=10)

        # Repeat 2 samples 10 times each.
        tuner = tune.Tuner(
            trainable,
            tune_config=tune.TuneConfig(
                search_alg=re_search_alg,
                num_samples=20,
            ),
        )
        tuner.fit()

    r   Tr	   r   	set_indexc                    sD   || _ || _|| _g | _i | _d | _tt| j| j j	| j j
d d S )N)metricmode)r	   r   
_set_index_groups_trial_id_to_group_current_groupsuperr   r   r8   r9   )r   r	   r   r7   	__class__r   r   r   w   s   

zRepeater.__init__r   r#   c                 C   s   | j d u s
| j  r,| j|}|d u r|S t|t|| jd| _ | j	| j  d}n| j 
 }| j | | j j }| jrD||t< | j | j|< |S )N)r   r   )r=   r%   r	   suggestr   copydeepcopyr   r;   appendr.   r"   r   r:   TRIAL_INDEXr<   )r   r   r   index_in_groupr   r   r   rA      s    
zRepeater.suggestNresultc                 K   s   || j vrtd|t| j  | j | }|r| jj|vr"tj}n|| jj }|	|| |
 rL| }| jj|jfd| jjt|ii| dS dS )a  Stores the score for and keeps track of a completed trial.

        Stores the metric of a trial as nan if any of the following conditions
        are met:

        1. ``result`` is empty or not provided.
        2. ``result`` is provided but no metric was provided.

        z;Trial {} not in group; cannot report score. Seen trials: {}rG   N)r<   r   errorr   r,   r	   r8   npnanr)   r+   r-   on_trial_completer   nanmean)r   r   rG   kwargstrial_groupr'   r-   r   r   r   rK      s(   



zRepeater.on_trial_completec                 C   s   | j  }|d= |S )Nr	   )__dict__rB   )r   
self_stater   r   r   	get_state   s   
zRepeater.get_statestatec                 C      | j | d S r   )rO   update)r   rR   r   r   r   	set_state   r&   zRepeater.set_statecheckpoint_pathc                 C   rS   r   )r	   saver   rV   r   r   r   rW      r&   zRepeater.savec                 C   rS   r   )r	   restorerX   r   r   r   rY      r&   zRepeater.restorer8   r9   r   c                 K   s   t | jj|||fi |S r   )r   r	   set_search_properties)r   r8   r9   r   specr   r   r   rZ      s
   zRepeater.set_search_properties)r   Tr   )r/   r0   r1   r2   r   r4   r5   r   r3   r   r   rA   rK   rQ   rU   rW   rY   rZ   __classcell__r   r   r?   r   r   J   s$    +r   )rB   loggingtypingr   r   r   numpyrI   ray.tune.search.searcherr   ray.tune.search.utilr   ray.utilr   	getLoggerr/   r   rE   r4   r   r   r   r   r   r   r   <module>   s    
.