o
    ziU2                     @   s  d dl 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mZ d dlmZ d dlmZmZ d d	lmZmZ d d
lmZmZmZ d dlmZ d dlmZm Z  d dl!m"Z" d dl#m$Z$ edZ%edZ&ere%ryd dl'm(Z( nd dl)m(Z( G dd deZ*dS )    N)	Namespace)TYPE_CHECKINGAnyDictMappingOptionalUnion)RequirementCache)Tensor)Module)override)Loggerrank_zero_experiment)_is_dirget_filesystem)_add_prefix_convert_params_flatten_dict)_sanitize_params)rank_zero_onlyrank_zero_warn)_PATH)_unwrap_objectstensorboardtensorboardXSummaryWriterc                       s  e Zd ZdZdZ					d4dedee d	eee	ef  d
e
dedee def fddZeedefddZeedee	ef fddZeedefddZeedefddZedee fddZeed5ddZeed6deeef dee	 ddfd d!Zee	d6d"eeeef ef deeeef  ddfd#d$Zeed6d%ed&ee ddfd'd(Zeed7d)d*Z eed+eddfd,d-Z!de	fd.d/Z"e#d"eeef deeef fd0d1Z$deeef fd2d3Z%  Z&S )8TensorBoardLoggera  Log to local file system in `TensorBoard <https://www.tensorflow.org/tensorboard>`_ format.

    Implemented using :class:`~tensorboardX.SummaryWriter`. Logs are saved to
    ``os.path.join(root_dir, name, version)``. This is the recommended logger in Lightning Fabric.

    Args:
        root_dir: The root directory in which all your experiments with different names and versions will be stored.
        name: Experiment name. Defaults to ``'lightning_logs'``. If it is the empty string then no per-experiment
            subdirectory is used.
        version: Experiment version. If version is not specified the logger inspects the save
            directory for existing versions, then automatically assigns the next available version.
            If it is a string then it is used as the run-specific subdirectory name,
            otherwise ``'version_${version}'`` is used.
        default_hp_metric: Enables a placeholder metric with key `hp_metric` when `log_hyperparams` is
            called without a metric (otherwise calls to ``log_hyperparams`` without a metric are ignored).
        prefix: A string to put at the beginning of all metric keys.
        sub_dir: Sub-directory to group TensorBoard logs. If a ``sub_dir`` argument is passed
            then logs are saved in ``/root_dir/name/version/sub_dir/``. Defaults to ``None`` in which case
            logs are saved in ``/root_dir/name/version/``.
        \**kwargs: Additional arguments used by :class:`tensorboardX.SummaryWriter` can be passed as keyword
            arguments in this logger. To automatically flush to disk, `max_queue` sets the size
            of the queue for pending logs before flushing. `flush_secs` determines how many seconds
            elapses before flushing.


    Example::

        from lightning_fabric.loggers import TensorBoardLogger

        logger = TensorBoardLogger("path/to/logs/root", name="my_model")
        logger.log_hyperparams({"epochs": 5, "optimizer": "Adam"})
        logger.log_metrics({"acc": 0.75})
        logger.finalize("success")

    -lightning_logsNT root_dirnameversiondefault_hp_metricprefixsub_dirkwargsc                    s   t ststdtt dtt  t   t|}|| _|p"d| _	|| _
|d u r-d nt|| _|| _|| _t|| _d | _|| _d S )NzTNeither `tensorboard` nor `tensorboardX` is available. Try `pip install`ing either.

r    )_TENSORBOARD_AVAILABLE_TENSORBOARDX_AVAILABLEModuleNotFoundErrorstrsuper__init__osfspath	_root_dir_name_version_sub_dir_default_hp_metric_prefixr   _fs_experiment_kwargs)selfr!   r"   r#   r$   r%   r&   r'   	__class__ X/home/ubuntu/.local/lib/python3.10/site-packages/lightning_fabric/loggers/tensorboard.pyr.   Q   s&   





zTensorBoardLogger.__init__returnc                 C      | j S )zcGet the name of the experiment.

        Returns:
            The name of the experiment.

        )r2   r:   r=   r=   r>   r"   n      	zTensorBoardLogger.namec                 C   s   | j du r
|  | _ | j S )z~Get the experiment version.

        Returns:
            The experiment version if specified else the next version.

        N)r3   _get_next_versionrA   r=   r=   r>   r#   y   s   
	
zTensorBoardLogger.versionc                 C   r@   )zGets the save directory where the TensorBoard experiments are saved.

        Returns:
            The local path to the save directory where the TensorBoard experiments are saved.

        )r1   rA   r=   r=   r>   r!      rB   zTensorBoardLogger.root_dirc                 C   sj   t | jtr	| jnd| j }tj| j| j|}t | jtr'tj|| j}tj	|}tj
|}|S )zThe directory for this run's tensorboard checkpoint.

        By default, it is named ``'version_${self.version}'`` but it can be overridden by passing a string value for the
        constructor's version parameter instead of ``None`` or an int.

        version_)
isinstancer#   r,   r/   pathjoinr!   r"   r&   
expandvars
expanduser)r:   r#   log_dirr=   r=   r>   rJ      s   	zTensorBoardLogger.log_dirc                 C   r@   )zGets the sub directory where the TensorBoard experiments are saved.

        Returns:
            The local path to the sub directory where the TensorBoard experiments are saved.

        )r4   rA   r=   r=   r>   r&      s   zTensorBoardLogger.sub_dirr   c                 C   sv   | j dur| j S tjdksJ d| jr| jj| jdd tr&ddlm} nddl	m} |dd| j
i| j| _ | j S )	zActual tensorboard object. To use TensorBoard features anywhere in your code, do the following.

        Example::

            logger.experiment.some_tensorboard_function()

        Nr   z+tried to init log dirs in non global_rank=0T)exist_okr   rJ   r=   )r8   r   rankr!   r7   makedirsr)   torch.utils.tensorboardr   r   rJ   r9   )r:   r   r=   r=   r>   
experiment   s   

zTensorBoardLogger.experimentmetricsstepc                 C   s   t jdks	J dt|| j| j}| D ]:\}}t|tr"| }t|t	r0| j
||| qz
| j
||| W q tyO } z	td| d|d }~ww d S )Nr   z-experiment tried to log from global_rank != 0z
 you tried to log zA which is currently not supported. Try a dict or a scalar/tensor.)r   rL   r   r6   LOGGER_JOIN_CHARitemsrE   r
   itemdictrO   add_scalars
add_scalar	Exception
ValueError)r:   rP   rQ   kvexr=   r=   r>   log_metrics   s$   


zTensorBoardLogger.log_metricsparamsc                 C   s   t |}t|}| |}|du r| jrddi}n	t|ts"d|i}|rW| |d tr3ddlm	} nddl
m	} |||\}}}| j }|| || || dS dS )a  Record hyperparameters. TensorBoard logs with and without saved hyperparameters are incompatible, the
        hyperparameters are then not displayed in the TensorBoard. Please delete or move the previously saved logs to
        display the new ones with hyperparameters.

        Args:
            params: a dictionary-like container with the hyperparameters
            metrics: Dictionary with metric names as keys and measured quantities as values

        N	hp_metricr   )hparams)r   r   r   r5   rE   rU   r]   r)   torch.utils.tensorboard.summaryra   tensorboardX.summaryrO   _get_file_writeradd_summary)r:   r^   rP   ra   expssiseiwriterr=   r=   r>   log_hyperparams   s(   




z!TensorBoardLogger.log_hyperparamsmodelinput_arrayc                 C   s   t |dd }|d u r|n|}t|}|d u rtd d S t|ttfs/tdt| d d S tt |dd rRtt |dd rR||}|	|}| j
|| d S | j
|| d S )Nexample_input_arrayzCould not log computational graph to TensorBoard: The `model.example_input_array` attribute is not set or `input_array` was not given.zlCould not log computational graph to TensorBoard: The `input_array` or `model.example_input_array` has type z which can't be traced by TensorBoard. Make the input array a tuple representing the positional arguments to the model's `forward()` implementation._on_before_batch_transfer_apply_batch_transfer_handler)getattrr   r   rE   r
   tupletypecallablern   ro   rO   	add_graph)r:   rk   rl   model_example_inputr=   r=   r>   	log_graph  s(   


zTensorBoardLogger.log_graphc                 C   s   | j   d S N)rO   flushrA   r=   r=   r>   save  s   zTensorBoardLogger.savestatusc                 C   s&   | j d ur| j  | j  d S d S rw   )r8   rO   rx   close)r:   rz   r=   r=   r>   finalize$  s   

zTensorBoardLogger.finalizec                 C   s   t j| j| j}z| j|}W n
 ty   Y dS w g }|D ]-}|d }t j|}t	| j|rM|
drM|dd dd}| rM|t| q t|dkrVdS t|d S )Nr   r"   rD   _   /r    )r/   rF   rG   r!   r"   r7   listdirOSErrorbasenamer   
startswithsplitreplaceisdigitappendintlenmax)r:   save_dirlistdir_infoexisting_versionslistingdbndir_verr=   r=   r>   rC   +  s$   z#TensorBoardLogger._get_next_versionc                 C   s   t | } dd |  D S )Nc                 S   s2   i | ]\}}|t |d r|jdkrt|n|qS )ndimr~   )hasattrr   r,   ).0rZ   r[   r=   r=   r>   
<dictcomp>D  s   2 z6TensorBoardLogger._sanitize_params.<locals>.<dictcomp>)_utils_sanitize_paramsrS   )r^   r=   r=   r>   r   @  s   z"TensorBoardLogger._sanitize_paramsc                 C   s   | j  }d |d< |S )Nr8   )__dict__copy)r:   stater=   r=   r>   __getstate__F  s   
zTensorBoardLogger.__getstate__)r   NTr    N)r?   r   rw   )r?   N)'__name__
__module____qualname____doc__rR   r   r   r,   r   r   boolr   r.   propertyr   r"   r#   r!   rJ   r&   r   rO   r   r   floatr]   r   r   rj   r   r
   rv   ry   r|   rC   staticmethodr   r   __classcell__r=   r=   r;   r>   r   *   s    $			(& $r   )+r/   argparser   typingr   r   r   r   r   r    lightning_utilities.core.importsr	   torchr
   torch.nnr   typing_extensionsr   lightning_fabric.loggers.loggerr   r   #lightning_fabric.utilities.cloud_ior   r   !lightning_fabric.utilities.loggerr   r   r   r   r   $lightning_fabric.utilities.rank_zeror   r    lightning_fabric.utilities.typesr   lightning_fabric.wrappersr   r)   r*   rN   r   r   r   r=   r=   r=   r>   <module>   s*    