
    &`iz                         d dl 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 ed	           G d
 de                      ZdS )    N)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 Air'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/air/integrations/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"   r/   r0   s      r#   _handlez_Callback._handle,   s    !!r%   Nc                 H    d| j         v r|                     |d           d S d S )Nr   r-   r3   r"   epochr/   s      r#   on_epoch_beginz_Callback.on_epoch_begin/   1    DH$$LL}----- %$r%   c                 H    d| j         v r|                     |d           d S d S )Nr   r5   r6   s      r#   on_epoch_endz_Callback.on_epoch_end3   1    $(""LL{+++++ #"r%   c                 H    d| j         v r|                     |d           d S d S )Nr   r5   r"   batchr/   s      r#   on_train_batch_beginz_Callback.on_train_batch_begin7   2    $(**LL233333 +*r%   c                 H    d| j         v r|                     |d           d S d S )Nr   r5   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   r5   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   r5   r>   s      r#   on_test_batch_endz_Callback.on_test_batch_endC   s2    tx''LL/00000 ('r%   c                 H    d| j         v r|                     |d           d S d S )Nr   r5   r>   s      r#   on_predict_batch_beginz _Callback.on_predict_batch_beginG   s2     DH,,LL455555 -,r%   c                 H    d| j         v r|                     |d           d S d S )Nr   r5   r>   s      r#   on_predict_batch_endz_Callback.on_predict_batch_endK   rA   r%   c                 H    d| j         v r|                     |d           d S d S )Nr   r5   r"   r/   s     r#   on_train_beginz_Callback.on_train_beginO   r9   r%   c                 H    d| j         v r|                     |d           d S d S )Nr   r5   rM   s     r#   on_train_endz_Callback.on_train_endS   r<   r%   c                 H    d| j         v r|                     |d           d S d S )Nr   r5   rM   s     r#   on_test_beginz_Callback.on_test_beginW   s1    48##LL|,,,,, $#r%   c                 H    d| j         v r|                     |d           d S d S )Nr   r5   rM   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   r5   rM   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   r5   rM   s     r#   on_predict_endz_Callback.on_predict_endc   r9   r%   )r   r   )__name__
__module____qualname____doc__r   r   strr   r'   r   r3   r8   r;   r@   rC   rE   rG   rI   rK   rN   rP   rR   rT   rV   rX   __classcell__r.   s   @r#   r   r      s       //  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   alpha)	stabilityc                        e Zd 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
dedefdZ xZ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:
        .. code-block:: 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".
    r   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!ReportCheckpointCallback.__init__   s     mS)) 	,*OM'-- 	4!2 3#m&778899B)6->r%   r/   r0   c                 ^   || j         v s|| j        v sJ |                     |          }|| j         v }|rWt          j        | j                  }t          j                            ||           t          j
        |j        d           d S t          j                            |d            d S )N)
checkpointT)ignore_errors)ri   rj   _get_reported_metricsr   
from_modelmodelraytrainreportshutilrmtreepath)r"   r/   r0   rf   should_checkpointrm   s         r#   r3   z ReportCheckpointCallback._handle   s    t***dd6M.M.M.MM,,T22 D$77 	7-8DDJIW<<<M*/>>>>>>IW66666r%   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  r|   )r    metricr/   s     r#   
<dictcomp>zBReportCheckpointCallback._get_reported_metrics.<locals>.<dictcomp>   s    QQQVQQQr%   c                 (    i | ]\  }}||         S r|   r|   )r    keyr}   r/   s      r#   r~   zBReportCheckpointCallback._get_reported_metrics.<locals>.<dictcomp>   s0          &1c6T&\     r%   )r(   rk   typer]   r)   dictitems)r"   r/   reported_metricss    ` r#   ro   z.ReportCheckpointCallback._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%   )r   r   N)rY   rZ   r[   r\   r   r]   r   r   r   r'   r3   ro   r^   r_   s   @r#   rc   rc   h   s         D 0;3>CG	   S$s)^,  !d3i0  %T#YS#X >?@	           $7D 7 7 7 7 7 $  4                r%   rc   )ru   typingr   r   r   r   tensorflow.keras.callbacksr   KerasCallbackrr   ray.train.tensorflowr   ray.util.annotationsr	   r   rc   r|   r%   r#   <module>r      s     . . . . . . . . . . . . @ @ @ @ @ @ 



 5 5 5 5 5 5 * * * * * *Z. Z. Z. Z. Z. Z. Z. Z.z WP  P  P  P  P y P  P  P  P  P r%   