
    &`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Zd dl	m
Z
 d dlmZ d dlmZ d dlmZ  G d de          Z G d	 d
e
j                  Zd Z	 	 	 	 	 	 ddededededededededefdZdS )    N)Counter)tune)safe_write_to_results_json)
Checkpoint)Callbackc                       e Zd Zd Zd ZdS )ProgressCallbackc                 "    d| _         d| _        d S )Nr   <   )last_updateupdate_intervalselfs    t/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/utils/release_test_util.py__init__zProgressCallback.__init__   s    !    c                     t          j                     | j        z
  | j        k    rVt          j                     }||t          t	          d |D                                 d}t          |d           || _        d S d S )Nc                     g | ]	}|j         
S  status.0trials     r   
<listcomp>z0ProgressCallback.on_step_end.<locals>.<listcomp>   s    -O-O-Ouel-O-O-Or   )r   	iterationtrial_statesz/tmp/release_test_out.json)timer   r   dictr   r   )r   r   trialskwargsnowresults         r   on_step_endzProgressCallback.on_step_end   s    9;;))D,@@@)++C"& $W-O-O-O-O-O%P%P Q Q F
 'v/KLLL"D A@r   N)__name__
__module____qualname__r   r$   r   r   r   r	   r	      s2        " " "
# 
# 
# 
# 
#r   r	   c                   <     e Zd Z fdZd Zd Zd Zd Zd Z xZ	S )TestDurableTrainablec                 p    |                                    t          t          |           j        |i | d S N)	setup_envsuperr)   r   )r   argsr!   	__class__s      r   r   zTestDurableTrainable.__init__#   s<    2"D))2DCFCCCCCr   c                     d S r+   r   r   s    r   r,   zTestDurableTrainable.setup_env(       r   c                     t          |d                   | _        |d         | _        |d         | _        |d         | _        |d         | _        | j        dz  | _        d| _        d S )N	num_iters
sleep_timescorecheckpoint_iterscheckpoint_size_b   r   )int
_num_iters_sleep_time_score_checkpoint_iters_checkpoint_size_b_checkpoint_num_items_iter)r   configs     r   setupzTestDurableTrainable.setup+   se    f[122!,/Wo!'(:!;"()<"=%)%<%A"


r   c                     | j         dk    rt          j        | j                   t	          | j         | j        z             }| j         | j        k    rd|d<   | xj         dz  c_         |S )Nr   )r5   Tdone   )r@   r   sleepr;   r   r<   r:   )r   ress     r   stepzTestDurableTrainable.step6   se    :>>Jt'(((dk1222:((CK

a


r   c                    t           j                            |d          }t          j                            dd| j                  }t          |d          5 }t          j	        ||           d d d            d S # 1 swxY w Y   d S )Nz
bogus.ckptr   rE   sizewb)
ospathjoinnprandomuniformr?   openpickledump)r   tmp_checkpoint_dircheckpoint_filecheckpoint_datafps        r   save_checkpointz$TestDurableTrainable.save_checkpointB   s    ',,'9<HH)++Aqt7Q+RR/4(( 	-BK,,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s   A;;A?A?c                     d S r+   r   )r   
checkpoints     r   load_checkpointz$TestDurableTrainable.load_checkpointH   r1   r   )
r%   r&   r'   r   r,   rB   rH   rZ   r]   __classcell__)r/   s   @r   r)   r)   "   s        D D D D D
  	 	 	
 
 
- - -      r   r)   c           	         t          | d                   }| d         }| d         }| d         }| d         }|dz  }| d         }t          |          D ]0}d||z   i}	|dk    r|dk    r||z  dk    rt          j                    5 }
t          |          D ]}t          j                            |
d	| d
          }t          j        	                    dd|          }t          |d          5 }t          j        ||           d d d            n# 1 swxY w Y   t          j        |	t          j        |
                     d d d            n# 1 swxY w Y   nt          j        |	           t#          j        |           2d S )Nr3   r4   r5   r6   r7   r8   checkpoint_num_filesr   bogus_z.ckptrE   rJ   rL   )r\   )r9   rangetempfileTemporaryDirectoryrM   rN   rO   rP   rQ   rR   rS   rT   rU   r   reportr   from_directoryr   rF   )rA   r3   r4   r5   r6   r7   checkpoint_num_itemsr`   imetricstmpdirrW   rX   rY   s                 r   function_trainablerk   L   s.   F;'((I%J7OE0123,1!"899  AI&!!!A%%$$)),.. S&344 9 9A&(gll6;LA;L;L;L&M&MO&(i&7&71CW&7&X&XOot44 9OR8889 9 9 9 9 9 9 9 9 9 9 9 9 9 9G
0I&0Q0QRRRRS S S S S S S S S S S S S S S K   
:# s7   A(E,D	EDED,EE	E	rE   ,  namenum_samplesresults_per_secondtrial_length_smax_runtimecheckpoint_freq_sr7   r`   returnc                 P   d|v o=|d         o5|d                              d          p|d                              d          }	d|z  }
t          ||
z            }d}|dk    rt          ||
z            }t          j        dd          ||
|||d}t	          d	|            d
dd}|                    |           t          }|	rt          }||d<   t          j	                    }t          j
        |f||dd|}t          j	                    |z
  }|t          t          d |j        D                                 t          j                    d}t          j                            dd          }t#          |d          5 }t%          j        ||           d d d            n# 1 swxY w Y   ||k    }|s9t	          d|  d|dd|dd|                                  d|dd|dd           n8t	          d|  d|dd|dd|                                  d|dd|dd           |S ) Nstorage_pathzs3://zgs://g      ?rm   r   g        )r5   r3   r4   r6   r7   r`   z Starting benchmark with config: T   )reuse_actorsverbosecheckpoint_freqF)rA   ro   raise_on_failed_trialc                     g | ]	}|j         
S r   r   r   s     r   r   z"timed_tune_run.<locals>.<listcomp>   s    %P%P%Puel%P%P%Pr   )
time_takenr   r   TEST_OUTPUT_JSONz/tmp/tune_test.jsonwtzThe z test took z.2fz' seconds, but should not have exceeded z% seconds. Test failed. 

--- FAILED: z ::: z > z ---z' seconds, which is below the budget of z) seconds. Test successful. 

--- PASSED: z <= )
startswithr9   r   rR   printupdaterk   r)   r   	monotonicrunr   r   r    rM   environgetrS   jsonrU   upper)rn   ro   rp   rq   rr   rs   r7   r`   tune_kwargsdurabler4   r3   r6   rA   
run_kwargs_train
start_timeanalysisr}   r#   test_output_jsonfsuccesss                          r   timed_tune_runr   j   s`    	+% 	
'	
 '227;; ?>*55g>>  ))JNZ/00IA0:=>> c3'' ,. 4 F 

5V
5
5666"&155Jk"""F 9%(8
$%!!Jx#	 
  H !!J.J !W%P%P%P%P%PQQRRy{{ F z~~&8:OPP		%	% 	&!               K'G 
84 8 8JM 8 8(I8 8::<<8 8 78 8 #.78 8 8	
 	
 	
 	
 	94 9 9JD 9 9&1A9 9  ::<<9 9 8	9 9 $/8	9 9 9	
 	
 	
 Ns    F""F&)F&)rE   rE   rl   rm   r   rE   )r   rM   rT   rc   r   collectionsr   numpyrP   rayr   ray._private.test_utilsr   ray.tuner   ray.tune.callbackr   r	   	Trainabler)   rk   strr9   boolr   r   r   r   <module>r      s    				                    > > > > > >       & & & & & &# # # # #x # # #$' ' ' ' '4> ' ' 'T  B   !T T
TT T 	T
 T T T T 
T T T T T Tr   