o
    $i                     @   sN  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 zd dlmZmZmZ W n ey_   d dlmZmZmZ Y nw eeZdd	 e jee jd
D Z ded ded fddZ!e!G dd deZ"eG dd de"Z#G dd de"Z$eG dd de#Z%dS )    N)contextmanager)DictListOptionalTypeUnion)
Checkpoint)log_once)
Deprecated	PublicAPI)CallbackLightningModuleTrainerc                 C   s   h | ]\}}| d r|qS on_)
startswith).0namefn r   c/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/tune/integration/pytorch_lightning.py	<setcomp>   s    r   )	predicatecallback_clsTuneCallbackreturnc                 C   s&   dd }t D ]
}t| ||| q| S )zFOverrides all allowed PTL Callback hooks with our custom handle logic.c                    s$   d ddt dtt f fdd}|S )N)	pl_moduletrainerr   c                   s     | j v r| j||d d S d S )N)r   r   )_on_handle)selfr   r   argskwargsfn_namer   r   overridden_hook"   s   
zN_override_ptl_hooks.<locals>.generate_overridden_hook.<locals>.overridden_hook)r   r   r   )r$   r%   r   r#   r   generate_overridden_hook!   s   
z5_override_ptl_hooks.<locals>.generate_overridden_hook)_allowed_hookssetattr)r   r&   r$   r   r   r   _override_ptl_hooks   s   r)   c                   @   sB   e Zd ZdZddeeee f fddZdede	e
 fd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                 C   sT   t |ts|g}|D ]}d| tvrtd| dt q
dd |D }|| _d S )Nr   zInvalid hook selected: z. Must be one of c                 S   s   g | ]}d | qS r   r   )r   hookr   r   r   
<listcomp>K   s    z)TuneCallback.__init__.<locals>.<listcomp>)
isinstancelistr'   
ValueErrorr   )r    r+   r,   r   r   r   __init__?   s   

zTuneCallback.__init__r   r   c                 C   s   t N)NotImplementedError)r    r   r   r   r   r   r   O   s   zTuneCallback._handleN)r*   )__name__
__module____qualname____doc__r   strr   r1   r   r   r   r   r   r   r   r   r   5   s    c                       s   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
dZ
dedefddZededee fddZdedefddZ  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_checkpointsr+   c                    s8   t t| j|d t|tr|g}|| _|| _|| _d S )N)r+   )superr9   r1   r.   r8   _save_checkpoints	_filename_metrics)r    r;   r<   r=   r+   	__class__r   r   r1   z   s   

z%TuneReportCheckpointCallback.__init__r   r   c                 C   s   |j rd S | jsdd |j D }|S i }| jD ](}t| jtr'| j| }n|}||jv r8|j|  ||< qtd| d q|S )Nc                 S   s   i | ]	\}}||  qS r   )item)r   kvr   r   r   
<dictcomp>   s    zATuneReportCheckpointCallback._get_report_dict.<locals>.<dictcomp>zMetric z- does not exist in `trainer.callback_metrics.)	sanity_checkingrA   callback_metricsitemsr.   dictrD   loggerwarning)r    r   r   report_dictkeymetricr   r   r   _get_report_dict   s    


z-TuneReportCheckpointCallback._get_report_dictr   c                 c   sf    | j s	d V  d S t }|tj|| j t	|}|V  W d    d S 1 s,w   Y  d S r2   )
r?   tempfileTemporaryDirectorysave_checkpointospathjoinr@   r   from_directory)r    r   checkpoint_dirr:   r   r   r   _get_checkpoint   s   

"z,TuneReportCheckpointCallback._get_checkpointc                 C   s^   |j rd S | ||}|sd S | |}tjj||d W d    d S 1 s(w   Y  d S )N)r:   )rH   rQ   rZ   raytunereport)r    r   r   rN   r:   r   r   r   r      s   "z$TuneReportCheckpointCallback._handle)Nr:   Tr*   )r4   r5   r6   r7   r   r   r8   r   r   boolr1   r   r   rQ   r   r   rZ   r   __classcell__r   r   rB   r   r9   S   s&    '
r9   c                   @   s   e Zd Zdd ZdS )_TuneCheckpointCallbackc                 O   s   t d)NzO`ray.tune.integration.pytorch_lightning._TuneCheckpointCallback` is deprecated.)DeprecationWarning)r    r!   r"   r   r   r   r1      s   z _TuneCheckpointCallback.__init__N)r4   r5   r6   r1   r   r   r   r   r`      s    r`   c                	       sR   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dZ  Z	S )TuneReportCallbackNr*   r;   r+   c                    s,   t dr	td tt| j|d|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)r;   r=   r+   )r	   warningswarnr>   rb   r1   )r    r;   r+   rB   r   r   r1      s   

zTuneReportCallback.__init__)Nr*   )
r4   r5   r6   r   r   r8   r   r   r1   r_   r   r   rB   r   rb      s    rb   )&inspectloggingrU   rR   rd   
contextlibr   typingr   r   r   r   r   ray.tuner[   r   ray.utilr	   ray.util.annotationsr
   r   	lightningr   r   r   ModuleNotFoundErrorpytorch_lightning	getLoggerr4   rL   
getmembers
isfunctionr'   r)   r   r9   r`   rb   r   r   r   r   <module>   s8    
b