
    &`i                     :   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 	 d d	lmZmZ n# e$ r d
\  ZZY nw xY w e j        e          Zeeej        ej         ej!        ej"        gZ#e G 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            	          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Zdd	d
efdZddZdedd	defdZddd	defdZ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                     t           
J d            || _        || _        t          |          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_kwargss        g/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/logger/aim.py__init__zAimLoggerCallback.__init__8   ss     /   /W 	U    -13    trialr   returnc                     |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_ip )	local_experiment_pathr   r   r   experiment_dir_namer   r'   pathget_ray_actor_ip)r   r#   experiment_dirrunr)   s        r    _create_runzAimLoggerCallback._create_runQ   s     4 
2N,I0I
 
 "
 
  .J$zO))++ 	'&C
O
r"   c                     || j         v r| j         |                                          |                                 |                     |          | j         |<   |j        r|                     |           d S d S N)r   closeinit_local_pathr1   evaluated_params_log_trial_hparams)r   r#   s     r    log_trial_startz!AimLoggerCallback.log_trial_starth   s    D&&&u%++---$($4$4U$;$;5!! 	+##E*****	+ 	+r"   	iterationresultc                    |                                 }|                    t          d           p|t                   }dddt          t          fD ]}|                    |d            |                    dd           }|                    dd           }| j        |         }	ddg}
t          |d	          }i }|                                D ]\  }}| j	        r
|| j	        vrd
                    |
|gz             }t          |t          t                              rHt          j        |          s4t          j        |          s |||<   |	                    |||||
           t          |t$          t          t&          f          rt)          |          dk    s%t          |t          j                  r|j        dk    r|||<   d S )Nconfigpid	timestampcontextepochraytune/	delimiter)valuenamer@   stepr?   r   )copygetr   r   r
   popr   r   itemsr   join
isinstancetupleVALID_SUMMARY_TYPESnpisnanisinftracklistsetlenndarraysize)r   r9   r#   r:   
tmp_resultrH   kr?   r@   	trial_runr-   flat_resultvalid_resultattrrF   	full_attrs                   r    log_trial_resultz"AimLoggerCallback.log_trial_results   s   [[]]
zz/400NF;M4NE;>PQ 	$ 	$ANN1d#### ..D11w--&u-	v":===&,,.. 	+ 	+KD%} T]!:!://I%':!;!;<< ++#%8E??+ &+T""#       UT5#$677 +CJJNN5"*-- =K27*q..%*T"'	+ 	+r"   Ffailedc                 b    | j                             |          }|                                 d S r3   )r   rK   r4   )r   r#   rb   r\   s       r    log_trial_endzAimLoggerCallback.log_trial_end   s.    &**511	r"   c                     t          |j        d          }t          |          } fd|                                D             } fd|                                D             }|                    |            fd|                                D             }|r(t                              dt          |                      j        |         }||d<   d S )NrC   rD   c                 F    i | ]\  }}t          |j                  ||S r*   )rN   VALID_HPARAMS.0r[   vr   s      r    
<dictcomp>z8AimLoggerCallback._log_trial_hparams.<locals>.<dictcomp>   sA     
 
 
QJq$BT4U4U
q
 
 
r"   c                 j    i | ]/\  }}t          |j                  ||                                0S r*   )rN   VALID_NP_HPARAMStolistrh   s      r    rk   z8AimLoggerCallback._log_trial_hparams.<locals>.<dictcomp>   sJ     
 
 
1!T233
qxxzz
 
 
r"   c                 V    i | ]%\  }}t          |j        j        z             "||&S r*   )rN   rg   rm   rh   s      r    rk   z8AimLoggerCallback._log_trial_hparams.<locals>.<dictcomp>   sJ     
 
 
1a!3d6K!KLL
q
 
 
r"   zCRemoved the following hyperparameter values when logging to aim: %shparams)r   r6   rL   updateloggerinfostrr   )r   r#   paramsflat_paramsscrubbed_params	np_paramsremovedr0   s   `       r    r7   z$AimLoggerCallback._log_trial_hparams   s!   e4DDD"6**
 
 
 
(..00
 
 

 
 
 
#))++
 
 
	 	y)))
 
 
 
#))++
 
 

  	KK%G    '(Ir"   )NNN)r#   r   )F)__name__
__module____qualname____doc__rt   r   intfloatrU   typerg   rQ   bool_float32float64int32int64rm   r   r   r   r!   r   r1   r8   r   ra   rd   r7   r*   r"   r    r   r      sK        4 $UD$$t**=M"*bj"(BHM .2)-'+	4 4uS&[)*4 "#4 $s)$	4 4 4 42 S    .	+ 	+ 	+ 	+&+# &+g &+t &+ &+ &+ &+P 7 D    ) ) ) ) ) )r"   r   )%loggingtypingr   r   r   r   r   numpyrQ   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	getLoggerrz   rr   r~   r   r   r   r   r   rP   r   r*   r"   r    <module>r      s    = = = = = = = = = = = = = =     0 0 0 0 0 0 1 1 1 1 1 1 9 9 9 9 9 9 9 9 ' ' ' ' ' ' * * * * * * 0//////!!!!!!!!!   ID### 
	8	$	$E2:rz28RXN  a) a) a) a) a) a) a) a) a) a)s    A	 	
AA