
    9i                         S SK Jr  S SKJr  S SKJr  SSKJr  SSKJ	r	   " S S5      r
\R                  " \R                  S	9 " S
 S\	5      5       rg)    )OrderedDict)Optional)Hooks   )HOOKS)Hookc                        \ rS rSrSrSrSrSrg)EvaluationStrategy
   by_epochby_stepno N)__name__
__module____qualname____firstlineno__r   r   r   __static_attributes__r       i/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/trainers/hooks/evaluation_hook.pyr
   r
   
   s    HG	Br   r
   )module_namec                   |    \ rS rSrSrS\R                  S4S\\   S\\	   S\\   4S jjr
S	 rS
 rS rS rS rSrg)EvaluationHook   a  
Evaluation hook.

Args:
    interval (int): Evaluation interval.
    by_epoch (bool): Evaluate by epoch or by iteration.
    start_idx (int or None, optional): The epoch or iterations validation begins.
        Default: None, validate every interval epochs/iterations from scratch.
r   Nintervaleval_strategy	start_idxc                     US:  d   S5       eXl         X0l        SU l        SU;   a0  US   (       a  [        R                  O[        R
                  U l        g X l        g )Nr   z"interval must be a positive number)NNr   )r   r   last_eval_tagr
   r   r   r   )selfr   r   r   kwargss        r   __init__EvaluationHook.__init__   s^    
 !|AAA| ")@FA!3!<!<!3!;!;  "/r   c                     U R                   [        R                  :X  a<  U R                  U5      (       a%  U R	                  U5        SUR
                  4U l        ggg)z9Called after every training iter to evaluate the results.iterN)r   r
   r   _should_evaluatedo_evaluater%   r   r    trainers     r   after_train_iterEvaluationHook.after_train_iter+   sV    !3!;!;;@U@UA AW%"(',,!7DA;r   c                     U R                   [        R                  :X  a<  U R                  U5      (       a%  U R	                  U5        SUR
                  4U l        ggg)z:Called after every training epoch to evaluate the results.epochN)r   r
   r   r&   r'   r-   r   r(   s     r   after_train_epoch EvaluationHook.after_train_epoch2   sV    !3!<!<<AVAVB BW%")7==!9DB<r   c                    UR                   R                  R                  SS 5       cY  [        5       UR                   R                  S'   UR                   R                  S   R	                  UR                  U5      5        g g )Neval_results)visualization_bufferoutputgetr   update	visualize)r    r)   resultss      r   add_visualization_info%EvaluationHook.add_visualization_info9   su    ''..22>379<@ABM-G((//?((//?FF!!'*,	Ar   c                     UR                  5       nUR                  5        H   u  p4XAR                  R                  SU-   '   M"     SUR                  l        g)zEvaluate the results.zevaluation/TN)evaluateitems
log_bufferr3   ready)r    r)   eval_resnamevals        r   r'   EvaluationHook.do_evaluateA   sL    ##%!)ID>A%%md&:; * $( r   c                 b   U R                   [        R                  :X  a  UR                  nU R                  nOUR
                  nU R                  nU R                  c  U" XR                  5      (       d  g gUS-   U R                  :  a  gUS-   U R                  -
  U R                  -  (       a  gg)a  Judge whether to perform evaluation.

Here is the rule to judge whether to perform evaluation:
1. It will not perform evaluation during the epoch/iteration interval,
   which is determined by ``self.interval``.
2. It will not perform evaluation if the ``start_idx`` is larger than
   current epochs/iters.
3. It will not perform evaluation when current epochs/iters is larger than
   the ``start_idx`` but during epoch/iteration interval.

Returns:
    bool: The flag indicating whether to perform evaluation.
Fr   T)	r   r
   r   r-   every_n_epochsr%   every_n_itersr   r   )r    r)   current
check_times       r   r&   EvaluationHook._should_evaluateI   s     !3!<!<<mmG,,JllG++J>>!g}}55 6  kT^^+!dnn,=r   )r   r   r   r   )r   r   r   r   __doc__r
   r   r   intstrr"   r*   r.   r8   r'   r&   r   r   r   r   r   r      s\     ,-0B0K0K,0/#C=/ (/ %SM/8:,(r   r   N)collectionsr   typingr   modelscope.metainfor   builderr   hookr   r
   register_moduler   r   r   r   <module>rR      sM    #  %    5#7#78UT U 9Ur   