
    &`i                         d dl Z d dlmZ d dlmZmZmZmZ d dlm	Z
 d dlZd dlmZ d dlmZ  G d de
          Z G d	 d
e          Z ed           G d de                      ZdS )    N)abstractmethod)DictListOptionalUnion)Callback)TensorflowCheckpoint)	PublicAPIc                        e Zd ZdZg dZddeeee         f         f fdZde	defdZ
dd
ZddZddZddZddZddZddZddZddZddZddZddZddZddZ xZS )	_Callbackz+Base class for Ray Train's Keras callbacks.)epoch_begin	epoch_endtrain_batch_begintrain_batch_endtest_batch_begintest_batch_endpredict_batch_beginpredict_batch_endtrain_begin	train_end
test_begintest_endpredict_beginpredict_endvalidation_endonc                     t          t                                                      t          |t                    s|g}t           fd|D                       r(t          d                    | j                            | _	        d S )Nc              3   *   K   | ]}|j         vV  d S N)_allowed).0wselfs     n/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/train/tensorflow/keras.py	<genexpr>z%_Callback.__init__.<locals>.<genexpr>%   s*      22!q%222222    z4Invalid trigger time selected: {}. Must be one of {})
superr   __init__
isinstancelistany
ValueErrorformatr    _on)r#   r   	__class__s   ` r$   r(   z_Callback.__init__    s    i'')))"d## 	B2222r22222 	FMM   
 r&   logswhenc                     t           r   NotImplementedError)r#   r0   r1   s      r$   _handlez_Callback._handle-   s    !!r&   Nc                 H    d| j         v r|                     |d           d S d S )Nr   r.   r5   r#   epochr0   s      r$   on_epoch_beginz_Callback.on_epoch_begin0   1    DH$$LL}----- %$r&   c                 H    d| j         v r|                     |d           d S d S )Nr   r7   r8   s      r$   on_epoch_endz_Callback.on_epoch_end4   1    $(""LL{+++++ #"r&   c                 H    d| j         v r|                     |d           d S d S )Nr   r7   r#   batchr0   s      r$   on_train_batch_beginz_Callback.on_train_batch_begin8   2    $(**LL233333 +*r&   c                 H    d| j         v r|                     |d           d S d S )Nr   r7   r@   s      r$   on_train_batch_endz_Callback.on_train_batch_end<   s2    ((LL011111 )(r&   c                 H    d| j         v r|                     |d           d S d S )Nr   r7   r@   s      r$   on_test_batch_beginz_Callback.on_test_batch_begin@   s2    ))LL122222 *)r&   c                 H    d| j         v r|                     |d           d S d S )Nr   r7   r@   s      r$   on_test_batch_endz_Callback.on_test_batch_endD   s2    tx''LL/00000 ('r&   c                 H    d| j         v r|                     |d           d S d S )Nr   r7   r@   s      r$   on_predict_batch_beginz _Callback.on_predict_batch_beginH   s2     DH,,LL455555 -,r&   c                 H    d| j         v r|                     |d           d S d S )Nr   r7   r@   s      r$   on_predict_batch_endz_Callback.on_predict_batch_endL   rC   r&   c                 H    d| j         v r|                     |d           d S d S )Nr   r7   r#   r0   s     r$   on_train_beginz_Callback.on_train_beginP   r;   r&   c                 H    d| j         v r|                     |d           d S d S )Nr   r7   rO   s     r$   on_train_endz_Callback.on_train_endT   r>   r&   c                 H    d| j         v r|                     |d           d S d S )Nr   r7   rO   s     r$   on_test_beginz_Callback.on_test_beginX   s1    48##LL|,,,,, $#r&   c                 H    d| j         v r|                     |d           d S d S )Nr   r7   rO   s     r$   on_test_endz_Callback.on_test_end\   s1    !!LLz***** "!r&   c                 H    d| j         v r|                     |d           d S d S )Nr   r7   rO   s     r$   on_predict_beginz_Callback.on_predict_begin`   s1    dh&&LL///// '&r&   c                 H    d| j         v r|                     |d           d S d S )Nr   r7   rO   s     r$   on_predict_endz_Callback.on_predict_endd   r;   r&   )r   r   )__name__
__module____qualname____doc__r    r   strr   r(   r   r5   r:   r=   rB   rE   rG   rI   rK   rM   rP   rR   rT   rV   rX   rZ   __classcell__r/   s   @r$   r   r      s       55  H" 5d3i0      "D " " " " ". . . ., , , ,4 4 4 42 2 2 23 3 3 31 1 1 16 6 6 64 4 4 4. . . ., , , ,- - - -+ + + +0 0 0 0. . . . . . . .r&   r   c                       e Zd Z	 	 	 ddeeee         f         deeee         f         deeeee         eeef         f                  f fdZdedefd	Z	e
ded
efd            Ze
defd            ZdedefdZ xZS )RayReportCallbackr   Ncheckpoint_onreport_metrics_onmetricsc                    t          |t                    r|g}t          |t                    r|g}t          t          ||z                       }t	                                          |           || _        || _        || _        d S )N)r   )	r)   r_   r*   setr'   r(   _checkpoint_on_report_metrics_on_metrics)r#   rd   re   rf   r   r/   s        r$   r(   zRayReportCallback.__init__j   s     mS)) 	,*OM'-- 	4!2 3#m&778899B)6->r&   r0   returnc                    t          | j        t          d           t          t          t
          f          sJ | j        }nt          | j        t                    r| j        | j                 i}nmt          | j        t                    rfd| j        D             }n?t          | j        t
                    r%fd| j                                        D             }t          |t
                    sJ |S )Nc                 "    i | ]}||         S  ro   )r!   metricr0   s     r$   
<dictcomp>z;RayReportCallback._get_reported_metrics.<locals>.<dictcomp>   s    QQQVQQQr&   c                 (    i | ]\  }}||         S ro   ro   )r!   keyrp   r0   s      r$   rq   z;RayReportCallback._get_reported_metrics.<locals>.<dictcomp>   s0          &1c6T&\     r&   )r)   rk   typer_   r*   dictitems)r#   r0   reported_metricss    ` r$   _get_reported_metricsz'RayReportCallback._get_reported_metrics|   s   $-$t**c4)FGGGGG= #s++ 	 $tDM/BCt,, 	QQQQ4=QQQt,, 	       59]5H5H5J5J      *D11111r&   
checkpointc                     t           )CSave checkpoint and report metrics corresonding to this checkpoint.r3   r#   rf   ry   s      r$   _save_and_report_checkpointz-RayReportCallback._save_and_report_checkpoint   s
    
 "!r&   c                     t           )Report metrics.r3   r#   rf   s     r$   _report_metricsz!RayReportCallback._report_metrics   s
     "!r&   r1   c                 0   || j         v s|| j        v sJ |                     |          }|| j         v }|rLt          j        | j                  }|                     ||           t          j        |j	        d           d S | 
                    |           d S )NT)ignore_errors)ri   rj   rx   r	   
from_modelmodelr}   shutilrmtreepathr   )r#   r0   r1   rf   should_checkpointry   s         r$   r5   zRayReportCallback._handle   s    t***dd6M.M.M.MM,,T22 D$77 	*-8DDJ,,WjAAAM*/>>>>>>  )))))r&   )r   r   N)r[   r\   r]   r   r_   r   r   r   r(   rx   r   r	   r}   r   r5   r`   ra   s   @r$   rc   rc   i   s;        0;3>CG	   S$s)^,  !d3i0  %T#YS#X >?@	           $ $  4        " "")=" " " ^" "t " " " ^"*D * * * * * * * * *r&   rc   alpha)	stabilityc                   .    e Zd ZdZdedefdZdefdZdS )ReportCheckpointCallbacka  Keras callback for Ray Train reporting and checkpointing.

    .. note::
        Metrics are always reported with checkpoints, even if the event isn't specified
        in ``report_metrics_on``.

    Example:
        .. testcode:: python

            ############# Using it in TrainSession ###############
            from ray.air.integrations.keras import ReportCheckpointCallback
            def train_loop_per_worker():
                strategy = tf.distribute.MultiWorkerMirroredStrategy()
                with strategy.scope():
                    model = build_model()

                model.fit(dataset_shard, callbacks=[ReportCheckpointCallback()])

    Args:
        metrics: Metrics to report. If this is a list, each item describes
            the metric key reported to Keras, and it's reported under the
            same name. If this is a dict, each key is the name reported
            and the respective value is the metric key reported to Keras.
            If this is None, all Keras logs are reported.
        report_metrics_on: When to report metrics. Must be one of
            the Keras event hooks (less the ``on_``), e.g.
            "train_start" or "predict_end". Defaults to "epoch_end".
        checkpoint_on: When to save checkpoints. Must be one of the Keras event hooks
            (less the ``on_``), e.g. "train_start" or "predict_end". Defaults to
            "epoch_end".
    rf   ry   c                 H    t           j                            ||           dS )r{   ry   Nraytrainreportr|   s      r$   r}   z4ReportCheckpointCallback._save_and_report_checkpoint   s%     		Z88888r&   c                 H    t           j                            |d           dS )r   Nr   r   r   s     r$   r   z(ReportCheckpointCallback._report_metrics   s#    	T22222r&   N)r[   r\   r]   r^   r   r	   r}   r   ro   r&   r$   r   r      s^         @99)=9 9 9 93t 3 3 3 3 3 3r&   r   )r   abcr   typingr   r   r   r   tensorflow.keras.callbacksr   KerasCallbackr   ray.train.tensorflowr	   ray.util.annotationsr
   r   rc   r   ro   r&   r$   <module>r      s0          . . . . . . . . . . . . @ @ @ @ @ @ 



 5 5 5 5 5 5 * * * * * *Z. Z. Z. Z. Z. Z. Z. Z.z<* <* <* <* <*	 <* <* <*~ W)3 )3 )3 )3 )30 )3 )3 )3 )3 )3r&   