o
    `۷iU                     @   s   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	 d dl
mZ d dlmZ ddd	Zd
d Zi adede jfddZdS )    N)CheckpointConfig)	TuneError)Trial)json_to_resources)TuneFunctionEncoder)SafeFallbackEncoderc                 K   s&  | r
| d(i |}nt jd(i |}|jddtdd |jddtjdd |jddtjd	d |jd
dtdd |jddtdd |jddtdd |jdddd |jddtdd |jddtdd |jdddd |jddtd d |jd!d"td#d |jd$dtjd%d |jd&dtd'd |S ))zReturns a base argument parser for the ray.tune tool.

    Args:
        parser_creator: A constructor for the parser class.
        kwargs: Non-positional args to be passed into the
            parser class constructor.
    z--runNzThe algorithm or model to train. This may refer to the name of a built-on algorithm (e.g. RLlib's DQN or PPO), or a user-defined trainable function or class registered in the tune registry.)defaulttypehelpz--stopz{}zThe stopping criteria, specified in JSON. The keys may be any field returned by 'train()' e.g. '{"time_total_s": 600, "training_iteration": 100000}' to stop after 600 seconds or 100k iterations, whichever is reached first.z--configzLAlgorithm-specific configuration (e.g. env, hyperparams), specified in JSON.z--resources-per-trialzOverride the machine resources to allocate per trial, e.g. '{"cpu": 64, "gpu": 8}'. Note that GPUs will not be assigned unless you specify them here. For RLlib, you probably want to leave this alone and use RLlib configs to control parallelism.z--num-samples   z%Number of times to repeat each trial.z--checkpoint-freqr   z`How many training iterations between checkpoints. A value of 0 (default) disables checkpointing.z--checkpoint-at-end
store_truezEWhether to checkpoint at the end of the experiment. Default is False.)actionr
   z--keep-checkpoints-numz]Number of best checkpoints to keep. Others get deleted. Default (None) keeps all checkpoints.z--checkpoint-score-attrtraining_iterationzSpecifies by which attribute to rank the best checkpoint. Default is increasing order. If attribute starts with min- it will rank attribute in decreasing order. Example: min-validation_lossz--export-formatszList of formats that exported at the end of the experiment. Default is None. For RLlib, 'checkpoint' and 'model' are supported for TensorFlow policy graphs.)r   r
   z--max-failures   zsTry to recover a trial from its last checkpoint at least this many times. Only applies if checkpointing is enabled.z--schedulerFIFOzGFIFO (default), MedianStopping, AsyncHyperBand, HyperBand, or HyperOpt.z--scheduler-configz(Config options to pass to the scheduler.z	--restorez+If specified, restore from this checkpoint. )argparseArgumentParseradd_argumentstrjsonloadsr   int)parser_creatorkwargsparserr   r   W/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/tune/experiment/config_parser.py_make_parser   s   					r   c              	   C   s   g }|   D ]Q\}}d|v rtd||du rqt|tr!|r-|d|dd t|tr8|| qt|tr>qt|rM|t	j
|td q|t	j
|td q|S )z9Converts configuration to a command line argument format.-zUse '_' instead of '-' in `{}`Nz--{}_)cls)items
ValueErrorformat
isinstanceboolappendreplacer   callabler   dumpsr   r   )configargvkvr   r   r   _to_argv   s    

r.   specr   c                 K   s   |   } | dd}z|t| \}}W n ty!   td| w |r(||d< | dt }td| d | di | di || d	g | d
| d| d| d|j	| dd|S )a  Creates a Trial object from parsing the spec.

    Args:
        spec: A resolved experiment specification. Arguments should
            The args here should correspond to the command line flags
            in ray.tune.experiment.config_parser.
        parser: An argument parser object from
            make_parser.
        trial_kwargs: Extra keyword arguments used in instantiating the Trial.

    Returns:
        A trial object with corresponding parameters to the specification.
    resources_per_trialNz%Error parsing args, see above messageplacement_group_factorycheckpoint_configrunr*   stopexport_formatsrestoretrial_name_creatortrial_dirname_creatorlog_to_filestorage)trainable_namer*   stopping_criterionr2   r5   restore_pathr7   r8   r9   max_failuresr:   r   )
copypopparse_known_argsr.   
SystemExitr   getr   r   r>   )r/   r   trial_kwargs	resourcesargsr   r2   r   r   r   _create_trial_from_spec   s2   



rG   )N)r   r   ray.tuner   ray.tune.errorr   ray.tune.experimentr   ray.tune.resourcesr   ray.tune.utils.serializationr   ray.tune.utils.utilr   r   r.   _cached_pgfdictr   rG   r   r   r   r   <module>   s     
z