o
    zimc                     @   s  d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlmZ d dlmZ d dlZd dlmZ d dlmZ ed	Zerd d
lmZmZ d dlmZmZ d dlmZmZm Z m!Z!m"Z"m#Z# d dl$mZ% d dl&m'Z' d dl(m)Z) G dd deZ*eG dd de!Z+G dd deZ,G dd de Z-G dd de Z.G dd de Z/G dd de Z0eG dd dZ1G dd  d eZ2d!e3fd"d#Z4dS )$    N)	dataclass)	timedelta)AnyDict	GeneratorOptionalUnioncast)RequirementCache)override)ProgressBar)STEP_OUTPUTzrich>=10.2.2)get_consolereconfigure)ConsoleRenderableType)	BarColumnProgressProgressColumnTaskTaskID
TextColumn)Style)Textc                   @   s"   e Zd ZdZdddefddZdS )CustomBarColumnzOverrides ``BarColumn`` to provide support for dataloaders that do not define a size (infinite size) such as
        ``IterableDataset``.taskr   returnc                 C   sz   |j dusJ |jdusJ ttd|j td|j| jdu r dntd| j|j p/t|j |	 | j
| j| j| jd	S )z&Gets a progress bar widget for a task.Nr      )	total	completedwidthpulseanimation_timestylecomplete_stylefinished_stylepulse_style)r   	remaining_RichProgressBarmaxr   	bar_widthstartedmathisfiniteget_timer#   r$   r%   r&   )selfr    r0   f/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/callbacks/progress/rich_progress.pyrender(   s   

zCustomBarColumn.renderN)__name__
__module____qualname____doc__r(   r2   r0   r0   r0   r1   r   $   s    r   c                   @   s&   e Zd ZdZedee fddZdS )CustomInfiniteTaskzOverrides ``Task`` to define an infinite task.

        This is useful for datasets that do not define a size (infinite size) such as ``IterableDataset``.

        r   c                 C   s   d S Nr0   r/   r0   r0   r1   time_remaining@   s   z!CustomInfiniteTask.time_remainingN)r3   r4   r5   r6   propertyr   floatr:   r0   r0   r0   r1   r7   8   s    r7   c                       sd   e Zd ZdZ				ddededee ded	ed
e	de
f fddZddedede
fddZ  ZS )CustomProgresszPOverrides ``Progress`` to support adding tasks that have an infinite total size.T      Y@r   descriptionstartr   r   visiblefieldsr   c              
      sZ   |d usJ t |st| j|||||| j| jd}| |S t j|||||fi |S )N)rA   rB   	_get_time_lock)	r,   r-   r7   _task_indexr.   rD   add_custom_tasksuperadd_task)r/   r?   r@   r   r   rA   rB   r   	__class__r0   r1   rH   G   s   	


zCustomProgress.add_taskr   c                 C   sh   | j # || j| j< |r| | j | j}tt| jd | _W d    n1 s)w   Y  |   |S Nr   )rD   _tasksrE   
start_taskr   intrefresh)r/   r   r@   new_task_indexr0   r0   r1   rF   _   s   zCustomProgress.add_custom_task)Tr>   r   TT)r3   r4   r5   r6   strboolr   r<   rN   r   r   rH   r7   rF   __classcell__r0   r0   rI   r1   r=   D   s,     r=   c                       sD   e Zd ZdZdeeef ddf fddZdddefd	d
Z	  Z
S )CustomTimeColumng      ?r#   r   Nc                       || _ t   d S r8   r#   rG   __init__r/   r#   rI   r0   r1   rX   m      zCustomTimeColumn.__init__r   r   c                 C   sl   |j r|jn|j}|j}|d u rdnttt|d}|d u r!dnttt|d}t| d| | jdS )Nz-:--:--)secondsu    • r#   )	finishedfinished_timeelapsedr:   rR   r   rN   r   r#   )r/   r   r_   r'   elapsed_deltaremaining_deltar0   r0   r1   r2   q   s
   zCustomTimeColumn.render)r3   r4   r5   max_refreshr   rR   r   rX   r   r2   rT   r0   r0   rI   r1   rU   i   s    rU   c                       <   e Zd Zdeeef f fddZdddefddZ  Z	S )	BatchesProcessedColumnr#   c                    rV   r8   rW   rY   rI   r0   r1   rX   y   rZ   zBatchesProcessedColumn.__init__r   r   r   c                 C   s6   |j tdkr
|j nd}tt|j d| | jdS )Ninfz--/r\   )r   r<   r   rN   r   r#   )r/   r   r   r0   r0   r1   r2   }   s   zBatchesProcessedColumn.render
r3   r4   r5   r   rR   r   rX   r   r2   rT   r0   r0   rI   r1   rd   x       rd   c                       rc   )	ProcessingSpeedColumnr#   c                    rV   r8   rW   rY   rI   r0   r1   rX      rZ   zProcessingSpeedColumn.__init__r   r   r   c                 C   s,   |j d ur
|j dnd}t| d| jdS )Nz>.2fz0.00zit/sr\   )speedr   r#   )r/   r   
task_speedr0   r0   r1   r2      s   zProcessingSpeedColumn.renderrg   r0   r0   rI   r1   ri      rh   ri   c                       s~   e Zd ZdZdddeedf dedef fdd	Zd
eeef ddfddZ	ddde
fddZdeeddf fddZ  ZS )MetricsTextColumnzA column containing text.trainer
pl.Trainerr#   r   text_delimitermetrics_formatc                    s8   || _ i | _d| _i | _|| _|| _|| _t   d S Nr   )	_trainerrL   _current_task_id_metrics_style_text_delimiter_metrics_formatrG   rX   )r/   rm   r#   ro   rp   rI   r0   r1   rX      s   zMetricsTextColumn.__init__metricsr   Nc                 C   s
   || _ d S r8   )rt   )r/   rx   r0   r0   r1   update   s   
zMetricsTextColumn.updater   r   c                 C   s   t | jjts	J | jjjdks| jjs| jjj|jkrt	 S | jj
rI|j| jvrId| j|j< | jrEtt| j| _| j| j d | j| j< |j| _| jj
rY|j| jkrY| j|j S |  }| j|}t	|d| jdS )NfitNoner   left)justifyr#   )
isinstancerr   progress_bar_callbackRichProgressBarstatefnsanity_checkingtrain_progress_bar_ididr   trainingrL   _renderable_cacher	   r   rs   _generate_metrics_textsrv   joinru   )r/   r   metrics_textstextr0   r0   r1   r2      s"   zMetricsTextColumn.renderc                 c   s@    | j  D ]\}}t|ts|| j }| d| V  qd S )Nz: )rt   itemsr~   rR   rw   )r/   namevaluer0   r0   r1   r      s   
z)MetricsTextColumn._generate_metrics_texts)r3   r4   r5   r6   r   rR   rX   r   r   ry   r   r2   r   r   rT   r0   r0   rI   r1   rl      s    
 rl   c                   @   s   e Zd ZU dZdZeedf ed< dZeedf ed< dZ	eedf ed< dZ
eedf ed< dZeedf ed	< d
Zeedf ed< dZeedf ed< dZeedf ed< dZeed< dZeed< dS )RichProgressBarThemea  Styles to associate to different base components.

    Args:
        description: Style for the progress bar description. For eg., Epoch x, Testing, etc.
        progress_bar: Style for the bar in progress.
        progress_bar_finished: Style for the finished progress bar.
        progress_bar_pulse: Style for the progress bar when `IterableDataset` is being processed.
        batch_progress: Style for the progress tracker (i.e 10/50 batches completed).
        time: Style for the processed time and estimate time remaining.
        processing_speed: Style for the speed of the batches being processed.
        metrics: Style for the metrics

    https://rich.readthedocs.io/en/stable/style.html

    whiter   r?   z#6206E0progress_barprogress_bar_finishedprogress_bar_pulsebatch_progressgrey54timegrey70processing_speedrx    metrics_text_delimiterz.3frp   N)r3   r4   r5   r6   r?   r   rR   __annotations__r   r   r   r   r   r   rx   r   rp   r0   r0   r0   r1   r      s   
 r   c                       s  e Zd ZdZdde dfdedededeee	e
f  d	df
 fd
dZed	efddZed	efddZed	efddZeduddZeduddZeduddZeduddZdvddZedvddZedvdd Zdwd#d$Zdvd%d&Zedxd)d*Zedxd+d,Zedxd-d.Zedxd/d0Zedxd1d2Zedxd3d4Z edxd5d6Z!e	7dyd!d"d'd(d8e
d9ed:ed	dfd;d<Z"dzd>e#eef d?e	d@ed	dAfdBdCZ$dzdDedA dEed@ed	dfdFdGZ%dEedHe#eef d	efdIdJZ&edxdKdLZ'edxdMdNZ(edxdOdPZ)edxdQdRZ*e	7dyd!d"d'd(d8e
d9ed:ed	dfdSdTZ+e	7dyd!d"d'd(d8e
d9ed:ed	dfdUdVZ,ed!d"d'd(dWe-d8e
d9ed	dfdXdYZ.edxdZd[Z/e	7dyd!d"d'd(dWe-d8e
d9ed:ed	dfd\d]Z0e	7dyd!d"d'd(dWe-d8e
d9ed:ed	dfd^d_Z1e	7dyd!d"d'd(dWe
d8e
d9ed:ed	dfd`daZ2dbed	e	fdcddZ3dvdedfZ4dvdgdhZ5dxdidjZ6ed!d"d'd(dke	d	dfdldmZ7ed!d"d'd(dne8d	dfdodpZ9d!d"d	e:fdqdrZ;d	efdsdtZ<  Z=S ){r   u]  Create a progress bar with `rich text formatting <https://github.com/Textualize/rich>`_.

    Install it with pip:

    .. code-block:: bash

        pip install rich

    .. code-block:: python

        from pytorch_lightning import Trainer
        from pytorch_lightning.callbacks import RichProgressBar

        trainer = Trainer(callbacks=RichProgressBar())

    Args:
        refresh_rate: Determines at which rate (in number of batches) the progress bars get updated.
            Set it to ``0`` to disable the display.
        leave: Leaves the finished progress bar in the terminal at the end of the epoch. Default: False
        theme: Contains styles used to stylize the progress bar.
        console_kwargs: Args for constructing a `Console`

    Raises:
        ModuleNotFoundError:
            If required `rich` package is not installed on the device.

    Note:
        PyCharm users will need to enable “emulate terminal” in output console option in
        run/debug configuration to see styled output.
        Reference: https://rich.readthedocs.io/en/latest/introduction.html#requirements

    r   FNrefresh_rateleavethemeconsole_kwargsr   c                    sz   t stdt   || _|| _d | _|pi | _d| _d | _	|  d | _
|  |  |  |   d | _d| _|| _|   d S )NzY`RichProgressBar` requires `rich` >= 10.2.2. Install it by running `pip install -U rich`.TF)_RICH_AVAILABLEModuleNotFoundErrorrG   rX   _refresh_rate_leave_console_console_kwargs_enabledprogressval_sanity_progress_bar_id_reset_progress_bar_ids_metric_component_progress_stoppedr   _update_for_light_colab_theme)r/   r   r   r   r   rI   r0   r1   rX      s*   

zRichProgressBar.__init__c                 C   s   | j S r8   )r   r9   r0   r0   r1   r     s   zRichProgressBar.refresh_ratec                 C   s   | j o| jdkS rq   )r   r   r9   r0   r0   r1   
is_enabled!     zRichProgressBar.is_enabledc                 C   s   | j  S r8   )r   r9   r0   r0   r1   is_disabled%  s   zRichProgressBar.is_disabledr   c                 C   *   | j d usJ | jd usJ | j j| j S r8   )r   r   tasksr9   r0   r0   r1   train_progress_bar)     z"RichProgressBar.train_progress_barc                 C   r   r8   )r   r   r   r9   r0   r0   r1   val_sanity_check_bar/  r   z$RichProgressBar.val_sanity_check_barc                 C   r   r8   )r   val_progress_bar_idr   r9   r0   r0   r1   val_progress_bar5  r   z RichProgressBar.val_progress_barc                 C   r   r8   )r   test_progress_bar_idr   r9   r0   r0   r1   test_progress_bar;  r   z!RichProgressBar.test_progress_barc                 C   s>   t  rg d}|D ]}t| j|dkrt| j|d q	d S d S )N)r?   r   rx   r   black)_detect_light_colab_themegetattrr   setattr)r/   
attributesattrr0   r0   r1   r   A  s   z-RichProgressBar._update_for_light_colab_themec                 C   
   d| _ d S )NFr   r9   r0   r0   r1   disableH     
zRichProgressBar.disablec                 C   r   NTr   r9   r0   r0   r1   enableL  r   zRichProgressBar.enablerm   rn   c                 C   s   | j rM| jd u s| jrO|   tdi | j t | _| j  t	|| j
j| j
j| j
j| _tg | || jR d| j| jd| _| j  d| _d S d S d S )NF)auto_refreshr   consoler0   )r   r   r   r   r   r   r   r   
clear_liverl   r   rx   r   rp   r   r=   configure_columnsr   r@   r/   rm   r0   r0   r1   _init_progressP  s.   



zRichProgressBar._init_progressc                 C   s   | j r
| j   d S d S r8   )r   rO   r9   r0   r0   r1   rO   g  s   zRichProgressBar.refresh	pl_modulepl.LightningModulec                 C      |  | d S r8   r   r/   rm   r   r0   r0   r1   on_train_startk     zRichProgressBar.on_train_startc                 C   r   r8   r   r   r0   r0   r1   on_predict_starto  r   z RichProgressBar.on_predict_startc                 C   r   r8   r   r   r0   r0   r1   on_test_starts  r   zRichProgressBar.on_test_startc                 C   r   r8   r   r   r0   r0   r1   on_validation_startw  r   z#RichProgressBar.on_validation_startc                 C   r   r8   r   r   r0   r0   r1   on_sanity_check_start{  r   z%RichProgressBar.on_sanity_check_startc                 C   s8   | j d ur| jd usJ | j j| jddd |   d S Nr   FadvancerA   )r   r   ry   rO   r   r0   r0   r1   on_sanity_check_end  s   
z#RichProgressBar.on_sanity_check_endc                 C   s   | j rd S | j}| |j}| jd ur| jr|   | | | jd ur<| jd u r1| 	||| _n| jj
| j||dd |   d S )NT)r   r?   rA   )r   total_train_batches_get_train_descriptioncurrent_epochr   r   _stop_progressr   r   	_add_taskresetrO   )r/   rm   r   total_batchestrain_descriptionr0   r0   r1   on_train_epoch_start  s"   


z$RichProgressBar.on_train_epoch_startr   batch	batch_idxdataloader_idxc                 C   s   | j s| |s
d S | jd usJ |jr/| jd ur#| jj| jddd | j| j| jdd| _n| j	d ur>| jj| j	ddd | j| j| j
dd| _	|   d S )Nr   Fr   )rA   )r   has_dataloader_changedr   r   r   ry   r   $total_val_batches_current_dataloadersanity_check_descriptionr   validation_descriptionrO   r/   rm   r   r   r   r   r0   r0   r1   on_validation_batch_start  s&   	


z)RichProgressBar.on_validation_batch_startTr   r?   rA   r   c                 C   s0   | j d usJ | j jd| jj d| ||dS )N[])r   rA   )r   rH   r   r?   )r/   r   r?   rA   r0   r0   r1   r     s   zRichProgressBar._add_taskprogress_bar_idcurrentc                 C   s   | j d urD| jrF|d usJ | j j| j}|d usJ | ||s#d S || j }||kr2|dkr2|n| j}| j j|||d |   d S d S d S )Nr   r   )r   r   r   r   _should_updater   ry   rO   )r/   r   r   rA   r   leftoverr   r0   r0   r1   _update  s   
zRichProgressBar._updater   c                 C   s   || j  dkp
||kS rq   )r   )r/   r   r   r0   r0   r1   r     s   zRichProgressBar._should_updatec                 C   sV   | j r%| jd ur'|jjdkr)| jd usJ | jj| jddd |   d S d S d S d S )Nrz   r   Fr   )r   r   r   r   r   ry   rO   r   r0   r0   r1   on_validation_epoch_end  s
   z'RichProgressBar.on_validation_epoch_endc                 C   s$   |j jdkr| || |   d S )Nrz   )r   r   _update_metricsreset_dataloader_idx_trackerr   r0   r0   r1   on_validation_end  s   z!RichProgressBar.on_validation_endc                 C      |    d S r8   r   r   r0   r0   r1   on_test_end     zRichProgressBar.on_test_endc                 C   r   r8   r   r   r0   r0   r1   on_predict_end  r   zRichProgressBar.on_predict_endc                 C   ^   | j s| |s
d S | jd ur | jd usJ | jj| jddd | | j| j| _|   d S r   )	r   r   r   r   ry   r   %total_test_batches_current_dataloadertest_descriptionrO   r   r0   r0   r1   on_test_batch_start  s   	
z#RichProgressBar.on_test_batch_startc                 C   r   r   )	r   r   predict_progress_bar_idr   ry   r   (total_predict_batches_current_dataloaderpredict_descriptionrO   r   r0   r0   r1   on_predict_batch_start   s   	
z&RichProgressBar.on_predict_batch_startoutputsc                 C   s*   |  | j|d  | || |   d S rK   )r   r   r   rO   )r/   rm   r   r  r   r   r0   r0   r1   on_train_batch_end  s   	z"RichProgressBar.on_train_batch_endc                 C   s   |  || d S r8   )r   r   r0   r0   r1   on_train_epoch_end!  r   z"RichProgressBar.on_train_epoch_endc                 C   sL   | j rd S |jr| | j|d  n| jd ur | | j|d  |   d S rK   )r   r   r   r   r   rO   r/   rm   r   r  r   r   r   r0   r0   r1   on_validation_batch_end%  s   

z'RichProgressBar.on_validation_batch_endc                 C   6   | j rd S | jd usJ | | j|d  |   d S rK   )r   r   r   rO   r  r0   r0   r1   on_test_batch_end7  
   
z!RichProgressBar.on_test_batch_endc                 C   r	  rK   )r   r   r   rO   r  r0   r0   r1   on_predict_batch_endG  r  z$RichProgressBar.on_predict_batch_endr   c                 C   sR   d| }| j jd ur|d| j jd  7 }t| jt|kr'|t| j }|S )NzEpoch rf   r   )rm   
max_epochslenr   )r/   r   r   r0   r0   r1   r   W  s   
z&RichProgressBar._get_train_descriptionc                 C   s"   | j d ur| j   d| _d S d S r   )r   stopr   r9   r0   r0   r1   r   a  s   


zRichProgressBar._stop_progressc                 C   s"   d | _ d | _d | _d | _d | _d S r8   )r   r   r   r   r   r9   r0   r0   r1   r   g  s
   
z'RichProgressBar._reset_progress_bar_idsc                 C   s&   |  ||}| jr| j| d S d S r8   )get_metricsr   ry   )r/   rm   r   rx   r0   r0   r1   r   n  s   zRichProgressBar._update_metricsstagec                 C   r   r8   r   )r/   rm   r   r  r0   r0   r1   teardowns  r   zRichProgressBar.teardown	exceptionc                 C   r   r8   r  )r/   rm   r   r  r0   r0   r1   on_exceptionw  s   zRichProgressBar.on_exceptionc                 C   sF   t dt| jj| jj| jjdt| jjdt| jj	dt
| jjdgS )Nz([progress.description]{task.description})r$   r%   r&   r\   )r   r   r   r   r   r   rd   r   rU   r   ri   r   r   r0   r0   r1   r     s   z!RichProgressBar.configure_columnsc                 C   s   | j  }d |d< d |d< |S )Nr   r   )__dict__copy)r/   r   r0   r0   r1   __getstate__  s   
zRichProgressBar.__getstate__)r   r   )r   N)rm   rn   r   N)rm   rn   r   r   r   N)r   rQ   )>r3   r4   r5   r6   r   rN   rS   r   r   rR   r   rX   r;   r<   r   r   r   r   r   r   r   r   r   r   r   r   rO   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r  r  r  r
  r  r   r   r   r   r  BaseExceptionr  listr   r  rT   r0   r0   rI   r1   r      s   #


$# 



r   r   c               	   C   sl   zddl } W n ttfy   Y dS w |  }dt|jv r4zddlm} |dW S  ty3   Y dS w dS )z$Detect if it's light theme in Colab.r   NFzgoogle.colab)outputz1document.documentElement.matches("[theme=light]"))get_ipython	NameErrorr   rR   rJ   google.colabr  eval_js)r  ipythonr  r0   r0   r1   r     s   r   )5r,   dataclassesr   datetimer   typingr   r   r   r   r   r	    lightning_utilities.core.importsr
   typing_extensionsr   pytorch_lightningpl1pytorch_lightning.callbacks.progress.progress_barr   !pytorch_lightning.utilities.typesr   r   richr   r   rich.consoler   r   rich.progressr   r   r   r   r   r   rich.progress_barr(   
rich.styler   	rich.textr   r   r7   r=   rU   rd   ri   rl   r   r   rS   r   r0   r0   r0   r1   <module>   s@     %		5   ;