o
    ziB                     @   s  d dl Z d dlZd dlZd dlZd dlmZmZmZmZ d dl	m
Z
 d dlmZ e jddur7d dlmZ nd dlmZ d dlZd dlmZ d dlmZ d	ZG d
d deZG dd deZdeeeef  deeeef  fddZdededdfddZdS )    N)AnyDictOptionalUnion)override)STEP_OUTPUT
ipywidgets)tqdm)ProgressBar)rank_zero_debug   c                       sJ   e Zd Zdededdf fddZedeeee	f de	fdd	Z
  ZS )
TqdmargskwargsreturnNc                    s   t  j|i | dS )zyCustom tqdm progressbar where we append 0 to floating points/strings to prevent the progress bar from
        flickering.N)super__init__)selfr   r   	__class__ f/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/callbacks/progress/tqdm_progress.pyr   (   s   zTqdm.__init__nc                 C   s   t | ttf}t | tst| } t | tsJ |rHd| vrHd| vr>t| tk r>zt| }W n ty9   |  Y S w | d7 } | dtt|   7 } | S )z0Add additional padding to the formatted numbers.e.0)
isinstancefloatstr_tqdm
format_numlen	_PAD_SIZE
ValueError)r   should_be_padded_r   r   r   r    .   s   

zTqdm.format_num)__name__
__module____qualname__r   r   staticmethodr   intr   r   r    __classcell__r   r   r   r   r   '   s    &r   c                       s  e Zd ZdZdZdndededef fd	d
ZdefddZ	e
defddZejdeddfddZe
defddZejdeddfddZe
defddZejdeddfddZe
defddZejdedd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
defd"d#Zedod$d%Zedod&d'Zdefd(d)Zdefd*d+Zdefd,d-Zdefd.d/Zdefd0d1Zed2eddfd3d4Zed2eddfd5d6Z ed2eddfd7d8Z!ed9d:d2eddfd;d<Z"ed9d:d=d>d?e#d@edAeddfdBdCZ$edpdDdEZ%ed2eddfdFdGZ&edpdHdIZ'e	dqd9d:d=d>d@edAedJeddfdKdLZ(e	dqd9d:d=d>d?e#d@edAedJeddfdMdNZ)edpdOdPZ*edpdQdRZ+e	dqd9d:d=d>d@edAedJeddfdSdTZ,e	dqd9d:d=d>d?e#d@edAedJeddfdUdVZ-edpdWdXZ.edpdYdZZ/e	dqd9d:d=d>d@edAedJeddfd[d\Z0e	dqd9d:d=d>d?ed@edAedJeddfd]d^Z1edpd_d`Z2edadbdcedde3deeddfdfdgZ4dhediedefdjdkZ5e6dedefdldmZ7  Z8S )rTQDMProgressBara  This is the default progress bar used by Lightning. It prints to ``stdout`` using the :mod:`tqdm` package and
    shows up to four different bars:

        - **sanity check progress:** the progress during the sanity check run
        - **train progress:** shows the training progress. It will pause if validation starts and will resume
          when it ends, and also accounts for multiple validation runs during training when
          :paramref:`~pytorch_lightning.trainer.trainer.Trainer.val_check_interval` is used.
        - **validation progress:** only visible during validation;
          shows total progress over all validation datasets.
        - **test progress:** only active when testing; shows total progress over all test datasets.

    For infinite datasets, the progress bar never ends.

    If you want to customize the default ``tqdm`` progress bars used by Lightning, you can override
    specific methods of the callback class and pass your custom implementation to the
    :class:`~pytorch_lightning.trainer.trainer.Trainer`.

    Example:

        >>> class LitProgressBar(TQDMProgressBar):
        ...     def init_validation_tqdm(self):
        ...         bar = super().init_validation_tqdm()
        ...         bar.set_description('running validation ...')
        ...         return bar
        ...
        >>> bar = LitProgressBar()
        >>> from pytorch_lightning import Trainer
        >>> trainer = Trainer(callbacks=[bar])

    Args:
        refresh_rate: Determines at which rate (in number of batches) the progress bars get updated.
            Set it to ``0`` to disable the display.
        process_position: Set this to a value greater than ``0`` to offset the progress bars by this many lines.
            This is useful when you have progress bars defined elsewhere and want to show all of them
            together.
        leave: If set to ``True``, leaves the finished progress bar in the terminal at the end of the epoch.
            Default: ``False``

    zT{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_noinv_fmt}{postfix}]   r   Frefresh_rateprocess_positionleavec                    sD   t    | || _|| _d| _d | _d | _d | _d | _	|| _
d S NT)r   r   _resolve_refresh_rate_refresh_rate_process_position_enabled_train_progress_bar_val_progress_bar_test_progress_bar_predict_progress_bar_leave)r   r.   r/   r0   r   r   r   r   k   s   

zTQDMProgressBar.__init__r   c                 C   s   dd t |  D S )Nc                 S   s$   i | ]\}}|t |ts|nd qS N)r   r   ).0kvr   r   r   
<dictcomp>x   s   $ z0TQDMProgressBar.__getstate__.<locals>.<dictcomp>)varsitemsr   r   r   r   __getstate__v   s   zTQDMProgressBar.__getstate__c                 C   $   | j d u rtd| jj d| j S )NThe `z5._train_progress_bar` reference has not been set yet.)r6   	TypeErrorr   r&   rB   r   r   r   train_progress_barz      
z"TQDMProgressBar.train_progress_barbarNc                 C   
   || _ d S r;   )r6   r   rI   r   r   r   rG         
c                 C   rD   )NrE   z3._val_progress_bar` reference has not been set yet.)r7   rF   r   r&   rB   r   r   r   val_progress_bar   rH   z TQDMProgressBar.val_progress_barc                 C   rJ   r;   )r7   rK   r   r   r   rM      rL   c                 C   rD   )NrE   z4._test_progress_bar` reference has not been set yet.)r8   rF   r   r&   rB   r   r   r   test_progress_bar   rH   z!TQDMProgressBar.test_progress_barc                 C   rJ   r;   )r8   rK   r   r   r   rN      rL   c                 C   rD   )NrE   z7._predict_progress_bar` reference has not been set yet.)r9   rF   r   r&   rB   r   r   r   predict_progress_bar   rH   z$TQDMProgressBar.predict_progress_barc                 C   rJ   r;   )r9   rK   r   r   r   rO      rL   c                 C      | j S r;   )r3   rB   r   r   r   r.         zTQDMProgressBar.refresh_ratec                 C   rP   r;   )r4   rB   r   r   r   r/      rQ   z TQDMProgressBar.process_positionc                 C   s   | j o| jdkS Nr   )r5   r.   rB   r   r   r   
is_enabled   s   zTQDMProgressBar.is_enabledc                 C   s   | j  S r;   )rS   rB   r   r   r   is_disabled   s   zTQDMProgressBar.is_disabledc                 C   
   d| _ d S )NFr5   rB   r   r   r   disable   rL   zTQDMProgressBar.disablec                 C   rU   r1   rV   rB   r   r   r   enable   rL   zTQDMProgressBar.enablec              	   C   s$   t | jd| j | jddtj| jdS )zFOverride this to customize the tqdm bar for the validation sanity run.   FTdescpositionrW   r0   dynamic_ncolsfile
bar_format)r   sanity_check_descriptionr/   rT   sysstdout
BAR_FORMATrB   r   r   r   init_sanity_tqdm   s   z TQDMProgressBar.init_sanity_tqdmc              
   C   &   t | jd| j | jddtjd| jdS )z5Override this to customize the tqdm bar for training.rY   Tr   r[   r\   rW   r0   r]   r^   	smoothingr_   )r   train_descriptionr/   rT   ra   rb   rc   rB   r   r   r   init_train_tqdm      zTQDMProgressBar.init_train_tqdmc              
   C   re   )z7Override this to customize the tqdm bar for predicting.rY   Tr   rf   )r   predict_descriptionr/   rT   ra   rb   rc   rB   r   r   r   init_predict_tqdm   rj   z!TQDMProgressBar.init_predict_tqdmc              	   C   s8   | j jjdk}t| jd| j | | j| dtj| j	dS )z7Override this to customize the tqdm bar for validation.validaterY   TrZ   )
trainerstatefnr   validation_descriptionr/   rT   ra   rb   rc   )r   has_main_barr   r   r   init_validation_tqdm   s   z$TQDMProgressBar.init_validation_tqdmc              	   C   s"   t dd| j | jddtj| jdS )z4Override this to customize the tqdm bar for testing.TestingrY   TrZ   )r   r/   rT   ra   rb   rc   rB   r   r   r   init_test_tqdm   s   zTQDMProgressBar.init_test_tqdmr%   c                 G   s   |   | _tdd| _d S )NT)rW   )rd   rM   r   rG   r   r%   r   r   r   on_sanity_check_start   s   
z%TQDMProgressBar.on_sanity_check_startc                 G   s   | j   | j  d S r;   )rM   closerG   rv   r   r   r   on_sanity_check_end   s   
z#TQDMProgressBar.on_sanity_check_endc                 G      |   | _d S r;   )ri   rG   rv   r   r   r   on_train_start     zTQDMProgressBar.on_train_startrn   
pl.Trainerc                 G   sB   | j r|  | _| jt| j d| j_| jd|j  d S )Nr   zEpoch )	r:   ri   rG   resetconvert_inftotal_train_batchesinitialset_descriptioncurrent_epoch)r   rn   r%   r   r   r   on_train_epoch_start  s
   
z$TQDMProgressBar.on_train_epoch_start	pl_modulepl.LightningModuleoutputsbatch	batch_idxc                 C   s@   |d }|  || jjrt| j| | j| || d S d S Nr-   )_should_updaterG   total	_update_nset_postfixget_metrics)r   rn   r   r   r   r   r   r   r   r   on_train_batch_end  s
   z"TQDMProgressBar.on_train_batch_endc                 C   s4   | j js| j | || | jr| j   d S d S r;   )rG   rW   r   r   r:   rx   r   rn   r   r   r   r   on_train_epoch_end  s
   z"TQDMProgressBar.on_train_epoch_endc                 G   s   | j   d S r;   )rG   rx   rv   r   r   r   on_train_end   r|   zTQDMProgressBar.on_train_endc                 C   s   |j s
|  | _d S d S r;   )sanity_checkingrs   rM   r   r   r   r   on_validation_start$  s   z#TQDMProgressBar.on_validation_startdataloader_idxc                 C   sT   |  |sd S | jt| j d| j_|jr| jn| j}| j	| d|  d S Nr   z DataLoader )
has_dataloader_changedrM   r~   r   $total_val_batches_current_dataloaderr   r   r`   rq   r   )r   rn   r   r   r   r   r[   r   r   r   on_validation_batch_start)  s   
	z)TQDMProgressBar.on_validation_batch_startc                 C   ,   |d }|  || jjrt| j| d S d S r   )r   rM   r   r   r   rn   r   r   r   r   r   r   r   r   r   on_validation_batch_end:     
z'TQDMProgressBar.on_validation_batch_endc                 C   sH   | j   |   | jd ur |jjdkr"| j| || d S d S d S )Nfit)	rM   rx   reset_dataloader_idx_trackerr6   ro   rp   rG   r   r   r   r   r   r   on_validation_endH  s
   
z!TQDMProgressBar.on_validation_endc                 C   rz   r;   )ru   rN   r   r   r   r   on_test_startO  r|   zTQDMProgressBar.on_test_startc                 C   D   |  |sd S | jt| j d| j_| j| j d|  d S r   )r   rN   r~   r   %total_test_batches_current_dataloaderr   r   test_descriptionr   rn   r   r   r   r   r   r   r   on_test_batch_startS  
   
	z#TQDMProgressBar.on_test_batch_startc                 C   r   r   )r   rN   r   r   r   r   r   r   on_test_batch_endc  r   z!TQDMProgressBar.on_test_batch_endc                 C      | j   |   d S r;   )rN   rx   r   r   r   r   r   on_test_endq     
zTQDMProgressBar.on_test_endc                 C   rz   r;   )rl   rO   r   r   r   r   on_predict_startv  r|   z TQDMProgressBar.on_predict_startc                 C   r   r   )r   rO   r~   r   (total_predict_batches_current_dataloaderr   r   rk   r   r   r   r   on_predict_batch_startz  r   z&TQDMProgressBar.on_predict_batch_startc                 C   r   r   )r   rO   r   r   r   r   r   r   on_predict_batch_end  r   z$TQDMProgressBar.on_predict_batch_endc                 C   r   r;   )rO   rx   r   r   r   r   r   on_predict_end  r   zTQDMProgressBar.on_predict_end )sepr   r   r   c                O   s   d }| j d ur| jjs| j}n&| jd ur| jjs| j}n| jd ur)| jjs)| j}n| jd ur5| jjs5| j}|d urL|	t
t|}|j|fi | d S d S r;   )r6   rG   rW   r7   rM   r8   rN   r9   rO   joinmapr   write)r   r   r   r   active_progress_barsr   r   r   print  s   zTQDMProgressBar.printcurrentr   c                 C   s   | j o|| j dkp||kS rR   )rS   r.   )r   r   r   r   r   r   r     s   zTQDMProgressBar._should_updatec                 C   s@   t dr| dkrtd dS dt jv rttt jd | S | S )N	COLAB_GPUr-   z8Using a higher refresh rate on Colab. Setting it to `20`   TQDM_MINITERS)osgetenvr   environmaxr*   )r.   r   r   r   r2     s   
z%TQDMProgressBar._resolve_refresh_rate)r-   r   F)r   N)rn   r}   r   r   r   N)r   )9r&   r'   r(   __doc__rc   r*   boolr   r   rC   propertyr   rG   setterrM   rN   rO   r.   r/   rS   rT   r   rW   rX   r   rd   ri   rl   rs   ru   r   rw   ry   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r)   r2   r+   r   r   r   r   r,   @   sb   ("r,   xr   c                 C   s$   | du st | st | rdS | S )zRThe tqdm doesn't support inf/nan values.

    We have to convert it to None.

    N)mathisinfisnan)r   r   r   r   r     s   r   rI   valuec                 C   s   | j s|| _|   d S d S r;   )rW   r   refresh)rI   r   r   r   r   r     s   r   )	importlibr   r   ra   typingr   r   r   r   typing_extensionsr   !pytorch_lightning.utilities.typesr   util	find_spec	tqdm.autor	   r   pytorch_lightningpl1pytorch_lightning.callbacks.progress.progress_barr
   %pytorch_lightning.utilities.rank_zeror   r"   r   r,   r*   r   r   r   r   r   r   r   <module>   s(     *