o
    ci                     @   s   d dl Z d dlmZmZmZmZmZ d dlZd dl	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r>d dlmZ z
d d	lmZmZ W n eyU   d
\ZZY nw e eZeeejej ej!ej"gZ#eG dd deZ$dS )    N)TYPE_CHECKINGDictListOptionalUnion)TRAINING_ITERATION)LoggerCallback)TIME_TOTAL_STIMESTEPS_TOTAL)flatten_dict)	PublicAPI)Trial)RepoRun)NNc                	   @   s   e Zd ZdZeeeeee	dfZ
ejejejejejfZ			ddeeedf  dee deee  fddZd	d
defddZdddZded	d
defddZdd	d
defddZdddZdS )AimLoggerCallbacka  Aim Logger: logs metrics in Aim format.

    Aim is an open-source, self-hosted ML experiment tracking tool.
    It's good at tracking lots (thousands) of training runs, and it allows you to
    compare them with a performant and well-designed UI.

    Source: https://github.com/aimhubio/aim

    Args:
        repo: Aim repository directory or a `Repo` object that the Run object will
            log results to. If not provided, a default repo will be set up in the
            experiment directory (one level above trial directories).
        experiment: Sets the `experiment` property of each Run object, which is the
            experiment name associated with it. Can be used later to query
            runs/sequences.
            If not provided, the default will be the Tune experiment name set
            by `RunConfig(name=...)`.
        metrics: List of metric names (out of the metrics reported by Tune) to
            track in Aim. If no metric are specified, log everything that
            is reported.
        aim_run_kwargs: Additional arguments that will be passed when creating the
            individual `Run` objects for each trial. For the full list of arguments,
            please see the Aim documentation:
            https://aimstack.readthedocs.io/en/latest/refs/sdk.html
    Nrepor   experiment_namemetricsc                 K   sJ   t dusJ d|| _|| _t|s|du std|| _|| _i | _dS )zT
        See help(AimLoggerCallback) for more information about parameters.
        NzPaim must be installed!. You can install aim with the command: `pip install aim`.z`metrics` must either contain at least one metric name, or be None, in which case all reported metrics will be logged to the aim repo.)r   
_repo_path_experiment_namebool
ValueError_metrics_aim_run_kwargs_trial_to_run)selfr   r   r   aim_run_kwargs r   G/home/ubuntu/.local/lib/python3.10/site-packages/ray/tune/logger/aim.py__init__8   s   


zAimLoggerCallback.__init__trialr   returnc                 C   sV   |j }td| jp	|| jp|jd| j}|j|d< |j|d< | }|r)||d< |S )zInitializes an Aim Run object for a given trial.

        Args:
            trial: The Tune trial that aim will track as a Run.

        Returns:
            Run: The created aim run for a specific trial.
        )r   
experimenttrial_idtrial_log_dirtrial_ipNr   )	local_experiment_pathr   r   r   experiment_dir_namer   r#   pathget_ray_actor_ip)r   r    experiment_dirrunr%   r   r   r   _create_runQ   s   	


zAimLoggerCallback._create_runc                 C   sH   || j v r| j |   |  | || j |< |jr"| | d S d S N)r   closeinit_local_pathr,   evaluated_params_log_trial_hparams)r   r    r   r   r   log_trial_starth   s   
z!AimLoggerCallback.log_trial_start	iterationresultc                 C   s(  |  }|td p|t }dddttfD ]}||d  q|dd }|dd }| j| }	ddg}
t|dd	}i }| D ]R\}}| j	rL|| j	vrLq?d
|
|g }t|ttrtt|stt|st|||< |	j|||||d
 q?t|tttfrt|dkst|tjr|jdkr|||< q?d S )Nconfigpid	timestampcontextepochraytune/	delimiter)valuenamer9   stepr8   r   )copygetr
   r   r	   popr   r   itemsr   join
isinstancetupleVALID_SUMMARY_TYPESnpisnanisinftracklistsetlenndarraysize)r   r3   r    r4   
tmp_resultrA   kr8   r9   	trial_runr(   flat_resultvalid_resultattrr?   	full_attrr   r   r   log_trial_results   sD   


z"AimLoggerCallback.log_trial_resultFfailedc                 C   s   | j |}|  d S r-   )r   rD   r.   )r   r    r[   rU   r   r   r   log_trial_end   s   zAimLoggerCallback.log_trial_endc                    s   t |jdd}t |} fdd| D } fdd| D }||  fdd| D }|r;tdt|  j| }||d< d S )	Nr<   r=   c                    s"   i | ]\}}t | jr||qS r   )rG   VALID_HPARAMS.0rT   vr   r   r   
<dictcomp>   s
    z8AimLoggerCallback._log_trial_hparams.<locals>.<dictcomp>c                    s&   i | ]\}}t | jr|| qS r   )rG   VALID_NP_HPARAMStolistr^   ra   r   r   rb      s    
c                    s(   i | ]\}}t | j j s||qS r   )rG   r]   rc   r^   ra   r   r   rb      s    zCRemoved the following hyperparameter values when logging to aim: %shparams)r   r0   rE   updateloggerinfostrr   )r   r    paramsflat_paramsscrubbed_params	np_paramsremovedr+   r   ra   r   r1      s&   




z$AimLoggerCallback._log_trial_hparams)NNN)r    r   )F)__name__
__module____qualname____doc__ri   r   intfloatrN   typer]   rJ   bool_float32float64int32int64rc   r   r   r   r   r   r,   r2   r   rZ   r\   r1   r   r   r   r   r      s&    


(r   )%loggingtypingr   r   r   r   r   numpyrJ   ray.air.constantsr   ray.tune.logger.loggerr   ray.tune.resultr	   r
   ray.tune.utilsr   ray.util.annotationsr   ray.tune.experiment.trialr   aim.sdkr   r   ImportError	getLoggerro   rg   rs   rt   rw   rx   ry   rz   rI   r   r   r   r   r   <module>   s&    
