o
    XεiO	                     @   sp   d dl Z d dlmZ d dlZd dlmZ d dlmZ d dlm	Z	 e r*d dl
mZ nedG dd	 d	eZdS )
    N)Any)TensorboardLogger)is_clearml_available)rank_zero_only)TaskzFClearML is not installed. Please install it with `pip install clearml`c                       sl   e Zd ZdZ	ddededededef
 fdd	Zed
d Zedd Ze	ede
defddZ  ZS )ClearMLLoggera  ClearML Logger using TensorBoard in the background.

    TODO:
        - Add hyperparameter handling
        - Use ClearML logger for plots
        - Handle continuing training

    Args:
        output_uri (str): URI of the ClearML repository.
        local_path (str): Path to the local directory where the model is saved.
        project_name (str): Name of the ClearML project.
        task_name (str): Name of the ClearML task.
        tags (str): Comma separated list of tags to add to the ClearML task.
    N
output_uri
local_pathproject_name	task_nametagsc                    sn   d | _ || _|| _|r|dng | _tj||| j|d| _|r.|dD ]}| j| q%t	 
dd  d S )N,)r
   r   r   r   run)_contextr	   r   splitr   r   initr   add_tagsuper__init__)selfr   r	   r
   r   r   tag	__class__ R/home/ubuntu/.local/lib/python3.10/site-packages/trainer/logging/clearml_logger.pyr       s   zClearMLLogger.__init__c                 C   sh   |  d|  d | jjd| d | j|j | jd|  | jjdtj	
| jdd dS )	z!Upload config file(s) to ClearML.
run_configr   model_config)nameconfigurationconfigsz*.jsonartifact_objectN)add_textto_jsonr   connect_configurationto_dictset_commentrun_descriptionupload_artifactospathjoinr	   )r   configr   r   r   
add_config4   s
    zClearMLLogger.add_configc                 K   s   | j j||d dS )zUpload artifact to ClearML.r    N)r   r(   )r   file_or_dirr   kwargsr   r   r   add_artifact=   s   zClearMLLogger.add_artifactstater*   c                 C   s   t | | d S N)torchsave)r1   r*   r   r   r   
save_modelB   s   zClearMLLogger.save_modelr2   )__name__
__module____qualname____doc__strr   r   r-   r0   staticmethodr   r5   __classcell__r   r   r   r   r      s*    

r   )r)   typingr   r3   "trainer.logging.tensorboard_loggerr   trainer.trainer_utilsr   trainer.utils.distributedr   clearmlr   ImportErrorr   r   r   r   r   <module>   s    