o
    `۷iO                     @   s  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	Z	d dl	m
Z
 d dlmZmZmZmZ d dlmZ d dlmZ G dd de
jZed	kre  Zejd
ddd e \ZZe	jdd ejdddd ee edddd g dddZG dd de
j Z!e! Z"e
j#ee
j$de"de
j%dddd d!e
j&edd"ddd#e
'd$de
'd$ddd%Z(e() Z*e*+ Z,e,j-Z.dS dS )&    N)datasets)tune)ConvNetget_data_loaders	test_func
train_func)PopulationBasedTraining)validate_save_restorec                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )PytorchTrainablea  Train a Pytorch ConvNet with Trainable and PopulationBasedTraining
       scheduler. The example reuse some of the functions in mnist_pytorch,
       and is a good demo for how to add the tuning function without
       changing the original training code.
    c                 C   sB   t  \| _| _t | _tj| j |dd|ddd| _	d S )Nlrg{Gz?momentum?r   r   )
r   train_loadertest_loaderr   modeloptimSGD
parametersget	optimizer)selfconfig r   [/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/tune/examples/pbt_convnet_example.pysetup%   s   

zPytorchTrainable.setupc                 C   s(   t | j| j| j t| j| j}d|iS )Nmean_accuracy)r   r   r   r   r   r   )r   accr   r   r   step-   s   zPytorchTrainable.stepc                 C   s$   t j|d}t| j | d S Nz	model.pth)ospathjointorchsaver   
state_dictr   checkpoint_dircheckpoint_pathr   r   r   save_checkpoint2      z PytorchTrainable.save_checkpointc                 C   s$   t j|d}| jt| d S r   )r    r!   r"   r   load_state_dictr#   loadr&   r   r   r   load_checkpoint6   r*   z PytorchTrainable.load_checkpointc                 C   s@   | j jD ]}d|v r|d |d< d|v r|d |d< q|| _dS )Nr   r   T)r   param_groupsr   )r   
new_configparam_groupr   r   r   reset_config:   s   zPytorchTrainable.reset_configN)	__name__
__module____qualname____doc__r   r   r)   r-   r1   r   r   r   r   r
      s    r
   __main__z--smoke-test
store_truezFinish quickly for testing)actionhelp   )num_cpusz~/dataT)traindownloadtraining_iteration   c                   C   s   t jddS )Ng-C6?   )nprandomuniformr   r   r   r   <lambda>X   s    rD   )g?r   gGz?r   )	time_attrperturbation_intervalhyperparam_mutationsc                   @   s$   e Zd Zdd Zdd Zdd ZdS )CustomStopperc                 C   s
   d| _ d S )NFshould_stopr   r   r   r   __init__`   s   
zCustomStopper.__init__c                 C   s8   t jrdnd}| js|d dkrd| _| jp|d |kS )Nr?   d   r   gQ?Tr>   )args
smoke_testrJ   )r   trial_idresultmax_iterr   r   r   __call__c   s   zCustomStopper.__call__c                 C   s   | j S )NrI   rK   r   r   r   stop_alli   s   zCustomStopper.stop_allN)r2   r3   r4   rL   rS   rT   r   r   r   r   rH   _   s    rH   pbt_testr@   r      )checkpoint_score_attributecheckpoint_frequencynum_to_keep)namestopverbosecheckpoint_configmax)	schedulermetricmodenum_samplesreuse_actorsgMbP?)
run_configtune_configparam_space)/argparser    numpyrA   r#   torch.optimr   torchvisionr   rayr   ray.tune.examples.mnist_pytorchr   r   r   r   ray.tune.schedulersr   ray.tune.utilsr	   	Trainabler
   r2   ArgumentParserparseradd_argumentparse_known_argsrN   _initMNISTr_   StopperrH   stopperTuner	RunConfigCheckpointConfig
TuneConfigrC   tunerfitresultsget_best_resultbest_result
checkpointbest_checkpointr   r   r   r   <module>   sr   (



