
    &`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m	Z	 d dl
mZ d dlmZmZmZmZmZ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 d d	lmZm Z  d d
l!m"Z" d dl#m$Z$m%Z% d dl&m'Z'm(Z(m)Z)m*Z* d dl+m,Z,m-Z- erd dl.Z/d dlm0Z0 d dl1m2Z2  ej3        e4          Z5d Z6e- G d d                      Z7dee7ee7         ef         fdZ8dS )    N)partial)Path)PicklingError)
TYPE_CHECKINGAnyCallableDictListMappingOptionalSequenceTypeUnion)RpcError)StorageContext)DEFAULT_STORAGE_PATH)CheckpointConfig
SyncConfig)	TuneError)is_function_trainableregister_trainable)CombinedStopperFunctionStopperStopperTimeoutStopper)
DeprecatedDeveloperAPI)PlacementGroupFactory)Trialc                    | sdx}}nt          | t                    r| rd}d}nt          | t                    r| x}}nt          | t                    rHt	          |           dk    r/t          d                    t	          |                               | \  }}n/t          d                    t          |                               ||fS )zsValidate ``tune.RunConfig``'s ``log_to_file`` parameter. Return
    validated relative stdout and stderr filenames.Nstdoutstderr   zIf you pass a Sequence to `log_to_file` it has to have a length of 2 (for stdout and stderr, respectively). The Sequence you passed has length {}.zqYou can pass a boolean, a string, or a Sequence of length 2 to `log_to_file`, but you passed something else ({}).)
isinstanceboolstrr   len
ValueErrorformattype)log_to_filestdout_filestderr_files      r/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/experiment/experiment.py_validate_log_to_filer/   *   s      
$((kk	K	&	& 
; 
	K	%	% 
$//kk	K	*	* 
{q  55;VC<L<L5M5M  
 $/ [[AAG[!!B B
 
 	
 ##    c            &          e Zd ZdZh dZeZdddddddddddddddddded	eee	e
f         d
eeeee	eegef         f                  deeeeej        f                  deeeef                  dedeeeeeef         f         df         dedee         ded         deeeef                  deeeef                  dee	dgef                  dee	dgef                  dedee         dedee         dee         f$dZededefd            Zed eee	e
f         fd!            Zed eee	e
f         fd"            Zed#             Zed$ee         fd%            Z e e!d&          d'                         Z"ed$ee         fd(            Z#ed$ee         fd)            Z$ed*             Z%e e!d&          d+                         Z&ed,             Z'ed$eeef         fd-            Z(dS ).
Experimenta  Tracks experiment specifications.

    Implicitly registers the Trainable if needed. The args here take
    the same meaning as the arguments defined `tune.py:run`.

    .. code-block:: python

        experiment_spec = Experiment(
            "my_experiment_name",
            my_func,
            stop={"mean_accuracy": 100},
            config={
                "alpha": tune.grid_search([0.2, 0.4, 0.6]),
                "beta": tune.grid_search([1, 2]),
            },
            resources_per_trial={
                "cpu": 1,
                "gpu": 0
            },
            num_samples=10,
            local_dir="~/ray_results",
            checkpoint_freq=10,
            max_failures=2)

    >   stopnum_samplestime_budget_sN   Fr   )r3   r5   configresources_per_trialr4   storage_pathstorage_filesystemsync_configcheckpoint_configtrial_name_creatortrial_dirname_creatorr+   export_formatsmax_failuresrestore	local_dirnamerunr3   r5   r7   r8   r   r4   r9   r:   zpyarrow.fs.FileSystemr;   r<   r=   r   r>   r+   r?   r@   rA   rB   c                   t          |t                    rt          di |}n|pt                      }t          |          r,|j        rt          d          |j        rt          d          	 t                              |          | _	        nQ# t          $ rD}|j        t          j        j        k    r#t          dt!          j                               |d }~ww xY w|st%          j        |          }|pt(          }|                     ||	|
|          | _        t.                              d| j                    |pi }t          |t                    s t          dt3          |           d          d | _        i }|snt          |t6                    r<d |D             }|rd	 |D             }t          d
| d          t9          | | _        nt          |t                    r|}nt;          |          rft=          j        |          rt=          |          | _        n`t          |t@                    r|| _        nCt          dt3          |           d          t          d| dt3          |           d          |rC| j        r(t9          | j        tC          |                    | _        ntC          |          | _        tE          |          \  }}| j	        ||||||||||f|pg ||rEtG          |          $                                %                                &                                nd | j        d}|| _'        d S )Nz'checkpoint_at_end' cannot be used with a function trainable. You should include one last call to `ray.tune.report(metrics=..., checkpoint=...)` at the end of your training loop to get this behavior.z'checkpoint_frequency' cannot be set for a function trainable. You will need to report a checkpoint every `checkpoint_frequency` iterations within your training loop using `ray.tune.report(metrics=..., checkpoint=...)` to get this behavior.a  The Trainable/training function is too large for grpc resource limit. Check that its definition is not implicitly capturing a large array or other object in scope. Tip: use tune.with_parameters() to put large objects in the Ray object store. 
Original exception: )r9   r:   r;   experiment_dir_namezStorageContext on the DRIVER:
z*`Experiment(config)` must be a dict, got: zB. Please convert your search space to a dict before passing it in.c                 <    g | ]}t          |t                    |S  )r$   r   .0ss     r.   
<listcomp>z'Experiment.__init__.<locals>.<listcomp>   s'    JJJ!:a3I3IJAJJJr0   c                 ,    g | ]}t          |          S rH   )r*   rI   s     r.   rL   z'Experiment.__init__.<locals>.<listcomp>   s     7 7 7Qa 7 7 7r0   zIf you pass a list as the `stop` argument to `tune.RunConfig()`, each element must be an instance of `tune.stopper.Stopper`. Got .zaProvided stop object must be either a dict, a function, or a subclass of `ray.tune.Stopper`. Got zInvalid stop criteria: z". Must be a callable or dict. Got )rD   r3   r5   r7   r8   r4   r<   r=   r>   r+   r?   r@   rA   storagerH   )(r$   dictr   r   checkpoint_at_endr(   checkpoint_frequencyr2   register_if_needed_run_identifierr   rpc_coderay_raylet#GRPC_STATUS_CODE_RESOURCE_EXHAUSTEDr   	traceback
format_excr   get_experiment_dir_namer   _storage_context_clsrO   loggerdebugr*   _stopperlistr   callabler   is_valid_functionr   r   r/   r   
expanduserabsoluteas_posixspec)selfrC   rD   r3   r5   r7   r8   r4   r9   r:   r;   r<   r=   r>   r+   r?   r@   rA   rB   estopping_criteriabad_stoppersstopper_typesr,   r-   rf   s                             r.   __init__zExperiment.__init__f   s'   2 '.. 	H 0 E E3D E E 1 G5E5G5G %% 	 2  M   !5  ,  	#-#@#@#E#ED   	 	 	zS[LLLD
 ,5+?+A+AD D   	  	?!9#>>D#;';00%1# $	 1 
 
 	Et|EEFFF2&$'' 	ST&\\ S S S  
  	d## 	JJtJJJL  7 7$ 7 7 7 D3@D D D  
 ,T2DMMd## 	 $d^^ 	066 	 / 5 5D'**  $ =/3Dzz= = =   7$ 7 7)-d7 7 7  
  	>} > /M>-#@#@! ! !/} = =#8#E#E [ '%*#6&!2"4%:'5,2(DKUW((**3355>>@@@QU|!
 
$ 			s   /B 
C?CCrf   c                 :   d|vrt          d          d|v r-|                    di           |d<   |d         |d         d<   |d= d|v r0t          |d         t                    rt	          d
i |d         |d<   d|v r0t          |d         t                    rt          d
i |d         |d<   t          j        |          }|                    d          }	  | ||fi |}n9# t          $ r,}t          dt          j        |           d|           d	}~ww xY w|S )zGenerates an Experiment object from JSON.

        Args:
            name: Name of Experiment.
            spec: JSON configuration of experiment.
        rD   zNo trainable specified!envr7   r;   r<   z>Failed to load the following Tune experiment specification:

 zd.

Please check that the arguments are valid. Experiment creation failed with the following error:
 NrH   )r   getr$   rP   r   r   copydeepcopypop	TypeErrorpppformat)clsrC   rf   	run_valueexprh   s         r.   	from_jsonzExperiment.from_json   sv    5666 D==!XXh33DN$(KDN5!UD  Z]0CT%J%J ","C"CtM/B"C"CD$&&:d;N6OQU+V+V&(8(U(U4@S;T(U(UD$%}T""HHUOO			#dI....CC 	 	 	 &(j&6&6    	     	 
s   
C" "
D,'DD
run_objectc                 X   ddl m} t          |t                    st          ||          r|S t          |t                    st          |          rd}t          |d          r|j        }nt          |d          r+|j        }|dk    rd}n|	                    d          rd}nw|}ntt          |t                    r2t          |d	          r"t          |j        d          r|j        j        }n-t                              d
                    |                     |S t          d          )a  Get Trainable name.

        Args:
            run_object: Trainable to run. If string,
                assumes it is an ID and does not modify it. Otherwise,
                returns a string corresponding to the run_object name.

        Returns:
            A string representing the trainable identifier.

        Raises:
            TuneError: if ``run_object`` passed in is invalid.
        r   DomainDEFAULT_name__name__z<lambda>lambda<funcz(No name detected on trainable. Using {}.z*Improper 'run' - not string nor trainable.)ray.tune.search.sampler}   r$   r&   r*   ra   hasattrr   r   
startswithr   r   r]   warningr)   r   )rv   rz   r}   rC   fn_names        r.   get_trainable_namezExperiment.get_trainable_name  sX    	211111j#&& 	J*Z*H*H 	J
D)) 	JXj-A-A 	JDz7++ X!'Z00 X$-j((#DD'',, #$DD"DD:w//XJ//X JOZ88X
 "/IPPQUVVWWWKHIIIr0   c                 t   ddl m} t          |t                    r|S t          ||          rt                              d           |S |                     |          }	 t          ||           nG# t          t          f$ r3}d} t          |          t          |          dz   |z             dd}~ww xY w|S )a  Registers Trainable or Function at runtime.

        Assumes already registered if run_object is a string.
        Also, does not inspect interface of given run_object.

        Args:
            run_object: Trainable to run. If string,
                assumes it is an ID and does not modify it. Otherwise,
                returns a string corresponding to the run_object name.

        Returns:
            A string representing the trainable identifier.
        r   r|   z0Not registering trainable. Resolving as variant.zOther options: 
-Try reproducing the issue by calling `pickle.dumps(trainable)`. 
-If the error is typing-related, try removing the type annotations and try again. N)r   r}   r$   r&   r]   r   r   r   rs   r   r*   )rv   rz   r}   rC   rh   	extra_msgs         r.   rS   zExperiment.register_if_neededG  s     	211111j#&& 	
F++ 	NNMNNN%%j11
	>tZ0000=) 	> 	> 	>6  $q''#a&&3,233=	> s    A1 1B5.B00B5c                     | j         S N)r_   rg   s    r.   stopperzExperiment.stopperk  s
    }r0   returnc                     | j         j        S r   )rO   experiment_driver_staging_pathr   s    r.   
local_pathzExperiment.local_patho  s    |::r0   zReplaced by `local_path`c                      t          d          )Nz(Use `local_path` instead of `local_dir`.DeprecationWarningr   s    r.   rB   zExperiment.local_dirs  s     !!KLLLr0   c                     | j         j        S r   )rO   experiment_fs_pathr   s    r.   remote_pathzExperiment.remote_pathy  s    |..r0   c                     | j         p| j        S r   )r   r   r   s    r.   pathzExperiment.path}  s    24?2r0   c                 6    | j                             d          S )Nr<   )rf   ro   r   s    r.   r<   zExperiment.checkpoint_config  s    y}}0111r0   c                      t          d          )Nz-Use `local_path` instead of `checkpoint_dir`.r   r   s    r.   checkpoint_dirzExperiment.checkpoint_dir  s     !!PQQQr0   c                     | j         S )z7Returns a string representing the trainable identifier.)rT   r   s    r.   run_identifierzExperiment.run_identifier  s     ##r0   c                 N      fd j                                         D             S )zReturns the spec dict with only the public-facing keys.

        Intended to be used for passing information to callbacks,
        Searchers and Schedulers.
        c                 .    i | ]\  }}|j         v ||S rH   )PUBLIC_KEYS)rJ   kvrg   s      r.   
<dictcomp>z*Experiment.public_spec.<locals>.<dictcomp>  s,    LLLAa4;K6K6K16K6K6Kr0   )rf   itemsr   s   `r.   public_speczExperiment.public_spec  s,     MLLL!2!2LLLLr0   ))r   
__module____qualname____doc__r   r   r\   r&   r   r   r   r   r   r   r%   intfloatdatetime	timedeltar	   r   r   rP   r   r   rl   classmethodry   r   rS   propertyr   r   r   rB   r   r   r<   r   r   r   rH   r0   r.   r2   r2   F   s$        6 ;::K) SWIM+/ &*@D9=EIAEDH!-1!%#'/M M MM 3$&'M
 uWgxg8L/MMNOM  c5(2D&D EFM c3h(M #'#uUC%899:<SS
M M smM %%<=M eJ$456M $E*:D*@$ABM  %Xwin%=>!M"  ('C(@A#M$ %M& !*'M( )M* #+M. C=/M M M M^ $S $ $ $ $ [$L (JE#x2E,F (J (J (J [(JT !E#x2E,F ! ! ! [!F   X ;HSM ; ; ; X; Z*++M M ,+ XM /Xc] / / / X/ 3hsm 3 3 3 X3 2 2 X2 Z*++R R ,+ XR $ $ X$ MT#s(^ M M M XM M Mr0   r2   experimentsc                    | }| g }nMt          | t                    r| g}n4t          |           t          u rd |                                 D             }t          |          t
          u rGt          d |D                       r.t          |          dk    rt          	                    d           n"t          d                    |                     |S )a%  Produces a list of Experiment objects.

    Converts input from dict, single experiment, or list of
    experiments to list of experiments. If input is None,
    will return an empty list.

    Arguments:
        experiments: Experiments to run.

    Returns:
        List of experiments.
    Nc                 J    g | ] \  }}t                               ||          !S rH   )r2   ry   )rJ   rC   rf   s      r.   rL   z/_convert_to_experiment_list.<locals>.<listcomp>  s9     
 
 
1;tJ  t,,
 
 
r0   c              3   @   K   | ]}t          |t                    V  d S r   )r$   r2   )rJ   rx   s     r.   	<genexpr>z._convert_to_experiment_list.<locals>.<genexpr>  s,      %V%Vcjj&A&A%V%V%V%V%V%Vr0   r6   zeRunning with multiple concurrent experiments. All experiments will be using the same SearchAlgorithm.zInvalid argument: {})r$   r2   r*   rP   r   r`   allr'   r]   infor   r)   )r   exp_lists     r.   _convert_to_experiment_listr     s     H 	K	,	, 
=	k		d	"	"
 
?J?P?P?R?R
 
 

 H~~#%V%VX%V%V%V"V"Vx==1KKJ  
 .55kBBCCCOr0   )9rp   r   loggingpprintrt   rY   	functoolsr   pathlibr   pickler   typingr   r   r   r	   r
   r   r   r   r   r   rV   ray.exceptionsr   ray.train._internal.storager   ray.train.constantsr   ray.tuner   r   ray.tune.errorr   ray.tune.registryr   r   ray.tune.stopperr   r   r   r   ray.util.annotationsr   r   
pyarrow.fspyarrowr   ray.tune.experimentr   	getLoggerr   r]   r/   r2   r   rH   r0   r.   <module>r      s|                                                             


 # # # # # # 6 6 6 6 6 6 4 4 4 4 4 4 1 1 1 1 1 1 1 1 $ $ $ $ $ $ G G G G G G G G V V V V V V V V V V V V 9 9 9 9 9 9 9 9 *......)))))) 
	8	$	$$ $ $8 PM PM PM PM PM PM PM PMf
#U:tJ?OQU3U-V # # # # # #r0   