o
    zi!                     @   s   d dl mZmZmZ d dlmZ d dlmZ eG dd dZeG dd deZ	eG dd	 d	e	Z
eG d
d de
ZeG dd deZeG dd deZeG dd deZeG dd deZeG dd deZdS )    )asdict	dataclassfield)Type)overridec                   @   sP   e Zd ZdZdefddZdeddfddZededd fd	d
ZdddZ	dS )_BaseProgressz>Mixin that implements state-loading utilities for dataclasses.returnc                 C   s   t | S N)r   self r   T/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/loops/progress.py
state_dict   s   z_BaseProgress.state_dictr   Nc                 C   s   | j | d S r	   )__dict__updater   r   r   r   r   load_state_dict   s   z_BaseProgress.load_state_dictc                 C   s   |  }| | |S r	   )r   )clsr   objr   r   r   from_state_dict   s   
z_BaseProgress.from_state_dictc                 C   s   t )zReset the object's state.)NotImplementedErrorr
   r   r   r   reset$   s   z_BaseProgress.resetr   N)
__name__
__module____qualname____doc__dictr   r   classmethodr   r   r   r   r   r   r      s    r   c                   @   sB   e Zd ZU dZdZeed< dZeed< edddZ	dd	d
Z
dS )_ReadyCompletedTrackeraF  Track an event's progress.

    Args:
        ready: Intended to track the number of events ready to start.
        completed: Intended to be incremented after the event completes (e.g. after ``on_*_end`` runs).

    These attributes should be increased in order, that is, :attr:`ready` first and :attr:`completed` last.

    r   ready	completedr   Nc                 C   s   d| _ d| _dS )zReset the state.r   N)r    r!   r
   r   r   r   r   8   s   
z_ReadyCompletedTracker.resetc                 C   s   | j | _dS )zReset the progress on restart.

        If there is a failure before all attributes are increased, restore the attributes to the last fully completed
        value.

        N)r!   r    r
   r   r   r   reset_on_restart>   s   z'_ReadyCompletedTracker.reset_on_restartr   )r   r   r   r   r    int__annotations__r!   r   r   r"   r   r   r   r   r   )   s   
 
r   c                       F   e Zd ZU dZdZeed< ed
 fddZed
 fdd	Z	  Z
S )_StartedTrackera  Track an event's progress.

    Args:
        ready: Intended to track the number of events ready to start.
        started: Intended to be incremented after the event is started (e.g. after ``on_*_start`` runs).
        completed: Intended to be incremented after the event completes (e.g. after ``on_*_end`` runs).

    These attributes should be increased in order, that is, :attr:`ready` first and :attr:`completed` last.

    r   startedr   Nc                       t    d| _d S Nr   )superr   r'   r
   	__class__r   r   r   W      

z_StartedTracker.resetc                       t    | j| _d S r	   )r*   r"   r!   r'   r
   r+   r   r   r"   \      
z _StartedTracker.reset_on_restartr   )r   r   r   r   r'   r#   r$   r   r   r"   __classcell__r   r   r+   r   r&   H   s   
 r&   c                       r%   )_ProcessedTrackera  Track an event's progress.

    Args:
        ready: Intended to track the number of events ready to start.
        started: Intended to be incremented after the event is started (e.g. after ``on_*_start`` runs).
        processed: Intended to be incremented after the event is processed.
        completed: Intended to be incremented after the event completes (e.g. after ``on_*_end`` runs).

    These attributes should be increased in order, that is, :attr:`ready` first and :attr:`completed` last.

    r   	processedr   Nc                    r(   r)   )r*   r   r2   r
   r+   r   r   r   r   r-   z_ProcessedTracker.resetc                    r.   r	   )r*   r"   r!   r2   r
   r+   r   r   r"   w   r/   z"_ProcessedTracker.reset_on_restartr   )r   r   r   r   r2   r#   r$   r   r   r"   r0   r   r   r+   r   r1   b   s   
 r1   c                   @   s   e Zd ZU dZeedZeed< eedZ	eed< dddZ
dd	d
ZdddZdddZdddZedee dedd fddZedddZdddZdddZededdfddZdS )	_ProgresszTrack aggregated and current progress.

    Args:
        total: Intended to track the total progress of an event.
        current: Intended to track the current progress of an event.

    default_factorytotalcurrentr   Nc                 C   s   | j j| jjurtdd S )Nz?The `total` and `current` instances should be of the same class)r6   r,   r7   
ValueErrorr
   r   r   r   __post_init__   s   z_Progress.__post_init__c                 C   $   | j  jd7  _| j jd7  _d S N   )r6   r    r7   r
   r   r   r   increment_ready      z_Progress.increment_readyc                 C   F   t | jtstd| jjj d| j jd7  _| j jd7  _d S )N`z$` doesn't have a `started` attributer<   )
isinstancer6   r&   	TypeErrorr,   r   r'   r7   r
   r   r   r   increment_started      z_Progress.increment_startedc                 C   r?   )Nr@   z&` doesn't have a `processed` attributer<   )rA   r6   r1   rB   r,   r   r2   r7   r
   r   r   r   increment_processed   rD   z_Progress.increment_processedc                 C   r:   r;   )r6   r!   r7   r
   r   r   r   increment_completed   r>   z_Progress.increment_completedtracker_clskwargsc                 K   s    | |di ||di |dS )zZUtility function to easily create an instance from keyword arguments to both ``Tracker``s.)r6   r7   Nr   r   )r   rG   rH   r   r   r   from_defaults   s    z_Progress.from_defaultsc                 C      | j   | j  d S r	   )r6   r   r7   r
   r   r   r   r         
z_Progress.resetc                 C      | j   d S r	   )r7   r   r
   r   r   r   reset_on_run      z_Progress.reset_on_runc                 C   rL   r	   )r7   r"   r
   r   r   r   r"      rN   z_Progress.reset_on_restartr   c                 C   $   | j |d  | j|d  d S )Nr6   r7   )r6   r   r7   r   r   r   r   r         z_Progress.load_state_dictr   )r   r   r   r   r   r1   r6   r   r$   r7   r9   r=   rC   rE   rF   r   r   r#   rI   r   r   rM   r"   r   r   r   r   r   r   r3   }   s"   
 






r3   c                       s`   e Zd ZU dZdZeed< ed fddZed fdd	Z	ed
e
ddf fddZ  ZS )_BatchProgressaW  Tracks batch progress.

    These counters are local to a trainer rank. By default, they are not globally synced across all ranks.

    Args:
        total: Tracks the total batch progress.
        current: Tracks the current batch progress.
        is_last_batch: Whether the batch is the last one. This is useful for iterable datasets.

    Fis_last_batchr   Nc                    r(   NF)r*   r   rR   r
   r+   r   r   r      r-   z_BatchProgress.resetc                    r(   rS   )r*   rM   rR   r
   r+   r   r   rM      r-   z_BatchProgress.reset_on_runr   c                    s   t  | |d | _d S )NrR   )r*   r   rR   r   r+   r   r   r      s   z_BatchProgress.load_state_dictr   )r   r   r   r   rR   boolr$   r   r   rM   r   r   r0   r   r   r+   r   rQ      s   
  rQ   c                   @   s6   e Zd ZU dZeedZeed< eedZeed< dS )_SchedulerProgressa  Tracks scheduler progress.

    These counters are local to a trainer rank. By default, they are not globally synced across all ranks.

    Args:
        total: Tracks the total scheduler progress.
        current: Tracks the current scheduler progress.

    r4   r6   r7   N)	r   r   r   r   r   r   r6   r$   r7   r   r   r   r   rU      s   
 
rU   c                   @   sv   e Zd ZU dZedd dZeed< edd dZeed< e	dd
dZ
dddZdddZe	dedd	fddZd	S )_OptimizerProgresszTrack optimizer progress.

    Args:
        step: Tracks ``optimizer.step`` calls.
        zero_grad: Tracks ``optimizer.zero_grad`` calls.

    c                   C   
   t tS r	   )r3   rI   r   r   r   r   r   <lambda>      
 z_OptimizerProgress.<lambda>r4   stepc                   C   rW   r	   )r3   rI   r&   r   r   r   r   rX      rY   	zero_gradr   Nc                 C   rJ   r	   )rZ   r   r[   r
   r   r   r   r      rK   z_OptimizerProgress.resetc                 C   rJ   r	   )rZ   rM   r[   r
   r   r   r   rM         
z_OptimizerProgress.reset_on_runc                 C   rJ   r	   )rZ   r"   r[   r
   r   r   r   r"      r\   z#_OptimizerProgress.reset_on_restartr   c                 C   rO   )NrZ   r[   )rZ   r   r[   r   r   r   r   r     rP   z"_OptimizerProgress.load_state_dictr   )r   r   r   r   r   rZ   r3   r$   r[   r   r   rM   r"   r   r   r   r   r   r   rV      s   
 

rV   c                   @   sn   e Zd ZU dZeedZeed< ede	fddZ
eddd	Zdd
dZdddZededdfddZdS )_OptimizationProgressz[Track optimization progress.

    Args:
        optimizer: Tracks optimizer progress.

    r4   	optimizerr   c                 C   s   | j jjjS r	   )r^   rZ   r6   r!   r
   r   r   r   optimizer_steps  s   z%_OptimizationProgress.optimizer_stepsNc                 C   rL   r	   )r^   r   r
   r   r   r   r     s   z_OptimizationProgress.resetc                 C   rL   r	   )r^   rM   r
   r   r   r   rM     rN   z"_OptimizationProgress.reset_on_runc                 C   rL   r	   )r^   r"   r
   r   r   r   r"     rN   z&_OptimizationProgress.reset_on_restartr   c                 C   s   | j |d  d S )Nr^   )r^   r   r   r   r   r   r      s   z%_OptimizationProgress.load_state_dictr   )r   r   r   r   r   rV   r^   r$   propertyr#   r_   r   r   rM   r"   r   r   r   r   r   r   r]     s   
 

r]   N)dataclassesr   r   r   typingr   typing_extensionsr   r   r   r&   r1   r3   rQ   rU   rV   r]   r   r   r   r   <module>   s*   :