o
    1 i                     @   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   Zaim_run_kwargs r   _/home/app/PaddleOCR-VL-test/.venv_paddleocr/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   Z
experimenttrial_idZtrial_log_dirtrial_ipNr   )	Zlocal_experiment_pathr   r   r   Zexperiment_dir_namer   r!   pathZget_ray_actor_ip)r   r   Z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   closeZ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epochZrayZtune/	delimiter)valuenamer1   stepr0   r   )copygetr
   r   r	   popr   r   itemsr   join
isinstancetupleVALID_SUMMARY_TYPESnpisnanisinftracklistsetlenZndarraysize)r   r+   r   r,   Z
tmp_resultr7   kr0   r1   	trial_runr#   Zflat_resultZvalid_resultattrr5   Z	full_attrr   r   r   log_trial_results   sD   


z"AimLoggerCallback.log_trial_resultFfailedc                 C   s   | j |}|  d S r&   )r   r:   r'   )r   r   rL   rI   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 )	Nr2   r3   c                    s"   i | ]\}}t | jr||qS r   )r=   VALID_HPARAMS.0rH   vr   r   r   
<dictcomp>   s
    z8AimLoggerCallback._log_trial_hparams.<locals>.<dictcomp>c                    s&   i | ]\}}t | jr|| qS r   )r=   VALID_NP_HPARAMStolistrO   rR   r   r   rS      s    
c                    s(   i | ]\}}t | j j s||qS r   )r=   rN   rT   rO   rR   r   r   rS      s    zCRemoved the following hyperparameter values when logging to aim: %sZhparams)r   r(   r;   updateloggerinfostrr   )r   r   paramsZflat_paramsZscrubbed_paramsZ	np_paramsremovedr$   r   rR   r   r)      s&   




z$AimLoggerCallback._log_trial_hparams)NNN)r   r   )F)__name__
__module____qualname____doc__rY   r   intfloatrD   typerN   r@   Zbool_float32float64int32int64rT   r   r   r   r   r   r%   r*   r   rK   rM   r)   r   r   r   r   r      s&    


(r   )%loggingtypingr   r   r   r   r   numpyr@   Zray.air.constantsr   Zray.tune.logger.loggerr   Zray.tune.resultr	   r
   Zray.tune.utilsr   Zray.util.annotationsr   Zray.tune.experiment.trialr   Zaim.sdkr   r   ImportError	getLoggerr\   rW   r`   ra   rc   rd   re   rf   r?   r   r   r   r   r   <module>   s&    
