
    9i                     f    S SK r S SKJrJr  S SKrS SKrS SKJr  S SK	J
r
  S SKJr   " S S\5      rg)    N)ABCMetaabstractmethod)Hook)Priority)ModeKeysc                       \ rS rSrSr\r\R                  r	    SS jr
\S 5       r\SS j5       rSS jrS rSS jrS	 rS
 rS rS rS rSrg)
LoggerHook   a}  Base class for logger hooks.

Args:
    interval (int): Logging interval (every k iterations). It is interval of iterations even by_epoch is true.
    ignore_last (bool): Ignore the log of last iterations in each epoch
        if less than `interval`.
    reset_flag (bool): Whether to clear the output buffer after logging.
    by_epoch (bool): Whether EpochBasedtrainer is used.
c                 4    Xl         X l        X0l        X@l        g N)intervalignore_last
reset_flagby_epoch)selfr   r   r   r   s        e/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/trainers/hooks/logger/base.py__init__LoggerHook.__init__   s    
 !&$     c                     g r    r   trainers     r   logLoggerHook.log&   s    r   c                    [        U [        R                  5      (       a  gU(       a0  [        U [        R                  5      (       a  U R
                  S:X  a  gU(       a/  [        U [        R                  5      (       a  [        U 5      S:X  a  gg)zTell the input variable is a scalar or not.

Args:
    val: Input variable.
    include_np (bool): Whether to treat 0-d np.ndarray as a scalar.
    include_torch (bool): Whether to treat 0-d torch.Tensor as a scalar.

Returns:
    bool: True or False.
Tr      F)	
isinstancenumbersNumbernpndarrayndimtorchTensorlen)val
include_npinclude_torchs      r   	is_scalarLoggerHook.is_scalar*   s[     c7>>**JsBJJ77CHHMz#u||<<SQr   c                    US:  d   eUR                   R                   H  nUR                   R                  U   U* S n[        U5       Hb  u  pV[        U[        R
                  5      (       d  M&  UR                  5       R                  5       R                  5       R                  5       XE'   Md     XAR                   R                  U   U* S& M     g)zYFetch latest n values or all values, process tensor type, convert to numpy for dump logs.r   N)

log_bufferval_history	enumerater   r$   r%   clonedetachcpunumpy)r   r   nkeyvaluesivs          r   fetch_tensorLoggerHook.fetch_tensor?   s    Avv%%11C''33C8!=F!&)a.. !	 0 0 2 6 6 8 > > @FI * 8>**3/4 2r   c                     UR                   [        R                  [        R                  4;   a  UR                  S-   nU$ [        S[        R                   S[        R                   SUR                    35      e)Nr   ztrainer mode should be z or z
, but got )moder   TRAINEVALepoch
ValueError)r   r   r?   s      r   	get_epochLoggerHook.get_epochK   si    <<HNNHMM::MMA%E
  )(..)9hmm_ M"<<.*+ +r   c                 v    U R                   (       a  U(       a  UR                  S-   nU$ UR                  S-   nU$ )z(Get the current training iteration step.r   )r   
inner_iteriter)r   r   rD   current_iters       r   get_iterLoggerHook.get_iterT   s9    ==Z"--1L  #<<!+Lr   c                 r    UR                   S S S2    H!  n[        U[        5      (       d  M  SUl          g    g )NT)hooksr   r	   r   )r   r   hooks      r   
before_runLoggerHook.before_run\   s0    MM$B$'D$
++"& (r   c                 8    UR                   R                  5         g r   )r-   clearr   s     r   before_epochLoggerHook.before_epochb   s      "r   c                 L   U R                   (       aa  U R                  XR                  5      (       aA  U R                  XR                  5        UR                  R                  U R                  5        OU R                   (       da  U R                  XR                  5      (       aA  U R                  XR                  5        UR                  R                  U R                  5        OgU R                  U5      (       aQ  U R                  (       d@  U R                  XR                  5        UR                  R                  U R                  5        UR                  R                  (       a>  U R                  U5        U R                  (       a  UR                  R                  5         g g g r   )r   every_n_inner_itersr   r9   r-   averageevery_n_itersend_of_epochr   readyr   r   clear_outputr   s     r   after_train_iterLoggerHook.after_train_itere   s   ==T55g}}MMg}}5&&t}}54#5#5g}}#M#Mg}}5&&t}}5w''0@0@g}}5&&t}}5##HHW""//1  $r   c                     UR                   R                  (       a>  U R                  U5        U R                  (       a  UR                   R	                  5         g g g r   )r-   rX   r   r   rY   r   s     r   after_train_epochLoggerHook.after_train_epochv   s@    ##HHW""//1  $r   c                     U R                  U5        UR                  R                  5         U R                  U5        U R                  (       a  UR                  R                  5         g g r   )r9   r-   rU   r   r   rY   r   s     r   after_val_epochLoggerHook.after_val_epoch|   sL    '"""$??++- r   )r   r   r   r   N)
   TFT)TT)r   )F)__name__
__module____qualname____firstlineno____doc__r   __metaclass__r   VERY_LOWPRIORITYr   r   r   staticmethodr*   r9   rA   rG   rM   rQ   rZ   r]   r`   __static_attributes__r   r   r   r	   r	      s|     M  H !!	!    (
>#2"2.r   r	   )r   abcr   r   r3   r!   r$   modelscope.trainers.hooks.hookr   "modelscope.trainers.hooks.priorityr   modelscope.utils.constantr   r	   r   r   r   <module>rq      s*     '   / 7 .s. s.r   