o
    zi"                     @   sz   d dl mZmZmZmZ d dlmZ d dlZd dl	m
Z
 d dlmZ G dd de
Zdd	d
eeeeef f fddZdS )    )AnyDictOptionalUnion)overrideN)Callback)rank_zero_warnc                   @   s  e Zd ZdZd2ddZed3d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defddZedeeef fddZedeeef fddZedeeef fddZedeeef fddZedeeef fddZdedefddZd2d d!Zd2d"d#Zd2d$d%Zd&ed'eddfd(d)Zed*dd+d,d-eddfd.d/Zd*dd+d,deeeeeeeeef f f fd0d1ZdS )4ProgressBara  The base class for progress bars in Lightning. It is a :class:`~pytorch_lightning.callbacks.Callback` that keeps
    track of the batch progress in the :class:`~pytorch_lightning.trainer.trainer.Trainer`. You should implement your
    highly custom progress bars with this as the base class.

    Example::

        class LitProgressBar(ProgressBar):

            def __init__(self):
                super().__init__()  # don't forget this :)
                self.enable = True

            def disable(self):
                self.enable = False

            def on_train_batch_end(self, trainer, pl_module, outputs, batch, batch_idx):
                super().on_train_batch_end(trainer, pl_module, outputs, batch, batch_idx)  # don't forget this :)
                percent = (batch_idx / self.total_train_batches) * 100
                sys.stdout.flush()
                sys.stdout.write(f'{percent:.01f} percent complete \r')

        bar = LitProgressBar()
        trainer = Trainer(callbacks=[bar])

    returnNc                 C   s   d | _ d | _d S N)_trainer_current_eval_dataloader_idxself r   e/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/callbacks/progress/progress_bar.py__init__2   s   
zProgressBar.__init__
pl.Trainerc                 C   s$   | j d u rtd| jj d| j S )NzThe `z*._trainer` reference has not been set yet.)r   	TypeError	__class____name__r   r   r   r   trainer6   s   
zProgressBar.trainerc                 C      dS )NzSanity Checkingr   r   r   r   r   sanity_check_description<      z$ProgressBar.sanity_check_descriptionc                 C   r   )NTrainingr   r   r   r   r   train_description@   r   zProgressBar.train_descriptionc                 C   r   )N
Validationr   r   r   r   r   validation_descriptionD   r   z"ProgressBar.validation_descriptionc                 C   r   )NTestingr   r   r   r   r   test_descriptionH   r   zProgressBar.test_descriptionc                 C   r   )N
Predictingr   r   r   r   r   predict_descriptionL   r   zProgressBar.predict_descriptionc                 C   s   | j jS )zThe total number of training batches, which may change from epoch to epoch.

        Use this to set the total number of iterations in the progress bar. Can return ``inf`` if the training
        dataloader is of infinite size.

        )r   num_training_batchesr   r   r   r   total_train_batchesP   s   zProgressBar.total_train_batchesc                 C   s>   | j jr| j jn| j j}t|tr| jdusJ || j S |S )a  The total number of validation batches, which may change from epoch to epoch for current dataloader.

        Use this to set the total number of iterations in the progress bar. Can return ``inf`` if the validation
        dataloader is of infinite size.

        N)r   sanity_checkingnum_sanity_val_batchesnum_val_batches
isinstancelistr   r   batchesr   r   r   $total_val_batches_current_dataloaderZ   s
   

z0ProgressBar.total_val_batches_current_dataloaderc                 C   s.   | j j}t|tr| jdusJ || j S |S )zThe total number of testing batches, which may change from epoch to epoch for current dataloader.

        Use this to set the total number of iterations in the progress bar. Can return ``inf`` if the test dataloader is
        of infinite size.

        N)r   num_test_batchesr(   r)   r   r*   r   r   r   %total_test_batches_current_dataloaderh   s
   

z1ProgressBar.total_test_batches_current_dataloaderc                 C   s   | j dusJ | jj| j  S )a  The total number of prediction batches, which may change from epoch to epoch for current dataloader.

        Use this to set the total number of iterations in the progress bar. Can return ``inf`` if the predict dataloader
        is of infinite size.

        N)r   r   num_predict_batchesr   r   r   r   (total_predict_batches_current_dataloaderv   s   z4ProgressBar.total_predict_batches_current_dataloaderc                 C   s4   | j jj s	dS t| j jtrt| j jS | j jS )a  The total number of validation batches, which may change from epoch to epoch for all val dataloaders.

        Use this to set the total number of iterations in the progress bar. Can return ``inf`` if the predict dataloader
        is of infinite size.

        r   )r   fit_loop
epoch_loop_should_check_val_epochr(   r'   r)   sumr   r   r   r   total_val_batches   s   zProgressBar.total_val_batchesdataloader_idxc                 C   s   | j }|| _ ||kS r   r   )r   r6   old_dataloader_idxr   r   r   has_dataloader_changed   s   z"ProgressBar.has_dataloader_changedc                 C   s
   d | _ d S r   r7   r   r   r   r   reset_dataloader_idx_tracker   s   
z(ProgressBar.reset_dataloader_idx_trackerc                 C      t )z5You should provide a way to disable the progress bar.NotImplementedErrorr   r   r   r   disable   r   zProgressBar.disablec                 C   r;   )aO  You should provide a way to enable the progress bar.

        The :class:`~pytorch_lightning.trainer.trainer.Trainer` will call this in e.g. pre-training
        routines like the :ref:`learning rate finder <advanced/training_tricks:Learning Rate Finder>`.
        to temporarily enable and disable the training progress bar.

        r<   r   r   r   r   enable   s   zProgressBar.enableargskwargsc                 O   s   t |i | dS )zDYou should provide a way to print without breaking the progress bar.N)print)r   r@   rA   r   r   r   rB      s   zProgressBar.printr   	pl_modulezpl.LightningModulestagec                 C   s   || _ |js|   d S d S r   )r   is_global_zeror>   )r   r   rC   rD   r   r   r   setup   s   zProgressBar.setupc                 C   sR   t |}|j}t| | @ }|r#tdd| d|d  d i ||S )a^  Combines progress bar metrics collected from the trainer with standard metrics from get_standard_metrics.
        Implement this to override the items displayed in the progress bar.

        Here is an example of how to override the defaults:

        .. code-block:: python

            def get_metrics(self, trainer, model):
                # don't show the version number
                items = super().get_metrics(trainer, model)
                items.pop("v_num", None)
                return items

        Return:
            Dictionary with the items to be displayed in the progress bar.

        z;The progress bar already tracks a metric with the name(s) 'z, z' and `self.log('r   z', ..., prog_bar=True)` will overwrite this value.  If this is undesired, change the name or override `get_metrics()` in the progress bar callback.)get_standard_metricsprogress_bar_metricsr)   keysr   join)r   r   rC   standard_metricspbar_metrics
duplicatesr   r   r   get_metrics   s   zProgressBar.get_metrics)r
   N)r
   r   ) r   
__module____qualname____doc__r   propertyr   strr   r   r   r    r"   r   intfloatr$   r,   r.   r0   r5   boolr9   r:   r>   r?   r   rB   r   rF   r   rN   r   r   r   r   r	      sN    
	




r	   r   r   r
   c                 C   sJ   i }| j r#ddlm} || j  }dvr#t|tr|dd }||d< |S )uX  Returns the standard metrics displayed in the progress bar. Currently, it only includes the version of the
    experiment when using a logger.

    .. code-block::

        Epoch 1:   4%|▎         | 40/1095 [00:03<01:37, 10.84it/s, v_num=10]

    Return:
        Dictionary with the standard metrics to be displayed in the progress bar.

    r   )_version) NNv_num)loggers#pytorch_lightning.loggers.utilitiesrW   r(   rS   )r   
items_dictrW   versionr   r   r   rG      s   
rG   )typingr   r   r   r   typing_extensionsr   pytorch_lightningplpytorch_lightning.callbacksr   %pytorch_lightning.utilities.rank_zeror   r	   rS   rT   rG   r   r   r   r   <module>   s    &<