
    &`i                        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 d dlm	Z	 d Z
 G d de          Z G d d	ej                  Zed
k    r3 e j                    Ze                    ddd           e                                \  ZZ edddddddddddddd            edd          Z ej        e ej        ddej        rd nd!ieg ej        d d"          #           ej        d$d%d&'           ej        d(d)           ej        d(d)          d*+          Z ed,d-d           e                                Z e !                                Z" ed.d/dd0e"j#         d1e"j$         dd2d           dS dS )3    N)tune)Callback)SHOULD_CHECKPOINTc                 R    t          j        d           d|| z  dz  z   dz  |dz  z   S )N皙?d   )timesleepstepwidthheights      /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/examples/custom_checkpointing_with_callback.pyevaluation_fnr      s1    JsOOO%$,$$"-<<    c                   0    e Zd ZdZddddedefdZd ZdS )	SmartCheckpointCallbacka  Custom callback that triggers checkpointing by updating the result dict.

    This callback demonstrates checkpointing logic beyond
    simple periodic checkpointing. It checkpoints based on performance improvements
    or when the loss becomes unstable.

    Args:
        checkpoint_on_improvement: Checkpoint when loss improves significantly
        checkpoint_on_instability: Checkpoint when loss becomes unstable
    Tcheckpoint_on_improvementcheckpoint_on_instabilityr   r   c                >    || _         || _        i | _        i | _        d S )N)r   r   best_loss_per_trialrecent_losses_per_trial)selfr   r   s      r   __init__z SmartCheckpointCallback.__init__#   s+     *C&)B&#% ')$$$r   c                    |j         }|                    dt          d                    }|                    dd          }|| j        vr!t          d          | j        |<   g | j        |<   d}	d}
|dk    r|dz  dk    rd}	d	| }
| j        r5|| j        |         d
z  k     r!d}	d|dd| j        |         d}
|| j        |<   | j        r|dk    r| j        |                             |           t                    dk    r	                    d           t                    dk    r,t          fdD                       dz  }|dk    rd}	d|d}
nJ| j        |                             |           t                    dk    r	                    d           |	r$t          d| d| d|
            d|t          <   dS dS )a@  Called after receiving a result from the trainable.

        This hook implements intelligent checkpointing logic:
        1. Checkpoint when we see significant improvement
        2. Checkpoint when loss becomes unstable (variance increases)
        3. Always checkpoint at specific milestones (every 10 steps)
        	mean_lossinf
iterationsr   F 
   Tzmilestone at step g?zsignificant improvement: z.4fz <    c              3   H   K   | ]}|t                    d z  z
  dz  V  dS )r#      N)sum).0xrecent_lossess     r   	<genexpr>z:SmartCheckpointCallback.on_trial_result.<locals>.<genexpr>Y   s:      QQaS//!339QQQQQQr   r   zinstability detected: variance=z)Callback requesting checkpoint for trial z	 at step z: N)trial_idgetfloatr   r   r   r   appendlenpopr&   printr   )r   	iterationtrialstrialresultinfor+   current_losscurrent_stepshould_checkpointreasonvariancer)   s               @r   on_trial_resultz'SmartCheckpointCallback.on_trial_result.   s[    >zz+uU||<<zz,22 433316uD$X.57D(2! !r 1Q 6 6 $8,88F ) 	Bt7ACGGG$(!r\rrr$JbckJlrrr5A(2 ) 	%lQ.>.> 8BM  ...=!!A%%!!!$$$=!!Q&&QQQQ=QQQQQTUU  c>>(,%MxMMMF !8BM  ...=!!A%%!!!$$$ 	-gHgg|gg_egg   )-F$%%%		- 	-r   N)__name__
__module____qualname____doc__boolr   r<    r   r   r   r      sg        	 	 +/*.		* 	* 	* $(	* $(		* 	* 	* 	*;- ;- ;- ;- ;-r   r   c                   *    e Zd ZdZd Zd Zd Zd ZdS )OptimizationTrainablezKA simple trainable that demonstrates automatic checkpointing with callbacksc                 H    d| _         |d         | _        |d         | _        dS )zInitialize the trainabler   r   r   N)r8   r   r   )r   configs     r   setupzOptimizationTrainable.setupo   s%    G_
X&r   c                     t          | j        | j        | j                  }| xj        dz  c_        | j        || j        dS )zPerform one step of training   )r    r   r   )r   r8   r   r   )r   intermediate_scores     r   r   zOptimizationTrainable.stepu   sO    *4+<dj$+VVQ ++%
 
 	
r   c                    t           j                            |d          }t          |d          5 }t	          j        | j        | j        | j        d|           ddd           n# 1 swxY w Y   t          d| j                    dS )zfSave checkpoint

        Called automatically by Tune when SHOULD_CHECKPOINT is in the result
        checkpoint.jsonwr   NzCheckpoint saved at step )
ospathjoinopenjsondumpr8   r   r   r1   )r   checkpoint_dircheckpoint_pathfs       r   save_checkpointz%OptimizationTrainable.save_checkpoint   s    
 ',,~7HII/3'' 	1I*TZ4;WW  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 	=$*;==>>>>>s   )A&&A*-A*c                 :   t           j                            |d          }t          |d          5 }t	          j        |          }ddd           n# 1 swxY w Y   |d         | _        |d         | _        |d         | _        t          d| j                    dS )zALoad checkpoint - called automatically by Tune during restorationrL   rNr   r   r   zCheckpoint loaded from step )
rN   rO   rP   rQ   rR   loadr8   r   r   r1   )r   
checkpointrU   rV   states        r   load_checkpointz%OptimizationTrainable.load_checkpoint   s    ',,z3DEE/3'' 	!1IaLLE	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!!&M7^
Ho@T->@@AAAAAs   AAAN)r=   r>   r?   r@   rG   r   rW   r]   rB   r   r   rD   rD   l   s[        UU' ' '	
 	
 	
? ? ?B B B B Br   rD   __main__z--smoke-test
store_truezFinish quickly for testing)actionhelpz<============================================================zGRay Tune Example: Smart Checkpointing with custom SHOULD_CHECKPOINT keyr!   zLThis example demonstrates how to set the SHOULD_CHECKPOINT key in a callbackzBto implement intelligent checkpointing based on training progress.zKey features:zK- Callback-driven checkpointing by setting result[SHOULD_CHECKPOINT] = Truez3- Checkpoints triggered by performance improvementsz.- Milestone-based checkpointing every 10 stepsz8- Instability detection (high variance in recent losses)z4- Automatic checkpoint save/load via class trainable
)sepTr   smart_checkpoint_testtraining_iterationrI      )checkpoint_frequencycheckpoint_at_end)namestop	callbackscheckpoint_configr   min   )metricmodenum_samplesr"   r   )r   r   )
run_configtune_configparam_spacez:Starting hyperparameter tuning with smart checkpointing...z8Watch for checkpoint messages triggered by the callback!z=
============================================================RESULTSzBest hyperparameters: zBest checkpoint: z=The checkpoints were triggered by the SmartCheckpointCallback)%argparserR   rN   r
   rayr   ray.tuner   ray.tune.resultr   r   r   	TrainablerD   r=   ArgumentParserparseradd_argumentparse_known_argsargs_r1   checkpoint_callbackTuner	RunConfig
smoke_testCheckpointConfig
TuneConfigrandint
loguniformtunerfitresultsget_best_resultbest_resultrF   r[   rB   r   r   <module>r      s  
   				              - - - - - -= = =
R- R- R- R- R-h R- R- R-j)B )B )B )B )BDN )B )B )BX z$X$&&F
|2N     %%''GD!	EQ
VL
U=8B>   $ 21"&$   DJ!4>(&T_(D"E*+3d3%&"&  	
 	
 	
 $DO
 
 
 "T\"c**%dob#..
 
#  E. 
EDB    iikkG))++K	E5!3554K244
G	 	 	 	 	 	y r   