
    &`i                        d dl Z d dlZd dlZd dlZd dlZd dlm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mZmZ n# e$ r d dlmZmZmZ Y nw xY w ej        e          Zd  e j        ee j        	          D             Z d
ed         ded         fdZ!e! G d de                      Z"e G d de"                      Z# G d de"          Z$e G d de#                      Z%dS )    N)contextmanager)DictListOptionalTypeUnion)
Checkpoint)log_once)
Deprecated	PublicAPI)CallbackLightningModuleTrainerc                 B    h | ]\  }}|                     d           |S on_)
startswith).0namefns      z/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/integration/pytorch_lightning.py	<setcomp>r      s>       bu      )	predicatecallback_clsTuneCallbackreturnc                 T    d }t           D ]}t          | | ||                     | S )zFOverrides all allowed PTL Callback hooks with our custom handle logic.c                 L     d ddt           dt          t                   f fd}|S )N)	pl_moduletrainerr    c                L    | j         v r|                     ||           d S d S )N)r!   r    )_on_handle)selfr!   r    argskwargsfn_names        r   overridden_hookzN_override_ptl_hooks.<locals>.generate_overridden_hook.<locals>.overridden_hook"   s6     $(""W	BBBBB #"r   )r   r   r   )r(   r)   s   ` r   generate_overridden_hookz5_override_ptl_hooks.<locals>.generate_overridden_hook!   sY    
 48		C 	C 	C	C  0		C 	C 	C 	C 	C 	C r   )_allowed_hookssetattr)r   r*   r(   s      r   _override_ptl_hooksr-      sK       " J Jg'?'?'H'HIIIIr   c                   X    e Zd ZdZd	deeee         f         fdZdede	e
         fdZdS )
r   a  Base class for Tune's PyTorch Lightning callbacks.

    Args:
        on: When to trigger checkpoint creations. Must be one of
            the PyTorch Lightning event hooks (less the ``on_``), e.g.
            "train_batch_start", or "train_end". Defaults to "validation_end"
    validation_endonc                     t          |t                    s|g}|D ](}d| t          vrt          d| dt                     )d |D             }|| _        d S )Nr   zInvalid hook selected: z. Must be one of c                     g | ]}d | S r    )r   hooks     r   
<listcomp>z)TuneCallback.__init__.<locals>.<listcomp>K   s    ***tlDll***r   )
isinstancelistr+   
ValueErrorr#   )r%   r0   r4   s      r   __init__zTuneCallback.__init__?   s    "d## 	B 	 	DT||>11 (d ( (%( (   2 +*r***r   r!   r    c                     t           N)NotImplementedError)r%   r!   r    s      r   r$   zTuneCallback._handleO   s    !!r   N)r/   )__name__
__module____qualname____doc__r   strr   r9   r   r   r   r$   r3   r   r   r   r   5   sr          5d3i0     "w "8O3L " " " " " "r   c                        e Zd ZdZ	 	 	 	 ddeeeee         eeef         f                  dede	d	eeee         f         f fd
Z
dedefdZededee         fd            ZdedefdZ xZS )TuneReportCheckpointCallbacka  PyTorch Lightning report and checkpoint callback

    Saves checkpoints after each validation step. Also reports metrics to Tune,
    which is needed for checkpoint registration.

    Args:
        metrics: Metrics to report to Tune. If this is a list,
            each item describes the metric key reported to PyTorch Lightning,
            and it will reported under the same name to Tune. If this is a
            dict, each key will be the name reported to Tune and the respective
            value will be the metric key reported to PyTorch Lightning.
        filename: Filename of the checkpoint within the checkpoint
            directory. Defaults to "checkpoint".
        save_checkpoints: If True (default), checkpoints will be saved and
            reported to Ray. If False, only metrics will be reported.
        on: When to trigger checkpoint creations and metric reports. Must be one of
            the PyTorch Lightning event hooks (less the ``on_``), e.g.
            "train_batch_start", or "train_end". Defaults to "validation_end".


    Example:

    .. code-block:: python

        import pytorch_lightning as pl
        from ray.tune.integration.pytorch_lightning import (
            TuneReportCheckpointCallback)

        # Save checkpoint after each training batch and after each
        # validation epoch.
        trainer = pl.Trainer(callbacks=[TuneReportCheckpointCallback(
            metrics={"loss": "val_loss", "mean_accuracy": "val_acc"},
            filename="trainer.ckpt", on="validation_end")])


    N
checkpointTr/   metricsfilenamesave_checkpointsr0   c                     t          t          |                               |           t          |t                    r|g}|| _        || _        || _        d S )N)r0   )superrC   r9   r6   rA   _save_checkpoints	_filename_metrics)r%   rE   rF   rG   r0   	__class__s        r   r9   z%TuneReportCheckpointCallback.__init__z   sZ     	*D11::b:AAAgs## 	 iG!1!r   r!   r    c                 n   |j         rd S | j        s$d |j                                        D             }ni }| j        D ]v}t	          | j        t
                    r| j        |         }n|}||j        v r#|j        |                                         ||<   Xt                              d| d           w|S )Nc                 >    i | ]\  }}||                                 S r3   )item)r   kvs      r   
<dictcomp>zATuneReportCheckpointCallback._get_report_dict.<locals>.<dictcomp>   s&    TTT41a1affhhTTTr   zMetric z- does not exist in `trainer.callback_metrics.)	sanity_checkingrL   callback_metricsitemsr6   dictrP   loggerwarning)r%   r!   r    report_dictkeymetrics         r   _get_report_dictz-TuneReportCheckpointCallback._get_report_dict   s    " 	F} 	TT73K3Q3Q3S3STTTKKK}  dmT22 !!]3/FF FW555'.'?'G'L'L'N'NK$$NN5& 5 5 5   
 r   r   c              #     K   | j         sd V  d S t          j                    5 }|                    t          j                            || j                             t          j	        |          }|V  d d d            d S # 1 swxY w Y   d S r;   )
rJ   tempfileTemporaryDirectorysave_checkpointospathjoinrK   r	   from_directory)r%   r!   checkpoint_dirrD   s       r   _get_checkpointz,TuneReportCheckpointCallback._get_checkpoint   s      % 	JJJF(** 	n##BGLL$P$PQQQ#2>BBJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   ABBBc                     |j         rd S |                     ||          }|sd S |                     |          5 }t          j                            ||           d d d            d S # 1 swxY w Y   d S )N)rD   )rT   r]   rg   raytunereport)r%   r!   r    rZ   rD   s        r   r$   z$TuneReportCheckpointCallback._handle   s    " 	F++GY?? 	F!!'** 	@jHOOKJO???	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@s   "A((A,/A,)NrD   Tr/   )r=   r>   r?   r@   r   r   rA   r   r   boolr9   r   r   r]   r   r	   rg   r$   __classcell__rM   s   @r   rC   rC   S   s.       # #N DH$!%$4   %T#YS#X >?@    	 
 #tCy.!            O    . w 8J3G    ^	@w 	@? 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@r   rC   c                       e Zd Zd ZdS )_TuneCheckpointCallbackc                      t          d          )NzO`ray.tune.integration.pytorch_lightning._TuneCheckpointCallback` is deprecated.)DeprecationWarning)r%   r&   r'   s      r   r9   z _TuneCheckpointCallback.__init__   s     
 
 	
r   N)r=   r>   r?   r9   r3   r   r   rp   rp      s#        
 
 
 
 
r   rp   c            	            e Zd Z	 	 ddeeeee         eeef         f                  deeee         f         f fdZ xZ	S )TuneReportCallbackNr/   rE   r0   c                     t          d          rt          j        d           t          t          |                               |d|           d S )Ntune_ptl_report_deprecatedz`ray.tune.integration.pytorch_lightning.TuneReportCallback` is deprecated. Use `ray.tune.integration.pytorch_lightning.TuneReportCheckpointCallback` instead.F)rE   rG   r0   )r
   warningswarnrI   rt   r9   )r%   rE   r0   rM   s      r   r9   zTuneReportCallback.__init__   sj    
 011 	M   	 $''00e 	1 	
 	
 	
 	
 	
r   )Nr/   )
r=   r>   r?   r   r   rA   r   r   r9   rm   rn   s   @r   rt   rt      s         DH$4
 
%T#YS#X >?@
 #tCy.!
 
 
 
 
 
 
 
 
 
r   rt   )&inspectloggingrb   r_   rw   
contextlibr   typingr   r   r   r   r   ray.tuneri   r	   ray.utilr
   ray.util.annotationsr   r   	lightningr   r   r   ModuleNotFoundErrorpytorch_lightning	getLoggerr=   rX   
getmembers
isfunctionr+   r-   r   rC   rp   rt   r3   r   r   <module>r      sy     				   % % % % % % 4 4 4 4 4 4 4 4 4 4 4 4 4 4              6 6 6 6 6 6 6 6E<<<<<<<<<<< E E EDDDDDDDDDDDDE 
	8	$	$ &G&x7;MNNN  d>&: tN?S    . " " " " "8 " " ": _@ _@ _@ _@ _@< _@ _@ _@D
 
 
 
 
l 
 
 
 
 
 
 
 
5 
 
 
 
 
s   
A AA