o
    ॵi.                     @   st   d dl Zd dlmZ d dlmZ ddlmZ ddlm	Z	 ddl
mZ G dd	 d	Zejejd
G dd de	ZdS )    N)Hooks)
get_logger   )HOOKS)Hook)Priorityc                   @   s   e Zd ZdZdZdZdS )EarlyStopStrategyby_epochby_stepnoN)__name__
__module____qualname__r	   r
   r    r   r   ]/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/trainers/hooks/early_stop_hook.pyr      s    r   )module_namec                   @   s   e Zd ZdZejZdd dd dZdddd	ej	d
fde
de
dededede
defddZdd Zdd Zdd Zdd Zdd ZdS )EarlyStopHookad  Early stop when a specific metric stops improving.

    Args:
        metric_key (str):  Metric key to be monitored.
        rule (str): Comparison rule for best score. Support "max" and "min".
            If rule is "max", the training will stop when `metric_key` has stopped increasing.
            If rule is "min", the training will stop when `metric_key` has stopped decreasing.
        patience (int): Trainer will stop if the monitored metric did not improve for the last `patience` times.
        min_delta (float): Minimum change in the monitored metric to qualify as an improvement.
        check_finite (bool): If true, stops training when the metric becomes NaN or infinite.
        early_stop_strategy (str): The strategy to early stop, can be by_epoch/by_step/none
        interval (int): The frequency to trigger early stop check, by epoch or step.
    c                 C   s   | |kS Nr   xyr   r   r   <lambda>#       zEarlyStopHook.<lambda>c                 C   s   | |k S r   r   r   r   r   r   r   #   r   )maxminr      g        Tr   
metric_keyrulepatience	min_deltacheck_finiteearly_stop_strategyintervalc           	      K   sv   || _ || _|| _|| _|| _d|v r|d rtjntj| _n|| _|| _	d| _
|dkr3td| _d S td | _d S )Nr	   r   r   inf)r   r   r   r   r    r   r	   r
   r!   r"   
wait_countfloat
best_score)	selfr   r   r   r   r    r!   r"   kwargsr   r   r   __init__%   s   	
&zEarlyStopHook.__init__c                 C   s"   t |dst | _d S |j| _d S )Nlogger)hasattrr   r*   r'   trainerr   r   r   
before_run=   s   
zEarlyStopHook.before_runc              	   C   s   |j }|d u r	dS | j|vrtd| j d| d}|| j }| jr?t|s?d}| jd| j d| d| jdd	 |S | j	| j
 || j | jrT|| _d
| _|S |  jd7  _| j| jkrwd}| jd| j d| j d| jdd	 |S )NFzMetric not found: z not in TzMetric z = z& is not finite. Previous best metric: z.4f.r   r   z did not improve in the last z# epochs or iterations. Best score: )metric_valuesr   
ValueErrorr    npisfiniter*   warningr&   rule_mapr   r   r$   r   info)r'   r-   r0   should_stopcurrent_scorer   r   r   _should_stopC   s@   

zEarlyStopHook._should_stopc                 C   s   | j d d|_d S )NzEarly Stopping!T)r*   r6   _stop_trainingr,   r   r   r   r:   a   s   
zEarlyStopHook._stop_trainingc                 C   >   | j tjkrd S | || jsd S | |r| | d S d S r   )r!   r   r	   every_n_epochsr"   r9   r:   r,   r   r   r   after_train_epoche      
zEarlyStopHook.after_train_epochc                 C   r;   r   )r!   r   r
   every_n_itersr"   r9   r:   r,   r   r   r   after_train_itero   r>   zEarlyStopHook.after_train_iterN)r   r   r   __doc__r   VERY_LOWPRIORITYr5   r   r	   strintr%   boolr)   r.   r9   r:   r=   r@   r   r   r   r   r      s<    

r   )numpyr2   modelscope.metainfor   modelscope.utils.loggerr   builderr   hookr   priorityr   r   register_moduler   r   r   r   r   <module>   s   