o
    8wi"$                     @   s   d dl mZmZ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)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   ]/home/ubuntu/sommelier/.venv/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                   @   sT   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eddf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   7   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_restartnc                 C   s    |  j |7  _ |  j|7  _d S r   r!   r
   r#   r   r   r   increment_byF   s   z#_ReadyCompletedTracker.increment_byr   )r   r   r   r   r   int__annotations__r    r   r   r"   r%   r   r   r   r   r   (   s   
 

	r   c                       `   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 )_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   Z      

z_StartedTracker.resetc                       t    | j| _d S r   )r-   r"   r    r*   r	   r.   r   r   r"   _      
z _StartedTracker.reset_on_restartr#   c                       t  | |  j|7  _d S r   )r-   r%   r*   r$   r.   r   r   r%   d      z_StartedTracker.increment_byr   )r   r   r   r   r*   r&   r'   r   r   r"   r%   __classcell__r   r   r.   r   r)   K   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   r7   r	   r.   r   r   r   z   r0   z_ProcessedTracker.resetc                    r1   r   )r-   r"   r    r7   r	   r.   r   r   r"      r2   z"_ProcessedTracker.reset_on_restartr#   c                    r3   r   )r-   r%   r7   r$   r.   r   r   r%      r4   z_ProcessedTracker.increment_byr   )r   r   r   r   r7   r&   r'   r   r   r"   r%   r5   r   r   r.   r   r6   j   s   
  r6   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deddf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)r;   r/   r<   
ValueErrorr	   r   r   r   __post_init__   s   z_Progress.__post_init__c                 C   $   | j  jd7  _| j jd7  _d S N   )r;   r   r<   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` attributerA   )
isinstancer;   r)   	TypeErrorr/   r   r*   r<   r	   r   r   r   increment_started      z_Progress.increment_startedc                 C   rD   )NrE   z&` doesn't have a `processed` attributerA   )rF   r;   r6   rG   r/   r   r7   r<   r	   r   r   r   increment_processed   rI   z_Progress.increment_processedc                 C   r?   r@   )r;   r    r<   r	   r   r   r   increment_completed   rC   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.)r;   r<   Nr   r   )r   rL   rM   r   r   r   from_defaults   s    z_Progress.from_defaultsc                 C      | j   | j  d S r   )r;   r   r<   r	   r   r   r   r         
z_Progress.resetc                 C      | j   d S r   )r<   r   r	   r   r   r   reset_on_run      z_Progress.reset_on_runc                 C   rQ   r   )r<   r"   r	   r   r   r   r"      rS   z_Progress.reset_on_restartr#   c                 C   s   | j | | j| d S r   )r;   r%   r<   r$   r   r   r   r%      s   z_Progress.increment_byr   c                 C   $   | j |d  | j|d  d S )Nr;   r<   )r;   r   r<   r   r   r   r   r         z_Progress.load_state_dictr   )r   r   r   r   r   r6   r;   r   r'   r<   r>   rB   rH   rJ   rK   r   typer&   rN   r   r   rR   r"   r%   r   r   r   r   r   r   r8      s$   
 






r8   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	dd
e
deddf 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   rX   r	   r.   r   r   r      r0   z_BatchProgress.resetc                    r+   rY   )r-   rR   rX   r	   r.   r   r   rR      r0   z_BatchProgress.reset_on_runr#   c                    s   t  | || _d S r   )r-   r%   rX   )r
   r#   rX   r.   r   r   r%      s   
z_BatchProgress.increment_byr   c                    s   t  | |d | _d S )NrX   )r-   r   rX   r   r.   r   r   r      s   z_BatchProgress.load_state_dictr   )F)r   r   r   r   rX   boolr'   r   r   rR   r&   r%   r   r   r5   r   r   r.   r   rW      s   
  rW   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.

    r9   r;   r<   N)	r   r   r   r   r   r   r;   r'   r<   r   r   r   r   r[      s   
 
r[   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   )r8   rN   r   r   r   r   r   <lambda>     
 z_OptimizerProgress.<lambda>r9   stepc                   C   r]   r   )r8   rN   r)   r   r   r   r   r^     r_   	zero_gradr   Nc                 C   rO   r   )r`   r   ra   r	   r   r   r   r   	  rP   z_OptimizerProgress.resetc                 C   rO   r   )r`   rR   ra   r	   r   r   r   rR        
z_OptimizerProgress.reset_on_runc                 C   rO   r   )r`   r"   ra   r	   r   r   r   r"     rb   z#_OptimizerProgress.reset_on_restartr   c                 C   rT   )Nr`   ra   )r`   r   ra   r   r   r   r   r     rU   z"_OptimizerProgress.load_state_dictr   )r   r   r   r   r   r`   r8   r'   ra   r   r   rR   r"   r   r   r   r   r   r   r\      s   
 

r\   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.

    r9   	optimizerr   c                 C   s   | j jjjS r   )rd   r`   r;   r    r	   r   r   r   optimizer_steps'  s   z%_OptimizationProgress.optimizer_stepsNc                 C   rQ   r   )rd   r   r	   r   r   r   r   +  s   z_OptimizationProgress.resetc                 C   rQ   r   )rd   rR   r	   r   r   r   rR   /  rS   z"_OptimizationProgress.reset_on_runc                 C   rQ   r   )rd   r"   r	   r   r   r   r"   2  rS   z&_OptimizationProgress.reset_on_restartr   c                 C   s   | j |d  d S )Nrd   )rd   r   r   r   r   r   r   5  s   z%_OptimizationProgress.load_state_dictr   )r   r   r   r   r   r\   rd   r'   propertyr&   re   r   r   rR   r"   r   r   r   r   r   r   rc     s   
 

rc   N)dataclassesr   r   r   typing_extensionsr   r   r   r)   r6   r8   rW   r[   r\   rc   r   r   r   r   <module>   s(   ">"